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.

1457 lines
56KB

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