Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / dsputil_mmx.c @ 2a2311be

History | View | Annotate | Download (132 KB)

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
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 *
20
 * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
21
 */
22

    
23
#include "../dsputil.h"
24
#include "../simple_idct.h"
25
#include "../mpegvideo.h"
26
#include "x86_cpu.h"
27
#include "mmx.h"
28

    
29
//#undef NDEBUG
30
//#include <assert.h>
31

    
32
extern void ff_idct_xvid_mmx(short *block);
33
extern void ff_idct_xvid_mmx2(short *block);
34

    
35
int mm_flags; /* multimedia extension flags */
36

    
37
/* pixel operations */
38
static const uint64_t mm_bone attribute_used __attribute__ ((aligned(8))) = 0x0101010101010101ULL;
39
static const uint64_t mm_wone attribute_used __attribute__ ((aligned(8))) = 0x0001000100010001ULL;
40
static const uint64_t mm_wtwo attribute_used __attribute__ ((aligned(8))) = 0x0002000200020002ULL;
41

    
42
static const uint64_t ff_pdw_80000000[2] attribute_used __attribute__ ((aligned(16))) =
43
{0x8000000080000000ULL, 0x8000000080000000ULL};
44

    
45
static const uint64_t ff_pw_20 attribute_used __attribute__ ((aligned(8))) = 0x0014001400140014ULL;
46
static const uint64_t ff_pw_3  attribute_used __attribute__ ((aligned(8))) = 0x0003000300030003ULL;
47
static const uint64_t ff_pw_4  attribute_used __attribute__ ((aligned(8))) = 0x0004000400040004ULL;
48
static const uint64_t ff_pw_5  attribute_used __attribute__ ((aligned(8))) = 0x0005000500050005ULL;
49
static const uint64_t ff_pw_8  attribute_used __attribute__ ((aligned(8))) = 0x0008000800080008ULL;
50
static const uint64_t ff_pw_16 attribute_used __attribute__ ((aligned(8))) = 0x0010001000100010ULL;
51
static const uint64_t ff_pw_32 attribute_used __attribute__ ((aligned(8))) = 0x0020002000200020ULL;
52
static const uint64_t ff_pw_64 attribute_used __attribute__ ((aligned(8))) = 0x0040004000400040ULL;
53
static const uint64_t ff_pw_15 attribute_used __attribute__ ((aligned(8))) = 0x000F000F000F000FULL;
54

    
55
static const uint64_t ff_pb_1  attribute_used __attribute__ ((aligned(8))) = 0x0101010101010101ULL;
56
static const uint64_t ff_pb_3  attribute_used __attribute__ ((aligned(8))) = 0x0303030303030303ULL;
57
static const uint64_t ff_pb_7  attribute_used __attribute__ ((aligned(8))) = 0x0707070707070707ULL;
58
static const uint64_t ff_pb_3F attribute_used __attribute__ ((aligned(8))) = 0x3F3F3F3F3F3F3F3FULL;
59
static const uint64_t ff_pb_FC attribute_used __attribute__ ((aligned(8))) = 0xFCFCFCFCFCFCFCFCULL;
60

    
61
#define JUMPALIGN() __asm __volatile (ASMALIGN(3)::)
62
#define MOVQ_ZERO(regd)  __asm __volatile ("pxor %%" #regd ", %%" #regd ::)
63

    
64
#define MOVQ_WONE(regd) \
65
    __asm __volatile ( \
66
    "pcmpeqd %%" #regd ", %%" #regd " \n\t" \
67
    "psrlw $15, %%" #regd ::)
68

    
69
#define MOVQ_BFE(regd) \
70
    __asm __volatile ( \
71
    "pcmpeqd %%" #regd ", %%" #regd " \n\t"\
72
    "paddb %%" #regd ", %%" #regd " \n\t" ::)
73

    
74
#ifndef PIC
75
#define MOVQ_BONE(regd)  __asm __volatile ("movq %0, %%" #regd " \n\t" ::"m"(mm_bone))
76
#define MOVQ_WTWO(regd)  __asm __volatile ("movq %0, %%" #regd " \n\t" ::"m"(mm_wtwo))
77
#else
78
// for shared library it's better to use this way for accessing constants
79
// pcmpeqd -> -1
80
#define MOVQ_BONE(regd) \
81
    __asm __volatile ( \
82
    "pcmpeqd %%" #regd ", %%" #regd " \n\t" \
83
    "psrlw $15, %%" #regd " \n\t" \
84
    "packuswb %%" #regd ", %%" #regd " \n\t" ::)
85

    
86
#define MOVQ_WTWO(regd) \
87
    __asm __volatile ( \
88
    "pcmpeqd %%" #regd ", %%" #regd " \n\t" \
89
    "psrlw $15, %%" #regd " \n\t" \
90
    "psllw $1, %%" #regd " \n\t"::)
91

    
92
#endif
93

    
94
// using regr as temporary and for the output result
95
// first argument is unmodifed and second is trashed
96
// regfe is supposed to contain 0xfefefefefefefefe
97
#define PAVGB_MMX_NO_RND(rega, regb, regr, regfe) \
98
    "movq " #rega ", " #regr "  \n\t"\
99
    "pand " #regb ", " #regr "  \n\t"\
100
    "pxor " #rega ", " #regb "  \n\t"\
101
    "pand " #regfe "," #regb "  \n\t"\
102
    "psrlq $1, " #regb "        \n\t"\
103
    "paddb " #regb ", " #regr " \n\t"
104

    
105
#define PAVGB_MMX(rega, regb, regr, regfe) \
106
    "movq " #rega ", " #regr "  \n\t"\
107
    "por  " #regb ", " #regr "  \n\t"\
108
    "pxor " #rega ", " #regb "  \n\t"\
109
    "pand " #regfe "," #regb "  \n\t"\
110
    "psrlq $1, " #regb "        \n\t"\
111
    "psubb " #regb ", " #regr " \n\t"
112

    
113
// mm6 is supposed to contain 0xfefefefefefefefe
114
#define PAVGBP_MMX_NO_RND(rega, regb, regr,  regc, regd, regp) \
115
    "movq " #rega ", " #regr "  \n\t"\
116
    "movq " #regc ", " #regp "  \n\t"\
117
    "pand " #regb ", " #regr "  \n\t"\
118
    "pand " #regd ", " #regp "  \n\t"\
119
    "pxor " #rega ", " #regb "  \n\t"\
120
    "pxor " #regc ", " #regd "  \n\t"\
121
    "pand %%mm6, " #regb "      \n\t"\
122
    "pand %%mm6, " #regd "      \n\t"\
123
    "psrlq $1, " #regb "        \n\t"\
124
    "psrlq $1, " #regd "        \n\t"\
125
    "paddb " #regb ", " #regr " \n\t"\
126
    "paddb " #regd ", " #regp " \n\t"
127

    
128
#define PAVGBP_MMX(rega, regb, regr, regc, regd, regp) \
129
    "movq " #rega ", " #regr "  \n\t"\
130
    "movq " #regc ", " #regp "  \n\t"\
131
    "por  " #regb ", " #regr "  \n\t"\
132
    "por  " #regd ", " #regp "  \n\t"\
133
    "pxor " #rega ", " #regb "  \n\t"\
134
    "pxor " #regc ", " #regd "  \n\t"\
135
    "pand %%mm6, " #regb "      \n\t"\
136
    "pand %%mm6, " #regd "      \n\t"\
137
    "psrlq $1, " #regd "        \n\t"\
138
    "psrlq $1, " #regb "        \n\t"\
139
    "psubb " #regb ", " #regr " \n\t"\
140
    "psubb " #regd ", " #regp " \n\t"
141

    
142
/***********************************/
143
/* MMX no rounding */
144
#define DEF(x, y) x ## _no_rnd_ ## y ##_mmx
145
#define SET_RND  MOVQ_WONE
146
#define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
147
#define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
148

    
149
#include "dsputil_mmx_rnd.h"
150

    
151
#undef DEF
152
#undef SET_RND
153
#undef PAVGBP
154
#undef PAVGB
155
/***********************************/
156
/* MMX rounding */
157

    
158
#define DEF(x, y) x ## _ ## y ##_mmx
159
#define SET_RND  MOVQ_WTWO
160
#define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
161
#define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
162

    
163
#include "dsputil_mmx_rnd.h"
164

    
165
#undef DEF
166
#undef SET_RND
167
#undef PAVGBP
168
#undef PAVGB
169

    
170
/***********************************/
171
/* 3Dnow specific */
172

    
173
#define DEF(x) x ## _3dnow
174
/* for Athlons PAVGUSB is prefered */
175
#define PAVGB "pavgusb"
176

    
177
#include "dsputil_mmx_avg.h"
178

    
179
#undef DEF
180
#undef PAVGB
181

    
182
/***********************************/
183
/* MMX2 specific */
184

    
185
#define DEF(x) x ## _mmx2
186

    
187
/* Introduced only in MMX2 set */
188
#define PAVGB "pavgb"
189

    
190
#include "dsputil_mmx_avg.h"
191

    
192
#undef DEF
193
#undef PAVGB
194

    
195
#define SBUTTERFLY(a,b,t,n)\
196
    "movq " #a ", " #t "              \n\t" /* abcd */\
197
    "punpckl" #n " " #b ", " #a "     \n\t" /* aebf */\
198
    "punpckh" #n " " #b ", " #t "     \n\t" /* cgdh */\
199

    
200
/***********************************/
201
/* standard MMX */
202

    
203
#ifdef CONFIG_ENCODERS
204
static void get_pixels_mmx(DCTELEM *block, const uint8_t *pixels, int line_size)
205
{
206
    asm volatile(
207
        "mov $-128, %%"REG_a"           \n\t"
208
        "pxor %%mm7, %%mm7              \n\t"
209
        ASMALIGN(4)
210
        "1:                             \n\t"
211
        "movq (%0), %%mm0               \n\t"
212
        "movq (%0, %2), %%mm2           \n\t"
213
        "movq %%mm0, %%mm1              \n\t"
214
        "movq %%mm2, %%mm3              \n\t"
215
        "punpcklbw %%mm7, %%mm0         \n\t"
216
        "punpckhbw %%mm7, %%mm1         \n\t"
217
        "punpcklbw %%mm7, %%mm2         \n\t"
218
        "punpckhbw %%mm7, %%mm3         \n\t"
219
        "movq %%mm0, (%1, %%"REG_a")    \n\t"
220
        "movq %%mm1, 8(%1, %%"REG_a")   \n\t"
221
        "movq %%mm2, 16(%1, %%"REG_a")  \n\t"
222
        "movq %%mm3, 24(%1, %%"REG_a")  \n\t"
223
        "add %3, %0                     \n\t"
224
        "add $32, %%"REG_a"             \n\t"
225
        "js 1b                          \n\t"
226
        : "+r" (pixels)
227
        : "r" (block+64), "r" ((long)line_size), "r" ((long)line_size*2)
228
        : "%"REG_a
229
    );
230
}
231

    
232
static inline void diff_pixels_mmx(DCTELEM *block, const uint8_t *s1, const uint8_t *s2, int stride)
233
{
234
    asm volatile(
235
        "pxor %%mm7, %%mm7              \n\t"
236
        "mov $-128, %%"REG_a"           \n\t"
237
        ASMALIGN(4)
238
        "1:                             \n\t"
239
        "movq (%0), %%mm0               \n\t"
240
        "movq (%1), %%mm2               \n\t"
241
        "movq %%mm0, %%mm1              \n\t"
242
        "movq %%mm2, %%mm3              \n\t"
243
        "punpcklbw %%mm7, %%mm0         \n\t"
244
        "punpckhbw %%mm7, %%mm1         \n\t"
245
        "punpcklbw %%mm7, %%mm2         \n\t"
246
        "punpckhbw %%mm7, %%mm3         \n\t"
247
        "psubw %%mm2, %%mm0             \n\t"
248
        "psubw %%mm3, %%mm1             \n\t"
249
        "movq %%mm0, (%2, %%"REG_a")    \n\t"
250
        "movq %%mm1, 8(%2, %%"REG_a")   \n\t"
251
        "add %3, %0                     \n\t"
252
        "add %3, %1                     \n\t"
253
        "add $16, %%"REG_a"             \n\t"
254
        "jnz 1b                         \n\t"
255
        : "+r" (s1), "+r" (s2)
256
        : "r" (block+64), "r" ((long)stride)
257
        : "%"REG_a
258
    );
259
}
260
#endif //CONFIG_ENCODERS
261

    
262
void put_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size)
263
{
264
    const DCTELEM *p;
265
    uint8_t *pix;
266

    
267
    /* read the pixels */
268
    p = block;
269
    pix = pixels;
270
    /* unrolled loop */
271
        __asm __volatile(
272
                "movq   %3, %%mm0               \n\t"
273
                "movq   8%3, %%mm1              \n\t"
274
                "movq   16%3, %%mm2             \n\t"
275
                "movq   24%3, %%mm3             \n\t"
276
                "movq   32%3, %%mm4             \n\t"
277
                "movq   40%3, %%mm5             \n\t"
278
                "movq   48%3, %%mm6             \n\t"
279
                "movq   56%3, %%mm7             \n\t"
280
                "packuswb %%mm1, %%mm0          \n\t"
281
                "packuswb %%mm3, %%mm2          \n\t"
282
                "packuswb %%mm5, %%mm4          \n\t"
283
                "packuswb %%mm7, %%mm6          \n\t"
284
                "movq   %%mm0, (%0)             \n\t"
285
                "movq   %%mm2, (%0, %1)         \n\t"
286
                "movq   %%mm4, (%0, %1, 2)      \n\t"
287
                "movq   %%mm6, (%0, %2)         \n\t"
288
                ::"r" (pix), "r" ((long)line_size), "r" ((long)line_size*3), "m"(*p)
289
                :"memory");
290
        pix += line_size*4;
291
        p += 32;
292

    
293
    // if here would be an exact copy of the code above
294
    // compiler would generate some very strange code
295
    // thus using "r"
296
    __asm __volatile(
297
            "movq       (%3), %%mm0             \n\t"
298
            "movq       8(%3), %%mm1            \n\t"
299
            "movq       16(%3), %%mm2           \n\t"
300
            "movq       24(%3), %%mm3           \n\t"
301
            "movq       32(%3), %%mm4           \n\t"
302
            "movq       40(%3), %%mm5           \n\t"
303
            "movq       48(%3), %%mm6           \n\t"
304
            "movq       56(%3), %%mm7           \n\t"
305
            "packuswb %%mm1, %%mm0              \n\t"
306
            "packuswb %%mm3, %%mm2              \n\t"
307
            "packuswb %%mm5, %%mm4              \n\t"
308
            "packuswb %%mm7, %%mm6              \n\t"
309
            "movq       %%mm0, (%0)             \n\t"
310
            "movq       %%mm2, (%0, %1)         \n\t"
311
            "movq       %%mm4, (%0, %1, 2)      \n\t"
312
            "movq       %%mm6, (%0, %2)         \n\t"
313
            ::"r" (pix), "r" ((long)line_size), "r" ((long)line_size*3), "r"(p)
314
            :"memory");
315
}
316

    
317
static DECLARE_ALIGNED_8(const unsigned char, vector128[8]) =
318
  { 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 };
319

    
320
void put_signed_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size)
321
{
322
    int i;
323

    
324
    movq_m2r(*vector128, mm1);
325
    for (i = 0; i < 8; i++) {
326
        movq_m2r(*(block), mm0);
327
        packsswb_m2r(*(block + 4), mm0);
328
        block += 8;
329
        paddb_r2r(mm1, mm0);
330
        movq_r2m(mm0, *pixels);
331
        pixels += line_size;
332
    }
333
}
334

    
335
void add_pixels_clamped_mmx(const DCTELEM *block, uint8_t *pixels, int line_size)
336
{
337
    const DCTELEM *p;
338
    uint8_t *pix;
339
    int i;
340

    
341
    /* read the pixels */
342
    p = block;
343
    pix = pixels;
344
    MOVQ_ZERO(mm7);
345
    i = 4;
346
    do {
347
        __asm __volatile(
348
                "movq   (%2), %%mm0     \n\t"
349
                "movq   8(%2), %%mm1    \n\t"
350
                "movq   16(%2), %%mm2   \n\t"
351
                "movq   24(%2), %%mm3   \n\t"
352
                "movq   %0, %%mm4       \n\t"
353
                "movq   %1, %%mm6       \n\t"
354
                "movq   %%mm4, %%mm5    \n\t"
355
                "punpcklbw %%mm7, %%mm4 \n\t"
356
                "punpckhbw %%mm7, %%mm5 \n\t"
357
                "paddsw %%mm4, %%mm0    \n\t"
358
                "paddsw %%mm5, %%mm1    \n\t"
359
                "movq   %%mm6, %%mm5    \n\t"
360
                "punpcklbw %%mm7, %%mm6 \n\t"
361
                "punpckhbw %%mm7, %%mm5 \n\t"
362
                "paddsw %%mm6, %%mm2    \n\t"
363
                "paddsw %%mm5, %%mm3    \n\t"
364
                "packuswb %%mm1, %%mm0  \n\t"
365
                "packuswb %%mm3, %%mm2  \n\t"
366
                "movq   %%mm0, %0       \n\t"
367
                "movq   %%mm2, %1       \n\t"
368
                :"+m"(*pix), "+m"(*(pix+line_size))
369
                :"r"(p)
370
                :"memory");
371
        pix += line_size*2;
372
        p += 16;
373
    } while (--i);
374
}
375

    
376
static void put_pixels4_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
377
{
378
    __asm __volatile(
379
         "lea (%3, %3), %%"REG_a"       \n\t"
380
         ASMALIGN(3)
381
         "1:                            \n\t"
382
         "movd (%1), %%mm0              \n\t"
383
         "movd (%1, %3), %%mm1          \n\t"
384
         "movd %%mm0, (%2)              \n\t"
385
         "movd %%mm1, (%2, %3)          \n\t"
386
         "add %%"REG_a", %1             \n\t"
387
         "add %%"REG_a", %2             \n\t"
388
         "movd (%1), %%mm0              \n\t"
389
         "movd (%1, %3), %%mm1          \n\t"
390
         "movd %%mm0, (%2)              \n\t"
391
         "movd %%mm1, (%2, %3)          \n\t"
392
         "add %%"REG_a", %1             \n\t"
393
         "add %%"REG_a", %2             \n\t"
394
         "subl $4, %0                   \n\t"
395
         "jnz 1b                        \n\t"
396
         : "+g"(h), "+r" (pixels),  "+r" (block)
397
         : "r"((long)line_size)
398
         : "%"REG_a, "memory"
399
        );
400
}
401

    
402
static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
403
{
404
    __asm __volatile(
405
         "lea (%3, %3), %%"REG_a"       \n\t"
406
         ASMALIGN(3)
407
         "1:                            \n\t"
408
         "movq (%1), %%mm0              \n\t"
409
         "movq (%1, %3), %%mm1          \n\t"
410
         "movq %%mm0, (%2)              \n\t"
411
         "movq %%mm1, (%2, %3)          \n\t"
412
         "add %%"REG_a", %1             \n\t"
413
         "add %%"REG_a", %2             \n\t"
414
         "movq (%1), %%mm0              \n\t"
415
         "movq (%1, %3), %%mm1          \n\t"
416
         "movq %%mm0, (%2)              \n\t"
417
         "movq %%mm1, (%2, %3)          \n\t"
418
         "add %%"REG_a", %1             \n\t"
419
         "add %%"REG_a", %2             \n\t"
420
         "subl $4, %0                   \n\t"
421
         "jnz 1b                        \n\t"
422
         : "+g"(h), "+r" (pixels),  "+r" (block)
423
         : "r"((long)line_size)
424
         : "%"REG_a, "memory"
425
        );
426
}
427

    
428
static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
429
{
430
    __asm __volatile(
431
         "lea (%3, %3), %%"REG_a"       \n\t"
432
         ASMALIGN(3)
433
         "1:                            \n\t"
434
         "movq (%1), %%mm0              \n\t"
435
         "movq 8(%1), %%mm4             \n\t"
436
         "movq (%1, %3), %%mm1          \n\t"
437
         "movq 8(%1, %3), %%mm5         \n\t"
438
         "movq %%mm0, (%2)              \n\t"
439
         "movq %%mm4, 8(%2)             \n\t"
440
         "movq %%mm1, (%2, %3)          \n\t"
441
         "movq %%mm5, 8(%2, %3)         \n\t"
442
         "add %%"REG_a", %1             \n\t"
443
         "add %%"REG_a", %2             \n\t"
444
         "movq (%1), %%mm0              \n\t"
445
         "movq 8(%1), %%mm4             \n\t"
446
         "movq (%1, %3), %%mm1          \n\t"
447
         "movq 8(%1, %3), %%mm5         \n\t"
448
         "movq %%mm0, (%2)              \n\t"
449
         "movq %%mm4, 8(%2)             \n\t"
450
         "movq %%mm1, (%2, %3)          \n\t"
451
         "movq %%mm5, 8(%2, %3)         \n\t"
452
         "add %%"REG_a", %1             \n\t"
453
         "add %%"REG_a", %2             \n\t"
454
         "subl $4, %0                   \n\t"
455
         "jnz 1b                        \n\t"
456
         : "+g"(h), "+r" (pixels),  "+r" (block)
457
         : "r"((long)line_size)
458
         : "%"REG_a, "memory"
459
        );
460
}
461

    
462
static void clear_blocks_mmx(DCTELEM *blocks)
463
{
464
    __asm __volatile(
465
                "pxor %%mm7, %%mm7              \n\t"
466
                "mov $-128*6, %%"REG_a"         \n\t"
467
                "1:                             \n\t"
468
                "movq %%mm7, (%0, %%"REG_a")    \n\t"
469
                "movq %%mm7, 8(%0, %%"REG_a")   \n\t"
470
                "movq %%mm7, 16(%0, %%"REG_a")  \n\t"
471
                "movq %%mm7, 24(%0, %%"REG_a")  \n\t"
472
                "add $32, %%"REG_a"             \n\t"
473
                " js 1b                         \n\t"
474
                : : "r" (((uint8_t *)blocks)+128*6)
475
                : "%"REG_a
476
        );
477
}
478

    
479
#ifdef CONFIG_ENCODERS
480
static int pix_sum16_mmx(uint8_t * pix, int line_size){
481
    const int h=16;
482
    int sum;
483
    long index= -line_size*h;
484

    
485
    __asm __volatile(
486
                "pxor %%mm7, %%mm7              \n\t"
487
                "pxor %%mm6, %%mm6              \n\t"
488
                "1:                             \n\t"
489
                "movq (%2, %1), %%mm0           \n\t"
490
                "movq (%2, %1), %%mm1           \n\t"
491
                "movq 8(%2, %1), %%mm2          \n\t"
492
                "movq 8(%2, %1), %%mm3          \n\t"
493
                "punpcklbw %%mm7, %%mm0         \n\t"
494
                "punpckhbw %%mm7, %%mm1         \n\t"
495
                "punpcklbw %%mm7, %%mm2         \n\t"
496
                "punpckhbw %%mm7, %%mm3         \n\t"
497
                "paddw %%mm0, %%mm1             \n\t"
498
                "paddw %%mm2, %%mm3             \n\t"
499
                "paddw %%mm1, %%mm3             \n\t"
500
                "paddw %%mm3, %%mm6             \n\t"
501
                "add %3, %1                     \n\t"
502
                " js 1b                         \n\t"
503
                "movq %%mm6, %%mm5              \n\t"
504
                "psrlq $32, %%mm6               \n\t"
505
                "paddw %%mm5, %%mm6             \n\t"
506
                "movq %%mm6, %%mm5              \n\t"
507
                "psrlq $16, %%mm6               \n\t"
508
                "paddw %%mm5, %%mm6             \n\t"
509
                "movd %%mm6, %0                 \n\t"
510
                "andl $0xFFFF, %0               \n\t"
511
                : "=&r" (sum), "+r" (index)
512
                : "r" (pix - index), "r" ((long)line_size)
513
        );
514

    
515
        return sum;
516
}
517
#endif //CONFIG_ENCODERS
518

    
519
static void add_bytes_mmx(uint8_t *dst, uint8_t *src, int w){
520
    long i=0;
521
    asm volatile(
522
        "1:                             \n\t"
523
        "movq  (%1, %0), %%mm0          \n\t"
524
        "movq  (%2, %0), %%mm1          \n\t"
525
        "paddb %%mm0, %%mm1             \n\t"
526
        "movq %%mm1, (%2, %0)           \n\t"
527
        "movq 8(%1, %0), %%mm0          \n\t"
528
        "movq 8(%2, %0), %%mm1          \n\t"
529
        "paddb %%mm0, %%mm1             \n\t"
530
        "movq %%mm1, 8(%2, %0)          \n\t"
531
        "add $16, %0                    \n\t"
532
        "cmp %3, %0                     \n\t"
533
        " jb 1b                         \n\t"
534
        : "+r" (i)
535
        : "r"(src), "r"(dst), "r"((long)w-15)
536
    );
537
    for(; i<w; i++)
538
        dst[i+0] += src[i+0];
539
}
540

    
541
#define H263_LOOP_FILTER \
542
        "pxor %%mm7, %%mm7              \n\t"\
543
        "movq  %0, %%mm0                \n\t"\
544
        "movq  %0, %%mm1                \n\t"\
545
        "movq  %3, %%mm2                \n\t"\
546
        "movq  %3, %%mm3                \n\t"\
547
        "punpcklbw %%mm7, %%mm0         \n\t"\
548
        "punpckhbw %%mm7, %%mm1         \n\t"\
549
        "punpcklbw %%mm7, %%mm2         \n\t"\
550
        "punpckhbw %%mm7, %%mm3         \n\t"\
551
        "psubw %%mm2, %%mm0             \n\t"\
552
        "psubw %%mm3, %%mm1             \n\t"\
553
        "movq  %1, %%mm2                \n\t"\
554
        "movq  %1, %%mm3                \n\t"\
555
        "movq  %2, %%mm4                \n\t"\
556
        "movq  %2, %%mm5                \n\t"\
557
        "punpcklbw %%mm7, %%mm2         \n\t"\
558
        "punpckhbw %%mm7, %%mm3         \n\t"\
559
        "punpcklbw %%mm7, %%mm4         \n\t"\
560
        "punpckhbw %%mm7, %%mm5         \n\t"\
561
        "psubw %%mm2, %%mm4             \n\t"\
562
        "psubw %%mm3, %%mm5             \n\t"\
563
        "psllw $2, %%mm4                \n\t"\
564
        "psllw $2, %%mm5                \n\t"\
565
        "paddw %%mm0, %%mm4             \n\t"\
566
        "paddw %%mm1, %%mm5             \n\t"\
567
        "pxor %%mm6, %%mm6              \n\t"\
568
        "pcmpgtw %%mm4, %%mm6           \n\t"\
569
        "pcmpgtw %%mm5, %%mm7           \n\t"\
570
        "pxor %%mm6, %%mm4              \n\t"\
571
        "pxor %%mm7, %%mm5              \n\t"\
572
        "psubw %%mm6, %%mm4             \n\t"\
573
        "psubw %%mm7, %%mm5             \n\t"\
574
        "psrlw $3, %%mm4                \n\t"\
575
        "psrlw $3, %%mm5                \n\t"\
576
        "packuswb %%mm5, %%mm4          \n\t"\
577
        "packsswb %%mm7, %%mm6          \n\t"\
578
        "pxor %%mm7, %%mm7              \n\t"\
579
        "movd %4, %%mm2                 \n\t"\
580
        "punpcklbw %%mm2, %%mm2         \n\t"\
581
        "punpcklbw %%mm2, %%mm2         \n\t"\
582
        "punpcklbw %%mm2, %%mm2         \n\t"\
583
        "psubusb %%mm4, %%mm2           \n\t"\
584
        "movq %%mm2, %%mm3              \n\t"\
585
        "psubusb %%mm4, %%mm3           \n\t"\
586
        "psubb %%mm3, %%mm2             \n\t"\
587
        "movq %1, %%mm3                 \n\t"\
588
        "movq %2, %%mm4                 \n\t"\
589
        "pxor %%mm6, %%mm3              \n\t"\
590
        "pxor %%mm6, %%mm4              \n\t"\
591
        "paddusb %%mm2, %%mm3           \n\t"\
592
        "psubusb %%mm2, %%mm4           \n\t"\
593
        "pxor %%mm6, %%mm3              \n\t"\
594
        "pxor %%mm6, %%mm4              \n\t"\
595
        "paddusb %%mm2, %%mm2           \n\t"\
596
        "packsswb %%mm1, %%mm0          \n\t"\
597
        "pcmpgtb %%mm0, %%mm7           \n\t"\
598
        "pxor %%mm7, %%mm0              \n\t"\
599
        "psubb %%mm7, %%mm0             \n\t"\
600
        "movq %%mm0, %%mm1              \n\t"\
601
        "psubusb %%mm2, %%mm0           \n\t"\
602
        "psubb %%mm0, %%mm1             \n\t"\
603
        "pand %5, %%mm1                 \n\t"\
604
        "psrlw $2, %%mm1                \n\t"\
605
        "pxor %%mm7, %%mm1              \n\t"\
606
        "psubb %%mm7, %%mm1             \n\t"\
607
        "movq %0, %%mm5                 \n\t"\
608
        "movq %3, %%mm6                 \n\t"\
609
        "psubb %%mm1, %%mm5             \n\t"\
610
        "paddb %%mm1, %%mm6             \n\t"
611

    
612
static void h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale){
613
    const int strength= ff_h263_loop_filter_strength[qscale];
614

    
615
    asm volatile(
616

    
617
        H263_LOOP_FILTER
618

    
619
        "movq %%mm3, %1                 \n\t"
620
        "movq %%mm4, %2                 \n\t"
621
        "movq %%mm5, %0                 \n\t"
622
        "movq %%mm6, %3                 \n\t"
623
        : "+m" (*(uint64_t*)(src - 2*stride)),
624
          "+m" (*(uint64_t*)(src - 1*stride)),
625
          "+m" (*(uint64_t*)(src + 0*stride)),
626
          "+m" (*(uint64_t*)(src + 1*stride))
627
        : "g" (2*strength), "m"(ff_pb_FC)
628
    );
629
}
630

    
631
static inline void transpose4x4(uint8_t *dst, uint8_t *src, int dst_stride, int src_stride){
632
    asm volatile( //FIXME could save 1 instruction if done as 8x4 ...
633
        "movd  %4, %%mm0                \n\t"
634
        "movd  %5, %%mm1                \n\t"
635
        "movd  %6, %%mm2                \n\t"
636
        "movd  %7, %%mm3                \n\t"
637
        "punpcklbw %%mm1, %%mm0         \n\t"
638
        "punpcklbw %%mm3, %%mm2         \n\t"
639
        "movq %%mm0, %%mm1              \n\t"
640
        "punpcklwd %%mm2, %%mm0         \n\t"
641
        "punpckhwd %%mm2, %%mm1         \n\t"
642
        "movd  %%mm0, %0                \n\t"
643
        "punpckhdq %%mm0, %%mm0         \n\t"
644
        "movd  %%mm0, %1                \n\t"
645
        "movd  %%mm1, %2                \n\t"
646
        "punpckhdq %%mm1, %%mm1         \n\t"
647
        "movd  %%mm1, %3                \n\t"
648

    
649
        : "=m" (*(uint32_t*)(dst + 0*dst_stride)),
650
          "=m" (*(uint32_t*)(dst + 1*dst_stride)),
651
          "=m" (*(uint32_t*)(dst + 2*dst_stride)),
652
          "=m" (*(uint32_t*)(dst + 3*dst_stride))
653
        :  "m" (*(uint32_t*)(src + 0*src_stride)),
654
           "m" (*(uint32_t*)(src + 1*src_stride)),
655
           "m" (*(uint32_t*)(src + 2*src_stride)),
656
           "m" (*(uint32_t*)(src + 3*src_stride))
657
    );
658
}
659

    
660
static void h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale){
661
    const int strength= ff_h263_loop_filter_strength[qscale];
662
    uint64_t temp[4] __attribute__ ((aligned(8)));
663
    uint8_t *btemp= (uint8_t*)temp;
664

    
665
    src -= 2;
666

    
667
    transpose4x4(btemp  , src           , 8, stride);
668
    transpose4x4(btemp+4, src + 4*stride, 8, stride);
669
    asm volatile(
670
        H263_LOOP_FILTER // 5 3 4 6
671

    
672
        : "+m" (temp[0]),
673
          "+m" (temp[1]),
674
          "+m" (temp[2]),
675
          "+m" (temp[3])
676
        : "g" (2*strength), "m"(ff_pb_FC)
677
    );
678

    
679
    asm volatile(
680
        "movq %%mm5, %%mm1              \n\t"
681
        "movq %%mm4, %%mm0              \n\t"
682
        "punpcklbw %%mm3, %%mm5         \n\t"
683
        "punpcklbw %%mm6, %%mm4         \n\t"
684
        "punpckhbw %%mm3, %%mm1         \n\t"
685
        "punpckhbw %%mm6, %%mm0         \n\t"
686
        "movq %%mm5, %%mm3              \n\t"
687
        "movq %%mm1, %%mm6              \n\t"
688
        "punpcklwd %%mm4, %%mm5         \n\t"
689
        "punpcklwd %%mm0, %%mm1         \n\t"
690
        "punpckhwd %%mm4, %%mm3         \n\t"
691
        "punpckhwd %%mm0, %%mm6         \n\t"
692
        "movd %%mm5, (%0)               \n\t"
693
        "punpckhdq %%mm5, %%mm5         \n\t"
694
        "movd %%mm5, (%0,%2)            \n\t"
695
        "movd %%mm3, (%0,%2,2)          \n\t"
696
        "punpckhdq %%mm3, %%mm3         \n\t"
697
        "movd %%mm3, (%0,%3)            \n\t"
698
        "movd %%mm1, (%1)               \n\t"
699
        "punpckhdq %%mm1, %%mm1         \n\t"
700
        "movd %%mm1, (%1,%2)            \n\t"
701
        "movd %%mm6, (%1,%2,2)          \n\t"
702
        "punpckhdq %%mm6, %%mm6         \n\t"
703
        "movd %%mm6, (%1,%3)            \n\t"
704
        :: "r" (src),
705
           "r" (src + 4*stride),
706
           "r" ((long)   stride ),
707
           "r" ((long)(3*stride))
708
    );
709
}
710

    
711
#ifdef CONFIG_ENCODERS
712
static int pix_norm1_mmx(uint8_t *pix, int line_size) {
713
    int tmp;
714
  asm volatile (
715
      "movl $16,%%ecx\n"
716
      "pxor %%mm0,%%mm0\n"
717
      "pxor %%mm7,%%mm7\n"
718
      "1:\n"
719
      "movq (%0),%%mm2\n"       /* mm2 = pix[0-7] */
720
      "movq 8(%0),%%mm3\n"      /* mm3 = pix[8-15] */
721

    
722
      "movq %%mm2,%%mm1\n"      /* mm1 = mm2 = pix[0-7] */
723

    
724
      "punpckhbw %%mm0,%%mm1\n" /* mm1 = [pix4-7] */
725
      "punpcklbw %%mm0,%%mm2\n" /* mm2 = [pix0-3] */
726

    
727
      "movq %%mm3,%%mm4\n"      /* mm4 = mm3 = pix[8-15] */
728
      "punpckhbw %%mm0,%%mm3\n" /* mm3 = [pix12-15] */
729
      "punpcklbw %%mm0,%%mm4\n" /* mm4 = [pix8-11] */
730

    
731
      "pmaddwd %%mm1,%%mm1\n"   /* mm1 = (pix0^2+pix1^2,pix2^2+pix3^2) */
732
      "pmaddwd %%mm2,%%mm2\n"   /* mm2 = (pix4^2+pix5^2,pix6^2+pix7^2) */
733

    
734
      "pmaddwd %%mm3,%%mm3\n"
735
      "pmaddwd %%mm4,%%mm4\n"
736

    
737
      "paddd %%mm1,%%mm2\n"     /* mm2 = (pix0^2+pix1^2+pix4^2+pix5^2,
738
                                          pix2^2+pix3^2+pix6^2+pix7^2) */
739
      "paddd %%mm3,%%mm4\n"
740
      "paddd %%mm2,%%mm7\n"
741

    
742
      "add %2, %0\n"
743
      "paddd %%mm4,%%mm7\n"
744
      "dec %%ecx\n"
745
      "jnz 1b\n"
746

    
747
      "movq %%mm7,%%mm1\n"
748
      "psrlq $32, %%mm7\n"      /* shift hi dword to lo */
749
      "paddd %%mm7,%%mm1\n"
750
      "movd %%mm1,%1\n"
751
      : "+r" (pix), "=r"(tmp) : "r" ((long)line_size) : "%ecx" );
752
    return tmp;
753
}
754

    
755
static int sse8_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
756
    int tmp;
