2 * DSP utils : average functions are compiled twice for 3dnow/mmx2
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 * Copyright (c) 2002 Michael Niedermayer
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.
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.
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
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>
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
28 static void DEF(put_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
31 "lea (%3, %3), %%eax \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"
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"
46 "movq %%mm0, (%2) \n\t"
47 "movq %%mm1, (%2, %3) \n\t"
51 :"+g"(h), "+S"(pixels), "+D"(block)
56 static void DEF(put_pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
60 "movq (%1), %%mm0 \n\t"
62 "movq (%1), %%mm1 \n\t"
64 PAVGB" (%2), %%mm0 \n\t"
65 PAVGB" 8(%2), %%mm1 \n\t"
66 "movq %%mm0, (%3) \n\t"
68 "movq %%mm1, (%3) \n\t"
70 "movq (%1), %%mm0 \n\t"
72 "movq (%1), %%mm1 \n\t"
74 PAVGB" 16(%2), %%mm0 \n\t"
75 PAVGB" 24(%2), %%mm1 \n\t"
76 "movq %%mm0, (%3) \n\t"
78 "movq %%mm1, (%3) \n\t"
83 :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst)
84 :"r"(src1Stride), "r"(dstStride)
88 static void DEF(put_pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
91 "lea (%3, %3), %%eax \n\t"
93 "movq (%1), %%mm0 \n\t"
94 "movq (%1, %3), %%mm1 \n\t"
95 "movq 8(%1), %%mm2 \n\t"
96 "movq 8(%1, %3), %%mm3 \n\t"
97 PAVGB" 1(%1), %%mm0 \n\t"
98 PAVGB" 1(%1, %3), %%mm1 \n\t"
99 PAVGB" 9(%1), %%mm2 \n\t"
100 PAVGB" 9(%1, %3), %%mm3 \n\t"
101 "movq %%mm0, (%2) \n\t"
102 "movq %%mm1, (%2, %3) \n\t"
103 "movq %%mm2, 8(%2) \n\t"
104 "movq %%mm3, 8(%2, %3) \n\t"
105 "addl %%eax, %1 \n\t"
106 "addl %%eax, %2 \n\t"
107 "movq (%1), %%mm0 \n\t"
108 "movq (%1, %3), %%mm1 \n\t"
109 "movq 8(%1), %%mm2 \n\t"
110 "movq 8(%1, %3), %%mm3 \n\t"
111 PAVGB" 1(%1), %%mm0 \n\t"
112 PAVGB" 1(%1, %3), %%mm1 \n\t"
113 PAVGB" 9(%1), %%mm2 \n\t"
114 PAVGB" 9(%1, %3), %%mm3 \n\t"
115 "addl %%eax, %1 \n\t"
116 "movq %%mm0, (%2) \n\t"
117 "movq %%mm1, (%2, %3) \n\t"
118 "movq %%mm2, 8(%2) \n\t"
119 "movq %%mm3, 8(%2, %3) \n\t"
120 "addl %%eax, %2 \n\t"
123 :"+g"(h), "+S"(pixels), "+D"(block)
128 static void DEF(put_pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
132 "movq (%1), %%mm0 \n\t"
133 "movq 8(%1), %%mm1 \n\t"
135 PAVGB" (%2), %%mm0 \n\t"
136 PAVGB" 8(%2), %%mm1 \n\t"
137 "movq %%mm0, (%3) \n\t"
138 "movq %%mm1, 8(%3) \n\t"
140 "movq (%1), %%mm0 \n\t"
141 "movq 8(%1), %%mm1 \n\t"
143 PAVGB" 16(%2), %%mm0 \n\t"
144 PAVGB" 24(%2), %%mm1 \n\t"
145 "movq %%mm0, (%3) \n\t"
146 "movq %%mm1, 8(%3) \n\t"
151 :"+g"(h), "+r"(src1), "+r"(src2), "+r"(dst)
152 :"r"(src1Stride), "r"(dstStride)
156 /* GL: this function does incorrect rounding if overflow */
157 static void DEF(put_no_rnd_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
161 "lea (%3, %3), %%eax \n\t"
163 "movq (%1), %%mm0 \n\t"
164 "movq (%1, %3), %%mm2 \n\t"
165 "movq 1(%1), %%mm1 \n\t"
166 "movq 1(%1, %3), %%mm3 \n\t"
167 "addl %%eax, %1 \n\t"
168 "psubusb %%mm6, %%mm0 \n\t"
169 "psubusb %%mm6, %%mm2 \n\t"
170 PAVGB" %%mm1, %%mm0 \n\t"
171 PAVGB" %%mm3, %%mm2 \n\t"
172 "movq %%mm0, (%2) \n\t"
173 "movq %%mm2, (%2, %3) \n\t"
174 "movq (%1), %%mm0 \n\t"
175 "movq 1(%1), %%mm1 \n\t"
176 "movq (%1, %3), %%mm2 \n\t"
177 "movq 1(%1, %3), %%mm3 \n\t"
178 "addl %%eax, %2 \n\t"
179 "addl %%eax, %1 \n\t"
180 "psubusb %%mm6, %%mm0 \n\t"
181 "psubusb %%mm6, %%mm2 \n\t"
182 PAVGB" %%mm1, %%mm0 \n\t"
183 PAVGB" %%mm3, %%mm2 \n\t"
184 "movq %%mm0, (%2) \n\t"
185 "movq %%mm2, (%2, %3) \n\t"
186 "addl %%eax, %2 \n\t"
189 :"+g"(h), "+S"(pixels), "+D"(block)
194 static void DEF(put_pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
197 "lea (%3, %3), %%eax \n\t"
198 "movq (%1), %%mm0 \n\t"
201 "movq (%1, %3), %%mm1 \n\t"
202 "movq (%1, %%eax), %%mm2 \n\t"
203 "addl %%eax, %1 \n\t"
204 PAVGB" %%mm1, %%mm0 \n\t"
205 PAVGB" %%mm2, %%mm1 \n\t"
206 "movq %%mm0, (%2, %3) \n\t"
207 "movq %%mm1, (%2, %%eax) \n\t"
208 "movq (%1, %3), %%mm1 \n\t"
209 "movq (%1, %%eax), %%mm0 \n\t"
210 "addl %%eax, %2 \n\t"
211 "addl %%eax, %1 \n\t"
212 PAVGB" %%mm1, %%mm2 \n\t"
213 PAVGB" %%mm0, %%mm1 \n\t"
214 "movq %%mm2, (%2, %3) \n\t"
215 "movq %%mm1, (%2, %%eax) \n\t"
216 "addl %%eax, %2 \n\t"
219 :"+g"(h), "+S"(pixels), "+D" (block)
224 /* GL: this function does incorrect rounding if overflow */
225 static void DEF(put_no_rnd_pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
229 "lea (%3, %3), %%eax \n\t"
230 "movq (%1), %%mm0 \n\t"
233 "movq (%1, %3), %%mm1 \n\t"
234 "movq (%1, %%eax), %%mm2 \n\t"
235 "addl %%eax, %1 \n\t"
236 "psubusb %%mm6, %%mm1 \n\t"
237 PAVGB" %%mm1, %%mm0 \n\t"
238 PAVGB" %%mm2, %%mm1 \n\t"
239 "movq %%mm0, (%2, %3) \n\t"
240 "movq %%mm1, (%2, %%eax) \n\t"
241 "movq (%1, %3), %%mm1 \n\t"
242 "movq (%1, %%eax), %%mm0 \n\t"
243 "addl %%eax, %2 \n\t"
244 "addl %%eax, %1 \n\t"
245 "psubusb %%mm6, %%mm1 \n\t"
246 PAVGB" %%mm1, %%mm2 \n\t"
247 PAVGB" %%mm0, %%mm1 \n\t"
248 "movq %%mm2, (%2, %3) \n\t"
249 "movq %%mm1, (%2, %%eax) \n\t"
250 "addl %%eax, %2 \n\t"
253 :"+g"(h), "+S"(pixels), "+D" (block)
258 static void DEF(avg_pixels8)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
261 "lea (%3, %3), %%eax \n\t"
263 "movq (%2), %%mm0 \n\t"
264 "movq (%2, %3), %%mm1 \n\t"
265 PAVGB" (%1), %%mm0 \n\t"
266 PAVGB" (%1, %3), %%mm1 \n\t"
267 "movq %%mm0, (%2) \n\t"
268 "movq %%mm1, (%2, %3) \n\t"
269 "addl %%eax, %1 \n\t"
270 "addl %%eax, %2 \n\t"
271 "movq (%2), %%mm0 \n\t"
272 "movq (%2, %3), %%mm1 \n\t"
273 PAVGB" (%1), %%mm0 \n\t"
274 PAVGB" (%1, %3), %%mm1 \n\t"
275 "addl %%eax, %1 \n\t"
276 "movq %%mm0, (%2) \n\t"
277 "movq %%mm1, (%2, %3) \n\t"
278 "addl %%eax, %2 \n\t"
281 :"+g"(h), "+S"(pixels), "+D"(block)
286 static void DEF(avg_pixels8_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
289 "lea (%3, %3), %%eax \n\t"
291 "movq (%1), %%mm0 \n\t"
292 "movq (%1, %3), %%mm2 \n\t"
293 PAVGB" 1(%1), %%mm0 \n\t"
294 PAVGB" 1(%1, %3), %%mm2 \n\t"
295 PAVGB" (%2), %%mm0 \n\t"
296 PAVGB" (%2, %3), %%mm2 \n\t"
297 "addl %%eax, %1 \n\t"
298 "movq %%mm0, (%2) \n\t"
299 "movq %%mm2, (%2, %3) \n\t"
300 "movq (%1), %%mm0 \n\t"
301 "movq (%1, %3), %%mm2 \n\t"
302 PAVGB" 1(%1), %%mm0 \n\t"
303 PAVGB" 1(%1, %3), %%mm2 \n\t"
304 "addl %%eax, %2 \n\t"
305 "addl %%eax, %1 \n\t"
306 PAVGB" (%2), %%mm0 \n\t"
307 PAVGB" (%2, %3), %%mm2 \n\t"
308 "movq %%mm0, (%2) \n\t"
309 "movq %%mm2, (%2, %3) \n\t"
310 "addl %%eax, %2 \n\t"
313 :"+g"(h), "+S"(pixels), "+D"(block)
318 static void DEF(avg_pixels8_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
321 "lea (%3, %3), %%eax \n\t"
322 "movq (%1), %%mm0 \n\t"
325 "movq (%1, %3), %%mm1 \n\t"
326 "movq (%1, %%eax), %%mm2 \n\t"
327 "addl %%eax, %1 \n\t"
328 PAVGB" %%mm1, %%mm0 \n\t"
329 PAVGB" %%mm2, %%mm1 \n\t"
330 "movq (%2, %3), %%mm3 \n\t"
331 "movq (%2, %%eax), %%mm4 \n\t"
332 PAVGB" %%mm3, %%mm0 \n\t"
333 PAVGB" %%mm4, %%mm1 \n\t"
334 "movq %%mm0, (%2, %3) \n\t"
335 "movq %%mm1, (%2, %%eax) \n\t"
336 "movq (%1, %3), %%mm1 \n\t"
337 "movq (%1, %%eax), %%mm0 \n\t"
338 PAVGB" %%mm1, %%mm2 \n\t"
339 PAVGB" %%mm0, %%mm1 \n\t"
340 "addl %%eax, %2 \n\t"
341 "addl %%eax, %1 \n\t"
342 "movq (%2, %3), %%mm3 \n\t"
343 "movq (%2, %%eax), %%mm4 \n\t"
344 PAVGB" %%mm3, %%mm2 \n\t"
345 PAVGB" %%mm4, %%mm1 \n\t"
346 "movq %%mm2, (%2, %3) \n\t"
347 "movq %%mm1, (%2, %%eax) \n\t"
348 "addl %%eax, %2 \n\t"
351 :"+g"(h), "+S"(pixels), "+D"(block)
356 // Note this is not correctly rounded, but this function is only used for b frames so it doesnt matter
357 static void DEF(avg_pixels8_xy2)(uint8_t *block, const uint8_t *pixels, int line_size, int h)
361 "lea (%3, %3), %%eax \n\t"
362 "movq (%1), %%mm0 \n\t"
363 PAVGB" 1(%1), %%mm0 \n\t"
366 "movq (%1, %%eax), %%mm2 \n\t"
367 "movq (%1, %3), %%mm1 \n\t"
368 "psubusb %%mm6, %%mm2 \n\t"
369 PAVGB" 1(%1, %3), %%mm1 \n\t"
370 PAVGB" 1(%1, %%eax), %%mm2 \n\t"
371 "addl %%eax, %1 \n\t"
372 PAVGB" %%mm1, %%mm0 \n\t"
373 PAVGB" %%mm2, %%mm1 \n\t"
374 PAVGB" (%2), %%mm0 \n\t"
375 PAVGB" (%2, %3), %%mm1 \n\t"
376 "movq %%mm0, (%2) \n\t"
377 "movq %%mm1, (%2, %3) \n\t"
378 "movq (%1, %3), %%mm1 \n\t"
379 "movq (%1, %%eax), %%mm0 \n\t"
380 PAVGB" 1(%1, %3), %%mm1 \n\t"
381 PAVGB" 1(%1, %%eax), %%mm0 \n\t"
382 "addl %%eax, %2 \n\t"
383 "addl %%eax, %1 \n\t"
384 PAVGB" %%mm1, %%mm2 \n\t"
385 PAVGB" %%mm0, %%mm1 \n\t"
386 PAVGB" (%2), %%mm2 \n\t"
387 PAVGB" (%2, %3), %%mm1 \n\t"
388 "movq %%mm2, (%2) \n\t"
389 "movq %%mm1, (%2, %3) \n\t"
390 "addl %%eax, %2 \n\t"
393 :"+g"(h), "+S"(pixels), "+D"(block)
398 //FIXME the following could be optimized too ...
399 static void DEF(put_no_rnd_pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
400 DEF(put_no_rnd_pixels8_x2)(block , pixels , line_size, h);
401 DEF(put_no_rnd_pixels8_x2)(block+8, pixels+8, line_size, h);
403 static void DEF(put_pixels16_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
404 DEF(put_pixels8_y2)(block , pixels , line_size, h);
405 DEF(put_pixels8_y2)(block+8, pixels+8, line_size, h);
407 static void DEF(put_no_rnd_pixels16_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
408 DEF(put_no_rnd_pixels8_y2)(block , pixels , line_size, h);
409 DEF(put_no_rnd_pixels8_y2)(block+8, pixels+8, line_size, h);
411 static void DEF(avg_pixels16)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
412 DEF(avg_pixels8)(block , pixels , line_size, h);
413 DEF(avg_pixels8)(block+8, pixels+8, line_size, h);
415 static void DEF(avg_pixels16_x2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
416 DEF(avg_pixels8_x2)(block , pixels , line_size, h);
417 DEF(avg_pixels8_x2)(block+8, pixels+8, line_size, h);
419 static void DEF(avg_pixels16_y2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
420 DEF(avg_pixels8_y2)(block , pixels , line_size, h);
421 DEF(avg_pixels8_y2)(block+8, pixels+8, line_size, h);
423 static void DEF(avg_pixels16_xy2)(uint8_t *block, const uint8_t *pixels, int line_size, int h){
424 DEF(avg_pixels8_xy2)(block , pixels , line_size, h);
425 DEF(avg_pixels8_xy2)(block+8, pixels+8, line_size, h);