jidctint-altivec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * AltiVec optimizations for libjpeg-turbo
  3. *
  4. * Copyright (C) 2014-2015, D. R. Commander. All Rights Reserved.
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the authors be held liable for any damages
  8. * arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose,
  11. * including commercial applications, and to alter it and redistribute it
  12. * freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must not
  15. * claim that you wrote the original software. If you use this software
  16. * in a product, an acknowledgment in the product documentation would be
  17. * appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. /* SLOW INTEGER INVERSE DCT */
  23. #include "jsimd_altivec.h"
  24. #define F_0_298 2446 /* FIX(0.298631336) */
  25. #define F_0_390 3196 /* FIX(0.390180644) */
  26. #define F_0_541 4433 /* FIX(0.541196100) */
  27. #define F_0_765 6270 /* FIX(0.765366865) */
  28. #define F_0_899 7373 /* FIX(0.899976223) */
  29. #define F_1_175 9633 /* FIX(1.175875602) */
  30. #define F_1_501 12299 /* FIX(1.501321110) */
  31. #define F_1_847 15137 /* FIX(1.847759065) */
  32. #define F_1_961 16069 /* FIX(1.961570560) */
  33. #define F_2_053 16819 /* FIX(2.053119869) */
  34. #define F_2_562 20995 /* FIX(2.562915447) */
  35. #define F_3_072 25172 /* FIX(3.072711026) */
  36. #define CONST_BITS 13
  37. #define PASS1_BITS 2
  38. #define DESCALE_P1 (CONST_BITS - PASS1_BITS)
  39. #define DESCALE_P2 (CONST_BITS + PASS1_BITS + 3)
  40. #define DO_IDCT(in, PASS) { \
  41. /* Even part \
  42. * \
  43. * (Original) \
  44. * z1 = (z2 + z3) * 0.541196100; \
  45. * tmp2 = z1 + z3 * -1.847759065; \
  46. * tmp3 = z1 + z2 * 0.765366865; \
  47. * \
  48. * (This implementation) \
  49. * tmp2 = z2 * 0.541196100 + z3 * (0.541196100 - 1.847759065); \
  50. * tmp3 = z2 * (0.541196100 + 0.765366865) + z3 * 0.541196100; \
  51. */ \
  52. \
  53. in##26l = vec_mergeh(in##2, in##6); \
  54. in##26h = vec_mergel(in##2, in##6); \
  55. \
  56. tmp3l = vec_msums(in##26l, pw_f130_f054, pd_zero); \
  57. tmp3h = vec_msums(in##26h, pw_f130_f054, pd_zero); \
  58. tmp2l = vec_msums(in##26l, pw_f054_mf130, pd_zero); \
  59. tmp2h = vec_msums(in##26h, pw_f054_mf130, pd_zero); \
  60. \
  61. tmp0 = vec_add(in##0, in##4); \
  62. tmp1 = vec_sub(in##0, in##4); \
  63. \
  64. tmp0l = vec_unpackh(tmp0); \
  65. tmp0h = vec_unpackl(tmp0); \
  66. tmp0l = vec_sl(tmp0l, const_bits); \
  67. tmp0h = vec_sl(tmp0h, const_bits); \
  68. tmp0l = vec_add(tmp0l, pd_descale_p##PASS); \
  69. tmp0h = vec_add(tmp0h, pd_descale_p##PASS); \
  70. \
  71. tmp10l = vec_add(tmp0l, tmp3l); \
  72. tmp10h = vec_add(tmp0h, tmp3h); \
  73. tmp13l = vec_sub(tmp0l, tmp3l); \
  74. tmp13h = vec_sub(tmp0h, tmp3h); \
  75. \
  76. tmp1l = vec_unpackh(tmp1); \
  77. tmp1h = vec_unpackl(tmp1); \
  78. tmp1l = vec_sl(tmp1l, const_bits); \
  79. tmp1h = vec_sl(tmp1h, const_bits); \
  80. tmp1l = vec_add(tmp1l, pd_descale_p##PASS); \
  81. tmp1h = vec_add(tmp1h, pd_descale_p##PASS); \
  82. \
  83. tmp11l = vec_add(tmp1l, tmp2l); \
  84. tmp11h = vec_add(tmp1h, tmp2h); \
  85. tmp12l = vec_sub(tmp1l, tmp2l); \
  86. tmp12h = vec_sub(tmp1h, tmp2h); \
  87. \
  88. /* Odd part */ \
  89. \
  90. z3 = vec_add(in##3, in##7); \
  91. z4 = vec_add(in##1, in##5); \
  92. \
  93. /* (Original) \
  94. * z5 = (z3 + z4) * 1.175875602; \
  95. * z3 = z3 * -1.961570560; z4 = z4 * -0.390180644; \
  96. * z3 += z5; z4 += z5; \
  97. * \
  98. * (This implementation) \
  99. * z3 = z3 * (1.175875602 - 1.961570560) + z4 * 1.175875602; \
  100. * z4 = z3 * 1.175875602 + z4 * (1.175875602 - 0.390180644); \
  101. */ \
  102. \
  103. z34l = vec_mergeh(z3, z4); \
  104. z34h = vec_mergel(z3, z4); \
  105. \
  106. z3l = vec_msums(z34l, pw_mf078_f117, pd_zero); \
  107. z3h = vec_msums(z34h, pw_mf078_f117, pd_zero); \
  108. z4l = vec_msums(z34l, pw_f117_f078, pd_zero); \
  109. z4h = vec_msums(z34h, pw_f117_f078, pd_zero); \
  110. \
  111. /* (Original) \
  112. * z1 = tmp0 + tmp3; z2 = tmp1 + tmp2; \
  113. * tmp0 = tmp0 * 0.298631336; tmp1 = tmp1 * 2.053119869; \
  114. * tmp2 = tmp2 * 3.072711026; tmp3 = tmp3 * 1.501321110; \
  115. * z1 = z1 * -0.899976223; z2 = z2 * -2.562915447; \
  116. * tmp0 += z1 + z3; tmp1 += z2 + z4; \
  117. * tmp2 += z2 + z3; tmp3 += z1 + z4; \
  118. * \
  119. * (This implementation) \
  120. * tmp0 = tmp0 * (0.298631336 - 0.899976223) + tmp3 * -0.899976223; \
  121. * tmp1 = tmp1 * (2.053119869 - 2.562915447) + tmp2 * -2.562915447; \
  122. * tmp2 = tmp1 * -2.562915447 + tmp2 * (3.072711026 - 2.562915447); \
  123. * tmp3 = tmp0 * -0.899976223 + tmp3 * (1.501321110 - 0.899976223); \
  124. * tmp0 += z3; tmp1 += z4; \
  125. * tmp2 += z3; tmp3 += z4; \
  126. */ \
  127. \
  128. in##71l = vec_mergeh(in##7, in##1); \
  129. in##71h = vec_mergel(in##7, in##1); \
  130. \
  131. tmp0l = vec_msums(in##71l, pw_mf060_mf089, z3l); \
  132. tmp0h = vec_msums(in##71h, pw_mf060_mf089, z3h); \
  133. tmp3l = vec_msums(in##71l, pw_mf089_f060, z4l); \
  134. tmp3h = vec_msums(in##71h, pw_mf089_f060, z4h); \
  135. \
  136. in##53l = vec_mergeh(in##5, in##3); \
  137. in##53h = vec_mergel(in##5, in##3); \
  138. \
  139. tmp1l = vec_msums(in##53l, pw_mf050_mf256, z4l); \
  140. tmp1h = vec_msums(in##53h, pw_mf050_mf256, z4h); \
  141. tmp2l = vec_msums(in##53l, pw_mf256_f050, z3l); \
  142. tmp2h = vec_msums(in##53h, pw_mf256_f050, z3h); \
  143. \
  144. /* Final output stage */ \
  145. \
  146. out0l = vec_add(tmp10l, tmp3l); \
  147. out0h = vec_add(tmp10h, tmp3h); \
  148. out7l = vec_sub(tmp10l, tmp3l); \
  149. out7h = vec_sub(tmp10h, tmp3h); \
  150. \
  151. out0l = vec_sra(out0l, descale_p##PASS); \
  152. out0h = vec_sra(out0h, descale_p##PASS); \
  153. out7l = vec_sra(out7l, descale_p##PASS); \
  154. out7h = vec_sra(out7h, descale_p##PASS); \
  155. \
  156. out0 = vec_pack(out0l, out0h); \
  157. out7 = vec_pack(out7l, out7h); \
  158. \
  159. out1l = vec_add(tmp11l, tmp2l); \
  160. out1h = vec_add(tmp11h, tmp2h); \
  161. out6l = vec_sub(tmp11l, tmp2l); \
  162. out6h = vec_sub(tmp11h, tmp2h); \
  163. \
  164. out1l = vec_sra(out1l, descale_p##PASS); \
  165. out1h = vec_sra(out1h, descale_p##PASS); \
  166. out6l = vec_sra(out6l, descale_p##PASS); \
  167. out6h = vec_sra(out6h, descale_p##PASS); \
  168. \
  169. out1 = vec_pack(out1l, out1h); \
  170. out6 = vec_pack(out6l, out6h); \
  171. \
  172. out2l = vec_add(tmp12l, tmp1l); \
  173. out2h = vec_add(tmp12h, tmp1h); \
  174. out5l = vec_sub(tmp12l, tmp1l); \
  175. out5h = vec_sub(tmp12h, tmp1h); \
  176. \
  177. out2l = vec_sra(out2l, descale_p##PASS); \
  178. out2h = vec_sra(out2h, descale_p##PASS); \
  179. out5l = vec_sra(out5l, descale_p##PASS); \
  180. out5h = vec_sra(out5h, descale_p##PASS); \
  181. \
  182. out2 = vec_pack(out2l, out2h); \
  183. out5 = vec_pack(out5l, out5h); \
  184. \
  185. out3l = vec_add(tmp13l, tmp0l); \
  186. out3h = vec_add(tmp13h, tmp0h); \
  187. out4l = vec_sub(tmp13l, tmp0l); \
  188. out4h = vec_sub(tmp13h, tmp0h); \
  189. \
  190. out3l = vec_sra(out3l, descale_p##PASS); \
  191. out3h = vec_sra(out3h, descale_p##PASS); \
  192. out4l = vec_sra(out4l, descale_p##PASS); \
  193. out4h = vec_sra(out4h, descale_p##PASS); \
  194. \
  195. out3 = vec_pack(out3l, out3h); \
  196. out4 = vec_pack(out4l, out4h); \
  197. }
  198. void jsimd_idct_islow_altivec(void *dct_table_, JCOEFPTR coef_block,
  199. JSAMPARRAY output_buf, JDIMENSION output_col)
  200. {
  201. short *dct_table = (short *)dct_table_;
  202. int *outptr;
  203. __vector short row0, row1, row2, row3, row4, row5, row6, row7,
  204. col0, col1, col2, col3, col4, col5, col6, col7,
  205. quant0, quant1, quant2, quant3, quant4, quant5, quant6, quant7,
  206. tmp0, tmp1, tmp2, tmp3, z3, z4,
  207. z34l, z34h, col71l, col71h, col26l, col26h, col53l, col53h,
  208. row71l, row71h, row26l, row26h, row53l, row53h,
  209. out0, out1, out2, out3, out4, out5, out6, out7;
  210. __vector int tmp0l, tmp0h, tmp1l, tmp1h, tmp2l, tmp2h, tmp3l, tmp3h,
  211. tmp10l, tmp10h, tmp11l, tmp11h, tmp12l, tmp12h, tmp13l, tmp13h,
  212. z3l, z3h, z4l, z4h,
  213. out0l, out0h, out1l, out1h, out2l, out2h, out3l, out3h, out4l, out4h,
  214. out5l, out5h, out6l, out6h, out7l, out7h;
  215. __vector signed char outb;
  216. /* Constants */
  217. __vector short pw_zero = { __8X(0) },
  218. pw_f130_f054 = { __4X2(F_0_541 + F_0_765, F_0_541) },
  219. pw_f054_mf130 = { __4X2(F_0_541, F_0_541 - F_1_847) },
  220. pw_mf078_f117 = { __4X2(F_1_175 - F_1_961, F_1_175) },
  221. pw_f117_f078 = { __4X2(F_1_175, F_1_175 - F_0_390) },
  222. pw_mf060_mf089 = { __4X2(F_0_298 - F_0_899, -F_0_899) },
  223. pw_mf089_f060 = { __4X2(-F_0_899, F_1_501 - F_0_899) },
  224. pw_mf050_mf256 = { __4X2(F_2_053 - F_2_562, -F_2_562) },
  225. pw_mf256_f050 = { __4X2(-F_2_562, F_3_072 - F_2_562) };
  226. __vector unsigned short pass1_bits = { __8X(PASS1_BITS) };
  227. __vector int pd_zero = { __4X(0) },
  228. pd_descale_p1 = { __4X(1 << (DESCALE_P1 - 1)) },
  229. pd_descale_p2 = { __4X(1 << (DESCALE_P2 - 1)) };
  230. __vector unsigned int descale_p1 = { __4X(DESCALE_P1) },
  231. descale_p2 = { __4X(DESCALE_P2) },
  232. const_bits = { __4X(CONST_BITS) };
  233. __vector signed char pb_centerjsamp = { __16X(CENTERJSAMPLE) };
  234. /* Pass 1: process columns */
  235. col0 = vec_ld(0, coef_block);
  236. col1 = vec_ld(16, coef_block);
  237. col2 = vec_ld(32, coef_block);
  238. col3 = vec_ld(48, coef_block);
  239. col4 = vec_ld(64, coef_block);
  240. col5 = vec_ld(80, coef_block);
  241. col6 = vec_ld(96, coef_block);
  242. col7 = vec_ld(112, coef_block);
  243. tmp1 = vec_or(col1, col2);
  244. tmp2 = vec_or(col3, col4);
  245. tmp1 = vec_or(tmp1, tmp2);
  246. tmp3 = vec_or(col5, col6);
  247. tmp3 = vec_or(tmp3, col7);
  248. tmp1 = vec_or(tmp1, tmp3);
  249. quant0 = vec_ld(0, dct_table);
  250. col0 = vec_mladd(col0, quant0, pw_zero);
  251. if (vec_all_eq(tmp1, pw_zero)) {
  252. /* AC terms all zero */
  253. col0 = vec_sl(col0, pass1_bits);
  254. row0 = vec_splat(col0, 0);
  255. row1 = vec_splat(col0, 1);
  256. row2 = vec_splat(col0, 2);
  257. row3 = vec_splat(col0, 3);
  258. row4 = vec_splat(col0, 4);
  259. row5 = vec_splat(col0, 5);
  260. row6 = vec_splat(col0, 6);
  261. row7 = vec_splat(col0, 7);
  262. } else {
  263. quant1 = vec_ld(16, dct_table);
  264. quant2 = vec_ld(32, dct_table);
  265. quant3 = vec_ld(48, dct_table);
  266. quant4 = vec_ld(64, dct_table);
  267. quant5 = vec_ld(80, dct_table);
  268. quant6 = vec_ld(96, dct_table);
  269. quant7 = vec_ld(112, dct_table);
  270. col1 = vec_mladd(col1, quant1, pw_zero);
  271. col2 = vec_mladd(col2, quant2, pw_zero);
  272. col3 = vec_mladd(col3, quant3, pw_zero);
  273. col4 = vec_mladd(col4, quant4, pw_zero);
  274. col5 = vec_mladd(col5, quant5, pw_zero);
  275. col6 = vec_mladd(col6, quant6, pw_zero);
  276. col7 = vec_mladd(col7, quant7, pw_zero);
  277. DO_IDCT(col, 1);
  278. TRANSPOSE(out, row);
  279. }
  280. /* Pass 2: process rows */
  281. DO_IDCT(row, 2);
  282. TRANSPOSE(out, col);
  283. outb = vec_packs(col0, col0);
  284. outb = vec_add(outb, pb_centerjsamp);
  285. outptr = (int *)(output_buf[0] + output_col);
  286. vec_ste((__vector int)outb, 0, outptr);
  287. vec_ste((__vector int)outb, 4, outptr);
  288. outb = vec_packs(col1, col1);
  289. outb = vec_add(outb, pb_centerjsamp);
  290. outptr = (int *)(output_buf[1] + output_col);
  291. vec_ste((__vector int)outb, 0, outptr);
  292. vec_ste((__vector int)outb, 4, outptr);
  293. outb = vec_packs(col2, col2);
  294. outb = vec_add(outb, pb_centerjsamp);
  295. outptr = (int *)(output_buf[2] + output_col);
  296. vec_ste((__vector int)outb, 0, outptr);
  297. vec_ste((__vector int)outb, 4, outptr);
  298. outb = vec_packs(col3, col3);
  299. outb = vec_add(outb, pb_centerjsamp);
  300. outptr = (int *)(output_buf[3] + output_col);
  301. vec_ste((__vector int)outb, 0, outptr);
  302. vec_ste((__vector int)outb, 4, outptr);
  303. outb = vec_packs(col4, col4);
  304. outb = vec_add(outb, pb_centerjsamp);
  305. outptr = (int *)(output_buf[4] + output_col);
  306. vec_ste((__vector int)outb, 0, outptr);
  307. vec_ste((__vector int)outb, 4, outptr);
  308. outb = vec_packs(col5, col5);
  309. outb = vec_add(outb, pb_centerjsamp);
  310. outptr = (int *)(output_buf[5] + output_col);
  311. vec_ste((__vector int)outb, 0, outptr);
  312. vec_ste((__vector int)outb, 4, outptr);
  313. outb = vec_packs(col6, col6);
  314. outb = vec_add(outb, pb_centerjsamp);
  315. outptr = (int *)(output_buf[6] + output_col);
  316. vec_ste((__vector int)outb, 0, outptr);
  317. vec_ste((__vector int)outb, 4, outptr);
  318. outb = vec_packs(col7, col7);
  319. outb = vec_add(outb, pb_centerjsamp);
  320. outptr = (int *)(output_buf[7] + output_col);
  321. vec_ste((__vector int)outb, 0, outptr);
  322. vec_ste((__vector int)outb, 4, outptr);
  323. }