jquanti-altivec.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /* INTEGER QUANTIZATION AND SAMPLE CONVERSION */
  23. #include "jsimd_altivec.h"
  24. /* NOTE: The address will either be aligned or offset by 8 bytes, so we can
  25. * always get the data we want by using a single vector load (although we may
  26. * have to permute the result.)
  27. */
  28. #if __BIG_ENDIAN__
  29. #define LOAD_ROW(row) { \
  30. elemptr = sample_data[row] + start_col; \
  31. in##row = vec_ld(0, elemptr); \
  32. if ((size_t)elemptr & 15) \
  33. in##row = vec_perm(in##row, in##row, vec_lvsl(0, elemptr)); \
  34. }
  35. #else
  36. #define LOAD_ROW(row) { \
  37. elemptr = sample_data[row] + start_col; \
  38. in##row = vec_vsx_ld(0, elemptr); \
  39. }
  40. #endif
  41. void jsimd_convsamp_altivec(JSAMPARRAY sample_data, JDIMENSION start_col,
  42. DCTELEM *workspace)
  43. {
  44. JSAMPROW elemptr;
  45. __vector unsigned char in0, in1, in2, in3, in4, in5, in6, in7;
  46. __vector short out0, out1, out2, out3, out4, out5, out6, out7;
  47. /* Constants */
  48. __vector short pw_centerjsamp = { __8X(CENTERJSAMPLE) };
  49. __vector unsigned char pb_zero = { __16X(0) };
  50. LOAD_ROW(0);
  51. LOAD_ROW(1);
  52. LOAD_ROW(2);
  53. LOAD_ROW(3);
  54. LOAD_ROW(4);
  55. LOAD_ROW(5);
  56. LOAD_ROW(6);
  57. LOAD_ROW(7);
  58. out0 = (__vector short)VEC_UNPACKHU(in0);
  59. out1 = (__vector short)VEC_UNPACKHU(in1);
  60. out2 = (__vector short)VEC_UNPACKHU(in2);
  61. out3 = (__vector short)VEC_UNPACKHU(in3);
  62. out4 = (__vector short)VEC_UNPACKHU(in4);
  63. out5 = (__vector short)VEC_UNPACKHU(in5);
  64. out6 = (__vector short)VEC_UNPACKHU(in6);
  65. out7 = (__vector short)VEC_UNPACKHU(in7);
  66. out0 = vec_sub(out0, pw_centerjsamp);
  67. out1 = vec_sub(out1, pw_centerjsamp);
  68. out2 = vec_sub(out2, pw_centerjsamp);
  69. out3 = vec_sub(out3, pw_centerjsamp);
  70. out4 = vec_sub(out4, pw_centerjsamp);
  71. out5 = vec_sub(out5, pw_centerjsamp);
  72. out6 = vec_sub(out6, pw_centerjsamp);
  73. out7 = vec_sub(out7, pw_centerjsamp);
  74. vec_st(out0, 0, workspace);
  75. vec_st(out1, 16, workspace);
  76. vec_st(out2, 32, workspace);
  77. vec_st(out3, 48, workspace);
  78. vec_st(out4, 64, workspace);
  79. vec_st(out5, 80, workspace);
  80. vec_st(out6, 96, workspace);
  81. vec_st(out7, 112, workspace);
  82. }
  83. #define WORD_BIT 16
  84. /* There is no AltiVec 16-bit unsigned multiply instruction, hence this.
  85. We basically need an unsigned equivalent of vec_madds(). */
  86. #define MULTIPLY(vs0, vs1, out) { \
  87. tmpe = vec_mule((__vector unsigned short)vs0, \
  88. (__vector unsigned short)vs1); \
  89. tmpo = vec_mulo((__vector unsigned short)vs0, \
  90. (__vector unsigned short)vs1); \
  91. out = (__vector short)vec_perm((__vector unsigned short)tmpe, \
  92. (__vector unsigned short)tmpo, \
  93. shift_pack_index); \
  94. }
  95. void jsimd_quantize_altivec(JCOEFPTR coef_block, DCTELEM *divisors,
  96. DCTELEM *workspace)
  97. {
  98. __vector short row0, row1, row2, row3, row4, row5, row6, row7,
  99. row0s, row1s, row2s, row3s, row4s, row5s, row6s, row7s,
  100. corr0, corr1, corr2, corr3, corr4, corr5, corr6, corr7,
  101. recip0, recip1, recip2, recip3, recip4, recip5, recip6, recip7,
  102. scale0, scale1, scale2, scale3, scale4, scale5, scale6, scale7;
  103. __vector unsigned int tmpe, tmpo;
  104. /* Constants */
  105. __vector unsigned short pw_word_bit_m1 = { __8X(WORD_BIT - 1) };
  106. #if __BIG_ENDIAN__
  107. __vector unsigned char shift_pack_index =
  108. { 0, 1, 16, 17, 4, 5, 20, 21, 8, 9, 24, 25, 12, 13, 28, 29 };
  109. #else
  110. __vector unsigned char shift_pack_index =
  111. { 2, 3, 18, 19, 6, 7, 22, 23, 10, 11, 26, 27, 14, 15, 30, 31 };
  112. #endif
  113. row0 = vec_ld(0, workspace);
  114. row1 = vec_ld(16, workspace);
  115. row2 = vec_ld(32, workspace);
  116. row3 = vec_ld(48, workspace);
  117. row4 = vec_ld(64, workspace);
  118. row5 = vec_ld(80, workspace);
  119. row6 = vec_ld(96, workspace);
  120. row7 = vec_ld(112, workspace);
  121. /* Branch-less absolute value */
  122. row0s = vec_sra(row0, pw_word_bit_m1);
  123. row1s = vec_sra(row1, pw_word_bit_m1);
  124. row2s = vec_sra(row2, pw_word_bit_m1);
  125. row3s = vec_sra(row3, pw_word_bit_m1);
  126. row4s = vec_sra(row4, pw_word_bit_m1);
  127. row5s = vec_sra(row5, pw_word_bit_m1);
  128. row6s = vec_sra(row6, pw_word_bit_m1);
  129. row7s = vec_sra(row7, pw_word_bit_m1);
  130. row0 = vec_xor(row0, row0s);
  131. row1 = vec_xor(row1, row1s);
  132. row2 = vec_xor(row2, row2s);
  133. row3 = vec_xor(row3, row3s);
  134. row4 = vec_xor(row4, row4s);
  135. row5 = vec_xor(row5, row5s);
  136. row6 = vec_xor(row6, row6s);
  137. row7 = vec_xor(row7, row7s);
  138. row0 = vec_sub(row0, row0s);
  139. row1 = vec_sub(row1, row1s);
  140. row2 = vec_sub(row2, row2s);
  141. row3 = vec_sub(row3, row3s);
  142. row4 = vec_sub(row4, row4s);
  143. row5 = vec_sub(row5, row5s);
  144. row6 = vec_sub(row6, row6s);
  145. row7 = vec_sub(row7, row7s);
  146. corr0 = vec_ld(DCTSIZE2 * 2, divisors);
  147. corr1 = vec_ld(DCTSIZE2 * 2 + 16, divisors);
  148. corr2 = vec_ld(DCTSIZE2 * 2 + 32, divisors);
  149. corr3 = vec_ld(DCTSIZE2 * 2 + 48, divisors);
  150. corr4 = vec_ld(DCTSIZE2 * 2 + 64, divisors);
  151. corr5 = vec_ld(DCTSIZE2 * 2 + 80, divisors);
  152. corr6 = vec_ld(DCTSIZE2 * 2 + 96, divisors);
  153. corr7 = vec_ld(DCTSIZE2 * 2 + 112, divisors);
  154. row0 = vec_add(row0, corr0);
  155. row1 = vec_add(row1, corr1);
  156. row2 = vec_add(row2, corr2);
  157. row3 = vec_add(row3, corr3);
  158. row4 = vec_add(row4, corr4);
  159. row5 = vec_add(row5, corr5);
  160. row6 = vec_add(row6, corr6);
  161. row7 = vec_add(row7, corr7);
  162. recip0 = vec_ld(0, divisors);
  163. recip1 = vec_ld(16, divisors);
  164. recip2 = vec_ld(32, divisors);
  165. recip3 = vec_ld(48, divisors);
  166. recip4 = vec_ld(64, divisors);
  167. recip5 = vec_ld(80, divisors);
  168. recip6 = vec_ld(96, divisors);
  169. recip7 = vec_ld(112, divisors);
  170. MULTIPLY(row0, recip0, row0);
  171. MULTIPLY(row1, recip1, row1);
  172. MULTIPLY(row2, recip2, row2);
  173. MULTIPLY(row3, recip3, row3);
  174. MULTIPLY(row4, recip4, row4);
  175. MULTIPLY(row5, recip5, row5);
  176. MULTIPLY(row6, recip6, row6);
  177. MULTIPLY(row7, recip7, row7);
  178. scale0 = vec_ld(DCTSIZE2 * 4, divisors);
  179. scale1 = vec_ld(DCTSIZE2 * 4 + 16, divisors);
  180. scale2 = vec_ld(DCTSIZE2 * 4 + 32, divisors);
  181. scale3 = vec_ld(DCTSIZE2 * 4 + 48, divisors);
  182. scale4 = vec_ld(DCTSIZE2 * 4 + 64, divisors);
  183. scale5 = vec_ld(DCTSIZE2 * 4 + 80, divisors);
  184. scale6 = vec_ld(DCTSIZE2 * 4 + 96, divisors);
  185. scale7 = vec_ld(DCTSIZE2 * 4 + 112, divisors);
  186. MULTIPLY(row0, scale0, row0);
  187. MULTIPLY(row1, scale1, row1);
  188. MULTIPLY(row2, scale2, row2);
  189. MULTIPLY(row3, scale3, row3);
  190. MULTIPLY(row4, scale4, row4);
  191. MULTIPLY(row5, scale5, row5);
  192. MULTIPLY(row6, scale6, row6);
  193. MULTIPLY(row7, scale7, row7);
  194. row0 = vec_xor(row0, row0s);
  195. row1 = vec_xor(row1, row1s);
  196. row2 = vec_xor(row2, row2s);
  197. row3 = vec_xor(row3, row3s);
  198. row4 = vec_xor(row4, row4s);
  199. row5 = vec_xor(row5, row5s);
  200. row6 = vec_xor(row6, row6s);
  201. row7 = vec_xor(row7, row7s);
  202. row0 = vec_sub(row0, row0s);
  203. row1 = vec_sub(row1, row1s);
  204. row2 = vec_sub(row2, row2s);
  205. row3 = vec_sub(row3, row3s);
  206. row4 = vec_sub(row4, row4s);
  207. row5 = vec_sub(row5, row5s);
  208. row6 = vec_sub(row6, row6s);
  209. row7 = vec_sub(row7, row7s);
  210. vec_st(row0, 0, coef_block);
  211. vec_st(row1, 16, coef_block);
  212. vec_st(row2, 32, coef_block);
  213. vec_st(row3, 48, coef_block);
  214. vec_st(row4, 64, coef_block);
  215. vec_st(row5, 80, coef_block);
  216. vec_st(row6, 96, coef_block);
  217. vec_st(row7, 112, coef_block);
  218. }