757
  asm volatile (
758
      "movl %4,%%ecx\n"
759
      "shr $1,%%ecx\n"
760
      "pxor %%mm0,%%mm0\n"      /* mm0 = 0 */
761
      "pxor %%mm7,%%mm7\n"      /* mm7 holds the sum */
762
      "1:\n"
763
      "movq (%0),%%mm1\n"       /* mm1 = pix1[0][0-7] */
764
      "movq (%1),%%mm2\n"       /* mm2 = pix2[0][0-7] */
765
      "movq (%0,%3),%%mm3\n"    /* mm3 = pix1[1][0-7] */
766
      "movq (%1,%3),%%mm4\n"    /* mm4 = pix2[1][0-7] */
767

    
768
      /* todo: mm1-mm2, mm3-mm4 */
769
      /* algo: substract mm1 from mm2 with saturation and vice versa */
770
      /*       OR the results to get absolute difference */
771
      "movq %%mm1,%%mm5\n"
772
      "movq %%mm3,%%mm6\n"
773
      "psubusb %%mm2,%%mm1\n"
774
      "psubusb %%mm4,%%mm3\n"
775
      "psubusb %%mm5,%%mm2\n"
776
      "psubusb %%mm6,%%mm4\n"
777

    
778
      "por %%mm1,%%mm2\n"
779
      "por %%mm3,%%mm4\n"
780

    
781
      /* now convert to 16-bit vectors so we can square them */
782
      "movq %%mm2,%%mm1\n"
783
      "movq %%mm4,%%mm3\n"
784

    
785
      "punpckhbw %%mm0,%%mm2\n"
786
      "punpckhbw %%mm0,%%mm4\n"
787
      "punpcklbw %%mm0,%%mm1\n" /* mm1 now spread over (mm1,mm2) */
788
      "punpcklbw %%mm0,%%mm3\n" /* mm4 now spread over (mm3,mm4) */
789

    
790
      "pmaddwd %%mm2,%%mm2\n"
791
      "pmaddwd %%mm4,%%mm4\n"
792
      "pmaddwd %%mm1,%%mm1\n"
793
      "pmaddwd %%mm3,%%mm3\n"
794

    
795
      "lea (%0,%3,2), %0\n"     /* pix1 += 2*line_size */
796
      "lea (%1,%3,2), %1\n"     /* pix2 += 2*line_size */
797

    
798
      "paddd %%mm2,%%mm1\n"
799
      "paddd %%mm4,%%mm3\n"
800
      "paddd %%mm1,%%mm7\n"
801
      "paddd %%mm3,%%mm7\n"
802

    
803
      "decl %%ecx\n"
804
      "jnz 1b\n"
805

    
806
      "movq %%mm7,%%mm1\n"
807
      "psrlq $32, %%mm7\n"      /* shift hi dword to lo */
808
      "paddd %%mm7,%%mm1\n"
809
      "movd %%mm1,%2\n"
810
      : "+r" (pix1), "+r" (pix2), "=r"(tmp)
811
      : "r" ((long)line_size) , "m" (h)
812
      : "%ecx");
813
    return tmp;
814
}
815

    
816
static int sse16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
817
    int tmp;
818
  asm volatile (
819
      "movl %4,%%ecx\n"
820
      "pxor %%mm0,%%mm0\n"      /* mm0 = 0 */
821
      "pxor %%mm7,%%mm7\n"      /* mm7 holds the sum */
822
      "1:\n"
823
      "movq (%0),%%mm1\n"       /* mm1 = pix1[0-7] */
824
      "movq (%1),%%mm2\n"       /* mm2 = pix2[0-7] */
825
      "movq 8(%0),%%mm3\n"      /* mm3 = pix1[8-15] */
826
      "movq 8(%1),%%mm4\n"      /* mm4 = pix2[8-15] */
827

    
828
      /* todo: mm1-mm2, mm3-mm4 */
829
      /* algo: substract mm1 from mm2 with saturation and vice versa */
830
      /*       OR the results to get absolute difference */
831
      "movq %%mm1,%%mm5\n"
832
      "movq %%mm3,%%mm6\n"
833
      "psubusb %%mm2,%%mm1\n"
834
      "psubusb %%mm4,%%mm3\n"
835
      "psubusb %%mm5,%%mm2\n"
836
      "psubusb %%mm6,%%mm4\n"
837

    
838
      "por %%mm1,%%mm2\n"
839
      "por %%mm3,%%mm4\n"
840

    
841
      /* now convert to 16-bit vectors so we can square them */
842
      "movq %%mm2,%%mm1\n"
843
      "movq %%mm4,%%mm3\n"
844

    
845
      "punpckhbw %%mm0,%%mm2\n"
846
      "punpckhbw %%mm0,%%mm4\n"
847
      "punpcklbw %%mm0,%%mm1\n" /* mm1 now spread over (mm1,mm2) */
848
      "punpcklbw %%mm0,%%mm3\n" /* mm4 now spread over (mm3,mm4) */
849

    
850
      "pmaddwd %%mm2,%%mm2\n"
851
      "pmaddwd %%mm4,%%mm4\n"
852
      "pmaddwd %%mm1,%%mm1\n"
853
      "pmaddwd %%mm3,%%mm3\n"
854

    
855
      "add %3,%0\n"
856
      "add %3,%1\n"
857

    
858
      "paddd %%mm2,%%mm1\n"
859
      "paddd %%mm4,%%mm3\n"
860
      "paddd %%mm1,%%mm7\n"
861
      "paddd %%mm3,%%mm7\n"
862

    
863
      "decl %%ecx\n"
864
      "jnz 1b\n"
865

    
866
      "movq %%mm7,%%mm1\n"
867
      "psrlq $32, %%mm7\n"      /* shift hi dword to lo */
868
      "paddd %%mm7,%%mm1\n"
869
      "movd %%mm1,%2\n"
870
      : "+r" (pix1), "+r" (pix2), "=r"(tmp)
871
      : "r" ((long)line_size) , "m" (h)
872
      : "%ecx");
873
    return tmp;
874
}
875

    
876
static int sse16_sse2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
877
    int tmp;
