vorbisdsp: convert x86 simd functions from inline asm to yasm.
[ffmpeg.git] / libavcodec / x86 / dsputil_mmx.c
1 /*
2  * MMX optimized DSP utils
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "libavutil/cpu.h"
26 #include "libavutil/x86/asm.h"
27 #include "libavcodec/dsputil.h"
28 #include "libavcodec/h264dsp.h"
29 #include "libavcodec/mpegvideo.h"
30 #include "libavcodec/simple_idct.h"
31 #include "dsputil_mmx.h"
32 #include "idct_xvid.h"
33
34 //#undef NDEBUG
35 //#include <assert.h>
36
37 /* pixel operations */
38 DECLARE_ALIGNED(8,  const uint64_t, ff_bone) = 0x0101010101010101ULL;
39 DECLARE_ALIGNED(8,  const uint64_t, ff_wtwo) = 0x0002000200020002ULL;
40
41 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1)    = { 0x0001000100010001ULL, 0x0001000100010001ULL };
42 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_2)    = { 0x0002000200020002ULL, 0x0002000200020002ULL };
43 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_3)    = { 0x0003000300030003ULL, 0x0003000300030003ULL };
44 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_4)    = { 0x0004000400040004ULL, 0x0004000400040004ULL };
45 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_5)    = { 0x0005000500050005ULL, 0x0005000500050005ULL };
46 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_8)    = { 0x0008000800080008ULL, 0x0008000800080008ULL };
47 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_9)    = { 0x0009000900090009ULL, 0x0009000900090009ULL };
48 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_15)   =   0x000F000F000F000FULL;
49 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_16)   = { 0x0010001000100010ULL, 0x0010001000100010ULL };
50 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_17)   = { 0x0011001100110011ULL, 0x0011001100110011ULL };
51 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_18)   = { 0x0012001200120012ULL, 0x0012001200120012ULL };
52 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_20)   =   0x0014001400140014ULL;
53 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_27)   = { 0x001B001B001B001BULL, 0x001B001B001B001BULL };
54 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_28)   = { 0x001C001C001C001CULL, 0x001C001C001C001CULL };
55 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_32)   = { 0x0020002000200020ULL, 0x0020002000200020ULL };
56 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_42)   =   0x002A002A002A002AULL;
57 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_53)   =   0x0035003500350035ULL;
58 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_63)   = { 0x003F003F003F003FULL, 0x003F003F003F003FULL };
59 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_64)   = { 0x0040004000400040ULL, 0x0040004000400040ULL };
60 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_96)   =   0x0060006000600060ULL;
61 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_128)  =   0x0080008000800080ULL;
62 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_255)  =   0x00ff00ff00ff00ffULL;
63 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_512)  = { 0x0200020002000200ULL, 0x0200020002000200ULL };
64 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1019) = { 0x03FB03FB03FB03FBULL, 0x03FB03FB03FB03FBULL };
65
66 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_0)    = { 0x0000000000000000ULL, 0x0000000000000000ULL };
67 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_1)    = { 0x0101010101010101ULL, 0x0101010101010101ULL };
68 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_3)    = { 0x0303030303030303ULL, 0x0303030303030303ULL };
69 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_4)    = { 0x0404040404040404ULL, 0x0404040404040404ULL };
70 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_7)    =   0x0707070707070707ULL;
71 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_1F)   =   0x1F1F1F1F1F1F1F1FULL;
72 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_3F)   =   0x3F3F3F3F3F3F3F3FULL;
73 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_80)   = { 0x8080808080808080ULL, 0x8080808080808080ULL };
74 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_81)   =   0x8181818181818181ULL;
75 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_A1)   = { 0xA1A1A1A1A1A1A1A1ULL, 0xA1A1A1A1A1A1A1A1ULL };
76 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_F8)   = { 0xF8F8F8F8F8F8F8F8ULL, 0xF8F8F8F8F8F8F8F8ULL };
77 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_FC)   =   0xFCFCFCFCFCFCFCFCULL;
78 DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_FE)   = { 0xFEFEFEFEFEFEFEFEULL, 0xFEFEFEFEFEFEFEFEULL };
79
80 DECLARE_ALIGNED(16, const double, ff_pd_1)[2] = { 1.0, 1.0 };
81 DECLARE_ALIGNED(16, const double, ff_pd_2)[2] = { 2.0, 2.0 };
82
83 #if HAVE_INLINE_ASM
84
85 #define JUMPALIGN()     __asm__ volatile (".p2align 3"::)
86 #define MOVQ_ZERO(regd) __asm__ volatile ("pxor %%"#regd", %%"#regd ::)
87
88 #define MOVQ_BFE(regd)                                  \
89     __asm__ volatile (                                  \
90         "pcmpeqd %%"#regd", %%"#regd"   \n\t"           \
91         "paddb   %%"#regd", %%"#regd"   \n\t" ::)
92
93 #ifndef PIC
94 #define MOVQ_BONE(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_bone))
95 #define MOVQ_WTWO(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_wtwo))
96 #else
97 // for shared library it's better to use this way for accessing constants
98 // pcmpeqd -> -1
99 #define MOVQ_BONE(regd)                                 \
100     __asm__ volatile (                                  \
101         "pcmpeqd  %%"#regd", %%"#regd"  \n\t"           \
102         "psrlw          $15, %%"#regd"  \n\t"           \
103         "packuswb %%"#regd", %%"#regd"  \n\t" ::)
104
105 #define MOVQ_WTWO(regd)                                 \
106     __asm__ volatile (                                  \
107         "pcmpeqd %%"#regd", %%"#regd"   \n\t"           \
108         "psrlw         $15, %%"#regd"   \n\t"           \
109         "psllw          $1, %%"#regd"   \n\t"::)
110
111 #endif
112
113 // using regr as temporary and for the output result
114 // first argument is unmodifed and second is trashed
115 // regfe is supposed to contain 0xfefefefefefefefe
116 #define PAVGB_MMX_NO_RND(rega, regb, regr, regfe)                \
117     "movq   "#rega", "#regr"            \n\t"                    \
118     "pand   "#regb", "#regr"            \n\t"                    \
119     "pxor   "#rega", "#regb"            \n\t"                    \
120     "pand  "#regfe", "#regb"            \n\t"                    \
121     "psrlq       $1, "#regb"            \n\t"                    \
122     "paddb  "#regb", "#regr"            \n\t"
123
124 #define PAVGB_MMX(rega, regb, regr, regfe)                       \
125     "movq   "#rega", "#regr"            \n\t"                    \
126     "por    "#regb", "#regr"            \n\t"                    \
127     "pxor   "#rega", "#regb"            \n\t"                    \
128     "pand  "#regfe", "#regb"            \n\t"                    \
129     "psrlq       $1, "#regb"            \n\t"                    \
130     "psubb  "#regb", "#regr"            \n\t"
131
132 // mm6 is supposed to contain 0xfefefefefefefefe
133 #define PAVGBP_MMX_NO_RND(rega, regb, regr,  regc, regd, regp)   \
134     "movq  "#rega", "#regr"             \n\t"                    \
135     "movq  "#regc", "#regp"             \n\t"                    \
136     "pand  "#regb", "#regr"             \n\t"                    \
137     "pand  "#regd", "#regp"             \n\t"                    \
138     "pxor  "#rega", "#regb"             \n\t"                    \
139     "pxor  "#regc", "#regd"             \n\t"                    \
140     "pand    %%mm6, "#regb"             \n\t"                    \
141     "pand    %%mm6, "#regd"             \n\t"                    \
142     "psrlq      $1, "#regb"             \n\t"                    \
143     "psrlq      $1, "#regd"             \n\t"                    \
144     "paddb "#regb", "#regr"             \n\t"                    \
145     "paddb "#regd", "#regp"             \n\t"
146
147 #define PAVGBP_MMX(rega, regb, regr, regc, regd, regp)           \
148     "movq  "#rega", "#regr"             \n\t"                    \
149     "movq  "#regc", "#regp"             \n\t"                    \
150     "por   "#regb", "#regr"             \n\t"                    \
151     "por   "#regd", "#regp"             \n\t"                    \
152     "pxor  "#rega", "#regb"             \n\t"                    \
153     "pxor  "#regc", "#regd"             \n\t"                    \
154     "pand    %%mm6, "#regb"             \n\t"                    \
155     "pand    %%mm6, "#regd"             \n\t"                    \
156     "psrlq      $1, "#regd"             \n\t"                    \
157     "psrlq      $1, "#regb"             \n\t"                    \
158     "psubb "#regb", "#regr"             \n\t"                    \
159     "psubb "#regd", "#regp"             \n\t"
160
161 /***********************************/
162 /* MMX no rounding */
163 #define DEF(x, y) x ## _no_rnd_ ## y ## _mmx
164 #define SET_RND  MOVQ_WONE
165 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
166 #define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
167 #define OP_AVG(a, b, c, e)              PAVGB_MMX(a, b, c, e)
168
169 #include "dsputil_rnd_template.c"
170
171 #undef DEF
172 #undef SET_RND
173 #undef PAVGBP
174 #undef PAVGB
175 /***********************************/
176 /* MMX rounding */
177
178 #define DEF(x, y) x ## _ ## y ## _mmx
179 #define SET_RND  MOVQ_WTWO
180 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
181 #define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
182
183 #include "dsputil_rnd_template.c"
184
185 #undef DEF
186 #undef SET_RND
187 #undef PAVGBP
188 #undef PAVGB
189 #undef OP_AVG
190
191 /***********************************/
192 /* 3Dnow specific */
193
194 #define DEF(x) x ## _3dnow
195 #define PAVGB "pavgusb"
196 #define SKIP_FOR_3DNOW
197
198 #include "dsputil_avg_template.c"
199
200 #undef DEF
201 #undef PAVGB
202 #undef SKIP_FOR_3DNOW
203
204 /***********************************/
205 /* MMXEXT specific */
206
207 #define DEF(x) x ## _mmxext
208
209 /* Introduced only in MMXEXT set */
210 #define PAVGB "pavgb"
211
212 #include "dsputil_avg_template.c"
213
214 #undef DEF
215 #undef PAVGB
216
217 #define put_no_rnd_pixels16_mmx put_pixels16_mmx
218 #define put_no_rnd_pixels8_mmx put_pixels8_mmx
219 #define put_pixels16_mmxext put_pixels16_mmx
220 #define put_pixels8_mmxext put_pixels8_mmx
221 #define put_pixels4_mmxext put_pixels4_mmx
222 #define put_no_rnd_pixels16_mmxext put_no_rnd_pixels16_mmx
223 #define put_no_rnd_pixels8_mmxext put_no_rnd_pixels8_mmx
224
225 /***********************************/
226 /* standard MMX */
227
228 void ff_put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels,
229                                int line_size)
230 {
231     const DCTELEM *p;
232     uint8_t *pix;
233
234     /* read the pixels */
235     p   = block;
236     pix = pixels;
237     /* unrolled loop */
238     __asm__ volatile (
239         "movq      (%3), %%mm0          \n\t"
240         "movq     8(%3), %%mm1          \n\t"
241         "movq    16(%3), %%mm2          \n\t"
242         "movq    24(%3), %%mm3          \n\t"
243         "movq    32(%3), %%mm4          \n\t"
244         "movq    40(%3), %%mm5          \n\t"
245         "movq    48(%3), %%mm6          \n\t"
246         "movq    56(%3), %%mm7          \n\t"
247         "packuswb %%mm1, %%mm0          \n\t"
248         "packuswb %%mm3, %%mm2          \n\t"
249         "packuswb %%mm5, %%mm4          \n\t"
250         "packuswb %%mm7, %%mm6          \n\t"
251         "movq     %%mm0, (%0)           \n\t"
252         "movq     %%mm2, (%0, %1)       \n\t"
253         "movq     %%mm4, (%0, %1, 2)    \n\t"
254         "movq     %%mm6, (%0, %2)       \n\t"
255         :: "r"(pix), "r"((x86_reg)line_size), "r"((x86_reg)line_size * 3),
256            "r"(p)
257         : "memory");
258     pix += line_size * 4;
259     p   += 32;
260
261     // if here would be an exact copy of the code above
262     // compiler would generate some very strange code
263     // thus using "r"
264     __asm__ volatile (
265         "movq       (%3), %%mm0         \n\t"
266         "movq      8(%3), %%mm1         \n\t"
267         "movq     16(%3), %%mm2         \n\t"
268         "movq     24(%3), %%mm3         \n\t"
269         "movq     32(%3), %%mm4         \n\t"
270         "movq     40(%3), %%mm5         \n\t"
271         "movq     48(%3), %%mm6         \n\t"
272         "movq     56(%3), %%mm7         \n\t"
273         "packuswb  %%mm1, %%mm0         \n\t"
274         "packuswb  %%mm3, %%mm2         \n\t"
275         "packuswb  %%mm5, %%mm4         \n\t"
276         "packuswb  %%mm7, %%mm6         \n\t"
277         "movq      %%mm0, (%0)          \n\t"
278         "movq      %%mm2, (%0, %1)      \n\t"
279         "movq      %%mm4, (%0, %1, 2)   \n\t"
280         "movq      %%mm6, (%0, %2)      \n\t"
281         :: "r"(pix), "r"((x86_reg)line_size), "r"((x86_reg)line_size * 3), "r"(p)
282         : "memory");
283 }
284
285 #define put_signed_pixels_clamped_mmx_half(off)             \
286     "movq          "#off"(%2), %%mm1        \n\t"           \
287     "movq     16 + "#off"(%2), %%mm2        \n\t"           \
288     "movq     32 + "#off"(%2), %%mm3        \n\t"           \
289     "movq     48 + "#off"(%2), %%mm4        \n\t"           \
290     "packsswb  8 + "#off"(%2), %%mm1        \n\t"           \
291     "packsswb 24 + "#off"(%2), %%mm2        \n\t"           \
292     "packsswb 40 + "#off"(%2), %%mm3        \n\t"           \
293     "packsswb 56 + "#off"(%2), %%mm4        \n\t"           \
294     "paddb              %%mm0, %%mm1        \n\t"           \
295     "paddb              %%mm0, %%mm2        \n\t"           \
296     "paddb              %%mm0, %%mm3        \n\t"           \
297     "paddb              %%mm0, %%mm4        \n\t"           \
298     "movq               %%mm1, (%0)         \n\t"           \
299     "movq               %%mm2, (%0, %3)     \n\t"           \
300     "movq               %%mm3, (%0, %3, 2)  \n\t"           \
301     "movq               %%mm4, (%0, %1)     \n\t"
302
303 void ff_put_signed_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels,
304                                       int line_size)
305 {
306     x86_reg line_skip = line_size;
307     x86_reg line_skip3;
308
309     __asm__ volatile (
310         "movq "MANGLE(ff_pb_80)", %%mm0     \n\t"
311         "lea         (%3, %3, 2), %1        \n\t"
312         put_signed_pixels_clamped_mmx_half(0)
313         "lea         (%0, %3, 4), %0        \n\t"
314         put_signed_pixels_clamped_mmx_half(64)
315         : "+&r"(pixels), "=&r"(line_skip3)
316         : "r"(block), "r"(line_skip)
317         : "memory");
318 }
319
320 void ff_add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels,
321                                int line_size)
322 {
323     const DCTELEM *p;
324     uint8_t *pix;
325     int i;
326
327     /* read the pixels */
328     p   = block;
329     pix = pixels;
330     MOVQ_ZERO(mm7);
331     i = 4;
332     do {
333         __asm__ volatile (
334             "movq        (%2), %%mm0    \n\t"
335             "movq       8(%2), %%mm1    \n\t"
336             "movq      16(%2), %%mm2    \n\t"
337             "movq      24(%2), %%mm3    \n\t"
338             "movq          %0, %%mm4    \n\t"
339             "movq          %1, %%mm6    \n\t"
340             "movq       %%mm4, %%mm5    \n\t"
341             "punpcklbw  %%mm7, %%mm4    \n\t"
342             "punpckhbw  %%mm7, %%mm5    \n\t"
343             "paddsw     %%mm4, %%mm0    \n\t"
344             "paddsw     %%mm5, %%mm1    \n\t"
345             "movq       %%mm6, %%mm5    \n\t"
346             "punpcklbw  %%mm7, %%mm6    \n\t"
347             "punpckhbw  %%mm7, %%mm5    \n\t"
348             "paddsw     %%mm6, %%mm2    \n\t"
349             "paddsw     %%mm5, %%mm3    \n\t"
350             "packuswb   %%mm1, %%mm0    \n\t"
351             "packuswb   %%mm3, %%mm2    \n\t"
352             "movq       %%mm0, %0       \n\t"
353             "movq       %%mm2, %1       \n\t"
354             : "+m"(*pix), "+m"(*(pix + line_size))
355             : "r"(p)
356             : "memory");
357         pix += line_size * 2;
358         p   += 16;
359     } while (--i);
360 }
361
362 static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels,
363                             int line_size, int h)
364 {
365     __asm__ volatile (
366         "lea   (%3, %3), %%"REG_a"      \n\t"
367         ".p2align     3                 \n\t"
368         "1:                             \n\t"
369         "movq  (%1    ), %%mm0          \n\t"
370         "movq  (%1, %3), %%mm1          \n\t"
371         "movq     %%mm0, (%2)           \n\t"
372         "movq     %%mm1, (%2, %3)       \n\t"
373         "add  %%"REG_a", %1             \n\t"
374         "add  %%"REG_a", %2             \n\t"
375         "movq  (%1    ), %%mm0          \n\t"
376         "movq  (%1, %3), %%mm1          \n\t"
377         "movq     %%mm0, (%2)           \n\t"
378         "movq     %%mm1, (%2, %3)       \n\t"
379         "add  %%"REG_a", %1             \n\t"
380         "add  %%"REG_a", %2             \n\t"
381         "subl        $4, %0             \n\t"
382         "jnz         1b                 \n\t"
383         : "+g"(h), "+r"(pixels),  "+r"(block)
384         : "r"((x86_reg)line_size)
385         : "%"REG_a, "memory"
386         );
387 }
388
389 static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels,
390                              int line_size, int h)
391 {
392     __asm__ volatile (
393         "lea   (%3, %3), %%"REG_a"      \n\t"
394         ".p2align     3                 \n\t"
395         "1:                             \n\t"
396         "movq  (%1    ), %%mm0          \n\t"
397         "movq 8(%1    ), %%mm4          \n\t"
398         "movq  (%1, %3), %%mm1          \n\t"
399         "movq 8(%1, %3), %%mm5          \n\t"
400         "movq     %%mm0,  (%2)          \n\t"
401         "movq     %%mm4, 8(%2)          \n\t"
402         "movq     %%mm1,  (%2, %3)      \n\t"
403         "movq     %%mm5, 8(%2, %3)      \n\t"
404         "add  %%"REG_a", %1             \n\t"
405         "add  %%"REG_a", %2             \n\t"
406         "movq  (%1    ), %%mm0          \n\t"
407         "movq 8(%1    ), %%mm4          \n\t"
408         "movq  (%1, %3), %%mm1          \n\t"
409         "movq 8(%1, %3), %%mm5          \n\t"
410         "movq     %%mm0,  (%2)          \n\t"
411         "movq     %%mm4, 8(%2)          \n\t"
412         "movq     %%mm1,  (%2, %3)      \n\t"
413         "movq     %%mm5, 8(%2, %3)      \n\t"
414         "add  %%"REG_a", %1             \n\t"
415         "add  %%"REG_a", %2             \n\t"
416         "subl        $4, %0             \n\t"
417         "jnz         1b                 \n\t"
418         : "+g"(h), "+r"(pixels),  "+r"(block)
419         : "r"((x86_reg)line_size)
420         : "%"REG_a, "memory"
421         );
422 }
423
424 #define CLEAR_BLOCKS(name, n)                           \
425 static void name(DCTELEM *blocks)                       \
426 {                                                       \
427     __asm__ volatile (                                  \
428         "pxor %%mm7, %%mm7              \n\t"           \
429         "mov     %1,        %%"REG_a"   \n\t"           \
430         "1:                             \n\t"           \
431         "movq %%mm7,   (%0, %%"REG_a")  \n\t"           \
432         "movq %%mm7,  8(%0, %%"REG_a")  \n\t"           \
433         "movq %%mm7, 16(%0, %%"REG_a")  \n\t"           \
434         "movq %%mm7, 24(%0, %%"REG_a")  \n\t"           \
435         "add    $32, %%"REG_a"          \n\t"           \
436         "js      1b                     \n\t"           \
437         :: "r"(((uint8_t *)blocks) + 128 * n),          \
438            "i"(-128 * n)                                \
439         : "%"REG_a                                      \
440         );                                              \
441 }
442 CLEAR_BLOCKS(clear_blocks_mmx, 6)
443 CLEAR_BLOCKS(clear_block_mmx, 1)
444
445 static void clear_block_sse(DCTELEM *block)
446 {
447     __asm__ volatile (
448         "xorps  %%xmm0, %%xmm0          \n"
449         "movaps %%xmm0,    (%0)         \n"
450         "movaps %%xmm0,  16(%0)         \n"
451         "movaps %%xmm0,  32(%0)         \n"
452         "movaps %%xmm0,  48(%0)         \n"
453         "movaps %%xmm0,  64(%0)         \n"
454         "movaps %%xmm0,  80(%0)         \n"
455         "movaps %%xmm0,  96(%0)         \n"
456         "movaps %%xmm0, 112(%0)         \n"
457         :: "r"(block)
458         : "memory"
459     );
460 }
461
462 static void clear_blocks_sse(DCTELEM *blocks)
463 {
464     __asm__ volatile (
465         "xorps  %%xmm0, %%xmm0              \n"
466         "mov        %1,         %%"REG_a"   \n"
467         "1:                                 \n"
468         "movaps %%xmm0,    (%0, %%"REG_a")  \n"
469         "movaps %%xmm0,  16(%0, %%"REG_a")  \n"
470         "movaps %%xmm0,  32(%0, %%"REG_a")  \n"
471         "movaps %%xmm0,  48(%0, %%"REG_a")  \n"
472         "movaps %%xmm0,  64(%0, %%"REG_a")  \n"
473         "movaps %%xmm0,  80(%0, %%"REG_a")  \n"
474         "movaps %%xmm0,  96(%0, %%"REG_a")  \n"
475         "movaps %%xmm0, 112(%0, %%"REG_a")  \n"
476         "add      $128,         %%"REG_a"   \n"
477         "js         1b                      \n"
478         :: "r"(((uint8_t *)blocks) + 128 * 6),
479            "i"(-128 * 6)
480         : "%"REG_a
481     );
482 }
483
484 static void add_bytes_mmx(uint8_t *dst, uint8_t *src, int w)
485 {
486     x86_reg i = 0;
487     __asm__ volatile (
488         "jmp          2f                \n\t"
489         "1:                             \n\t"
490         "movq   (%1, %0), %%mm0         \n\t"
491         "movq   (%2, %0), %%mm1         \n\t"
492         "paddb     %%mm0, %%mm1         \n\t"
493         "movq      %%mm1, (%2, %0)      \n\t"
494         "movq  8(%1, %0), %%mm0         \n\t"
495         "movq  8(%2, %0), %%mm1         \n\t"
496         "paddb     %%mm0, %%mm1         \n\t"
497         "movq      %%mm1, 8(%2, %0)     \n\t"
498         "add         $16, %0            \n\t"
499         "2:                             \n\t"
500         "cmp          %3, %0            \n\t"
501         "js           1b                \n\t"
502         : "+r"(i)
503         : "r"(src), "r"(dst), "r"((x86_reg)w - 15)
504     );
505     for ( ; i < w; i++)
506         dst[i + 0] += src[i + 0];
507 }
508
509 #if HAVE_7REGS
510 static void add_hfyu_median_prediction_cmov(uint8_t *dst, const uint8_t *top,
511                                             const uint8_t *diff, int w,
512                                             int *left, int *left_top)
513 {
514     x86_reg w2 = -w;
515     x86_reg x;
516     int l  = *left     & 0xff;
517     int tl = *left_top & 0xff;
518     int t;
519     __asm__ volatile (
520         "mov          %7, %3            \n"
521         "1:                             \n"
522         "movzbl (%3, %4), %2            \n"
523         "mov          %2, %k3           \n"
524         "sub         %b1, %b3           \n"
525         "add         %b0, %b3           \n"
526         "mov          %2, %1            \n"
527         "cmp          %0, %2            \n"
528         "cmovg        %0, %2            \n"
529         "cmovg        %1, %0            \n"
530         "cmp         %k3, %0            \n"
531         "cmovg       %k3, %0            \n"
532         "mov          %7, %3            \n"
533         "cmp          %2, %0            \n"
534         "cmovl        %2, %0            \n"
535         "add    (%6, %4), %b0           \n"
536         "mov         %b0, (%5, %4)      \n"
537         "inc          %4                \n"
538         "jl           1b                \n"
539         : "+&q"(l), "+&q"(tl), "=&r"(t), "=&q"(x), "+&r"(w2)
540         : "r"(dst + w), "r"(diff + w), "rm"(top + w)
541     );
542     *left     = l;
543     *left_top = tl;
544 }
545 #endif
546
547 static inline void transpose4x4(uint8_t *dst, uint8_t *src, x86_reg dst_stride, x86_reg src_stride){
548     __asm__ volatile( //FIXME could save 1 instruction if done as 8x4 ...
549         "movd  (%1), %%mm0              \n\t"
550         "add   %3, %1                   \n\t"
551         "movd  (%1), %%mm1              \n\t"
552         "movd  (%1,%3,1), %%mm2         \n\t"
553         "movd  (%1,%3,2), %%mm3         \n\t"
554         "punpcklbw %%mm1, %%mm0         \n\t"
555         "punpcklbw %%mm3, %%mm2         \n\t"
556         "movq %%mm0, %%mm1              \n\t"
557         "punpcklwd %%mm2, %%mm0         \n\t"
558         "punpckhwd %%mm2, %%mm1         \n\t"
559         "movd  %%mm0, (%0)              \n\t"
560         "add   %2, %0                   \n\t"
561         "punpckhdq %%mm0, %%mm0         \n\t"
562         "movd  %%mm0, (%0)              \n\t"
563         "movd  %%mm1, (%0,%2,1)         \n\t"
564         "punpckhdq %%mm1, %%mm1         \n\t"
565         "movd  %%mm1, (%0,%2,2)         \n\t"
566
567         :  "+&r" (dst),
568            "+&r" (src)
569         :  "r" (dst_stride),
570            "r" (src_stride)
571         :  "memory"
572     );
573 }
574
575 #define H263_LOOP_FILTER                        \
576     "pxor      %%mm7, %%mm7             \n\t"   \
577     "movq         %0, %%mm0             \n\t"   \
578     "movq         %0, %%mm1             \n\t"   \
579     "movq         %3, %%mm2             \n\t"   \
580     "movq         %3, %%mm3             \n\t"   \
581     "punpcklbw %%mm7, %%mm0             \n\t"   \
582     "punpckhbw %%mm7, %%mm1             \n\t"   \
583     "punpcklbw %%mm7, %%mm2             \n\t"   \
584     "punpckhbw %%mm7, %%mm3             \n\t"   \
585     "psubw     %%mm2, %%mm0             \n\t"   \
586     "psubw     %%mm3, %%mm1             \n\t"   \
587     "movq         %1, %%mm2             \n\t"   \
588     "movq         %1, %%mm3             \n\t"   \
589     "movq         %2, %%mm4             \n\t"   \
590     "movq         %2, %%mm5             \n\t"   \
591     "punpcklbw %%mm7, %%mm2             \n\t"   \
592     "punpckhbw %%mm7, %%mm3             \n\t"   \
593     "punpcklbw %%mm7, %%mm4             \n\t"   \
594     "punpckhbw %%mm7, %%mm5             \n\t"   \
595     "psubw     %%mm2, %%mm4             \n\t"   \
596     "psubw     %%mm3, %%mm5             \n\t"   \
597     "psllw        $2, %%mm4             \n\t"   \
598     "psllw        $2, %%mm5             \n\t"   \
599     "paddw     %%mm0, %%mm4             \n\t"   \
600     "paddw     %%mm1, %%mm5             \n\t"   \
601     "pxor      %%mm6, %%mm6             \n\t"   \
602     "pcmpgtw   %%mm4, %%mm6             \n\t"   \
603     "pcmpgtw   %%mm5, %%mm7             \n\t"   \
604     "pxor      %%mm6, %%mm4             \n\t"   \
605     "pxor      %%mm7, %%mm5             \n\t"   \
606     "psubw     %%mm6, %%mm4             \n\t"   \
607     "psubw     %%mm7, %%mm5             \n\t"   \
608     "psrlw        $3, %%mm4             \n\t"   \
609     "psrlw        $3, %%mm5             \n\t"   \
610     "packuswb  %%mm5, %%mm4             \n\t"   \
611     "packsswb  %%mm7, %%mm6             \n\t"   \
612     "pxor      %%mm7, %%mm7             \n\t"   \
613     "movd         %4, %%mm2             \n\t"   \
614     "punpcklbw %%mm2, %%mm2             \n\t"   \
615     "punpcklbw %%mm2, %%mm2             \n\t"   \
616     "punpcklbw %%mm2, %%mm2             \n\t"   \
617     "psubusb   %%mm4, %%mm2             \n\t"   \
618     "movq      %%mm2, %%mm3             \n\t"   \
619     "psubusb   %%mm4, %%mm3             \n\t"   \
620     "psubb     %%mm3, %%mm2             \n\t"   \
621     "movq         %1, %%mm3             \n\t"   \
622     "movq         %2, %%mm4             \n\t"   \
623     "pxor      %%mm6, %%mm3             \n\t"   \
624     "pxor      %%mm6, %%mm4             \n\t"   \
625     "paddusb   %%mm2, %%mm3             \n\t"   \
626     "psubusb   %%mm2, %%mm4             \n\t"   \
627     "pxor      %%mm6, %%mm3             \n\t"   \
628     "pxor      %%mm6, %%mm4             \n\t"   \
629     "paddusb   %%mm2, %%mm2             \n\t"   \
630     "packsswb  %%mm1, %%mm0             \n\t"   \
631     "pcmpgtb   %%mm0, %%mm7             \n\t"   \
632     "pxor      %%mm7, %%mm0             \n\t"   \
633     "psubb     %%mm7, %%mm0             \n\t"   \
634     "movq      %%mm0, %%mm1             \n\t"   \
635     "psubusb   %%mm2, %%mm0             \n\t"   \
636     "psubb     %%mm0, %%mm1             \n\t"   \
637     "pand         %5, %%mm1             \n\t"   \
638     "psrlw        $2, %%mm1             \n\t"   \
639     "pxor      %%mm7, %%mm1             \n\t"   \
640     "psubb     %%mm7, %%mm1             \n\t"   \
641     "movq         %0, %%mm5             \n\t"   \
642     "movq         %3, %%mm6             \n\t"   \
643     "psubb     %%mm1, %%mm5             \n\t"   \
644     "paddb     %%mm1, %%mm6             \n\t"
645
646 static void h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale)
647 {
648     if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
649         const int strength = ff_h263_loop_filter_strength[qscale];
650
651         __asm__ volatile (
652             H263_LOOP_FILTER
653
654             "movq %%mm3, %1             \n\t"
655             "movq %%mm4, %2             \n\t"
656             "movq %%mm5, %0             \n\t"
657             "movq %%mm6, %3             \n\t"
658             : "+m"(*(uint64_t*)(src - 2 * stride)),
659               "+m"(*(uint64_t*)(src - 1 * stride)),
660               "+m"(*(uint64_t*)(src + 0 * stride)),
661               "+m"(*(uint64_t*)(src + 1 * stride))
662             : "g"(2 * strength), "m"(ff_pb_FC)
663             );
664     }
665 }
666
667 static void h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale)
668 {
669     if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
670         const int strength = ff_h263_loop_filter_strength[qscale];
671         DECLARE_ALIGNED(8, uint64_t, temp)[4];
672         uint8_t *btemp = (uint8_t*)temp;
673
674         src -= 2;
675
676         transpose4x4(btemp,     src,              8, stride);
677         transpose4x4(btemp + 4, src + 4 * stride, 8, stride);
678         __asm__ volatile (
679             H263_LOOP_FILTER // 5 3 4 6
680
681             : "+m"(temp[0]),
682               "+m"(temp[1]),
683               "+m"(temp[2]),
684               "+m"(temp[3])
685             : "g"(2 * strength), "m"(ff_pb_FC)
686             );
687
688         __asm__ volatile (
689             "movq      %%mm5, %%mm1         \n\t"
690             "movq      %%mm4, %%mm0         \n\t"
691             "punpcklbw %%mm3, %%mm5         \n\t"
692             "punpcklbw %%mm6, %%mm4         \n\t"
693             "punpckhbw %%mm3, %%mm1         \n\t"
694             "punpckhbw %%mm6, %%mm0         \n\t"
695             "movq      %%mm5, %%mm3         \n\t"
696             "movq      %%mm1, %%mm6         \n\t"
697             "punpcklwd %%mm4, %%mm5         \n\t"
698             "punpcklwd %%mm0, %%mm1         \n\t"
699             "punpckhwd %%mm4, %%mm3         \n\t"
700             "punpckhwd %%mm0, %%mm6         \n\t"
701             "movd      %%mm5, (%0)          \n\t"
702             "punpckhdq %%mm5, %%mm5         \n\t"
703             "movd      %%mm5, (%0, %2)      \n\t"
704             "movd      %%mm3, (%0, %2, 2)   \n\t"
705             "punpckhdq %%mm3, %%mm3         \n\t"
706             "movd      %%mm3, (%0, %3)      \n\t"
707             "movd      %%mm1, (%1)          \n\t"
708             "punpckhdq %%mm1, %%mm1         \n\t"
709             "movd      %%mm1, (%1, %2)      \n\t"
710             "movd      %%mm6, (%1, %2, 2)   \n\t"
711             "punpckhdq %%mm6, %%mm6         \n\t"
712             "movd      %%mm6, (%1, %3)      \n\t"
713             :: "r"(src),
714                "r"(src + 4 * stride),
715                "r"((x86_reg)stride),
716                "r"((x86_reg)(3 * stride))
717             );
718     }
719 }
720
721 /* Draw the edges of width 'w' of an image of size width, height
722  * this MMX version can only handle w == 8 || w == 16. */
723 static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height,
724                            int w, int h, int sides)
725 {
726     uint8_t *ptr, *last_line;
727     int i;
728
729     last_line = buf + (height - 1) * wrap;
730     /* left and right */
731     ptr = buf;
732     if (w == 8) {
733         __asm__ volatile (
734             "1:                             \n\t"
735             "movd            (%0), %%mm0    \n\t"
736             "punpcklbw      %%mm0, %%mm0    \n\t"
737             "punpcklwd      %%mm0, %%mm0    \n\t"
738             "punpckldq      %%mm0, %%mm0    \n\t"
739             "movq           %%mm0, -8(%0)   \n\t"
740             "movq      -8(%0, %2), %%mm1    \n\t"
741             "punpckhbw      %%mm1, %%mm1    \n\t"
742             "punpckhwd      %%mm1, %%mm1    \n\t"
743             "punpckhdq      %%mm1, %%mm1    \n\t"
744             "movq           %%mm1, (%0, %2) \n\t"
745             "add               %1, %0       \n\t"
746             "cmp               %3, %0       \n\t"
747             "jb                1b           \n\t"
748             : "+r"(ptr)
749             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
750             );
751     } else {
752         __asm__ volatile (
753             "1:                                 \n\t"
754             "movd            (%0), %%mm0        \n\t"
755             "punpcklbw      %%mm0, %%mm0        \n\t"
756             "punpcklwd      %%mm0, %%mm0        \n\t"
757             "punpckldq      %%mm0, %%mm0        \n\t"
758             "movq           %%mm0, -8(%0)       \n\t"
759             "movq           %%mm0, -16(%0)      \n\t"
760             "movq      -8(%0, %2), %%mm1        \n\t"
761             "punpckhbw      %%mm1, %%mm1        \n\t"
762             "punpckhwd      %%mm1, %%mm1        \n\t"
763             "punpckhdq      %%mm1, %%mm1        \n\t"
764             "movq           %%mm1,  (%0, %2)    \n\t"
765             "movq           %%mm1, 8(%0, %2)    \n\t"
766             "add               %1, %0           \n\t"
767             "cmp               %3, %0           \n\t"
768             "jb                1b               \n\t"
769             : "+r"(ptr)
770             : "r"((x86_reg)wrap), "r"((x86_reg)width), "r"(ptr + wrap * height)
771             );
772     }
773
774     /* top and bottom (and hopefully also the corners) */
775     if (sides & EDGE_TOP) {
776         for (i = 0; i < h; i += 4) {
777             ptr = buf - (i + 1) * wrap - w;
778             __asm__ volatile (
779                 "1:                             \n\t"
780                 "movq (%1, %0), %%mm0           \n\t"
781                 "movq    %%mm0, (%0)            \n\t"
782                 "movq    %%mm0, (%0, %2)        \n\t"
783                 "movq    %%mm0, (%0, %2, 2)     \n\t"
784                 "movq    %%mm0, (%0, %3)        \n\t"
785                 "add        $8, %0              \n\t"
786                 "cmp        %4, %0              \n\t"
787                 "jb         1b                  \n\t"
788                 : "+r"(ptr)
789                 : "r"((x86_reg)buf - (x86_reg)ptr - w), "r"((x86_reg) -wrap),
790                   "r"((x86_reg) -wrap * 3), "r"(ptr + width + 2 * w)
791                 );
792         }
793     }
794
795     if (sides & EDGE_BOTTOM) {
796         for (i = 0; i < h; i += 4) {
797             ptr = last_line + (i + 1) * wrap - w;
798             __asm__ volatile (
799                 "1:                             \n\t"
800                 "movq (%1, %0), %%mm0           \n\t"
801                 "movq    %%mm0, (%0)            \n\t"
802                 "movq    %%mm0, (%0, %2)        \n\t"
803                 "movq    %%mm0, (%0, %2, 2)     \n\t"
804                 "movq    %%mm0, (%0, %3)        \n\t"
805                 "add        $8, %0              \n\t"
806                 "cmp        %4, %0              \n\t"
807                 "jb         1b                  \n\t"
808                 : "+r"(ptr)
809                 : "r"((x86_reg)last_line - (x86_reg)ptr - w),
810                   "r"((x86_reg)wrap), "r"((x86_reg)wrap * 3),
811                   "r"(ptr + width + 2 * w)
812                 );
813         }
814     }
815 }
816
817 #define QPEL_V_LOW(m3, m4, m5, m6, pw_20, pw_3, rnd,                      \
818                    in0, in1, in2, in7, out, OP)                           \
819     "paddw               "#m4", "#m3"   \n\t" /* x1 */                    \
820     "movq   "MANGLE(ff_pw_20)", %%mm4   \n\t" /* 20 */                    \
821     "pmullw              "#m3", %%mm4   \n\t" /* 20x1 */                  \
822     "movq               "#in7", "#m3"   \n\t" /* d */                     \
823     "movq               "#in0", %%mm5   \n\t" /* D */                     \
824     "paddw               "#m3", %%mm5   \n\t" /* x4 */                    \
825     "psubw               %%mm5, %%mm4   \n\t" /* 20x1 - x4 */             \
826     "movq               "#in1", %%mm5   \n\t" /* C */                     \
827     "movq               "#in2", %%mm6   \n\t" /* B */                     \
828     "paddw               "#m6", %%mm5   \n\t" /* x3 */                    \
829     "paddw               "#m5", %%mm6   \n\t" /* x2 */                    \
830     "paddw               %%mm6, %%mm6   \n\t" /* 2x2 */                   \
831     "psubw               %%mm6, %%mm5   \n\t" /* -2x2 + x3 */             \
832     "pmullw  "MANGLE(ff_pw_3)", %%mm5   \n\t" /* -6x2 + 3x3 */            \
833     "paddw              "#rnd", %%mm4   \n\t" /* x2 */                    \
834     "paddw               %%mm4, %%mm5   \n\t" /* 20x1 - 6x2 + 3x3 - x4 */ \
835     "psraw                  $5, %%mm5   \n\t"                             \
836     "packuswb            %%mm5, %%mm5   \n\t"                             \
837     OP(%%mm5, out, %%mm7, d)
838
839 #define QPEL_BASE(OPNAME, ROUNDER, RND, OP_MMXEXT)                        \
840 static void OPNAME ## mpeg4_qpel16_h_lowpass_mmxext(uint8_t *dst,         \
841                                                     uint8_t *src,         \
842                                                     int dstStride,        \
843                                                     int srcStride,        \
844                                                     int h)                \
845 {                                                                         \
846     uint64_t temp;                                                        \
847                                                                           \
848     __asm__ volatile (                                                    \
849         "pxor      %%mm7, %%mm7             \n\t"                         \
850         "1:                                 \n\t"                         \
851         "movq       (%0), %%mm0             \n\t" /* ABCDEFGH */          \
852         "movq      %%mm0, %%mm1             \n\t" /* ABCDEFGH */          \
853         "movq      %%mm0, %%mm2             \n\t" /* ABCDEFGH */          \
854         "punpcklbw %%mm7, %%mm0             \n\t" /* 0A0B0C0D */          \
855         "punpckhbw %%mm7, %%mm1             \n\t" /* 0E0F0G0H */          \
856         "pshufw    $0x90, %%mm0, %%mm5      \n\t" /* 0A0A0B0C */          \
857         "pshufw    $0x41, %%mm0, %%mm6      \n\t" /* 0B0A0A0B */          \
858         "movq      %%mm2, %%mm3             \n\t" /* ABCDEFGH */          \
859         "movq      %%mm2, %%mm4             \n\t" /* ABCDEFGH */          \
860         "psllq        $8, %%mm2             \n\t" /* 0ABCDEFG */          \
861         "psllq       $16, %%mm3             \n\t" /* 00ABCDEF */          \
862         "psllq       $24, %%mm4             \n\t" /* 000ABCDE */          \
863         "punpckhbw %%mm7, %%mm2             \n\t" /* 0D0E0F0G */          \
864         "punpckhbw %%mm7, %%mm3             \n\t" /* 0C0D0E0F */          \
865         "punpckhbw %%mm7, %%mm4             \n\t" /* 0B0C0D0E */          \
866         "paddw     %%mm3, %%mm5             \n\t" /* b */                 \
867         "paddw     %%mm2, %%mm6             \n\t" /* c */                 \
868         "paddw     %%mm5, %%mm5             \n\t" /* 2b */                \
869         "psubw     %%mm5, %%mm6             \n\t" /* c - 2b */            \
870         "pshufw    $0x06, %%mm0, %%mm5      \n\t" /* 0C0B0A0A */          \
871         "pmullw "MANGLE(ff_pw_3)", %%mm6    \n\t" /* 3c - 6b */           \
872         "paddw     %%mm4, %%mm0             \n\t" /* a */                 \
873         "paddw     %%mm1, %%mm5             \n\t" /* d */                 \
874         "pmullw "MANGLE(ff_pw_20)", %%mm0   \n\t" /* 20a */               \
875         "psubw     %%mm5, %%mm0             \n\t" /* 20a - d */           \
876         "paddw        %6, %%mm6             \n\t"                         \
877         "paddw     %%mm6, %%mm0             \n\t" /* 20a - 6b + 3c - d */ \
878         "psraw        $5, %%mm0             \n\t"                         \
879         "movq      %%mm0, %5                \n\t"                         \
880         /* mm1 = EFGH, mm2 = DEFG, mm3 = CDEF, mm4 = BCDE, mm7 = 0 */     \
881                                                                           \
882         "movq      5(%0), %%mm0             \n\t" /* FGHIJKLM */          \
883         "movq      %%mm0, %%mm5             \n\t" /* FGHIJKLM */          \
884         "movq      %%mm0, %%mm6             \n\t" /* FGHIJKLM */          \
885         "psrlq        $8, %%mm0             \n\t" /* GHIJKLM0 */          \
886         "psrlq       $16, %%mm5             \n\t" /* HIJKLM00 */          \
887         "punpcklbw %%mm7, %%mm0             \n\t" /* 0G0H0I0J */          \
888         "punpcklbw %%mm7, %%mm5             \n\t" /* 0H0I0J0K */          \
889         "paddw     %%mm0, %%mm2             \n\t" /* b */                 \
890         "paddw     %%mm5, %%mm3             \n\t" /* c */                 \
891         "paddw     %%mm2, %%mm2             \n\t" /* 2b */                \
892         "psubw     %%mm2, %%mm3             \n\t" /* c - 2b */            \
893         "movq      %%mm6, %%mm2             \n\t" /* FGHIJKLM */          \
894         "psrlq       $24, %%mm6             \n\t" /* IJKLM000 */          \
895         "punpcklbw %%mm7, %%mm2             \n\t" /* 0F0G0H0I */          \
896         "punpcklbw %%mm7, %%mm6             \n\t" /* 0I0J0K0L */          \
897         "pmullw "MANGLE(ff_pw_3)", %%mm3    \n\t" /* 3c - 6b */           \
898         "paddw     %%mm2, %%mm1             \n\t" /* a */                 \
899         "paddw     %%mm6, %%mm4             \n\t" /* d */                 \
900         "pmullw "MANGLE(ff_pw_20)", %%mm1   \n\t" /* 20a */               \
901         "psubw     %%mm4, %%mm3             \n\t" /* - 6b +3c - d */      \
902         "paddw        %6, %%mm1             \n\t"                         \
903         "paddw     %%mm1, %%mm3             \n\t" /* 20a - 6b +3c - d */  \
904         "psraw        $5, %%mm3             \n\t"                         \
905         "movq         %5, %%mm1             \n\t"                         \
906         "packuswb  %%mm3, %%mm1             \n\t"                         \
907         OP_MMXEXT(%%mm1, (%1), %%mm4, q)                                  \
908         /* mm0 = GHIJ, mm2 = FGHI, mm5 = HIJK, mm6 = IJKL, mm7 = 0 */     \
909                                                                           \
910         "movq      9(%0), %%mm1             \n\t" /* JKLMNOPQ */          \
911         "movq      %%mm1, %%mm4             \n\t" /* JKLMNOPQ */          \
912         "movq      %%mm1, %%mm3             \n\t" /* JKLMNOPQ */          \
913         "psrlq        $8, %%mm1             \n\t" /* KLMNOPQ0 */          \
914         "psrlq       $16, %%mm4             \n\t" /* LMNOPQ00 */          \
915         "punpcklbw %%mm7, %%mm1             \n\t" /* 0K0L0M0N */          \
916         "punpcklbw %%mm7, %%mm4             \n\t" /* 0L0M0N0O */          \
917         "paddw     %%mm1, %%mm5             \n\t" /* b */                 \
918         "paddw     %%mm4, %%mm0             \n\t" /* c */                 \
919         "paddw     %%mm5, %%mm5             \n\t" /* 2b */                \
920         "psubw     %%mm5, %%mm0             \n\t" /* c - 2b */            \
921         "movq      %%mm3, %%mm5             \n\t" /* JKLMNOPQ */          \
922         "psrlq       $24, %%mm3             \n\t" /* MNOPQ000 */          \
923         "pmullw "MANGLE(ff_pw_3)", %%mm0    \n\t" /* 3c - 6b */           \
924         "punpcklbw %%mm7, %%mm3             \n\t" /* 0M0N0O0P */          \
925         "paddw     %%mm3, %%mm2             \n\t" /* d */                 \
926         "psubw     %%mm2, %%mm0             \n\t" /* -6b + 3c - d */      \
927         "movq      %%mm5, %%mm2             \n\t" /* JKLMNOPQ */          \
928         "punpcklbw %%mm7, %%mm2             \n\t" /* 0J0K0L0M */          \
929         "punpckhbw %%mm7, %%mm5             \n\t" /* 0N0O0P0Q */          \
930         "paddw     %%mm2, %%mm6             \n\t" /* a */                 \
931         "pmullw "MANGLE(ff_pw_20)", %%mm6   \n\t" /* 20a */               \
932         "paddw        %6, %%mm0             \n\t"                         \
933         "paddw     %%mm6, %%mm0             \n\t" /* 20a - 6b + 3c - d */ \
934         "psraw        $5, %%mm0             \n\t"                         \
935         /* mm1 = KLMN, mm2 = JKLM, mm3 = MNOP, */                         \
936         /* mm4 = LMNO, mm5 = NOPQ mm7 = 0 */                              \
937                                                                           \
938         "paddw    %%mm5, %%mm3              \n\t" /* a */                 \
939         "pshufw   $0xF9, %%mm5, %%mm6       \n\t" /* 0O0P0Q0Q */          \
940         "paddw    %%mm4, %%mm6              \n\t" /* b */                 \
941         "pshufw   $0xBE, %%mm5, %%mm4       \n\t" /* 0P0Q0Q0P */          \
942         "pshufw   $0x6F, %%mm5, %%mm5       \n\t" /* 0Q0Q0P0O */          \
943         "paddw    %%mm1, %%mm4              \n\t" /* c */                 \
944         "paddw    %%mm2, %%mm5              \n\t" /* d */                 \
945         "paddw    %%mm6, %%mm6              \n\t" /* 2b */                \
946         "psubw    %%mm6, %%mm4              \n\t" /* c - 2b */            \
947         "pmullw "MANGLE(ff_pw_20)", %%mm3   \n\t" /* 20a */               \
948         "pmullw  "MANGLE(ff_pw_3)", %%mm4   \n\t" /* 3c - 6b */           \
949         "psubw    %%mm5, %%mm3              \n\t" /* -6b + 3c - d */      \
950         "paddw       %6, %%mm4              \n\t"                         \
951         "paddw    %%mm3, %%mm4              \n\t" /* 20a - 6b + 3c - d */ \
952         "psraw       $5, %%mm4              \n\t"                         \
953         "packuswb %%mm4, %%mm0              \n\t"                         \
954         OP_MMXEXT(%%mm0, 8(%1), %%mm4, q)                                 \
955                                                                           \
956         "add         %3, %0                 \n\t"                         \
957         "add         %4, %1                 \n\t"                         \
958         "decl        %2                     \n\t"                         \
959         "jnz         1b                     \n\t"                         \
960         : "+a"(src), "+c"(dst), "+D"(h)                                   \
961         : "d"((x86_reg)srcStride), "S"((x86_reg)dstStride),               \
962           /* "m"(ff_pw_20), "m"(ff_pw_3), */ "m"(temp), "m"(ROUNDER)      \
963         : "memory"                                                        \
964         );                                                                \
965 }                                                                         \
966                                                                           \
967 static void OPNAME ## mpeg4_qpel8_h_lowpass_mmxext(uint8_t *dst,          \
968                                                    uint8_t *src,          \
969                                                    int dstStride,         \
970                                                    int srcStride,         \
971                                                    int h)                 \
972 {                                                                         \
973     __asm__ volatile (                                                    \
974         "pxor      %%mm7, %%mm7             \n\t"                         \
975         "1:                                 \n\t"                         \
976         "movq       (%0), %%mm0             \n\t" /* ABCDEFGH */          \
977         "movq      %%mm0, %%mm1             \n\t" /* ABCDEFGH */          \
978         "movq      %%mm0, %%mm2             \n\t" /* ABCDEFGH */          \
979         "punpcklbw %%mm7, %%mm0             \n\t" /* 0A0B0C0D */          \
980         "punpckhbw %%mm7, %%mm1             \n\t" /* 0E0F0G0H */          \
981         "pshufw    $0x90, %%mm0, %%mm5      \n\t" /* 0A0A0B0C */          \
982         "pshufw    $0x41, %%mm0, %%mm6      \n\t" /* 0B0A0A0B */          \
983         "movq      %%mm2, %%mm3             \n\t" /* ABCDEFGH */          \
984         "movq      %%mm2, %%mm4             \n\t" /* ABCDEFGH */          \
985         "psllq        $8, %%mm2             \n\t" /* 0ABCDEFG */          \
986         "psllq       $16, %%mm3             \n\t" /* 00ABCDEF */          \
987         "psllq       $24, %%mm4             \n\t" /* 000ABCDE */          \
988         "punpckhbw %%mm7, %%mm2             \n\t" /* 0D0E0F0G */          \
989         "punpckhbw %%mm7, %%mm3             \n\t" /* 0C0D0E0F */          \
990         "punpckhbw %%mm7, %%mm4             \n\t" /* 0B0C0D0E */          \
991         "paddw     %%mm3, %%mm5             \n\t" /* b */                 \
992         "paddw     %%mm2, %%mm6             \n\t" /* c */                 \
993         "paddw     %%mm5, %%mm5             \n\t" /* 2b */                \
994         "psubw     %%mm5, %%mm6             \n\t" /* c - 2b */            \
995         "pshufw    $0x06, %%mm0, %%mm5      \n\t" /* 0C0B0A0A */          \
996         "pmullw "MANGLE(ff_pw_3)", %%mm6    \n\t" /* 3c - 6b */           \
997         "paddw     %%mm4, %%mm0             \n\t" /* a */                 \
998         "paddw     %%mm1, %%mm5             \n\t" /* d */                 \
999         "pmullw "MANGLE(ff_pw_20)", %%mm0   \n\t" /* 20a */               \
1000         "psubw     %%mm5, %%mm0             \n\t" /* 20a - d */           \
1001         "paddw        %5, %%mm6             \n\t"                         \
1002         "paddw     %%mm6, %%mm0             \n\t" /* 20a - 6b + 3c - d */ \
1003         "psraw        $5, %%mm0             \n\t"                         \
1004         /* mm1 = EFGH, mm2 = DEFG, mm3 = CDEF, mm4 = BCDE, mm7 = 0 */     \
1005                                                                           \
1006         "movd      5(%0), %%mm5             \n\t" /* FGHI */              \
1007         "punpcklbw %%mm7, %%mm5             \n\t" /* 0F0G0H0I */          \
1008         "pshufw    $0xF9, %%mm5, %%mm6      \n\t" /* 0G0H0I0I */          \
1009         "paddw     %%mm5, %%mm1             \n\t" /* a */                 \
1010         "paddw     %%mm6, %%mm2             \n\t" /* b */                 \
1011         "pshufw    $0xBE, %%mm5, %%mm6      \n\t" /* 0H0I0I0H */          \
1012         "pshufw    $0x6F, %%mm5, %%mm5      \n\t" /* 0I0I0H0G */          \
1013         "paddw     %%mm6, %%mm3             \n\t" /* c */                 \
1014         "paddw     %%mm5, %%mm4             \n\t" /* d */                 \
1015         "paddw     %%mm2, %%mm2             \n\t" /* 2b */                \
1016         "psubw     %%mm2, %%mm3             \n\t" /* c - 2b */            \
1017         "pmullw "MANGLE(ff_pw_20)", %%mm1   \n\t" /* 20a */               \
1018         "pmullw  "MANGLE(ff_pw_3)", %%mm3   \n\t" /* 3c - 6b */           \
1019         "psubw     %%mm4, %%mm3             \n\t" /* -6b + 3c - d */      \
1020         "paddw        %5, %%mm1             \n\t"                         \
1021         "paddw     %%mm1, %%mm3             \n\t" /* 20a - 6b + 3c - d */ \
1022         "psraw        $5, %%mm3             \n\t"                         \
1023         "packuswb  %%mm3, %%mm0             \n\t"                         \
1024         OP_MMXEXT(%%mm0, (%1), %%mm4, q)                                  \
1025                                                                           \
1026         "add          %3, %0                \n\t"                         \
1027         "add          %4, %1                \n\t"                         \
1028         "decl         %2                    \n\t"                         \
1029         "jnz          1b                    \n\t"                         \
1030         : "+a"(src), "+c"(dst), "+d"(h)                                   \
1031         : "S"((x86_reg)srcStride), "D"((x86_reg)dstStride),               \
1032           /* "m"(ff_pw_20), "m"(ff_pw_3), */ "m"(ROUNDER)                 \
1033         : "memory"                                                        \
1034         );                                                                \
1035 }
1036
1037 #define QPEL_OP(OPNAME, ROUNDER, RND, OP, MMX)                          \
1038 static void OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(uint8_t *dst,      \
1039                                                      uint8_t *src,      \
1040                                                      int dstStride,     \
1041                                                      int srcStride)     \
1042 {                                                                       \
1043     uint64_t temp[17 * 4];                                              \
1044     uint64_t *temp_ptr = temp;                                          \
1045     int count = 17;                                                     \
1046                                                                         \
1047     /* FIXME unroll */                                                  \
1048     __asm__ volatile (                                                  \
1049         "pxor      %%mm7, %%mm7             \n\t"                       \
1050         "1:                                 \n\t"                       \
1051         "movq       (%0), %%mm0             \n\t"                       \
1052         "movq       (%0), %%mm1             \n\t"                       \
1053         "movq      8(%0), %%mm2             \n\t"                       \
1054         "movq      8(%0), %%mm3             \n\t"                       \
1055         "punpcklbw %%mm7, %%mm0             \n\t"                       \
1056         "punpckhbw %%mm7, %%mm1             \n\t"                       \
1057         "punpcklbw %%mm7, %%mm2             \n\t"                       \
1058         "punpckhbw %%mm7, %%mm3             \n\t"                       \
1059         "movq      %%mm0, (%1)              \n\t"                       \
1060         "movq      %%mm1, 17 * 8(%1)        \n\t"                       \
1061         "movq      %%mm2, 2 * 17 * 8(%1)    \n\t"                       \
1062         "movq      %%mm3, 3 * 17 * 8(%1)    \n\t"                       \
1063         "add          $8, %1                \n\t"                       \
1064         "add          %3, %0                \n\t"                       \
1065         "decl         %2                    \n\t"                       \
1066         "jnz          1b                    \n\t"                       \
1067         : "+r"(src), "+r"(temp_ptr), "+r"(count)                        \
1068         : "r"((x86_reg)srcStride)                                       \
1069         : "memory"                                                      \
1070         );                                                              \
1071                                                                         \
1072     temp_ptr = temp;                                                    \
1073     count    = 4;                                                       \
1074                                                                         \
1075     /* FIXME reorder for speed */                                       \
1076     __asm__ volatile (                                                  \
1077         /* "pxor  %%mm7, %%mm7            \n\t" */                      \
1078         "1:                             \n\t"                           \
1079         "movq    (%0), %%mm0            \n\t"                           \
1080         "movq   8(%0), %%mm1            \n\t"                           \
1081         "movq  16(%0), %%mm2            \n\t"                           \
1082         "movq  24(%0), %%mm3            \n\t"                           \
1083         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 16(%0),   8(%0),    (%0),  32(%0), (%1),     OP) \
1084         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5,  8(%0),    (%0),    (%0),  40(%0), (%1, %3), OP) \
1085         "add       %4, %1               \n\t"                           \
1086         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5,   (%0),    (%0),   8(%0),  48(%0), (%1),     OP) \
1087                                                                         \
1088         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5,   (%0),   8(%0),  16(%0),  56(%0), (%1, %3), OP) \
1089         "add       %4, %1               \n\t"                           \
1090         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5,  8(%0),  16(%0),  24(%0),  64(%0), (%1),     OP) \
1091         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 16(%0),  24(%0),  32(%0),  72(%0), (%1, %3), OP) \
1092         "add       %4, %1               \n\t"                           \
1093         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 24(%0),  32(%0),  40(%0),  80(%0), (%1),     OP) \
1094         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 32(%0),  40(%0),  48(%0),  88(%0), (%1, %3), OP) \
1095         "add       %4, %1               \n\t"                           \
1096         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 40(%0),  48(%0),  56(%0),  96(%0), (%1),     OP) \
1097         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 48(%0),  56(%0),  64(%0), 104(%0), (%1, %3), OP) \
1098         "add       %4, %1               \n\t"                           \
1099         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 56(%0),  64(%0),  72(%0), 112(%0), (%1),     OP) \
1100         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 64(%0),  72(%0),  80(%0), 120(%0), (%1, %3), OP) \
1101         "add       %4, %1               \n\t"                           \
1102         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 72(%0),  80(%0),  88(%0), 128(%0), (%1),     OP) \
1103                                                                         \
1104         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 80(%0),  88(%0),  96(%0), 128(%0), (%1, %3), OP) \
1105         "add       %4, %1               \n\t"                           \
1106         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 88(%0),  96(%0), 104(%0), 120(%0), (%1),     OP) \
1107         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 96(%0), 104(%0), 112(%0), 112(%0), (%1, %3), OP) \
1108                                                                         \
1109         "add     $136, %0               \n\t"                           \
1110         "add       %6, %1               \n\t"                           \
1111         "decl      %2                   \n\t"                           \
1112         "jnz       1b                   \n\t"                           \
1113                                                                         \
1114         : "+r"(temp_ptr), "+r"(dst), "+g"(count)                        \
1115         : "r"((x86_reg)dstStride), "r"(2 * (x86_reg)dstStride),         \
1116           /* "m"(ff_pw_20), "m"(ff_pw_3), */ "m"(ROUNDER),              \
1117           "g"(4 - 14 * (x86_reg)dstStride)                              \
1118         : "memory"                                                      \
1119         );                                                              \
1120 }                                                                       \
1121                                                                         \
1122 static void OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(uint8_t *dst,       \
1123                                                     uint8_t *src,       \
1124                                                     int dstStride,      \
1125                                                     int srcStride)      \
1126 {                                                                       \
1127     uint64_t temp[9 * 2];                                               \
1128     uint64_t *temp_ptr = temp;                                          \
1129     int count = 9;                                                      \
1130                                                                         \
1131     /* FIXME unroll */                                                  \
1132     __asm__ volatile (                                                  \
1133         "pxor      %%mm7, %%mm7         \n\t"                           \
1134         "1:                             \n\t"                           \
1135         "movq       (%0), %%mm0         \n\t"                           \
1136         "movq       (%0), %%mm1         \n\t"                           \
1137         "punpcklbw %%mm7, %%mm0         \n\t"                           \
1138         "punpckhbw %%mm7, %%mm1         \n\t"                           \
1139         "movq      %%mm0, (%1)          \n\t"                           \
1140         "movq      %%mm1, 9*8(%1)       \n\t"                           \
1141         "add          $8, %1            \n\t"                           \
1142         "add          %3, %0            \n\t"                           \
1143         "decl         %2                \n\t"                           \
1144         "jnz          1b                \n\t"                           \
1145         : "+r"(src), "+r"(temp_ptr), "+r"(count)                        \
1146         : "r"((x86_reg)srcStride)                                       \
1147         : "memory"                                                      \
1148         );                                                              \
1149                                                                         \
1150     temp_ptr = temp;                                                    \
1151     count    = 2;                                                       \
1152                                                                         \
1153     /* FIXME reorder for speed */                                       \
1154     __asm__ volatile (                                                  \
1155         /* "pxor  %%mm7, %%mm7            \n\t" */                      \
1156         "1:                             \n\t"                           \
1157         "movq    (%0), %%mm0            \n\t"                           \
1158         "movq   8(%0), %%mm1            \n\t"                           \
1159         "movq  16(%0), %%mm2            \n\t"                           \
1160         "movq  24(%0), %%mm3            \n\t"                           \
1161         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 16(%0),  8(%0),   (%0), 32(%0), (%1), OP)     \
1162         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5,  8(%0),   (%0),   (%0), 40(%0), (%1, %3), OP) \
1163         "add       %4, %1               \n\t"                           \
1164         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5,   (%0),   (%0),  8(%0), 48(%0), (%1), OP)     \
1165                                                                         \
1166         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5,   (%0),  8(%0), 16(%0), 56(%0), (%1, %3), OP) \
1167         "add       %4, %1               \n\t"                           \
1168         QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5,  8(%0), 16(%0), 24(%0), 64(%0), (%1), OP)     \
1169                                                                         \
1170         QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 16(%0), 24(%0), 32(%0), 64(%0), (%1, %3), OP) \
1171         "add       %4, %1               \n\t"                           \
1172         QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 24(%0), 32(%0), 40(%0), 56(%0), (%1), OP)     \
1173         QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 32(%0), 40(%0), 48(%0), 48(%0), (%1, %3), OP) \
1174                                                                         \
1175         "add      $72, %0               \n\t"                           \
1176         "add       %6, %1               \n\t"                           \
1177         "decl      %2                   \n\t"                           \
1178         "jnz       1b                   \n\t"                           \
1179                                                                         \
1180         : "+r"(temp_ptr), "+r"(dst), "+g"(count)                        \
1181         : "r"((x86_reg)dstStride), "r"(2 * (x86_reg)dstStride),         \
1182           /* "m"(ff_pw_20), "m"(ff_pw_3), */ "m"(ROUNDER),              \
1183           "g"(4 - 6 * (x86_reg)dstStride)                               \
1184         : "memory"                                                      \
1185         );                                                              \
1186 }                                                                       \
1187                                                                         \
1188 static void OPNAME ## qpel8_mc00_ ## MMX (uint8_t *dst, uint8_t *src,   \
1189                                           int stride)                   \
1190 {                                                                       \
1191     OPNAME ## pixels8_ ## MMX(dst, src, stride, 8);                     \
1192 }                                                                       \
1193                                                                         \
1194 static void OPNAME ## qpel8_mc10_ ## MMX(uint8_t *dst, uint8_t *src,    \
1195                                          int stride)                    \
1196 {                                                                       \
1197     uint64_t temp[8];                                                   \
1198     uint8_t * const half = (uint8_t*)temp;                              \
1199     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8,           \
1200                                                 stride, 8);             \
1201     OPNAME ## pixels8_l2_ ## MMX(dst, src, half, stride, stride, 8);    \
1202 }                                                                       \
1203                                                                         \
1204 static void OPNAME ## qpel8_mc20_ ## MMX(uint8_t *dst, uint8_t *src,    \
1205                                          int stride)                    \
1206 {                                                                       \
1207     OPNAME ## mpeg4_qpel8_h_lowpass_ ## MMX(dst, src, stride,           \
1208                                             stride, 8);                 \
1209 }                                                                       \
1210                                                                         \
1211 static void OPNAME ## qpel8_mc30_ ## MMX(uint8_t *dst, uint8_t *src,    \
1212                                          int stride)                    \
1213 {                                                                       \
1214     uint64_t temp[8];                                                   \
1215     uint8_t * const half = (uint8_t*)temp;                              \
1216     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8,           \
1217                                                 stride, 8);             \
1218     OPNAME ## pixels8_l2_ ## MMX(dst, src + 1, half, stride,            \
1219                                  stride, 8);                            \
1220 }                                                                       \
1221                                                                         \
1222 static void OPNAME ## qpel8_mc01_ ## MMX(uint8_t *dst, uint8_t *src,    \
1223                                          int stride)                    \
1224 {                                                                       \
1225     uint64_t temp[8];                                                   \
1226     uint8_t * const half = (uint8_t*)temp;                              \
1227     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, 8, stride);  \
1228     OPNAME ## pixels8_l2_ ## MMX(dst, src, half, stride, stride, 8);    \
1229 }                                                                       \
1230                                                                         \
1231 static void OPNAME ## qpel8_mc02_ ## MMX(uint8_t *dst, uint8_t *src,    \
1232                                          int stride)                    \
1233 {                                                                       \
1234     OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, src, stride, stride);  \
1235 }                                                                       \
1236                                                                         \
1237 static void OPNAME ## qpel8_mc03_ ## MMX(uint8_t *dst, uint8_t *src,    \
1238                                          int stride)                    \
1239 {                                                                       \
1240     uint64_t temp[8];                                                   \
1241     uint8_t * const half = (uint8_t*)temp;                              \
1242     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, 8, stride);  \
1243     OPNAME ## pixels8_l2_ ## MMX(dst, src + stride, half, stride,       \
1244                                  stride, 8);                            \
1245 }                                                                       \
1246                                                                         \
1247 static void OPNAME ## qpel8_mc11_ ## MMX(uint8_t *dst, uint8_t *src,    \
1248                                          int stride)                    \
1249 {                                                                       \
1250     uint64_t half[8 + 9];                                               \
1251     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
1252     uint8_t * const halfHV = ((uint8_t*)half);                          \
1253     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,          \
1254                                                 stride, 9);             \
1255     put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);  \
1256     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);   \
1257     OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);     \
1258 }                                                                       \
1259                                                                         \
1260 static void OPNAME ## qpel8_mc31_ ## MMX(uint8_t *dst, uint8_t *src,    \
1261                                          int stride)                    \
1262 {                                                                       \
1263     uint64_t half[8 + 9];                                               \
1264     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
1265     uint8_t * const halfHV = ((uint8_t*)half);                          \
1266     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,          \
1267                                                 stride, 9);             \
1268     put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,          \
1269                                      stride, 9);                        \
1270     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);   \
1271     OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);     \
1272 }                                                                       \
1273                                                                         \
1274 static void OPNAME ## qpel8_mc13_ ## MMX(uint8_t *dst, uint8_t *src,    \
1275                                          int stride)                    \
1276 {                                                                       \
1277     uint64_t half[8 + 9];                                               \
1278     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
1279     uint8_t * const halfHV = ((uint8_t*)half);                          \
1280     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,          \
1281                                                 stride, 9);             \
1282     put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);  \
1283     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);   \
1284     OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV, stride, 8, 8); \
1285 }                                                                       \
1286                                                                         \
1287 static void OPNAME ## qpel8_mc33_ ## MMX(uint8_t *dst, uint8_t *src,    \
1288                                          int stride)                    \
1289 {                                                                       \
1290     uint64_t half[8 + 9];                                               \
1291     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
1292     uint8_t * const halfHV = ((uint8_t*)half);                          \
1293     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,          \
1294                                                 stride, 9);             \
1295     put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,          \
1296                                      stride, 9);                        \
1297     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);   \
1298     OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV, stride, 8, 8); \
1299 }                                                                       \
1300                                                                         \
1301 static void OPNAME ## qpel8_mc21_ ## MMX(uint8_t *dst, uint8_t *src,    \
1302                                          int stride)                    \
1303 {                                                                       \
1304     uint64_t half[8 + 9];                                               \
1305     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
1306     uint8_t * const halfHV = ((uint8_t*)half);                          \
1307     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,          \
1308                                                 stride, 9);             \
1309     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);   \
1310     OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);     \
1311 }                                                                       \
1312                                                                         \
1313 static void OPNAME ## qpel8_mc23_ ## MMX(uint8_t *dst, uint8_t *src,    \
1314                                          int stride)                    \
1315 {                                                                       \
1316     uint64_t half[8 + 9];                                               \
1317     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
1318     uint8_t * const halfHV = ((uint8_t*)half);                          \
1319     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,          \
1320                                                 stride, 9);             \
1321     put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);   \
1322     OPNAME ## pixels8_l2_ ## MMX(dst, halfH + 8, halfHV, stride, 8, 8); \
1323 }                                                                       \
1324                                                                         \
1325 static void OPNAME ## qpel8_mc12_ ## MMX(uint8_t *dst, uint8_t *src,    \
1326                                          int stride)                    \
1327 {                                                                       \
1328     uint64_t half[8 + 9];                                               \
1329     uint8_t * const halfH = ((uint8_t*)half);                           \
1330     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,          \
1331                                                 stride, 9);             \
1332     put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);  \
1333     OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);     \
1334 }                                                                       \
1335                                                                         \
1336 static void OPNAME ## qpel8_mc32_ ## MMX(uint8_t *dst, uint8_t *src,    \
1337                                          int stride)                    \
1338 {                                                                       \
1339     uint64_t half[8 + 9];                                               \
1340     uint8_t * const halfH = ((uint8_t*)half);                           \
1341     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,          \
1342                                                 stride, 9);             \
1343     put ## RND ## pixels8_l2_ ## MMX(halfH, src + 1, halfH, 8,          \
1344                                      stride, 9);                        \
1345     OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);     \
1346 }                                                                       \
1347                                                                         \
1348 static void OPNAME ## qpel8_mc22_ ## MMX(uint8_t *dst, uint8_t *src,    \
1349                                          int stride)                    \
1350 {                                                                       \
1351     uint64_t half[9];                                                   \
1352     uint8_t * const halfH = ((uint8_t*)half);                           \
1353     put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8,          \
1354                                                 stride, 9);             \
1355     OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);     \
1356 }                                                                       \
1357                                                                         \
1358 static void OPNAME ## qpel16_mc00_ ## MMX (uint8_t *dst, uint8_t *src,  \
1359                                            int stride)                  \
1360 {                                                                       \
1361     OPNAME ## pixels16_ ## MMX(dst, src, stride, 16);                   \
1362 }                                                                       \
1363                                                                         \
1364 static void OPNAME ## qpel16_mc10_ ## MMX(uint8_t *dst, uint8_t *src,   \
1365                                           int stride)                   \
1366 {                                                                       \
1367     uint64_t temp[32];                                                  \
1368     uint8_t * const half = (uint8_t*)temp;                              \
1369     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16,         \
1370                                                  stride, 16);           \
1371     OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, stride, 16);  \
1372 }                                                                       \
1373                                                                         \
1374 static void OPNAME ## qpel16_mc20_ ## MMX(uint8_t *dst, uint8_t *src,   \
1375                                           int stride)                   \
1376 {                                                                       \
1377     OPNAME ## mpeg4_qpel16_h_lowpass_ ## MMX(dst, src,                  \
1378                                              stride, stride, 16);       \
1379 }                                                                       \
1380                                                                         \
1381 static void OPNAME ## qpel16_mc30_ ## MMX(uint8_t *dst, uint8_t *src,   \
1382                                           int stride)                   \
1383 {                                                                       \
1384     uint64_t temp[32];                                                  \
1385     uint8_t * const half = (uint8_t*)temp;                              \
1386     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16,         \
1387                                                  stride, 16);           \
1388     OPNAME ## pixels16_l2_ ## MMX(dst, src + 1, half,                   \
1389                                   stride, stride, 16);                  \
1390 }                                                                       \
1391                                                                         \
1392 static void OPNAME ## qpel16_mc01_ ## MMX(uint8_t *dst, uint8_t *src,   \
1393                                           int stride)                   \
1394 {                                                                       \
1395     uint64_t temp[32];                                                  \
1396     uint8_t * const half = (uint8_t*)temp;                              \
1397     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16,         \
1398                                                  stride);               \
1399     OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, stride, 16);  \
1400 }                                                                       \
1401                                                                         \
1402 static void OPNAME ## qpel16_mc02_ ## MMX(uint8_t *dst, uint8_t *src,   \
1403                                           int stride)                   \
1404 {                                                                       \
1405     OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, src, stride, stride); \
1406 }                                                                       \
1407                                                                         \
1408 static void OPNAME ## qpel16_mc03_ ## MMX(uint8_t *dst, uint8_t *src,   \
1409                                           int stride)                   \
1410 {                                                                       \
1411     uint64_t temp[32];                                                  \
1412     uint8_t * const half = (uint8_t*)temp;                              \
1413     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16,         \
1414                                                  stride);               \
1415     OPNAME ## pixels16_l2_ ## MMX(dst, src+stride, half,                \
1416                                   stride, stride, 16);                  \
1417 }                                                                       \
1418                                                                         \
1419 static void OPNAME ## qpel16_mc11_ ## MMX(uint8_t *dst, uint8_t *src,   \
1420                                           int stride)                   \
1421 {                                                                       \
1422     uint64_t half[16 * 2 + 17 * 2];                                     \
1423     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1424     uint8_t * const halfHV = ((uint8_t*)half);                          \
1425     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,        \
1426                                                  stride, 17);           \
1427     put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,            \
1428                                       stride, 17);                      \
1429     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,         \
1430                                                  16, 16);               \
1431     OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);  \
1432 }                                                                       \
1433                                                                         \
1434 static void OPNAME ## qpel16_mc31_ ## MMX(uint8_t *dst, uint8_t *src,   \
1435                                           int stride)                   \
1436 {                                                                       \
1437     uint64_t half[16 * 2 + 17 * 2];                                     \
1438     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1439     uint8_t * const halfHV = ((uint8_t*)half);                          \
1440     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,        \
1441                                                  stride, 17);           \
1442     put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,        \
1443                                       stride, 17);                      \
1444     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,         \
1445                                                  16, 16);               \
1446     OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);  \
1447 }                                                                       \
1448                                                                         \
1449 static void OPNAME ## qpel16_mc13_ ## MMX(uint8_t *dst, uint8_t *src,   \
1450                                           int stride)                   \
1451 {                                                                       \
1452     uint64_t half[16 * 2 + 17 * 2];                                     \
1453     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1454     uint8_t * const halfHV = ((uint8_t*)half);                          \
1455     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,        \
1456                                                  stride, 17);           \
1457     put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,            \
1458                                       stride, 17);                      \
1459     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,         \
1460                                                  16, 16);               \
1461     OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV, stride,      \
1462                                   16, 16);                              \
1463 }                                                                       \
1464                                                                         \
1465 static void OPNAME ## qpel16_mc33_ ## MMX(uint8_t *dst, uint8_t *src,   \
1466                                           int stride)                   \
1467 {                                                                       \
1468     uint64_t half[16 * 2 + 17 * 2];                                     \
1469     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1470     uint8_t * const halfHV = ((uint8_t*)half);                          \
1471     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,        \
1472                                                  stride, 17);           \
1473     put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,        \
1474                                       stride, 17);                      \
1475     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,         \
1476                                                  16, 16);               \
1477     OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV, stride,      \
1478                                   16, 16);                              \
1479 }                                                                       \
1480                                                                         \
1481 static void OPNAME ## qpel16_mc21_ ## MMX(uint8_t *dst, uint8_t *src,   \
1482                                           int stride)                   \
1483 {                                                                       \
1484     uint64_t half[16 * 2 + 17 * 2];                                     \
1485     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1486     uint8_t * const halfHV = ((uint8_t*)half);                          \
1487     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,        \
1488                                                  stride, 17);           \
1489     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,         \
1490                                                  16, 16);               \
1491     OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);  \
1492 }                                                                       \
1493                                                                         \
1494 static void OPNAME ## qpel16_mc23_ ## MMX(uint8_t *dst, uint8_t *src,   \
1495                                           int stride)                   \
1496 {                                                                       \
1497     uint64_t half[16 * 2 + 17 * 2];                                     \
1498     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
1499     uint8_t * const halfHV = ((uint8_t*)half);                          \
1500     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,        \
1501                                                  stride, 17);           \
1502     put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH,         \
1503                                                  16, 16);               \
1504     OPNAME ## pixels16_l2_ ## MMX(dst, halfH + 16, halfHV, stride,      \
1505                                   16, 16);                              \
1506 }                                                                       \
1507                                                                         \
1508 static void OPNAME ## qpel16_mc12_ ## MMX(uint8_t *dst, uint8_t *src,   \
1509                                           int stride)                   \
1510 {                                                                       \
1511     uint64_t half[17 * 2];                                              \
1512     uint8_t * const halfH = ((uint8_t*)half);                           \
1513     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,        \
1514                                                  stride, 17);           \
1515     put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16,            \
1516                                       stride, 17);                      \
1517     OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);   \
1518 }                                                                       \
1519                                                                         \
1520 static void OPNAME ## qpel16_mc32_ ## MMX(uint8_t *dst, uint8_t *src,   \
1521                                           int stride)                   \
1522 {                                                                       \
1523     uint64_t half[17 * 2];                                              \
1524     uint8_t * const halfH = ((uint8_t*)half);                           \
1525     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,        \
1526                                                  stride, 17);           \
1527     put ## RND ## pixels16_l2_ ## MMX(halfH, src + 1, halfH, 16,        \
1528                                       stride, 17);                      \
1529     OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);   \
1530 }                                                                       \
1531                                                                         \
1532 static void OPNAME ## qpel16_mc22_ ## MMX(uint8_t *dst, uint8_t *src,   \
1533                                           int stride)                   \
1534 {                                                                       \
1535     uint64_t half[17 * 2];                                              \
1536     uint8_t * const halfH = ((uint8_t*)half);                           \
1537     put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16,        \
1538                                                  stride, 17);           \
1539     OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);   \
1540 }
1541
1542 #define PUT_OP(a, b, temp, size)                \
1543     "mov"#size"        "#a", "#b"       \n\t"
1544
1545 #define AVG_MMXEXT_OP(a, b, temp, size)         \
1546     "mov"#size"        "#b", "#temp"    \n\t"   \
1547     "pavgb          "#temp", "#a"       \n\t"   \
1548     "mov"#size"        "#a", "#b"       \n\t"
1549
1550 QPEL_BASE(put_,        ff_pw_16, _,        PUT_OP)
1551 QPEL_BASE(avg_,        ff_pw_16, _,        AVG_MMXEXT_OP)
1552 QPEL_BASE(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP)
1553 QPEL_OP(put_,          ff_pw_16, _,        PUT_OP,        mmxext)
1554 QPEL_OP(avg_,          ff_pw_16, _,        AVG_MMXEXT_OP, mmxext)
1555 QPEL_OP(put_no_rnd_,   ff_pw_15, _no_rnd_, PUT_OP,        mmxext)
1556
1557 void ff_put_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
1558 {
1559   put_pixels8_xy2_mmx(dst, src, stride, 8);
1560 }
1561 void ff_put_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
1562 {
1563   put_pixels16_xy2_mmx(dst, src, stride, 16);
1564 }
1565 void ff_avg_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
1566 {
1567   avg_pixels8_xy2_mmx(dst, src, stride, 8);
1568 }
1569 void ff_avg_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
1570 {
1571   avg_pixels16_xy2_mmx(dst, src, stride, 16);
1572 }
1573
1574 static void gmc_mmx(uint8_t *dst, uint8_t *src,
1575                     int stride, int h, int ox, int oy,
1576                     int dxx, int dxy, int dyx, int dyy,
1577                     int shift, int r, int width, int height)
1578 {
1579     const int w    = 8;
1580     const int ix   = ox  >> (16 + shift);
1581     const int iy   = oy  >> (16 + shift);
1582     const int oxs  = ox  >> 4;
1583     const int oys  = oy  >> 4;
1584     const int dxxs = dxx >> 4;
1585     const int dxys = dxy >> 4;
1586     const int dyxs = dyx >> 4;
1587     const int dyys = dyy >> 4;
1588     const uint16_t r4[4]   = { r, r, r, r };
1589     const uint16_t dxy4[4] = { dxys, dxys, dxys, dxys };
1590     const uint16_t dyy4[4] = { dyys, dyys, dyys, dyys };
1591     const uint64_t shift2 = 2 * shift;
1592     int x, y;
1593
1594     const int dxw = (dxx - (1 << (16 + shift))) * (w - 1);
1595     const int dyh = (dyy - (1 << (16 + shift))) * (h - 1);
1596     const int dxh = dxy * (h - 1);
1597     const int dyw = dyx * (w - 1);
1598     if ( // non-constant fullpel offset (3% of blocks)
1599         ((ox ^ (ox + dxw)) | (ox ^ (ox + dxh)) | (ox ^ (ox + dxw + dxh)) |
1600          (oy ^ (oy + dyw)) | (oy ^ (oy + dyh)) | (oy ^ (oy + dyw + dyh))) >> (16 + shift)
1601         // uses more than 16 bits of subpel mv (only at huge resolution)
1602         || (dxx | dxy | dyx | dyy) & 15 ||
1603         (unsigned)ix >= width  - w ||
1604         (unsigned)iy >= height - h) {
1605         // FIXME could still use mmx for some of the rows
1606         ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy,
1607                  shift, r, width, height);
1608         return;
1609     }
1610
1611     src += ix + iy * stride;
1612
1613     __asm__ volatile (
1614         "movd         %0, %%mm6         \n\t"
1615         "pxor      %%mm7, %%mm7         \n\t"
1616         "punpcklwd %%mm6, %%mm6         \n\t"
1617         "punpcklwd %%mm6, %%mm6         \n\t"
1618         :: "r"(1<<shift)
1619     );
1620
1621     for (x = 0; x < w; x += 4) {
1622         uint16_t dx4[4] = { oxs - dxys + dxxs * (x + 0),
1623                             oxs - dxys + dxxs * (x + 1),
1624                             oxs - dxys + dxxs * (x + 2),
1625                             oxs - dxys + dxxs * (x + 3) };
1626         uint16_t dy4[4] = { oys - dyys + dyxs * (x + 0),
1627                             oys - dyys + dyxs * (x + 1),
1628                             oys - dyys + dyxs * (x + 2),
1629                             oys - dyys + dyxs * (x + 3) };
1630
1631         for (y = 0; y < h; y++) {
1632             __asm__ volatile (
1633                 "movq      %0, %%mm4    \n\t"
1634                 "movq      %1, %%mm5    \n\t"
1635                 "paddw     %2, %%mm4    \n\t"
1636                 "paddw     %3, %%mm5    \n\t"
1637                 "movq   %%mm4, %0       \n\t"
1638                 "movq   %%mm5, %1       \n\t"
1639                 "psrlw    $12, %%mm4    \n\t"
1640                 "psrlw    $12, %%mm5    \n\t"
1641                 : "+m"(*dx4), "+m"(*dy4)
1642                 : "m"(*dxy4), "m"(*dyy4)
1643             );
1644
1645             __asm__ volatile (
1646                 "movq      %%mm6, %%mm2 \n\t"
1647                 "movq      %%mm6, %%mm1 \n\t"
1648                 "psubw     %%mm4, %%mm2 \n\t"
1649                 "psubw     %%mm5, %%mm1 \n\t"
1650                 "movq      %%mm2, %%mm0 \n\t"
1651                 "movq      %%mm4, %%mm3 \n\t"
1652                 "pmullw    %%mm1, %%mm0 \n\t" // (s - dx) * (s - dy)
1653                 "pmullw    %%mm5, %%mm3 \n\t" // dx * dy
1654                 "pmullw    %%mm5, %%mm2 \n\t" // (s - dx) * dy
1655                 "pmullw    %%mm4, %%mm1 \n\t" // dx * (s - dy)
1656
1657                 "movd         %4, %%mm5 \n\t"
1658                 "movd         %3, %%mm4 \n\t"
1659                 "punpcklbw %%mm7, %%mm5 \n\t"
1660                 "punpcklbw %%mm7, %%mm4 \n\t"
1661                 "pmullw    %%mm5, %%mm3 \n\t" // src[1, 1] * dx * dy
1662                 "pmullw    %%mm4, %%mm2 \n\t" // src[0, 1] * (s - dx) * dy
1663
1664                 "movd         %2, %%mm5 \n\t"
1665                 "movd         %1, %%mm4 \n\t"
1666                 "punpcklbw %%mm7, %%mm5 \n\t"
1667                 "punpcklbw %%mm7, %%mm4 \n\t"
1668                 "pmullw    %%mm5, %%mm1 \n\t" // src[1, 0] * dx * (s - dy)
1669                 "pmullw    %%mm4, %%mm0 \n\t" // src[0, 0] * (s - dx) * (s - dy)
1670                 "paddw        %5, %%mm1 \n\t"
1671                 "paddw     %%mm3, %%mm2 \n\t"
1672                 "paddw     %%mm1, %%mm0 \n\t"
1673                 "paddw     %%mm2, %%mm0 \n\t"
1674
1675                 "psrlw        %6, %%mm0 \n\t"
1676                 "packuswb  %%mm0, %%mm0 \n\t"
1677                 "movd      %%mm0, %0    \n\t"
1678
1679                 : "=m"(dst[x + y * stride])
1680                 : "m"(src[0]), "m"(src[1]),
1681                   "m"(src[stride]), "m"(src[stride + 1]),
1682                   "m"(*r4), "m"(shift2)
1683             );
1684             src += stride;
1685         }
1686         src += 4 - h * stride;
1687     }
1688 }
1689 #endif /* HAVE_INLINE_ASM */
1690
1691 #include "h264_qpel.c"
1692
1693 void ff_put_h264_chroma_mc8_rnd_mmx  (uint8_t *dst, uint8_t *src,
1694                                       int stride, int h, int x, int y);
1695 void ff_avg_h264_chroma_mc8_rnd_mmxext(uint8_t *dst, uint8_t *src,
1696                                        int stride, int h, int x, int y);
1697 void ff_avg_h264_chroma_mc8_rnd_3dnow(uint8_t *dst, uint8_t *src,
1698                                       int stride, int h, int x, int y);
1699
1700 void ff_put_h264_chroma_mc4_mmx      (uint8_t *dst, uint8_t *src,
1701                                       int stride, int h, int x, int y);
1702 void ff_avg_h264_chroma_mc4_mmxext   (uint8_t *dst, uint8_t *src,
1703                                       int stride, int h, int x, int y);
1704 void ff_avg_h264_chroma_mc4_3dnow    (uint8_t *dst, uint8_t *src,
1705                                       int stride, int h, int x, int y);
1706
1707 void ff_put_h264_chroma_mc2_mmxext   (uint8_t *dst, uint8_t *src,
1708                                       int stride, int h, int x, int y);
1709 void ff_avg_h264_chroma_mc2_mmxext   (uint8_t *dst, uint8_t *src,
1710                                       int stride, int h, int x, int y);
1711
1712 void ff_put_h264_chroma_mc8_rnd_ssse3(uint8_t *dst, uint8_t *src,
1713                                       int stride, int h, int x, int y);
1714 void ff_put_h264_chroma_mc4_ssse3    (uint8_t *dst, uint8_t *src,
1715                                       int stride, int h, int x, int y);
1716
1717 void ff_avg_h264_chroma_mc8_rnd_ssse3(uint8_t *dst, uint8_t *src,
1718                                       int stride, int h, int x, int y);
1719 void ff_avg_h264_chroma_mc4_ssse3    (uint8_t *dst, uint8_t *src,
1720                                       int stride, int h, int x, int y);
1721
1722 #define CHROMA_MC(OP, NUM, DEPTH, OPT)                                  \
1723 void ff_ ## OP ## _h264_chroma_mc ## NUM ## _ ## DEPTH ## _ ## OPT      \
1724                                       (uint8_t *dst, uint8_t *src,      \
1725                                        int stride, int h, int x, int y);
1726
1727 CHROMA_MC(put, 2, 10, mmxext)
1728 CHROMA_MC(avg, 2, 10, mmxext)
1729 CHROMA_MC(put, 4, 10, mmxext)
1730 CHROMA_MC(avg, 4, 10, mmxext)
1731 CHROMA_MC(put, 8, 10, sse2)
1732 CHROMA_MC(avg, 8, 10, sse2)
1733 CHROMA_MC(put, 8, 10, avx)
1734 CHROMA_MC(avg, 8, 10, avx)
1735
1736 #if HAVE_INLINE_ASM
1737
1738 /* CAVS-specific */
1739 void ff_put_cavs_qpel8_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
1740 {
1741     put_pixels8_mmx(dst, src, stride, 8);
1742 }
1743
1744 void ff_avg_cavs_qpel8_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
1745 {
1746     avg_pixels8_mmx(dst, src, stride, 8);
1747 }
1748
1749 void ff_put_cavs_qpel16_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
1750 {
1751     put_pixels16_mmx(dst, src, stride, 16);
1752 }
1753
1754 void ff_avg_cavs_qpel16_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
1755 {
1756     avg_pixels16_mmx(dst, src, stride, 16);
1757 }
1758
1759 /* VC-1-specific */
1760 void ff_put_vc1_mspel_mc00_mmx(uint8_t *dst, const uint8_t *src,
1761                                int stride, int rnd)
1762 {
1763     put_pixels8_mmx(dst, src, stride, 8);
1764 }
1765
1766 void ff_avg_vc1_mspel_mc00_mmxext(uint8_t *dst, const uint8_t *src,
1767                                   int stride, int rnd)
1768 {
1769     avg_pixels8_mmxext(dst, src, stride, 8);
1770 }
1771
1772 static void vector_clipf_sse(float *dst, const float *src,
1773                              float min, float max, int len)
1774 {
1775     x86_reg i = (len - 16) * 4;
1776     __asm__ volatile (
1777         "movss          %3, %%xmm4      \n\t"
1778         "movss          %4, %%xmm5      \n\t"
1779         "shufps $0, %%xmm4, %%xmm4      \n\t"
1780         "shufps $0, %%xmm5, %%xmm5      \n\t"
1781         "1:                             \n\t"
1782         "movaps   (%2, %0), %%xmm0      \n\t" // 3/1 on intel
1783         "movaps 16(%2, %0), %%xmm1      \n\t"
1784         "movaps 32(%2, %0), %%xmm2      \n\t"
1785         "movaps 48(%2, %0), %%xmm3      \n\t"
1786         "maxps      %%xmm4, %%xmm0      \n\t"
1787         "maxps      %%xmm4, %%xmm1      \n\t"
1788         "maxps      %%xmm4, %%xmm2      \n\t"
1789         "maxps      %%xmm4, %%xmm3      \n\t"
1790         "minps      %%xmm5, %%xmm0      \n\t"
1791         "minps      %%xmm5, %%xmm1      \n\t"
1792         "minps      %%xmm5, %%xmm2      \n\t"
1793         "minps      %%xmm5, %%xmm3      \n\t"
1794         "movaps     %%xmm0,   (%1, %0)  \n\t"
1795         "movaps     %%xmm1, 16(%1, %0)  \n\t"
1796         "movaps     %%xmm2, 32(%1, %0)  \n\t"
1797         "movaps     %%xmm3, 48(%1, %0)  \n\t"
1798         "sub           $64, %0          \n\t"
1799         "jge            1b              \n\t"
1800         : "+&r"(i)
1801         : "r"(dst), "r"(src), "m"(min), "m"(max)
1802         : "memory"
1803     );
1804 }
1805
1806 #endif /* HAVE_INLINE_ASM */
1807
1808 int32_t ff_scalarproduct_int16_mmxext(const int16_t *v1, const int16_t *v2,
1809                                       int order);
1810 int32_t ff_scalarproduct_int16_sse2(const int16_t *v1, const int16_t *v2,
1811                                     int order);
1812 int32_t ff_scalarproduct_and_madd_int16_mmxext(int16_t *v1, const int16_t *v2,
1813                                                const int16_t *v3,
1814                                                int order, int mul);
1815 int32_t ff_scalarproduct_and_madd_int16_sse2(int16_t *v1, const int16_t *v2,
1816                                              const int16_t *v3,
1817                                              int order, int mul);
1818 int32_t ff_scalarproduct_and_madd_int16_ssse3(int16_t *v1, const int16_t *v2,
1819                                               const int16_t *v3,
1820                                               int order, int mul);
1821
1822 void ff_apply_window_int16_round_mmxext(int16_t *output, const int16_t *input,
1823                                         const int16_t *window, unsigned int len);
1824 void ff_apply_window_int16_round_sse2(int16_t *output, const int16_t *input,
1825                                       const int16_t *window, unsigned int len);
1826 void ff_apply_window_int16_mmxext(int16_t *output, const int16_t *input,
1827                                   const int16_t *window, unsigned int len);
1828 void ff_apply_window_int16_sse2(int16_t *output, const int16_t *input,
1829                                 const int16_t *window, unsigned int len);
1830 void ff_apply_window_int16_ssse3(int16_t *output, const int16_t *input,
1831                                  const int16_t *window, unsigned int len);
1832 void ff_apply_window_int16_ssse3_atom(int16_t *output, const int16_t *input,
1833                                       const int16_t *window, unsigned int len);
1834
1835 void ff_bswap32_buf_ssse3(uint32_t *dst, const uint32_t *src, int w);
1836 void ff_bswap32_buf_sse2(uint32_t *dst, const uint32_t *src, int w);
1837
1838 void ff_add_hfyu_median_prediction_mmxext(uint8_t *dst, const uint8_t *top,
1839                                           const uint8_t *diff, int w,
1840                                           int *left, int *left_top);
1841 int  ff_add_hfyu_left_prediction_ssse3(uint8_t *dst, const uint8_t *src,
1842                                        int w, int left);
1843 int  ff_add_hfyu_left_prediction_sse4(uint8_t *dst, const uint8_t *src,
1844                                       int w, int left);
1845
1846 void ff_vector_clip_int32_mmx     (int32_t *dst, const int32_t *src,
1847                                    int32_t min, int32_t max, unsigned int len);
1848 void ff_vector_clip_int32_sse2    (int32_t *dst, const int32_t *src,
1849                                    int32_t min, int32_t max, unsigned int len);
1850 void ff_vector_clip_int32_int_sse2(int32_t *dst, const int32_t *src,
1851                                    int32_t min, int32_t max, unsigned int len);
1852 void ff_vector_clip_int32_sse4    (int32_t *dst, const int32_t *src,
1853                                    int32_t min, int32_t max, unsigned int len);
1854
1855 #define SET_QPEL_FUNCS(PFX, IDX, SIZE, CPU, PREFIX)                          \
1856     do {                                                                     \
1857     c->PFX ## _pixels_tab[IDX][ 0] = PREFIX ## PFX ## SIZE ## _mc00_ ## CPU; \
1858     c->PFX ## _pixels_tab[IDX][ 1] = PREFIX ## PFX ## SIZE ## _mc10_ ## CPU; \
1859     c->PFX ## _pixels_tab[IDX][ 2] = PREFIX ## PFX ## SIZE ## _mc20_ ## CPU; \
1860     c->PFX ## _pixels_tab[IDX][ 3] = PREFIX ## PFX ## SIZE ## _mc30_ ## CPU; \
1861     c->PFX ## _pixels_tab[IDX][ 4] = PREFIX ## PFX ## SIZE ## _mc01_ ## CPU; \
1862     c->PFX ## _pixels_tab[IDX][ 5] = PREFIX ## PFX ## SIZE ## _mc11_ ## CPU; \
1863     c->PFX ## _pixels_tab[IDX][ 6] = PREFIX ## PFX ## SIZE ## _mc21_ ## CPU; \
1864     c->PFX ## _pixels_tab[IDX][ 7] = PREFIX ## PFX ## SIZE ## _mc31_ ## CPU; \
1865     c->PFX ## _pixels_tab[IDX][ 8] = PREFIX ## PFX ## SIZE ## _mc02_ ## CPU; \
1866     c->PFX ## _pixels_tab[IDX][ 9] = PREFIX ## PFX ## SIZE ## _mc12_ ## CPU; \
1867     c->PFX ## _pixels_tab[IDX][10] = PREFIX ## PFX ## SIZE ## _mc22_ ## CPU; \
1868     c->PFX ## _pixels_tab[IDX][11] = PREFIX ## PFX ## SIZE ## _mc32_ ## CPU; \
1869     c->PFX ## _pixels_tab[IDX][12] = PREFIX ## PFX ## SIZE ## _mc03_ ## CPU; \
1870     c->PFX ## _pixels_tab[IDX][13] = PREFIX ## PFX ## SIZE ## _mc13_ ## CPU; \
1871     c->PFX ## _pixels_tab[IDX][14] = PREFIX ## PFX ## SIZE ## _mc23_ ## CPU; \
1872     c->PFX ## _pixels_tab[IDX][15] = PREFIX ## PFX ## SIZE ## _mc33_ ## CPU; \
1873     } while (0)
1874
1875 #define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU)                                     \
1876     do {                                                                        \
1877         c->PFX ## _pixels_tab[IDX][0] = PFX ## _pixels ## SIZE ## _     ## CPU; \
1878         c->PFX ## _pixels_tab[IDX][1] = PFX ## _pixels ## SIZE ## _x2_  ## CPU; \
1879         c->PFX ## _pixels_tab[IDX][2] = PFX ## _pixels ## SIZE ## _y2_  ## CPU; \
1880         c->PFX ## _pixels_tab[IDX][3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU; \
1881     } while (0)
1882
1883 #define H264_QPEL_FUNCS(x, y, CPU)                                                            \
1884     do {                                                                                      \
1885         c->put_h264_qpel_pixels_tab[0][x + y * 4] = put_h264_qpel16_mc ## x ## y ## _ ## CPU; \
1886         c->put_h264_qpel_pixels_tab[1][x + y * 4] = put_h264_qpel8_mc  ## x ## y ## _ ## CPU; \
1887         c->avg_h264_qpel_pixels_tab[0][x + y * 4] = avg_h264_qpel16_mc ## x ## y ## _ ## CPU; \
1888         c->avg_h264_qpel_pixels_tab[1][x + y * 4] = avg_h264_qpel8_mc  ## x ## y ## _ ## CPU; \
1889     } while (0)
1890
1891 #define H264_QPEL_FUNCS_10(x, y, CPU)                                                               \
1892     do {                                                                                            \
1893         c->put_h264_qpel_pixels_tab[0][x + y * 4] = ff_put_h264_qpel16_mc ## x ## y ## _10_ ## CPU; \
1894         c->put_h264_qpel_pixels_tab[1][x + y * 4] = ff_put_h264_qpel8_mc  ## x ## y ## _10_ ## CPU; \
1895         c->avg_h264_qpel_pixels_tab[0][x + y * 4] = ff_avg_h264_qpel16_mc ## x ## y ## _10_ ## CPU; \
1896         c->avg_h264_qpel_pixels_tab[1][x + y * 4] = ff_avg_h264_qpel8_mc  ## x ## y ## _10_ ## CPU; \
1897     } while (0)
1898
1899 static void dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx, int mm_flags)
1900 {
1901     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
1902
1903 #if HAVE_INLINE_ASM
1904     c->put_pixels_clamped        = ff_put_pixels_clamped_mmx;
1905     c->put_signed_pixels_clamped = ff_put_signed_pixels_clamped_mmx;
1906     c->add_pixels_clamped        = ff_add_pixels_clamped_mmx;
1907
1908     if (!high_bit_depth) {
1909         c->clear_block  = clear_block_mmx;
1910         c->clear_blocks = clear_blocks_mmx;
1911         c->draw_edges   = draw_edges_mmx;
1912
1913         SET_HPEL_FUNCS(put,        0, 16, mmx);
1914         SET_HPEL_FUNCS(put_no_rnd, 0, 16, mmx);
1915         SET_HPEL_FUNCS(avg,        0, 16, mmx);
1916         SET_HPEL_FUNCS(avg_no_rnd, 0, 16, mmx);
1917         SET_HPEL_FUNCS(put,        1,  8, mmx);
1918         SET_HPEL_FUNCS(put_no_rnd, 1,  8, mmx);
1919         SET_HPEL_FUNCS(avg,        1,  8, mmx);
1920         SET_HPEL_FUNCS(avg_no_rnd, 1,  8, mmx);
1921
1922         switch (avctx->idct_algo) {
1923         case FF_IDCT_AUTO:
1924         case FF_IDCT_SIMPLEMMX:
1925             c->idct_put              = ff_simple_idct_put_mmx;
1926             c->idct_add              = ff_simple_idct_add_mmx;
1927             c->idct                  = ff_simple_idct_mmx;
1928             c->idct_permutation_type = FF_SIMPLE_IDCT_PERM;
1929             break;
1930         case FF_IDCT_XVIDMMX:
1931             c->idct_put              = ff_idct_xvid_mmx_put;
1932             c->idct_add              = ff_idct_xvid_mmx_add;
1933             c->idct                  = ff_idct_xvid_mmx;
1934             break;
1935         }
1936     }
1937
1938     c->gmc = gmc_mmx;
1939
1940     c->add_bytes = add_bytes_mmx;
1941
1942     if (CONFIG_H263_DECODER || CONFIG_H263_ENCODER) {
1943         c->h263_v_loop_filter = h263_v_loop_filter_mmx;
1944         c->h263_h_loop_filter = h263_h_loop_filter_mmx;
1945     }
1946 #endif /* HAVE_INLINE_ASM */
1947
1948 #if HAVE_YASM
1949     if (!high_bit_depth && CONFIG_H264CHROMA) {
1950         c->put_h264_chroma_pixels_tab[0] = ff_put_h264_chroma_mc8_rnd_mmx;
1951         c->put_h264_chroma_pixels_tab[1] = ff_put_h264_chroma_mc4_mmx;
1952     }
1953
1954     c->vector_clip_int32 = ff_vector_clip_int32_mmx;
1955 #endif
1956
1957 }
1958
1959 static void dsputil_init_mmxext(DSPContext *c, AVCodecContext *avctx,
1960                                 int mm_flags)
1961 {
1962     const int bit_depth      = avctx->bits_per_raw_sample;
1963     const int high_bit_depth = bit_depth > 8;
1964
1965 #if HAVE_INLINE_ASM
1966     SET_QPEL_FUNCS(avg_qpel,        0, 16, mmxext, );
1967     SET_QPEL_FUNCS(avg_qpel,        1,  8, mmxext, );
1968
1969     SET_QPEL_FUNCS(put_qpel,        0, 16, mmxext, );
1970     SET_QPEL_FUNCS(put_qpel,        1,  8, mmxext, );
1971     SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, mmxext, );
1972     SET_QPEL_FUNCS(put_no_rnd_qpel, 1,  8, mmxext, );
1973
1974     if (!high_bit_depth) {
1975         c->put_pixels_tab[0][1] = put_pixels16_x2_mmxext;
1976         c->put_pixels_tab[0][2] = put_pixels16_y2_mmxext;
1977
1978         c->avg_pixels_tab[0][0] = avg_pixels16_mmxext;
1979         c->avg_pixels_tab[0][1] = avg_pixels16_x2_mmxext;
1980         c->avg_pixels_tab[0][2] = avg_pixels16_y2_mmxext;
1981
1982         c->put_pixels_tab[1][1] = put_pixels8_x2_mmxext;
1983         c->put_pixels_tab[1][2] = put_pixels8_y2_mmxext;
1984
1985         c->avg_pixels_tab[1][0] = avg_pixels8_mmxext;
1986         c->avg_pixels_tab[1][1] = avg_pixels8_x2_mmxext;
1987         c->avg_pixels_tab[1][2] = avg_pixels8_y2_mmxext;
1988     }
1989
1990     if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
1991         if (!high_bit_depth) {
1992             c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_mmxext;
1993             c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_mmxext;
1994             c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_mmxext;
1995             c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_mmxext;
1996
1997             c->avg_pixels_tab[0][3] = avg_pixels16_xy2_mmxext;
1998             c->avg_pixels_tab[1][3] = avg_pixels8_xy2_mmxext;
1999         }
2000     }
2001
2002     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
2003         c->idct_put = ff_idct_xvid_mmxext_put;
2004         c->idct_add = ff_idct_xvid_mmxext_add;
2005         c->idct     = ff_idct_xvid_mmxext;
2006     }
2007
2008     if (CONFIG_VP3_DECODER && (avctx->codec_id == AV_CODEC_ID_VP3 ||
2009                                avctx->codec_id == AV_CODEC_ID_THEORA)) {
2010         c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_exact_mmxext;
2011         c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_exact_mmxext;
2012     }
2013 #endif /* HAVE_INLINE_ASM */
2014
2015 #if HAVE_MMXEXT_EXTERNAL
2016     if (CONFIG_H264QPEL) {
2017         if (!high_bit_depth) {
2018             SET_QPEL_FUNCS(put_h264_qpel, 0, 16, mmxext, );
2019             SET_QPEL_FUNCS(put_h264_qpel, 1,  8, mmxext, );
2020             SET_QPEL_FUNCS(put_h264_qpel, 2,  4, mmxext, );
2021             SET_QPEL_FUNCS(avg_h264_qpel, 0, 16, mmxext, );
2022             SET_QPEL_FUNCS(avg_h264_qpel, 1,  8, mmxext, );
2023             SET_QPEL_FUNCS(avg_h264_qpel, 2,  4, mmxext, );
2024         } else if (bit_depth == 10) {
2025 #if !ARCH_X86_64
2026             SET_QPEL_FUNCS(avg_h264_qpel, 0, 16, 10_mmxext, ff_);
2027             SET_QPEL_FUNCS(put_h264_qpel, 0, 16, 10_mmxext, ff_);
2028             SET_QPEL_FUNCS(put_h264_qpel, 1,  8, 10_mmxext, ff_);
2029             SET_QPEL_FUNCS(avg_h264_qpel, 1,  8, 10_mmxext, ff_);
2030 #endif
2031             SET_QPEL_FUNCS(put_h264_qpel, 2, 4,  10_mmxext, ff_);
2032             SET_QPEL_FUNCS(avg_h264_qpel, 2, 4,  10_mmxext, ff_);
2033         }
2034     }
2035
2036     if (!high_bit_depth && CONFIG_H264CHROMA) {
2037         c->avg_h264_chroma_pixels_tab[0] = ff_avg_h264_chroma_mc8_rnd_mmxext;
2038         c->avg_h264_chroma_pixels_tab[1] = ff_avg_h264_chroma_mc4_mmxext;
2039         c->avg_h264_chroma_pixels_tab[2] = ff_avg_h264_chroma_mc2_mmxext;
2040         c->put_h264_chroma_pixels_tab[2] = ff_put_h264_chroma_mc2_mmxext;
2041     }
2042     if (bit_depth == 10 && CONFIG_H264CHROMA) {
2043         c->put_h264_chroma_pixels_tab[2] = ff_put_h264_chroma_mc2_10_mmxext;
2044         c->avg_h264_chroma_pixels_tab[2] = ff_avg_h264_chroma_mc2_10_mmxext;
2045         c->put_h264_chroma_pixels_tab[1] = ff_put_h264_chroma_mc4_10_mmxext;
2046         c->avg_h264_chroma_pixels_tab[1] = ff_avg_h264_chroma_mc4_10_mmxext;
2047     }
2048
2049     /* slower than cmov version on AMD */
2050     if (!(mm_flags & AV_CPU_FLAG_3DNOW))
2051         c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmxext;
2052
2053     c->scalarproduct_int16          = ff_scalarproduct_int16_mmxext;
2054     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_mmxext;
2055
2056     if (avctx->flags & CODEC_FLAG_BITEXACT) {
2057         c->apply_window_int16 = ff_apply_window_int16_mmxext;
2058     } else {
2059         c->apply_window_int16 = ff_apply_window_int16_round_mmxext;
2060     }
2061 #endif /* HAVE_MMXEXT_EXTERNAL */
2062 }
2063
2064 static void dsputil_init_3dnow(DSPContext *c, AVCodecContext *avctx,
2065                                int mm_flags)
2066 {
2067     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
2068
2069 #if HAVE_INLINE_ASM
2070     if (!high_bit_depth) {
2071         c->put_pixels_tab[0][1] = put_pixels16_x2_3dnow;
2072         c->put_pixels_tab[0][2] = put_pixels16_y2_3dnow;
2073
2074         c->avg_pixels_tab[0][0] = avg_pixels16_3dnow;
2075         c->avg_pixels_tab[0][1] = avg_pixels16_x2_3dnow;
2076         c->avg_pixels_tab[0][2] = avg_pixels16_y2_3dnow;
2077
2078         c->put_pixels_tab[1][1] = put_pixels8_x2_3dnow;
2079         c->put_pixels_tab[1][2] = put_pixels8_y2_3dnow;
2080
2081         c->avg_pixels_tab[1][0] = avg_pixels8_3dnow;
2082         c->avg_pixels_tab[1][1] = avg_pixels8_x2_3dnow;
2083         c->avg_pixels_tab[1][2] = avg_pixels8_y2_3dnow;
2084
2085         if (!(avctx->flags & CODEC_FLAG_BITEXACT)){
2086             c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_3dnow;
2087             c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_3dnow;
2088             c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_3dnow;
2089             c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_3dnow;
2090
2091             c->avg_pixels_tab[0][3] = avg_pixels16_xy2_3dnow;
2092             c->avg_pixels_tab[1][3] = avg_pixels8_xy2_3dnow;
2093         }
2094     }
2095
2096     if (CONFIG_VP3_DECODER && (avctx->codec_id == AV_CODEC_ID_VP3 ||
2097                                avctx->codec_id == AV_CODEC_ID_THEORA)) {
2098         c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_exact_3dnow;
2099         c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_exact_3dnow;
2100     }
2101 #endif /* HAVE_INLINE_ASM */
2102
2103 #if HAVE_YASM
2104     if (!high_bit_depth && CONFIG_H264CHROMA) {
2105         c->avg_h264_chroma_pixels_tab[0] = ff_avg_h264_chroma_mc8_rnd_3dnow;
2106         c->avg_h264_chroma_pixels_tab[1] = ff_avg_h264_chroma_mc4_3dnow;
2107     }
2108 #endif /* HAVE_YASM */
2109 }
2110
2111 static void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx, int mm_flags)
2112 {
2113     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
2114
2115 #if HAVE_INLINE_ASM
2116     if (!high_bit_depth) {
2117         if (!(CONFIG_MPEG_XVMC_DECODER && avctx->xvmc_acceleration > 1)) {
2118             /* XvMCCreateBlocks() may not allocate 16-byte aligned blocks */
2119             c->clear_block  = clear_block_sse;
2120             c->clear_blocks = clear_blocks_sse;
2121         }
2122     }
2123
2124     c->vector_clipf = vector_clipf_sse;
2125 #endif /* HAVE_INLINE_ASM */
2126 }
2127
2128 static void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx,
2129                               int mm_flags)
2130 {
2131     const int bit_depth      = avctx->bits_per_raw_sample;
2132     const int high_bit_depth = bit_depth > 8;
2133
2134 #if HAVE_SSE2_INLINE
2135     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
2136         c->idct_put              = ff_idct_xvid_sse2_put;
2137         c->idct_add              = ff_idct_xvid_sse2_add;
2138         c->idct                  = ff_idct_xvid_sse2;
2139         c->idct_permutation_type = FF_SSE2_IDCT_PERM;
2140     }
2141 #endif /* HAVE_SSE2_INLINE */
2142
2143 #if HAVE_SSE2_EXTERNAL
2144     if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
2145         // these functions are slower than mmx on AMD, but faster on Intel
2146         if (!high_bit_depth) {
2147             c->put_pixels_tab[0][0]        = ff_put_pixels16_sse2;
2148             c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_sse2;
2149             c->avg_pixels_tab[0][0]        = ff_avg_pixels16_sse2;
2150             if (CONFIG_H264QPEL)
2151                 H264_QPEL_FUNCS(0, 0, sse2);
2152         }
2153     }
2154
2155     if (!high_bit_depth && CONFIG_H264QPEL) {
2156         H264_QPEL_FUNCS(0, 1, sse2);
2157         H264_QPEL_FUNCS(0, 2, sse2);
2158         H264_QPEL_FUNCS(0, 3, sse2);
2159         H264_QPEL_FUNCS(1, 1, sse2);
2160         H264_QPEL_FUNCS(1, 2, sse2);
2161         H264_QPEL_FUNCS(1, 3, sse2);
2162         H264_QPEL_FUNCS(2, 1, sse2);
2163         H264_QPEL_FUNCS(2, 2, sse2);
2164         H264_QPEL_FUNCS(2, 3, sse2);
2165         H264_QPEL_FUNCS(3, 1, sse2);
2166         H264_QPEL_FUNCS(3, 2, sse2);
2167         H264_QPEL_FUNCS(3, 3, sse2);
2168     }
2169
2170     if (bit_depth == 10) {
2171         if (CONFIG_H264QPEL) {
2172             SET_QPEL_FUNCS(put_h264_qpel, 0, 16, 10_sse2, ff_);
2173             SET_QPEL_FUNCS(put_h264_qpel, 1,  8, 10_sse2, ff_);
2174             SET_QPEL_FUNCS(avg_h264_qpel, 0, 16, 10_sse2, ff_);
2175             SET_QPEL_FUNCS(avg_h264_qpel, 1,  8, 10_sse2, ff_);
2176             H264_QPEL_FUNCS_10(1, 0, sse2_cache64);
2177             H264_QPEL_FUNCS_10(2, 0, sse2_cache64);
2178             H264_QPEL_FUNCS_10(3, 0, sse2_cache64);
2179         }
2180         if (CONFIG_H264CHROMA) {
2181             c->put_h264_chroma_pixels_tab[0] = ff_put_h264_chroma_mc8_10_sse2;
2182             c->avg_h264_chroma_pixels_tab[0] = ff_avg_h264_chroma_mc8_10_sse2;
2183         }
2184     }
2185
2186     c->scalarproduct_int16          = ff_scalarproduct_int16_sse2;
2187     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2;
2188     if (mm_flags & AV_CPU_FLAG_ATOM) {
2189         c->vector_clip_int32 = ff_vector_clip_int32_int_sse2;
2190     } else {
2191         c->vector_clip_int32 = ff_vector_clip_int32_sse2;
2192     }
2193     if (avctx->flags & CODEC_FLAG_BITEXACT) {
2194         c->apply_window_int16 = ff_apply_window_int16_sse2;
2195     } else if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
2196         c->apply_window_int16 = ff_apply_window_int16_round_sse2;
2197     }
2198     c->bswap_buf = ff_bswap32_buf_sse2;
2199 #endif /* HAVE_SSE2_EXTERNAL */
2200 }
2201
2202 static void dsputil_init_ssse3(DSPContext *c, AVCodecContext *avctx,
2203                                int mm_flags)
2204 {
2205 #if HAVE_SSSE3_EXTERNAL
2206     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
2207     const int bit_depth      = avctx->bits_per_raw_sample;
2208
2209     if (!high_bit_depth && CONFIG_H264QPEL) {
2210         H264_QPEL_FUNCS(1, 0, ssse3);
2211         H264_QPEL_FUNCS(1, 1, ssse3);
2212         H264_QPEL_FUNCS(1, 2, ssse3);
2213         H264_QPEL_FUNCS(1, 3, ssse3);
2214         H264_QPEL_FUNCS(2, 0, ssse3);
2215         H264_QPEL_FUNCS(2, 1, ssse3);
2216         H264_QPEL_FUNCS(2, 2, ssse3);
2217         H264_QPEL_FUNCS(2, 3, ssse3);
2218         H264_QPEL_FUNCS(3, 0, ssse3);
2219         H264_QPEL_FUNCS(3, 1, ssse3);
2220         H264_QPEL_FUNCS(3, 2, ssse3);
2221         H264_QPEL_FUNCS(3, 3, ssse3);
2222     }
2223     if (bit_depth == 10 && CONFIG_H264QPEL) {
2224         H264_QPEL_FUNCS_10(1, 0, ssse3_cache64);
2225         H264_QPEL_FUNCS_10(2, 0, ssse3_cache64);
2226         H264_QPEL_FUNCS_10(3, 0, ssse3_cache64);
2227     }
2228     if (!high_bit_depth && CONFIG_H264CHROMA) {
2229         c->put_h264_chroma_pixels_tab[0] = ff_put_h264_chroma_mc8_rnd_ssse3;
2230         c->avg_h264_chroma_pixels_tab[0] = ff_avg_h264_chroma_mc8_rnd_ssse3;
2231         c->put_h264_chroma_pixels_tab[1] = ff_put_h264_chroma_mc4_ssse3;
2232         c->avg_h264_chroma_pixels_tab[1] = ff_avg_h264_chroma_mc4_ssse3;
2233     }
2234     c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_ssse3;
2235     if (mm_flags & AV_CPU_FLAG_SSE4) // not really sse4, just slow on Conroe
2236         c->add_hfyu_left_prediction = ff_add_hfyu_left_prediction_sse4;
2237
2238     if (mm_flags & AV_CPU_FLAG_ATOM)
2239         c->apply_window_int16 = ff_apply_window_int16_ssse3_atom;
2240     else
2241         c->apply_window_int16 = ff_apply_window_int16_ssse3;
2242     if (!(mm_flags & (AV_CPU_FLAG_SSE42|AV_CPU_FLAG_3DNOW))) // cachesplit
2243         c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_ssse3;
2244     c->bswap_buf = ff_bswap32_buf_ssse3;
2245 #endif /* HAVE_SSSE3_EXTERNAL */
2246 }
2247
2248 static void dsputil_init_sse4(DSPContext *c, AVCodecContext *avctx,
2249                               int mm_flags)
2250 {
2251 #if HAVE_SSE4_EXTERNAL
2252     c->vector_clip_int32 = ff_vector_clip_int32_sse4;
2253 #endif /* HAVE_SSE4_EXTERNAL */
2254 }
2255
2256 static void dsputil_init_avx(DSPContext *c, AVCodecContext *avctx, int mm_flags)
2257 {
2258 #if HAVE_AVX_EXTERNAL
2259     const int bit_depth = avctx->bits_per_raw_sample;
2260
2261     if (bit_depth == 10) {
2262         // AVX implies !cache64.
2263         // TODO: Port cache(32|64) detection from x264.
2264         if (CONFIG_H264QPEL) {
2265             H264_QPEL_FUNCS_10(1, 0, sse2);
2266             H264_QPEL_FUNCS_10(2, 0, sse2);
2267             H264_QPEL_FUNCS_10(3, 0, sse2);
2268         }
2269
2270         if (CONFIG_H264CHROMA) {
2271             c->put_h264_chroma_pixels_tab[0] = ff_put_h264_chroma_mc8_10_avx;
2272             c->avg_h264_chroma_pixels_tab[0] = ff_avg_h264_chroma_mc8_10_avx;
2273         }
2274     }
2275 #endif /* HAVE_AVX_EXTERNAL */
2276 }
2277
2278 void ff_dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx)
2279 {
2280     int mm_flags = av_get_cpu_flags();
2281
2282 #if HAVE_7REGS && HAVE_INLINE_ASM
2283     if (mm_flags & AV_CPU_FLAG_CMOV)
2284         c->add_hfyu_median_prediction = add_hfyu_median_prediction_cmov;
2285 #endif
2286
2287     if (mm_flags & AV_CPU_FLAG_MMX)
2288         dsputil_init_mmx(c, avctx, mm_flags);
2289
2290     if (mm_flags & AV_CPU_FLAG_MMXEXT)
2291         dsputil_init_mmxext(c, avctx, mm_flags);
2292
2293     if (mm_flags & AV_CPU_FLAG_3DNOW)
2294         dsputil_init_3dnow(c, avctx, mm_flags);
2295
2296     if (mm_flags & AV_CPU_FLAG_SSE)
2297         dsputil_init_sse(c, avctx, mm_flags);
2298
2299     if (mm_flags & AV_CPU_FLAG_SSE2)
2300         dsputil_init_sse2(c, avctx, mm_flags);
2301
2302     if (mm_flags & AV_CPU_FLAG_SSSE3)
2303         dsputil_init_ssse3(c, avctx, mm_flags);
2304
2305     if (mm_flags & AV_CPU_FLAG_SSE4)
2306         dsputil_init_sse4(c, avctx, mm_flags);
2307
2308     if (mm_flags & AV_CPU_FLAG_AVX)
2309         dsputil_init_avx(c, avctx, mm_flags);
2310
2311     if (CONFIG_ENCODERS)
2312         ff_dsputilenc_init_mmx(c, avctx);
2313 }