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