878
  asm volatile (
879
      "shr $1,%2\n"
880
      "pxor %%xmm0,%%xmm0\n"    /* mm0 = 0 */
881
      "pxor %%xmm7,%%xmm7\n"    /* mm7 holds the sum */
882
      "1:\n"
883
      "movdqu (%0),%%xmm1\n"    /* mm1 = pix1[0][0-15] */
884
      "movdqu (%1),%%xmm2\n"    /* mm2 = pix2[0][0-15] */
885
      "movdqu (%0,%4),%%xmm3\n" /* mm3 = pix1[1][0-15] */
886
      "movdqu (%1,%4),%%xmm4\n" /* mm4 = pix2[1][0-15] */
887

    
888
      /* todo: mm1-mm2, mm3-mm4 */
889
      /* algo: substract mm1 from mm2 with saturation and vice versa */
890
      /*       OR the results to get absolute difference */
891
      "movdqa %%xmm1,%%xmm5\n"
892
      "movdqa %%xmm3,%%xmm6\n"
893
      "psubusb %%xmm2,%%xmm1\n"
894
      "psubusb %%xmm4,%%xmm3\n"
895
      "psubusb %%xmm5,%%xmm2\n"
896
      "psubusb %%xmm6,%%xmm4\n"
897

    
898
      "por %%xmm1,%%xmm2\n"
899
      "por %%xmm3,%%xmm4\n"
900

    
901
      /* now convert to 16-bit vectors so we can square them */
902
      "movdqa %%xmm2,%%xmm1\n"
903
      "movdqa %%xmm4,%%xmm3\n"
904

    
905
      "punpckhbw %%xmm0,%%xmm2\n"
906
      "punpckhbw %%xmm0,%%xmm4\n"
907
      "punpcklbw %%xmm0,%%xmm1\n"  /* mm1 now spread over (mm1,mm2) */
908
      "punpcklbw %%xmm0,%%xmm3\n"  /* mm4 now spread over (mm3,mm4) */
909

    
910
      "pmaddwd %%xmm2,%%xmm2\n"
911
      "pmaddwd %%xmm4,%%xmm4\n"
912
      "pmaddwd %%xmm1,%%xmm1\n"
913
      "pmaddwd %%xmm3,%%xmm3\n"
914

    
915
      "lea (%0,%4,2), %0\n"        /* pix1 += 2*line_size */
916
      "lea (%1,%4,2), %1\n"        /* pix2 += 2*line_size */
917

    
918
      "paddd %%xmm2,%%xmm1\n"
919
      "paddd %%xmm4,%%xmm3\n"
920
      "paddd %%xmm1,%%xmm7\n"
921
      "paddd %%xmm3,%%xmm7\n"
922

    
923
      "decl %2\n"
924
      "jnz 1b\n"
925

    
926
      "movdqa %%xmm7,%%xmm1\n"
927
      "psrldq $8, %%xmm7\n"        /* shift hi qword to lo */
928
      "paddd %%xmm1,%%xmm7\n"
929
      "movdqa %%xmm7,%%xmm1\n"
930
      "psrldq $4, %%xmm7\n"        /* shift hi dword to lo */
931
      "paddd %%xmm1,%%xmm7\n"
932
      "movd %%xmm7,%3\n"
933
      : "+r" (pix1), "+r" (pix2), "+r"(h), "=r"(tmp)
934
      : "r" ((long)line_size));
935
    return tmp;
936
}
937

    
938
static int hf_noise8_mmx(uint8_t * pix1, int line_size, int h) {
939
    int tmp;
940
  asm volatile (
941
      "movl %3,%%ecx\n"
942
      "pxor %%mm7,%%mm7\n"
943
      "pxor %%mm6,%%mm6\n"
944

    
945
      "movq (%0),%%mm0\n"
946
      "movq %%mm0, %%mm1\n"
947
      "psllq $8, %%mm0\n"
948
      "psrlq $8, %%mm1\n"
949
      "psrlq $8, %%mm0\n"
950
      "movq %%mm0, %%mm2\n"
951
      "movq %%mm1, %%mm3\n"
952
      "punpcklbw %%mm7,%%mm0\n"
953
      "punpcklbw %%mm7,%%mm1\n"
954
      "punpckhbw %%mm7,%%mm2\n"
955
      "punpckhbw %%mm7,%%mm3\n"
956
      "psubw %%mm1, %%mm0\n"
957
      "psubw %%mm3, %%mm2\n"
958

    
959
      "add %2,%0\n"
960

    
961
      "movq (%0),%%mm4\n"
962
      "movq %%mm4, %%mm1\n"
963
      "psllq $8, %%mm4\n"
964
      "psrlq $8, %%mm1\n"
965
      "psrlq $8, %%mm4\n"
966
      "movq %%mm4, %%mm5\n"
967
      "movq %%mm1, %%mm3\n"
968
      "punpcklbw %%mm7,%%mm4\n"
969
      "punpcklbw %%mm7,%%mm1\n"
970
      "punpckhbw %%mm7,%%mm5\n"
971
      "punpckhbw %%mm7,%%mm3\n"
972
      "psubw %%mm1, %%mm4\n"
973
      "psubw %%mm3, %%mm5\n"
974
      "psubw %%mm4, %%mm0\n"
975
      "psubw %%mm5, %%mm2\n"
976
      "pxor %%mm3, %%mm3\n"
977
      "pxor %%mm1, %%mm1\n"
978
      "pcmpgtw %%mm0, %%mm3\n\t"
979
      "pcmpgtw %%mm2, %%mm1\n\t"
980
      "pxor %%mm3, %%mm0\n"
981
      "pxor %%mm1, %%mm2\n"
982
      "psubw %%mm3, %%mm0\n"
983
      "psubw %%mm1, %%mm2\n"
984
      "paddw %%mm0, %%mm2\n"
985
      "paddw %%mm2, %%mm6\n"
986

    
987
      "add %2,%0\n"
988
      "1:\n"
989

    
990
      "movq (%0),%%mm0\n"
991
      "movq %%mm0, %%mm1\n"
992
      "psllq $8, %%mm0\n"
993
      "psrlq $8, %%mm1\n"
994
      "psrlq $8, %%mm0\n"
995
      "movq %%mm0, %%mm2\n"
996
      "movq %%mm1, %%mm3\n"
997
      "punpcklbw %%mm7,%%mm0\n"
998
      "punpcklbw %%mm7,%%mm1\n"
999
      "punpckhbw %%mm7,%%mm2\n"
1000
      "punpckhbw %%mm7,%%mm3\n"
1001
      "psubw %%mm1, %%mm0\n"
1002
      "psubw %%mm3, %%mm2\n"
1003
      "psubw %%mm0, %%mm4\n"
1004
      "psubw %%mm2, %%mm5\n"
1005
      "pxor %%mm3, %%mm3\n"
1006
      "pxor %%mm1, %%mm1\n"
1007
      "pcmpgtw %%mm4, %%mm3\n\t"
1008
      "pcmpgtw %%mm5, %%mm1\n\t"
1009
      "pxor %%mm3, %%mm4\n"
1010
      "pxor %%mm1, %%mm5\n"
1011
      "psubw %%mm3, %%mm4\n"
1012
      "psubw %%mm1, %%mm5\n"
1013
      "paddw %%mm4, %%mm5\n"
1014
      "paddw %%mm5, %%mm6\n"
1015

    
1016
      "add %2,%0\n"
1017

    
1018
      "movq (%0),%%mm4\n"
1019
      "movq %%mm4, %%mm1\n"
1020
      "psllq $8, %%mm4\n"
1021
      "psrlq $8, %%mm1\n"
1022
      "psrlq $8, %%mm4\n"
1023
      "movq %%mm4, %%mm5\n"
1024
      "movq %%mm1, %%mm3\n"
1025
      "punpcklbw %%mm7,%%mm4\n"
1026
      "punpcklbw %%mm7,%%mm1\n"
1027
      "punpckhbw %%mm7,%%mm5\n"
1028
      "punpckhbw %%mm7,%%mm3\n"
1029
      "psubw %%mm1, %%mm4\n"
1030
      "psubw %%mm3, %%mm5\n"
1031
      "psubw %%mm4, %%mm0\n"
1032
      "psubw %%mm5, %%mm2\n"
1033
      "pxor %%mm3, %%mm3\n"
1034
      "pxor %%mm1, %%mm1\n"
1035
      "pcmpgtw %%mm0, %%mm3\n\t"
1036
      "pcmpgtw %%mm2, %%mm1\n\t"
1037
      "pxor %%mm3, %%mm0\n"
1038
      "pxor %%mm1, %%mm2\n"
1039
      "psubw %%mm3, %%mm0\n"
1040
      "psubw %%mm1, %%mm2\n"
1041
      "paddw %%mm0, %%mm2\n"
1042
      "paddw %%mm2, %%mm6\n"
1043

    
1044
      "add %2,%0\n"
1045
      "subl $2, %%ecx\n"
1046
      " jnz 1b\n"
1047

    
1048
      "movq %%mm6, %%mm0\n"
1049
      "punpcklwd %%mm7,%%mm0\n"
1050
      "punpckhwd %%mm7,%%mm6\n"
1051
      "paddd %%mm0, %%mm6\n"
1052

    
1053
      "movq %%mm6,%%mm0\n"
1054
      "psrlq $32, %%mm6\n"
1055
      "paddd %%mm6,%%mm0\n"
1056
      "movd %%mm0,%1\n"
1057
      : "+r" (pix1), "=r"(tmp)
1058
      : "r" ((long)line_size) , "g" (h-2)
1059
      : "%ecx");
1060
      return tmp;
1061
}
1062

    
1063
static int hf_noise16_mmx(uint8_t * pix1, int line_size, int h) {
1064
    int tmp;
1065
    uint8_t * pix= pix1;
1066
  asm volatile (
1067
      "movl %3,%%ecx\n"
1068
      "pxor %%mm7,%%mm7\n"
1069
      "pxor %%mm6,%%mm6\n"
1070

    
1071
      "movq (%0),%%mm0\n"
1072
      "movq 1(%0),%%mm1\n"
1073
      "movq %%mm0, %%mm2\n"
1074
      "movq %%mm1, %%mm3\n"
1075
      "punpcklbw %%mm7,%%mm0\n"
1076
      "punpcklbw %%mm7,%%mm1\n"
1077
      "punpckhbw %%mm7,%%mm2\n"
1078
      "punpckhbw %%mm7,%%mm3\n"
1079
      "psubw %%mm1, %%mm0\n"
1080
      "psubw %%mm3, %%mm2\n"
1081

    
1082
      "add %2,%0\n"
1083

    
1084
      "movq (%0),%%mm4\n"
1085
      "movq 1(%0),%%mm1\n"
1086
      "movq %%mm4, %%mm5\n"
1087
      "movq %%mm1, %%mm3\n"
1088
      "punpcklbw %%mm7,%%mm4\n"
1089
      "punpcklbw %%mm7,%%mm1\n"
1090
      "punpckhbw %%mm7,%%mm5\n"
1091
      "punpckhbw %%mm7,%%mm3\n"
1092
      "psubw %%mm1, %%mm4\n"
1093
      "psubw %%mm3, %%mm5\n"
1094
      "psubw %%mm4, %%mm0\n"
1095
      "psubw %%mm5, %%mm2\n"
1096
      "pxor %%mm3, %%mm3\n"
1097
      "pxor %%mm1, %%mm1\n"
1098
      "pcmpgtw %%mm0, %%mm3\n\t"
1099
      "pcmpgtw %%mm2, %%mm1\n\t"
1100
      "pxor %%mm3, %%mm0\n"
1101
      "pxor %%mm1, %%mm2\n"
1102
      "psubw %%mm3, %%mm0\n"
1103
      "psubw %%mm1, %%mm2\n"
1104
      "paddw %%mm0, %%mm2\n"
1105
      "paddw %%mm2, %%mm6\n"
1106

    
1107
      "add %2,%0\n"
1108
      "1:\n"
1109

    
1110
      "movq (%0),%%mm0\n"
1111
      "movq 1(%0),%%mm1\n"
1112
      "movq %%mm0, %%mm2\n"
1113
      "movq %%mm1, %%mm3\n"
1114
      "punpcklbw %%mm7,%%mm0\n"
1115
      "punpcklbw %%mm7,%%mm1\n"
1116
      "punpckhbw %%mm7,%%mm2\n"
1117
      "punpckhbw %%mm7,%%mm3\n"
1118
      "psubw %%mm1, %%mm0\n"
1119
      "psubw %%mm3, %%mm2\n"
1120
      "psubw %%mm0, %%mm4\n"
1121
      "psubw %%mm2, %%mm5\n"
1122
      "pxor %%mm3, %%mm3\n"
1123
      "pxor %%mm1, %%mm1\n"
1124
      "pcmpgtw %%mm4, %%mm3\n\t"
1125
      "pcmpgtw %%mm5, %%mm1\n\t"
1126
      "pxor %%mm3, %%mm4\n"
1127
      "pxor %%mm1, %%mm5\n"
1128
      "psubw %%mm3, %%mm4\n"
1129
      "psubw %%mm1, %%mm5\n"
1130
      "paddw %%mm4, %%mm5\n"
1131
      "paddw %%mm5, %%mm6\n"
1132

    
1133
      "add %2,%0\n"
1134

    
1135
      "movq (%0),%%mm4\n"
1136
      "movq 1(%0),%%mm1\n"
1137
      "movq %%mm4, %%mm5\n"
1138
      "movq %%mm1, %%mm3\n"
1139
      "punpcklbw %%mm7,%%mm4\n"
1140
      "punpcklbw %%mm7,%%mm1\n"
1141
      "punpckhbw %%mm7,%%mm5\n"
1142
      "punpckhbw %%mm7,%%mm3\n"
1143
      "psubw %%mm1, %%mm4\n"
1144
      "psubw %%mm3, %%mm5\n"
1145
      "psubw %%mm4, %%mm0\n"
1146
      "psubw %%mm5, %%mm2\n"
1147
      "pxor %%mm3, %%mm3\n"
1148
      "pxor %%mm1, %%mm1\n"
1149
      "pcmpgtw %%mm0, %%mm3\n\t"
1150
      "pcmpgtw %%mm2, %%mm1\n\t"
1151
      "pxor %%mm3, %%mm0\n"
1152
      "pxor %%mm1, %%mm2\n"
1153
      "psubw %%mm3, %%mm0\n"
1154
      "psubw %%mm1, %%mm2\n"
1155
      "paddw %%mm0, %%mm2\n"
1156
      "paddw %%mm2, %%mm6\n"
1157

    
1158
      "add %2,%0\n"
1159
      "subl $2, %%ecx\n"
1160
      " jnz 1b\n"
1161

    
1162
      "movq %%mm6, %%mm0\n"
1163
      "punpcklwd %%mm7,%%mm0\n"
1164
      "punpckhwd %%mm7,%%mm6\n"
1165
      "paddd %%mm0, %%mm6\n"
1166

    
1167
      "movq %%mm6,%%mm0\n"
1168
      "psrlq $32, %%mm6\n"
1169
      "paddd %%mm6,%%mm0\n"
1170
      "movd %%mm0,%1\n"
1171
      : "+r" (pix1), "=r"(tmp)
1172
      : "r" ((long)line_size) , "g" (h-2)
1173
      : "%ecx");
1174
      return tmp + hf_noise8_mmx(pix+8, line_size, h);
1175
}
1176

    
1177
static int nsse16_mmx(void *p, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
1178
    MpegEncContext *c = p;
1179
    int score1, score2;
1180

    
1181
    if(c) score1 = c->dsp.sse[0](c, pix1, pix2, line_size, h);
1182
    else  score1 = sse16_mmx(c, pix1, pix2, line_size, h);
1183
    score2= hf_noise16_mmx(pix1, line_size, h) - hf_noise16_mmx(pix2, line_size, h);
1184

    
1185
    if(c) return score1 + ABS(score2)*c->avctx->nsse_weight;
1186
    else  return score1 + ABS(score2)*8;
1187
}
1188

    
1189
static int nsse8_mmx(void *p, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
1190
    MpegEncContext *c = p;
1191
    int score1= sse8_mmx(c, pix1, pix2, line_size, h);
1192
    int score2= hf_noise8_mmx(pix1, line_size, h) - hf_noise8_mmx(pix2, line_size, h);
1193

    
1194
    if(c) return score1 + ABS(score2)*c->avctx->nsse_weight;
1195
    else  return score1 + ABS(score2)*8;
1196
}
1197

    
1198
static int vsad_intra16_mmx(void *v, uint8_t * pix, uint8_t * dummy, int line_size, int h) {
1199
    int tmp;
1200

    
1201
    assert( (((int)pix) & 7) == 0);
1202
    assert((line_size &7) ==0);
1203

    
1204
#define SUM(in0, in1, out0, out1) \
1205
      "movq (%0), %%mm2\n"\
1206
      "movq 8(%0), %%mm3\n"\
1207
      "add %2,%0\n"\
1208
      "movq %%mm2, " #out0 "\n"\
1209
      "movq %%mm3, " #out1 "\n"\
1210
      "psubusb " #in0 ", %%mm2\n"\
1211
      "psubusb " #in1 ", %%mm3\n"\
1212
      "psubusb " #out0 ", " #in0 "\n"\
1213
      "psubusb " #out1 ", " #in1 "\n"\
1214
      "por %%mm2, " #in0 "\n"\
1215
      "por %%mm3, " #in1 "\n"\
1216
      "movq " #in0 ", %%mm2\n"\
1217
      "movq " #in1 ", %%mm3\n"\
1218
      "punpcklbw %%mm7, " #in0 "\n"\
1219
      "punpcklbw %%mm7, " #in1 "\n"\
1220
      "punpckhbw %%mm7, %%mm2\n"\
1221
      "punpckhbw %%mm7, %%mm3\n"\
1222
      "paddw " #in1 ", " #in0 "\n"\
1223
      "paddw %%mm3, %%mm2\n"\
1224
      "paddw %%mm2, " #in0 "\n"\
1225
      "paddw " #in0 ", %%mm6\n"
1226

    
1227

    
1228
  asm volatile (
1229
      "movl %3,%%ecx\n"
1230
      "pxor %%mm6,%%mm6\n"
1231
      "pxor %%mm7,%%mm7\n"
1232
      "movq (%0),%%mm0\n"
1233
      "movq 8(%0),%%mm1\n"
1234
      "add %2,%0\n"
1235
      "subl $2, %%ecx\n"
1236
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1237
      "1:\n"
1238

    
1239
      SUM(%%mm4, %%mm5, %%mm0, %%mm1)
1240

    
1241
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1242

    
1243
      "subl $2, %%ecx\n"
1244
      "jnz 1b\n"
1245

    
1246
      "movq %%mm6,%%mm0\n"
1247
      "psrlq $32, %%mm6\n"
1248
      "paddw %%mm6,%%mm0\n"
1249
      "movq %%mm0,%%mm6\n"
1250
      "psrlq $16, %%mm0\n"
1251
      "paddw %%mm6,%%mm0\n"
1252
      "movd %%mm0,%1\n"
1253
      : "+r" (pix), "=r"(tmp)
1254
      : "r" ((long)line_size) , "m" (h)
1255
      : "%ecx");
1256
    return tmp & 0xFFFF;
1257
}
1258
#undef SUM
1259

    
1260
static int vsad_intra16_mmx2(void *v, uint8_t * pix, uint8_t * dummy, int line_size, int h) {
1261
    int tmp;
1262

    
1263
    assert( (((int)pix) & 7) == 0);
1264
    assert((line_size &7) ==0);
1265

    
1266
#define SUM(in0, in1, out0, out1) \
1267
      "movq (%0), " #out0 "\n"\
1268
      "movq 8(%0), " #out1 "\n"\
1269
      "add %2,%0\n"\
1270
      "psadbw " #out0 ", " #in0 "\n"\
1271
      "psadbw " #out1 ", " #in1 "\n"\
1272
      "paddw " #in1 ", " #in0 "\n"\
1273
      "paddw " #in0 ", %%mm6\n"
1274

    
1275
  asm volatile (
1276
      "movl %3,%%ecx\n"
1277
      "pxor %%mm6,%%mm6\n"
1278
      "pxor %%mm7,%%mm7\n"
1279
      "movq (%0),%%mm0\n"
1280
      "movq 8(%0),%%mm1\n"
1281
      "add %2,%0\n"
1282
      "subl $2, %%ecx\n"
1283
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1284
      "1:\n"
1285

    
1286
      SUM(%%mm4, %%mm5, %%mm0, %%mm1)
1287

    
1288
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1289

    
1290
      "subl $2, %%ecx\n"
1291
      "jnz 1b\n"
1292

    
1293
      "movd %%mm6,%1\n"
1294
      : "+r" (pix), "=r"(tmp)
1295
      : "r" ((long)line_size) , "m" (h)
1296
      : "%ecx");
1297
    return tmp;
1298
}
1299
#undef SUM
1300

    
1301
static int vsad16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
1302
    int tmp;
1303

    
1304
    assert( (((int)pix1) & 7) == 0);
1305
    assert( (((int)pix2) & 7) == 0);
1306
    assert((line_size &7) ==0);
1307

    
1308
#define SUM(in0, in1, out0, out1) \
1309
      "movq (%0),%%mm2\n"\
1310
      "movq (%1)," #out0 "\n"\
1311
      "movq 8(%0),%%mm3\n"\
1312
      "movq 8(%1)," #out1 "\n"\
1313
      "add %3,%0\n"\
1314
      "add %3,%1\n"\
1315
      "psubb " #out0 ", %%mm2\n"\
1316
      "psubb " #out1 ", %%mm3\n"\
1317
      "pxor %%mm7, %%mm2\n"\
1318
      "pxor %%mm7, %%mm3\n"\
1319
      "movq %%mm2, " #out0 "\n"\
1320
      "movq %%mm3, " #out1 "\n"\
1321
      "psubusb " #in0 ", %%mm2\n"\
1322
      "psubusb " #in1 ", %%mm3\n"\
1323
      "psubusb " #out0 ", " #in0 "\n"\
1324
      "psubusb " #out1 ", " #in1 "\n"\
1325
      "por %%mm2, " #in0 "\n"\
1326
      "por %%mm3, " #in1 "\n"\
1327
      "movq " #in0 ", %%mm2\n"\
1328
      "movq " #in1 ", %%mm3\n"\
1329
      "punpcklbw %%mm7, " #in0 "\n"\
1330
      "punpcklbw %%mm7, " #in1 "\n"\
1331
      "punpckhbw %%mm7, %%mm2\n"\
1332
      "punpckhbw %%mm7, %%mm3\n"\
1333
      "paddw " #in1 ", " #in0 "\n"\
1334
      "paddw %%mm3, %%mm2\n"\
1335
      "paddw %%mm2, " #in0 "\n"\
1336
      "paddw " #in0 ", %%mm6\n"
