jidctfst-altivec.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* FAST INTEGER INVERSE DCT
  23. *
  24. * This is similar to the SSE2 implementation, except that we left-shift the
  25. * constants by 1 less bit (the -1 in CONST_SHIFT.) This is because
  26. * vec_madds(arg1, arg2, arg3) generates the 16-bit saturated sum of:
  27. * the elements in arg3 + the most significant 17 bits of
  28. * (the elements in arg1 * the elements in arg2).
  29. */
  30. #include "jsimd_altivec.h"
  31. #define F_1_082 277 /* FIX(1.082392200) */
  32. #define F_1_414 362 /* FIX(1.414213562) */
  33. #define F_1_847 473 /* FIX(1.847759065) */
  34. #define F_2_613 669 /* FIX(2.613125930) */
  35. #define F_1_613 (F_2_613 - 256) /* FIX(2.613125930) - FIX(1) */
  36. #define CONST_BITS 8
  37. #define PASS1_BITS 2
  38. #define PRE_MULTIPLY_SCALE_BITS 2
  39. #define CONST_SHIFT (16 - PRE_MULTIPLY_SCALE_BITS - CONST_BITS - 1)
  40. #define DO_IDCT(in) \
  41. { \
  42. /* Even part */ \
  43. \
  44. tmp10 = vec_add(in##0, in##4); \
  45. tmp11 = vec_sub(in##0, in##4); \
  46. tmp13 = vec_add(in##2, in##6); \
  47. \
  48. tmp12 = vec_sub(in##2, in##6); \
  49. tmp12 = vec_sl(tmp12, pre_multiply_scale_bits); \
  50. tmp12 = vec_madds(tmp12, pw_F1414, pw_zero); \
  51. tmp12 = vec_sub(tmp12, tmp13); \
  52. \
  53. tmp0 = vec_add(tmp10, tmp13); \
  54. tmp3 = vec_sub(tmp10, tmp13); \
  55. tmp1 = vec_add(tmp11, tmp12); \
  56. tmp2 = vec_sub(tmp11, tmp12); \
  57. \
  58. /* Odd part */ \
  59. \
  60. z13 = vec_add(in##5, in##3); \
  61. z10 = vec_sub(in##5, in##3); \
  62. z10s = vec_sl(z10, pre_multiply_scale_bits); \
  63. z11 = vec_add(in##1, in##7); \
  64. z12s = vec_sub(in##1, in##7); \
  65. z12s = vec_sl(z12s, pre_multiply_scale_bits); \
  66. \
  67. tmp11 = vec_sub(z11, z13); \
  68. tmp11 = vec_sl(tmp11, pre_multiply_scale_bits); \
  69. tmp11 = vec_madds(tmp11, pw_F1414, pw_zero); \
  70. \
  71. tmp7 = vec_add(z11, z13); \
  72. \
  73. /* To avoid overflow... \
  74. * \
  75. * (Original) \
  76. * tmp12 = -2.613125930 * z10 + z5; \
  77. * \
  78. * (This implementation) \
  79. * tmp12 = (-1.613125930 - 1) * z10 + z5; \
  80. * = -1.613125930 * z10 - z10 + z5; \
  81. */ \
  82. \
  83. z5 = vec_add(z10s, z12s); \
  84. z5 = vec_madds(z5, pw_F1847, pw_zero); \
  85. \
  86. tmp10 = vec_madds(z12s, pw_F1082, pw_zero); \
  87. tmp10 = vec_sub(tmp10, z5); \
  88. tmp12 = vec_madds(z10s, pw_MF1613, z5); \
  89. tmp12 = vec_sub(tmp12, z10); \
  90. \
  91. tmp6 = vec_sub(tmp12, tmp7); \
  92. tmp5 = vec_sub(tmp11, tmp6); \
  93. tmp4 = vec_add(tmp10, tmp5); \
  94. \
  95. out0 = vec_add(tmp0, tmp7); \
  96. out1 = vec_add(tmp1, tmp6); \
  97. out2 = vec_add(tmp2, tmp5); \
  98. out3 = vec_sub(tmp3, tmp4); \
  99. out4 = vec_add(tmp3, tmp4); \
  100. out5 = vec_sub(tmp2, tmp5); \
  101. out6 = vec_sub(tmp1, tmp6); \
  102. out7 = vec_sub(tmp0, tmp7); \
  103. }
  104. void
  105. jsimd_idct_ifast_altivec (void *dct_table_, JCOEFPTR coef_block,
  106. JSAMPARRAY output_buf, JDIMENSION output_col)
  107. {
  108. short *dct_table = (short *)dct_table_;
  109. int *outptr;
  110. __vector short row0, row1, row2, row3, row4, row5, row6, row7,
  111. col0, col1, col2, col3, col4, col5, col6, col7,
  112. quant0, quant1, quant2, quant3, quant4, quant5, quant6, quant7,
  113. tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp10, tmp11, tmp12, tmp13,
  114. z5, z10, z10s, z11, z12s, z13,
  115. out0, out1, out2, out3, out4, out5, out6, out7;
  116. __vector signed char outb;
  117. /* Constants */
  118. __vector short pw_zero = { __8X(0) },
  119. pw_F1414 = { __8X(F_1_414 << CONST_SHIFT) },
  120. pw_F1847 = { __8X(F_1_847 << CONST_SHIFT) },
  121. pw_MF1613 = { __8X(-F_1_613 << CONST_SHIFT) },
  122. pw_F1082 = { __8X(F_1_082 << CONST_SHIFT) };
  123. __vector unsigned short
  124. pre_multiply_scale_bits = { __8X(PRE_MULTIPLY_SCALE_BITS) },
  125. pass1_bits3 = { __8X(PASS1_BITS + 3) };
  126. __vector signed char pb_centerjsamp = { __16X(CENTERJSAMPLE) };
  127. /* Pass 1: process columns */
  128. col0 = vec_ld(0, coef_block);
  129. col1 = vec_ld(16, coef_block);
  130. col2 = vec_ld(32, coef_block);
  131. col3 = vec_ld(48, coef_block);
  132. col4 = vec_ld(64, coef_block);
  133. col5 = vec_ld(80, coef_block);
  134. col6 = vec_ld(96, coef_block);
  135. col7 = vec_ld(112, coef_block);
  136. tmp1 = vec_or(col1, col2);
  137. tmp2 = vec_or(col3, col4);
  138. tmp1 = vec_or(tmp1, tmp2);
  139. tmp3 = vec_or(col5, col6);
  140. tmp3 = vec_or(tmp3, col7);
  141. tmp1 = vec_or(tmp1, tmp3);
  142. quant0 = vec_ld(0, dct_table);
  143. col0 = vec_mladd(col0, quant0, pw_zero);
  144. if (vec_all_eq(tmp1, pw_zero)) {
  145. /* AC terms all zero */
  146. row0 = vec_splat(col0, 0);
  147. row1 = vec_splat(col0, 1);
  148. row2 = vec_splat(col0, 2);
  149. row3 = vec_splat(col0, 3);
  150. row4 = vec_splat(col0, 4);
  151. row5 = vec_splat(col0, 5);
  152. row6 = vec_splat(col0, 6);
  153. row7 = vec_splat(col0, 7);
  154. } else {
  155. quant1 = vec_ld(16, dct_table);
  156. quant2 = vec_ld(32, dct_table);
  157. quant3 = vec_ld(48, dct_table);
  158. quant4 = vec_ld(64, dct_table);
  159. quant5 = vec_ld(80, dct_table);
  160. quant6 = vec_ld(96, dct_table);
  161. quant7 = vec_ld(112, dct_table);
  162. col1 = vec_mladd(col1, quant1, pw_zero);
  163. col2 = vec_mladd(col2, quant2, pw_zero);
  164. col3 = vec_mladd(col3, quant3, pw_zero);
  165. col4 = vec_mladd(col4, quant4, pw_zero);
  166. col5 = vec_mladd(col5, quant5, pw_zero);
  167. col6 = vec_mladd(col6, quant6, pw_zero);
  168. col7 = vec_mladd(col7, quant7, pw_zero);
  169. DO_IDCT(col);
  170. TRANSPOSE(out, row);
  171. }
  172. /* Pass 2: process rows */
  173. DO_IDCT(row);
  174. out0 = vec_sra(out0, pass1_bits3);
  175. out1 = vec_sra(out1, pass1_bits3);
  176. out2 = vec_sra(out2, pass1_bits3);
  177. out3 = vec_sra(out3, pass1_bits3);
  178. out4 = vec_sra(out4, pass1_bits3);
  179. out5 = vec_sra(out5, pass1_bits3);
  180. out6 = vec_sra(out6, pass1_bits3);
  181. out7 = vec_sra(out7, pass1_bits3);
  182. TRANSPOSE(out, col);
  183. outb = vec_packs(col0, col0);
  184. outb = vec_add(outb, pb_centerjsamp);
  185. outptr = (int *)(output_buf[0] + output_col);
  186. vec_ste((__vector int)outb, 0, outptr);
  187. vec_ste((__vector int)outb, 4, outptr);
  188. outb = vec_packs(col1, col1);
  189. outb = vec_add(outb, pb_centerjsamp);
  190. outptr = (int *)(output_buf[1] + output_col);
  191. vec_ste((__vector int)outb, 0, outptr);
  192. vec_ste((__vector int)outb, 4, outptr);
  193. outb = vec_packs(col2, col2);
  194. outb = vec_add(outb, pb_centerjsamp);
  195. outptr = (int *)(output_buf[2] + output_col);
  196. vec_ste((__vector int)outb, 0, outptr);
  197. vec_ste((__vector int)outb, 4, outptr);
  198. outb = vec_packs(col3, col3);
  199. outb = vec_add(outb, pb_centerjsamp);
  200. outptr = (int *)(output_buf[3] + output_col);
  201. vec_ste((__vector int)outb, 0, outptr);
  202. vec_ste((__vector int)outb, 4, outptr);
  203. outb = vec_packs(col4, col4);
  204. outb = vec_add(outb, pb_centerjsamp);
  205. outptr = (int *)(output_buf[4] + output_col);
  206. vec_ste((__vector int)outb, 0, outptr);
  207. vec_ste((__vector int)outb, 4, outptr);
  208. outb = vec_packs(col5, col5);
  209. outb = vec_add(outb, pb_centerjsamp);
  210. outptr = (int *)(output_buf[5] + output_col);
  211. vec_ste((__vector int)outb, 0, outptr);
  212. vec_ste((__vector int)outb, 4, outptr);
  213. outb = vec_packs(col6, col6);
  214. outb = vec_add(outb, pb_centerjsamp);
  215. outptr = (int *)(output_buf[6] + output_col);
  216. vec_ste((__vector int)outb, 0, outptr);
  217. vec_ste((__vector int)outb, 4, outptr);
  218. outb = vec_packs(col7, col7);
  219. outb = vec_add(outb, pb_centerjsamp);
  220. outptr = (int *)(output_buf[7] + output_col);
  221. vec_ste((__vector int)outb, 0, outptr);
  222. vec_ste((__vector int)outb, 4, outptr);
  223. }