2 * MMX optimized DSP utils
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
25 #include "libavutil/cpu.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavcodec/hpeldsp.h"
28 #include "dsputil_mmx.h"
30 void ff_put_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
31 ptrdiff_t line_size, int h);
32 void ff_put_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
33 ptrdiff_t line_size, int h);
34 void ff_put_pixels16_x2_mmxext(uint8_t *block, const uint8_t *pixels,
35 ptrdiff_t line_size, int h);
36 void ff_put_pixels16_x2_3dnow(uint8_t *block, const uint8_t *pixels,
37 ptrdiff_t line_size, int h);
38 void ff_put_no_rnd_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
39 ptrdiff_t line_size, int h);
40 void ff_put_no_rnd_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
41 ptrdiff_t line_size, int h);
42 void ff_put_no_rnd_pixels8_x2_exact_mmxext(uint8_t *block,
43 const uint8_t *pixels,
44 ptrdiff_t line_size, int h);
45 void ff_put_no_rnd_pixels8_x2_exact_3dnow(uint8_t *block,
46 const uint8_t *pixels,
47 ptrdiff_t line_size, int h);
48 void ff_put_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
49 ptrdiff_t line_size, int h);
50 void ff_put_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
51 ptrdiff_t line_size, int h);
52 void ff_put_no_rnd_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
53 ptrdiff_t line_size, int h);
54 void ff_put_no_rnd_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
55 ptrdiff_t line_size, int h);
56 void ff_put_no_rnd_pixels8_y2_exact_mmxext(uint8_t *block,
57 const uint8_t *pixels,
58 ptrdiff_t line_size, int h);
59 void ff_put_no_rnd_pixels8_y2_exact_3dnow(uint8_t *block,
60 const uint8_t *pixels,
61 ptrdiff_t line_size, int h);
62 void ff_avg_pixels8_3dnow(uint8_t *block, const uint8_t *pixels,
63 ptrdiff_t line_size, int h);
64 void ff_avg_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
65 ptrdiff_t line_size, int h);
66 void ff_avg_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
67 ptrdiff_t line_size, int h);
68 void ff_avg_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
69 ptrdiff_t line_size, int h);
70 void ff_avg_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
71 ptrdiff_t line_size, int h);
72 void ff_avg_pixels8_xy2_mmxext(uint8_t *block, const uint8_t *pixels,
73 ptrdiff_t line_size, int h);
74 void ff_avg_pixels8_xy2_3dnow(uint8_t *block, const uint8_t *pixels,
75 ptrdiff_t line_size, int h);
80 #define JUMPALIGN() __asm__ volatile (".p2align 3"::)
81 #define MOVQ_ZERO(regd) __asm__ volatile ("pxor %%"#regd", %%"#regd ::)
83 #define MOVQ_BFE(regd) \
85 "pcmpeqd %%"#regd", %%"#regd" \n\t" \
86 "paddb %%"#regd", %%"#regd" \n\t" ::)
89 #define MOVQ_BONE(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_bone))
90 #define MOVQ_WTWO(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_wtwo))
92 // for shared library it's better to use this way for accessing constants
94 #define MOVQ_BONE(regd) \
96 "pcmpeqd %%"#regd", %%"#regd" \n\t" \
97 "psrlw $15, %%"#regd" \n\t" \
98 "packuswb %%"#regd", %%"#regd" \n\t" ::)
100 #define MOVQ_WTWO(regd) \
102 "pcmpeqd %%"#regd", %%"#regd" \n\t" \
103 "psrlw $15, %%"#regd" \n\t" \
104 "psllw $1, %%"#regd" \n\t"::)
108 // using regr as temporary and for the output result
109 // first argument is unmodifed and second is trashed
110 // regfe is supposed to contain 0xfefefefefefefefe
111 #define PAVGB_MMX_NO_RND(rega, regb, regr, regfe) \
112 "movq "#rega", "#regr" \n\t" \
113 "pand "#regb", "#regr" \n\t" \
114 "pxor "#rega", "#regb" \n\t" \
115 "pand "#regfe", "#regb" \n\t" \
116 "psrlq $1, "#regb" \n\t" \
117 "paddb "#regb", "#regr" \n\t"
119 #define PAVGB_MMX(rega, regb, regr, regfe) \
120 "movq "#rega", "#regr" \n\t" \
121 "por "#regb", "#regr" \n\t" \
122 "pxor "#rega", "#regb" \n\t" \
123 "pand "#regfe", "#regb" \n\t" \
124 "psrlq $1, "#regb" \n\t" \
125 "psubb "#regb", "#regr" \n\t"
127 // mm6 is supposed to contain 0xfefefefefefefefe
128 #define PAVGBP_MMX_NO_RND(rega, regb, regr, regc, regd, regp) \
129 "movq "#rega", "#regr" \n\t" \
130 "movq "#regc", "#regp" \n\t" \
131 "pand "#regb", "#regr" \n\t" \
132 "pand "#regd", "#regp" \n\t" \
133 "pxor "#rega", "#regb" \n\t" \
134 "pxor "#regc", "#regd" \n\t" \
135 "pand %%mm6, "#regb" \n\t" \
136 "pand %%mm6, "#regd" \n\t" \
137 "psrlq $1, "#regb" \n\t" \
138 "psrlq $1, "#regd" \n\t" \
139 "paddb "#regb", "#regr" \n\t" \
140 "paddb "#regd", "#regp" \n\t"
142 #define PAVGBP_MMX(rega, regb, regr, regc, regd, regp) \
143 "movq "#rega", "#regr" \n\t" \
144 "movq "#regc", "#regp" \n\t" \
145 "por "#regb", "#regr" \n\t" \
146 "por "#regd", "#regp" \n\t" \
147 "pxor "#rega", "#regb" \n\t" \
148 "pxor "#regc", "#regd" \n\t" \
149 "pand %%mm6, "#regb" \n\t" \
150 "pand %%mm6, "#regd" \n\t" \
151 "psrlq $1, "#regd" \n\t" \
152 "psrlq $1, "#regb" \n\t" \
153 "psubb "#regb", "#regr" \n\t" \
154 "psubb "#regd", "#regp" \n\t"
156 /***********************************/
157 /* MMX no rounding */
159 #define DEF(x, y) x ## _no_rnd_ ## y ## _mmx
160 #define SET_RND MOVQ_WONE
161 #define PAVGBP(a, b, c, d, e, f) PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
162 #define PAVGB(a, b, c, e) PAVGB_MMX_NO_RND(a, b, c, e)
163 #define OP_AVG(a, b, c, e) PAVGB_MMX(a, b, c, e)
165 #include "hpeldsp_rnd_template.c"
172 /***********************************/
175 #define DEF(x, y) x ## _ ## y ## _mmx
176 #define SET_RND MOVQ_WTWO
177 #define PAVGBP(a, b, c, d, e, f) PAVGBP_MMX(a, b, c, d, e, f)
178 #define PAVGB(a, b, c, e) PAVGB_MMX(a, b, c, e)
180 #include "hpeldsp_rnd_template.c"
188 #endif /* HAVE_INLINE_ASM */
192 #define ff_put_pixels8_mmx ff_put_pixels8_mmxext
194 /***********************************/
197 #define DEF(x) x ## _3dnow
199 #include "hpeldsp_avg_template.c"
203 /***********************************/
204 /* MMXEXT specific */
206 #define DEF(x) x ## _mmxext
208 #include "hpeldsp_avg_template.c"
212 #endif /* HAVE_YASM */
216 #define put_no_rnd_pixels16_mmx put_pixels16_mmx
217 #define put_no_rnd_pixels8_mmx put_pixels8_mmx
218 #define put_pixels16_mmxext put_pixels16_mmx
219 #define put_pixels8_mmxext put_pixels8_mmx
220 #define put_pixels4_mmxext put_pixels4_mmx
221 #define put_no_rnd_pixels16_mmxext put_no_rnd_pixels16_mmx
222 #define put_no_rnd_pixels8_mmxext put_no_rnd_pixels8_mmx
224 static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels,
225 ptrdiff_t line_size, int h)
228 "lea (%3, %3), %%"REG_a" \n\t"
231 "movq (%1 ), %%mm0 \n\t"
232 "movq (%1, %3), %%mm1 \n\t"
233 "movq %%mm0, (%2) \n\t"
234 "movq %%mm1, (%2, %3) \n\t"
235 "add %%"REG_a", %1 \n\t"
236 "add %%"REG_a", %2 \n\t"
237 "movq (%1 ), %%mm0 \n\t"
238 "movq (%1, %3), %%mm1 \n\t"
239 "movq %%mm0, (%2) \n\t"
240 "movq %%mm1, (%2, %3) \n\t"
241 "add %%"REG_a", %1 \n\t"
242 "add %%"REG_a", %2 \n\t"
245 : "+g"(h), "+r"(pixels), "+r"(block)
246 : "r"((x86_reg)line_size)
251 static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels,
252 ptrdiff_t line_size, int h)
255 "lea (%3, %3), %%"REG_a" \n\t"
258 "movq (%1 ), %%mm0 \n\t"
259 "movq 8(%1 ), %%mm4 \n\t"
260 "movq (%1, %3), %%mm1 \n\t"
261 "movq 8(%1, %3), %%mm5 \n\t"
262 "movq %%mm0, (%2) \n\t"
263 "movq %%mm4, 8(%2) \n\t"
264 "movq %%mm1, (%2, %3) \n\t"
265 "movq %%mm5, 8(%2, %3) \n\t"
266 "add %%"REG_a", %1 \n\t"
267 "add %%"REG_a", %2 \n\t"
268 "movq (%1 ), %%mm0 \n\t"
269 "movq 8(%1 ), %%mm4 \n\t"
270 "movq (%1, %3), %%mm1 \n\t"
271 "movq 8(%1, %3), %%mm5 \n\t"
272 "movq %%mm0, (%2) \n\t"
273 "movq %%mm4, 8(%2) \n\t"
274 "movq %%mm1, (%2, %3) \n\t"
275 "movq %%mm5, 8(%2, %3) \n\t"
276 "add %%"REG_a", %1 \n\t"
277 "add %%"REG_a", %2 \n\t"
280 : "+g"(h), "+r"(pixels), "+r"(block)
281 : "r"((x86_reg)line_size)
285 #endif /* HAVE_INLINE_ASM */
287 #define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU) \
289 c->PFX ## _pixels_tab IDX [0] = PFX ## _pixels ## SIZE ## _ ## CPU; \
290 c->PFX ## _pixels_tab IDX [1] = PFX ## _pixels ## SIZE ## _x2_ ## CPU; \
291 c->PFX ## _pixels_tab IDX [2] = PFX ## _pixels ## SIZE ## _y2_ ## CPU; \
292 c->PFX ## _pixels_tab IDX [3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU; \
295 static void hpeldsp_init_mmx(HpelDSPContext *c, int flags, int mm_flags)
298 SET_HPEL_FUNCS(put, [0], 16, mmx);
299 SET_HPEL_FUNCS(put_no_rnd, [0], 16, mmx);
300 SET_HPEL_FUNCS(avg, [0], 16, mmx);
301 SET_HPEL_FUNCS(avg_no_rnd, , 16, mmx);
302 SET_HPEL_FUNCS(put, [1], 8, mmx);
303 SET_HPEL_FUNCS(put_no_rnd, [1], 8, mmx);
304 SET_HPEL_FUNCS(avg, [1], 8, mmx);
305 #endif /* HAVE_INLINE_ASM */
308 static void hpeldsp_init_mmxext(HpelDSPContext *c, int flags, int mm_flags)
311 c->put_pixels_tab[0][1] = ff_put_pixels16_x2_mmxext;
312 c->put_pixels_tab[0][2] = ff_put_pixels16_y2_mmxext;
314 c->avg_pixels_tab[0][0] = ff_avg_pixels16_mmxext;
315 c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_mmxext;
316 c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_mmxext;
318 c->put_pixels_tab[1][1] = ff_put_pixels8_x2_mmxext;
319 c->put_pixels_tab[1][2] = ff_put_pixels8_y2_mmxext;
321 c->avg_pixels_tab[1][0] = ff_avg_pixels8_mmxext;
322 c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_mmxext;
323 c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_mmxext;
325 if (!(flags & CODEC_FLAG_BITEXACT)) {
326 c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_mmxext;
327 c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_mmxext;
328 c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_mmxext;
329 c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_mmxext;
331 c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_mmxext;
332 c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_mmxext;
335 if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
336 c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_mmxext;
337 c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_mmxext;
339 #endif /* HAVE_YASM */
342 static void hpeldsp_init_3dnow(HpelDSPContext *c, int flags, int mm_flags)
345 c->put_pixels_tab[0][1] = ff_put_pixels16_x2_3dnow;
346 c->put_pixels_tab[0][2] = ff_put_pixels16_y2_3dnow;
348 c->avg_pixels_tab[0][0] = ff_avg_pixels16_3dnow;
349 c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_3dnow;
350 c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_3dnow;
352 c->put_pixels_tab[1][1] = ff_put_pixels8_x2_3dnow;
353 c->put_pixels_tab[1][2] = ff_put_pixels8_y2_3dnow;
355 c->avg_pixels_tab[1][0] = ff_avg_pixels8_3dnow;
356 c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_3dnow;
357 c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_3dnow;
359 if (!(flags & CODEC_FLAG_BITEXACT)){
360 c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_3dnow;
361 c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_3dnow;
362 c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_3dnow;
363 c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_3dnow;
365 c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_3dnow;
366 c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_3dnow;
369 if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
370 c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_3dnow;
371 c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_3dnow;
373 #endif /* HAVE_YASM */
376 static void hpeldsp_init_sse2(HpelDSPContext *c, int flags, int mm_flags)
379 if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
380 // these functions are slower than mmx on AMD, but faster on Intel
381 c->put_pixels_tab[0][0] = ff_put_pixels16_sse2;
382 c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_sse2;
383 c->avg_pixels_tab[0][0] = ff_avg_pixels16_sse2;
385 #endif /* HAVE_YASM */
388 void ff_hpeldsp_init_x86(HpelDSPContext *c, int flags)
390 int mm_flags = av_get_cpu_flags();
392 if (HAVE_MMX && mm_flags & AV_CPU_FLAG_MMX)
393 hpeldsp_init_mmx(c, flags, mm_flags);
395 if (mm_flags & AV_CPU_FLAG_MMXEXT)
396 hpeldsp_init_mmxext(c, flags, mm_flags);
398 if (mm_flags & AV_CPU_FLAG_3DNOW)
399 hpeldsp_init_3dnow(c, flags, mm_flags);
401 if (mm_flags & AV_CPU_FLAG_SSE2)
402 hpeldsp_init_sse2(c, flags, mm_flags);