1337

    
1338

    
1339
  asm volatile (
1340
      "movl %4,%%ecx\n"
1341
      "pxor %%mm6,%%mm6\n"
1342
      "pcmpeqw %%mm7,%%mm7\n"
1343
      "psllw $15, %%mm7\n"
1344
      "packsswb %%mm7, %%mm7\n"
1345
      "movq (%0),%%mm0\n"
1346
      "movq (%1),%%mm2\n"
1347
      "movq 8(%0),%%mm1\n"
1348
      "movq 8(%1),%%mm3\n"
1349
      "add %3,%0\n"
1350
      "add %3,%1\n"
1351
      "subl $2, %%ecx\n"
1352
      "psubb %%mm2, %%mm0\n"
1353
      "psubb %%mm3, %%mm1\n"
1354
      "pxor %%mm7, %%mm0\n"
1355
      "pxor %%mm7, %%mm1\n"
1356
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1357
      "1:\n"
1358

    
1359
      SUM(%%mm4, %%mm5, %%mm0, %%mm1)
1360

    
1361
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1362

    
1363
      "subl $2, %%ecx\n"
1364
      "jnz 1b\n"
1365

    
1366
      "movq %%mm6,%%mm0\n"
1367
      "psrlq $32, %%mm6\n"
1368
      "paddw %%mm6,%%mm0\n"
1369
      "movq %%mm0,%%mm6\n"
1370
      "psrlq $16, %%mm0\n"
1371
      "paddw %%mm6,%%mm0\n"
1372
      "movd %%mm0,%2\n"
1373
      : "+r" (pix1), "+r" (pix2), "=r"(tmp)
1374
      : "r" ((long)line_size) , "m" (h)
1375
      : "%ecx");
1376
    return tmp & 0x7FFF;
1377
}
1378
#undef SUM
1379

    
1380
static int vsad16_mmx2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) {
1381
    int tmp;
1382

    
1383
    assert( (((int)pix1) & 7) == 0);
1384
    assert( (((int)pix2) & 7) == 0);
1385
    assert((line_size &7) ==0);
1386

    
1387
#define SUM(in0, in1, out0, out1) \
1388
      "movq (%0)," #out0 "\n"\
1389
      "movq (%1),%%mm2\n"\
1390
      "movq 8(%0)," #out1 "\n"\
1391
      "movq 8(%1),%%mm3\n"\
1392
      "add %3,%0\n"\
1393
      "add %3,%1\n"\
1394
      "psubb %%mm2, " #out0 "\n"\
1395
      "psubb %%mm3, " #out1 "\n"\
1396
      "pxor %%mm7, " #out0 "\n"\
1397
      "pxor %%mm7, " #out1 "\n"\
1398
      "psadbw " #out0 ", " #in0 "\n"\
1399
      "psadbw " #out1 ", " #in1 "\n"\
1400
      "paddw " #in1 ", " #in0 "\n"\
1401
      "paddw " #in0 ", %%mm6\n"
1402

    
1403
  asm volatile (
1404
      "movl %4,%%ecx\n"
1405
      "pxor %%mm6,%%mm6\n"
1406
      "pcmpeqw %%mm7,%%mm7\n"
1407
      "psllw $15, %%mm7\n"
1408
      "packsswb %%mm7, %%mm7\n"
1409
      "movq (%0),%%mm0\n"
1410
      "movq (%1),%%mm2\n"
1411
      "movq 8(%0),%%mm1\n"
1412
      "movq 8(%1),%%mm3\n"
1413
      "add %3,%0\n"
1414
      "add %3,%1\n"
1415
      "subl $2, %%ecx\n"
1416
      "psubb %%mm2, %%mm0\n"
1417
      "psubb %%mm3, %%mm1\n"
1418
      "pxor %%mm7, %%mm0\n"
1419
      "pxor %%mm7, %%mm1\n"
1420
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1421
      "1:\n"
1422

    
1423
      SUM(%%mm4, %%mm5, %%mm0, %%mm1)
1424

    
1425
      SUM(%%mm0, %%mm1, %%mm4, %%mm5)
1426

    
1427
      "subl $2, %%ecx\n"
1428
      "jnz 1b\n"
1429

    
1430
      "movd %%mm6,%2\n"
1431
      : "+r" (pix1), "+r" (pix2), "=r"(tmp)
1432
      : "r" ((long)line_size) , "m" (h)
1433
      : "%ecx");
1434
    return tmp;
1435
}
1436
#undef SUM
1437

    
1438
static void diff_bytes_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w){
1439
    long i=0;
1440
    asm volatile(
1441
        "1:                             \n\t"
1442
        "movq  (%2, %0), %%mm0          \n\t"
1443
        "movq  (%1, %0), %%mm1          \n\t"
1444
        "psubb %%mm0, %%mm1             \n\t"
1445
        "movq %%mm1, (%3, %0)           \n\t"
1446
        "movq 8(%2, %0), %%mm0          \n\t"
1447
        "movq 8(%1, %0), %%mm1          \n\t"
1448
        "psubb %%mm0, %%mm1             \n\t"
1449
        "movq %%mm1, 8(%3, %0)          \n\t"
1450
        "add $16, %0                    \n\t"
1451
        "cmp %4, %0                     \n\t"
1452
        " jb 1b                         \n\t"
1453
        : "+r" (i)
1454
        : "r"(src1), "r"(src2), "r"(dst), "r"((long)w-15)
1455
    );
1456
    for(; i<w; i++)
1457
        dst[i+0] = src1[i+0]-src2[i+0];
1458
}
1459

    
1460
static void sub_hfyu_median_prediction_mmx2(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w, int *left, int *left_top){
1461
    long i=0;
1462
    uint8_t l, lt;
1463

    
1464
    asm volatile(
1465
        "1:                             \n\t"
1466
        "movq  -1(%1, %0), %%mm0        \n\t" // LT
1467
        "movq  (%1, %0), %%mm1          \n\t" // T
1468
        "movq  -1(%2, %0), %%mm2        \n\t" // L
1469
        "movq  (%2, %0), %%mm3          \n\t" // X
1470
        "movq %%mm2, %%mm4              \n\t" // L
1471
        "psubb %%mm0, %%mm2             \n\t"
1472
        "paddb %%mm1, %%mm2             \n\t" // L + T - LT
1473
        "movq %%mm4, %%mm5              \n\t" // L
1474
        "pmaxub %%mm1, %%mm4            \n\t" // max(T, L)
1475
        "pminub %%mm5, %%mm1            \n\t" // min(T, L)
1476
        "pminub %%mm2, %%mm4            \n\t"
1477
        "pmaxub %%mm1, %%mm4            \n\t"
1478
        "psubb %%mm4, %%mm3             \n\t" // dst - pred
1479
        "movq %%mm3, (%3, %0)           \n\t"
1480
        "add $8, %0                     \n\t"
1481
        "cmp %4, %0                     \n\t"
1482
        " jb 1b                         \n\t"
1483
        : "+r" (i)
1484
        : "r"(src1), "r"(src2), "r"(dst), "r"((long)w)
1485
    );
1486

    
1487
    l= *left;
1488
    lt= *left_top;
1489

    
1490
    dst[0]= src2[0] - mid_pred(l, src1[0], (l + src1[0] - lt)&0xFF);
1491

    
1492
    *left_top= src1[w-1];
1493
    *left    = src2[w-1];
1494
}
1495

    
1496
#define LBUTTERFLY2(a1,b1,a2,b2)\
1497
    "paddw " #b1 ", " #a1 "           \n\t"\
1498
    "paddw " #b2 ", " #a2 "           \n\t"\
1499
    "paddw " #b1 ", " #b1 "           \n\t"\
1500
    "paddw " #b2 ", " #b2 "           \n\t"\
1501
    "psubw " #a1 ", " #b1 "           \n\t"\
1502
    "psubw " #a2 ", " #b2 "           \n\t"
1503

    
1504
#define HADAMARD48\
1505
        LBUTTERFLY2(%%mm0, %%mm1, %%mm2, %%mm3)\
1506
        LBUTTERFLY2(%%mm4, %%mm5, %%mm6, %%mm7)\
1507
        LBUTTERFLY2(%%mm0, %%mm2, %%mm1, %%mm3)\
1508
        LBUTTERFLY2(%%mm4, %%mm6, %%mm5, %%mm7)\
1509
        LBUTTERFLY2(%%mm0, %%mm4, %%mm1, %%mm5)\
1510
        LBUTTERFLY2(%%mm2, %%mm6, %%mm3, %%mm7)\
1511

    
1512
#define MMABS(a,z)\
1513
    "pxor " #z ", " #z "              \n\t"\
1514
    "pcmpgtw " #a ", " #z "           \n\t"\
1515
    "pxor " #z ", " #a "              \n\t"\
1516
    "psubw " #z ", " #a "             \n\t"
1517

    
1518
#define MMABS_SUM(a,z, sum)\
1519
    "pxor " #z ", " #z "              \n\t"\
1520
    "pcmpgtw " #a ", " #z "           \n\t"\
1521
    "pxor " #z ", " #a "              \n\t"\
1522
    "psubw " #z ", " #a "             \n\t"\
1523
    "paddusw " #a ", " #sum "         \n\t"
1524

    
1525
#define MMABS_MMX2(a,z)\
1526
    "pxor " #z ", " #z "              \n\t"\
1527
    "psubw " #a ", " #z "             \n\t"\
1528
    "pmaxsw " #z ", " #a "            \n\t"
1529

    
1530
#define MMABS_SUM_MMX2(a,z, sum)\
1531
    "pxor " #z ", " #z "              \n\t"\
1532
    "psubw " #a ", " #z "             \n\t"\
1533
    "pmaxsw " #z ", " #a "            \n\t"\
1534
    "paddusw " #a ", " #sum "         \n\t"
1535

    
1536
#define TRANSPOSE4(a,b,c,d,t)\
1537
    SBUTTERFLY(a,b,t,wd) /* a=aebf t=cgdh */\
1538
    SBUTTERFLY(c,d,b,wd) /* c=imjn b=kolp */\
1539
    SBUTTERFLY(a,c,d,dq) /* a=aeim d=bfjn */\
1540
    SBUTTERFLY(t,b,c,dq) /* t=cgko c=dhlp */
1541

    
1542
#define LOAD4(o, a, b, c, d)\
1543
        "movq "#o"(%1), " #a "        \n\t"\
1544
        "movq "#o"+16(%1), " #b "     \n\t"\
1545
        "movq "#o"+32(%1), " #c "     \n\t"\
1546
        "movq "#o"+48(%1), " #d "     \n\t"
1547

    
1548
#define STORE4(o, a, b, c, d)\
1549
        "movq "#a", "#o"(%1)          \n\t"\
1550
        "movq "#b", "#o"+16(%1)       \n\t"\
1551
        "movq "#c", "#o"+32(%1)       \n\t"\
1552
        "movq "#d", "#o"+48(%1)       \n\t"\
1553

    
1554
static int hadamard8_diff_mmx(void *s, uint8_t *src1, uint8_t *src2, int stride, int h){
1555
    DECLARE_ALIGNED_8(uint64_t, temp[16]);
1556
    int sum=0;
1557

    
1558
    assert(h==8);
1559

    
1560
    diff_pixels_mmx((DCTELEM*)temp, src1, src2, stride);
1561

    
1562
    asm volatile(
1563
        LOAD4(0 , %%mm0, %%mm1, %%mm2, %%mm3)
1564
        LOAD4(64, %%mm4, %%mm5, %%mm6, %%mm7)
1565

    
1566
        HADAMARD48
1567

    
1568
        "movq %%mm7, 112(%1)            \n\t"
1569

    
1570
        TRANSPOSE4(%%mm0, %%mm1, %%mm2, %%mm3, %%mm7)
1571
        STORE4(0 , %%mm0, %%mm3, %%mm7, %%mm2)
1572

    
1573
        "movq 112(%1), %%mm7            \n\t"
1574
        TRANSPOSE4(%%mm4, %%mm5, %%mm6, %%mm7, %%mm0)
1575
        STORE4(64, %%mm4, %%mm7, %%mm0, %%mm6)
1576

    
1577
        LOAD4(8 , %%mm0, %%mm1, %%mm2, %%mm3)
1578
        LOAD4(72, %%mm4, %%mm5, %%mm6, %%mm7)
1579

    
1580
        HADAMARD48
1581

    
1582
        "movq %%mm7, 120(%1)            \n\t"
1583

    
1584
        TRANSPOSE4(%%mm0, %%mm1, %%mm2, %%mm3, %%mm7)
1585
        STORE4(8 , %%mm0, %%mm3, %%mm7, %%mm2)
1586

    
1587
        "movq 120(%1), %%mm7            \n\t"
1588
        TRANSPOSE4(%%mm4, %%mm5, %%mm6, %%mm7, %%mm0)
1589
        "movq %%mm7, %%mm5              \n\t"//FIXME remove
1590
        "movq %%mm6, %%mm7              \n\t"
1591
        "movq %%mm0, %%mm6              \n\t"
1592
//        STORE4(72, %%mm4, %%mm7, %%mm0, %%mm6) //FIXME remove
1593

    
1594
        LOAD4(64, %%mm0, %%mm1, %%mm2, %%mm3)
1595
//        LOAD4(72, %%mm4, %%mm5, %%mm6, %%mm7)
1596

    
1597
        HADAMARD48
1598
        "movq %%mm7, 64(%1)             \n\t"
1599
        MMABS(%%mm0, %%mm7)
1600
        MMABS_SUM(%%mm1, %%mm7, %%mm0)
1601
        MMABS_SUM(%%mm2, %%mm7, %%mm0)
1602
        MMABS_SUM(%%mm3, %%mm7, %%mm0)
1603
        MMABS_SUM(%%mm4, %%mm7, %%mm0)
1604
        MMABS_SUM(%%mm5, %%mm7, %%mm0)
1605
        MMABS_SUM(%%mm6, %%mm7, %%mm0)
1606
        "movq 64(%1), %%mm1             \n\t"
1607
        MMABS_SUM(%%mm1, %%mm7, %%mm0)
1608
        "movq %%mm0, 64(%1)             \n\t"
1609

    
1610
        LOAD4(0 , %%mm0, %%mm1, %%mm2, %%mm3)
1611
        LOAD4(8 , %%mm4, %%mm5, %%mm6, %%mm7)
1612

    
1613
        HADAMARD48
1614
        "movq %%mm7, (%1)               \n\t"
1615
        MMABS(%%mm0, %%mm7)
1616
        MMABS_SUM(%%mm1, %%mm7, %%mm0)
1617
        MMABS_SUM(%%mm2, %%mm7, %%mm0)
1618
        MMABS_SUM(%%mm3, %%mm7, %%mm0)
1619
        MMABS_SUM(%%mm4, %%mm7, %%mm0)
1620
        MMABS_SUM(%%mm5, %%mm7, %%mm0)
1621
        MMABS_SUM(%%mm6, %%mm7, %%mm0)
1622
        "movq (%1), %%mm1               \n\t"
1623
        MMABS_SUM(%%mm1, %%mm7, %%mm0)
1624
        "movq 64(%1), %%mm1             \n\t"
1625
        MMABS_SUM(%%mm1, %%mm7, %%mm0)
1626

    
1627
        "movq %%mm0, %%mm1              \n\t"
1628
        "psrlq $32, %%mm0               \n\t"
1629
        "paddusw %%mm1, %%mm0           \n\t"
1630
        "movq %%mm0, %%mm1              \n\t"
1631
        "psrlq $16, %%mm0               \n\t"
1632
        "paddusw %%mm1, %%mm0           \n\t"
1633
        "movd %%mm0, %0                 \n\t"
1634

    
1635
        : "=r" (sum)
1636
        : "r"(temp)
1637
    );
1638
    return sum&0xFFFF;
1639
}
1640

    
1641
static int hadamard8_diff_mmx2(void *s, uint8_t *src1, uint8_t *src2, int stride, int h){
1642
    DECLARE_ALIGNED_8(uint64_t, temp[16]);
1643
    int sum=0;
1644

    
1645
    assert(h==8);
1646

    
1647
    diff_pixels_mmx((DCTELEM*)temp, src1, src2, stride);
1648

    
1649
    asm volatile(
1650
        LOAD4(0 , %%mm0, %%mm1, %%mm2, %%mm3)
1651
        LOAD4(64, %%mm4, %%mm5, %%mm6, %%mm7)
1652

    
1653
        HADAMARD48
1654

    
1655
        "movq %%mm7, 112(%1)            \n\t"
1656

    
1657
        TRANSPOSE4(%%mm0, %%mm1, %%mm2, %%mm3, %%mm7)
1658
        STORE4(0 , %%mm0, %%mm3, %%mm7, %%mm2)
1659

    
1660
        "movq 112(%1), %%mm7            \n\t"
1661
        TRANSPOSE4(%%mm4, %%mm5, %%mm6, %%mm7, %%mm0)
1662
        STORE4(64, %%mm4, %%mm7, %%mm0, %%mm6)
1663

    
1664
        LOAD4(8 , %%mm0, %%mm1, %%mm2, %%mm3)
1665
        LOAD4(72, %%mm4, %%mm5, %%mm6, %%mm7)
1666

    
1667
        HADAMARD48
1668

    
1669
        "movq %%mm7, 120(%1)            \n\t"
1670

    
1671
        TRANSPOSE4(%%mm0, %%mm1, %%mm2, %%mm3, %%mm7)
1672
        STORE4(8 , %%mm0, %%mm3, %%mm7, %%mm2)
1673

    
1674
        "movq 120(%1), %%mm7            \n\t"
1675
        TRANSPOSE4(%%mm4, %%mm5, %%mm6, %%mm7, %%mm0)
1676
        "movq %%mm7, %%mm5              \n\t"//FIXME remove
1677
        "movq %%mm6, %%mm7              \n\t"
1678
        "movq %%mm0, %%mm6              \n\t"
1679
//        STORE4(72, %%mm4, %%mm7, %%mm0, %%mm6) //FIXME remove
1680

    
1681
        LOAD4(64, %%mm0, %%mm1, %%mm2, %%mm3)
1682
//        LOAD4(72, %%mm4, %%mm5, %%mm6, %%mm7)
1683

    
1684
        HADAMARD48
1685
        "movq %%mm7, 64(%1)             \n\t"
1686
        MMABS_MMX2(%%mm0, %%mm7)
1687
        MMABS_SUM_MMX2(%%mm1, %%mm7, %%mm0)
1688
        MMABS_SUM_MMX2(%%mm2, %%mm7, %%mm0)
1689
        MMABS_SUM_MMX2(%%mm3, %%mm7, %%mm0)
1690
        MMABS_SUM_MMX2(%%mm4, %%mm7, %%mm0)
1691
        MMABS_SUM_MMX2(%%mm5, %%mm7, %%mm0)
1692
        MMABS_SUM_MMX2(%%mm6, %%mm7, %%mm0)
1693
        "movq 64(%1), %%mm1             \n\t"
1694
        MMABS_SUM_MMX2(%%mm1, %%mm7, %%mm0)
1695
        "movq %%mm0, 64(%1)             \n\t"
1696

    
1697
        LOAD4(0 , %%mm0, %%mm1, %%mm2, %%mm3)
1698
        LOAD4(8 , %%mm4, %%mm5, %%mm6, %%mm7)
1699

    
1700
        HADAMARD48
1701
        "movq %%mm7, (%1)               \n\t"
1702
        MMABS_MMX2(%%mm0, %%mm7)
1703
        MMABS_SUM_MMX2(%%mm1, %%mm7, %%mm0)
1704
        MMABS_SUM_MMX2(%%mm2, %%mm7, %%mm0)
1705
        MMABS_SUM_MMX2(%%mm3, %%mm7, %%mm0)
1706
        MMABS_SUM_MMX2(%%mm4, %%mm7, %%mm0)
1707
        MMABS_SUM_MMX2(%%mm5, %%mm7, %%mm0)
1708
        MMABS_SUM_MMX2(%%mm6, %%mm7, %%mm0)
1709
        "movq (%1), %%mm1               \n\t"
1710
        MMABS_SUM_MMX2(%%mm1, %%mm7, %%mm0)
1711
        "movq 64(%1), %%mm1             \n\t"
1712
        MMABS_SUM_MMX2(%%mm1, %%mm7, %%mm0)
1713

    
1714
        "pshufw $0x0E, %%mm0, %%mm1     \n\t"
1715
        "paddusw %%mm1, %%mm0           \n\t"
1716
        "pshufw $0x01, %%mm0, %%mm1     \n\t"
1717
        "paddusw %%mm1, %%mm0           \n\t"
1718
        "movd %%mm0, %0                 \n\t"
1719

    
1720
        : "=r" (sum)
1721
        : "r"(temp)
1722
    );
1723
    return sum&0xFFFF;
1724
}
1725

    
1726

    
1727
WARPER8_16_SQ(hadamard8_diff_mmx, hadamard8_diff16_mmx)
1728
WARPER8_16_SQ(hadamard8_diff_mmx2, hadamard8_diff16_mmx2)
1729
#endif //CONFIG_ENCODERS
1730

    
1731
#define put_no_rnd_pixels8_mmx(a,b,c,d) put_pixels8_mmx(a,b,c,d)
1732
#define put_no_rnd_pixels16_mmx(a,b,c,d) put_pixels16_mmx(a,b,c,d)
1733

    
1734
#define QPEL_V_LOW(m3,m4,m5,m6, pw_20, pw_3, rnd, in0, in1, in2, in7, out, OP)\
1735
        "paddw " #m4 ", " #m3 "           \n\t" /* x1 */\
1736
        "movq "MANGLE(ff_pw_20)", %%mm4   \n\t" /* 20 */\
1737
        "pmullw " #m3 ", %%mm4            \n\t" /* 20x1 */\
1738
        "movq "#in7", " #m3 "             \n\t" /* d */\
1739
        "movq "#in0", %%mm5               \n\t" /* D */\
1740
        "paddw " #m3 ", %%mm5             \n\t" /* x4 */\
1741
        "psubw %%mm5, %%mm4               \n\t" /* 20x1 - x4 */\
1742
        "movq "#in1", %%mm5               \n\t" /* C */\
1743
        "movq "#in2", %%mm6               \n\t" /* B */\
1744
        "paddw " #m6 ", %%mm5             \n\t" /* x3 */\
1745
        "paddw " #m5 ", %%mm6             \n\t" /* x2 */\
1746
        "paddw %%mm6, %%mm6               \n\t" /* 2x2 */\
