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