]> git.sesse.net Git - ffmpeg/blob - libavcodec/i386/dsputil_mmx_avg.h
WV1F support
[ffmpeg] / libavcodec / i386 / dsputil_mmx_avg.h
1 /*
2  * DSP utils : average functions are compiled twice for 3dnow/mmx2
3  * Copyright (c) 2000, 2001 Fabrice Bellard.
4  * Copyright (c) 2002-2004 Michael Niedermayer
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
21  * mostly rewritten by Michael Niedermayer <michaelni@gmx.at>
22  * and improved by Zdenek Kabelac <kabi@users.sf.net>
23  */
24  
25 /* XXX: we use explicit registers to avoid a gcc 2.95.2 register asm
26    clobber bug - now it will work with 2.95.2 and also with -fPIC
27  */
28 static void DEF(put_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
29 {
30     __asm __volatile(
31         "lea (%3, %3), %%"REG_a"        \n\t"
32         "1:                             \n\t"
33         "movq (%1), %%mm0               \n\t"
34         "movq (%1, %3), %%mm1           \n\t"
35         PAVGB" 1(%1), %%mm0             \n\t"
36         PAVGB" 1(%1, %3), %%mm1         \n\t"
37         "movq %%mm0, (%2)               \n\t"
38         "movq %%mm1, (%2, %3)           \n\t"
39         "add %%"REG_a", %1              \n\t"
40         "add %%"REG_a", %2              \n\t"
41         "movq (%1), %%mm0               \n\t"
42         "movq (%1, %3), %%mm1           \n\t"
43         PAVGB" 1(%1), %%mm0             \n\t"
44         PAVGB" 1(%1, %3), %%mm1         \n\t"
45         "add %%"REG_a", %1              \n\t"
46         "movq %%mm0, (%2)               \n\t"
47         "movq %%mm1, (%2, %3)           \n\t"
48         "add %%"REG_a", %2              \n\t"
49         "subl $4, %0                    \n\t"
50         "jnz 1b                         \n\t"
51         :"+g"(h), "+S"(pixels), "+D"(block)
52         :"r" ((long)line_size)
53         :"%"REG_a, "memory");
54 }
55
56 static void DEF(put_pixels4_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
57 {
58     __asm __volatile(
59         "testl $1, %0                   \n\t"
60             " jz 1f                             \n\t"
61         "movd   (%1), %%mm0             \n\t"
62         "movd   (%2), %%mm1             \n\t"
63         "add    %4, %1                  \n\t"
64         "add    $4, %2                  \n\t"
65         PAVGB" %%mm1, %%mm0             \n\t"
66         "movd   %%mm0, (%3)             \n\t"
67         "add    %5, %3                  \n\t"
68         "decl   %0                      \n\t"
69         "1:                             \n\t"
70         "movd   (%1), %%mm0             \n\t"
71         "add    %4, %1                  \n\t"
72         "movd   (%1), %%mm1             \n\t"
73         "add    %4, %1                  \n\t"
74         PAVGB" (%2), %%mm0              \n\t"
75         PAVGB" 4(%2), %%mm1             \n\t"
76         "movd   %%mm0, (%3)             \n\t"
77         "add    %5, %3                  \n\t"
78         "movd   %%mm1, (%3)             \n\t"
79         "add    %5, %3                  \n\t"
80         "movd   (%1), %%mm0             \n\t"
81         "add    %4, %1                  \n\t"
82         "movd   (%1), %%mm1             \n\t"
83         "add    %4, %1                  \n\t"
84         PAVGB" 8(%2), %%mm0             \n\t"
85         PAVGB" 12(%2), %%mm1            \n\t"
86         "movd   %%mm0, (%3)             \n\t"
87         "add    %5, %3                  \n\t"
88         "movd   %%mm1, (%3)             \n\t"
89         "add    %5, %3                  \n\t"
90         "add    $16, %2                 \n\t"
91         "subl   $4, %0                  \n\t"
92         "jnz    1b                      \n\t"
93 #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used
94         :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
95 #else
96         :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
97 #endif
98         :"S"((long)src1Stride), "D"((long)dstStride)
99         :"memory"); 
100 }
101
102
103 static void DEF(put_pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
104 {
105     __asm __volatile(
106         "testl $1, %0                   \n\t"
107             " jz 1f                             \n\t"
108         "movq   (%1), %%mm0             \n\t"
109         "movq   (%2), %%mm1             \n\t"
110         "add    %4, %1                  \n\t"
111         "add    $8, %2                  \n\t"
112         PAVGB" %%mm1, %%mm0             \n\t"
113         "movq   %%mm0, (%3)             \n\t"
114         "add    %5, %3                  \n\t"
115         "decl   %0                      \n\t"
116         "1:                             \n\t"
117         "movq   (%1), %%mm0             \n\t"
118         "add    %4, %1                  \n\t"
119         "movq   (%1), %%mm1             \n\t"
120         "add    %4, %1                  \n\t"
121         PAVGB" (%2), %%mm0              \n\t"
122         PAVGB" 8(%2), %%mm1             \n\t"
123         "movq   %%mm0, (%3)             \n\t"
124         "add    %5, %3                  \n\t"
125         "movq   %%mm1, (%3)             \n\t"
126         "add    %5, %3                  \n\t"
127         "movq   (%1), %%mm0             \n\t"
128         "add    %4, %1                  \n\t"
129         "movq   (%1), %%mm1             \n\t"
130         "add    %4, %1                  \n\t"
131         PAVGB" 16(%2), %%mm0            \n\t"
132         PAVGB" 24(%2), %%mm1            \n\t"
133         "movq   %%mm0, (%3)             \n\t"
134         "add    %5, %3                  \n\t"
135         "movq   %%mm1, (%3)             \n\t"
136         "add    %5, %3                  \n\t"
137         "add    $32, %2                 \n\t"
138         "subl   $4, %0                  \n\t"
139         "jnz    1b                      \n\t"
140 #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used
141         :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
142 #else
143         :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
144 #endif
145         :"S"((long)src1Stride), "D"((long)dstStride)
146         :"memory"); 
147 //the following should be used, though better not with gcc ...
148 /*      :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst)
149         :"r"(src1Stride), "r"(dstStride)
150         :"memory");*/
151 }
152
153 static void DEF(put_no_rnd_pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
154 {
155     __asm __volatile(
156         "pcmpeqb %%mm6, %%mm6   \n\t"
157         "testl $1, %0                   \n\t"
158             " jz 1f                             \n\t"
159         "movq   (%1), %%mm0             \n\t"
160         "movq   (%2), %%mm1             \n\t"
161         "add    %4, %1                  \n\t"
162         "add    $8, %2                  \n\t"
163         "pxor %%mm6, %%mm0              \n\t"
164         "pxor %%mm6, %%mm1              \n\t"
165         PAVGB" %%mm1, %%mm0             \n\t"
166         "pxor %%mm6, %%mm0              \n\t"
167         "movq   %%mm0, (%3)             \n\t"
168         "add    %5, %3                  \n\t"
169         "decl   %0                      \n\t"
170         "1:                             \n\t"
171         "movq   (%1), %%mm0             \n\t"
172         "add    %4, %1                  \n\t"
173         "movq   (%1), %%mm1             \n\t"
174         "add    %4, %1                  \n\t"
175         "movq   (%2), %%mm2             \n\t"
176         "movq   8(%2), %%mm3            \n\t"
177         "pxor %%mm6, %%mm0              \n\t"
178         "pxor %%mm6, %%mm1              \n\t"
179         "pxor %%mm6, %%mm2              \n\t"
180         "pxor %%mm6, %%mm3              \n\t"
181         PAVGB" %%mm2, %%mm0             \n\t"
182         PAVGB" %%mm3, %%mm1             \n\t"
183         "pxor %%mm6, %%mm0              \n\t"
184         "pxor %%mm6, %%mm1              \n\t"
185         "movq   %%mm0, (%3)             \n\t"
186         "add    %5, %3                  \n\t"
187         "movq   %%mm1, (%3)             \n\t"
188         "add    %5, %3                  \n\t"
189         "movq   (%1), %%mm0             \n\t"
190         "add    %4, %1                  \n\t"
191         "movq   (%1), %%mm1             \n\t"
192         "add    %4, %1                  \n\t"
193         "movq   16(%2), %%mm2           \n\t"
194         "movq   24(%2), %%mm3           \n\t"
195         "pxor %%mm6, %%mm0              \n\t"
196         "pxor %%mm6, %%mm1              \n\t"
197         "pxor %%mm6, %%mm2              \n\t"
198         "pxor %%mm6, %%mm3              \n\t"
199         PAVGB" %%mm2, %%mm0             \n\t"
200         PAVGB" %%mm3, %%mm1             \n\t"
201         "pxor %%mm6, %%mm0              \n\t"
202         "pxor %%mm6, %%mm1              \n\t"
203         "movq   %%mm0, (%3)             \n\t"
204         "add    %5, %3                  \n\t"
205         "movq   %%mm1, (%3)             \n\t"
206         "add    %5, %3                  \n\t"
207         "add    $32, %2                 \n\t"
208         "subl   $4, %0                  \n\t"
209         "jnz    1b                      \n\t"
210 #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used
211         :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
212 #else
213         :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
214 #endif
215         :"S"((long)src1Stride), "D"((long)dstStride)
216         :"memory"); 
217 //the following should be used, though better not with gcc ...
218 /*      :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst)
219         :"r"(src1Stride), "r"(dstStride)
220         :"memory");*/
221 }
222
223 static void DEF(avg_pixels4_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
224 {
225     __asm __volatile(
226         "testl $1, %0                   \n\t"
227             " jz 1f                             \n\t"
228         "movd   (%1), %%mm0             \n\t"
229         "movd   (%2), %%mm1             \n\t"
230         "add    %4, %1                  \n\t"
231         "add    $4, %2                  \n\t"
232         PAVGB" %%mm1, %%mm0             \n\t"
233         PAVGB" (%3), %%mm0              \n\t"
234         "movd   %%mm0, (%3)             \n\t"
235         "add    %5, %3                  \n\t"
236         "decl   %0                      \n\t"
237         "1:                             \n\t"
238         "movd   (%1), %%mm0             \n\t"
239         "add    %4, %1                  \n\t"
240         "movd   (%1), %%mm1             \n\t"
241         "add    %4, %1                  \n\t"
242         PAVGB" (%2), %%mm0              \n\t"
243         PAVGB" 4(%2), %%mm1             \n\t"
244         PAVGB" (%3), %%mm0              \n\t"
245         "movd   %%mm0, (%3)             \n\t"
246         "add    %5, %3                  \n\t"
247         PAVGB" (%3), %%mm1              \n\t"
248         "movd   %%mm1, (%3)             \n\t"
249         "add    %5, %3                  \n\t"
250         "movd   (%1), %%mm0             \n\t"
251         "add    %4, %1                  \n\t"
252         "movd   (%1), %%mm1             \n\t"
253         "add    %4, %1                  \n\t"
254         PAVGB" 8(%2), %%mm0             \n\t"
255         PAVGB" 12(%2), %%mm1            \n\t"
256         PAVGB" (%3), %%mm0              \n\t"
257         "movd   %%mm0, (%3)             \n\t"
258         "add    %5, %3                  \n\t"
259         PAVGB" (%3), %%mm1              \n\t"
260         "movd   %%mm1, (%3)             \n\t"
261         "add    %5, %3                  \n\t"
262         "add    $16, %2                 \n\t"
263         "subl   $4, %0                  \n\t"
264         "jnz    1b                      \n\t"
265 #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used
266         :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
267 #else
268         :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
269 #endif
270         :"S"((long)src1Stride), "D"((long)dstStride)
271         :"memory"); 
272 }
273
274
275 static void DEF(avg_pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
276 {
277     __asm __volatile(
278         "testl $1, %0                   \n\t"
279             " jz 1f                             \n\t"
280         "movq   (%1), %%mm0             \n\t"
281         "movq   (%2), %%mm1             \n\t"
282         "add    %4, %1                  \n\t"
283         "add    $8, %2                  \n\t"
284         PAVGB" %%mm1, %%mm0             \n\t"
285         PAVGB" (%3), %%mm0              \n\t"
286         "movq   %%mm0, (%3)             \n\t"
287         "add    %5, %3                  \n\t"
288         "decl   %0                      \n\t"
289         "1:                             \n\t"
290         "movq   (%1), %%mm0             \n\t"
291         "add    %4, %1                  \n\t"
292         "movq   (%1), %%mm1             \n\t"
293         "add    %4, %1                  \n\t"
294         PAVGB" (%2), %%mm0              \n\t"
295         PAVGB" 8(%2), %%mm1             \n\t"
296         PAVGB" (%3), %%mm0              \n\t"
297         "movq   %%mm0, (%3)             \n\t"
298         "add    %5, %3                  \n\t"
299         PAVGB" (%3), %%mm1              \n\t"
300         "movq   %%mm1, (%3)             \n\t"
301         "add    %5, %3                  \n\t"
302         "movq   (%1), %%mm0             \n\t"
303         "add    %4, %1                  \n\t"
304         "movq   (%1), %%mm1             \n\t"
305         "add    %4, %1                  \n\t"
306         PAVGB" 16(%2), %%mm0            \n\t"
307         PAVGB" 24(%2), %%mm1            \n\t"
308         PAVGB" (%3), %%mm0              \n\t"
309         "movq   %%mm0, (%3)             \n\t"
310         "add    %5, %3                  \n\t"
311         PAVGB" (%3), %%mm1              \n\t"
312         "movq   %%mm1, (%3)             \n\t"
313         "add    %5, %3                  \n\t"
314         "add    $32, %2                 \n\t"
315         "subl   $4, %0                  \n\t"
316         "jnz    1b                      \n\t"
317 #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used
318         :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
319 #else
320         :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
321 #endif
322         :"S"((long)src1Stride), "D"((long)dstStride)
323         :"memory"); 
324 //the following should be used, though better not with gcc ...
325 /*      :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst)
326         :"r"(src1Stride), "r"(dstStride)
327         :"memory");*/
328 }
329
330 static void DEF(put_pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
331 {
332     __asm __volatile(
333         "lea (%3, %3), %%"REG_a"        \n\t"
334         "1:                             \n\t"
335         "movq (%1), %%mm0               \n\t"
336         "movq (%1, %3), %%mm1           \n\t"
337         "movq 8(%1), %%mm2              \n\t"
338         "movq 8(%1, %3), %%mm3          \n\t"
339         PAVGB" 1(%1), %%mm0             \n\t"
340         PAVGB" 1(%1, %3), %%mm1         \n\t"
341         PAVGB" 9(%1), %%mm2             \n\t"
342         PAVGB" 9(%1, %3), %%mm3         \n\t"
343         "movq %%mm0, (%2)               \n\t"
344         "movq %%mm1, (%2, %3)           \n\t"
345         "movq %%mm2, 8(%2)              \n\t"
346         "movq %%mm3, 8(%2, %3)          \n\t"
347         "add %%"REG_a", %1              \n\t"
348         "add %%"REG_a", %2              \n\t"
349         "movq (%1), %%mm0               \n\t"
350         "movq (%1, %3), %%mm1           \n\t"
351         "movq 8(%1), %%mm2              \n\t"
352         "movq 8(%1, %3), %%mm3          \n\t"
353         PAVGB" 1(%1), %%mm0             \n\t"
354         PAVGB" 1(%1, %3), %%mm1         \n\t"
355         PAVGB" 9(%1), %%mm2             \n\t"
356         PAVGB" 9(%1, %3), %%mm3         \n\t"
357         "add %%"REG_a", %1              \n\t"
358         "movq %%mm0, (%2)               \n\t"
359         "movq %%mm1, (%2, %3)           \n\t"
360         "movq %%mm2, 8(%2)              \n\t"
361         "movq %%mm3, 8(%2, %3)          \n\t"
362         "add %%"REG_a", %2              \n\t"
363         "subl $4, %0                    \n\t"
364         "jnz 1b                         \n\t"
365         :"+g"(h), "+S"(pixels), "+D"(block)
366         :"r" ((long)line_size)
367         :"%"REG_a, "memory");
368 }
369
370 static void DEF(put_pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
371 {
372     __asm __volatile(
373         "testl $1, %0                   \n\t"
374             " jz 1f                             \n\t"
375         "movq   (%1), %%mm0             \n\t"
376         "movq   8(%1), %%mm1            \n\t"
377         PAVGB" (%2), %%mm0              \n\t"
378         PAVGB" 8(%2), %%mm1             \n\t"
379         "add    %4, %1                  \n\t"
380         "add    $16, %2                 \n\t"
381         "movq   %%mm0, (%3)             \n\t"
382         "movq   %%mm1, 8(%3)            \n\t"
383         "add    %5, %3                  \n\t"
384         "decl   %0                      \n\t"
385         "1:                             \n\t"
386         "movq   (%1), %%mm0             \n\t"
387         "movq   8(%1), %%mm1            \n\t"
388         "add    %4, %1                  \n\t"
389         PAVGB" (%2), %%mm0              \n\t"
390         PAVGB" 8(%2), %%mm1             \n\t"
391         "movq   %%mm0, (%3)             \n\t"
392         "movq   %%mm1, 8(%3)            \n\t"
393         "add    %5, %3                  \n\t"
394         "movq   (%1), %%mm0             \n\t"
395         "movq   8(%1), %%mm1            \n\t"
396         "add    %4, %1                  \n\t"
397         PAVGB" 16(%2), %%mm0            \n\t"
398         PAVGB" 24(%2), %%mm1            \n\t"
399         "movq   %%mm0, (%3)             \n\t"
400         "movq   %%mm1, 8(%3)            \n\t"
401         "add    %5, %3                  \n\t"
402         "add    $32, %2                 \n\t"
403         "subl   $2, %0                  \n\t"
404         "jnz    1b                      \n\t"
405 #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used
406         :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
407 #else
408         :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
409 #endif
410         :"S"((long)src1Stride), "D"((long)dstStride)
411         :"memory"); 
412 //the following should be used, though better not with gcc ...
413 /*      :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst)
414         :"r"(src1Stride), "r"(dstStride)
415         :"memory");*/
416 }
417
418 static void DEF(avg_pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
419 {
420     __asm __volatile(
421         "testl $1, %0                   \n\t"
422             " jz 1f                             \n\t"
423         "movq   (%1), %%mm0             \n\t"
424         "movq   8(%1), %%mm1            \n\t"
425         PAVGB" (%2), %%mm0              \n\t"
426         PAVGB" 8(%2), %%mm1             \n\t"
427         "add    %4, %1                  \n\t"
428         "add    $16, %2                 \n\t"
429         PAVGB" (%3), %%mm0              \n\t"
430         PAVGB" 8(%3), %%mm1             \n\t"
431         "movq   %%mm0, (%3)             \n\t"
432         "movq   %%mm1, 8(%3)            \n\t"
433         "add    %5, %3                  \n\t"
434         "decl   %0                      \n\t"
435         "1:                             \n\t"
436         "movq   (%1), %%mm0             \n\t"
437         "movq   8(%1), %%mm1            \n\t"
438         "add    %4, %1                  \n\t"
439         PAVGB" (%2), %%mm0              \n\t"
440         PAVGB" 8(%2), %%mm1             \n\t"
441         PAVGB" (%3), %%mm0              \n\t"
442         PAVGB" 8(%3), %%mm1             \n\t"
443         "movq   %%mm0, (%3)             \n\t"
444         "movq   %%mm1, 8(%3)            \n\t"
445         "add    %5, %3                  \n\t"
446         "movq   (%1), %%mm0             \n\t"
447         "movq   8(%1), %%mm1            \n\t"
448         "add    %4, %1                  \n\t"
449         PAVGB" 16(%2), %%mm0            \n\t"
450         PAVGB" 24(%2), %%mm1            \n\t"
451         PAVGB" (%3), %%mm0              \n\t"
452         PAVGB" 8(%3), %%mm1             \n\t"
453         "movq   %%mm0, (%3)             \n\t"
454         "movq   %%mm1, 8(%3)            \n\t"
455         "add    %5, %3                  \n\t"
456         "add    $32, %2                 \n\t"
457         "subl   $2, %0                  \n\t"
458         "jnz    1b                      \n\t"
459 #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used
460         :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
461 #else
462         :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
463 #endif
464         :"S"((long)src1Stride), "D"((long)dstStride)
465         :"memory"); 
466 //the following should be used, though better not with gcc ...
467 /*      :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst)
468         :"r"(src1Stride), "r"(dstStride)
469         :"memory");*/
470 }
471
472 static void DEF(put_no_rnd_pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
473 {
474     __asm __volatile(
475         "pcmpeqb %%mm6, %%mm6\n\t"
476         "testl $1, %0                   \n\t"
477             " jz 1f                             \n\t"
478         "movq   (%1), %%mm0             \n\t"
479         "movq   8(%1), %%mm1            \n\t"
480         "movq   (%2), %%mm2             \n\t"
481         "movq   8(%2), %%mm3            \n\t"
482         "pxor %%mm6, %%mm0              \n\t"
483         "pxor %%mm6, %%mm1              \n\t"
484         "pxor %%mm6, %%mm2              \n\t"
485         "pxor %%mm6, %%mm3              \n\t"
486         PAVGB" %%mm2, %%mm0             \n\t"
487         PAVGB" %%mm3, %%mm1             \n\t"
488         "pxor %%mm6, %%mm0              \n\t"
489         "pxor %%mm6, %%mm1              \n\t"
490         "add    %4, %1                  \n\t"
491         "add    $16, %2                 \n\t"
492         "movq   %%mm0, (%3)             \n\t"
493         "movq   %%mm1, 8(%3)            \n\t"
494         "add    %5, %3                  \n\t"
495         "decl   %0                      \n\t"
496         "1:                             \n\t"
497         "movq   (%1), %%mm0             \n\t"
498         "movq   8(%1), %%mm1            \n\t"
499         "add    %4, %1                  \n\t"
500         "movq   (%2), %%mm2             \n\t"
501         "movq   8(%2), %%mm3            \n\t"
502         "pxor %%mm6, %%mm0              \n\t"
503         "pxor %%mm6, %%mm1              \n\t"
504         "pxor %%mm6, %%mm2              \n\t"
505         "pxor %%mm6, %%mm3              \n\t"
506         PAVGB" %%mm2, %%mm0             \n\t"
507         PAVGB" %%mm3, %%mm1             \n\t"
508         "pxor %%mm6, %%mm0              \n\t"
509         "pxor %%mm6, %%mm1              \n\t"
510         "movq   %%mm0, (%3)             \n\t"
511         "movq   %%mm1, 8(%3)            \n\t"
512         "add    %5, %3                  \n\t"
513         "movq   (%1), %%mm0             \n\t"
514         "movq   8(%1), %%mm1            \n\t"
515         "add    %4, %1                  \n\t"
516         "movq   16(%2), %%mm2           \n\t"
517         "movq   24(%2), %%mm3           \n\t"
518         "pxor %%mm6, %%mm0              \n\t"
519         "pxor %%mm6, %%mm1              \n\t"
520         "pxor %%mm6, %%mm2              \n\t"
521         "pxor %%mm6, %%mm3              \n\t"
522         PAVGB" %%mm2, %%mm0             \n\t"
523         PAVGB" %%mm3, %%mm1             \n\t"
524         "pxor %%mm6, %%mm0              \n\t"
525         "pxor %%mm6, %%mm1              \n\t"
526         "movq   %%mm0, (%3)             \n\t"
527         "movq   %%mm1, 8(%3)            \n\t"
528         "add    %5, %3                  \n\t"
529         "add    $32, %2                 \n\t"
530         "subl   $2, %0                  \n\t"
531         "jnz    1b                      \n\t"
532 #ifdef PIC //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cant be used
533         :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
534 #else
535         :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
536 #endif
537         :"S"((long)src1Stride), "D"((long)dstStride)
538         :"memory"); 
539 //the following should be used, though better not with gcc ...
540 /*      :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst)
541         :"r"(src1Stride), "r"(dstStride)
542         :"memory");*/
543 }
544  
545 /* GL: this function does incorrect rounding if overflow */
546 static void DEF(put_no_rnd_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
547 {
548     MOVQ_BONE(mm6);
549     __asm __volatile(
550         "lea (%3, %3), %%"REG_a"        \n\t"
551         "1:                             \n\t"
552         "movq (%1), %%mm0               \n\t"
553         "movq (%1, %3), %%mm2           \n\t"
554         "movq 1(%1), %%mm1              \n\t"
555         "movq 1(%1, %3), %%mm3          \n\t"
556         "add %%"REG_a", %1              \n\t"
557         "psubusb %%mm6, %%mm0           \n\t"
558         "psubusb %%mm6, %%mm2           \n\t"
559         PAVGB" %%mm1, %%mm0             \n\t"
560         PAVGB" %%mm3, %%mm2             \n\t"
561         "movq %%mm0, (%2)               \n\t"
562         "movq %%mm2, (%2, %3)           \n\t"
563         "movq (%1), %%mm0               \n\t"
564         "movq 1(%1), %%mm1              \n\t"
565         "movq (%1, %3), %%mm2           \n\t"
566         "movq 1(%1, %3), %%mm3          \n\t"
567         "add %%"REG_a", %2              \n\t"
568         "add %%"REG_a", %1              \n\t"
569         "psubusb %%mm6, %%mm0           \n\t"
570         "psubusb %%mm6, %%mm2           \n\t"
571         PAVGB" %%mm1, %%mm0             \n\t"
572         PAVGB" %%mm3, %%mm2             \n\t"
573         "movq %%mm0, (%2)               \n\t"
574         "movq %%mm2, (%2, %3)           \n\t"
575         "add %%"REG_a", %2              \n\t"
576         "subl $4, %0                    \n\t"
577         "jnz 1b                         \n\t"
578         :"+g"(h), "+S"(pixels), "+D"(block)
579         :"r" ((long)line_size)
580         :"%"REG_a, "memory");
581 }
582
583 static void DEF(put_pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
584 {
585     __asm __volatile(
586         "lea (%3, %3), %%"REG_a"        \n\t"
587         "movq (%1), %%mm0               \n\t"
588         "sub %3, %2                     \n\t"
589         "1:                             \n\t"
590         "movq (%1, %3), %%mm1           \n\t"
591         "movq (%1, %%"REG_a"), %%mm2    \n\t"
592         "add %%"REG_a", %1              \n\t"
593         PAVGB" %%mm1, %%mm0             \n\t"
594         PAVGB" %%mm2, %%mm1             \n\t"
595         "movq %%mm0, (%2, %3)           \n\t"
596         "movq %%mm1, (%2, %%"REG_a")    \n\t"
597         "movq (%1, %3), %%mm1           \n\t"
598         "movq (%1, %%"REG_a"), %%mm0    \n\t"
599         "add %%"REG_a", %2              \n\t"
600         "add %%"REG_a", %1              \n\t"
601         PAVGB" %%mm1, %%mm2             \n\t"
602         PAVGB" %%mm0, %%mm1             \n\t"
603         "movq %%mm2, (%2, %3)           \n\t"
604         "movq %%mm1, (%2, %%"REG_a")    \n\t"
605         "add %%"REG_a", %2              \n\t"
606         "subl $4, %0                    \n\t"
607         "jnz 1b                         \n\t"
608         :"+g"(h), "+S"(pixels), "+D" (block)
609         :"r" ((long)line_size)
610         :"%"REG_a, "memory");
611 }
612
613 /* GL: this function does incorrect rounding if overflow */
614 static void DEF(put_no_rnd_pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
615 {
616     MOVQ_BONE(mm6);
617     __asm __volatile(
618         "lea (%3, %3), %%"REG_a"        \n\t"
619         "movq (%1), %%mm0               \n\t"
620         "sub %3, %2                     \n\t"
621         "1:                             \n\t"
622         "movq (%1, %3), %%mm1           \n\t"
623         "movq (%1, %%"REG_a"), %%mm2    \n\t"
624         "add %%"REG_a", %1              \n\t"
625         "psubusb %%mm6, %%mm1           \n\t"
626         PAVGB" %%mm1, %%mm0             \n\t"
627         PAVGB" %%mm2, %%mm1             \n\t"
628         "movq %%mm0, (%2, %3)           \n\t"
629         "movq %%mm1, (%2, %%"REG_a")    \n\t"
630         "movq (%1, %3), %%mm1           \n\t"
631         "movq (%1, %%"REG_a"), %%mm0    \n\t"
632         "add %%"REG_a", %2              \n\t"
633         "add %%"REG_a", %1              \n\t"
634         "psubusb %%mm6, %%mm1           \n\t"
635         PAVGB" %%mm1, %%mm2             \n\t"
636         PAVGB" %%mm0, %%mm1             \n\t"
637         "movq %%mm2, (%2, %3)           \n\t"
638         "movq %%mm1, (%2, %%"REG_a")    \n\t"
639         "add %%"REG_a", %2              \n\t"
640         "subl $4, %0                    \n\t"
641         "jnz 1b                         \n\t"
642         :"+g"(h), "+S"(pixels), "+D" (block)
643         :"r" ((long)line_size)
644         :"%"REG_a, "memory");
645 }
646
647 static void DEF(avg_pixels8)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
648 {
649     __asm __volatile(
650         "lea (%3, %3), %%"REG_a"        \n\t"
651         "1:                             \n\t"
652         "movq (%2), %%mm0               \n\t"
653         "movq (%2, %3), %%mm1           \n\t"
654         PAVGB" (%1), %%mm0              \n\t"
655         PAVGB" (%1, %3), %%mm1          \n\t"
656         "movq %%mm0, (%2)               \n\t"
657         "movq %%mm1, (%2, %3)           \n\t"
658         "add %%"REG_a", %1              \n\t"
659         "add %%"REG_a", %2              \n\t"
660         "movq (%2), %%mm0               \n\t"
661         "movq (%2, %3), %%mm1           \n\t"
662         PAVGB" (%1), %%mm0              \n\t"
663         PAVGB" (%1, %3), %%mm1          \n\t"
664         "add %%"REG_a", %1              \n\t"
665         "movq %%mm0, (%2)               \n\t"
666         "movq %%mm1, (%2, %3)           \n\t"
667         "add %%"REG_a", %2              \n\t"
668         "subl $4, %0                    \n\t"
669         "jnz 1b                         \n\t"
670         :"+g"(h), "+S"(pixels), "+D"(block)
671         :"r" ((long)line_size)
672         :"%"REG_a, "memory");
673 }
674
675 static void DEF(avg_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
676 {
677     __asm __volatile(
678         "lea (%3, %3), %%"REG_a"        \n\t"
679         "1:                             \n\t"
680         "movq (%1), %%mm0               \n\t"
681         "movq (%1, %3), %%mm2           \n\t"
682         PAVGB" 1(%1), %%mm0             \n\t"
683         PAVGB" 1(%1, %3), %%mm2         \n\t"
684         PAVGB" (%2), %%mm0              \n\t"
685         PAVGB" (%2, %3), %%mm2          \n\t"
686         "add %%"REG_a", %1              \n\t"
687         "movq %%mm0, (%2)               \n\t"
688         "movq %%mm2, (%2, %3)           \n\t"
689         "movq (%1), %%mm0               \n\t"
690         "movq (%1, %3), %%mm2           \n\t"
691         PAVGB" 1(%1), %%mm0             \n\t"
692         PAVGB" 1(%1, %3), %%mm2         \n\t"
693         "add %%"REG_a", %2              \n\t"
694         "add %%"REG_a", %1              \n\t"
695         PAVGB" (%2), %%mm0              \n\t"
696         PAVGB" (%2, %3), %%mm2          \n\t"
697         "movq %%mm0, (%2)               \n\t"
698         "movq %%mm2, (%2, %3)           \n\t"
699         "add %%"REG_a", %2              \n\t"
700         "subl $4, %0                    \n\t"
701         "jnz 1b                         \n\t"
702         :"+g"(h), "+S"(pixels), "+D"(block)
703         :"r" ((long)line_size)
704         :"%"REG_a, "memory");
705 }
706
707 static void DEF(avg_pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
708 {
709     __asm __volatile(
710         "lea (%3, %3), %%"REG_a"        \n\t"
711         "movq (%1), %%mm0               \n\t"
712         "sub %3, %2                     \n\t"
713         "1:                             \n\t"
714         "movq (%1, %3), %%mm1           \n\t"
715         "movq (%1, %%"REG_a"), %%mm2    \n\t"
716         "add %%"REG_a", %1              \n\t"
717         PAVGB" %%mm1, %%mm0             \n\t"
718         PAVGB" %%mm2, %%mm1             \n\t"
719         "movq (%2, %3), %%mm3           \n\t"
720         "movq (%2, %%"REG_a"), %%mm4    \n\t"
721         PAVGB" %%mm3, %%mm0             \n\t"
722         PAVGB" %%mm4, %%mm1             \n\t"
723         "movq %%mm0, (%2, %3)           \n\t"
724         "movq %%mm1, (%2, %%"REG_a")    \n\t"
725         "movq (%1, %3), %%mm1           \n\t"
726         "movq (%1, %%"REG_a"), %%mm0    \n\t"
727         PAVGB" %%mm1, %%mm2             \n\t"
728         PAVGB" %%mm0, %%mm1             \n\t"
729         "add %%"REG_a", %2              \n\t"
730         "add %%"REG_a", %1              \n\t"
731         "movq (%2, %3), %%mm3           \n\t"
732         "movq (%2, %%"REG_a"), %%mm4    \n\t"
733         PAVGB" %%mm3, %%mm2             \n\t"
734         PAVGB" %%mm4, %%mm1             \n\t"
735         "movq %%mm2, (%2, %3)           \n\t"
736         "movq %%mm1, (%2, %%"REG_a")    \n\t"
737         "add %%"REG_a", %2              \n\t"
738         "subl $4, %0                    \n\t"
739         "jnz 1b                         \n\t"
740         :"+g"(h), "+S"(pixels), "+D"(block)
741         :"r" ((long)line_size)
742         :"%"REG_a, "memory");
743 }
744
745 // Note this is not correctly rounded, but this function is only used for b frames so it doesnt matter 
746 static void DEF(avg_pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
747 {
748     MOVQ_BONE(mm6);
749     __asm __volatile(
750         "lea (%3, %3), %%"REG_a"        \n\t"
751         "movq (%1), %%mm0               \n\t"
752         PAVGB" 1(%1), %%mm0             \n\t"
753         ".balign 8                      \n\t"
754         "1:                             \n\t"
755         "movq (%1, %%"REG_a"), %%mm2    \n\t"
756         "movq (%1, %3), %%mm1           \n\t"
757         "psubusb %%mm6, %%mm2           \n\t"
758         PAVGB" 1(%1, %3), %%mm1         \n\t"
759         PAVGB" 1(%1, %%"REG_a"), %%mm2  \n\t"
760         "add %%"REG_a", %1              \n\t"
761         PAVGB" %%mm1, %%mm0             \n\t"
762         PAVGB" %%mm2, %%mm1             \n\t"
763         PAVGB" (%2), %%mm0              \n\t"
764         PAVGB" (%2, %3), %%mm1          \n\t"
765         "movq %%mm0, (%2)               \n\t"
766         "movq %%mm1, (%2, %3)           \n\t"
767         "movq (%1, %3), %%mm1           \n\t"
768         "movq (%1, %%"REG_a"), %%mm0    \n\t"
769         PAVGB" 1(%1, %3), %%mm1         \n\t"
770         PAVGB" 1(%1, %%"REG_a"), %%mm0  \n\t"
771         "add %%"REG_a", %2              \n\t"
772         "add %%"REG_a", %1              \n\t"
773         PAVGB" %%mm1, %%mm2             \n\t"
774         PAVGB" %%mm0, %%mm1             \n\t"
775         PAVGB" (%2), %%mm2              \n\t"
776         PAVGB" (%2, %3), %%mm1          \n\t"
777         "movq %%mm2, (%2)               \n\t"
778         "movq %%mm1, (%2, %3)           \n\t"
779         "add %%"REG_a", %2              \n\t"
780         "subl $4, %0                    \n\t"
781         "jnz 1b                         \n\t"
782         :"+g"(h), "+S"(pixels), "+D"(block)
783         :"r" ((long)line_size)
784         :"%"REG_a,  "memory");
785 }
786
787 //FIXME the following could be optimized too ...
788 static void DEF(put_no_rnd_pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
789     DEF(put_no_rnd_pixels8_x2)(block  , pixels  , line_size, h);
790     DEF(put_no_rnd_pixels8_x2)(block+8, pixels+8, line_size, h);
791 }
792 static void DEF(put_pixels16_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
793     DEF(put_pixels8_y2)(block  , pixels  , line_size, h);
794     DEF(put_pixels8_y2)(block+8, pixels+8, line_size, h);
795 }
796 static void DEF(put_no_rnd_pixels16_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
797     DEF(put_no_rnd_pixels8_y2)(block  , pixels  , line_size, h);
798     DEF(put_no_rnd_pixels8_y2)(block+8, pixels+8, line_size, h);
799 }
800 static void DEF(avg_pixels16)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
801     DEF(avg_pixels8)(block  , pixels  , line_size, h);
802     DEF(avg_pixels8)(block+8, pixels+8, line_size, h);
803 }
804 static void DEF(avg_pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
805     DEF(avg_pixels8_x2)(block  , pixels  , line_size, h);
806     DEF(avg_pixels8_x2)(block+8, pixels+8, line_size, h);
807 }
808 static void DEF(avg_pixels16_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
809     DEF(avg_pixels8_y2)(block  , pixels  , line_size, h);
810     DEF(avg_pixels8_y2)(block+8, pixels+8, line_size, h);
811 }
812 static void DEF(avg_pixels16_xy2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
813     DEF(avg_pixels8_xy2)(block  , pixels  , line_size, h);
814     DEF(avg_pixels8_xy2)(block+8, pixels+8, line_size, h);
815 }
816