1747
        "psubw %%mm6, %%mm5               \n\t" /* -2x2 + x3 */\
1748
        "pmullw "MANGLE(ff_pw_3)", %%mm5  \n\t" /* -6x2 + 3x3 */\
1749
        "paddw " #rnd ", %%mm4            \n\t" /* x2 */\
1750
        "paddw %%mm4, %%mm5               \n\t" /* 20x1 - 6x2 + 3x3 - x4 */\
1751
        "psraw $5, %%mm5                  \n\t"\
1752
        "packuswb %%mm5, %%mm5            \n\t"\
1753
        OP(%%mm5, out, %%mm7, d)
1754

    
1755
#define QPEL_BASE(OPNAME, ROUNDER, RND, OP_MMX2, OP_3DNOW)\
1756
static void OPNAME ## mpeg4_qpel16_h_lowpass_mmx2(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1757
    uint64_t temp;\
1758
\
1759
    asm volatile(\
1760
        "pxor %%mm7, %%mm7                \n\t"\
1761
        "1:                               \n\t"\
1762
        "movq  (%0), %%mm0                \n\t" /* ABCDEFGH */\
1763
        "movq %%mm0, %%mm1                \n\t" /* ABCDEFGH */\
1764
        "movq %%mm0, %%mm2                \n\t" /* ABCDEFGH */\
1765
        "punpcklbw %%mm7, %%mm0           \n\t" /* 0A0B0C0D */\
1766
        "punpckhbw %%mm7, %%mm1           \n\t" /* 0E0F0G0H */\
1767
        "pshufw $0x90, %%mm0, %%mm5       \n\t" /* 0A0A0B0C */\
1768
        "pshufw $0x41, %%mm0, %%mm6       \n\t" /* 0B0A0A0B */\
1769
        "movq %%mm2, %%mm3                \n\t" /* ABCDEFGH */\
1770
        "movq %%mm2, %%mm4                \n\t" /* ABCDEFGH */\
1771
        "psllq $8, %%mm2                  \n\t" /* 0ABCDEFG */\
1772
        "psllq $16, %%mm3                 \n\t" /* 00ABCDEF */\
1773
        "psllq $24, %%mm4                 \n\t" /* 000ABCDE */\
1774
        "punpckhbw %%mm7, %%mm2           \n\t" /* 0D0E0F0G */\
1775
        "punpckhbw %%mm7, %%mm3           \n\t" /* 0C0D0E0F */\
1776
        "punpckhbw %%mm7, %%mm4           \n\t" /* 0B0C0D0E */\
1777
        "paddw %%mm3, %%mm5               \n\t" /* b */\
1778
        "paddw %%mm2, %%mm6               \n\t" /* c */\
1779
        "paddw %%mm5, %%mm5               \n\t" /* 2b */\
1780
        "psubw %%mm5, %%mm6               \n\t" /* c - 2b */\
1781
        "pshufw $0x06, %%mm0, %%mm5       \n\t" /* 0C0B0A0A */\
1782
        "pmullw "MANGLE(ff_pw_3)", %%mm6  \n\t" /* 3c - 6b */\
1783
        "paddw %%mm4, %%mm0               \n\t" /* a */\
1784
        "paddw %%mm1, %%mm5               \n\t" /* d */\
1785
        "pmullw "MANGLE(ff_pw_20)", %%mm0 \n\t" /* 20a */\
1786
        "psubw %%mm5, %%mm0               \n\t" /* 20a - d */\
1787
        "paddw %6, %%mm6                  \n\t"\
1788
        "paddw %%mm6, %%mm0               \n\t" /* 20a - 6b + 3c - d */\
1789
        "psraw $5, %%mm0                  \n\t"\
1790
        "movq %%mm0, %5                   \n\t"\
1791
        /* mm1=EFGH, mm2=DEFG, mm3=CDEF, mm4=BCDE, mm7=0 */\
1792
        \
1793
        "movq 5(%0), %%mm0                \n\t" /* FGHIJKLM */\
1794
        "movq %%mm0, %%mm5                \n\t" /* FGHIJKLM */\
1795
        "movq %%mm0, %%mm6                \n\t" /* FGHIJKLM */\
1796
        "psrlq $8, %%mm0                  \n\t" /* GHIJKLM0 */\
1797
        "psrlq $16, %%mm5                 \n\t" /* HIJKLM00 */\
1798
        "punpcklbw %%mm7, %%mm0           \n\t" /* 0G0H0I0J */\
1799
        "punpcklbw %%mm7, %%mm5           \n\t" /* 0H0I0J0K */\
1800
        "paddw %%mm0, %%mm2               \n\t" /* b */\
1801
        "paddw %%mm5, %%mm3               \n\t" /* c */\
1802
        "paddw %%mm2, %%mm2               \n\t" /* 2b */\
1803
        "psubw %%mm2, %%mm3               \n\t" /* c - 2b */\
1804
        "movq %%mm6, %%mm2                \n\t" /* FGHIJKLM */\
1805
        "psrlq $24, %%mm6                 \n\t" /* IJKLM000 */\
1806
        "punpcklbw %%mm7, %%mm2           \n\t" /* 0F0G0H0I */\
1807
        "punpcklbw %%mm7, %%mm6           \n\t" /* 0I0J0K0L */\
1808
        "pmullw "MANGLE(ff_pw_3)", %%mm3  \n\t" /* 3c - 6b */\
1809
        "paddw %%mm2, %%mm1               \n\t" /* a */\
1810
        "paddw %%mm6, %%mm4               \n\t" /* d */\
1811
        "pmullw "MANGLE(ff_pw_20)", %%mm1 \n\t" /* 20a */\
1812
        "psubw %%mm4, %%mm3               \n\t" /* - 6b +3c - d */\
1813
        "paddw %6, %%mm1                  \n\t"\
1814
        "paddw %%mm1, %%mm3               \n\t" /* 20a - 6b +3c - d */\
1815
        "psraw $5, %%mm3                  \n\t"\
1816
        "movq %5, %%mm1                   \n\t"\
1817
        "packuswb %%mm3, %%mm1            \n\t"\
1818
        OP_MMX2(%%mm1, (%1),%%mm4, q)\
1819
        /* mm0= GHIJ, mm2=FGHI, mm5=HIJK, mm6=IJKL, mm7=0 */\
1820
        \
1821
        "movq 9(%0), %%mm1                \n\t" /* JKLMNOPQ */\
1822
        "movq %%mm1, %%mm4                \n\t" /* JKLMNOPQ */\
1823
        "movq %%mm1, %%mm3                \n\t" /* JKLMNOPQ */\
1824
        "psrlq $8, %%mm1                  \n\t" /* KLMNOPQ0 */\
1825
        "psrlq $16, %%mm4                 \n\t" /* LMNOPQ00 */\
1826
        "punpcklbw %%mm7, %%mm1           \n\t" /* 0K0L0M0N */\
1827
        "punpcklbw %%mm7, %%mm4           \n\t" /* 0L0M0N0O */\
1828
        "paddw %%mm1, %%mm5               \n\t" /* b */\
1829
        "paddw %%mm4, %%mm0               \n\t" /* c */\
1830
        "paddw %%mm5, %%mm5               \n\t" /* 2b */\
1831
        "psubw %%mm5, %%mm0               \n\t" /* c - 2b */\
1832
        "movq %%mm3, %%mm5                \n\t" /* JKLMNOPQ */\
1833
        "psrlq $24, %%mm3                 \n\t" /* MNOPQ000 */\
1834
        "pmullw "MANGLE(ff_pw_3)", %%mm0  \n\t" /* 3c - 6b */\
1835
        "punpcklbw %%mm7, %%mm3           \n\t" /* 0M0N0O0P */\
1836
        "paddw %%mm3, %%mm2               \n\t" /* d */\
1837
        "psubw %%mm2, %%mm0               \n\t" /* -6b + 3c - d */\
1838
        "movq %%mm5, %%mm2                \n\t" /* JKLMNOPQ */\
1839
        "punpcklbw %%mm7, %%mm2           \n\t" /* 0J0K0L0M */\
1840
        "punpckhbw %%mm7, %%mm5           \n\t" /* 0N0O0P0Q */\
1841
        "paddw %%mm2, %%mm6               \n\t" /* a */\
1842
        "pmullw "MANGLE(ff_pw_20)", %%mm6 \n\t" /* 20a */\
1843
        "paddw %6, %%mm0                  \n\t"\
1844
        "paddw %%mm6, %%mm0               \n\t" /* 20a - 6b + 3c - d */\
1845
        "psraw $5, %%mm0                  \n\t"\
1846
        /* mm1=KLMN, mm2=JKLM, mm3=MNOP, mm4=LMNO, mm5=NOPQ mm7=0 */\
1847
        \
1848
        "paddw %%mm5, %%mm3               \n\t" /* a */\
1849
        "pshufw $0xF9, %%mm5, %%mm6       \n\t" /* 0O0P0Q0Q */\
1850
        "paddw %%mm4, %%mm6               \n\t" /* b */\
1851
        "pshufw $0xBE, %%mm5, %%mm4       \n\t" /* 0P0Q0Q0P */\
1852
        "pshufw $0x6F, %%mm5, %%mm5       \n\t" /* 0Q0Q0P0O */\
1853
        "paddw %%mm1, %%mm4               \n\t" /* c */\
1854
        "paddw %%mm2, %%mm5               \n\t" /* d */\
1855
        "paddw %%mm6, %%mm6               \n\t" /* 2b */\
1856
        "psubw %%mm6, %%mm4               \n\t" /* c - 2b */\
1857
        "pmullw "MANGLE(ff_pw_20)", %%mm3 \n\t" /* 20a */\
1858
        "pmullw "MANGLE(ff_pw_3)", %%mm4  \n\t" /* 3c - 6b */\
1859
        "psubw %%mm5, %%mm3               \n\t" /* -6b + 3c - d */\
1860
        "paddw %6, %%mm4                  \n\t"\
1861
        "paddw %%mm3, %%mm4               \n\t" /* 20a - 6b + 3c - d */\
1862
        "psraw $5, %%mm4                  \n\t"\
1863
        "packuswb %%mm4, %%mm0            \n\t"\
1864
        OP_MMX2(%%mm0, 8(%1), %%mm4, q)\
1865
        \
1866
        "add %3, %0                       \n\t"\
1867
        "add %4, %1                       \n\t"\
1868
        "decl %2                          \n\t"\
1869
        " jnz 1b                          \n\t"\
1870
        : "+a"(src), "+c"(dst), "+m"(h)\
1871
        : "d"((long)srcStride), "S"((long)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(temp), "m"(ROUNDER)\
1872
        : "memory"\
1873
    );\
1874
}\
1875
\
1876
static void OPNAME ## mpeg4_qpel16_h_lowpass_3dnow(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1877
    int i;\
1878
    int16_t temp[16];\
1879
    /* quick HACK, XXX FIXME MUST be optimized */\
1880
    for(i=0; i<h; i++)\
1881
    {\
1882
        temp[ 0]= (src[ 0]+src[ 1])*20 - (src[ 0]+src[ 2])*6 + (src[ 1]+src[ 3])*3 - (src[ 2]+src[ 4]);\
1883
        temp[ 1]= (src[ 1]+src[ 2])*20 - (src[ 0]+src[ 3])*6 + (src[ 0]+src[ 4])*3 - (src[ 1]+src[ 5]);\
1884
        temp[ 2]= (src[ 2]+src[ 3])*20 - (src[ 1]+src[ 4])*6 + (src[ 0]+src[ 5])*3 - (src[ 0]+src[ 6]);\
1885
        temp[ 3]= (src[ 3]+src[ 4])*20 - (src[ 2]+src[ 5])*6 + (src[ 1]+src[ 6])*3 - (src[ 0]+src[ 7]);\
1886
        temp[ 4]= (src[ 4]+src[ 5])*20 - (src[ 3]+src[ 6])*6 + (src[ 2]+src[ 7])*3 - (src[ 1]+src[ 8]);\
1887
        temp[ 5]= (src[ 5]+src[ 6])*20 - (src[ 4]+src[ 7])*6 + (src[ 3]+src[ 8])*3 - (src[ 2]+src[ 9]);\
1888
        temp[ 6]= (src[ 6]+src[ 7])*20 - (src[ 5]+src[ 8])*6 + (src[ 4]+src[ 9])*3 - (src[ 3]+src[10]);\
1889
        temp[ 7]= (src[ 7]+src[ 8])*20 - (src[ 6]+src[ 9])*6 + (src[ 5]+src[10])*3 - (src[ 4]+src[11]);\
1890
        temp[ 8]= (src[ 8]+src[ 9])*20 - (src[ 7]+src[10])*6 + (src[ 6]+src[11])*3 - (src[ 5]+src[12]);\
1891
        temp[ 9]= (src[ 9]+src[10])*20 - (src[ 8]+src[11])*6 + (src[ 7]+src[12])*3 - (src[ 6]+src[13]);\
1892
        temp[10]= (src[10]+src[11])*20 - (src[ 9]+src[12])*6 + (src[ 8]+src[13])*3 - (src[ 7]+src[14]);\
1893
        temp[11]= (src[11]+src[12])*20 - (src[10]+src[13])*6 + (src[ 9]+src[14])*3 - (src[ 8]+src[15]);\
1894
        temp[12]= (src[12]+src[13])*20 - (src[11]+src[14])*6 + (src[10]+src[15])*3 - (src[ 9]+src[16]);\
1895
        temp[13]= (src[13]+src[14])*20 - (src[12]+src[15])*6 + (src[11]+src[16])*3 - (src[10]+src[16]);\
1896
        temp[14]= (src[14]+src[15])*20 - (src[13]+src[16])*6 + (src[12]+src[16])*3 - (src[11]+src[15]);\
1897
        temp[15]= (src[15]+src[16])*20 - (src[14]+src[16])*6 + (src[13]+src[15])*3 - (src[12]+src[14]);\
1898
        asm volatile(\
1899
            "movq (%0), %%mm0               \n\t"\
1900
            "movq 8(%0), %%mm1              \n\t"\
1901
            "paddw %2, %%mm0                \n\t"\
1902
            "paddw %2, %%mm1                \n\t"\
1903
            "psraw $5, %%mm0                \n\t"\
1904
            "psraw $5, %%mm1                \n\t"\
1905
            "packuswb %%mm1, %%mm0          \n\t"\
1906
            OP_3DNOW(%%mm0, (%1), %%mm1, q)\
1907
            "movq 16(%0), %%mm0             \n\t"\
1908
            "movq 24(%0), %%mm1             \n\t"\
1909
            "paddw %2, %%mm0                \n\t"\
1910
            "paddw %2, %%mm1                \n\t"\
1911
            "psraw $5, %%mm0                \n\t"\
1912
            "psraw $5, %%mm1                \n\t"\
1913
            "packuswb %%mm1, %%mm0          \n\t"\
1914
            OP_3DNOW(%%mm0, 8(%1), %%mm1, q)\
1915
            :: "r"(temp), "r"(dst), "m"(ROUNDER)\
1916
            : "memory"\
1917
        );\
1918
        dst+=dstStride;\
1919
        src+=srcStride;\
1920
    }\
1921
}\
1922
\
1923
static void OPNAME ## mpeg4_qpel8_h_lowpass_mmx2(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1924
    uint64_t temp;\
1925
\
1926
    asm volatile(\
1927
        "pxor %%mm7, %%mm7                \n\t"\
1928
        "1:                               \n\t"\
1929
        "movq  (%0), %%mm0                \n\t" /* ABCDEFGH */\
1930
        "movq %%mm0, %%mm1                \n\t" /* ABCDEFGH */\
1931
        "movq %%mm0, %%mm2                \n\t" /* ABCDEFGH */\
1932
        "punpcklbw %%mm7, %%mm0           \n\t" /* 0A0B0C0D */\
1933
        "punpckhbw %%mm7, %%mm1           \n\t" /* 0E0F0G0H */\
1934
        "pshufw $0x90, %%mm0, %%mm5       \n\t" /* 0A0A0B0C */\
1935
        "pshufw $0x41, %%mm0, %%mm6       \n\t" /* 0B0A0A0B */\
1936
        "movq %%mm2, %%mm3                \n\t" /* ABCDEFGH */\
1937
        "movq %%mm2, %%mm4                \n\t" /* ABCDEFGH */\
1938
        "psllq $8, %%mm2                  \n\t" /* 0ABCDEFG */\
1939
        "psllq $16, %%mm3                 \n\t" /* 00ABCDEF */\
1940
        "psllq $24, %%mm4                 \n\t" /* 000ABCDE */\
1941
        "punpckhbw %%mm7, %%mm2           \n\t" /* 0D0E0F0G */\
1942
        "punpckhbw %%mm7, %%mm3           \n\t" /* 0C0D0E0F */\
1943
        "punpckhbw %%mm7, %%mm4           \n\t" /* 0B0C0D0E */\
1944
        "paddw %%mm3, %%mm5               \n\t" /* b */\
1945
        "paddw %%mm2, %%mm6               \n\t" /* c */\
1946
        "paddw %%mm5, %%mm5               \n\t" /* 2b */\
1947
        "psubw %%mm5, %%mm6               \n\t" /* c - 2b */\
1948
        "pshufw $0x06, %%mm0, %%mm5       \n\t" /* 0C0B0A0A */\
1949
        "pmullw "MANGLE(ff_pw_3)", %%mm6  \n\t" /* 3c - 6b */\
1950
        "paddw %%mm4, %%mm0               \n\t" /* a */\
1951
        "paddw %%mm1, %%mm5               \n\t" /* d */\
1952
        "pmullw "MANGLE(ff_pw_20)", %%mm0 \n\t" /* 20a */\
1953
        "psubw %%mm5, %%mm0               \n\t" /* 20a - d */\
1954
        "paddw %6, %%mm6                  \n\t"\
1955
        "paddw %%mm6, %%mm0               \n\t" /* 20a - 6b + 3c - d */\
1956
        "psraw $5, %%mm0                  \n\t"\
1957
        /* mm1=EFGH, mm2=DEFG, mm3=CDEF, mm4=BCDE, mm7=0 */\
1958
        \
1959
        "movd 5(%0), %%mm5                \n\t" /* FGHI */\
1960
        "punpcklbw %%mm7, %%mm5           \n\t" /* 0F0G0H0I */\
1961
        "pshufw $0xF9, %%mm5, %%mm6       \n\t" /* 0G0H0I0I */\
1962
        "paddw %%mm5, %%mm1               \n\t" /* a */\
1963
        "paddw %%mm6, %%mm2               \n\t" /* b */\
1964
        "pshufw $0xBE, %%mm5, %%mm6       \n\t" /* 0H0I0I0H */\
1965
        "pshufw $0x6F, %%mm5, %%mm5       \n\t" /* 0I0I0H0G */\
1966
        "paddw %%mm6, %%mm3               \n\t" /* c */\
1967
        "paddw %%mm5, %%mm4               \n\t" /* d */\
1968
        "paddw %%mm2, %%mm2               \n\t" /* 2b */\
1969
        "psubw %%mm2, %%mm3               \n\t" /* c - 2b */\
1970
        "pmullw "MANGLE(ff_pw_20)", %%mm1 \n\t" /* 20a */\
1971
        "pmullw "MANGLE(ff_pw_3)", %%mm3  \n\t" /* 3c - 6b */\
1972
        "psubw %%mm4, %%mm3               \n\t" /* -6b + 3c - d */\
1973
        "paddw %6, %%mm1                  \n\t"\
1974
        "paddw %%mm1, %%mm3               \n\t" /* 20a - 6b + 3c - d */\
1975
        "psraw $5, %%mm3                  \n\t"\
1976
        "packuswb %%mm3, %%mm0            \n\t"\
1977
        OP_MMX2(%%mm0, (%1), %%mm4, q)\
1978
        \
1979
        "add %3, %0                       \n\t"\
1980
        "add %4, %1                       \n\t"\
1981
        "decl %2                          \n\t"\
1982
        " jnz 1b                          \n\t"\
1983
        : "+a"(src), "+c"(dst), "+m"(h)\
1984
        : "S"((long)srcStride), "D"((long)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(temp), "m"(ROUNDER)\
1985
        : "memory"\
1986
    );\
1987
}\
1988
\
1989
static void OPNAME ## mpeg4_qpel8_h_lowpass_3dnow(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
1990
    int i;\
1991
    int16_t temp[8];\
1992
    /* quick HACK, XXX FIXME MUST be optimized */\
1993
    for(i=0; i<h; i++)\
1994
    {\
1995
        temp[ 0]= (src[ 0]+src[ 1])*20 - (src[ 0]+src[ 2])*6 + (src[ 1]+src[ 3])*3 - (src[ 2]+src[ 4]);\
1996
        temp[ 1]= (src[ 1]+src[ 2])*20 - (src[ 0]+src[ 3])*6 + (src[ 0]+src[ 4])*3 - (src[ 1]+src[ 5]);\
1997
        temp[ 2]= (src[ 2]+src[ 3])*20 - (src[ 1]+src[ 4])*6 + (src[ 0]+src[ 5])*3 - (src[ 0]+src[ 6]);\
1998
        temp[ 3]= (src[ 3]+src[ 4])*20 - (src[ 2]+src[ 5])*6 + (src[ 1]+src[ 6])*3 - (src[ 0]+src[ 7]);\
1999
        temp[ 4]= (src[ 4]+src[ 5])*20 - (src[ 3]+src[ 6])*6 + (src[ 2]+src[ 7])*3 - (src[ 1]+src[ 8]);\
2000
        temp[ 5]= (src[ 5]+src[ 6])*20 - (src[ 4]+src[ 7])*6 + (src[ 3]+src[ 8])*3 - (src[ 2]+src[ 8]);\
2001
        temp[ 6]= (src[ 6]+src[ 7])*20 - (src[ 5]+src[ 8])*6 + (src[ 4]+src[ 8])*3 - (src[ 3]+src[ 7]);\
2002
        temp[ 7]= (src[ 7]+src[ 8])*20 - (src[ 6]+src[ 8])*6 + (src[ 5]+src[ 7])*3 - (src[ 4]+src[ 6]);\
2003
        asm volatile(\
2004
            "movq (%0), %%mm0           \n\t"\
2005
            "movq 8(%0), %%mm1          \n\t"\
2006
            "paddw %2, %%mm0            \n\t"\
2007
            "paddw %2, %%mm1            \n\t"\
2008
            "psraw $5, %%mm0            \n\t"\
2009
            "psraw $5, %%mm1            \n\t"\
2010
            "packuswb %%mm1, %%mm0      \n\t"\
2011
            OP_3DNOW(%%mm0, (%1), %%mm1, q)\
2012
            :: "r"(temp), "r"(dst), "m"(ROUNDER)\
2013
            :"memory"\
2014
        );\
2015
        dst+=dstStride;\
2016
        src+=srcStride;\
2017
    }\
