You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1411 lines
55KB

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