]> git.sesse.net Git - ffmpeg/blob - libavcodec/ppc/dsputil_altivec.c
Merge commit '0e083d7e43805db1a978cb57bfa25fda62e8ff18'
[ffmpeg] / 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
28 #include "libavutil/attributes.h"
29 #include "libavutil/ppc/types_altivec.h"
30 #include "libavutil/ppc/util_altivec.h"
31 #include "libavcodec/avcodec.h"
32 #include "libavcodec/dsputil.h"
33 #include "dsputil_altivec.h"
34
35 static int sad16_x2_altivec(void *v, uint8_t *pix1, uint8_t *pix2,
36                             int line_size, int h)
37 {
38     int i, s = 0;
39     const vector unsigned char zero =
40         (const vector unsigned char) vec_splat_u8(0);
41     vector unsigned char perm1 = vec_lvsl(0, pix2);
42     vector unsigned char perm2 = vec_add(perm1, vec_splat_u8(1));
43     vector unsigned int sad = (vector unsigned int) vec_splat_u32(0);
44     vector signed int sumdiffs;
45
46     for (i = 0; i < h; i++) {
47         /* Read unaligned pixels into our vectors. The vectors are as follows:
48          * pix1v: pix1[0] - pix1[15]
49          * pix2v: pix2[0] - pix2[15]      pix2iv: pix2[1] - pix2[16] */
50         vector unsigned char pix1v  = vec_ld(0,  pix1);
51         vector unsigned char pix2l  = vec_ld(0,  pix2);
52         vector unsigned char pix2r  = vec_ld(16, pix2);
53         vector unsigned char pix2v  = vec_perm(pix2l, pix2r, perm1);
54         vector unsigned char pix2iv = vec_perm(pix2l, pix2r, perm2);
55
56         /* Calculate the average vector. */
57         vector unsigned char avgv = vec_avg(pix2v, pix2iv);
58
59         /* Calculate a sum of abs differences vector. */
60         vector unsigned char t5 = vec_sub(vec_max(pix1v, avgv),
61                                           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 static int sad16_y2_altivec(void *v, uint8_t *pix1, uint8_t *pix2,
78                             int line_size, int h)
79 {
80     int i, s = 0;
81     const vector unsigned char zero =
82         (const vector unsigned char) vec_splat_u8(0);
83     vector unsigned char perm = vec_lvsl(0, pix2);
84     vector unsigned char pix1v, pix3v, avgv, t5;
85     vector unsigned int sad = (vector unsigned int) vec_splat_u32(0);
86     vector signed int sumdiffs;
87     uint8_t *pix3 = pix2 + line_size;
88
89     /* Due to the fact that pix3 = pix2 + line_size, the pix3 of one
90      * iteration becomes pix2 in the next iteration. We can use this
91      * fact to avoid a potentially expensive unaligned read, each
92      * time around the loop.
93      * Read unaligned pixels into our vectors. The vectors are as follows:
94      * pix2v: pix2[0] - pix2[15]
95      * Split the pixel vectors into shorts. */
96     vector unsigned char pix2l = vec_ld(0,  pix2);
97     vector unsigned char pix2r = vec_ld(15, pix2);
98     vector unsigned char pix2v = vec_perm(pix2l, pix2r, perm);
99
100     for (i = 0; i < h; i++) {
101         /* Read unaligned pixels into our vectors. The vectors are as follows:
102          * pix1v: pix1[0] - pix1[15]
103          * pix3v: pix3[0] - pix3[15] */
104         pix1v = vec_ld(0,  pix1);
105
106         pix2l = vec_ld(0,  pix3);
107         pix2r = vec_ld(15, pix3);
108         pix3v = vec_perm(pix2l, pix2r, perm);
109
110         /* Calculate the average vector. */
111         avgv = vec_avg(pix2v, pix3v);
112
113         /* Calculate a sum of abs differences vector. */
114         t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
115
116         /* Add each 4 pixel group together and put 4 results into sad. */
117         sad = vec_sum4s(t5, sad);
118
119         pix1 += line_size;
120         pix2v = pix3v;
121         pix3 += line_size;
122     }
123
124     /* Sum up the four partial sums, and put the result into s. */
125     sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
126     sumdiffs = vec_splat(sumdiffs, 3);
127     vec_ste(sumdiffs, 0, &s);
128     return s;
129 }
130
131 static int sad16_xy2_altivec(void *v, uint8_t *pix1, uint8_t *pix2,
132                              int line_size, int h)
133 {
134     int i, s = 0;
135     uint8_t *pix3 = pix2 + line_size;
136     const vector unsigned char zero =
137         (const vector unsigned char) vec_splat_u8(0);
138     const vector unsigned short two =
139         (const vector unsigned short) vec_splat_u16(2);
140     vector unsigned char avgv, t5;
141     vector unsigned char perm1 = vec_lvsl(0, pix2);
142     vector unsigned char perm2 = vec_add(perm1, vec_splat_u8(1));
143     vector unsigned char pix1v, pix3v, pix3iv;
144     vector unsigned short pix3lv, pix3hv, pix3ilv, pix3ihv;
145     vector unsigned short avghv, avglv;
146     vector unsigned int sad = (vector unsigned int) vec_splat_u32(0);
147     vector signed int sumdiffs;
148
149     /* Due to the fact that pix3 = pix2 + line_size, the pix3 of one
150      * iteration becomes pix2 in the next iteration. We can use this
151      * fact to avoid a potentially expensive unaligned read, as well
152      * as some splitting, and vector addition each time around the loop.
153      * Read unaligned pixels into our vectors. The vectors are as follows:
154      * pix2v: pix2[0] - pix2[15]  pix2iv: pix2[1] - pix2[16]
155      * Split the pixel vectors into shorts. */
156     vector unsigned char pix2l  = vec_ld(0,  pix2);
157     vector unsigned char pix2r  = vec_ld(16, pix2);
158     vector unsigned char pix2v  = vec_perm(pix2l, pix2r, perm1);
159     vector unsigned char pix2iv = vec_perm(pix2l, pix2r, perm2);
160
161     vector unsigned short pix2hv  =
162         (vector unsigned short) vec_mergeh(zero, pix2v);
163     vector unsigned short pix2lv  =
164         (vector unsigned short) vec_mergel(zero, pix2v);
165     vector unsigned short pix2ihv =
166         (vector unsigned short) vec_mergeh(zero, pix2iv);
167     vector unsigned short pix2ilv =
168         (vector unsigned short) vec_mergel(zero, pix2iv);
169     vector unsigned short t1 = vec_add(pix2hv, pix2ihv);
170     vector unsigned short t2 = vec_add(pix2lv, pix2ilv);
171     vector unsigned short t3, t4;
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         pix1v  = vec_ld(0, pix1);
178
179         pix2l  = vec_ld(0, pix3);
180         pix2r  = vec_ld(16, pix3);
181         pix3v  = vec_perm(pix2l, pix2r, perm1);
182         pix3iv = vec_perm(pix2l, pix2r, perm2);
183
184         /* Note that AltiVec does have vec_avg, but this works on vector pairs
185          * and rounds up. We could do avg(avg(a, b), avg(c, d)), but the
186          * rounding would mean that, for example, avg(3, 0, 0, 1) = 2, when
187          * it should be 1. Instead, we have to split the pixel vectors into
188          * vectors of shorts and do the averaging by hand. */
189
190         /* Split the pixel vectors into shorts. */
191         pix3hv  = (vector unsigned short) vec_mergeh(zero, pix3v);
192         pix3lv  = (vector unsigned short) vec_mergel(zero, pix3v);
193         pix3ihv = (vector unsigned short) vec_mergeh(zero, pix3iv);
194         pix3ilv = (vector unsigned short) vec_mergel(zero, pix3iv);
195
196         /* Do the averaging on them. */
197         t3 = vec_add(pix3hv, pix3ihv);
198         t4 = vec_add(pix3lv, pix3ilv);
199
200         avghv = vec_sr(vec_add(vec_add(t1, t3), two), two);
201         avglv = vec_sr(vec_add(vec_add(t2, t4), two), two);
202
203         /* Pack the shorts back into a result. */
204         avgv = vec_pack(avghv, avglv);
205
206         /* Calculate a sum of abs differences vector. */
207         t5 = vec_sub(vec_max(pix1v, avgv), vec_min(pix1v, avgv));
208
209         /* Add each 4 pixel group together and put 4 results into sad. */
210         sad = vec_sum4s(t5, sad);
211
212         pix1 += line_size;
213         pix3 += line_size;
214         /* Transfer the calculated values for pix3 into pix2. */
215         t1 = t3;
216         t2 = t4;
217     }
218     /* Sum up the four partial sums, and put the result into s. */
219     sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
220     sumdiffs = vec_splat(sumdiffs, 3);
221     vec_ste(sumdiffs, 0, &s);
222
223     return s;
224 }
225
226 static int sad16_altivec(void *v, uint8_t *pix1, uint8_t *pix2,
227                          int line_size, int h)
228 {
229     int i, s;
230     const vector unsigned int zero =
231         (const vector unsigned int) vec_splat_u32(0);
232     vector unsigned char perm = vec_lvsl(0, pix2);
233     vector unsigned int sad = (vector unsigned int) vec_splat_u32(0);
234     vector signed int sumdiffs;
235
236     for (i = 0; i < h; i++) {
237         /* Read potentially unaligned pixels into t1 and t2. */
238         vector unsigned char pix2l = vec_ld(0,  pix2);
239         vector unsigned char pix2r = vec_ld(15, pix2);
240         vector unsigned char t1 = vec_ld(0, pix1);
241         vector unsigned char t2 = vec_perm(pix2l, pix2r, perm);
242
243         /* Calculate a sum of abs differences vector. */
244         vector unsigned char t3 = vec_max(t1, t2);
245         vector unsigned char t4 = vec_min(t1, t2);
246         vector unsigned char t5 = vec_sub(t3, t4);
247
248         /* Add each 4 pixel group together and put 4 results into sad. */
249         sad = vec_sum4s(t5, sad);
250
251         pix1 += line_size;
252         pix2 += line_size;
253     }
254
255     /* Sum up the four partial sums, and put the result into s. */
256     sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
257     sumdiffs = vec_splat(sumdiffs, 3);
258     vec_ste(sumdiffs, 0, &s);
259
260     return s;
261 }
262
263 static int sad8_altivec(void *v, uint8_t *pix1, uint8_t *pix2,
264                         int line_size, int h)
265 {
266     int i, s;
267     const vector unsigned int zero =
268         (const vector unsigned int) vec_splat_u32(0);
269     const vector unsigned char permclear =
270         (vector unsigned char)
271         { 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0 };
272     vector unsigned char perm1 = vec_lvsl(0, pix1);
273     vector unsigned char perm2 = vec_lvsl(0, pix2);
274     vector unsigned int sad = (vector unsigned int) vec_splat_u32(0);
275     vector signed int sumdiffs;
276
277     for (i = 0; i < h; i++) {
278         /* Read potentially unaligned pixels into t1 and t2.
279          * Since we're reading 16 pixels, and actually only want 8,
280          * mask out the last 8 pixels. The 0s don't change the sum. */
281         vector unsigned char pix1l = vec_ld(0, pix1);
282         vector unsigned char pix1r = vec_ld(7, pix1);
283         vector unsigned char pix2l = vec_ld(0, pix2);
284         vector unsigned char pix2r = vec_ld(7, pix2);
285         vector unsigned char t1 = vec_and(vec_perm(pix1l, pix1r, perm1),
286                                           permclear);
287         vector unsigned char t2 = vec_and(vec_perm(pix2l, pix2r, perm2),
288                                           permclear);
289
290         /* Calculate a sum of abs differences vector. */
291         vector unsigned char t3 = vec_max(t1, t2);
292         vector unsigned char t4 = vec_min(t1, t2);
293         vector unsigned char t5 = vec_sub(t3, t4);
294
295         /* Add each 4 pixel group together and put 4 results into sad. */
296         sad = vec_sum4s(t5, sad);
297
298         pix1 += line_size;
299         pix2 += line_size;
300     }
301
302     /* Sum up the four partial sums, and put the result into s. */
303     sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
304     sumdiffs = vec_splat(sumdiffs, 3);
305     vec_ste(sumdiffs, 0, &s);
306
307     return s;
308 }
309
310 static int pix_norm1_altivec(uint8_t *pix, int line_size)
311 {
312     int i, s = 0;
313     const vector unsigned int zero =
314         (const vector unsigned int) vec_splat_u32(0);
315     vector unsigned char perm = vec_lvsl(0, pix);
316     vector unsigned int sv = (vector unsigned int) vec_splat_u32(0);
317     vector signed int sum;
318
319     for (i = 0; i < 16; i++) {
320         /* Read the potentially unaligned pixels. */
321         vector unsigned char pixl = vec_ld(0,  pix);
322         vector unsigned char pixr = vec_ld(15, pix);
323         vector unsigned char pixv = vec_perm(pixl, pixr, perm);
324
325         /* Square the values, and add them to our sum. */
326         sv = vec_msum(pixv, pixv, sv);
327
328         pix += line_size;
329     }
330     /* Sum up the four partial sums, and put the result into s. */
331     sum = vec_sums((vector signed int) sv, (vector signed int) zero);
332     sum = vec_splat(sum, 3);
333     vec_ste(sum, 0, &s);
334
335     return s;
336 }
337
338 /* Sum of Squared Errors for an 8x8 block, AltiVec-enhanced.
339  * It's the sad8_altivec code above w/ squaring added. */
340 static int sse8_altivec(void *v, uint8_t *pix1, uint8_t *pix2,
341                         int line_size, int h)
342 {
343     int i, s;
344     const vector unsigned int zero =
345         (const vector unsigned int) vec_splat_u32(0);
346     const vector unsigned char permclear =
347         (vector unsigned char)
348         { 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0 };
349     vector unsigned char perm1 = vec_lvsl(0, pix1);
350     vector unsigned char perm2 = vec_lvsl(0, pix2);
351     vector unsigned int sum = (vector unsigned int) vec_splat_u32(0);
352     vector signed int sumsqr;
353
354     for (i = 0; i < h; i++) {
355         /* Read potentially unaligned pixels into t1 and t2.
356          * Since we're reading 16 pixels, and actually only want 8,
357          * mask out the last 8 pixels. The 0s don't change the sum. */
358         vector unsigned char pix1l = vec_ld(0, pix1);
359         vector unsigned char pix1r = vec_ld(7, pix1);
360         vector unsigned char pix2l = vec_ld(0, pix2);
361         vector unsigned char pix2r = vec_ld(7, pix2);
362         vector unsigned char t1 = vec_and(vec_perm(pix1l, pix1r, perm1),
363                                           permclear);
364         vector unsigned char t2 = vec_and(vec_perm(pix2l, pix2r, perm2),
365                                           permclear);
366
367         /* Since we want to use unsigned chars, we can take advantage
368          * of the fact that abs(a - b) ^ 2 = (a - b) ^ 2. */
369
370         /* Calculate abs differences vector. */
371         vector unsigned char t3 = vec_max(t1, t2);
372         vector unsigned char t4 = vec_min(t1, t2);
373         vector unsigned char t5 = vec_sub(t3, t4);
374
375         /* Square the values and add them to our sum. */
376         sum = vec_msum(t5, t5, sum);
377
378         pix1 += line_size;
379         pix2 += line_size;
380     }
381
382     /* Sum up the four partial sums, and put the result into s. */
383     sumsqr = vec_sums((vector signed int) sum, (vector signed int) zero);
384     sumsqr = vec_splat(sumsqr, 3);
385     vec_ste(sumsqr, 0, &s);
386
387     return s;
388 }
389
390 /* Sum of Squared Errors for a 16x16 block, AltiVec-enhanced.
391  * It's the sad16_altivec code above w/ squaring added. */
392 static int sse16_altivec(void *v, uint8_t *pix1, uint8_t *pix2,
393                          int line_size, int h)
394 {
395     int i, s;
396     const vector unsigned int zero =
397         (const vector unsigned int) vec_splat_u32(0);
398     vector unsigned char perm = vec_lvsl(0, pix2);
399     vector unsigned int sum = (vector unsigned int) vec_splat_u32(0);
400     vector signed int sumsqr;
401
402     for (i = 0; i < h; i++) {
403         /* Read potentially unaligned pixels into t1 and t2. */
404         vector unsigned char pix2l = vec_ld(0,  pix2);
405         vector unsigned char pix2r = vec_ld(15, pix2);
406         vector unsigned char t1 = vec_ld(0, pix1);
407         vector unsigned char t2 = vec_perm(pix2l, pix2r, perm);
408
409         /* Since we want to use unsigned chars, we can take advantage
410          * of the fact that abs(a - b) ^ 2 = (a - b) ^ 2. */
411
412         /* Calculate abs differences vector. */
413         vector unsigned char t3 = vec_max(t1, t2);
414         vector unsigned char t4 = vec_min(t1, t2);
415         vector unsigned char t5 = vec_sub(t3, t4);
416
417         /* Square the values and add them to our sum. */
418         sum = vec_msum(t5, t5, sum);
419
420         pix1 += line_size;
421         pix2 += line_size;
422     }
423
424     /* Sum up the four partial sums, and put the result into s. */
425     sumsqr = vec_sums((vector signed int) sum, (vector signed int) zero);
426     sumsqr = vec_splat(sumsqr, 3);
427     vec_ste(sumsqr, 0, &s);
428
429     return s;
430 }
431
432 static int pix_sum_altivec(uint8_t *pix, int line_size)
433 {
434     int i, s;
435     const vector unsigned int zero =
436         (const vector unsigned int) vec_splat_u32(0);
437     vector unsigned char perm = vec_lvsl(0, pix);
438     vector unsigned int sad = (vector unsigned int) vec_splat_u32(0);
439     vector signed int sumdiffs;
440
441     for (i = 0; i < 16; i++) {
442         /* Read the potentially unaligned 16 pixels into t1. */
443         vector unsigned char pixl = vec_ld(0,  pix);
444         vector unsigned char pixr = vec_ld(15, pix);
445         vector unsigned char t1   = vec_perm(pixl, pixr, perm);
446
447         /* Add each 4 pixel group together and put 4 results into sad. */
448         sad = vec_sum4s(t1, sad);
449
450         pix += line_size;
451     }
452
453     /* Sum up the four partial sums, and put the result into s. */
454     sumdiffs = vec_sums((vector signed int) sad, (vector signed int) zero);
455     sumdiffs = vec_splat(sumdiffs, 3);
456     vec_ste(sumdiffs, 0, &s);
457
458     return s;
459 }
460
461 static void get_pixels_altivec(int16_t *restrict block, const uint8_t *pixels,
462                                int line_size)
463 {
464     int i;
465     vector unsigned char perm = vec_lvsl(0, pixels);
466     const vector unsigned char zero =
467         (const vector unsigned char) vec_splat_u8(0);
468
469     for (i = 0; i < 8; i++) {
470         /* Read potentially unaligned pixels.
471          * We're reading 16 pixels, and actually only want 8,
472          * but we simply ignore the extras. */
473         vector unsigned char pixl = vec_ld(0, pixels);
474         vector unsigned char pixr = vec_ld(7, pixels);
475         vector unsigned char bytes = vec_perm(pixl, pixr, perm);
476
477         // Convert the bytes into shorts.
478         vector signed short shorts = (vector signed short) vec_mergeh(zero,
479                                                                       bytes);
480
481         // Save the data to the block, we assume the block is 16-byte aligned.
482         vec_st(shorts, i * 16, (vector signed short *) block);
483
484         pixels += line_size;
485     }
486 }
487
488 static void diff_pixels_altivec(int16_t *restrict block, const uint8_t *s1,
489                                 const uint8_t *s2, int stride)
490 {
491     int i;
492     vector unsigned char perm1 = vec_lvsl(0, s1);
493     vector unsigned char perm2 = vec_lvsl(0, s2);
494     const vector unsigned char zero =
495         (const vector unsigned char) vec_splat_u8(0);
496     vector signed short shorts1, shorts2;
497
498     for (i = 0; i < 4; i++) {
499         /* Read potentially unaligned pixels.
500          * We're reading 16 pixels, and actually only want 8,
501          * but we simply ignore the extras. */
502         vector unsigned char pixl  = vec_ld(0,  s1);
503         vector unsigned char pixr  = vec_ld(15, s1);
504         vector unsigned char bytes = vec_perm(pixl, pixr, perm1);
505
506         // Convert the bytes into shorts.
507         shorts1 = (vector signed short) vec_mergeh(zero, bytes);
508
509         // Do the same for the second block of pixels.
510         pixl  = vec_ld(0,  s2);
511         pixr  = vec_ld(15, s2);
512         bytes = vec_perm(pixl, pixr, perm2);
513
514         // Convert the bytes into shorts.
515         shorts2 = (vector signed short) vec_mergeh(zero, bytes);
516
517         // Do the subtraction.
518         shorts1 = vec_sub(shorts1, shorts2);
519
520         // Save the data to the block, we assume the block is 16-byte aligned.
521         vec_st(shorts1, 0, (vector signed short *) block);
522
523         s1    += stride;
524         s2    += stride;
525         block += 8;
526
527         /* The code below is a copy of the code above...
528          * This is a manual unroll. */
529
530         /* Read potentially unaligned pixels.
531          * We're reading 16 pixels, and actually only want 8,
532          * but we simply ignore the extras. */
533         pixl  = vec_ld(0,  s1);
534         pixr  = vec_ld(15, s1);
535         bytes = vec_perm(pixl, pixr, perm1);
536
537         // Convert the bytes into shorts.
538         shorts1 = (vector signed short) vec_mergeh(zero, bytes);
539
540         // Do the same for the second block of pixels.
541         pixl  = vec_ld(0,  s2);
542         pixr  = vec_ld(15, s2);
543         bytes = vec_perm(pixl, pixr, perm2);
544
545         // Convert the bytes into shorts.
546         shorts2 = (vector signed short) vec_mergeh(zero, bytes);
547
548         // Do the subtraction.
549         shorts1 = vec_sub(shorts1, shorts2);
550
551         // Save the data to the block, we assume the block is 16-byte aligned.
552         vec_st(shorts1, 0, (vector signed short *) block);
553
554         s1    += stride;
555         s2    += stride;
556         block += 8;
557     }
558 }
559
560 static void clear_block_altivec(int16_t *block)
561 {
562     LOAD_ZERO;
563     vec_st(zero_s16v,   0, block);
564     vec_st(zero_s16v,  16, block);
565     vec_st(zero_s16v,  32, block);
566     vec_st(zero_s16v,  48, block);
567     vec_st(zero_s16v,  64, block);
568     vec_st(zero_s16v,  80, block);
569     vec_st(zero_s16v,  96, block);
570     vec_st(zero_s16v, 112, block);
571 }
572
573 static void add_bytes_altivec(uint8_t *dst, uint8_t *src, int w)
574 {
575     register int i;
576     register vector unsigned char vdst, vsrc;
577
578     /* dst and src are 16 bytes-aligned (guaranteed). */
579     for (i = 0; i + 15 < w; i += 16) {
580         vdst = vec_ld(i, (unsigned char *) dst);
581         vsrc = vec_ld(i, (unsigned char *) src);
582         vdst = vec_add(vsrc, vdst);
583         vec_st(vdst, i, (unsigned char *) dst);
584     }
585     /* If w is not a multiple of 16. */
586     for (; i < w; i++)
587         dst[i] = src[i];
588 }
589
590 static int hadamard8_diff8x8_altivec(/* MpegEncContext */ void *s, uint8_t *dst,
591                                      uint8_t *src, int stride, int h)
592 {
593     int sum;
594     register const vector unsigned char vzero =
595         (const vector unsigned char) vec_splat_u8(0);
596     register vector signed short temp0, temp1, temp2, temp3, temp4,
597                                  temp5, temp6, temp7;
598     {
599         register const vector signed short vprod1 =
600             (const vector signed short) { 1, -1, 1, -1, 1, -1, 1, -1 };
601         register const vector signed short vprod2 =
602             (const vector signed short) { 1, 1, -1, -1, 1, 1, -1, -1 };
603         register const vector signed short vprod3 =
604             (const vector signed short) { 1, 1, 1, 1, -1, -1, -1, -1 };
605         register const vector unsigned char perm1 =
606             (const vector unsigned char)
607             { 0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
608               0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D };
609         register const vector unsigned char perm2 =
610             (const vector unsigned char)
611             { 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
612               0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B };
613         register const vector unsigned char perm3 =
614             (const vector unsigned char)
615             { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
616               0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
617
618 #define ONEITERBUTTERFLY(i, res)                                            \
619     {                                                                       \
620         register vector unsigned char src1 = vec_ld(stride * i, src);       \
621         register vector unsigned char src2 = vec_ld(stride * i + 15, src);  \
622         register vector unsigned char srcO =                                \
623             vec_perm(src1, src2, vec_lvsl(stride * i, src));                \
624         register vector unsigned char dst1 = vec_ld(stride * i, dst);       \
625         register vector unsigned char dst2 = vec_ld(stride * i + 15, dst);  \
626         register vector unsigned char dstO =                                \
627             vec_perm(dst1, dst2, vec_lvsl(stride * i, dst));                \
628                                                                             \
629         /* Promote the unsigned chars to signed shorts. */                  \
630         /* We're in the 8x8 function, we only care for the first 8. */      \
631         register vector signed short srcV =                                 \
632             (vector signed short) vec_mergeh((vector signed char) vzero,    \
633                                              (vector signed char) srcO);    \
634         register vector signed short dstV =                                 \
635             (vector signed short) vec_mergeh((vector signed char) vzero,    \
636                                              (vector signed char) dstO);    \
637                                                                             \
638         /* subtractions inside the first butterfly */                       \
639         register vector signed short but0 = vec_sub(srcV, dstV);            \
640         register vector signed short op1  = vec_perm(but0, but0, perm1);    \
641         register vector signed short but1 = vec_mladd(but0, vprod1, op1);   \
642         register vector signed short op2  = vec_perm(but1, but1, perm2);    \
643         register vector signed short but2 = vec_mladd(but1, vprod2, op2);   \
644         register vector signed short op3  = vec_perm(but2, but2, perm3);    \
645         res  = vec_mladd(but2, vprod3, op3);                                \
646     }
647         ONEITERBUTTERFLY(0, temp0);
648         ONEITERBUTTERFLY(1, temp1);
649         ONEITERBUTTERFLY(2, temp2);
650         ONEITERBUTTERFLY(3, temp3);
651         ONEITERBUTTERFLY(4, temp4);
652         ONEITERBUTTERFLY(5, temp5);
653         ONEITERBUTTERFLY(6, temp6);
654         ONEITERBUTTERFLY(7, temp7);
655     }
656 #undef ONEITERBUTTERFLY
657     {
658         register vector signed int vsum;
659         register vector signed short line0  = vec_add(temp0, temp1);
660         register vector signed short line1  = vec_sub(temp0, temp1);
661         register vector signed short line2  = vec_add(temp2, temp3);
662         register vector signed short line3  = vec_sub(temp2, temp3);
663         register vector signed short line4  = vec_add(temp4, temp5);
664         register vector signed short line5  = vec_sub(temp4, temp5);
665         register vector signed short line6  = vec_add(temp6, temp7);
666         register vector signed short line7  = vec_sub(temp6, temp7);
667
668         register vector signed short line0B = vec_add(line0, line2);
669         register vector signed short line2B = vec_sub(line0, line2);
670         register vector signed short line1B = vec_add(line1, line3);
671         register vector signed short line3B = vec_sub(line1, line3);
672         register vector signed short line4B = vec_add(line4, line6);
673         register vector signed short line6B = vec_sub(line4, line6);
674         register vector signed short line5B = vec_add(line5, line7);
675         register vector signed short line7B = vec_sub(line5, line7);
676
677         register vector signed short line0C = vec_add(line0B, line4B);
678         register vector signed short line4C = vec_sub(line0B, line4B);
679         register vector signed short line1C = vec_add(line1B, line5B);
680         register vector signed short line5C = vec_sub(line1B, line5B);
681         register vector signed short line2C = vec_add(line2B, line6B);
682         register vector signed short line6C = vec_sub(line2B, line6B);
683         register vector signed short line3C = vec_add(line3B, line7B);
684         register vector signed short line7C = vec_sub(line3B, line7B);
685
686         vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
687         vsum = vec_sum4s(vec_abs(line1C), vsum);
688         vsum = vec_sum4s(vec_abs(line2C), vsum);
689         vsum = vec_sum4s(vec_abs(line3C), vsum);
690         vsum = vec_sum4s(vec_abs(line4C), vsum);
691         vsum = vec_sum4s(vec_abs(line5C), vsum);
692         vsum = vec_sum4s(vec_abs(line6C), vsum);
693         vsum = vec_sum4s(vec_abs(line7C), vsum);
694         vsum = vec_sums(vsum, (vector signed int) vzero);
695         vsum = vec_splat(vsum, 3);
696         vec_ste(vsum, 0, &sum);
697     }
698     return sum;
699 }
700
701 /*
702  * 16x8 works with 16 elements; it allows to avoid replicating loads, and
703  * gives the compiler more room for scheduling. It's only used from
704  * inside hadamard8_diff16_altivec.
705  *
706  * Unfortunately, it seems gcc-3.3 is a bit dumb, and the compiled code has
707  * a LOT of spill code, it seems gcc (unlike xlc) cannot keep everything in
708  * registers by itself. The following code includes hand-made register
709  * allocation. It's not clean, but on a 7450 the resulting code is much faster
710  * (best case falls from 700+ cycles to 550).
711  *
712  * xlc doesn't add spill code, but it doesn't know how to schedule for the
713  * 7450, and its code isn't much faster than gcc-3.3 on the 7450 (but uses
714  * 25% fewer instructions...)
715  *
716  * On the 970, the hand-made RA is still a win (around 690 vs. around 780),
717  * but xlc goes to around 660 on the regular C code...
718  */
719 static int hadamard8_diff16x8_altivec(/* MpegEncContext */ void *s, uint8_t *dst,
720                                       uint8_t *src, int stride, int h)
721 {
722     int sum;
723     register vector signed short
724         temp0 __asm__ ("v0"),
725         temp1 __asm__ ("v1"),
726         temp2 __asm__ ("v2"),
727         temp3 __asm__ ("v3"),
728         temp4 __asm__ ("v4"),
729         temp5 __asm__ ("v5"),
730         temp6 __asm__ ("v6"),
731         temp7 __asm__ ("v7");
732     register vector signed short
733         temp0S __asm__ ("v8"),
734         temp1S __asm__ ("v9"),
735         temp2S __asm__ ("v10"),
736         temp3S __asm__ ("v11"),
737         temp4S __asm__ ("v12"),
738         temp5S __asm__ ("v13"),
739         temp6S __asm__ ("v14"),
740         temp7S __asm__ ("v15");
741     register const vector unsigned char vzero __asm__ ("v31") =
742         (const vector unsigned char) vec_splat_u8(0);
743     {
744         register const vector signed short vprod1 __asm__ ("v16") =
745             (const vector signed short) { 1, -1, 1, -1, 1, -1, 1, -1 };
746
747         register const vector signed short vprod2 __asm__ ("v17") =
748             (const vector signed short) { 1, 1, -1, -1, 1, 1, -1, -1 };
749
750         register const vector signed short vprod3 __asm__ ("v18") =
751             (const vector signed short) { 1, 1, 1, 1, -1, -1, -1, -1 };
752
753         register const vector unsigned char perm1 __asm__ ("v19") =
754             (const vector unsigned char)
755             { 0x02, 0x03, 0x00, 0x01, 0x06, 0x07, 0x04, 0x05,
756               0x0A, 0x0B, 0x08, 0x09, 0x0E, 0x0F, 0x0C, 0x0D };
757
758         register const vector unsigned char perm2 __asm__ ("v20") =
759             (const vector unsigned char)
760             { 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03,
761               0x0C, 0x0D, 0x0E, 0x0F, 0x08, 0x09, 0x0A, 0x0B };
762
763         register const vector unsigned char perm3 __asm__ ("v21") =
764             (const vector unsigned char)
765             { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
766               0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
767
768 #define ONEITERBUTTERFLY(i, res1, res2)                                     \
769     {                                                                       \
770         register vector unsigned char src1 __asm__ ("v22") =                \
771             vec_ld(stride * i, src);                                        \
772         register vector unsigned char src2 __asm__ ("v23") =                \
773             vec_ld(stride * i + 16, src);                                   \
774         register vector unsigned char srcO __asm__ ("v22") =                \
775             vec_perm(src1, src2, vec_lvsl(stride * i, src));                \
776         register vector unsigned char dst1 __asm__ ("v24") =                \
777             vec_ld(stride * i, dst);                                        \
778         register vector unsigned char dst2 __asm__ ("v25") =                \
779             vec_ld(stride * i + 16, dst);                                   \
780         register vector unsigned char dstO __asm__ ("v23") =                \
781             vec_perm(dst1, dst2, vec_lvsl(stride * i, dst));                \
782                                                                             \
783         /* Promote the unsigned chars to signed shorts. */                  \
784         register vector signed short srcV __asm__ ("v24") =                 \
785             (vector signed short) vec_mergeh((vector signed char) vzero,    \
786                                              (vector signed char) srcO);    \
787         register vector signed short dstV __asm__ ("v25") =                 \
788             (vector signed short) vec_mergeh((vector signed char) vzero,    \
789                                              (vector signed char) dstO);    \
790         register vector signed short srcW __asm__ ("v26") =                 \
791             (vector signed short) vec_mergel((vector signed char) vzero,    \
792                                              (vector signed char) srcO);    \
793         register vector signed short dstW __asm__ ("v27") =                 \
794             (vector signed short) vec_mergel((vector signed char) vzero,    \
795                                              (vector signed char) dstO);    \
796                                                                             \
797         /* subtractions inside the first butterfly */                       \
798         register vector signed short but0  __asm__ ("v28") =                \
799             vec_sub(srcV, dstV);                                            \
800         register vector signed short but0S __asm__ ("v29") =                \
801             vec_sub(srcW, dstW);                                            \
802         register vector signed short op1   __asm__ ("v30") =                \
803             vec_perm(but0, but0, perm1);                                    \
804         register vector signed short but1  __asm__ ("v22") =                \
805             vec_mladd(but0, vprod1, op1);                                   \
806         register vector signed short op1S  __asm__ ("v23") =                \
807             vec_perm(but0S, but0S, perm1);                                  \
808         register vector signed short but1S __asm__ ("v24") =                \
809             vec_mladd(but0S, vprod1, op1S);                                 \
810         register vector signed short op2   __asm__ ("v25") =                \
811             vec_perm(but1, but1, perm2);                                    \
812         register vector signed short but2  __asm__ ("v26") =                \
813             vec_mladd(but1, vprod2, op2);                                   \
814         register vector signed short op2S  __asm__ ("v27") =                \
815             vec_perm(but1S, but1S, perm2);                                  \
816         register vector signed short but2S __asm__ ("v28") =                \
817             vec_mladd(but1S, vprod2, op2S);                                 \
818         register vector signed short op3   __asm__ ("v29") =                \
819             vec_perm(but2, but2, perm3);                                    \
820         register vector signed short op3S  __asm__ ("v30") =                \
821             vec_perm(but2S, but2S, perm3);                                  \
822         res1 = vec_mladd(but2, vprod3, op3);                                \
823         res2 = vec_mladd(but2S, vprod3, op3S);                              \
824     }
825         ONEITERBUTTERFLY(0, temp0, temp0S);
826         ONEITERBUTTERFLY(1, temp1, temp1S);
827         ONEITERBUTTERFLY(2, temp2, temp2S);
828         ONEITERBUTTERFLY(3, temp3, temp3S);
829         ONEITERBUTTERFLY(4, temp4, temp4S);
830         ONEITERBUTTERFLY(5, temp5, temp5S);
831         ONEITERBUTTERFLY(6, temp6, temp6S);
832         ONEITERBUTTERFLY(7, temp7, temp7S);
833     }
834 #undef ONEITERBUTTERFLY
835     {
836         register vector signed int vsum;
837
838         register vector signed short line0  = vec_add(temp0, temp1);
839         register vector signed short line1  = vec_sub(temp0, temp1);
840         register vector signed short line2  = vec_add(temp2, temp3);
841         register vector signed short line3  = vec_sub(temp2, temp3);
842         register vector signed short line4  = vec_add(temp4, temp5);
843         register vector signed short line5  = vec_sub(temp4, temp5);
844         register vector signed short line6  = vec_add(temp6, temp7);
845         register vector signed short line7  = vec_sub(temp6, temp7);
846
847         register vector signed short line0B = vec_add(line0, line2);
848         register vector signed short line2B = vec_sub(line0, line2);
849         register vector signed short line1B = vec_add(line1, line3);
850         register vector signed short line3B = vec_sub(line1, line3);
851         register vector signed short line4B = vec_add(line4, line6);
852         register vector signed short line6B = vec_sub(line4, line6);
853         register vector signed short line5B = vec_add(line5, line7);
854         register vector signed short line7B = vec_sub(line5, line7);
855
856         register vector signed short line0C = vec_add(line0B, line4B);
857         register vector signed short line4C = vec_sub(line0B, line4B);
858         register vector signed short line1C = vec_add(line1B, line5B);
859         register vector signed short line5C = vec_sub(line1B, line5B);
860         register vector signed short line2C = vec_add(line2B, line6B);
861         register vector signed short line6C = vec_sub(line2B, line6B);
862         register vector signed short line3C = vec_add(line3B, line7B);
863         register vector signed short line7C = vec_sub(line3B, line7B);
864
865         register vector signed short line0S = vec_add(temp0S, temp1S);
866         register vector signed short line1S = vec_sub(temp0S, temp1S);
867         register vector signed short line2S = vec_add(temp2S, temp3S);
868         register vector signed short line3S = vec_sub(temp2S, temp3S);
869         register vector signed short line4S = vec_add(temp4S, temp5S);
870         register vector signed short line5S = vec_sub(temp4S, temp5S);
871         register vector signed short line6S = vec_add(temp6S, temp7S);
872         register vector signed short line7S = vec_sub(temp6S, temp7S);
873
874         register vector signed short line0BS = vec_add(line0S, line2S);
875         register vector signed short line2BS = vec_sub(line0S, line2S);
876         register vector signed short line1BS = vec_add(line1S, line3S);
877         register vector signed short line3BS = vec_sub(line1S, line3S);
878         register vector signed short line4BS = vec_add(line4S, line6S);
879         register vector signed short line6BS = vec_sub(line4S, line6S);
880         register vector signed short line5BS = vec_add(line5S, line7S);
881         register vector signed short line7BS = vec_sub(line5S, line7S);
882
883         register vector signed short line0CS = vec_add(line0BS, line4BS);
884         register vector signed short line4CS = vec_sub(line0BS, line4BS);
885         register vector signed short line1CS = vec_add(line1BS, line5BS);
886         register vector signed short line5CS = vec_sub(line1BS, line5BS);
887         register vector signed short line2CS = vec_add(line2BS, line6BS);
888         register vector signed short line6CS = vec_sub(line2BS, line6BS);
889         register vector signed short line3CS = vec_add(line3BS, line7BS);
890         register vector signed short line7CS = vec_sub(line3BS, line7BS);
891
892         vsum = vec_sum4s(vec_abs(line0C), vec_splat_s32(0));
893         vsum = vec_sum4s(vec_abs(line1C), vsum);
894         vsum = vec_sum4s(vec_abs(line2C), vsum);
895         vsum = vec_sum4s(vec_abs(line3C), vsum);
896         vsum = vec_sum4s(vec_abs(line4C), vsum);
897         vsum = vec_sum4s(vec_abs(line5C), vsum);
898         vsum = vec_sum4s(vec_abs(line6C), vsum);
899         vsum = vec_sum4s(vec_abs(line7C), vsum);
900
901         vsum = vec_sum4s(vec_abs(line0CS), vsum);
902         vsum = vec_sum4s(vec_abs(line1CS), vsum);
903         vsum = vec_sum4s(vec_abs(line2CS), vsum);
904         vsum = vec_sum4s(vec_abs(line3CS), vsum);
905         vsum = vec_sum4s(vec_abs(line4CS), vsum);
906         vsum = vec_sum4s(vec_abs(line5CS), vsum);
907         vsum = vec_sum4s(vec_abs(line6CS), vsum);
908         vsum = vec_sum4s(vec_abs(line7CS), vsum);
909         vsum = vec_sums(vsum, (vector signed int) vzero);
910         vsum = vec_splat(vsum, 3);
911         vec_ste(vsum, 0, &sum);
912     }
913     return sum;
914 }
915
916 static int hadamard8_diff16_altivec(/* MpegEncContext */ void *s, uint8_t *dst,
917                                     uint8_t *src, int stride, int h)
918 {
919     int score = hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
920
921     if (h == 16) {
922         dst   += 8 * stride;
923         src   += 8 * stride;
924         score += hadamard8_diff16x8_altivec(s, dst, src, stride, 8);
925     }
926     return score;
927 }
928
929 av_cold void ff_dsputil_init_altivec(DSPContext *c, AVCodecContext *avctx,
930                                      unsigned high_bit_depth)
931 {
932     c->pix_abs[0][1] = sad16_x2_altivec;
933     c->pix_abs[0][2] = sad16_y2_altivec;
934     c->pix_abs[0][3] = sad16_xy2_altivec;
935     c->pix_abs[0][0] = sad16_altivec;
936     c->pix_abs[1][0] = sad8_altivec;
937
938     c->sad[0] = sad16_altivec;
939     c->sad[1] = sad8_altivec;
940     c->sse[0] = sse16_altivec;
941     c->sse[1] = sse8_altivec;
942
943     c->pix_norm1 = pix_norm1_altivec;
944     c->pix_sum   = pix_sum_altivec;
945
946     c->diff_pixels = diff_pixels_altivec;
947     c->add_bytes   = add_bytes_altivec;
948
949     if (!high_bit_depth) {
950         c->get_pixels = get_pixels_altivec;
951         c->clear_block = clear_block_altivec;
952     }
953
954     c->hadamard8_diff[0] = hadamard8_diff16_altivec;
955     c->hadamard8_diff[1] = hadamard8_diff8x8_altivec;
956 }