2018
}
2019

    
2020
#define QPEL_OP(OPNAME, ROUNDER, RND, OP, MMX)\
2021
\
2022
static void OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
2023
    uint64_t temp[17*4];\
2024
    uint64_t *temp_ptr= temp;\
2025
    int count= 17;\
2026
\
2027
    /*FIXME unroll */\
2028
    asm volatile(\
2029
        "pxor %%mm7, %%mm7              \n\t"\
2030
        "1:                             \n\t"\
2031
        "movq (%0), %%mm0               \n\t"\
2032
        "movq (%0), %%mm1               \n\t"\
2033
        "movq 8(%0), %%mm2              \n\t"\
2034
        "movq 8(%0), %%mm3              \n\t"\
2035
        "punpcklbw %%mm7, %%mm0         \n\t"\
2036
        "punpckhbw %%mm7, %%mm1         \n\t"\
2037
        "punpcklbw %%mm7, %%mm2         \n\t"\
2038
        "punpckhbw %%mm7, %%mm3         \n\t"\
2039
        "movq %%mm0, (%1)               \n\t"\
2040
        "movq %%mm1, 17*8(%1)           \n\t"\
2041
        "movq %%mm2, 2*17*8(%1)         \n\t"\
2042
        "movq %%mm3, 3*17*8(%1)         \n\t"\
2043
        "add $8, %1                     \n\t"\
2044
        "add %3, %0                     \n\t"\
2045
        "decl %2                        \n\t"\
2046
        " jnz 1b                        \n\t"\
2047
        : "+r" (src), "+r" (temp_ptr), "+r"(count)\
2048
        : "r" ((long)srcStride)\
2049
        : "memory"\
2050
    );\
2051
    \
2052
    temp_ptr= temp;\
2053
    count=4;\
2054
    \
2055
/*FIXME reorder for speed */\
2056
    asm volatile(\
2057
        /*"pxor %%mm7, %%mm7              \n\t"*/\
2058
        "1:                             \n\t"\
2059
        "movq (%0), %%mm0               \n\t"\
2060
        "movq 8(%0), %%mm1              \n\t"\
2061
        "movq 16(%0), %%mm2             \n\t"\
2062
        "movq 24(%0), %%mm3             \n\t"\
2063
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 16(%0),  8(%0),   (%0), 32(%0), (%1), OP)\
2064
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5,  8(%0),   (%0),   (%0), 40(%0), (%1, %3), OP)\
2065
        "add %4, %1                     \n\t"\
2066
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5,   (%0),   (%0),  8(%0), 48(%0), (%1), OP)\
2067
        \
2068
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5,   (%0),  8(%0), 16(%0), 56(%0), (%1, %3), OP)\
2069
        "add %4, %1                     \n\t"\
2070
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5,  8(%0), 16(%0), 24(%0), 64(%0), (%1), OP)\
2071
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 16(%0), 24(%0), 32(%0), 72(%0), (%1, %3), OP)\
2072
        "add %4, %1                     \n\t"\
2073
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 24(%0), 32(%0), 40(%0), 80(%0), (%1), OP)\
2074
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 32(%0), 40(%0), 48(%0), 88(%0), (%1, %3), OP)\
2075
        "add %4, %1                     \n\t"\
2076
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 40(%0), 48(%0), 56(%0), 96(%0), (%1), OP)\
2077
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 48(%0), 56(%0), 64(%0),104(%0), (%1, %3), OP)\
2078
        "add %4, %1                     \n\t"\
2079
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 56(%0), 64(%0), 72(%0),112(%0), (%1), OP)\
2080
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 64(%0), 72(%0), 80(%0),120(%0), (%1, %3), OP)\
2081
        "add %4, %1                     \n\t"\
2082
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 72(%0), 80(%0), 88(%0),128(%0), (%1), OP)\
2083
        \
2084
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 80(%0), 88(%0), 96(%0),128(%0), (%1, %3), OP)\
2085
        "add %4, %1                     \n\t"  \
2086
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 88(%0), 96(%0),104(%0),120(%0), (%1), OP)\
2087
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 96(%0),104(%0),112(%0),112(%0), (%1, %3), OP)\
2088
        \
2089
        "add $136, %0                   \n\t"\
2090
        "add %6, %1                     \n\t"\
2091
        "decl %2                        \n\t"\
2092
        " jnz 1b                        \n\t"\
2093
        \
2094
        : "+r"(temp_ptr), "+r"(dst), "+g"(count)\
2095
        : "r"((long)dstStride), "r"(2*(long)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(ROUNDER), "g"(4-14*(long)dstStride)\
2096
        :"memory"\
2097
    );\
2098
}\
2099
\
2100
static void OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
2101
    uint64_t temp[9*2];\
2102
    uint64_t *temp_ptr= temp;\
2103
    int count= 9;\
2104
\
2105
    /*FIXME unroll */\
2106
    asm volatile(\
2107
        "pxor %%mm7, %%mm7              \n\t"\
2108
        "1:                             \n\t"\
2109
        "movq (%0), %%mm0               \n\t"\
2110
        "movq (%0), %%mm1               \n\t"\
2111
        "punpcklbw %%mm7, %%mm0         \n\t"\
2112
        "punpckhbw %%mm7, %%mm1         \n\t"\
2113
        "movq %%mm0, (%1)               \n\t"\
2114
        "movq %%mm1, 9*8(%1)            \n\t"\
2115
        "add $8, %1                     \n\t"\
2116
        "add %3, %0                     \n\t"\
2117
        "decl %2                        \n\t"\
2118
        " jnz 1b                        \n\t"\
2119
        : "+r" (src), "+r" (temp_ptr), "+r"(count)\
2120
        : "r" ((long)srcStride)\
2121
        : "memory"\
2122
    );\
2123
    \
2124
    temp_ptr= temp;\
2125
    count=2;\
2126
    \
2127
/*FIXME reorder for speed */\
2128
    asm volatile(\
2129
        /*"pxor %%mm7, %%mm7              \n\t"*/\
2130
        "1:                             \n\t"\
2131
        "movq (%0), %%mm0               \n\t"\
2132
        "movq 8(%0), %%mm1              \n\t"\
2133
        "movq 16(%0), %%mm2             \n\t"\
2134
        "movq 24(%0), %%mm3             \n\t"\
2135
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5, 16(%0),  8(%0),   (%0), 32(%0), (%1), OP)\
2136
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5,  8(%0),   (%0),   (%0), 40(%0), (%1, %3), OP)\
2137
        "add %4, %1                     \n\t"\
2138
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5,   (%0),   (%0),  8(%0), 48(%0), (%1), OP)\
2139
        \
2140
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5,   (%0),  8(%0), 16(%0), 56(%0), (%1, %3), OP)\
2141
        "add %4, %1                     \n\t"\
2142
        QPEL_V_LOW(%%mm0, %%mm1, %%mm2, %%mm3, %5, %6, %5,  8(%0), 16(%0), 24(%0), 64(%0), (%1), OP)\
2143
        \
2144
        QPEL_V_LOW(%%mm1, %%mm2, %%mm3, %%mm0, %5, %6, %5, 16(%0), 24(%0), 32(%0), 64(%0), (%1, %3), OP)\
2145
        "add %4, %1                     \n\t"\
2146
        QPEL_V_LOW(%%mm2, %%mm3, %%mm0, %%mm1, %5, %6, %5, 24(%0), 32(%0), 40(%0), 56(%0), (%1), OP)\
2147
        QPEL_V_LOW(%%mm3, %%mm0, %%mm1, %%mm2, %5, %6, %5, 32(%0), 40(%0), 48(%0), 48(%0), (%1, %3), OP)\
2148
                \
2149
        "add $72, %0                    \n\t"\
2150
        "add %6, %1                     \n\t"\
2151
        "decl %2                        \n\t"\
2152
        " jnz 1b                        \n\t"\
2153
         \
2154
        : "+r"(temp_ptr), "+r"(dst), "+g"(count)\
2155
        : "r"((long)dstStride), "r"(2*(long)dstStride), /*"m"(ff_pw_20), "m"(ff_pw_3),*/ "m"(ROUNDER), "g"(4-6*(long)dstStride)\
2156
        : "memory"\
2157
   );\
2158
}\
2159
\
2160
static void OPNAME ## qpel8_mc00_ ## MMX (uint8_t *dst, uint8_t *src, int stride){\
2161
    OPNAME ## pixels8_mmx(dst, src, stride, 8);\
2162
}\
2163
\
2164
static void OPNAME ## qpel8_mc10_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2165
    uint64_t temp[8];\
2166
    uint8_t * const half= (uint8_t*)temp;\
2167
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8, stride, 8);\
2168
    OPNAME ## pixels8_l2_ ## MMX(dst, src, half, stride, stride, 8);\
2169
}\
2170
\
2171
static void OPNAME ## qpel8_mc20_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2172
    OPNAME ## mpeg4_qpel8_h_lowpass_ ## MMX(dst, src, stride, stride, 8);\
2173
}\
2174
\
2175
static void OPNAME ## qpel8_mc30_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2176
    uint64_t temp[8];\
2177
    uint8_t * const half= (uint8_t*)temp;\
2178
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(half, src, 8, stride, 8);\
2179
    OPNAME ## pixels8_l2_ ## MMX(dst, src+1, half, stride, stride, 8);\
2180
}\
2181
\
2182
static void OPNAME ## qpel8_mc01_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2183
    uint64_t temp[8];\
2184
    uint8_t * const half= (uint8_t*)temp;\
2185
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, 8, stride);\
2186
    OPNAME ## pixels8_l2_ ## MMX(dst, src, half, stride, stride, 8);\
2187
}\
2188
\
2189
static void OPNAME ## qpel8_mc02_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2190
    OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, src, stride, stride);\
2191
}\
2192
\
2193
static void OPNAME ## qpel8_mc03_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2194
    uint64_t temp[8];\
2195
    uint8_t * const half= (uint8_t*)temp;\
2196
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(half, src, 8, stride);\
2197
    OPNAME ## pixels8_l2_ ## MMX(dst, src+stride, half, stride, stride, 8);\
2198
}\
2199
static void OPNAME ## qpel8_mc11_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2200
    uint64_t half[8 + 9];\
2201
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2202
    uint8_t * const halfHV= ((uint8_t*)half);\
2203
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2204
    put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);\
2205
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2206
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);\
2207
}\
2208
static void OPNAME ## qpel8_mc31_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2209
    uint64_t half[8 + 9];\
2210
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2211
    uint8_t * const halfHV= ((uint8_t*)half);\
2212
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2213
    put ## RND ## pixels8_l2_ ## MMX(halfH, src+1, halfH, 8, stride, 9);\
2214
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2215
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);\
2216
}\
2217
static void OPNAME ## qpel8_mc13_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2218
    uint64_t half[8 + 9];\
2219
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2220
    uint8_t * const halfHV= ((uint8_t*)half);\
2221
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2222
    put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);\
2223
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2224
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH+8, halfHV, stride, 8, 8);\
2225
}\
2226
static void OPNAME ## qpel8_mc33_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2227
    uint64_t half[8 + 9];\
2228
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2229
    uint8_t * const halfHV= ((uint8_t*)half);\
2230
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2231
    put ## RND ## pixels8_l2_ ## MMX(halfH, src+1, halfH, 8, stride, 9);\
2232
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2233
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH+8, halfHV, stride, 8, 8);\
2234
}\
2235
static void OPNAME ## qpel8_mc21_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2236
    uint64_t half[8 + 9];\
2237
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2238
    uint8_t * const halfHV= ((uint8_t*)half);\
2239
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2240
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2241
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH, halfHV, stride, 8, 8);\
2242
}\
2243
static void OPNAME ## qpel8_mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2244
    uint64_t half[8 + 9];\
2245
    uint8_t * const halfH= ((uint8_t*)half) + 64;\
2246
    uint8_t * const halfHV= ((uint8_t*)half);\
2247
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2248
    put ## RND ## mpeg4_qpel8_v_lowpass_ ## MMX(halfHV, halfH, 8, 8);\
2249
    OPNAME ## pixels8_l2_ ## MMX(dst, halfH+8, halfHV, stride, 8, 8);\
2250
}\
2251
static void OPNAME ## qpel8_mc12_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2252
    uint64_t half[8 + 9];\
2253
    uint8_t * const halfH= ((uint8_t*)half);\
2254
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2255
    put ## RND ## pixels8_l2_ ## MMX(halfH, src, halfH, 8, stride, 9);\
2256
    OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);\
2257
}\
2258
static void OPNAME ## qpel8_mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2259
    uint64_t half[8 + 9];\
2260
    uint8_t * const halfH= ((uint8_t*)half);\
2261
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2262
    put ## RND ## pixels8_l2_ ## MMX(halfH, src+1, halfH, 8, stride, 9);\
2263
    OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);\
2264
}\
2265
static void OPNAME ## qpel8_mc22_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2266
    uint64_t half[9];\
2267
    uint8_t * const halfH= ((uint8_t*)half);\
2268
    put ## RND ## mpeg4_qpel8_h_lowpass_ ## MMX(halfH, src, 8, stride, 9);\
2269
    OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, halfH, stride, 8);\
2270
}\
2271
static void OPNAME ## qpel16_mc00_ ## MMX (uint8_t *dst, uint8_t *src, int stride){\
2272
    OPNAME ## pixels16_mmx(dst, src, stride, 16);\
2273
}\
2274
\
2275
static void OPNAME ## qpel16_mc10_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2276
    uint64_t temp[32];\
2277
    uint8_t * const half= (uint8_t*)temp;\
2278
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16, stride, 16);\
2279
    OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, stride, 16);\
2280
}\
2281
\
2282
static void OPNAME ## qpel16_mc20_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2283
    OPNAME ## mpeg4_qpel16_h_lowpass_ ## MMX(dst, src, stride, stride, 16);\
2284
}\
2285
\
2286
static void OPNAME ## qpel16_mc30_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2287
    uint64_t temp[32];\
2288
    uint8_t * const half= (uint8_t*)temp;\
2289
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(half, src, 16, stride, 16);\
2290
    OPNAME ## pixels16_l2_ ## MMX(dst, src+1, half, stride, stride, 16);\
2291
}\
2292
\
2293
static void OPNAME ## qpel16_mc01_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2294
    uint64_t temp[32];\
2295
    uint8_t * const half= (uint8_t*)temp;\
2296
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16, stride);\
2297
    OPNAME ## pixels16_l2_ ## MMX(dst, src, half, stride, stride, 16);\
2298
}\
2299
\
2300
static void OPNAME ## qpel16_mc02_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2301
    OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, src, stride, stride);\
2302
}\
2303
\
2304
static void OPNAME ## qpel16_mc03_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2305
    uint64_t temp[32];\
2306
    uint8_t * const half= (uint8_t*)temp;\
2307
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(half, src, 16, stride);\
2308
    OPNAME ## pixels16_l2_ ## MMX(dst, src+stride, half, stride, stride, 16);\
2309
}\
2310
static void OPNAME ## qpel16_mc11_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2311
    uint64_t half[16*2 + 17*2];\
2312
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2313
    uint8_t * const halfHV= ((uint8_t*)half);\
2314
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2315
    put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, stride, 17);\
2316
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2317
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);\
2318
}\
2319
static void OPNAME ## qpel16_mc31_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2320
    uint64_t half[16*2 + 17*2];\
2321
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2322
    uint8_t * const halfHV= ((uint8_t*)half);\
2323
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2324
    put ## RND ## pixels16_l2_ ## MMX(halfH, src+1, halfH, 16, stride, 17);\
2325
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2326
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);\
2327
}\
2328
static void OPNAME ## qpel16_mc13_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2329
    uint64_t half[16*2 + 17*2];\
2330
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2331
    uint8_t * const halfHV= ((uint8_t*)half);\
2332
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2333
    put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, stride, 17);\
2334
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2335
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH+16, halfHV, stride, 16, 16);\
2336
}\
2337
static void OPNAME ## qpel16_mc33_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2338
    uint64_t half[16*2 + 17*2];\
2339
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2340
    uint8_t * const halfHV= ((uint8_t*)half);\
2341
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2342
    put ## RND ## pixels16_l2_ ## MMX(halfH, src+1, halfH, 16, stride, 17);\
2343
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2344
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH+16, halfHV, stride, 16, 16);\
2345
}\
2346
static void OPNAME ## qpel16_mc21_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2347
    uint64_t half[16*2 + 17*2];\
2348
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2349
    uint8_t * const halfHV= ((uint8_t*)half);\
2350
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2351
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2352
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH, halfHV, stride, 16, 16);\
2353
}\
2354
static void OPNAME ## qpel16_mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2355
    uint64_t half[16*2 + 17*2];\
2356
    uint8_t * const halfH= ((uint8_t*)half) + 256;\
2357
    uint8_t * const halfHV= ((uint8_t*)half);\
2358
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2359
    put ## RND ## mpeg4_qpel16_v_lowpass_ ## MMX(halfHV, halfH, 16, 16);\
2360
    OPNAME ## pixels16_l2_ ## MMX(dst, halfH+16, halfHV, stride, 16, 16);\
2361
}\
2362
static void OPNAME ## qpel16_mc12_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2363
    uint64_t half[17*2];\
2364
    uint8_t * const halfH= ((uint8_t*)half);\
2365
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2366
    put ## RND ## pixels16_l2_ ## MMX(halfH, src, halfH, 16, stride, 17);\
2367
    OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);\
2368
}\
2369
static void OPNAME ## qpel16_mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2370
    uint64_t half[17*2];\
2371
    uint8_t * const halfH= ((uint8_t*)half);\
2372
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2373
    put ## RND ## pixels16_l2_ ## MMX(halfH, src+1, halfH, 16, stride, 17);\
2374
    OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);\
2375
}\
2376
static void OPNAME ## qpel16_mc22_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
2377
    uint64_t half[17*2];\
2378
    uint8_t * const halfH= ((uint8_t*)half);\
2379
    put ## RND ## mpeg4_qpel16_h_lowpass_ ## MMX(halfH, src, 16, stride, 17);\
2380
    OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, halfH, stride, 16);\
2381
}
2382

    
2383
#define PUT_OP(a,b,temp, size) "mov" #size " " #a ", " #b "        \n\t"
2384
#define AVG_3DNOW_OP(a,b,temp, size) \
2385
"mov" #size " " #b ", " #temp "   \n\t"\
2386
"pavgusb " #temp ", " #a "        \n\t"\
2387
"mov" #size " " #a ", " #b "      \n\t"
2388
#define AVG_MMX2_OP(a,b,temp, size) \
2389
"mov" #size " " #b ", " #temp "   \n\t"\
2390
"pavgb " #temp ", " #a "          \n\t"\
2391
"mov" #size " " #a ", " #b "      \n\t"
2392

    
2393
QPEL_BASE(put_       , ff_pw_16, _       , PUT_OP, PUT_OP)
2394
QPEL_BASE(avg_       , ff_pw_16, _       , AVG_MMX2_OP, AVG_3DNOW_OP)
2395
QPEL_BASE(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP, PUT_OP)
2396
QPEL_OP(put_       , ff_pw_16, _       , PUT_OP, 3dnow)
2397
QPEL_OP(avg_       , ff_pw_16, _       , AVG_3DNOW_OP, 3dnow)
2398
QPEL_OP(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP, 3dnow)
2399
QPEL_OP(put_       , ff_pw_16, _       , PUT_OP, mmx2)
2400
QPEL_OP(avg_       , ff_pw_16, _       , AVG_MMX2_OP, mmx2)
2401
QPEL_OP(put_no_rnd_, ff_pw_15, _no_rnd_, PUT_OP, mmx2)
2402

    
2403
#if 0
2404
static void just_return() { return; }
2405
#endif
2406

    
2407
#define SET_QPEL_FUNC(postfix1, postfix2) \
2408
    c->put_ ## postfix1 = put_ ## postfix2;\
2409
    c->put_no_rnd_ ## postfix1 = put_no_rnd_ ## postfix2;\
2410
    c->avg_ ## postfix1 = avg_ ## postfix2;
2411

    
2412
static void gmc_mmx(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
2413
                    int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height){
2414
    const int w = 8;
2415
    const int ix = ox>>(16+shift);
2416
    const int iy = oy>>(16+shift);
2417
    const int oxs = ox>>4;
2418
    const int oys = oy>>4;
2419
    const int dxxs = dxx>>4;
2420
    const int dxys = dxy>>4;
2421
    const int dyxs = dyx>>4;
2422
    const int dyys = dyy>>4;
2423
    const uint16_t r4[4] = {r,r,r,r};
2424
    const uint16_t dxy4[4] = {dxys,dxys,dxys,dxys};
2425
    const uint16_t dyy4[4] = {dyys,dyys,dyys,dyys};
2426
    const uint64_t shift2 = 2*shift;
2427
    uint8_t edge_buf[(h+1)*stride];
2428
    int x, y;
2429

    
2430
    const int dxw = (dxx-(1<<(16+shift)))*(w-1);
2431
    const int dyh = (dyy-(1<<(16+shift)))*(h-1);
2432
    const int dxh = dxy*(h-1);
2433
    const int dyw = dyx*(w-1);
2434
    if( // non-constant fullpel offset (3% of blocks)
2435
        (ox^(ox+dxw) | ox^(ox+dxh) | ox^(ox+dxw+dxh) |
2436
         oy^(oy+dyw) | oy^(oy+dyh) | oy^(oy+dyw+dyh)) >> (16+shift)
2437
        // uses more than 16 bits of subpel mv (only at huge resolution)
2438
        || (dxx|dxy|dyx|dyy)&15 )
2439
    {
2440
        //FIXME could still use mmx for some of the rows
2441
        ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy, shift, r, width, height);
2442
        return;
2443
    }
