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
25 #include "gcc_fixes.h"
27 #include "dsputil_altivec.h"
30 #include <sys/sysctl.h>
32 #include <exec/exec.h>
33 #include <interfaces/exec.h>
34 #include <proto/exec.h>
39 static sigjmp_buf jmpbuf;
40 static volatile sig_atomic_t canjump = 0;
42 static void sigill_handler (int sig)
45 signal (sig, SIG_DFL);
50 siglongjmp (jmpbuf, 1);
52 #endif /* CONFIG_DARWIN */
54 int sad16_x2_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
57 DECLARE_ALIGNED_16(int, s);
58 const_vector unsigned char zero = (const_vector unsigned char)vec_splat_u8(0);
59 vector unsigned char *tv;
60 vector unsigned char pix1v, pix2v, pix2iv, avgv, t5;
61 vector unsigned int sad;
62 vector signed int sumdiffs;
65 sad = (vector unsigned int)vec_splat_u32(0);
68 Read unaligned pixels into our vectors. The vectors are as follows:
69 pix1v: pix1[0]-pix1[15]
70 pix2v: pix2[0]-pix2[15] pix2iv: pix2[1]-pix2[16]
72 tv = (vector unsigned char *) pix1;
73 pix1v = vec_perm(tv[0], tv[1], vec_lvsl(0, pix1));
75 tv = (vector unsigned char *) &pix2[0];
76 pix2v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[0]));
78 tv = (vector unsigned char *) &pix2[1];
79 pix2iv = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[1]));
81 /* Calculate the average vector */
82 avgv = vec_avg(pix2v, pix2iv);
84 /* Calculate a sum of abs differences vector */
85 t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
87 /* Add each 4 pixel group together and put 4 results into sad */
88 sad = vec_sum4s(t5, sad);
93 /* Sum up the four partial sums, and put the result into s */
94 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
95 sumdiffs = vec_splat(sumdiffs, 3);
96 vec_ste(sumdiffs, 0, &s);
101 int sad16_y2_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
104 DECLARE_ALIGNED_16(int, s);
105 const_vector unsigned char zero = (const_vector unsigned char)vec_splat_u8(0);
106 vector unsigned char *tv;
107 vector unsigned char pix1v, pix2v, pix3v, avgv, t5;
108 vector unsigned int sad;
109 vector signed int sumdiffs;
110 uint8_t *pix3 = pix2 + line_size;
113 sad = (vector unsigned int)vec_splat_u32(0);
116 Due to the fact that pix3 = pix2 + line_size, the pix3 of one
117 iteration becomes pix2 in the next iteration. We can use this
118 fact to avoid a potentially expensive unaligned read, each
119 time around the loop.
120 Read unaligned pixels into our vectors. The vectors are as follows:
121 pix2v: pix2[0]-pix2[15]
122 Split the pixel vectors into shorts
124 tv = (vector unsigned char *) &pix2[0];
125 pix2v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[0]));
129 Read unaligned pixels into our vectors. The vectors are as follows:
130 pix1v: pix1[0]-pix1[15]
131 pix3v: pix3[0]-pix3[15]
133 tv = (vector unsigned char *) pix1;
134 pix1v = vec_perm(tv[0], tv[1], vec_lvsl(0, pix1));
136 tv = (vector unsigned char *) &pix3[0];
137 pix3v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix3[0]));
139 /* Calculate the average vector */
140 avgv = vec_avg(pix2v, pix3v);
142 /* Calculate a sum of abs differences vector */
143 t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
145 /* Add each 4 pixel group together and put 4 results into sad */
146 sad = vec_sum4s(t5, sad);
154 /* Sum up the four partial sums, and put the result into s */
155 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
156 sumdiffs = vec_splat(sumdiffs, 3);
157 vec_ste(sumdiffs, 0, &s);
161 int sad16_xy2_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
164 DECLARE_ALIGNED_16(int, s);
165 uint8_t *pix3 = pix2 + line_size;
166 const_vector unsigned char zero = (const_vector unsigned char)vec_splat_u8(0);
167 const_vector unsigned short two = (const_vector unsigned short)vec_splat_u16(2);
168 vector unsigned char *tv, avgv, t5;
169 vector unsigned char pix1v, pix2v, pix3v, pix2iv, pix3iv;
170 vector unsigned short pix2lv, pix2hv, pix2ilv, pix2ihv;
171 vector unsigned short pix3lv, pix3hv, pix3ilv, pix3ihv;
172 vector unsigned short avghv, avglv;
173 vector unsigned short t1, t2, t3, t4;
174 vector unsigned int sad;
175 vector signed int sumdiffs;
177 sad = (vector unsigned int)vec_splat_u32(0);
182 Due to the fact that pix3 = pix2 + line_size, the pix3 of one
183 iteration becomes pix2 in the next iteration. We can use this
184 fact to avoid a potentially expensive unaligned read, as well
185 as some splitting, and vector addition each time around the loop.
186 Read unaligned pixels into our vectors. The vectors are as follows:
187 pix2v: pix2[0]-pix2[15] pix2iv: pix2[1]-pix2[16]
188 Split the pixel vectors into shorts
190 tv = (vector unsigned char *) &pix2[0];
191 pix2v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[0]));
193 tv = (vector unsigned char *) &pix2[1];
194 pix2iv = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix2[1]));
196 pix2hv = (vector unsigned short) vec_mergeh(zero, pix2v);
197 pix2lv = (vector unsigned short) vec_mergel(zero, pix2v);
198 pix2ihv = (vector unsigned short) vec_mergeh(zero, pix2iv);
199 pix2ilv = (vector unsigned short) vec_mergel(zero, pix2iv);
200 t1 = vec_add(pix2hv, pix2ihv);
201 t2 = vec_add(pix2lv, pix2ilv);
205 Read unaligned pixels into our vectors. The vectors are as follows:
206 pix1v: pix1[0]-pix1[15]
207 pix3v: pix3[0]-pix3[15] pix3iv: pix3[1]-pix3[16]
209 tv = (vector unsigned char *) pix1;
210 pix1v = vec_perm(tv[0], tv[1], vec_lvsl(0, pix1));
212 tv = (vector unsigned char *) &pix3[0];
213 pix3v = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix3[0]));
215 tv = (vector unsigned char *) &pix3[1];
216 pix3iv = vec_perm(tv[0], tv[1], vec_lvsl(0, &pix3[1]));
219 Note that Altivec does have vec_avg, but this works on vector pairs
220 and rounds up. We could do avg(avg(a,b),avg(c,d)), but the rounding
221 would mean that, for example, avg(3,0,0,1) = 2, when it should be 1.
222 Instead, we have to split the pixel vectors into vectors of shorts,
223 and do the averaging by hand.
226 /* Split the pixel vectors into shorts */
227 pix3hv = (vector unsigned short) vec_mergeh(zero, pix3v);
228 pix3lv = (vector unsigned short) vec_mergel(zero, pix3v);
229 pix3ihv = (vector unsigned short) vec_mergeh(zero, pix3iv);
230 pix3ilv = (vector unsigned short) vec_mergel(zero, pix3iv);
232 /* Do the averaging on them */
233 t3 = vec_add(pix3hv, pix3ihv);
234 t4 = vec_add(pix3lv, pix3ilv);
236 avghv = vec_sr(vec_add(vec_add(t1, t3), two), two);
237 avglv = vec_sr(vec_add(vec_add(t2, t4), two), two);
239 /* Pack the shorts back into a result */
240 avgv = vec_pack(avghv, avglv);
242 /* Calculate a sum of abs differences vector */
243 t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
245 /* Add each 4 pixel group together and put 4 results into sad */
246 sad = vec_sum4s(t5, sad);
250 /* Transfer the calculated values for pix3 into pix2 */
254 /* Sum up the four partial sums, and put the result into s */
255 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
256 sumdiffs = vec_splat(sumdiffs, 3);
257 vec_ste(sumdiffs, 0, &s);
262 int sad16_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
265 DECLARE_ALIGNED_16(int, s);
266 const_vector unsigned int zero = (const_vector unsigned int)vec_splat_u32(0);
267 vector unsigned char perm1, perm2, *pix1v, *pix2v;
268 vector unsigned char t1, t2, t3,t4, t5;
269 vector unsigned int sad;
270 vector signed int sumdiffs;
272 sad = (vector unsigned int)vec_splat_u32(0);
276 /* Read potentially unaligned pixels into t1 and t2 */
277 perm1 = vec_lvsl(0, pix1);
278 pix1v = (vector unsigned char *) pix1;
279 perm2 = vec_lvsl(0, pix2);
280 pix2v = (vector unsigned char *) pix2;
281 t1 = vec_perm(pix1v[0], pix1v[1], perm1);
282 t2 = vec_perm(pix2v[0], pix2v[1], perm2);
284 /* Calculate a sum of abs differences vector */
285 t3 = vec_max(t1, t2);
286 t4 = vec_min(t1, t2);
287 t5 = vec_sub(t3, t4);
289 /* Add each 4 pixel group together and put 4 results into sad */
290 sad = vec_sum4s(t5, sad);
296 /* Sum up the four partial sums, and put the result into s */
297 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
298 sumdiffs = vec_splat(sumdiffs, 3);
299 vec_ste(sumdiffs, 0, &s);
304 int sad8_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
307 DECLARE_ALIGNED_16(int, s);;
308 const_vector unsigned int zero = (const_vector unsigned int)vec_splat_u32(0);
309 vector unsigned char perm1, perm2, permclear, *pix1v, *pix2v;
310 vector unsigned char t1, t2, t3,t4, t5;
311 vector unsigned int sad;
312 vector signed int sumdiffs;
314 sad = (vector unsigned int)vec_splat_u32(0);
316 permclear = (vector unsigned char)AVV(255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0);
319 /* Read potentially unaligned pixels into t1 and t2
320 Since we're reading 16 pixels, and actually only want 8,
321 mask out the last 8 pixels. The 0s don't change the sum. */
322 perm1 = vec_lvsl(0, pix1);
323 pix1v = (vector unsigned char *) pix1;
324 perm2 = vec_lvsl(0, pix2);
325 pix2v = (vector unsigned char *) pix2;
326 t1 = vec_and(vec_perm(pix1v[0], pix1v[1], perm1), permclear);
327 t2 = vec_and(vec_perm(pix2v[0], pix2v[1], perm2), permclear);
329 /* Calculate a sum of abs differences vector */
330 t3 = vec_max(t1, t2);
331 t4 = vec_min(t1, t2);
332 t5 = vec_sub(t3, t4);
334 /* Add each 4 pixel group together and put 4 results into sad */
335 sad = vec_sum4s(t5, sad);
341 /* Sum up the four partial sums, and put the result into s */
342 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
343 sumdiffs = vec_splat(sumdiffs, 3);
344 vec_ste(sumdiffs, 0, &s);
349 int pix_norm1_altivec(uint8_t *pix, int line_size)
352 DECLARE_ALIGNED_16(int, s);
353 const_vector unsigned int zero = (const_vector unsigned int)vec_splat_u32(0);
354 vector unsigned char *tv;
355 vector unsigned char pixv;
356 vector unsigned int sv;
357 vector signed int sum;
359 sv = (vector unsigned int)vec_splat_u32(0);
362 for (i = 0; i < 16; i++) {
363 /* Read in the potentially unaligned pixels */
364 tv = (vector unsigned char *) pix;
365 pixv = vec_perm(tv[0], tv[1], vec_lvsl(0, pix));
367 /* Square the values, and add them to our sum */
368 sv = vec_msum(pixv, pixv, sv);
372 /* Sum up the four partial sums, and put the result into s */
373 sum = vec_sums((vector signed int) sv, (vector signed int) zero);
374 sum = vec_splat(sum, 3);
381 * Sum of Squared Errors for a 8x8 block.
383 * It's the sad8_altivec code above w/ squaring added.
385 int sse8_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
388 DECLARE_ALIGNED_16(int, s);
389 const_vector unsigned int zero = (const_vector unsigned int)vec_splat_u32(0);
390 vector unsigned char perm1, perm2, permclear, *pix1v, *pix2v;
391 vector unsigned char t1, t2, t3,t4, t5;
392 vector unsigned int sum;
393 vector signed int sumsqr;
395 sum = (vector unsigned int)vec_splat_u32(0);
397 permclear = (vector unsigned char)AVV(255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0);
401 /* Read potentially unaligned pixels into t1 and t2
402 Since we're reading 16 pixels, and actually only want 8,
403 mask out the last 8 pixels. The 0s don't change the sum. */
404 perm1 = vec_lvsl(0, pix1);
405 pix1v = (vector unsigned char *) pix1;
406 perm2 = vec_lvsl(0, pix2);
407 pix2v = (vector unsigned char *) pix2;
408 t1 = vec_and(vec_perm(pix1v[0], pix1v[1], perm1), permclear);
409 t2 = vec_and(vec_perm(pix2v[0], pix2v[1], perm2), permclear);
412 Since we want to use unsigned chars, we can take advantage
413 of the fact that abs(a-b)^2 = (a-b)^2.
416 /* Calculate abs differences vector */
417 t3 = vec_max(t1, t2);
418 t4 = vec_min(t1, t2);
419 t5 = vec_sub(t3, t4);
421 /* Square the values and add them to our sum */
422 sum = vec_msum(t5, t5, sum);
428 /* Sum up the four partial sums, and put the result into s */
429 sumsqr = vec_sums((vector signed int) sum, (vector signed int) zero);
430 sumsqr = vec_splat(sumsqr, 3);
431 vec_ste(sumsqr, 0, &s);
437 * Sum of Squared Errors for a 16x16 block.
439 * It's the sad16_altivec code above w/ squaring added.
441 int sse16_altivec(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
444 DECLARE_ALIGNED_16(int, s);
445 const_vector unsigned int zero = (const_vector unsigned int)vec_splat_u32(0);
446 vector unsigned char perm1, perm2, *pix1v, *pix2v;
447 vector unsigned char t1, t2, t3,t4, t5;
448 vector unsigned int sum;
449 vector signed int sumsqr;
451 sum = (vector unsigned int)vec_splat_u32(0);
454 /* Read potentially unaligned pixels into t1 and t2 */
455 perm1 = vec_lvsl(0, pix1);
456 pix1v = (vector unsigned char *) pix1;
457 perm2 = vec_lvsl(0, pix2);
458 pix2v = (vector unsigned char *) pix2;
459 t1 = vec_perm(pix1v[0], pix1v[1], perm1);
460 t2 = vec_perm(pix2v[0], pix2v[1], perm2);
463 Since we want to use unsigned chars, we can take advantage
464 of the fact that abs(a-b)^2 = (a-b)^2.
467 /* Calculate abs differences vector */
468 t3 = vec_max(t1, t2);
469 t4 = vec_min(t1, t2);
470 t5 = vec_sub(t3, t4);
472 /* Square the values and add them to our sum */
473 sum = vec_msum(t5, t5, sum);
479 /* Sum up the four partial sums, and put the result into s */
480 sumsqr = vec_sums((vector signed int) sum, (vector signed int) zero);
481 sumsqr = vec_splat(sumsqr, 3);
482 vec_ste(sumsqr, 0, &s);
487 int pix_sum_altivec(uint8_t * pix, int line_size)
489 const_vector unsigned int zero = (const_vector unsigned int)vec_splat_u32(0);
490 vector unsigned char perm, *pixv;
491 vector unsigned char t1;
492 vector unsigned int sad;
493 vector signed int sumdiffs;
496 DECLARE_ALIGNED_16(int, s);
498 sad = (vector unsigned int)vec_splat_u32(0);
500 for (i = 0; i < 16; i++) {
501 /* Read the potentially unaligned 16 pixels into t1 */
502 perm = vec_lvsl(0, pix);
503 pixv = (vector unsigned char *) pix;
504 t1 = vec_perm(pixv[0], pixv[1], perm);
506 /* Add each 4 pixel group together and put 4 results into sad */
507 sad = vec_sum4s(t1, sad);
512 /* Sum up the four partial sums, and put the result into s */
513 sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
514 sumdiffs = vec_splat(sumdiffs, 3);
515 vec_ste(sumdiffs, 0, &s);
520 void get_pixels_altivec(DCTELEM *restrict block, const uint8_t *pixels, int line_size)
523 vector unsigned char perm, bytes, *pixv;
524 const_vector unsigned char zero = (const_vector unsigned char)vec_splat_u8(0);
525 vector signed short shorts;
529 // Read potentially unaligned pixels.
530 // We're reading 16 pixels, and actually only want 8,
531 // but we simply ignore the extras.
532 perm = vec_lvsl(0, pixels);
533 pixv = (vector unsigned char *) pixels;
534 bytes = vec_perm(pixv[0], pixv[1], perm);
536 // convert the bytes into shorts
537 shorts = (vector signed short)vec_mergeh(zero, bytes);
539 // save the data to the block, we assume the block is 16-byte aligned
540 vec_st(shorts, i*16, (vector signed short*)block);
546 void diff_pixels_altivec(DCTELEM *restrict block, const uint8_t *s1,
547 const uint8_t *s2, int stride)
550 vector unsigned char perm, bytes, *pixv;
551 const_vector unsigned char zero = (const_vector unsigned char)vec_splat_u8(0);
552 vector signed short shorts1, shorts2;
556 // Read potentially unaligned pixels
557 // We're reading 16 pixels, and actually only want 8,
558 // but we simply ignore the extras.
559 perm = vec_lvsl(0, s1);
560 pixv = (vector unsigned char *) s1;
561 bytes = vec_perm(pixv[0], pixv[1], perm);
563 // convert the bytes into shorts
564 shorts1 = (vector signed short)vec_mergeh(zero, bytes);
566 // Do the same for the second block of pixels
567 perm = vec_lvsl(0, s2);
568 pixv = (vector unsigned char *) s2;
569 bytes = vec_perm(pixv[0], pixv[1], perm);
571 // convert the bytes into shorts
572 shorts2 = (vector signed short)vec_mergeh(zero, bytes);
574 // Do the subtraction
575 shorts1 = vec_sub(shorts1, shorts2);
577 // save the data to the block, we assume the block is 16-byte aligned
578 vec_st(shorts1, 0, (vector signed short*)block);
585 // The code below is a copy of the code above... This is a manual
588 // Read potentially unaligned pixels
589 // We're reading 16 pixels, and actually only want 8,
590 // but we simply ignore the extras.
591 perm = vec_lvsl(0, s1);
592 pixv = (vector unsigned char *) s1;
593 bytes = vec_perm(pixv[0], pixv[1], perm);
595 // convert the bytes into shorts
596 shorts1 = (vector signed short)vec_mergeh(zero, bytes);
598 // Do the same for the second block of pixels
599 perm = vec_lvsl(0, s2);
600 pixv = (vector unsigned char *) s2;
601 bytes = vec_perm(pixv[0], pixv[1], perm);
603 // convert the bytes into shorts
604 shorts2 = (vector signed short)vec_mergeh(zero, bytes);
606 // Do the subtraction
607 shorts1 = vec_sub(shorts1, shorts2);
609 // save the data to the block, we assume the block is 16-byte aligned
610 vec_st(shorts1, 0, (vector signed short*)block);
618 void add_bytes_altivec(uint8_t *dst, uint8_t *src, int w) {
620 register vector unsigned char vdst, vsrc;
622 /* dst and src are 16 bytes-aligned (guaranteed) */
623 for(i = 0 ; (i + 15) < w ; i+=16)
625 vdst = vec_ld(i, (unsigned char*)dst);
626 vsrc = vec_ld(i, (unsigned char*)src);
627 vdst = vec_add(vsrc, vdst);
628 vec_st(vdst, i, (unsigned char*)dst);
630 /* if w is not a multiple of 16 */
631 for (; (i < w) ; i++)
637 /* next one assumes that ((line_size % 16) == 0) */
638 void put_pixels16_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
640 POWERPC_PERF_DECLARE(altivec_put_pixels16_num, 1);
641 register vector unsigned char pixelsv1, pixelsv2;
642 register vector unsigned char pixelsv1B, pixelsv2B;
643 register vector unsigned char pixelsv1C, pixelsv2C;
644 register vector unsigned char pixelsv1D, pixelsv2D;
646 register vector unsigned char perm = vec_lvsl(0, pixels);
648 register int line_size_2 = line_size << 1;
649 register int line_size_3 = line_size + line_size_2;
650 register int line_size_4 = line_size << 2;
652 POWERPC_PERF_START_COUNT(altivec_put_pixels16_num, 1);
653 // hand-unrolling the loop by 4 gains about 15%
654 // mininum execution time goes from 74 to 60 cycles
655 // it's faster than -funroll-loops, but using
656 // -funroll-loops w/ this is bad - 74 cycles again.
657 // all this is on a 7450, tuning for the 7450
660 pixelsv1 = vec_ld(0, (unsigned char*)pixels);
661 pixelsv2 = vec_ld(16, (unsigned char*)pixels);
662 vec_st(vec_perm(pixelsv1, pixelsv2, perm),
663 0, (unsigned char*)block);
668 for(i=0; i<h; i+=4) {
669 pixelsv1 = vec_ld(0, (unsigned char*)pixels);
670 pixelsv2 = vec_ld(15, (unsigned char*)pixels);
671 pixelsv1B = vec_ld(line_size, (unsigned char*)pixels);
672 pixelsv2B = vec_ld(15 + line_size, (unsigned char*)pixels);
673 pixelsv1C = vec_ld(line_size_2, (unsigned char*)pixels);
674 pixelsv2C = vec_ld(15 + line_size_2, (unsigned char*)pixels);
675 pixelsv1D = vec_ld(line_size_3, (unsigned char*)pixels);
676 pixelsv2D = vec_ld(15 + line_size_3, (unsigned char*)pixels);
677 vec_st(vec_perm(pixelsv1, pixelsv2, perm),
678 0, (unsigned char*)block);
679 vec_st(vec_perm(pixelsv1B, pixelsv2B, perm),
680 line_size, (unsigned char*)block);
681 vec_st(vec_perm(pixelsv1C, pixelsv2C, perm),
682 line_size_2, (unsigned char*)block);
683 vec_st(vec_perm(pixelsv1D, pixelsv2D, perm),
684 line_size_3, (unsigned char*)block);
689 POWERPC_PERF_STOP_COUNT(altivec_put_pixels16_num, 1);
692 /* next one assumes that ((line_size % 16) == 0) */
693 #define op_avg(a,b) a = ( ((a)|(b)) - ((((a)^(b))&0xFEFEFEFEUL)>>1) )
694 void avg_pixels16_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
696 POWERPC_PERF_DECLARE(altivec_avg_pixels16_num, 1);
697 register vector unsigned char pixelsv1, pixelsv2, pixelsv, blockv;
698 register vector unsigned char perm = vec_lvsl(0, pixels);
701 POWERPC_PERF_START_COUNT(altivec_avg_pixels16_num, 1);
704 pixelsv1 = vec_ld(0, (unsigned char*)pixels);
705 pixelsv2 = vec_ld(16, (unsigned char*)pixels);
706 blockv = vec_ld(0, block);
707 pixelsv = vec_perm(pixelsv1, pixelsv2, perm);
708 blockv = vec_avg(blockv,pixelsv);
709 vec_st(blockv, 0, (unsigned char*)block);
714 POWERPC_PERF_STOP_COUNT(altivec_avg_pixels16_num, 1);
717 /* next one assumes that ((line_size % 8) == 0) */
718 void avg_pixels8_altivec(uint8_t * block, const uint8_t * pixels, int line_size, int h)
720 POWERPC_PERF_DECLARE(altivec_avg_pixels8_num, 1);
721 register vector unsigned char pixelsv1, pixelsv2, pixelsv, blockv;
724 POWERPC_PERF_START_COUNT(altivec_avg_pixels8_num, 1);
726 for (i = 0; i < h; i++) {
728 block is 8 bytes-aligned, so we're either in the
729 left block (16 bytes-aligned) or in the right block (not)
731 int rightside = ((unsigned long)block & 0x0000000F);
733 blockv = vec_ld(0, block);
734 pixelsv1 = vec_ld(0, (unsigned char*)pixels);
735 pixelsv2 = vec_ld(16, (unsigned char*)pixels);
736 pixelsv = vec_perm(pixelsv1, pixelsv2, vec_lvsl(0, pixels));
740 pixelsv = vec_perm(blockv, pixelsv, vcprm(0,1,s0,s1));
744 pixelsv = vec_perm(blockv, pixelsv, vcprm(s0,s1,2,3));
747 blockv = vec_avg(blockv, pixelsv);
749 vec_st(blockv, 0, block);
755 POWERPC_PERF_STOP_COUNT(altivec_avg_pixels8_num, 1);
758 /* next one assumes that ((line_size % 8) == 0) */
759 void put_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
761 POWERPC_PERF_DECLARE(altivec_put_pixels8_xy2_num, 1);
763 register vector unsigned char
766 register vector unsigned char
767 blockv, temp1, temp2;
768 register vector unsigned short
769 pixelssum1, pixelssum2, temp3;
770 register const_vector unsigned char vczero = (const_vector unsigned char)vec_splat_u8(0);
771 register const_vector unsigned short vctwo = (const_vector unsigned short)vec_splat_u16(2);
773 temp1 = vec_ld(0, pixels);
774 temp2 = vec_ld(16, pixels);
775 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
776 if ((((unsigned long)pixels) & 0x0000000F) == 0x0000000F)
782 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
784 pixelsv1 = vec_mergeh(vczero, pixelsv1);
785 pixelsv2 = vec_mergeh(vczero, pixelsv2);
786 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
787 (vector unsigned short)pixelsv2);
788 pixelssum1 = vec_add(pixelssum1, vctwo);
790 POWERPC_PERF_START_COUNT(altivec_put_pixels8_xy2_num, 1);
791 for (i = 0; i < h ; i++) {
792 int rightside = ((unsigned long)block & 0x0000000F);
793 blockv = vec_ld(0, block);
795 temp1 = vec_ld(line_size, pixels);
796 temp2 = vec_ld(line_size + 16, pixels);
797 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
798 if (((((unsigned long)pixels) + line_size) & 0x0000000F) == 0x0000000F)
804 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
807 pixelsv1 = vec_mergeh(vczero, pixelsv1);
808 pixelsv2 = vec_mergeh(vczero, pixelsv2);
809 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
810 (vector unsigned short)pixelsv2);
811 temp3 = vec_add(pixelssum1, pixelssum2);
812 temp3 = vec_sra(temp3, vctwo);
813 pixelssum1 = vec_add(pixelssum2, vctwo);
814 pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
818 blockv = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
822 blockv = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
825 vec_st(blockv, 0, block);
831 POWERPC_PERF_STOP_COUNT(altivec_put_pixels8_xy2_num, 1);
834 /* next one assumes that ((line_size % 8) == 0) */
835 void put_no_rnd_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
837 POWERPC_PERF_DECLARE(altivec_put_no_rnd_pixels8_xy2_num, 1);
839 register vector unsigned char
842 register vector unsigned char
843 blockv, temp1, temp2;
844 register vector unsigned short
845 pixelssum1, pixelssum2, temp3;
846 register const_vector unsigned char vczero = (const_vector unsigned char)vec_splat_u8(0);
847 register const_vector unsigned short vcone = (const_vector unsigned short)vec_splat_u16(1);
848 register const_vector unsigned short vctwo = (const_vector unsigned short)vec_splat_u16(2);
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)
859 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
861 pixelsv1 = vec_mergeh(vczero, pixelsv1);
862 pixelsv2 = vec_mergeh(vczero, pixelsv2);
863 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
864 (vector unsigned short)pixelsv2);
865 pixelssum1 = vec_add(pixelssum1, vcone);
867 POWERPC_PERF_START_COUNT(altivec_put_no_rnd_pixels8_xy2_num, 1);
868 for (i = 0; i < h ; i++) {
869 int rightside = ((unsigned long)block & 0x0000000F);
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)
881 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
884 pixelsv1 = vec_mergeh(vczero, pixelsv1);
885 pixelsv2 = vec_mergeh(vczero, pixelsv2);
886 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
887 (vector unsigned short)pixelsv2);
888 temp3 = vec_add(pixelssum1, pixelssum2);
889 temp3 = vec_sra(temp3, vctwo);
890 pixelssum1 = vec_add(pixelssum2, vcone);
891 pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
895 blockv = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
899 blockv = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
902 vec_st(blockv, 0, block);
908 POWERPC_PERF_STOP_COUNT(altivec_put_no_rnd_pixels8_xy2_num, 1);
911 /* next one assumes that ((line_size % 16) == 0) */
912 void put_pixels16_xy2_altivec(uint8_t * block, const uint8_t * pixels, int line_size, int h)
914 POWERPC_PERF_DECLARE(altivec_put_pixels16_xy2_num, 1);
916 register vector unsigned char
917 pixelsv1, pixelsv2, pixelsv3, pixelsv4;
918 register vector unsigned char
919 blockv, temp1, temp2;
920 register vector unsigned short
921 pixelssum1, pixelssum2, temp3,
922 pixelssum3, pixelssum4, temp4;
923 register const_vector unsigned char vczero = (const_vector unsigned char)vec_splat_u8(0);
924 register const_vector unsigned short vctwo = (const_vector unsigned short)vec_splat_u16(2);
926 POWERPC_PERF_START_COUNT(altivec_put_pixels16_xy2_num, 1);
928 temp1 = vec_ld(0, pixels);
929 temp2 = vec_ld(16, pixels);
930 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
931 if ((((unsigned long)pixels) & 0x0000000F) == 0x0000000F)
937 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
939 pixelsv3 = vec_mergel(vczero, pixelsv1);
940 pixelsv4 = vec_mergel(vczero, pixelsv2);
941 pixelsv1 = vec_mergeh(vczero, pixelsv1);
942 pixelsv2 = vec_mergeh(vczero, pixelsv2);
943 pixelssum3 = vec_add((vector unsigned short)pixelsv3,
944 (vector unsigned short)pixelsv4);
945 pixelssum3 = vec_add(pixelssum3, vctwo);
946 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
947 (vector unsigned short)pixelsv2);
948 pixelssum1 = vec_add(pixelssum1, vctwo);
950 for (i = 0; i < h ; i++) {
951 blockv = vec_ld(0, block);
953 temp1 = vec_ld(line_size, pixels);
954 temp2 = vec_ld(line_size + 16, pixels);
955 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
956 if (((((unsigned long)pixels) + line_size) & 0x0000000F) == 0x0000000F)
962 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
965 pixelsv3 = vec_mergel(vczero, pixelsv1);
966 pixelsv4 = vec_mergel(vczero, pixelsv2);
967 pixelsv1 = vec_mergeh(vczero, pixelsv1);
968 pixelsv2 = vec_mergeh(vczero, pixelsv2);
970 pixelssum4 = vec_add((vector unsigned short)pixelsv3,
971 (vector unsigned short)pixelsv4);
972 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
973 (vector unsigned short)pixelsv2);
974 temp4 = vec_add(pixelssum3, pixelssum4);
975 temp4 = vec_sra(temp4, vctwo);
976 temp3 = vec_add(pixelssum1, pixelssum2);
977 temp3 = vec_sra(temp3, vctwo);
979 pixelssum3 = vec_add(pixelssum4, vctwo);
980 pixelssum1 = vec_add(pixelssum2, vctwo);
982 blockv = vec_packsu(temp3, temp4);
984 vec_st(blockv, 0, block);
990 POWERPC_PERF_STOP_COUNT(altivec_put_pixels16_xy2_num, 1);
993 /* next one assumes that ((line_size % 16) == 0) */
994 void put_no_rnd_pixels16_xy2_altivec(uint8_t * block, const uint8_t * pixels, int line_size, int h)
996 POWERPC_PERF_DECLARE(altivec_put_no_rnd_pixels16_xy2_num, 1);
998 register vector unsigned char
999 pixelsv1, pixelsv2, pixelsv3, pixelsv4;
1000 register vector unsigned char
1001 blockv, temp1, temp2;
1002 register vector unsigned short
1003 pixelssum1, pixelssum2, temp3,
1004 pixelssum3, pixelssum4, temp4;
1005 register const_vector unsigned char vczero = (const_vector unsigned char)vec_splat_u8(0);
1006 register const_vector unsigned short vcone = (const_vector unsigned short)vec_splat_u16(1);
1007 register const_vector unsigned short vctwo = (const_vector unsigned short)vec_splat_u16(2);
1009 POWERPC_PERF_START_COUNT(altivec_put_no_rnd_pixels16_xy2_num, 1);
1011 temp1 = vec_ld(0, pixels);
1012 temp2 = vec_ld(16, pixels);
1013 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
1014 if ((((unsigned long)pixels) & 0x0000000F) == 0x0000000F)
1020 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
1022 pixelsv3 = vec_mergel(vczero, pixelsv1);
1023 pixelsv4 = vec_mergel(vczero, pixelsv2);
1024 pixelsv1 = vec_mergeh(vczero, pixelsv1);
1025 pixelsv2 = vec_mergeh(vczero, pixelsv2);
1026 pixelssum3 = vec_add((vector unsigned short)pixelsv3,
1027 (vector unsigned short)pixelsv4);
1028 pixelssum3 = vec_add(pixelssum3, vcone);
1029 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
1030 (vector unsigned short)pixelsv2);
1031 pixelssum1 = vec_add(pixelssum1, vcone);
1033 for (i = 0; i < h ; i++) {
1034 blockv = vec_ld(0, block);
1036 temp1 = vec_ld(line_size, pixels);
1037 temp2 = vec_ld(line_size + 16, pixels);
1038 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
1039 if (((((unsigned long)pixels) + line_size) & 0x0000000F) == 0x0000000F)
1045 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
1048 pixelsv3 = vec_mergel(vczero, pixelsv1);
1049 pixelsv4 = vec_mergel(vczero, pixelsv2);
1050 pixelsv1 = vec_mergeh(vczero, pixelsv1);
1051 pixelsv2 = vec_mergeh(vczero, pixelsv2);
1053 pixelssum4 = vec_add((vector unsigned short)pixelsv3,
1054 (vector unsigned short)pixelsv4);
1055 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
1056 (vector unsigned short)pixelsv2);
1057 temp4 = vec_add(pixelssum3, pixelssum4);
1058 temp4 = vec_sra(temp4, vctwo);
1059 temp3 = vec_add(pixelssum1, pixelssum2);
1060 temp3 = vec_sra(temp3, vctwo);
1062 pixelssum3 = vec_add(pixelssum4, vcone);
1063 pixelssum1 = vec_add(pixelssum2, vcone);
1065 blockv = vec_packsu(temp3, temp4);
1067 vec_st(blockv, 0, block);
1070 pixels += line_size;
1073 POWERPC_PERF_STOP_COUNT(altivec_put_no_rnd_pixels16_xy2_num, 1);
1076 int hadamard8_diff8x8_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h){
1077 POWERPC_PERF_DECLARE(altivec_hadamard8_diff8x8_num, 1);
1079 register const_vector unsigned char vzero =
1080 (const_vector unsigned char)vec_splat_u8(0);
1081 register vector signed short temp0, temp1, temp2, temp3, temp4,
1082 temp5, temp6, temp7;
1083 POWERPC_PERF_START_COUNT(altivec_hadamard8_diff8x8_num, 1);
1085 register const_vector signed short vprod1 =(const_vector signed short)
1086 AVV( 1,-1, 1,-1, 1,-1, 1,-1);
1087 register const_vector signed short vprod2 =(const_vector signed short)
1088 AVV( 1, 1,-1,-1, 1, 1,-1,-1);
1089 register const_vector signed short vprod3 =(const_vector signed short)
1090 AVV( 1, 1, 1, 1,-1,-1,-1,-1);
1091 register const_vector unsigned char perm1 = (const_vector unsigned char)
1092 AVV(0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
1093 0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D);
1094 register const_vector unsigned char perm2 = (const_vector unsigned char)
1095 AVV(0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
1096 0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B);
1097 register const_vector unsigned char perm3 = (const_vector unsigned char)
1098 AVV(0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1099 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07);
1101 #define ONEITERBUTTERFLY(i, res) \
1103 register vector unsigned char src1, src2, srcO; \
1104 register vector unsigned char dst1, dst2, dstO; \
1105 register vector signed short srcV, dstV; \
1106 register vector signed short but0, but1, but2, op1, op2, op3; \
1107 src1 = vec_ld(stride * i, src); \
1108 src2 = vec_ld((stride * i) + 15, src); \
1109 srcO = vec_perm(src1, src2, vec_lvsl(stride * i, src)); \
1110 dst1 = vec_ld(stride * i, dst); \
1111 dst2 = vec_ld((stride * i) + 15, dst); \
1112 dstO = vec_perm(dst1, dst2, vec_lvsl(stride * i, dst)); \
1113 /* promote the unsigned chars to signed shorts */ \
1114 /* we're in the 8x8 function, we only care for the first 8 */ \
1116 (vector signed short)vec_mergeh((vector signed char)vzero, \
1117 (vector signed char)srcO); \
1119 (vector signed short)vec_mergeh((vector signed char)vzero, \
1120 (vector signed char)dstO); \
1121 /* substractions inside the first butterfly */ \
1122 but0 = vec_sub(srcV, dstV); \
1123 op1 = vec_perm(but0, but0, perm1); \
1124 but1 = vec_mladd(but0, vprod1, op1); \
1125 op2 = vec_perm(but1, but1, perm2); \
1126 but2 = vec_mladd(but1, vprod2, op2); \
1127 op3 = vec_perm(but2, but2, perm3); \
1128 res = vec_mladd(but2, vprod3, op3); \
1130 ONEITERBUTTERFLY(0, temp0);
1131 ONEITERBUTTERFLY(1, temp1);
1132 ONEITERBUTTERFLY(2, temp2);
1133 ONEITERBUTTERFLY(3, temp3);
1134 ONEITERBUTTERFLY(4, temp4);
1135 ONEITERBUTTERFLY(5, temp5);
1136 ONEITERBUTTERFLY(6, temp6);
1137 ONEITERBUTTERFLY(7, temp7);
1139 #undef ONEITERBUTTERFLY
1141 register vector signed int vsum;
1142 register vector signed short line0 = vec_add(temp0, temp1);
1143 register vector signed short line1 = vec_sub(temp0, temp1);
1144 register vector signed short line2 = vec_add(temp2, temp3);
1145 register vector signed short line3 = vec_sub(temp2, temp3);
1146 register vector signed short line4 = vec_add(temp4, temp5);
1147 register vector signed short line5 = vec_sub(temp4, temp5);
1148 register vector signed short line6 = vec_add(temp6, temp7);
1149 register vector signed short line7 = vec_sub(temp6, temp7);
1151 register vector signed short line0B = vec_add(line0, line2);
1152 register vector signed short line2B = vec_sub(line0, line2);
1153 register vector signed short line1B = vec_add(line1, line3);
1154 register vector signed short line3B = vec_sub(line1, line3);
1155 register vector signed short line4B = vec_add(line4, line6);
1156 register vector signed short line6B = vec_sub(line4, line6);
1157 register vector signed short line5B = vec_add(line5, line7);
1158 register vector signed short line7B = vec_sub(line5, line7);
1160 register vector signed short line0C = vec_add(line0B, line4B);
1161 register vector signed short line4C = vec_sub(line0B, line4B);
1162 register vector signed short line1C = vec_add(line1B, line5B);
1163 register vector signed short line5C = vec_sub(line1B, line5B);
1164 register vector signed short line2C = vec_add(line2B, line6B);
1165 register vector signed short line6C = vec_sub(line2B, line6B);
1166 register vector signed short line3C = vec_add(line3B, line7B);
1167 register vector signed short line7C = vec_sub(line3B, line7B);
1169 vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
1170 vsum = vec_sum4s(vec_abs(line1C), vsum);
1171 vsum = vec_sum4s(vec_abs(line2C), vsum);
1172 vsum = vec_sum4s(vec_abs(line3C), vsum);
1173 vsum = vec_sum4s(vec_abs(line4C), vsum);
1174 vsum = vec_sum4s(vec_abs(line5C), vsum);
1175 vsum = vec_sum4s(vec_abs(line6C), vsum);
1176 vsum = vec_sum4s(vec_abs(line7C), vsum);
1177 vsum = vec_sums(vsum, (vector signed int)vzero);
1178 vsum = vec_splat(vsum, 3);
1179 vec_ste(vsum, 0, &sum);
1181 POWERPC_PERF_STOP_COUNT(altivec_hadamard8_diff8x8_num, 1);
1186 16x8 works with 16 elements ; it allows to avoid replicating
1187 loads, and give the compiler more rooms for scheduling.
1188 It's only used from inside hadamard8_diff16_altivec.
1190 Unfortunately, it seems gcc-3.3 is a bit dumb, and
1191 the compiled code has a LOT of spill code, it seems
1192 gcc (unlike xlc) cannot keep everything in registers
1193 by itself. The following code include hand-made
1194 registers allocation. It's not clean, but on
1195 a 7450 the resulting code is much faster (best case
1196 fall from 700+ cycles to 550).
1198 xlc doesn't add spill code, but it doesn't know how to
1199 schedule for the 7450, and its code isn't much faster than
1200 gcc-3.3 on the 7450 (but uses 25% less instructions...)
1202 On the 970, the hand-made RA is still a win (arount 690
1203 vs. around 780), but xlc goes to around 660 on the
1207 static int hadamard8_diff16x8_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h) {
1209 register vector signed short
1218 register vector signed short
1227 register const_vector unsigned char vzero REG_v(v31)=
1228 (const_vector unsigned char)vec_splat_u8(0);
1230 register const_vector signed short vprod1 REG_v(v16)=
1231 (const_vector signed short)AVV( 1,-1, 1,-1, 1,-1, 1,-1);
1232 register const_vector signed short vprod2 REG_v(v17)=
1233 (const_vector signed short)AVV( 1, 1,-1,-1, 1, 1,-1,-1);
1234 register const_vector signed short vprod3 REG_v(v18)=
1235 (const_vector signed short)AVV( 1, 1, 1, 1,-1,-1,-1,-1);
1236 register const_vector unsigned char perm1 REG_v(v19)=
1237 (const_vector unsigned char)
1238 AVV(0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
1239 0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D);
1240 register const_vector unsigned char perm2 REG_v(v20)=
1241 (const_vector unsigned char)
1242 AVV(0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
1243 0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B);
1244 register const_vector unsigned char perm3 REG_v(v21)=
1245 (const_vector unsigned char)
1246 AVV(0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1247 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07);
1249 #define ONEITERBUTTERFLY(i, res1, res2) \
1251 register vector unsigned char src1 REG_v(v22), \
1258 register vector signed short srcV REG_v(v24), \
1275 src1 = vec_ld(stride * i, src); \
1276 src2 = vec_ld((stride * i) + 16, src); \
1277 srcO = vec_perm(src1, src2, vec_lvsl(stride * i, src)); \
1278 dst1 = vec_ld(stride * i, dst); \
1279 dst2 = vec_ld((stride * i) + 16, dst); \
1280 dstO = vec_perm(dst1, dst2, vec_lvsl(stride * i, dst)); \
1281 /* promote the unsigned chars to signed shorts */ \
1283 (vector signed short)vec_mergeh((vector signed char)vzero, \
1284 (vector signed char)srcO); \
1286 (vector signed short)vec_mergeh((vector signed char)vzero, \
1287 (vector signed char)dstO); \
1289 (vector signed short)vec_mergel((vector signed char)vzero, \
1290 (vector signed char)srcO); \
1292 (vector signed short)vec_mergel((vector signed char)vzero, \
1293 (vector signed char)dstO); \
1294 /* substractions inside the first butterfly */ \
1295 but0 = vec_sub(srcV, dstV); \
1296 but0S = vec_sub(srcW, dstW); \
1297 op1 = vec_perm(but0, but0, perm1); \
1298 but1 = vec_mladd(but0, vprod1, op1); \
1299 op1S = vec_perm(but0S, but0S, perm1); \
1300 but1S = vec_mladd(but0S, vprod1, op1S); \
1301 op2 = vec_perm(but1, but1, perm2); \
1302 but2 = vec_mladd(but1, vprod2, op2); \
1303 op2S = vec_perm(but1S, but1S, perm2); \
1304 but2S = vec_mladd(but1S, vprod2, op2S); \
1305 op3 = vec_perm(but2, but2, perm3); \
1306 res1 = vec_mladd(but2, vprod3, op3); \
1307 op3S = vec_perm(but2S, but2S, perm3); \
1308 res2 = vec_mladd(but2S, vprod3, op3S); \
1310 ONEITERBUTTERFLY(0, temp0, temp0S);
1311 ONEITERBUTTERFLY(1, temp1, temp1S);
1312 ONEITERBUTTERFLY(2, temp2, temp2S);
1313 ONEITERBUTTERFLY(3, temp3, temp3S);
1314 ONEITERBUTTERFLY(4, temp4, temp4S);
1315 ONEITERBUTTERFLY(5, temp5, temp5S);
1316 ONEITERBUTTERFLY(6, temp6, temp6S);
1317 ONEITERBUTTERFLY(7, temp7, temp7S);
1319 #undef ONEITERBUTTERFLY
1321 register vector signed int vsum;
1322 register vector signed short line0S, line1S, line2S, line3S, line4S,
1323 line5S, line6S, line7S, line0BS,line2BS,
1324 line1BS,line3BS,line4BS,line6BS,line5BS,
1325 line7BS,line0CS,line4CS,line1CS,line5CS,
1326 line2CS,line6CS,line3CS,line7CS;
1328 register vector signed short line0 = vec_add(temp0, temp1);
1329 register vector signed short line1 = vec_sub(temp0, temp1);
1330 register vector signed short line2 = vec_add(temp2, temp3);
1331 register vector signed short line3 = vec_sub(temp2, temp3);
1332 register vector signed short line4 = vec_add(temp4, temp5);
1333 register vector signed short line5 = vec_sub(temp4, temp5);
1334 register vector signed short line6 = vec_add(temp6, temp7);
1335 register vector signed short line7 = vec_sub(temp6, temp7);
1337 register vector signed short line0B = vec_add(line0, line2);
1338 register vector signed short line2B = vec_sub(line0, line2);
1339 register vector signed short line1B = vec_add(line1, line3);
1340 register vector signed short line3B = vec_sub(line1, line3);
1341 register vector signed short line4B = vec_add(line4, line6);
1342 register vector signed short line6B = vec_sub(line4, line6);
1343 register vector signed short line5B = vec_add(line5, line7);
1344 register vector signed short line7B = vec_sub(line5, line7);
1346 register vector signed short line0C = vec_add(line0B, line4B);
1347 register vector signed short line4C = vec_sub(line0B, line4B);
1348 register vector signed short line1C = vec_add(line1B, line5B);
1349 register vector signed short line5C = vec_sub(line1B, line5B);
1350 register vector signed short line2C = vec_add(line2B, line6B);
1351 register vector signed short line6C = vec_sub(line2B, line6B);
1352 register vector signed short line3C = vec_add(line3B, line7B);
1353 register vector signed short line7C = vec_sub(line3B, line7B);
1355 vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
1356 vsum = vec_sum4s(vec_abs(line1C), vsum);
1357 vsum = vec_sum4s(vec_abs(line2C), vsum);
1358 vsum = vec_sum4s(vec_abs(line3C), vsum);
1359 vsum = vec_sum4s(vec_abs(line4C), vsum);
1360 vsum = vec_sum4s(vec_abs(line5C), vsum);
1361 vsum = vec_sum4s(vec_abs(line6C), vsum);
1362 vsum = vec_sum4s(vec_abs(line7C), vsum);
1364 line0S = vec_add(temp0S, temp1S);
1365 line1S = vec_sub(temp0S, temp1S);
1366 line2S = vec_add(temp2S, temp3S);
1367 line3S = vec_sub(temp2S, temp3S);
1368 line4S = vec_add(temp4S, temp5S);
1369 line5S = vec_sub(temp4S, temp5S);
1370 line6S = vec_add(temp6S, temp7S);
1371 line7S = vec_sub(temp6S, temp7S);
1373 line0BS = vec_add(line0S, line2S);
1374 line2BS = vec_sub(line0S, line2S);
1375 line1BS = vec_add(line1S, line3S);
1376 line3BS = vec_sub(line1S, line3S);
1377 line4BS = vec_add(line4S, line6S);
1378 line6BS = vec_sub(line4S, line6S);
1379 line5BS = vec_add(line5S, line7S);
1380 line7BS = vec_sub(line5S, line7S);
1382 line0CS = vec_add(line0BS, line4BS);
1383 line4CS = vec_sub(line0BS, line4BS);
1384 line1CS = vec_add(line1BS, line5BS);
1385 line5CS = vec_sub(line1BS, line5BS);
1386 line2CS = vec_add(line2BS, line6BS);
1387 line6CS = vec_sub(line2BS, line6BS);
1388 line3CS = vec_add(line3BS, line7BS);
1389 line7CS = vec_sub(line3BS, line7BS);
1391 vsum = vec_sum4s(vec_abs(line0CS), vsum);
1392 vsum = vec_sum4s(vec_abs(line1CS), vsum);
1393 vsum = vec_sum4s(vec_abs(line2CS), vsum);
1394 vsum = vec_sum4s(vec_abs(line3CS), vsum);
1395 vsum = vec_sum4s(vec_abs(line4CS), vsum);
1396 vsum = vec_sum4s(vec_abs(line5CS), vsum);
1397 vsum = vec_sum4s(vec_abs(line6CS), vsum);
1398 vsum = vec_sum4s(vec_abs(line7CS), vsum);
1399 vsum = vec_sums(vsum, (vector signed int)vzero);
1400 vsum = vec_splat(vsum, 3);
1401 vec_ste(vsum, 0, &sum);
1406 int hadamard8_diff16_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h){
1407 POWERPC_PERF_DECLARE(altivec_hadamard8_diff16_num, 1);
1409 POWERPC_PERF_START_COUNT(altivec_hadamard8_diff16_num, 1);
1410 score = hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
1414 score += hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
1416 POWERPC_PERF_STOP_COUNT(altivec_hadamard8_diff16_num, 1);
1420 int has_altivec(void)
1424 extern struct ExecIFace *IExec;
1426 IExec->GetCPUInfoTags(GCIT_VectorUnit, &result, TAG_DONE);
1427 if (result == VECTORTYPE_ALTIVEC) return 1;
1429 #else /* __AMIGAOS4__ */
1431 #ifdef CONFIG_DARWIN
1432 int sels[2] = {CTL_HW, HW_VECTORUNIT};
1434 size_t len = sizeof(has_vu);
1437 err = sysctl(sels, 2, &has_vu, &len, NULL, 0);
1439 if (err == 0) return (has_vu != 0);
1440 #else /* CONFIG_DARWIN */
1441 /* no Darwin, do it the brute-force way */
1442 /* this is borrowed from the libmpeg2 library */
1444 signal (SIGILL, sigill_handler);
1445 if (sigsetjmp (jmpbuf, 1)) {
1446 signal (SIGILL, SIG_DFL);
1450 asm volatile ("mtspr 256, %0\n\t"
1451 "vand %%v0, %%v0, %%v0"
1455 signal (SIGILL, SIG_DFL);
1459 #endif /* CONFIG_DARWIN */
1461 #endif /* __AMIGAOS4__ */
1464 static void vorbis_inverse_coupling_altivec(float *mag, float *ang,
1469 vector bool int t0, t1;
1470 const vector unsigned int v_31 = //XXX
1471 vec_add(vec_add(vec_splat_u32(15),vec_splat_u32(15)),vec_splat_u32(1));
1472 for(i=0; i<blocksize; i+=4) {
1473 m = vec_ld(0, mag+i);
1474 a = vec_ld(0, ang+i);
1475 t0 = vec_cmple(m, (vector float)vec_splat_u32(0));
1476 t1 = vec_cmple(a, (vector float)vec_splat_u32(0));
1477 a = vec_xor(a, (vector float) vec_sl((vector unsigned int)t0, v_31));
1478 t0 = (vector bool int)vec_and(a, t1);
1479 t1 = (vector bool int)vec_andc(a, t1);
1480 a = vec_sub(m, (vector float)t1);
1481 m = vec_add(m, (vector float)t0);
1482 vec_stl(a, 0, ang+i);
1483 vec_stl(m, 0, mag+i);
1487 /* next one assumes that ((line_size % 8) == 0) */
1488 void avg_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, int line_size, int h)
1490 POWERPC_PERF_DECLARE(altivec_avg_pixels8_xy2_num, 1);
1492 register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
1493 register vector unsigned char blockv, temp1, temp2, blocktemp;
1494 register vector unsigned short pixelssum1, pixelssum2, temp3;
1496 register const_vector unsigned char vczero = (const_vector unsigned char)
1498 register const_vector unsigned short vctwo = (const_vector unsigned short)
1501 temp1 = vec_ld(0, pixels);
1502 temp2 = vec_ld(16, pixels);
1503 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
1504 if ((((unsigned long)pixels) & 0x0000000F) == 0x0000000F) {
1507 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
1509 pixelsv1 = vec_mergeh(vczero, pixelsv1);
1510 pixelsv2 = vec_mergeh(vczero, pixelsv2);
1511 pixelssum1 = vec_add((vector unsigned short)pixelsv1,
1512 (vector unsigned short)pixelsv2);
1513 pixelssum1 = vec_add(pixelssum1, vctwo);
1515 POWERPC_PERF_START_COUNT(altivec_avg_pixels8_xy2_num, 1);
1516 for (i = 0; i < h ; i++) {
1517 int rightside = ((unsigned long)block & 0x0000000F);
1518 blockv = vec_ld(0, block);
1520 temp1 = vec_ld(line_size, pixels);
1521 temp2 = vec_ld(line_size + 16, pixels);
1522 pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
1523 if (((((unsigned long)pixels) + line_size) & 0x0000000F) == 0x0000000F)
1527 pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
1530 pixelsv1 = vec_mergeh(vczero, pixelsv1);
1531 pixelsv2 = vec_mergeh(vczero, pixelsv2);
1532 pixelssum2 = vec_add((vector unsigned short)pixelsv1,
1533 (vector unsigned short)pixelsv2);
1534 temp3 = vec_add(pixelssum1, pixelssum2);
1535 temp3 = vec_sra(temp3, vctwo);
1536 pixelssum1 = vec_add(pixelssum2, vctwo);
1537 pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
1540 blocktemp = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
1542 blocktemp = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
1545 blockv = vec_avg(blocktemp, blockv);
1546 vec_st(blockv, 0, block);
1549 pixels += line_size;
1552 POWERPC_PERF_STOP_COUNT(altivec_avg_pixels8_xy2_num, 1);
1555 void dsputil_init_altivec(DSPContext* c, AVCodecContext *avctx)
1557 c->pix_abs[0][1] = sad16_x2_altivec;
1558 c->pix_abs[0][2] = sad16_y2_altivec;
1559 c->pix_abs[0][3] = sad16_xy2_altivec;
1560 c->pix_abs[0][0] = sad16_altivec;
1561 c->pix_abs[1][0] = sad8_altivec;
1562 c->sad[0]= sad16_altivec;
1563 c->sad[1]= sad8_altivec;
1564 c->pix_norm1 = pix_norm1_altivec;
1565 c->sse[1]= sse8_altivec;
1566 c->sse[0]= sse16_altivec;
1567 c->pix_sum = pix_sum_altivec;
1568 c->diff_pixels = diff_pixels_altivec;
1569 c->get_pixels = get_pixels_altivec;
1570 c->add_bytes= add_bytes_altivec;
1571 c->put_pixels_tab[0][0] = put_pixels16_altivec;
1572 /* the two functions do the same thing, so use the same code */
1573 c->put_no_rnd_pixels_tab[0][0] = put_pixels16_altivec;
1574 c->avg_pixels_tab[0][0] = avg_pixels16_altivec;
1575 c->avg_pixels_tab[1][0] = avg_pixels8_altivec;
1576 c->avg_pixels_tab[1][3] = avg_pixels8_xy2_altivec;
1577 c->put_pixels_tab[1][3] = put_pixels8_xy2_altivec;
1578 c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy2_altivec;
1579 c->put_pixels_tab[0][3] = put_pixels16_xy2_altivec;
1580 c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_altivec;
1582 c->hadamard8_diff[0] = hadamard8_diff16_altivec;
1583 c->hadamard8_diff[1] = hadamard8_diff8x8_altivec;
1584 #ifdef CONFIG_VORBIS_DECODER
1585 c->vorbis_inverse_coupling = vorbis_inverse_coupling_altivec;