Merge branch 'mirror' into vdpau
[FFMpeg-mirror/ffmpeg-vdpau.git] / libavcodec / ppc / dsputil_altivec.c
blob6ff219e3b035dbf0357130dc7f9aa10bc8519b35
1 /*
2 * Copyright (c) 2002 Brian Foley
3 * Copyright (c) 2002 Dieter Shirley
4 * Copyright (c) 2003-2004 Romain Dolbeau <romain@dolbeau.org>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavcodec/dsputil.h"
25 #include "gcc_fixes.h"
27 #include "dsputil_ppc.h"
28 #include "util_altivec.h"
30 int sad16_x2_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
32 int i;
33 DECLARE_ALIGNED_16(int, s);
34 const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
35 vector unsigned char *tv;
36 vector unsigned char pix1v, pix2v, pix2iv, avgv, t5;
37 vector unsigned int sad;
38 vector signed int sumdiffs;
40 s = 0;
41 sad = (vector unsigned int)vec_splat_u32(0);
42 for (i = 0; i < h; i++) {
43 /* Read unaligned pixels into our vectors. The vectors are as follows:
44 pix1v: pix1[0]-pix1[15]
45 pix2v: pix2[0]-pix2[15] pix2iv: pix2[1]-pix2[16] */
46 tv = (vector unsigned char *) pix1;
47 pix1v = vec_perm(tv[0], tv[1], vec_lvsl(0, pix1));
49 tv = (vector unsigned char *) &pix2[0];
50 pix2v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[0]));
52 tv = (vector unsigned char *) &pix2[1];
53 pix2iv = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[1]));
55 /* Calculate the average vector */
56 avgv = vec_avg(pix2v, pix2iv);
58 /* Calculate a sum of abs differences vector */
59 t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
61 /* Add each 4 pixel group together and put 4 results into sad */
62 sad = vec_sum4s(t5, sad);
64 pix1 += line_size;
65 pix2 += line_size;
67 /* Sum up the four partial sums, and put the result into s */
68 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
69 sumdiffs = vec_splat(sumdiffs, 3);
70 vec_ste(sumdiffs, 0, &s);
72 return s;
75 int sad16_y2_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
77 int i;
78 DECLARE_ALIGNED_16(int, s);
79 const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
80 vector unsigned char *tv;
81 vector unsigned char pix1v, pix2v, pix3v, avgv, t5;
82 vector unsigned int sad;
83 vector signed int sumdiffs;
84 uint8_t *pix3 = pix2 + line_size;
86 s = 0;
87 sad = (vector unsigned int)vec_splat_u32(0);
89 /* Due to the fact that pix3 = pix2 + line_size, the pix3 of one
90 iteration becomes pix2 in the next iteration. We can use this
91 fact to avoid a potentially expensive unaligned read, each
92 time around the loop.
93 Read unaligned pixels into our vectors. The vectors are as follows:
94 pix2v: pix2[0]-pix2[15]
95 Split the pixel vectors into shorts */
96 tv = (vector unsigned char *) &pix2[0];
97 pix2v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[0]));
99 for (i = 0; i < h; i++) {
100 /* Read unaligned pixels into our vectors. The vectors are as follows:
101 pix1v: pix1[0]-pix1[15]
102 pix3v: pix3[0]-pix3[15] */
103 tv = (vector unsigned char *) pix1;
104 pix1v = vec_perm(tv[0], tv[1], vec_lvsl(0, pix1));
106 tv = (vector unsigned char *) &pix3[0];
107 pix3v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix3[0]));
109 /* Calculate the average vector */
110 avgv = vec_avg(pix2v, pix3v);
112 /* Calculate a sum of abs differences vector */
113 t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
115 /* Add each 4 pixel group together and put 4 results into sad */
116 sad = vec_sum4s(t5, sad);
118 pix1 += line_size;
119 pix2v = pix3v;
120 pix3 += line_size;
124 /* Sum up the four partial sums, and put the result into s */
125 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
126 sumdiffs = vec_splat(sumdiffs, 3);
127 vec_ste(sumdiffs, 0, &s);
128 return s;
131 int sad16_xy2_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
133 int i;
134 DECLARE_ALIGNED_16(int, s);
135 uint8_t *pix3 = pix2 + line_size;
136 const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
137 const vector unsigned short two = (const vector unsigned short)vec_splat_u16(2);
138 vector unsigned char *tv, avgv, t5;
139 vector unsigned char pix1v, pix2v, pix3v, pix2iv, pix3iv;
140 vector unsigned short pix2lv, pix2hv, pix2ilv, pix2ihv;
141 vector unsigned short pix3lv, pix3hv, pix3ilv, pix3ihv;
142 vector unsigned short avghv, avglv;
143 vector unsigned short t1, t2, t3, t4;
144 vector unsigned int sad;
145 vector signed int sumdiffs;
147 sad = (vector unsigned int)vec_splat_u32(0);
149 s = 0;
151 /* Due to the fact that pix3 = pix2 + line_size, the pix3 of one
152 iteration becomes pix2 in the next iteration. We can use this
153 fact to avoid a potentially expensive unaligned read, as well
154 as some splitting, and vector addition each time around the loop.
155 Read unaligned pixels into our vectors. The vectors are as follows:
156 pix2v: pix2[0]-pix2[15] pix2iv: pix2[1]-pix2[16]
157 Split the pixel vectors into shorts */
158 tv = (vector unsigned char *) &pix2[0];
159 pix2v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[0]));
161 tv = (vector unsigned char *) &pix2[1];
162 pix2iv = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[1]));
164 pix2hv = (vector unsigned short) vec_mergeh(zero, pix2v);
165 pix2lv = (vector unsigned short) vec_mergel(zero, pix2v);
166 pix2ihv = (vector unsigned short) vec_mergeh(zero, pix2iv);
167 pix2ilv = (vector unsigned short) vec_mergel(zero, pix2iv);
168 t1 = vec_add(pix2hv, pix2ihv);
169 t2 = vec_add(pix2lv, pix2ilv);
171 for (i = 0; i < h; i++) {
172 /* Read unaligned pixels into our vectors. The vectors are as follows:
173 pix1v: pix1[0]-pix1[15]
174 pix3v: pix3[0]-pix3[15] pix3iv: pix3[1]-pix3[16] */
175 tv = (vector unsigned char *) pix1;
176 pix1v = vec_perm(tv[0], tv[1], vec_lvsl(0, pix1));
178 tv = (vector unsigned char *) &pix3[0];
179 pix3v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix3[0]));
181 tv = (vector unsigned char *) &pix3[1];
182 pix3iv = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix3[1]));
184 /* Note that AltiVec does have vec_avg, but this works on vector pairs
185 and rounds up. We could do avg(avg(a,b),avg(c,d)), but the rounding
186 would mean that, for example, avg(3,0,0,1) = 2, when it should be 1.
187 Instead, we have to split the pixel vectors into vectors of shorts,
188 and do the averaging by hand. */
190 /* Split the pixel vectors into shorts */
191 pix3hv = (vector unsigned short) vec_mergeh(zero, pix3v);
192 pix3lv = (vector unsigned short) vec_mergel(zero, pix3v);
193 pix3ihv = (vector unsigned short) vec_mergeh(zero, pix3iv);
194 pix3ilv = (vector unsigned short) vec_mergel(zero, pix3iv);
196 /* Do the averaging on them */
197 t3 = vec_add(pix3hv, pix3ihv);
198 t4 = vec_add(pix3lv, pix3ilv);
200 avghv = vec_sr(vec_add(vec_add(t1, t3), two), two);
201 avglv = vec_sr(vec_add(vec_add(t2, t4), two), two);
203 /* Pack the shorts back into a result */
204 avgv = vec_pack(avghv, avglv);
206 /* Calculate a sum of abs differences vector */
207 t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
209 /* Add each 4 pixel group together and put 4 results into sad */
210 sad = vec_sum4s(t5, sad);
212 pix1 += line_size;
213 pix3 += line_size;
214 /* Transfer the calculated values for pix3 into pix2 */
215 t1 = t3;
216 t2 = t4;
218 /* Sum up the four partial sums, and put the result into s */
219 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
220 sumdiffs = vec_splat(sumdiffs, 3);
221 vec_ste(sumdiffs, 0, &s);
223 return s;
226 int sad16_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
228 int i;
229 DECLARE_ALIGNED_16(int, s);
230 const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
231 vector unsigned char perm1, perm2, *pix1v, *pix2v;
232 vector unsigned char t1, t2, t3,t4, t5;
233 vector unsigned int sad;
234 vector signed int sumdiffs;
236 sad = (vector unsigned int)vec_splat_u32(0);
239 for (i = 0; i < h; i++) {
240 /* Read potentially unaligned pixels into t1 and t2 */
241 perm1 = vec_lvsl(0, pix1);
242 pix1v = (vector unsigned char *) pix1;
243 perm2 = vec_lvsl(0, pix2);
244 pix2v = (vector unsigned char *) pix2;
245 t1 = vec_perm(pix1v[0], pix1v[1], perm1);
246 t2 = vec_perm(pix2v[0], pix2v[1], perm2);
248 /* Calculate a sum of abs differences vector */
249 t3 = vec_max(t1, t2);
250 t4 = vec_min(t1, t2);
251 t5 = vec_sub(t3, t4);
253 /* Add each 4 pixel group together and put 4 results into sad */
254 sad = vec_sum4s(t5, sad);
256 pix1 += line_size;
257 pix2 += line_size;
260 /* Sum up the four partial sums, and put the result into s */
261 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
262 sumdiffs = vec_splat(sumdiffs, 3);
263 vec_ste(sumdiffs, 0, &s);
265 return s;
268 int sad8_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
270 int i;
271 DECLARE_ALIGNED_16(int, s);
272 const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
273 vector unsigned char perm1, perm2, permclear, *pix1v, *pix2v;
274 vector unsigned char t1, t2, t3,t4, t5;
275 vector unsigned int sad;
276 vector signed int sumdiffs;
278 sad = (vector unsigned int)vec_splat_u32(0);
280 permclear = (vector unsigned char){255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0};
282 for (i = 0; i < h; i++) {
283 /* Read potentially unaligned pixels into t1 and t2
284 Since we're reading 16 pixels, and actually only want 8,
285 mask out the last 8 pixels. The 0s don't change the sum. */
286 perm1 = vec_lvsl(0, pix1);
287 pix1v = (vector unsigned char *) pix1;
288 perm2 = vec_lvsl(0, pix2);
289 pix2v = (vector unsigned char *) pix2;
290 t1 = vec_and(vec_perm(pix1v[0], pix1v[1], perm1), permclear);
291 t2 = vec_and(vec_perm(pix2v[0], pix2v[1], perm2), permclear);
293 /* Calculate a sum of abs differences vector */
294 t3 = vec_max(t1, t2);
295 t4 = vec_min(t1, t2);
296 t5 = vec_sub(t3, t4);
298 /* Add each 4 pixel group together and put 4 results into sad */
299 sad = vec_sum4s(t5, sad);
301 pix1 += line_size;
302 pix2 += line_size;
305 /* Sum up the four partial sums, and put the result into s */
306 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
307 sumdiffs = vec_splat(sumdiffs, 3);
308 vec_ste(sumdiffs, 0, &s);
310 return s;
313 int pix_norm1_altivec(uint8_t *pix, int line_size)
315 int i;
316 DECLARE_ALIGNED_16(int, s);
317 const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
318 vector unsigned char *tv;
319 vector unsigned char pixv;
320 vector unsigned int sv;
321 vector signed int sum;
323 sv = (vector unsigned int)vec_splat_u32(0);
325 s = 0;
326 for (i = 0; i < 16; i++) {
327 /* Read in the potentially unaligned pixels */
328 tv = (vector unsigned char *) pix;
329 pixv = vec_perm(tv[0], tv[1], vec_lvsl(0, pix));
331 /* Square the values, and add them to our sum */
332 sv = vec_msum(pixv, pixv, sv);
334 pix += line_size;
336 /* Sum up the four partial sums, and put the result into s */
337 sum = vec_sums((vector signed int) sv, (vector signed int) zero);
338 sum = vec_splat(sum, 3);
339 vec_ste(sum, 0, &s);
341 return s;
345 * Sum of Squared Errors for a 8x8 block.
346 * AltiVec-enhanced.
347 * It's the sad8_altivec code above w/ squaring added.
349 int sse8_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
351 int i;
352 DECLARE_ALIGNED_16(int, s);
353 const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
354 vector unsigned char perm1, perm2, permclear, *pix1v, *pix2v;
355 vector unsigned char t1, t2, t3,t4, t5;
356 vector unsigned int sum;
357 vector signed int sumsqr;
359 sum = (vector unsigned int)vec_splat_u32(0);
361 permclear = (vector unsigned char){255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0};
364 for (i = 0; i < h; i++) {
365 /* Read potentially unaligned pixels into t1 and t2
366 Since we're reading 16 pixels, and actually only want 8,
367 mask out the last 8 pixels. The 0s don't change the sum. */
368 perm1 = vec_lvsl(0, pix1);
369 pix1v = (vector unsigned char *) pix1;
370 perm2 = vec_lvsl(0, pix2);
371 pix2v = (vector unsigned char *) pix2;
372 t1 = vec_and(vec_perm(pix1v[0], pix1v[1], perm1), permclear);
373 t2 = vec_and(vec_perm(pix2v[0], pix2v[1], perm2), permclear);
375 /* Since we want to use unsigned chars, we can take advantage
376 of the fact that abs(a-b)^2 = (a-b)^2. */
378 /* Calculate abs differences vector */
379 t3 = vec_max(t1, t2);
380 t4 = vec_min(t1, t2);
381 t5 = vec_sub(t3, t4);
383 /* Square the values and add them to our sum */
384 sum = vec_msum(t5, t5, sum);
386 pix1 += line_size;
387 pix2 += line_size;
390 /* Sum up the four partial sums, and put the result into s */
391 sumsqr = vec_sums((vector signed int) sum, (vector signed int) zero);
392 sumsqr = vec_splat(sumsqr, 3);
393 vec_ste(sumsqr, 0, &s);
395 return s;
399 * Sum of Squared Errors for a 16x16 block.
400 * AltiVec-enhanced.
401 * It's the sad16_altivec code above w/ squaring added.
403 int sse16_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
405 int i;
406 DECLARE_ALIGNED_16(int, s);
407 const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
408 vector unsigned char perm1, perm2, *pix1v, *pix2v;
409 vector unsigned char t1, t2, t3,t4, t5;
410 vector unsigned int sum;
411 vector signed int sumsqr;
413 sum = (vector unsigned int)vec_splat_u32(0);
415 for (i = 0; i < h; i++) {
416 /* Read potentially unaligned pixels into t1 and t2 */
417 perm1 = vec_lvsl(0, pix1);
418 pix1v = (vector unsigned char *) pix1;
419 perm2 = vec_lvsl(0, pix2);
420 pix2v = (vector unsigned char *) pix2;
421 t1 = vec_perm(pix1v[0], pix1v[1], perm1);
422 t2 = vec_perm(pix2v[0], pix2v[1], perm2);
424 /* Since we want to use unsigned chars, we can take advantage
425 of the fact that abs(a-b)^2 = (a-b)^2. */
427 /* Calculate abs differences vector */
428 t3 = vec_max(t1, t2);
429 t4 = vec_min(t1, t2);
430 t5 = vec_sub(t3, t4);
432 /* Square the values and add them to our sum */
433 sum = vec_msum(t5, t5, sum);
435 pix1 += line_size;
436 pix2 += line_size;
439 /* Sum up the four partial sums, and put the result into s */
440 sumsqr = vec_sums((vector signed int) sum, (vector signed int) zero);
441 sumsqr = vec_splat(sumsqr, 3);
442 vec_ste(sumsqr, 0, &s);
444 return s;
447 int pix_sum_altivec(uint8_t * pix, int line_size)
449 const vector unsigned int zero = (const vector unsigned int)vec_splat_u32(0);
450 vector unsigned char perm, *pixv;
451 vector unsigned char t1;
452 vector unsigned int sad;
453 vector signed int sumdiffs;
455 int i;
456 DECLARE_ALIGNED_16(int, s);
458 sad = (vector unsigned int)vec_splat_u32(0);
460 for (i = 0; i < 16; i++) {
461 /* Read the potentially unaligned 16 pixels into t1 */
462 perm = vec_lvsl(0, pix);
463 pixv = (vector unsigned char *) pix;
464 t1 = vec_perm(pixv[0], pixv[1], perm);
466 /* Add each 4 pixel group together and put 4 results into sad */
467 sad = vec_sum4s(t1, sad);
469 pix += line_size;
472 /* Sum up the four partial sums, and put the result into s */
473 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
474 sumdiffs = vec_splat(sumdiffs, 3);
475 vec_ste(sumdiffs, 0, &s);
477 return s;
480 void get_pixels_altivec(DCTELEM *restrict block, const uint8_t *pixels, int line_size)
482 int i;
483 vector unsigned char perm, bytes, *pixv;
484 const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
485 vector signed short shorts;
487 for (i = 0; i < 8; i++) {
488 // Read potentially unaligned pixels.
489 // We're reading 16 pixels, and actually only want 8,
490 // but we simply ignore the extras.
491 perm = vec_lvsl(0, pixels);
492 pixv = (vector unsigned char *) pixels;
493 bytes = vec_perm(pixv[0], pixv[1], perm);
495 // convert the bytes into shorts
496 shorts = (vector signed short)vec_mergeh(zero, bytes);
498 // save the data to the block, we assume the block is 16-byte aligned
499 vec_st(shorts, i*16, (vector signed short*)block);
501 pixels += line_size;
505 void diff_pixels_altivec(DCTELEM *restrict block, const uint8_t *s1,
506 const uint8_t *s2, int stride)
508 int i;
509 vector unsigned char perm, bytes, *pixv;
510 const vector unsigned char zero = (const vector unsigned char)vec_splat_u8(0);
511 vector signed short shorts1, shorts2;
513 for (i = 0; i < 4; i++) {
514 // Read potentially unaligned pixels
515 // We're reading 16 pixels, and actually only want 8,
516 // but we simply ignore the extras.
517 perm = vec_lvsl(0, s1);
518 pixv = (vector unsigned char *) s1;
519 bytes = vec_perm(pixv[0], pixv[1], perm);
521 // convert the bytes into shorts
522 shorts1 = (vector signed short)vec_mergeh(zero, bytes);
524 // Do the same for the second block of pixels
525 perm = vec_lvsl(0, s2);
526 pixv = (vector unsigned char *) s2;
527 bytes = vec_perm(pixv[0], pixv[1], perm);
529 // convert the bytes into shorts
530 shorts2 = (vector signed short)vec_mergeh(zero, bytes);
532 // Do the subtraction
533 shorts1 = vec_sub(shorts1, shorts2);
535 // save the data to the block, we assume the block is 16-byte aligned
536 vec_st(shorts1, 0, (vector signed short*)block);
538 s1 += stride;
539 s2 += stride;
540 block += 8;
543 // The code below is a copy of the code above... This is a manual
544 // unroll.
546 // Read potentially unaligned pixels
547 // We're reading 16 pixels, and actually only want 8,
548 // but we simply ignore the extras.
549 perm = vec_lvsl(0, s1);
550 pixv = (vector unsigned char *) s1;
551 bytes = vec_perm(pixv[0], pixv[1], perm);
553 // convert the bytes into shorts
554 shorts1 = (vector signed short)vec_mergeh(zero, bytes);
556 // Do the same for the second block of pixels
557 perm = vec_lvsl(0, s2);
558 pixv = (vector unsigned char *) s2;
559 bytes = vec_perm(pixv[0], pixv[1], perm);
561 // convert the bytes into shorts
562 shorts2 = (vector signed short)vec_mergeh(zero, bytes);
564 // Do the subtraction
565 shorts1 = vec_sub(shorts1, shorts2);
567 // save the data to the block, we assume the block is 16-byte aligned
568 vec_st(shorts1, 0, (vector signed short*)block);
570 s1 += stride;
571 s2 += stride;
572 block += 8;
576 void add_bytes_altivec(uint8_t *dst, uint8_t *src, int w) {
577 register int i;
578 register vector unsigned char vdst, vsrc;
580 /* dst and src are 16 bytes-aligned (guaranteed) */
581 for (i = 0 ; (i + 15) < w ; i+=16) {
582 vdst = vec_ld(i, (unsigned char*)dst);
583 vsrc = vec_ld(i, (unsigned char*)src);
584 vdst = vec_add(vsrc, vdst);
585 vec_st(vdst, i, (unsigned char*)dst);
587 /* if w is not a multiple of 16 */
588 for (; (i < w) ; i++) {
589 dst[i] = src[i];
593 /* next one assumes that ((line_size % 16) == 0) */
594 void put_pixels16_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
596 POWERPC_PERF_DECLARE(altivec_put_pixels16_num, 1);
597 register vector unsigned char pixelsv1, pixelsv2;
598 register vector unsigned char pixelsv1B, pixelsv2B;
599 register vector unsigned char pixelsv1C, pixelsv2C;
600 register vector unsigned char pixelsv1D, pixelsv2D;
602 register vector unsigned char perm = vec_lvsl(0, pixels);
603 int i;
604 register int line_size_2 = line_size << 1;
605 register int line_size_3 = line_size + line_size_2;
606 register int line_size_4 = line_size << 2;
608 POWERPC_PERF_START_COUNT(altivec_put_pixels16_num, 1);
609 // hand-unrolling the loop by 4 gains about 15%
610 // mininum execution time goes from 74 to 60 cycles
611 // it's faster than -funroll-loops, but using
612 // -funroll-loops w/ this is bad - 74 cycles again.
613 // all this is on a 7450, tuning for the 7450
614 #if 0
615 for (i = 0; i < h; i++) {
616 pixelsv1 = vec_ld(0, (unsigned char*)pixels);
617 pixelsv2 = vec_ld(16, (unsigned char*)pixels);
618 vec_st(vec_perm(pixelsv1, pixelsv2, perm),
619 0, (unsigned char*)block);
620 pixels+=line_size;
621 block +=line_size;
623 #else
624 for (i = 0; i < h; i += 4) {
625 pixelsv1 = vec_ld(0, (unsigned char*)pixels);
626 pixelsv2 = vec_ld(15, (unsigned char*)pixels);
627 pixelsv1B = vec_ld(line_size, (unsigned char*)pixels);
628 pixelsv2B = vec_ld(15 + line_size, (unsigned char*)pixels);
629 pixelsv1C = vec_ld(line_size_2, (unsigned char*)pixels);
630 pixelsv2C = vec_ld(15 + line_size_2, (unsigned char*)pixels);
631 pixelsv1D = vec_ld(line_size_3, (unsigned char*)pixels);
632 pixelsv2D = vec_ld(15 + line_size_3, (unsigned char*)pixels);
633 vec_st(vec_perm(pixelsv1, pixelsv2, perm),
634 0, (unsigned char*)block);
635 vec_st(vec_perm(pixelsv1B, pixelsv2B, perm),
636 line_size, (unsigned char*)block);
637 vec_st(vec_perm(pixelsv1C, pixelsv2C, perm),
638 line_size_2, (unsigned char*)block);
639 vec_st(vec_perm(pixelsv1D, pixelsv2D, perm),
640 line_size_3, (unsigned char*)block);
641 pixels+=line_size_4;
642 block +=line_size_4;
644 #endif
645 POWERPC_PERF_STOP_COUNT(altivec_put_pixels16_num, 1);
648 /* next one assumes that ((line_size % 16) == 0) */
649 #define op_avg(a,b) a = ( ((a)|(b)) - ((((a)^(b))&0xFEFEFEFEUL)>>1) )
650 void avg_pixels16_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
652 POWERPC_PERF_DECLARE(altivec_avg_pixels16_num, 1);
653 register vector unsigned char pixelsv1, pixelsv2, pixelsv, blockv;
654 register vector unsigned char perm = vec_lvsl(0, pixels);
655 int i;
657 POWERPC_PERF_START_COUNT(altivec_avg_pixels16_num, 1);
659 for (i = 0; i < h; i++) {
660 pixelsv1 = vec_ld(0, (unsigned char*)pixels);
661 pixelsv2 = vec_ld(16, (unsigned char*)pixels);
662 blockv = vec_ld(0, block);
663 pixelsv = vec_perm(pixelsv1, pixelsv2, perm);
664 blockv = vec_avg(blockv,pixelsv);
665 vec_st(blockv, 0, (unsigned char*)block);
666 pixels+=line_size;
667 block +=line_size;
670 POWERPC_PERF_STOP_COUNT(altivec_avg_pixels16_num, 1);
673 /* next one assumes that ((line_size % 8) == 0) */
674 void avg_pixels8_altivec(uint8_t * block, const uint8_t * pixels, int line_size, int h)
676 POWERPC_PERF_DECLARE(altivec_avg_pixels8_num, 1);
677 register vector unsigned char pixelsv1, pixelsv2, pixelsv, blockv;
678 int i;
680 POWERPC_PERF_START_COUNT(altivec_avg_pixels8_num, 1);
682 for (i = 0; i < h; i++) {
683 /* block is 8 bytes-aligned, so we're either in the
684 left block (16 bytes-aligned) or in the right block (not) */
685 int rightside = ((unsigned long)block & 0x0000000F);
687 blockv = vec_ld(0, block);
688 pixelsv1 = vec_ld(0, (unsigned char*)pixels);
689 pixelsv2 = vec_ld(16, (unsigned char*)pixels);
690 pixelsv = vec_perm(pixelsv1, pixelsv2, vec_lvsl(0, pixels));
692 if (rightside) {
693 pixelsv = vec_perm(blockv, pixelsv, vcprm(0,1,s0,s1));
694 } else {
695 pixelsv = vec_perm(blockv, pixelsv, vcprm(s0,s1,2,3));
698 blockv = vec_avg(blockv, pixelsv);
700 vec_st(blockv, 0, block);
702 pixels += line_size;
703 block += line_size;
706 POWERPC_PERF_STOP_COUNT(altivec_avg_pixels8_num, 1);
709 /* next one assumes that ((line_size % 8) == 0) */
710 void put_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
712 POWERPC_PERF_DECLARE(altivec_put_pixels8_xy2_num, 1);
713 register int i;
714 register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
715 register vector unsigned char blockv, temp1, temp2;
716 register vector unsigned short pixelssum1, pixelssum2, temp3;
717 register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
718 register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
720 temp1 = vec_ld(0, pixels);
721 temp2 = vec_ld(16, pixels);
722 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
723 if ((((unsigned long)pixels) & 0x0000000F) == 0x0000000F) {
724 pixelsv2 = temp2;
725 } else {
726 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
728 pixelsv1 = vec_mergeh(vczero, pixelsv1);
729 pixelsv2 = vec_mergeh(vczero, pixelsv2);
730 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
731 (vector unsigned short)pixelsv2);
732 pixelssum1 = vec_add(pixelssum1, vctwo);
734 POWERPC_PERF_START_COUNT(altivec_put_pixels8_xy2_num, 1);
735 for (i = 0; i < h ; i++) {
736 int rightside = ((unsigned long)block & 0x0000000F);
737 blockv = vec_ld(0, block);
739 temp1 = vec_ld(line_size, pixels);
740 temp2 = vec_ld(line_size + 16, pixels);
741 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
742 if (((((unsigned long)pixels) + line_size) & 0x0000000F) == 0x0000000F) {
743 pixelsv2 = temp2;
744 } else {
745 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
748 pixelsv1 = vec_mergeh(vczero, pixelsv1);
749 pixelsv2 = vec_mergeh(vczero, pixelsv2);
750 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
751 (vector unsigned short)pixelsv2);
752 temp3 = vec_add(pixelssum1, pixelssum2);
753 temp3 = vec_sra(temp3, vctwo);
754 pixelssum1 = vec_add(pixelssum2, vctwo);
755 pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
757 if (rightside) {
758 blockv = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
759 } else {
760 blockv = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
763 vec_st(blockv, 0, block);
765 block += line_size;
766 pixels += line_size;
769 POWERPC_PERF_STOP_COUNT(altivec_put_pixels8_xy2_num, 1);
772 /* next one assumes that ((line_size % 8) == 0) */
773 void put_no_rnd_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
775 POWERPC_PERF_DECLARE(altivec_put_no_rnd_pixels8_xy2_num, 1);
776 register int i;
777 register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
778 register vector unsigned char blockv, temp1, temp2;
779 register vector unsigned short pixelssum1, pixelssum2, temp3;
780 register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
781 register const vector unsigned short vcone = (const vector unsigned short)vec_splat_u16(1);
782 register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
784 temp1 = vec_ld(0, pixels);
785 temp2 = vec_ld(16, pixels);
786 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
787 if ((((unsigned long)pixels) & 0x0000000F) == 0x0000000F) {
788 pixelsv2 = temp2;
789 } else {
790 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
792 pixelsv1 = vec_mergeh(vczero, pixelsv1);
793 pixelsv2 = vec_mergeh(vczero, pixelsv2);
794 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
795 (vector unsigned short)pixelsv2);
796 pixelssum1 = vec_add(pixelssum1, vcone);
798 POWERPC_PERF_START_COUNT(altivec_put_no_rnd_pixels8_xy2_num, 1);
799 for (i = 0; i < h ; i++) {
800 int rightside = ((unsigned long)block & 0x0000000F);
801 blockv = vec_ld(0, block);
803 temp1 = vec_ld(line_size, pixels);
804 temp2 = vec_ld(line_size + 16, pixels);
805 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
806 if (((((unsigned long)pixels) + line_size) & 0x0000000F) == 0x0000000F) {
807 pixelsv2 = temp2;
808 } else {
809 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
812 pixelsv1 = vec_mergeh(vczero, pixelsv1);
813 pixelsv2 = vec_mergeh(vczero, pixelsv2);
814 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
815 (vector unsigned short)pixelsv2);
816 temp3 = vec_add(pixelssum1, pixelssum2);
817 temp3 = vec_sra(temp3, vctwo);
818 pixelssum1 = vec_add(pixelssum2, vcone);
819 pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
821 if (rightside) {
822 blockv = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
823 } else {
824 blockv = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
827 vec_st(blockv, 0, block);
829 block += line_size;
830 pixels += line_size;
833 POWERPC_PERF_STOP_COUNT(altivec_put_no_rnd_pixels8_xy2_num, 1);
836 /* next one assumes that ((line_size % 16) == 0) */
837 void put_pixels16_xy2_altivec(uint8_t * block, const uint8_t * pixels, int line_size, int h)
839 POWERPC_PERF_DECLARE(altivec_put_pixels16_xy2_num, 1);
840 register int i;
841 register vector unsigned char pixelsv1, pixelsv2, pixelsv3, pixelsv4;
842 register vector unsigned char blockv, temp1, temp2;
843 register vector unsigned short temp3, temp4,
844 pixelssum1, pixelssum2, pixelssum3, pixelssum4;
845 register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
846 register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
848 POWERPC_PERF_START_COUNT(altivec_put_pixels16_xy2_num, 1);
850 temp1 = vec_ld(0, pixels);
851 temp2 = vec_ld(16, pixels);
852 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
853 if ((((unsigned long)pixels) & 0x0000000F) == 0x0000000F) {
854 pixelsv2 = temp2;
855 } else {
856 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
858 pixelsv3 = vec_mergel(vczero, pixelsv1);
859 pixelsv4 = vec_mergel(vczero, pixelsv2);
860 pixelsv1 = vec_mergeh(vczero, pixelsv1);
861 pixelsv2 = vec_mergeh(vczero, pixelsv2);
862 pixelssum3 = vec_add((vector unsigned short)pixelsv3,
863 (vector unsigned short)pixelsv4);
864 pixelssum3 = vec_add(pixelssum3, vctwo);
865 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
866 (vector unsigned short)pixelsv2);
867 pixelssum1 = vec_add(pixelssum1, vctwo);
869 for (i = 0; i < h ; i++) {
870 blockv = vec_ld(0, block);
872 temp1 = vec_ld(line_size, pixels);
873 temp2 = vec_ld(line_size + 16, pixels);
874 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
875 if (((((unsigned long)pixels) + line_size) & 0x0000000F) == 0x0000000F) {
876 pixelsv2 = temp2;
877 } else {
878 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
881 pixelsv3 = vec_mergel(vczero, pixelsv1);
882 pixelsv4 = vec_mergel(vczero, pixelsv2);
883 pixelsv1 = vec_mergeh(vczero, pixelsv1);
884 pixelsv2 = vec_mergeh(vczero, pixelsv2);
886 pixelssum4 = vec_add((vector unsigned short)pixelsv3,
887 (vector unsigned short)pixelsv4);
888 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
889 (vector unsigned short)pixelsv2);
890 temp4 = vec_add(pixelssum3, pixelssum4);
891 temp4 = vec_sra(temp4, vctwo);
892 temp3 = vec_add(pixelssum1, pixelssum2);
893 temp3 = vec_sra(temp3, vctwo);
895 pixelssum3 = vec_add(pixelssum4, vctwo);
896 pixelssum1 = vec_add(pixelssum2, vctwo);
898 blockv = vec_packsu(temp3, temp4);
900 vec_st(blockv, 0, block);
902 block += line_size;
903 pixels += line_size;
906 POWERPC_PERF_STOP_COUNT(altivec_put_pixels16_xy2_num, 1);
909 /* next one assumes that ((line_size % 16) == 0) */
910 void put_no_rnd_pixels16_xy2_altivec(uint8_t * block, const uint8_t * pixels, int line_size, int h)
912 POWERPC_PERF_DECLARE(altivec_put_no_rnd_pixels16_xy2_num, 1);
913 register int i;
914 register vector unsigned char pixelsv1, pixelsv2, pixelsv3, pixelsv4;
915 register vector unsigned char blockv, temp1, temp2;
916 register vector unsigned short temp3, temp4,
917 pixelssum1, pixelssum2, pixelssum3, pixelssum4;
918 register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
919 register const vector unsigned short vcone = (const vector unsigned short)vec_splat_u16(1);
920 register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
922 POWERPC_PERF_START_COUNT(altivec_put_no_rnd_pixels16_xy2_num, 1);
924 temp1 = vec_ld(0, pixels);
925 temp2 = vec_ld(16, pixels);
926 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
927 if ((((unsigned long)pixels) & 0x0000000F) == 0x0000000F) {
928 pixelsv2 = temp2;
929 } else {
930 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
932 pixelsv3 = vec_mergel(vczero, pixelsv1);
933 pixelsv4 = vec_mergel(vczero, pixelsv2);
934 pixelsv1 = vec_mergeh(vczero, pixelsv1);
935 pixelsv2 = vec_mergeh(vczero, pixelsv2);
936 pixelssum3 = vec_add((vector unsigned short)pixelsv3,
937 (vector unsigned short)pixelsv4);
938 pixelssum3 = vec_add(pixelssum3, vcone);
939 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
940 (vector unsigned short)pixelsv2);
941 pixelssum1 = vec_add(pixelssum1, vcone);
943 for (i = 0; i < h ; i++) {
944 blockv = vec_ld(0, block);
946 temp1 = vec_ld(line_size, pixels);
947 temp2 = vec_ld(line_size + 16, pixels);
948 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
949 if (((((unsigned long)pixels) + line_size) & 0x0000000F) == 0x0000000F) {
950 pixelsv2 = temp2;
951 } else {
952 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
955 pixelsv3 = vec_mergel(vczero, pixelsv1);
956 pixelsv4 = vec_mergel(vczero, pixelsv2);
957 pixelsv1 = vec_mergeh(vczero, pixelsv1);
958 pixelsv2 = vec_mergeh(vczero, pixelsv2);
960 pixelssum4 = vec_add((vector unsigned short)pixelsv3,
961 (vector unsigned short)pixelsv4);
962 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
963 (vector unsigned short)pixelsv2);
964 temp4 = vec_add(pixelssum3, pixelssum4);
965 temp4 = vec_sra(temp4, vctwo);
966 temp3 = vec_add(pixelssum1, pixelssum2);
967 temp3 = vec_sra(temp3, vctwo);
969 pixelssum3 = vec_add(pixelssum4, vcone);
970 pixelssum1 = vec_add(pixelssum2, vcone);
972 blockv = vec_packsu(temp3, temp4);
974 vec_st(blockv, 0, block);
976 block += line_size;
977 pixels += line_size;
980 POWERPC_PERF_STOP_COUNT(altivec_put_no_rnd_pixels16_xy2_num, 1);
983 int hadamard8_diff8x8_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h){
984 POWERPC_PERF_DECLARE(altivec_hadamard8_diff8x8_num, 1);
985 int sum;
986 register const vector unsigned char vzero =
987 (const vector unsigned char)vec_splat_u8(0);
988 register vector signed short temp0, temp1, temp2, temp3, temp4,
989 temp5, temp6, temp7;
990 POWERPC_PERF_START_COUNT(altivec_hadamard8_diff8x8_num, 1);
992 register const vector signed short vprod1 =(const vector signed short)
993 { 1,-1, 1,-1, 1,-1, 1,-1 };
994 register const vector signed short vprod2 =(const vector signed short)
995 { 1, 1,-1,-1, 1, 1,-1,-1 };
996 register const vector signed short vprod3 =(const vector signed short)
997 { 1, 1, 1, 1,-1,-1,-1,-1 };
998 register const vector unsigned char perm1 = (const vector unsigned char)
999 {0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
1000 0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D};
1001 register const vector unsigned char perm2 = (const vector unsigned char)
1002 {0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
1003 0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B};
1004 register const vector unsigned char perm3 = (const vector unsigned char)
1005 {0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1006 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
1008 #define ONEITERBUTTERFLY(i, res) \
1010 register vector unsigned char src1, src2, srcO; \
1011 register vector unsigned char dst1, dst2, dstO; \
1012 register vector signed short srcV, dstV; \
1013 register vector signed short but0, but1, but2, op1, op2, op3; \
1014 src1 = vec_ld(stride * i, src); \
1015 src2 = vec_ld((stride * i) + 15, src); \
1016 srcO = vec_perm(src1, src2, vec_lvsl(stride * i, src)); \
1017 dst1 = vec_ld(stride * i, dst); \
1018 dst2 = vec_ld((stride * i) + 15, dst); \
1019 dstO = vec_perm(dst1, dst2, vec_lvsl(stride * i, dst)); \
1020 /* promote the unsigned chars to signed shorts */ \
1021 /* we're in the 8x8 function, we only care for the first 8 */ \
1022 srcV = (vector signed short)vec_mergeh((vector signed char)vzero, \
1023 (vector signed char)srcO); \
1024 dstV = (vector signed short)vec_mergeh((vector signed char)vzero, \
1025 (vector signed char)dstO); \
1026 /* subtractions inside the first butterfly */ \
1027 but0 = vec_sub(srcV, dstV); \
1028 op1 = vec_perm(but0, but0, perm1); \
1029 but1 = vec_mladd(but0, vprod1, op1); \
1030 op2 = vec_perm(but1, but1, perm2); \
1031 but2 = vec_mladd(but1, vprod2, op2); \
1032 op3 = vec_perm(but2, but2, perm3); \
1033 res = vec_mladd(but2, vprod3, op3); \
1035 ONEITERBUTTERFLY(0, temp0);
1036 ONEITERBUTTERFLY(1, temp1);
1037 ONEITERBUTTERFLY(2, temp2);
1038 ONEITERBUTTERFLY(3, temp3);
1039 ONEITERBUTTERFLY(4, temp4);
1040 ONEITERBUTTERFLY(5, temp5);
1041 ONEITERBUTTERFLY(6, temp6);
1042 ONEITERBUTTERFLY(7, temp7);
1044 #undef ONEITERBUTTERFLY
1046 register vector signed int vsum;
1047 register vector signed short line0 = vec_add(temp0, temp1);
1048 register vector signed short line1 = vec_sub(temp0, temp1);
1049 register vector signed short line2 = vec_add(temp2, temp3);
1050 register vector signed short line3 = vec_sub(temp2, temp3);
1051 register vector signed short line4 = vec_add(temp4, temp5);
1052 register vector signed short line5 = vec_sub(temp4, temp5);
1053 register vector signed short line6 = vec_add(temp6, temp7);
1054 register vector signed short line7 = vec_sub(temp6, temp7);
1056 register vector signed short line0B = vec_add(line0, line2);
1057 register vector signed short line2B = vec_sub(line0, line2);
1058 register vector signed short line1B = vec_add(line1, line3);
1059 register vector signed short line3B = vec_sub(line1, line3);
1060 register vector signed short line4B = vec_add(line4, line6);
1061 register vector signed short line6B = vec_sub(line4, line6);
1062 register vector signed short line5B = vec_add(line5, line7);
1063 register vector signed short line7B = vec_sub(line5, line7);
1065 register vector signed short line0C = vec_add(line0B, line4B);
1066 register vector signed short line4C = vec_sub(line0B, line4B);
1067 register vector signed short line1C = vec_add(line1B, line5B);
1068 register vector signed short line5C = vec_sub(line1B, line5B);
1069 register vector signed short line2C = vec_add(line2B, line6B);
1070 register vector signed short line6C = vec_sub(line2B, line6B);
1071 register vector signed short line3C = vec_add(line3B, line7B);
1072 register vector signed short line7C = vec_sub(line3B, line7B);
1074 vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
1075 vsum = vec_sum4s(vec_abs(line1C), vsum);
1076 vsum = vec_sum4s(vec_abs(line2C), vsum);
1077 vsum = vec_sum4s(vec_abs(line3C), vsum);
1078 vsum = vec_sum4s(vec_abs(line4C), vsum);
1079 vsum = vec_sum4s(vec_abs(line5C), vsum);
1080 vsum = vec_sum4s(vec_abs(line6C), vsum);
1081 vsum = vec_sum4s(vec_abs(line7C), vsum);
1082 vsum = vec_sums(vsum, (vector signed int)vzero);
1083 vsum = vec_splat(vsum, 3);
1084 vec_ste(vsum, 0, &sum);
1086 POWERPC_PERF_STOP_COUNT(altivec_hadamard8_diff8x8_num, 1);
1087 return sum;
1091 16x8 works with 16 elements; it allows to avoid replicating loads, and
1092 give the compiler more rooms for scheduling. It's only used from
1093 inside hadamard8_diff16_altivec.
1095 Unfortunately, it seems gcc-3.3 is a bit dumb, and the compiled code has a LOT
1096 of spill code, it seems gcc (unlike xlc) cannot keep everything in registers
1097 by itself. The following code include hand-made registers allocation. It's not
1098 clean, but on a 7450 the resulting code is much faster (best case fall from
1099 700+ cycles to 550).
1101 xlc doesn't add spill code, but it doesn't know how to schedule for the 7450,
1102 and its code isn't much faster than gcc-3.3 on the 7450 (but uses 25% less
1103 instructions...)
1105 On the 970, the hand-made RA is still a win (around 690 vs. around 780), but
1106 xlc goes to around 660 on the regular C code...
1109 static int hadamard8_diff16x8_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h) {
1110 int sum;
1111 register vector signed short
1112 temp0 REG_v(v0),
1113 temp1 REG_v(v1),
1114 temp2 REG_v(v2),
1115 temp3 REG_v(v3),
1116 temp4 REG_v(v4),
1117 temp5 REG_v(v5),
1118 temp6 REG_v(v6),
1119 temp7 REG_v(v7);
1120 register vector signed short
1121 temp0S REG_v(v8),
1122 temp1S REG_v(v9),
1123 temp2S REG_v(v10),
1124 temp3S REG_v(v11),
1125 temp4S REG_v(v12),
1126 temp5S REG_v(v13),
1127 temp6S REG_v(v14),
1128 temp7S REG_v(v15);
1129 register const vector unsigned char vzero REG_v(v31)=
1130 (const vector unsigned char)vec_splat_u8(0);
1132 register const vector signed short vprod1 REG_v(v16)=
1133 (const vector signed short){ 1,-1, 1,-1, 1,-1, 1,-1 };
1134 register const vector signed short vprod2 REG_v(v17)=
1135 (const vector signed short){ 1, 1,-1,-1, 1, 1,-1,-1 };
1136 register const vector signed short vprod3 REG_v(v18)=
1137 (const vector signed short){ 1, 1, 1, 1,-1,-1,-1,-1 };
1138 register const vector unsigned char perm1 REG_v(v19)=
1139 (const vector unsigned char)
1140 {0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
1141 0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D};
1142 register const vector unsigned char perm2 REG_v(v20)=
1143 (const vector unsigned char)
1144 {0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
1145 0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B};
1146 register const vector unsigned char perm3 REG_v(v21)=
1147 (const vector unsigned char)
1148 {0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1149 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
1151 #define ONEITERBUTTERFLY(i, res1, res2) \
1153 register vector unsigned char src1 REG_v(v22), \
1154 src2 REG_v(v23), \
1155 dst1 REG_v(v24), \
1156 dst2 REG_v(v25), \
1157 srcO REG_v(v22), \
1158 dstO REG_v(v23); \
1160 register vector signed short srcV REG_v(v24), \
1161 dstV REG_v(v25), \
1162 srcW REG_v(v26), \
1163 dstW REG_v(v27), \
1164 but0 REG_v(v28), \
1165 but0S REG_v(v29), \
1166 op1 REG_v(v30), \
1167 but1 REG_v(v22), \
1168 op1S REG_v(v23), \
1169 but1S REG_v(v24), \
1170 op2 REG_v(v25), \
1171 but2 REG_v(v26), \
1172 op2S REG_v(v27), \
1173 but2S REG_v(v28), \
1174 op3 REG_v(v29), \
1175 op3S REG_v(v30); \
1177 src1 = vec_ld(stride * i, src); \
1178 src2 = vec_ld((stride * i) + 16, src); \
1179 srcO = vec_perm(src1, src2, vec_lvsl(stride * i, src)); \
1180 dst1 = vec_ld(stride * i, dst); \
1181 dst2 = vec_ld((stride * i) + 16, dst); \
1182 dstO = vec_perm(dst1, dst2, vec_lvsl(stride * i, dst)); \
1183 /* promote the unsigned chars to signed shorts */ \
1184 srcV = (vector signed short)vec_mergeh((vector signed char)vzero, \
1185 (vector signed char)srcO); \
1186 dstV = (vector signed short)vec_mergeh((vector signed char)vzero, \
1187 (vector signed char)dstO); \
1188 srcW = (vector signed short)vec_mergel((vector signed char)vzero, \
1189 (vector signed char)srcO); \
1190 dstW = (vector signed short)vec_mergel((vector signed char)vzero, \
1191 (vector signed char)dstO); \
1192 /* subtractions inside the first butterfly */ \
1193 but0 = vec_sub(srcV, dstV); \
1194 but0S = vec_sub(srcW, dstW); \
1195 op1 = vec_perm(but0, but0, perm1); \
1196 but1 = vec_mladd(but0, vprod1, op1); \
1197 op1S = vec_perm(but0S, but0S, perm1); \
1198 but1S = vec_mladd(but0S, vprod1, op1S); \
1199 op2 = vec_perm(but1, but1, perm2); \
1200 but2 = vec_mladd(but1, vprod2, op2); \
1201 op2S = vec_perm(but1S, but1S, perm2); \
1202 but2S = vec_mladd(but1S, vprod2, op2S); \
1203 op3 = vec_perm(but2, but2, perm3); \
1204 res1 = vec_mladd(but2, vprod3, op3); \
1205 op3S = vec_perm(but2S, but2S, perm3); \
1206 res2 = vec_mladd(but2S, vprod3, op3S); \
1208 ONEITERBUTTERFLY(0, temp0, temp0S);
1209 ONEITERBUTTERFLY(1, temp1, temp1S);
1210 ONEITERBUTTERFLY(2, temp2, temp2S);
1211 ONEITERBUTTERFLY(3, temp3, temp3S);
1212 ONEITERBUTTERFLY(4, temp4, temp4S);
1213 ONEITERBUTTERFLY(5, temp5, temp5S);
1214 ONEITERBUTTERFLY(6, temp6, temp6S);
1215 ONEITERBUTTERFLY(7, temp7, temp7S);
1217 #undef ONEITERBUTTERFLY
1219 register vector signed int vsum;
1220 register vector signed short line0S, line1S, line2S, line3S, line4S,
1221 line5S, line6S, line7S, line0BS,line2BS,
1222 line1BS,line3BS,line4BS,line6BS,line5BS,
1223 line7BS,line0CS,line4CS,line1CS,line5CS,
1224 line2CS,line6CS,line3CS,line7CS;
1226 register vector signed short line0 = vec_add(temp0, temp1);
1227 register vector signed short line1 = vec_sub(temp0, temp1);
1228 register vector signed short line2 = vec_add(temp2, temp3);
1229 register vector signed short line3 = vec_sub(temp2, temp3);
1230 register vector signed short line4 = vec_add(temp4, temp5);
1231 register vector signed short line5 = vec_sub(temp4, temp5);
1232 register vector signed short line6 = vec_add(temp6, temp7);
1233 register vector signed short line7 = vec_sub(temp6, temp7);
1235 register vector signed short line0B = vec_add(line0, line2);
1236 register vector signed short line2B = vec_sub(line0, line2);
1237 register vector signed short line1B = vec_add(line1, line3);
1238 register vector signed short line3B = vec_sub(line1, line3);
1239 register vector signed short line4B = vec_add(line4, line6);
1240 register vector signed short line6B = vec_sub(line4, line6);
1241 register vector signed short line5B = vec_add(line5, line7);
1242 register vector signed short line7B = vec_sub(line5, line7);
1244 register vector signed short line0C = vec_add(line0B, line4B);
1245 register vector signed short line4C = vec_sub(line0B, line4B);
1246 register vector signed short line1C = vec_add(line1B, line5B);
1247 register vector signed short line5C = vec_sub(line1B, line5B);
1248 register vector signed short line2C = vec_add(line2B, line6B);
1249 register vector signed short line6C = vec_sub(line2B, line6B);
1250 register vector signed short line3C = vec_add(line3B, line7B);
1251 register vector signed short line7C = vec_sub(line3B, line7B);
1253 vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
1254 vsum = vec_sum4s(vec_abs(line1C), vsum);
1255 vsum = vec_sum4s(vec_abs(line2C), vsum);
1256 vsum = vec_sum4s(vec_abs(line3C), vsum);
1257 vsum = vec_sum4s(vec_abs(line4C), vsum);
1258 vsum = vec_sum4s(vec_abs(line5C), vsum);
1259 vsum = vec_sum4s(vec_abs(line6C), vsum);
1260 vsum = vec_sum4s(vec_abs(line7C), vsum);
1262 line0S = vec_add(temp0S, temp1S);
1263 line1S = vec_sub(temp0S, temp1S);
1264 line2S = vec_add(temp2S, temp3S);
1265 line3S = vec_sub(temp2S, temp3S);
1266 line4S = vec_add(temp4S, temp5S);
1267 line5S = vec_sub(temp4S, temp5S);
1268 line6S = vec_add(temp6S, temp7S);
1269 line7S = vec_sub(temp6S, temp7S);
1271 line0BS = vec_add(line0S, line2S);
1272 line2BS = vec_sub(line0S, line2S);
1273 line1BS = vec_add(line1S, line3S);
1274 line3BS = vec_sub(line1S, line3S);
1275 line4BS = vec_add(line4S, line6S);
1276 line6BS = vec_sub(line4S, line6S);
1277 line5BS = vec_add(line5S, line7S);
1278 line7BS = vec_sub(line5S, line7S);
1280 line0CS = vec_add(line0BS, line4BS);
1281 line4CS = vec_sub(line0BS, line4BS);
1282 line1CS = vec_add(line1BS, line5BS);
1283 line5CS = vec_sub(line1BS, line5BS);
1284 line2CS = vec_add(line2BS, line6BS);
1285 line6CS = vec_sub(line2BS, line6BS);
1286 line3CS = vec_add(line3BS, line7BS);
1287 line7CS = vec_sub(line3BS, line7BS);
1289 vsum = vec_sum4s(vec_abs(line0CS), vsum);
1290 vsum = vec_sum4s(vec_abs(line1CS), vsum);
1291 vsum = vec_sum4s(vec_abs(line2CS), vsum);
1292 vsum = vec_sum4s(vec_abs(line3CS), vsum);
1293 vsum = vec_sum4s(vec_abs(line4CS), vsum);
1294 vsum = vec_sum4s(vec_abs(line5CS), vsum);
1295 vsum = vec_sum4s(vec_abs(line6CS), vsum);
1296 vsum = vec_sum4s(vec_abs(line7CS), vsum);
1297 vsum = vec_sums(vsum, (vector signed int)vzero);
1298 vsum = vec_splat(vsum, 3);
1299 vec_ste(vsum, 0, &sum);
1301 return sum;
1304 int hadamard8_diff16_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h){
1305 POWERPC_PERF_DECLARE(altivec_hadamard8_diff16_num, 1);
1306 int score;
1307 POWERPC_PERF_START_COUNT(altivec_hadamard8_diff16_num, 1);
1308 score = hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
1309 if (h==16) {
1310 dst += 8*stride;
1311 src += 8*stride;
1312 score += hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
1314 POWERPC_PERF_STOP_COUNT(altivec_hadamard8_diff16_num, 1);
1315 return score;
1318 static void vorbis_inverse_coupling_altivec(float *mag, float *ang,
1319 int blocksize)
1321 int i;
1322 vector float m, a;
1323 vector bool int t0, t1;
1324 const vector unsigned int v_31 = //XXX
1325 vec_add(vec_add(vec_splat_u32(15),vec_splat_u32(15)),vec_splat_u32(1));
1326 for (i = 0; i < blocksize; i += 4) {
1327 m = vec_ld(0, mag+i);
1328 a = vec_ld(0, ang+i);
1329 t0 = vec_cmple(m, (vector float)vec_splat_u32(0));
1330 t1 = vec_cmple(a, (vector float)vec_splat_u32(0));
1331 a = vec_xor(a, (vector float) vec_sl((vector unsigned int)t0, v_31));
1332 t0 = (vector bool int)vec_and(a, t1);
1333 t1 = (vector bool int)vec_andc(a, t1);
1334 a = vec_sub(m, (vector float)t1);
1335 m = vec_add(m, (vector float)t0);
1336 vec_stl(a, 0, ang+i);
1337 vec_stl(m, 0, mag+i);
1341 /* next one assumes that ((line_size % 8) == 0) */
1342 void avg_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
1344 POWERPC_PERF_DECLARE(altivec_avg_pixels8_xy2_num, 1);
1345 register int i;
1346 register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
1347 register vector unsigned char blockv, temp1, temp2, blocktemp;
1348 register vector unsigned short pixelssum1, pixelssum2, temp3;
1350 register const vector unsigned char vczero = (const vector unsigned char)
1351 vec_splat_u8(0);
1352 register const vector unsigned short vctwo = (const vector unsigned short)
1353 vec_splat_u16(2);
1355 temp1 = vec_ld(0, pixels);
1356 temp2 = vec_ld(16, pixels);
1357 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
1358 if ((((unsigned long)pixels) & 0x0000000F) == 0x0000000F) {
1359 pixelsv2 = temp2;
1360 } else {
1361 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
1363 pixelsv1 = vec_mergeh(vczero, pixelsv1);
1364 pixelsv2 = vec_mergeh(vczero, pixelsv2);
1365 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
1366 (vector unsigned short)pixelsv2);
1367 pixelssum1 = vec_add(pixelssum1, vctwo);
1369 POWERPC_PERF_START_COUNT(altivec_avg_pixels8_xy2_num, 1);
1370 for (i = 0; i < h ; i++) {
1371 int rightside = ((unsigned long)block & 0x0000000F);
1372 blockv = vec_ld(0, block);
1374 temp1 = vec_ld(line_size, pixels);
1375 temp2 = vec_ld(line_size + 16, pixels);
1376 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
1377 if (((((unsigned long)pixels) + line_size) & 0x0000000F) == 0x0000000F) {
1378 pixelsv2 = temp2;
1379 } else {
1380 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
1383 pixelsv1 = vec_mergeh(vczero, pixelsv1);
1384 pixelsv2 = vec_mergeh(vczero, pixelsv2);
1385 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
1386 (vector unsigned short)pixelsv2);
1387 temp3 = vec_add(pixelssum1, pixelssum2);
1388 temp3 = vec_sra(temp3, vctwo);
1389 pixelssum1 = vec_add(pixelssum2, vctwo);
1390 pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
1392 if (rightside) {
1393 blocktemp = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
1394 } else {
1395 blocktemp = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
1398 blockv = vec_avg(blocktemp, blockv);
1399 vec_st(blockv, 0, block);
1401 block += line_size;
1402 pixels += line_size;
1405 POWERPC_PERF_STOP_COUNT(altivec_avg_pixels8_xy2_num, 1);
1408 void dsputil_init_altivec(DSPContext* c, AVCodecContext *avctx)
1410 c->pix_abs[0][1] = sad16_x2_altivec;
1411 c->pix_abs[0][2] = sad16_y2_altivec;
1412 c->pix_abs[0][3] = sad16_xy2_altivec;
1413 c->pix_abs[0][0] = sad16_altivec;
1414 c->pix_abs[1][0] = sad8_altivec;
1415 c->sad[0]= sad16_altivec;
1416 c->sad[1]= sad8_altivec;
1417 c->pix_norm1 = pix_norm1_altivec;
1418 c->sse[1]= sse8_altivec;
1419 c->sse[0]= sse16_altivec;
1420 c->pix_sum = pix_sum_altivec;
1421 c->diff_pixels = diff_pixels_altivec;
1422 c->get_pixels = get_pixels_altivec;
1423 c->add_bytes= add_bytes_altivec;
1424 c->put_pixels_tab[0][0] = put_pixels16_altivec;
1425 /* the two functions do the same thing, so use the same code */
1426 c->put_no_rnd_pixels_tab[0][0] = put_pixels16_altivec;
1427 c->avg_pixels_tab[0][0] = avg_pixels16_altivec;
1428 c->avg_pixels_tab[1][0] = avg_pixels8_altivec;
1429 c->avg_pixels_tab[1][3] = avg_pixels8_xy2_altivec;
1430 c->put_pixels_tab[1][3] = put_pixels8_xy2_altivec;
1431 c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy2_altivec;
1432 c->put_pixels_tab[0][3] = put_pixels16_xy2_altivec;
1433 c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_altivec;
1435 c->hadamard8_diff[0] = hadamard8_diff16_altivec;
1436 c->hadamard8_diff[1] = hadamard8_diff8x8_altivec;
1437 if (ENABLE_VORBIS_DECODER)
1438 c->vorbis_inverse_coupling = vorbis_inverse_coupling_altivec;