2444

    
2445
    src += ix + iy*stride;
2446
    if( (unsigned)ix >= width-w ||
2447
        (unsigned)iy >= height-h )
2448
    {
2449
        ff_emulated_edge_mc(edge_buf, src, stride, w+1, h+1, ix, iy, width, height);
2450
        src = edge_buf;
2451
    }
2452

    
2453
    asm volatile(
2454
        "movd         %0, %%mm6 \n\t"
2455
        "pxor      %%mm7, %%mm7 \n\t"
2456
        "punpcklwd %%mm6, %%mm6 \n\t"
2457
        "punpcklwd %%mm6, %%mm6 \n\t"
2458
        :: "r"(1<<shift)
2459
    );
2460

    
2461
    for(x=0; x<w; x+=4){
2462
        uint16_t dx4[4] = { oxs - dxys + dxxs*(x+0),
2463
                            oxs - dxys + dxxs*(x+1),
2464
                            oxs - dxys + dxxs*(x+2),
2465
                            oxs - dxys + dxxs*(x+3) };
2466
        uint16_t dy4[4] = { oys - dyys + dyxs*(x+0),
2467
                            oys - dyys + dyxs*(x+1),
2468
                            oys - dyys + dyxs*(x+2),
2469
                            oys - dyys + dyxs*(x+3) };
2470

    
2471
        for(y=0; y<h; y++){
2472
            asm volatile(
2473
                "movq   %0,  %%mm4 \n\t"
2474
                "movq   %1,  %%mm5 \n\t"
2475
                "paddw  %2,  %%mm4 \n\t"
2476
                "paddw  %3,  %%mm5 \n\t"
2477
                "movq   %%mm4, %0  \n\t"
2478
                "movq   %%mm5, %1  \n\t"
2479
                "psrlw  $12, %%mm4 \n\t"
2480
                "psrlw  $12, %%mm5 \n\t"
2481
                : "+m"(*dx4), "+m"(*dy4)
2482
                : "m"(*dxy4), "m"(*dyy4)
2483
            );
2484

    
2485
            asm volatile(
2486
                "movq   %%mm6, %%mm2 \n\t"
2487
                "movq   %%mm6, %%mm1 \n\t"
2488
                "psubw  %%mm4, %%mm2 \n\t"
2489
                "psubw  %%mm5, %%mm1 \n\t"
2490
                "movq   %%mm2, %%mm0 \n\t"
2491
                "movq   %%mm4, %%mm3 \n\t"
2492
                "pmullw %%mm1, %%mm0 \n\t" // (s-dx)*(s-dy)
2493
                "pmullw %%mm5, %%mm3 \n\t" // dx*dy
2494
                "pmullw %%mm5, %%mm2 \n\t" // (s-dx)*dy
2495
                "pmullw %%mm4, %%mm1 \n\t" // dx*(s-dy)
2496

    
2497
                "movd   %4,    %%mm5 \n\t"
2498
                "movd   %3,    %%mm4 \n\t"
2499
                "punpcklbw %%mm7, %%mm5 \n\t"
2500
                "punpcklbw %%mm7, %%mm4 \n\t"
2501
                "pmullw %%mm5, %%mm3 \n\t" // src[1,1] * dx*dy
2502
                "pmullw %%mm4, %%mm2 \n\t" // src[0,1] * (s-dx)*dy
2503

    
2504
                "movd   %2,    %%mm5 \n\t"
2505
                "movd   %1,    %%mm4 \n\t"
2506
                "punpcklbw %%mm7, %%mm5 \n\t"
2507
                "punpcklbw %%mm7, %%mm4 \n\t"
2508
                "pmullw %%mm5, %%mm1 \n\t" // src[1,0] * dx*(s-dy)
2509
                "pmullw %%mm4, %%mm0 \n\t" // src[0,0] * (s-dx)*(s-dy)
2510
                "paddw  %5,    %%mm1 \n\t"
2511
                "paddw  %%mm3, %%mm2 \n\t"
2512
                "paddw  %%mm1, %%mm0 \n\t"
2513
                "paddw  %%mm2, %%mm0 \n\t"
2514

    
2515
                "psrlw    %6,    %%mm0 \n\t"
2516
                "packuswb %%mm0, %%mm0 \n\t"
2517
                "movd     %%mm0, %0    \n\t"
2518

    
2519
                : "=m"(dst[x+y*stride])
2520
                : "m"(src[0]), "m"(src[1]),
2521
                  "m"(src[stride]), "m"(src[stride+1]),
2522
                  "m"(*r4), "m"(shift2)
2523
            );
2524
            src += stride;
2525
        }
2526
        src += 4-h*stride;
2527
    }
2528
}
2529

    
2530
static int try_8x8basis_mmx(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale){
2531
    long i=0;
2532

    
2533
    assert(ABS(scale) < 256);
2534
    scale<<= 16 + 1 - BASIS_SHIFT + RECON_SHIFT;
2535

    
2536
    asm volatile(
2537
        "pcmpeqw %%mm6, %%mm6           \n\t" // -1w
2538
        "psrlw $15, %%mm6               \n\t" //  1w
2539
        "pxor %%mm7, %%mm7              \n\t"
2540
        "movd  %4, %%mm5                \n\t"
2541
        "punpcklwd %%mm5, %%mm5         \n\t"
2542
        "punpcklwd %%mm5, %%mm5         \n\t"
2543
        "1:                             \n\t"
2544
        "movq  (%1, %0), %%mm0          \n\t"
2545
        "movq  8(%1, %0), %%mm1         \n\t"
2546
        "pmulhw %%mm5, %%mm0            \n\t"
2547
        "pmulhw %%mm5, %%mm1            \n\t"
2548
        "paddw %%mm6, %%mm0             \n\t"
2549
        "paddw %%mm6, %%mm1             \n\t"
2550
        "psraw $1, %%mm0                \n\t"
2551
        "psraw $1, %%mm1                \n\t"
2552
        "paddw (%2, %0), %%mm0          \n\t"
2553
        "paddw 8(%2, %0), %%mm1         \n\t"
2554
        "psraw $6, %%mm0                \n\t"
2555
        "psraw $6, %%mm1                \n\t"
2556
        "pmullw (%3, %0), %%mm0         \n\t"
2557
        "pmullw 8(%3, %0), %%mm1        \n\t"
2558
        "pmaddwd %%mm0, %%mm0           \n\t"
2559
        "pmaddwd %%mm1, %%mm1           \n\t"
2560
        "paddd %%mm1, %%mm0             \n\t"
2561
        "psrld $4, %%mm0                \n\t"
2562
        "paddd %%mm0, %%mm7             \n\t"
2563
        "add $16, %0                    \n\t"
2564
        "cmp $128, %0                   \n\t" //FIXME optimize & bench
2565
        " jb 1b                         \n\t"
2566
        "movq %%mm7, %%mm6              \n\t"
2567
        "psrlq $32, %%mm7               \n\t"
2568
        "paddd %%mm6, %%mm7             \n\t"
2569
        "psrld $2, %%mm7                \n\t"
2570
        "movd %%mm7, %0                 \n\t"
2571

    
2572
        : "+r" (i)
2573
        : "r"(basis), "r"(rem), "r"(weight), "g"(scale)
2574
    );
2575
    return i;
2576
}
2577

    
2578
static void add_8x8basis_mmx(int16_t rem[64], int16_t basis[64], int scale){
2579
    long i=0;
2580

    
2581
    if(ABS(scale) < 256){
2582
        scale<<= 16 + 1 - BASIS_SHIFT + RECON_SHIFT;
2583
        asm volatile(
2584
                "pcmpeqw %%mm6, %%mm6   \n\t" // -1w
2585
                "psrlw $15, %%mm6       \n\t" //  1w
2586
                "movd  %3, %%mm5        \n\t"
2587
                "punpcklwd %%mm5, %%mm5 \n\t"
2588
                "punpcklwd %%mm5, %%mm5 \n\t"
2589
                "1:                     \n\t"
2590
                "movq  (%1, %0), %%mm0  \n\t"
2591
                "movq  8(%1, %0), %%mm1 \n\t"
2592
                "pmulhw %%mm5, %%mm0    \n\t"
2593
                "pmulhw %%mm5, %%mm1    \n\t"
2594
                "paddw %%mm6, %%mm0     \n\t"
2595
                "paddw %%mm6, %%mm1     \n\t"
2596
                "psraw $1, %%mm0        \n\t"
2597
                "psraw $1, %%mm1        \n\t"
2598
                "paddw (%2, %0), %%mm0  \n\t"
2599
                "paddw 8(%2, %0), %%mm1 \n\t"
2600
                "movq %%mm0, (%2, %0)   \n\t"
2601
                "movq %%mm1, 8(%2, %0)  \n\t"
2602
                "add $16, %0            \n\t"
2603
                "cmp $128, %0           \n\t" //FIXME optimize & bench
2604
                " jb 1b                 \n\t"
2605

    
2606
                : "+r" (i)
2607
                : "r"(basis), "r"(rem), "g"(scale)
2608
        );
2609
    }else{
2610
        for(i=0; i<8*8; i++){
2611
            rem[i] += (basis[i]*scale + (1<<(BASIS_SHIFT - RECON_SHIFT-1)))>>(BASIS_SHIFT - RECON_SHIFT);
2612
        }
2613
    }
2614
}
2615

    
2616
#define PREFETCH(name, op) \
2617
void name(void *mem, int stride, int h){\
2618
    const uint8_t *p= mem;\
2619
    do{\
2620
        asm volatile(#op" %0" :: "m"(*p));\
2621
        p+= stride;\
2622
    }while(--h);\
2623
}
2624
PREFETCH(prefetch_mmx2,  prefetcht0)
2625
PREFETCH(prefetch_3dnow, prefetch)
2626
#undef PREFETCH
2627

    
2628
#include "h264dsp_mmx.c"
2629

    
2630
/* AVS specific */
2631
void ff_cavsdsp_init_mmx2(DSPContext* c, AVCodecContext *avctx);
2632

    
2633
void ff_put_cavs_qpel8_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
2634
    put_pixels8_mmx(dst, src, stride, 8);
2635
}
2636
void ff_avg_cavs_qpel8_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
2637
    avg_pixels8_mmx(dst, src, stride, 8);
2638
}
2639
void ff_put_cavs_qpel16_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
2640
    put_pixels16_mmx(dst, src, stride, 16);
2641
}
2642
void ff_avg_cavs_qpel16_mc00_mmx2(uint8_t *dst, uint8_t *src, int stride) {
2643
    avg_pixels16_mmx(dst, src, stride, 16);
2644
}
2645

    
2646
/* external functions, from idct_mmx.c */
2647
void ff_mmx_idct(DCTELEM *block);
2648
void ff_mmxext_idct(DCTELEM *block);
2649

    
2650
void ff_vp3_idct_sse2(int16_t *input_data);
2651
void ff_vp3_idct_mmx(int16_t *data);
2652
void ff_vp3_dsp_init_mmx(void);
2653

    
2654
/* XXX: those functions should be suppressed ASAP when all IDCTs are
2655
   converted */
