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.

714 lines
29KB

  1. /*
  2. AltiVec optimizations (C) 2004 Romain Dolbeau <romain@dolbeau.org>
  3. based on code by Copyright (C) 2001-2003 Michael Niedermayer (michaelni@gmx.at)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #ifdef CONFIG_DARWIN
  17. #define AVV(x...) (x)
  18. #else
  19. #define AVV(x...) {x}
  20. #endif
  21. static inline int vertClassify_altivec(uint8_t src[], int stride, PPContext *c) {
  22. /*
  23. this code makes no assumption on src or stride.
  24. One could remove the recomputation of the perm
  25. vector by assuming (stride % 16) == 0, unfortunately
  26. this is not always true.
  27. */
  28. register int y;
  29. short __attribute__ ((aligned(16))) data[8];
  30. int numEq;
  31. uint8_t *src2 = src;
  32. vector signed short v_dcOffset;
  33. vector signed short v2QP;
  34. vector unsigned short v4QP;
  35. vector unsigned short v_dcThreshold;
  36. int two_vectors = ((((unsigned long)src2 % 16) > 8) || (stride % 16)) ? 1 : 0;
  37. const vector signed int zero = vec_splat_s32(0);
  38. const vector signed short mask = vec_splat_s16(1);
  39. vector signed int v_numEq = vec_splat_s32(0);
  40. data[0] = ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1;
  41. data[1] = data[0] * 2 + 1;
  42. data[2] = c->QP * 2;
  43. data[3] = c->QP * 4;
  44. vector signed short v_data = vec_ld(0, data);
  45. v_dcOffset = vec_splat(v_data, 0);
  46. v_dcThreshold = (vector unsigned short)vec_splat(v_data, 1);
  47. v2QP = vec_splat(v_data, 2);
  48. v4QP = (vector unsigned short)vec_splat(v_data, 3);
  49. src2 += stride * 4;
  50. #define LOAD_LINE(i) \
  51. register int j##i = i * stride; \
  52. vector unsigned char perm##i = vec_lvsl(j##i, src2); \
  53. const vector unsigned char v_srcA1##i = vec_ld(j##i, src2); \
  54. vector unsigned char v_srcA2##i; \
  55. if (two_vectors) \
  56. v_srcA2##i = vec_ld(j##i + 16, src2); \
  57. const vector unsigned char v_srcA##i = \
  58. vec_perm(v_srcA1##i, v_srcA2##i, perm##i); \
  59. vector signed short v_srcAss##i = \
  60. (vector signed short)vec_mergeh((vector signed char)zero, \
  61. (vector signed char)v_srcA##i)
  62. LOAD_LINE(0);
  63. LOAD_LINE(1);
  64. LOAD_LINE(2);
  65. LOAD_LINE(3);
  66. LOAD_LINE(4);
  67. LOAD_LINE(5);
  68. LOAD_LINE(6);
  69. LOAD_LINE(7);
  70. #undef LOAD_LINE
  71. #define ITER(i, j) \
  72. const vector signed short v_diff##i = \
  73. vec_sub(v_srcAss##i, v_srcAss##j); \
  74. const vector signed short v_sum##i = \
  75. vec_add(v_diff##i, v_dcOffset); \
  76. const vector signed short v_comp##i = \
  77. (vector signed short)vec_cmplt((vector unsigned short)v_sum##i, \
  78. v_dcThreshold); \
  79. const vector signed short v_part##i = vec_and(mask, v_comp##i); \
  80. v_numEq = vec_sum4s(v_part##i, v_numEq);
  81. ITER(0, 1);
  82. ITER(1, 2);
  83. ITER(2, 3);
  84. ITER(3, 4);
  85. ITER(4, 5);
  86. ITER(5, 6);
  87. ITER(6, 7);
  88. #undef ITER
  89. v_numEq = vec_sums(v_numEq, zero);
  90. v_numEq = vec_splat(v_numEq, 3);
  91. vec_ste(v_numEq, 0, &numEq);
  92. if (numEq > c->ppMode.flatnessThreshold)
  93. {
  94. const vector unsigned char mmoP1 = (const vector unsigned char)
  95. AVV(0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
  96. 0x00, 0x01, 0x12, 0x13, 0x08, 0x09, 0x1A, 0x1B);
  97. const vector unsigned char mmoP2 = (const vector unsigned char)
  98. AVV(0x04, 0x05, 0x16, 0x17, 0x0C, 0x0D, 0x1E, 0x1F,
  99. 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f);
  100. const vector unsigned char mmoP = (const vector unsigned char)
  101. vec_lvsl(8, (unsigned char*)0);
  102. vector signed short mmoL1 = vec_perm(v_srcAss0, v_srcAss2, mmoP1);
  103. vector signed short mmoL2 = vec_perm(v_srcAss4, v_srcAss6, mmoP2);
  104. vector signed short mmoL = vec_perm(mmoL1, mmoL2, mmoP);
  105. vector signed short mmoR1 = vec_perm(v_srcAss5, v_srcAss7, mmoP1);
  106. vector signed short mmoR2 = vec_perm(v_srcAss1, v_srcAss3, mmoP2);
  107. vector signed short mmoR = vec_perm(mmoR1, mmoR2, mmoP);
  108. vector signed short mmoDiff = vec_sub(mmoL, mmoR);
  109. vector unsigned short mmoSum = (vector unsigned short)vec_add(mmoDiff, v2QP);
  110. if (vec_any_gt(mmoSum, v4QP))
  111. return 0;
  112. else
  113. return 1;
  114. }
  115. else return 2;
  116. }
  117. static inline void doVertLowPass_altivec(uint8_t *src, int stride, PPContext *c) {
  118. /*
  119. this code makes no assumption on src or stride.
  120. One could remove the recomputation of the perm
  121. vector by assuming (stride % 16) == 0, unfortunately
  122. this is not always true. Quite a lot of load/stores
  123. can be removed by assuming proper alignement of
  124. src & stride :-(
  125. */
  126. uint8_t *src2 = src;
  127. const vector signed int zero = vec_splat_s32(0);
  128. short __attribute__ ((aligned(16))) qp[8];
  129. qp[0] = c->QP;
  130. vector signed short vqp = vec_ld(0, qp);
  131. vqp = vec_splat(vqp, 0);
  132. #define LOAD_LINE(i) \
  133. const vector unsigned char perml##i = \
  134. vec_lvsl(i * stride, src2); \
  135. const vector unsigned char vbA##i = \
  136. vec_ld(i * stride, src2); \
  137. const vector unsigned char vbB##i = \
  138. vec_ld(i * stride + 16, src2); \
  139. const vector unsigned char vbT##i = \
  140. vec_perm(vbA##i, vbB##i, perml##i); \
  141. const vector signed short vb##i = \
  142. (vector signed short)vec_mergeh((vector unsigned char)zero, \
  143. (vector unsigned char)vbT##i)
  144. src2 += stride*3;
  145. LOAD_LINE(0);
  146. LOAD_LINE(1);
  147. LOAD_LINE(2);
  148. LOAD_LINE(3);
  149. LOAD_LINE(4);
  150. LOAD_LINE(5);
  151. LOAD_LINE(6);
  152. LOAD_LINE(7);
  153. LOAD_LINE(8);
  154. LOAD_LINE(9);
  155. #undef LOAD_LINE
  156. const vector unsigned short v_1 = vec_splat_u16(1);
  157. const vector unsigned short v_2 = vec_splat_u16(2);
  158. const vector unsigned short v_4 = vec_splat_u16(4);
  159. const vector signed short v_8 = vec_splat_s16(8);
  160. const vector signed short v_first = vec_sel(vb1, vb0,
  161. vec_cmplt(vec_abs(vec_sub(vb0, vb1)),
  162. vqp));
  163. const vector signed short v_last = vec_sel(vb8, vb9,
  164. vec_cmplt(vec_abs(vec_sub(vb8, vb9)),
  165. vqp));
  166. const vector signed short v_sums0 = vec_add(v_first, vb1);
  167. const vector signed short v_sums1 = vec_add(vb1, vb2);
  168. const vector signed short v_sums2 = vec_add(vb2, vb3);
  169. const vector signed short v_sums3 = vec_add(vb3, vb4);
  170. const vector signed short v_sums4 = vec_add(vb4, vb5);
  171. const vector signed short v_sums5 = vec_add(vb5, vb6);
  172. const vector signed short v_sums6 = vec_add(vb6, vb7);
  173. const vector signed short v_sums7 = vec_add(vb7, vb8);
  174. const vector signed short v_sums8 = vec_add(vb8, v_last);
  175. const vector signed short vr1 = vec_sra(vec_add(vec_add(vec_sl(v_sums0, v_2),
  176. vec_sl(vec_add(v_first, v_sums2), v_1)),
  177. vec_add(v_sums4, v_8)),
  178. v_4);
  179. const vector signed short vr2 = vec_sra(vec_add(vec_add(vec_sl(vb2, v_2),
  180. v_sums5),
  181. vec_add(v_8,
  182. vec_sl(vec_add(v_first,
  183. vec_add(v_sums0, v_sums3)),
  184. v_1))),
  185. v_4);
  186. const vector signed short vr3 = vec_sra(vec_add(vec_add(vec_sl(vb3, v_2),
  187. v_sums6),
  188. vec_add(v_8,
  189. vec_sl(vec_add(v_first,
  190. vec_add(v_sums1, v_sums4)),
  191. v_1))),
  192. v_4);
  193. const vector signed short vr4 = vec_sra(vec_add(vec_add(vec_sl(vb4, v_2),
  194. v_sums7),
  195. vec_add(v_8,
  196. vec_add(v_sums0,
  197. vec_sl(vec_add(v_sums2, v_sums5),
  198. v_1)))),
  199. v_4);
  200. const vector signed short vr5 = vec_sra(vec_add(vec_add(vec_sl(vb5, v_2),
  201. v_sums8),
  202. vec_add(v_8,
  203. vec_add(v_sums1,
  204. vec_sl(vec_add(v_sums3, v_sums6),
  205. v_1)))),
  206. v_4);
  207. const vector signed short vr6 = vec_sra(vec_add(vec_add(vec_sl(vb6, v_2),
  208. v_sums2),
  209. vec_add(v_8,
  210. vec_sl(vec_add(v_last,
  211. vec_add(v_sums7, v_sums4)),
  212. v_1))),
  213. v_4);
  214. const vector signed short vr7 = vec_sra(vec_add(vec_add(vec_sl(vec_add(v_last, vb7), v_2),
  215. vec_sl(vec_add(vb8, v_sums5), v_1)),
  216. vec_add(v_8, v_sums3)),
  217. v_4);
  218. const vector signed short vr8 = vec_sra(vec_add(vec_add(vec_sl(v_sums8, v_2),
  219. vec_sl(vec_add(v_last, v_sums6), v_1)),
  220. vec_add(v_sums4, v_8)),
  221. v_4);
  222. const vector unsigned char neg1 = (vector unsigned char)AVV(-1, -1, -1, -1, -1, -1, -1, -1,
  223. -1, -1, -1, -1, -1, -1, -1, -1);
  224. const vector unsigned char permHH = (vector unsigned char)AVV(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  225. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F);
  226. #define PACK_AND_STORE(i) \
  227. const vector unsigned char perms##i = \
  228. vec_lvsr(i * stride, src2); \
  229. const vector unsigned char vf##i = \
  230. vec_packsu(vr##i, (vector signed short)zero); \
  231. const vector unsigned char vg##i = \
  232. vec_perm(vf##i, vbT##i, permHH); \
  233. const vector unsigned char mask##i = \
  234. vec_perm((vector unsigned char)zero, neg1, perms##i); \
  235. const vector unsigned char vg2##i = \
  236. vec_perm(vg##i, vg##i, perms##i); \
  237. const vector unsigned char svA##i = \
  238. vec_sel(vbA##i, vg2##i, mask##i); \
  239. const vector unsigned char svB##i = \
  240. vec_sel(vg2##i, vbB##i, mask##i); \
  241. vec_st(svA##i, i * stride, src2); \
  242. vec_st(svB##i, i * stride + 16, src2)
  243. PACK_AND_STORE(1);
  244. PACK_AND_STORE(2);
  245. PACK_AND_STORE(3);
  246. PACK_AND_STORE(4);
  247. PACK_AND_STORE(5);
  248. PACK_AND_STORE(6);
  249. PACK_AND_STORE(7);
  250. PACK_AND_STORE(8);
  251. #undef PACK_AND_STORE
  252. }
  253. static inline void doVertDefFilter_altivec(uint8_t src[], int stride, PPContext *c) {
  254. /*
  255. this code makes no assumption on src or stride.
  256. One could remove the recomputation of the perm
  257. vector by assuming (stride % 16) == 0, unfortunately
  258. this is not always true. Quite a lot of load/stores
  259. can be removed by assuming proper alignement of
  260. src & stride :-(
  261. */
  262. uint8_t *src2 = src;
  263. const vector signed int zero = vec_splat_s32(0);
  264. short __attribute__ ((aligned(16))) qp[8];
  265. qp[0] = 8*c->QP;
  266. vector signed short vqp = vec_ld(0, qp);
  267. vqp = vec_splat(vqp, 0);
  268. #define LOAD_LINE(i) \
  269. const vector unsigned char perm##i = \
  270. vec_lvsl(i * stride, src2); \
  271. const vector unsigned char vbA##i = \
  272. vec_ld(i * stride, src2); \
  273. const vector unsigned char vbB##i = \
  274. vec_ld(i * stride + 16, src2); \
  275. const vector unsigned char vbT##i = \
  276. vec_perm(vbA##i, vbB##i, perm##i); \
  277. const vector signed short vb##i = \
  278. (vector signed short)vec_mergeh((vector unsigned char)zero, \
  279. (vector unsigned char)vbT##i)
  280. src2 += stride*3;
  281. LOAD_LINE(1);
  282. LOAD_LINE(2);
  283. LOAD_LINE(3);
  284. LOAD_LINE(4);
  285. LOAD_LINE(5);
  286. LOAD_LINE(6);
  287. LOAD_LINE(7);
  288. LOAD_LINE(8);
  289. #undef LOAD_LINE
  290. const vector signed short v_1 = vec_splat_s16(1);
  291. const vector signed short v_2 = vec_splat_s16(2);
  292. const vector signed short v_5 = vec_splat_s16(5);
  293. const vector signed short v_32 = vec_sl(v_1,
  294. (vector unsigned short)v_5);
  295. /* middle energy */
  296. const vector signed short l3minusl6 = vec_sub(vb3, vb6);
  297. const vector signed short l5minusl4 = vec_sub(vb5, vb4);
  298. const vector signed short twotimes_l3minusl6 = vec_mladd(v_2, l3minusl6, (vector signed short)zero);
  299. const vector signed short mE = vec_mladd(v_5, l5minusl4, twotimes_l3minusl6);
  300. const vector signed short absmE = vec_abs(mE);
  301. /* left & right energy */
  302. const vector signed short l1minusl4 = vec_sub(vb1, vb4);
  303. const vector signed short l3minusl2 = vec_sub(vb3, vb2);
  304. const vector signed short l5minusl8 = vec_sub(vb5, vb8);
  305. const vector signed short l7minusl6 = vec_sub(vb7, vb6);
  306. const vector signed short twotimes_l1minusl4 = vec_mladd(v_2, l1minusl4, (vector signed short)zero);
  307. const vector signed short twotimes_l5minusl8 = vec_mladd(v_2, l5minusl8, (vector signed short)zero);
  308. const vector signed short lE = vec_mladd(v_5, l3minusl2, twotimes_l1minusl4);
  309. const vector signed short rE = vec_mladd(v_5, l7minusl6, twotimes_l5minusl8);
  310. /* d */
  311. const vector signed short ddiff = vec_sub(absmE,
  312. vec_min(vec_abs(lE),
  313. vec_abs(rE)));
  314. const vector signed short ddiffclamp = vec_max(ddiff, (vector signed short)zero);
  315. const vector signed short dtimes64 = vec_mladd(v_5, ddiffclamp, v_32);
  316. const vector signed short d = vec_sra(dtimes64, vec_splat_u16(6));
  317. const vector signed short minusd = vec_sub((vector signed short)zero, d);
  318. const vector signed short finald = vec_sel(minusd,
  319. d,
  320. vec_cmpgt(vec_sub((vector signed short)zero, mE),
  321. (vector signed short)zero));
  322. /* q */
  323. const vector signed short qtimes2 = vec_sub(vb4, vb5);
  324. /* for a shift right to behave like /2, we need to add one
  325. to all negative integer */
  326. const vector signed short rounddown = vec_sel((vector signed short)zero,
  327. v_1,
  328. vec_cmplt(qtimes2, (vector signed short)zero));
  329. const vector signed short q = vec_sra(vec_add(qtimes2, rounddown), vec_splat_u16(1));
  330. /* clamp */
  331. const vector signed short dclamp_P1 = vec_max((vector signed short)zero, finald);
  332. const vector signed short dclamp_P = vec_min(dclamp_P1, q);
  333. const vector signed short dclamp_N1 = vec_min((vector signed short)zero, finald);
  334. const vector signed short dclamp_N = vec_max(dclamp_N1, q);
  335. const vector signed short dclampedfinal = vec_sel(dclamp_N,
  336. dclamp_P,
  337. vec_cmpgt(q, (vector signed short)zero));
  338. const vector signed short dornotd = vec_sel((vector signed short)zero,
  339. dclampedfinal,
  340. vec_cmplt(absmE, vqp));
  341. /* add/substract to l4 and l5 */
  342. const vector signed short vb4minusd = vec_sub(vb4, dornotd);
  343. const vector signed short vb5plusd = vec_add(vb5, dornotd);
  344. /* finally, stores */
  345. const vector unsigned char st4 = vec_packsu(vb4minusd, (vector signed short)zero);
  346. const vector unsigned char st5 = vec_packsu(vb5plusd, (vector signed short)zero);
  347. const vector unsigned char neg1 = (vector unsigned char)AVV(-1, -1, -1, -1, -1, -1, -1, -1,
  348. -1, -1, -1, -1, -1, -1, -1, -1);
  349. const vector unsigned char permHH = (vector unsigned char)AVV(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  350. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F);
  351. #define STORE(i) \
  352. const vector unsigned char perms##i = \
  353. vec_lvsr(i * stride, src2); \
  354. const vector unsigned char vg##i = \
  355. vec_perm(st##i, vbT##i, permHH); \
  356. const vector unsigned char mask##i = \
  357. vec_perm((vector unsigned char)zero, neg1, perms##i); \
  358. const vector unsigned char vg2##i = \
  359. vec_perm(vg##i, vg##i, perms##i); \
  360. const vector unsigned char svA##i = \
  361. vec_sel(vbA##i, vg2##i, mask##i); \
  362. const vector unsigned char svB##i = \
  363. vec_sel(vg2##i, vbB##i, mask##i); \
  364. vec_st(svA##i, i * stride, src2); \
  365. vec_st(svB##i, i * stride + 16, src2)
  366. STORE(4);
  367. STORE(5);
  368. }
  369. static inline void dering_altivec(uint8_t src[], int stride, PPContext *c) {
  370. /*
  371. this code makes no assumption on src or stride.
  372. One could remove the recomputation of the perm
  373. vector by assuming (stride % 16) == 0, unfortunately
  374. this is not always true. Quite a lot of load/stores
  375. can be removed by assuming proper alignement of
  376. src & stride :-(
  377. */
  378. uint8_t *srcCopy = src;
  379. uint8_t __attribute__((aligned(16))) dt[16];
  380. const vector unsigned char vuint8_1 = vec_splat_u8(1);
  381. const vector signed int zero = vec_splat_s32(0);
  382. vector unsigned char v_dt;
  383. dt[0] = deringThreshold;
  384. v_dt = vec_splat(vec_ld(0, dt), 0);
  385. #define LOAD_LINE(i) \
  386. const vector unsigned char perm##i = \
  387. vec_lvsl(i * stride, srcCopy); \
  388. vector unsigned char sA##i = vec_ld(i * stride, srcCopy); \
  389. vector unsigned char sB##i = vec_ld(i * stride + 16, srcCopy); \
  390. vector unsigned char src##i = vec_perm(sA##i, sB##i, perm##i)
  391. LOAD_LINE(0);
  392. LOAD_LINE(1);
  393. LOAD_LINE(2);
  394. LOAD_LINE(3);
  395. LOAD_LINE(4);
  396. LOAD_LINE(5);
  397. LOAD_LINE(6);
  398. LOAD_LINE(7);
  399. LOAD_LINE(8);
  400. LOAD_LINE(9);
  401. #undef LOAD_LINE
  402. vector unsigned char v_avg;
  403. {
  404. const vector unsigned char trunc_perm = (vector unsigned char)
  405. AVV(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  406. 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18);
  407. const vector unsigned char trunc_src12 = vec_perm(src1, src2, trunc_perm);
  408. const vector unsigned char trunc_src34 = vec_perm(src3, src4, trunc_perm);
  409. const vector unsigned char trunc_src56 = vec_perm(src5, src6, trunc_perm);
  410. const vector unsigned char trunc_src78 = vec_perm(src7, src8, trunc_perm);
  411. #define EXTRACT(op) do { \
  412. const vector unsigned char s##op##_1 = vec_##op(trunc_src12, trunc_src34); \
  413. const vector unsigned char s##op##_2 = vec_##op(trunc_src56, trunc_src78); \
  414. const vector unsigned char s##op##_6 = vec_##op(s##op##_1, s##op##_2); \
  415. const vector unsigned char s##op##_8h = vec_mergeh(s##op##_6, s##op##_6); \
  416. const vector unsigned char s##op##_8l = vec_mergel(s##op##_6, s##op##_6); \
  417. const vector unsigned char s##op##_9 = vec_##op(s##op##_8h, s##op##_8l); \
  418. const vector unsigned char s##op##_9h = vec_mergeh(s##op##_9, s##op##_9); \
  419. const vector unsigned char s##op##_9l = vec_mergel(s##op##_9, s##op##_9); \
  420. const vector unsigned char s##op##_10 = vec_##op(s##op##_9h, s##op##_9l); \
  421. const vector unsigned char s##op##_10h = vec_mergeh(s##op##_10, s##op##_10); \
  422. const vector unsigned char s##op##_10l = vec_mergel(s##op##_10, s##op##_10); \
  423. const vector unsigned char s##op##_11 = vec_##op(s##op##_10h, s##op##_10l); \
  424. const vector unsigned char s##op##_11h = vec_mergeh(s##op##_11, s##op##_11); \
  425. const vector unsigned char s##op##_11l = vec_mergel(s##op##_11, s##op##_11); \
  426. v_##op = vec_##op(s##op##_11h, s##op##_11l); } while (0)
  427. vector unsigned char v_min;
  428. vector unsigned char v_max;
  429. EXTRACT(min);
  430. EXTRACT(max);
  431. #undef EXTRACT
  432. if (vec_all_lt(vec_sub(v_max, v_min), v_dt))
  433. return;
  434. v_avg = vec_avg(v_min, v_max);
  435. }
  436. signed int __attribute__((aligned(16))) S[8];
  437. {
  438. const vector unsigned short mask1 = (vector unsigned short)
  439. AVV(0x0001, 0x0002, 0x0004, 0x0008,
  440. 0x0010, 0x0020, 0x0040, 0x0080);
  441. const vector unsigned short mask2 = (vector unsigned short)
  442. AVV(0x0100, 0x0200, 0x0000, 0x0000,
  443. 0x0000, 0x0000, 0x0000, 0x0000);
  444. const vector unsigned int vuint32_16 = vec_sl(vec_splat_u32(1), vec_splat_u32(4));
  445. const vector unsigned int vuint32_1 = vec_splat_u32(1);
  446. #define COMPARE(i) \
  447. vector signed int sum##i; \
  448. do { \
  449. const vector unsigned char cmp##i = \
  450. (vector unsigned char)vec_cmpgt(src##i, v_avg); \
  451. const vector unsigned short cmpHi##i = \
  452. (vector unsigned short)vec_mergeh(cmp##i, cmp##i); \
  453. const vector unsigned short cmpLi##i = \
  454. (vector unsigned short)vec_mergel(cmp##i, cmp##i); \
  455. const vector signed short cmpHf##i = \
  456. (vector signed short)vec_and(cmpHi##i, mask1); \
  457. const vector signed short cmpLf##i = \
  458. (vector signed short)vec_and(cmpLi##i, mask2); \
  459. const vector signed int sump##i = vec_sum4s(cmpHf##i, zero); \
  460. const vector signed int sumq##i = vec_sum4s(cmpLf##i, sump##i); \
  461. sum##i = vec_sums(sumq##i, zero); } while (0)
  462. COMPARE(0);
  463. COMPARE(1);
  464. COMPARE(2);
  465. COMPARE(3);
  466. COMPARE(4);
  467. COMPARE(5);
  468. COMPARE(6);
  469. COMPARE(7);
  470. COMPARE(8);
  471. COMPARE(9);
  472. #undef COMPARE
  473. vector signed int sumA2;
  474. vector signed int sumB2;
  475. {
  476. const vector signed int sump02 = vec_mergel(sum0, sum2);
  477. const vector signed int sump13 = vec_mergel(sum1, sum3);
  478. const vector signed int sumA = vec_mergel(sump02, sump13);
  479. const vector signed int sump46 = vec_mergel(sum4, sum6);
  480. const vector signed int sump57 = vec_mergel(sum5, sum7);
  481. const vector signed int sumB = vec_mergel(sump46, sump57);
  482. const vector signed int sump8A = vec_mergel(sum8, zero);
  483. const vector signed int sump9B = vec_mergel(sum9, zero);
  484. const vector signed int sumC = vec_mergel(sump8A, sump9B);
  485. const vector signed int tA = vec_sl(vec_nor(zero, sumA), vuint32_16);
  486. const vector signed int tB = vec_sl(vec_nor(zero, sumB), vuint32_16);
  487. const vector signed int tC = vec_sl(vec_nor(zero, sumC), vuint32_16);
  488. const vector signed int t2A = vec_or(sumA, tA);
  489. const vector signed int t2B = vec_or(sumB, tB);
  490. const vector signed int t2C = vec_or(sumC, tC);
  491. const vector signed int t3A = vec_and(vec_sra(t2A, vuint32_1),
  492. vec_sl(t2A, vuint32_1));
  493. const vector signed int t3B = vec_and(vec_sra(t2B, vuint32_1),
  494. vec_sl(t2B, vuint32_1));
  495. const vector signed int t3C = vec_and(vec_sra(t2C, vuint32_1),
  496. vec_sl(t2C, vuint32_1));
  497. const vector signed int yA = vec_and(t2A, t3A);
  498. const vector signed int yB = vec_and(t2B, t3B);
  499. const vector signed int yC = vec_and(t2C, t3C);
  500. const vector unsigned char strangeperm1 = vec_lvsl(4, (unsigned char*)0);
  501. const vector unsigned char strangeperm2 = vec_lvsl(8, (unsigned char*)0);
  502. const vector signed int sumAd4 = vec_perm(yA, yB, strangeperm1);
  503. const vector signed int sumAd8 = vec_perm(yA, yB, strangeperm2);
  504. const vector signed int sumBd4 = vec_perm(yB, yC, strangeperm1);
  505. const vector signed int sumBd8 = vec_perm(yB, yC, strangeperm2);
  506. const vector signed int sumAp = vec_and(yA,
  507. vec_and(sumAd4,sumAd8));
  508. const vector signed int sumBp = vec_and(yB,
  509. vec_and(sumBd4,sumBd8));
  510. sumA2 = vec_or(sumAp,
  511. vec_sra(sumAp,
  512. vuint32_16));
  513. sumB2 = vec_or(sumBp,
  514. vec_sra(sumBp,
  515. vuint32_16));
  516. }
  517. vec_st(sumA2, 0, S);
  518. vec_st(sumB2, 16, S);
  519. }
  520. /* I'm not sure the following is actually faster
  521. than straight, unvectorized C code :-( */
  522. int __attribute__((aligned(16))) tQP2[4];
  523. tQP2[0]= c->QP/2 + 1;
  524. vector signed int vQP2 = vec_ld(0, tQP2);
  525. vQP2 = vec_splat(vQP2, 0);
  526. const vector unsigned char vuint8_2 = vec_splat_u8(2);
  527. const vector signed int vsint32_8 = vec_splat_s32(8);
  528. const vector unsigned int vuint32_4 = vec_splat_u32(4);
  529. const vector unsigned char permA1 = (vector unsigned char)
  530. AVV(0x00, 0x01, 0x02, 0x10, 0x11, 0x12, 0x1F, 0x1F,
  531. 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F);
  532. const vector unsigned char permA2 = (vector unsigned char)
  533. AVV(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x10, 0x11,
  534. 0x12, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F);
  535. const vector unsigned char permA1inc = (vector unsigned char)
  536. AVV(0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
  537. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  538. const vector unsigned char permA2inc = (vector unsigned char)
  539. AVV(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  540. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  541. const vector unsigned char magic = (vector unsigned char)
  542. AVV(0x01, 0x02, 0x01, 0x02, 0x04, 0x02, 0x01, 0x02,
  543. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  544. const vector unsigned char extractPerm = (vector unsigned char)
  545. AVV(0x10, 0x10, 0x10, 0x01, 0x10, 0x10, 0x10, 0x01,
  546. 0x10, 0x10, 0x10, 0x01, 0x10, 0x10, 0x10, 0x01);
  547. const vector unsigned char extractPermInc = (vector unsigned char)
  548. AVV(0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  549. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01);
  550. const vector unsigned char identity = vec_lvsl(0,(unsigned char *)0);
  551. const vector unsigned char tenRight = (vector unsigned char)
  552. AVV(0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  553. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  554. const vector unsigned char eightLeft = (vector unsigned char)
  555. AVV(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  556. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08);
  557. #define F_INIT(i) \
  558. vector unsigned char tenRightM##i = tenRight; \
  559. vector unsigned char permA1M##i = permA1; \
  560. vector unsigned char permA2M##i = permA2; \
  561. vector unsigned char extractPermM##i = extractPerm
  562. #define F2(i, j, k, l) \
  563. if (S[i] & (1 << (l+1))) { \
  564. const vector unsigned char a_##j##_A##l = \
  565. vec_perm(src##i, src##j, permA1M##i); \
  566. const vector unsigned char a_##j##_B##l = \
  567. vec_perm(a_##j##_A##l, src##k, permA2M##i); \
  568. const vector signed int a_##j##_sump##l = \
  569. (vector signed int)vec_msum(a_##j##_B##l, magic, \
  570. (vector unsigned int)zero); \
  571. vector signed int F_##j##_##l = \
  572. vec_sr(vec_sums(a_##j##_sump##l, vsint32_8), vuint32_4); \
  573. F_##j##_##l = vec_splat(F_##j##_##l, 3); \
  574. const vector signed int p_##j##_##l = \
  575. (vector signed int)vec_perm(src##j, \
  576. (vector unsigned char)zero, \
  577. extractPermM##i); \
  578. const vector signed int sum_##j##_##l = vec_add( p_##j##_##l, vQP2); \
  579. const vector signed int diff_##j##_##l = vec_sub( p_##j##_##l, vQP2); \
  580. vector signed int newpm_##j##_##l; \
  581. if (vec_all_lt(sum_##j##_##l, F_##j##_##l)) \
  582. newpm_##j##_##l = sum_##j##_##l; \
  583. else if (vec_all_gt(diff_##j##_##l, F_##j##_##l)) \
  584. newpm_##j##_##l = diff_##j##_##l; \
  585. else newpm_##j##_##l = F_##j##_##l; \
  586. const vector unsigned char newpm2_##j##_##l = \
  587. vec_splat((vector unsigned char)newpm_##j##_##l, 15); \
  588. const vector unsigned char mask##j##l = vec_add(identity, \
  589. tenRightM##i); \
  590. src##j = vec_perm(src##j, newpm2_##j##_##l, mask##j##l); \
  591. } \
  592. permA1M##i = vec_add(permA1M##i, permA1inc); \
  593. permA2M##i = vec_add(permA2M##i, permA2inc); \
  594. tenRightM##i = vec_sro(tenRightM##i, eightLeft); \
  595. extractPermM##i = vec_add(extractPermM##i, extractPermInc)
  596. #define ITER(i, j, k) \
  597. F_INIT(i); \
  598. F2(i, j, k, 0); \
  599. F2(i, j, k, 1); \
  600. F2(i, j, k, 2); \
  601. F2(i, j, k, 3); \
  602. F2(i, j, k, 4); \
  603. F2(i, j, k, 5); \
  604. F2(i, j, k, 6); \
  605. F2(i, j, k, 7)
  606. ITER(0, 1, 2);
  607. ITER(1, 2, 3);
  608. ITER(2, 3, 4);
  609. ITER(3, 4, 5);
  610. ITER(4, 5, 6);
  611. ITER(5, 6, 7);
  612. ITER(6, 7, 8);
  613. ITER(7, 8, 9);
  614. const vector signed char neg1 = vec_splat_s8( -1 );
  615. #define STORE_LINE(i) \
  616. const vector unsigned char permST##i = \
  617. vec_lvsr(i * stride, srcCopy); \
  618. const vector unsigned char maskST##i = \
  619. vec_perm((vector unsigned char)zero, \
  620. (vector unsigned char)neg1, permST##i); \
  621. src##i = vec_perm(src##i ,src##i, permST##i); \
  622. sA##i= vec_sel(sA##i, src##i, maskST##i); \
  623. sB##i= vec_sel(src##i, sB##i, maskST##i); \
  624. vec_st(sA##i, i * stride, srcCopy); \
  625. vec_st(sB##i, i * stride + 16, srcCopy)
  626. STORE_LINE(1);
  627. STORE_LINE(2);
  628. STORE_LINE(3);
  629. STORE_LINE(4);
  630. STORE_LINE(5);
  631. STORE_LINE(6);
  632. STORE_LINE(7);
  633. STORE_LINE(8);
  634. #undef STORE_LINE
  635. #undef ITER
  636. #undef F2
  637. }
  638. #define horizClassify_altivec(a...) horizClassify_C(a)
  639. #define doHorizLowPass_altivec(a...) doHorizLowPass_C(a)
  640. #define doHorizDefFilter_altivec(a...) doHorizDefFilter_C(a)