2656
static void ff_libmpeg2mmx_idct_put(uint8_t *dest, int line_size, DCTELEM *block)
2657
{
2658
    ff_mmx_idct (block);
2659
    put_pixels_clamped_mmx(block, dest, line_size);
2660
}
2661
static void ff_libmpeg2mmx_idct_add(uint8_t *dest, int line_size, DCTELEM *block)
2662
{
2663
    ff_mmx_idct (block);
2664
    add_pixels_clamped_mmx(block, dest, line_size);
2665
}
2666
static void ff_libmpeg2mmx2_idct_put(uint8_t *dest, int line_size, DCTELEM *block)
2667
{
2668
    ff_mmxext_idct (block);
2669
    put_pixels_clamped_mmx(block, dest, line_size);
2670
}
2671
static void ff_libmpeg2mmx2_idct_add(uint8_t *dest, int line_size, DCTELEM *block)
2672
{
2673
    ff_mmxext_idct (block);
2674
    add_pixels_clamped_mmx(block, dest, line_size);
2675
}
2676
static void ff_vp3_idct_put_sse2(uint8_t *dest, int line_size, DCTELEM *block)
2677
{
2678
    ff_vp3_idct_sse2(block);
2679
    put_signed_pixels_clamped_mmx(block, dest, line_size);
2680
}
2681
static void ff_vp3_idct_add_sse2(uint8_t *dest, int line_size, DCTELEM *block)
2682
{
2683
    ff_vp3_idct_sse2(block);
2684
    add_pixels_clamped_mmx(block, dest, line_size);
2685
}
2686
static void ff_vp3_idct_put_mmx(uint8_t *dest, int line_size, DCTELEM *block)
2687
{
2688
    ff_vp3_idct_mmx(block);
2689
    put_signed_pixels_clamped_mmx(block, dest, line_size);
2690
}
2691
static void ff_vp3_idct_add_mmx(uint8_t *dest, int line_size, DCTELEM *block)
2692
{
2693
    ff_vp3_idct_mmx(block);
2694
    add_pixels_clamped_mmx(block, dest, line_size);
2695
}
2696
#ifdef CONFIG_GPL
2697
static void ff_idct_xvid_mmx_put(uint8_t *dest, int line_size, DCTELEM *block)
2698
{
2699
    ff_idct_xvid_mmx (block);
2700
    put_pixels_clamped_mmx(block, dest, line_size);
2701
}
2702
static void ff_idct_xvid_mmx_add(uint8_t *dest, int line_size, DCTELEM *block)
2703
{
2704
    ff_idct_xvid_mmx (block);
2705
    add_pixels_clamped_mmx(block, dest, line_size);
2706
}
2707
static void ff_idct_xvid_mmx2_put(uint8_t *dest, int line_size, DCTELEM *block)
2708
{
2709
    ff_idct_xvid_mmx2 (block);
2710
    put_pixels_clamped_mmx(block, dest, line_size);
2711
}
2712
static void ff_idct_xvid_mmx2_add(uint8_t *dest, int line_size, DCTELEM *block)
2713
{
2714
    ff_idct_xvid_mmx2 (block);
2715
    add_pixels_clamped_mmx(block, dest, line_size);
2716
}
2717
#endif
2718

    
2719
static void vorbis_inverse_coupling_3dnow(float *mag, float *ang, int blocksize)
2720
{
2721
    int i;
2722
    asm volatile("pxor %%mm7, %%mm7":);
2723
    for(i=0; i<blocksize; i+=2) {
2724
        asm volatile(
2725
            "movq    %0,    %%mm0 \n\t"
2726
            "movq    %1,    %%mm1 \n\t"
2727
            "movq    %%mm0, %%mm2 \n\t"
2728
            "movq    %%mm1, %%mm3 \n\t"
2729
            "pfcmpge %%mm7, %%mm2 \n\t" // m <= 0.0
2730
            "pfcmpge %%mm7, %%mm3 \n\t" // a <= 0.0
2731
            "pslld   $31,   %%mm2 \n\t" // keep only the sign bit
2732
            "pxor    %%mm2, %%mm1 \n\t"
2733
            "movq    %%mm3, %%mm4 \n\t"
2734
            "pand    %%mm1, %%mm3 \n\t"
2735
            "pandn   %%mm1, %%mm4 \n\t"
2736
            "pfadd   %%mm0, %%mm3 \n\t" // a = m + ((a<0) & (a ^ sign(m)))
2737
            "pfsub   %%mm4, %%mm0 \n\t" // m = m + ((a>0) & (a ^ sign(m)))
2738
            "movq    %%mm3, %1    \n\t"
2739
            "movq    %%mm0, %0    \n\t"
2740
            :"+m"(mag[i]), "+m"(ang[i])
2741
            ::"memory"
2742
        );
2743
    }
2744
    asm volatile("femms");
2745
}
2746
static void vorbis_inverse_coupling_sse(float *mag, float *ang, int blocksize)
2747
{
2748
    int i;
2749

    
2750
    asm volatile(
2751
            "movaps  %0,     %%xmm5 \n\t"
2752
        ::"m"(ff_pdw_80000000[0])
2753
    );
2754
    for(i=0; i<blocksize; i+=4) {
2755
        asm volatile(
2756
            "movaps  %0,     %%xmm0 \n\t"
2757
            "movaps  %1,     %%xmm1 \n\t"
2758
            "xorps   %%xmm2, %%xmm2 \n\t"
2759
            "xorps   %%xmm3, %%xmm3 \n\t"
2760
            "cmpleps %%xmm0, %%xmm2 \n\t" // m <= 0.0
2761
            "cmpleps %%xmm1, %%xmm3 \n\t" // a <= 0.0
2762
            "andps   %%xmm5, %%xmm2 \n\t" // keep only the sign bit
2763
            "xorps   %%xmm2, %%xmm1 \n\t"
2764
            "movaps  %%xmm3, %%xmm4 \n\t"
2765
            "andps   %%xmm1, %%xmm3 \n\t"
2766
            "andnps  %%xmm1, %%xmm4 \n\t"
2767
            "addps   %%xmm0, %%xmm3 \n\t" // a = m + ((a<0) & (a ^ sign(m)))
2768
            "subps   %%xmm4, %%xmm0 \n\t" // m = m + ((a>0) & (a ^ sign(m)))
2769
            "movaps  %%xmm3, %1     \n\t"
2770
            "movaps  %%xmm0, %0     \n\t"
2771
            :"+m"(mag[i]), "+m"(ang[i])
2772
            ::"memory"
2773
        );
2774
    }
2775
}
2776

    
2777
static void vector_fmul_3dnow(float *dst, const float *src, int len){
2778
    long i = (len-4)*4;
2779
    asm volatile(
2780
        "1: \n\t"
2781
        "movq    (%1,%0), %%mm0 \n\t"
2782
        "movq   8(%1,%0), %%mm1 \n\t"
2783
        "pfmul   (%2,%0), %%mm0 \n\t"
2784
        "pfmul  8(%2,%0), %%mm1 \n\t"
2785
        "movq   %%mm0,  (%1,%0) \n\t"
2786
        "movq   %%mm1, 8(%1,%0) \n\t"
2787
        "sub  $16, %0 \n\t"
2788
        "jge 1b \n\t"
2789
        "femms  \n\t"
2790
        :"+r"(i)
2791
        :"r"(dst), "r"(src)
2792
        :"memory"
2793
    );
2794
}
2795
static void vector_fmul_sse(float *dst, const float *src, int len){
2796
    long i = (len-8)*4;
2797
    asm volatile(
2798
        "1: \n\t"
2799
        "movaps    (%1,%0), %%xmm0 \n\t"
2800
        "movaps  16(%1,%0), %%xmm1 \n\t"
2801
        "mulps     (%2,%0), %%xmm0 \n\t"
2802
        "mulps   16(%2,%0), %%xmm1 \n\t"
2803
        "movaps  %%xmm0,   (%1,%0) \n\t"
2804
        "movaps  %%xmm1, 16(%1,%0) \n\t"
2805
        "sub  $32, %0 \n\t"
2806
        "jge 1b \n\t"
2807
        :"+r"(i)
2808
        :"r"(dst), "r"(src)
2809
        :"memory"
2810
    );
2811
}
2812

    
2813
static void vector_fmul_reverse_3dnow2(float *dst, const float *src0, const float *src1, int len){
2814
    long i = len*4-16;
2815
    asm volatile(
2816
        "1: \n\t"
2817
        "pswapd   8(%1), %%mm0 \n\t"
2818
        "pswapd    (%1), %%mm1 \n\t"
2819
        "pfmul  (%3,%0), %%mm0 \n\t"
2820
        "pfmul 8(%3,%0), %%mm1 \n\t"
2821
        "movq  %%mm0,  (%2,%0) \n\t"
2822
        "movq  %%mm1, 8(%2,%0) \n\t"
2823
        "add   $16, %1 \n\t"
2824
        "sub   $16, %0 \n\t"
2825
        "jge   1b \n\t"
2826
        :"+r"(i), "+r"(src1)
2827
        :"r"(dst), "r"(src0)
2828
    );
2829
    asm volatile("femms");
2830
}
2831
static void vector_fmul_reverse_sse(float *dst, const float *src0, const float *src1, int len){
2832
    long i = len*4-32;
2833
    asm volatile(
2834
        "1: \n\t"
2835
        "movaps        16(%1), %%xmm0 \n\t"
2836
        "movaps          (%1), %%xmm1 \n\t"
2837
        "shufps $0x1b, %%xmm0, %%xmm0 \n\t"
2838
        "shufps $0x1b, %%xmm1, %%xmm1 \n\t"
2839
        "mulps        (%3,%0), %%xmm0 \n\t"
2840
        "mulps      16(%3,%0), %%xmm1 \n\t"
2841
        "movaps     %%xmm0,   (%2,%0) \n\t"
2842
        "movaps     %%xmm1, 16(%2,%0) \n\t"
2843
        "add    $32, %1 \n\t"
2844
        "sub    $32, %0 \n\t"
2845
        "jge    1b \n\t"
2846
        :"+r"(i), "+r"(src1)
2847
        :"r"(dst), "r"(src0)
2848
    );
2849
}
2850

    
2851
static void vector_fmul_add_add_3dnow(float *dst, const float *src0, const float *src1,
2852
                                      const float *src2, int src3, int len, int step){
2853
    long i = (len-4)*4;
2854
    if(step == 2 && src3 == 0){
2855
        dst += (len-4)*2;
2856
        asm volatile(
2857
            "1: \n\t"
2858
            "movq   (%2,%0),  %%mm0 \n\t"
2859
            "movq  8(%2,%0),  %%mm1 \n\t"
2860
            "pfmul  (%3,%0),  %%mm0 \n\t"
2861
            "pfmul 8(%3,%0),  %%mm1 \n\t"
2862
            "pfadd  (%4,%0),  %%mm0 \n\t"
2863
            "pfadd 8(%4,%0),  %%mm1 \n\t"
2864
            "movd     %%mm0,   (%1) \n\t"
2865
            "movd     %%mm1, 16(%1) \n\t"
2866
            "psrlq      $32,  %%mm0 \n\t"
2867
            "psrlq      $32,  %%mm1 \n\t"
2868
            "movd     %%mm0,  8(%1) \n\t"
2869
            "movd     %%mm1, 24(%1) \n\t"
2870
            "sub  $32, %1 \n\t"
2871
            "sub  $16, %0 \n\t"
2872
            "jge  1b \n\t"
2873
            :"+r"(i), "+r"(dst)
2874
            :"r"(src0), "r"(src1), "r"(src2)
2875
            :"memory"
2876
        );
2877
    }
2878
    else if(step == 1 && src3 == 0){
2879
        asm volatile(
2880
            "1: \n\t"
2881
            "movq    (%2,%0), %%mm0 \n\t"
2882
            "movq   8(%2,%0), %%mm1 \n\t"
2883
            "pfmul   (%3,%0), %%mm0 \n\t"
2884
            "pfmul  8(%3,%0), %%mm1 \n\t"
2885
            "pfadd   (%4,%0), %%mm0 \n\t"
2886
            "pfadd  8(%4,%0), %%mm1 \n\t"
2887
            "movq  %%mm0,   (%1,%0) \n\t"
2888
            "movq  %%mm1,  8(%1,%0) \n\t"
2889
            "sub  $16, %0 \n\t"
2890
            "jge  1b \n\t"
2891
            :"+r"(i)
2892
            :"r"(dst), "r"(src0), "r"(src1), "r"(src2)
2893
            :"memory"
2894
        );
2895
    }
2896
    else
2897
        ff_vector_fmul_add_add_c(dst, src0, src1, src2, src3, len, step);
2898
    asm volatile("femms");
2899
}
2900
static void vector_fmul_add_add_sse(float *dst, const float *src0, const float *src1,
2901
                                    const float *src2, int src3, int len, int step){
2902
    long i = (len-8)*4;
2903
    if(step == 2 && src3 == 0){
2904
        dst += (len-8)*2;
2905
        asm volatile(
2906
            "1: \n\t"
2907
            "movaps   (%2,%0), %%xmm0 \n\t"
2908
            "movaps 16(%2,%0), %%xmm1 \n\t"
2909
            "mulps    (%3,%0), %%xmm0 \n\t"
2910
            "mulps  16(%3,%0), %%xmm1 \n\t"
2911
            "addps    (%4,%0), %%xmm0 \n\t"
2912
            "addps  16(%4,%0), %%xmm1 \n\t"
2913
            "movss     %%xmm0,   (%1) \n\t"
2914
            "movss     %%xmm1, 32(%1) \n\t"
2915
            "movhlps   %%xmm0, %%xmm2 \n\t"
2916
            "movhlps   %%xmm1, %%xmm3 \n\t"
2917
            "movss     %%xmm2, 16(%1) \n\t"
2918
            "movss     %%xmm3, 48(%1) \n\t"
2919
            "shufps $0xb1, %%xmm0, %%xmm0 \n\t"
2920
            "shufps $0xb1, %%xmm1, %%xmm1 \n\t"
2921
            "movss     %%xmm0,  8(%1) \n\t"
2922
            "movss     %%xmm1, 40(%1) \n\t"
2923
            "movhlps   %%xmm0, %%xmm2 \n\t"
2924
            "movhlps   %%xmm1, %%xmm3 \n\t"
2925
            "movss     %%xmm2, 24(%1) \n\t"
2926
            "movss     %%xmm3, 56(%1) \n\t"
2927
            "sub  $64, %1 \n\t"
2928
            "sub  $32, %0 \n\t"
2929
            "jge  1b \n\t"
2930
            :"+r"(i), "+r"(dst)
2931
            :"r"(src0), "r"(src1), "r"(src2)
2932
            :"memory"
2933
        );
2934
    }
2935
    else if(step == 1 && src3 == 0){
2936
        asm volatile(
2937
            "1: \n\t"
2938
            "movaps   (%2,%0), %%xmm0 \n\t"
2939
            "movaps 16(%2,%0), %%xmm1 \n\t"
2940
            "mulps    (%3,%0), %%xmm0 \n\t"
2941
            "mulps  16(%3,%0), %%xmm1 \n\t"
2942
            "addps    (%4,%0), %%xmm0 \n\t"
2943
            "addps  16(%4,%0), %%xmm1 \n\t"
2944
            "movaps %%xmm0,   (%1,%0) \n\t"
2945
            "movaps %%xmm1, 16(%1,%0) \n\t"
2946
            "sub  $32, %0 \n\t"
2947
            "jge  1b \n\t"
2948
            :"+r"(i)
2949
            :"r"(dst), "r"(src0), "r"(src1), "r"(src2)
2950
            :"memory"
2951
        );
2952
    }
2953
    else
2954
        ff_vector_fmul_add_add_c(dst, src0, src1, src2, src3, len, step);
2955
}
2956

    
2957
void float_to_int16_3dnow(int16_t *dst, const float *src, int len){
2958
    // not bit-exact: pf2id uses different rounding than C and SSE
2959
    int i;
2960
    for(i=0; i<len; i+=4) {
2961
        asm volatile(
2962
            "pf2id       %1, %%mm0 \n\t"
2963
            "pf2id       %2, %%mm1 \n\t"
2964
            "packssdw %%mm1, %%mm0 \n\t"
2965
            "movq     %%mm0, %0    \n\t"
2966
            :"=m"(dst[i])
2967
            :"m"(src[i]), "m"(src[i+2])
2968
        );
2969
    }
2970
    asm volatile("femms");
2971
}
2972
void float_to_int16_sse(int16_t *dst, const float *src, int len){
2973
    int i;
2974
    for(i=0; i<len; i+=4) {
2975
        asm volatile(
2976
            "cvtps2pi    %1, %%mm0 \n\t"
2977
            "cvtps2pi    %2, %%mm1 \n\t"
2978
            "packssdw %%mm1, %%mm0 \n\t"
2979
            "movq     %%mm0, %0    \n\t"
2980
            :"=m"(dst[i])
2981
            :"m"(src[i]), "m"(src[i+2])
2982
        );
2983
    }
2984
    asm volatile("emms");
2985
}
2986

    
2987
#ifdef CONFIG_SNOW_ENCODER
2988
extern void ff_snow_horizontal_compose97i_sse2(DWTELEM *b, int width);
2989
extern void ff_snow_horizontal_compose97i_mmx(DWTELEM *b, int width);
2990
extern void ff_snow_vertical_compose97i_sse2(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width);
2991
extern void ff_snow_vertical_compose97i_mmx(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width);
2992
extern void ff_snow_inner_add_yblock_sse2(uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
2993
                           int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
2994
extern void ff_snow_inner_add_yblock_mmx(uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
2995
                          int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8);
2996
#endif
2997

    
2998
void dsputil_init_mmx(DSPContext* c, AVCodecContext *avctx)
2999
{
3000
    mm_flags = mm_support();
3001

    
3002
    if (avctx->dsp_mask) {
3003
        if (avctx->dsp_mask & FF_MM_FORCE)
3004
            mm_flags |= (avctx->dsp_mask & 0xffff);
3005
        else
3006
            mm_flags &= ~(avctx->dsp_mask & 0xffff);
3007
    }
3008

    
3009
#if 0
3010
    av_log(avctx, AV_LOG_INFO, "libavcodec: CPU flags:");
3011
    if (mm_flags & MM_MMX)
3012
        av_log(avctx, AV_LOG_INFO, " mmx");
3013
    if (mm_flags & MM_MMXEXT)
3014
        av_log(avctx, AV_LOG_INFO, " mmxext");
3015
    if (mm_flags & MM_3DNOW)
3016
        av_log(avctx, AV_LOG_INFO, " 3dnow");
3017
    if (mm_flags & MM_SSE)
3018
        av_log(avctx, AV_LOG_INFO, " sse");
3019
    if (mm_flags & MM_SSE2)
3020
        av_log(avctx, AV_LOG_INFO, " sse2");
3021
    av_log(avctx, AV_LOG_INFO, "\n");
3022
#endif
3023

    
3024
    if (mm_flags & MM_MMX) {
3025
        const int idct_algo= avctx->idct_algo;
3026

    
3027
#ifdef CONFIG_ENCODERS
3028
        const int dct_algo = avctx->dct_algo;
3029
        if(dct_algo==FF_DCT_AUTO || dct_algo==FF_DCT_MMX){
3030
            if(mm_flags & MM_SSE2){
3031
                c->fdct = ff_fdct_sse2;
3032
            }else if(mm_flags & MM_MMXEXT){
3033
                c->fdct = ff_fdct_mmx2;
3034
            }else{
3035
                c->fdct = ff_fdct_mmx;
3036
            }
3037
        }
3038
#endif //CONFIG_ENCODERS
3039
        if(avctx->lowres==0){
3040
            if(idct_algo==FF_IDCT_AUTO || idct_algo==FF_IDCT_SIMPLEMMX){
3041
                c->idct_put= ff_simple_idct_put_mmx;
3042
                c->idct_add= ff_simple_idct_add_mmx;
3043
                c->idct    = ff_simple_idct_mmx;
3044
                c->idct_permutation_type= FF_SIMPLE_IDCT_PERM;
3045
#ifdef CONFIG_GPL
3046
            }else if(idct_algo==FF_IDCT_LIBMPEG2MMX){
3047
                if(mm_flags & MM_MMXEXT){
3048
                    c->idct_put= ff_libmpeg2mmx2_idct_put;
3049
                    c->idct_add= ff_libmpeg2mmx2_idct_add;
3050
                    c->idct    = ff_mmxext_idct;
3051
                }else{
3052
                    c->idct_put= ff_libmpeg2mmx_idct_put;
3053
                    c->idct_add= ff_libmpeg2mmx_idct_add;
3054
                    c->idct    = ff_mmx_idct;
3055
                }
3056
                c->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;
3057
#endif
3058
            }else if(idct_algo==FF_IDCT_VP3 &&
3059
                     avctx->codec->id!=CODEC_ID_THEORA &&
3060
                     !(avctx->flags & CODEC_FLAG_BITEXACT)){
3061
                if(mm_flags & MM_SSE2){
3062
                    c->idct_put= ff_vp3_idct_put_sse2;
3063
                    c->idct_add= ff_vp3_idct_add_sse2;
3064
                    c->idct    = ff_vp3_idct_sse2;
3065
                    c->idct_permutation_type= FF_TRANSPOSE_IDCT_PERM;
3066
                }else{
3067
                    ff_vp3_dsp_init_mmx();
3068
                    c->idct_put= ff_vp3_idct_put_mmx;
3069
                    c->idct_add= ff_vp3_idct_add_mmx;
3070
                    c->idct    = ff_vp3_idct_mmx;
3071
                    c->idct_permutation_type= FF_PARTTRANS_IDCT_PERM;
3072
                }
3073
            }else if(idct_algo==FF_IDCT_CAVS){
3074
                    c->idct_permutation_type= FF_TRANSPOSE_IDCT_PERM;
3075
#ifdef CONFIG_GPL
3076
            }else if(idct_algo==FF_IDCT_XVIDMMX){
3077
                if(mm_flags & MM_MMXEXT){
3078
                    c->idct_put= ff_idct_xvid_mmx2_put;
3079
                    c->idct_add= ff_idct_xvid_mmx2_add;
3080
                    c->idct    = ff_idct_xvid_mmx2;
3081
                }else{
3082
                    c->idct_put= ff_idct_xvid_mmx_put;
3083
                    c->idct_add= ff_idct_xvid_mmx_add;
3084
                    c->idct    = ff_idct_xvid_mmx;
3085
                }
3086
#endif
3087
            }
3088
        }
3089

    
3090
#ifdef CONFIG_ENCODERS
3091
        c->get_pixels = get_pixels_mmx;
3092
        c->diff_pixels = diff_pixels_mmx;
3093
#endif //CONFIG_ENCODERS
3094
        c->put_pixels_clamped = put_pixels_clamped_mmx;
3095
        c->put_signed_pixels_clamped = put_signed_pixels_clamped_mmx;
3096
        c->add_pixels_clamped = add_pixels_clamped_mmx;
3097
        c->clear_blocks = clear_blocks_mmx;
3098
#ifdef CONFIG_ENCODERS
3099
        c->pix_sum = pix_sum16_mmx;
3100
#endif //CONFIG_ENCODERS
3101

    
3102
        c->put_pixels_tab[0][0] = put_pixels16_mmx;
3103
        c->put_pixels_tab[0][1] = put_pixels16_x2_mmx;
3104
        c->put_pixels_tab[0][2] = put_pixels16_y2_mmx;
3105
        c->put_pixels_tab[0][3] = put_pixels16_xy2_mmx;
3106

    
3107
        c->put_no_rnd_pixels_tab[0][0] = put_pixels16_mmx;
3108
        c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_mmx;
3109
        c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_mmx;
3110
        c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_mmx;
3111

    
3112
        c->avg_pixels_tab[0][0] = avg_pixels16_mmx;
3113
        c->avg_pixels_tab[0][1] = avg_pixels16_x2_mmx;
3114
        c->avg_pixels_tab[0][2] = avg_pixels16_y2_mmx;
3115
        c->avg_pixels_tab[0][3] = avg_pixels16_xy2_mmx;
3116

    
3117
        c->avg_no_rnd_pixels_tab[0][0] = avg_no_rnd_pixels16_mmx;
3118
        c->avg_no_rnd_pixels_tab[0][1] = avg_no_rnd_pixels16_x2_mmx;
3119
        c->avg_no_rnd_pixels_tab[0][2] = avg_no_rnd_pixels16_y2_mmx;
3120
        c->avg_no_rnd_pixels_tab[0][3] = avg_no_rnd_pixels16_xy2_mmx;
3121

    
3122
        c->put_pixels_tab[1][0] = put_pixels8_mmx;
3123
        c->put_pixels_tab[1][1] = put_pixels8_x2_mmx;
3124
        c->put_pixels_tab[1][2] = put_pixels8_y2_mmx;
3125
        c->put_pixels_tab[1][3] = put_pixels8_xy2_mmx;
3126

    
3127
        c->put_no_rnd_pixels_tab[1][0] = put_pixels8_mmx;
3128
        c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_mmx;
3129
        c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_mmx;
3130
        c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy2_mmx;
3131

    
3132
        c->avg_pixels_tab[1][0] = avg_pixels8_mmx;
3133
        c->avg_pixels_tab[1][1] = avg_pixels8_x2_mmx;
3134
        c->avg_pixels_tab[1][2] = avg_pixels8_y2_mmx;
3135
        c->avg_pixels_tab[1][3] = avg_pixels8_xy2_mmx;
3136

    
3137
        c->avg_no_rnd_pixels_tab[1][0] = avg_no_rnd_pixels8_mmx;
3138
        c->avg_no_rnd_pixels_tab[1][1] = avg_no_rnd_pixels8_x2_mmx;
3139
        c->avg_no_rnd_pixels_tab[1][2] = avg_no_rnd_pixels8_y2_mmx;
3140
        c->avg_no_rnd_pixels_tab[1][3] = avg_no_rnd_pixels8_xy2_mmx;
3141

    
3142
        c->gmc= gmc_mmx;
3143

    
3144
        c->add_bytes= add_bytes_mmx;
3145
#ifdef CONFIG_ENCODERS
3146
        c->diff_bytes= diff_bytes_mmx;
3147

    
3148
        c->hadamard8_diff[0]= hadamard8_diff16_mmx;
3149
        c->hadamard8_diff[1]= hadamard8_diff_mmx;
3150

    
3151
        c->pix_norm1 = pix_norm1_mmx;
3152
        c->sse[0] = (mm_flags & MM_SSE2) ? sse16_sse2 : sse16_mmx;
3153
          c->sse[1] = sse8_mmx;
3154
        c->vsad[4]= vsad_intra16_mmx;
3155

    
3156
        c->nsse[0] = nsse16_mmx;
3157
        c->nsse[1] = nsse8_mmx;
3158
        if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
3159
            c->vsad[0] = vsad16_mmx;
3160
        }
3161

    
3162
        if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
3163
            c->try_8x8basis= try_8x8basis_mmx;
3164
        }
3165
        c->add_8x8basis= add_8x8basis_mmx;
3166

    
3167
#endif //CONFIG_ENCODERS
3168

    
3169
        c->h263_v_loop_filter= h263_v_loop_filter_mmx;
3170
        c->h263_h_loop_filter= h263_h_loop_filter_mmx;
3171
        c->put_h264_chroma_pixels_tab[0]= put_h264_chroma_mc8_mmx;
3172
        c->put_h264_chroma_pixels_tab[1]= put_h264_chroma_mc4_mmx;
3173

    
3174
        c->h264_idct_dc_add=
3175
        c->h264_idct_add= ff_h264_idct_add_mmx;
3176
        c->h264_idct8_dc_add=
3177
        c->h264_idct8_add= ff_h264_idct8_add_mmx;
3178

    
3179
        if (mm_flags & MM_MMXEXT) {
3180
            c->prefetch = prefetch_mmx2;
3181

    
3182
            c->put_pixels_tab[0][1] = put_pixels16_x2_mmx2;
3183
            c->put_pixels_tab[0][2] = put_pixels16_y2_mmx2;
3184

    
3185
            c->avg_pixels_tab[0][0] = avg_pixels16_mmx2;
3186
            c->avg_pixels_tab[0][1] = avg_pixels16_x2_mmx2;
3187
            c->avg_pixels_tab[0][2] = avg_pixels16_y2_mmx2;
3188

    
3189
            c->put_pixels_tab[1][1] = put_pixels8_x2_mmx2;
3190
            c->put_pixels_tab[1][2] = put_pixels8_y2_mmx2;
3191

    
3192
            c->avg_pixels_tab[1][0] = avg_pixels8_mmx2;
3193
            c->avg_pixels_tab[1][1] = avg_pixels8_x2_mmx2;
3194
            c->avg_pixels_tab[1][2] = avg_pixels8_y2_mmx2;
3195

    
3196
#ifdef CONFIG_ENCODERS
3197
            c->hadamard8_diff[0]= hadamard8_diff16_mmx2;
3198
            c->hadamard8_diff[1]= hadamard8_diff_mmx2;
3199
            c->vsad[4]= vsad_intra16_mmx2;
3200
#endif //CONFIG_ENCODERS
3201

    
3202
            c->h264_idct_dc_add= ff_h264_idct_dc_add_mmx2;
3203
            c->h264_idct8_dc_add= ff_h264_idct8_dc_add_mmx2;
3204

    
3205
            if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
3206
                c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_mmx2;
3207
                c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_mmx2;
3208
                c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x2_mmx2;
3209
                c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y2_mmx2;
3210
                c->avg_pixels_tab[0][3] = avg_pixels16_xy2_mmx2;
3211
                c->avg_pixels_tab[1][3] = avg_pixels8_xy2_mmx2;
3212
#ifdef CONFIG_ENCODERS
3213
                c->vsad[0] = vsad16_mmx2;
3214
#endif //CONFIG_ENCODERS
3215
            }
3216

    
3217
#if 1
3218
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 0], qpel16_mc00_mmx2)
3219
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 1], qpel16_mc10_mmx2)
3220
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 2], qpel16_mc20_mmx2)
3221
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 3], qpel16_mc30_mmx2)
3222
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 4], qpel16_mc01_mmx2)
3223
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_mmx2)
3224
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 6], qpel16_mc21_mmx2)
3225
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_mmx2)
3226
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 8], qpel16_mc02_mmx2)
3227
            SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_mmx2)
3228
            SET_QPEL_FUNC(qpel_pixels_tab[0][10], qpel16_mc22_mmx2)
3229
            SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_mmx2)
3230
            SET_QPEL_FUNC(qpel_pixels_tab[0][12], qpel16_mc03_mmx2)
3231
            SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_mmx2)
3232
            SET_QPEL_FUNC(qpel_pixels_tab[0][14], qpel16_mc23_mmx2)
3233
            SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_mmx2)
3234
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 0], qpel8_mc00_mmx2)
3235
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 1], qpel8_mc10_mmx2)
3236
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 2], qpel8_mc20_mmx2)
3237
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 3], qpel8_mc30_mmx2)
3238
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 4], qpel8_mc01_mmx2)
3239
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_mmx2)
3240
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 6], qpel8_mc21_mmx2)
3241
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_mmx2)
3242
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 8], qpel8_mc02_mmx2)
3243
            SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_mmx2)
3244
            SET_QPEL_FUNC(qpel_pixels_tab[1][10], qpel8_mc22_mmx2)
3245
            SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_mmx2)
3246
            SET_QPEL_FUNC(qpel_pixels_tab[1][12], qpel8_mc03_mmx2)
3247
            SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_mmx2)
3248
            SET_QPEL_FUNC(qpel_pixels_tab[1][14], qpel8_mc23_mmx2)
3249
            SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_mmx2)
3250
#endif
3251

    
3252
//FIXME 3dnow too
3253
#define dspfunc(PFX, IDX, NUM) \
3254
    c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_mmx2; \
3255
    c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_mmx2; \
3256
    c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_mmx2; \
3257
    c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_mmx2; \
3258
    c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_mmx2; \
3259
    c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_mmx2; \
3260
    c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_mmx2; \
3261
    c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_mmx2; \
3262
    c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_mmx2; \
3263
    c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_mmx2; \
3264
    c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_mmx2; \
3265
    c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_mmx2; \
3266
    c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_mmx2; \
3267
    c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_mmx2; \
3268
    c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_mmx2; \
3269
    c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_mmx2
3270

    
3271
            dspfunc(put_h264_qpel, 0, 16);
3272
            dspfunc(put_h264_qpel, 1, 8);
3273
            dspfunc(put_h264_qpel, 2, 4);
3274
            dspfunc(avg_h264_qpel, 0, 16);
3275
            dspfunc(avg_h264_qpel, 1, 8);
3276
            dspfunc(avg_h264_qpel, 2, 4);
3277
#undef dspfunc
3278