jdcolext-altivec.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * AltiVec optimizations for libjpeg-turbo
  3. *
  4. * Copyright (C) 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. /* This file is included by jdcolor-altivec.c */
  23. void jsimd_ycc_rgb_convert_altivec (JDIMENSION out_width, JSAMPIMAGE input_buf,
  24. JDIMENSION input_row,
  25. JSAMPARRAY output_buf, int num_rows)
  26. {
  27. JSAMPROW outptr, inptr0, inptr1, inptr2;
  28. int pitch = out_width * RGB_PIXELSIZE, num_cols;
  29. #if __BIG_ENDIAN__
  30. int offset;
  31. #endif
  32. unsigned char __attribute__((aligned(16))) tmpbuf[RGB_PIXELSIZE * 16];
  33. __vector unsigned char rgb0, rgb1, rgb2, rgbx0, rgbx1, rgbx2, rgbx3,
  34. y, cb, cr;
  35. #if __BIG_ENDIAN__
  36. __vector unsigned char edgel, edgeh, edges, out0, out1, out2, out3;
  37. #if RGB_PIXELSIZE == 4
  38. __vector unsigned char out4;
  39. #endif
  40. #endif
  41. #if RGB_PIXELSIZE == 4
  42. __vector unsigned char rgb3;
  43. #endif
  44. __vector short rg0, rg1, rg2, rg3, bx0, bx1, bx2, bx3, yl, yh, cbl, cbh,
  45. crl, crh, rl, rh, gl, gh, bl, bh, g0w, g1w, g2w, g3w;
  46. __vector int g0, g1, g2, g3;
  47. /* Constants
  48. * NOTE: The >> 1 is to compensate for the fact that vec_madds() returns 17
  49. * high-order bits, not 16.
  50. */
  51. __vector short pw_f0402 = { __8X(F_0_402 >> 1) },
  52. pw_mf0228 = { __8X(-F_0_228 >> 1) },
  53. pw_mf0344_f0285 = { __4X2(-F_0_344, F_0_285) },
  54. pw_one = { __8X(1) }, pw_255 = { __8X(255) },
  55. pw_cj = { __8X(CENTERJSAMPLE) };
  56. __vector int pd_onehalf = { __4X(ONE_HALF) };
  57. __vector unsigned char pb_zero = { __16X(0) },
  58. #if __BIG_ENDIAN__
  59. shift_pack_index = {0,1,4,5,8,9,12,13,16,17,20,21,24,25,28,29};
  60. #else
  61. shift_pack_index = {2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31};
  62. #endif
  63. while (--num_rows >= 0) {
  64. inptr0 = input_buf[0][input_row];
  65. inptr1 = input_buf[1][input_row];
  66. inptr2 = input_buf[2][input_row];
  67. input_row++;
  68. outptr = *output_buf++;
  69. for (num_cols = pitch; num_cols > 0;
  70. num_cols -= RGB_PIXELSIZE * 16, outptr += RGB_PIXELSIZE * 16,
  71. inptr0 += 16, inptr1 += 16, inptr2 += 16) {
  72. y = vec_ld(0, inptr0);
  73. /* NOTE: We have to use vec_merge*() here because vec_unpack*() doesn't
  74. * support unsigned vectors.
  75. */
  76. yl = (__vector signed short)VEC_UNPACKHU(y);
  77. yh = (__vector signed short)VEC_UNPACKLU(y);
  78. cb = vec_ld(0, inptr1);
  79. cbl = (__vector signed short)VEC_UNPACKHU(cb);
  80. cbh = (__vector signed short)VEC_UNPACKLU(cb);
  81. cbl = vec_sub(cbl, pw_cj);
  82. cbh = vec_sub(cbh, pw_cj);
  83. cr = vec_ld(0, inptr2);
  84. crl = (__vector signed short)VEC_UNPACKHU(cr);
  85. crh = (__vector signed short)VEC_UNPACKLU(cr);
  86. crl = vec_sub(crl, pw_cj);
  87. crh = vec_sub(crh, pw_cj);
  88. /* (Original)
  89. * R = Y + 1.40200 * Cr
  90. * G = Y - 0.34414 * Cb - 0.71414 * Cr
  91. * B = Y + 1.77200 * Cb
  92. *
  93. * (This implementation)
  94. * R = Y + 0.40200 * Cr + Cr
  95. * G = Y - 0.34414 * Cb + 0.28586 * Cr - Cr
  96. * B = Y - 0.22800 * Cb + Cb + Cb
  97. */
  98. bl = vec_add(cbl, cbl);
  99. bh = vec_add(cbh, cbh);
  100. bl = vec_madds(bl, pw_mf0228, pw_one);
  101. bh = vec_madds(bh, pw_mf0228, pw_one);
  102. bl = vec_sra(bl, (__vector unsigned short)pw_one);
  103. bh = vec_sra(bh, (__vector unsigned short)pw_one);
  104. bl = vec_add(bl, cbl);
  105. bh = vec_add(bh, cbh);
  106. bl = vec_add(bl, cbl);
  107. bh = vec_add(bh, cbh);
  108. bl = vec_add(bl, yl);
  109. bh = vec_add(bh, yh);
  110. rl = vec_add(crl, crl);
  111. rh = vec_add(crh, crh);
  112. rl = vec_madds(rl, pw_f0402, pw_one);
  113. rh = vec_madds(rh, pw_f0402, pw_one);
  114. rl = vec_sra(rl, (__vector unsigned short)pw_one);
  115. rh = vec_sra(rh, (__vector unsigned short)pw_one);
  116. rl = vec_add(rl, crl);
  117. rh = vec_add(rh, crh);
  118. rl = vec_add(rl, yl);
  119. rh = vec_add(rh, yh);
  120. g0w = vec_mergeh(cbl, crl);
  121. g1w = vec_mergel(cbl, crl);
  122. g0 = vec_msums(g0w, pw_mf0344_f0285, pd_onehalf);
  123. g1 = vec_msums(g1w, pw_mf0344_f0285, pd_onehalf);
  124. g2w = vec_mergeh(cbh, crh);
  125. g3w = vec_mergel(cbh, crh);
  126. g2 = vec_msums(g2w, pw_mf0344_f0285, pd_onehalf);
  127. g3 = vec_msums(g3w, pw_mf0344_f0285, pd_onehalf);
  128. /* Clever way to avoid 4 shifts + 2 packs. This packs the high word from
  129. * each dword into a new 16-bit vector, which is the equivalent of
  130. * descaling the 32-bit results (right-shifting by 16 bits) and then
  131. * packing them.
  132. */
  133. gl = vec_perm((__vector short)g0, (__vector short)g1, shift_pack_index);
  134. gh = vec_perm((__vector short)g2, (__vector short)g3, shift_pack_index);
  135. gl = vec_sub(gl, crl);
  136. gh = vec_sub(gh, crh);
  137. gl = vec_add(gl, yl);
  138. gh = vec_add(gh, yh);
  139. rg0 = vec_mergeh(rl, gl);
  140. bx0 = vec_mergeh(bl, pw_255);
  141. rg1 = vec_mergel(rl, gl);
  142. bx1 = vec_mergel(bl, pw_255);
  143. rg2 = vec_mergeh(rh, gh);
  144. bx2 = vec_mergeh(bh, pw_255);
  145. rg3 = vec_mergel(rh, gh);
  146. bx3 = vec_mergel(bh, pw_255);
  147. rgbx0 = vec_packsu(rg0, bx0);
  148. rgbx1 = vec_packsu(rg1, bx1);
  149. rgbx2 = vec_packsu(rg2, bx2);
  150. rgbx3 = vec_packsu(rg3, bx3);
  151. #if RGB_PIXELSIZE == 3
  152. /* rgbx0 = R0 G0 R1 G1 R2 G2 R3 G3 B0 X0 B1 X1 B2 X2 B3 X3
  153. * rgbx1 = R4 G4 R5 G5 R6 G6 R7 G7 B4 X4 B5 X5 B6 X6 B7 X7
  154. * rgbx2 = R8 G8 R9 G9 Ra Ga Rb Gb B8 X8 B9 X9 Ba Xa Bb Xb
  155. * rgbx3 = Rc Gc Rd Gd Re Ge Rf Gf Bc Xc Bd Xd Be Xe Bf Xf
  156. *
  157. * rgb0 = R0 G0 B0 R1 G1 B1 R2 G2 B2 R3 G3 B3 R4 G4 B4 R5
  158. * rgb1 = G5 B5 R6 G6 B6 R7 G7 B7 R8 G8 B8 R9 G9 B9 Ra Ga
  159. * rgb2 = Ba Rb Gb Bb Rc Gc Bc Rd Gd Bd Re Ge Be Rf Gf Bf
  160. */
  161. rgb0 = vec_perm(rgbx0, rgbx1, (__vector unsigned char)RGB_INDEX0);
  162. rgb1 = vec_perm(rgbx1, rgbx2, (__vector unsigned char)RGB_INDEX1);
  163. rgb2 = vec_perm(rgbx2, rgbx3, (__vector unsigned char)RGB_INDEX2);
  164. #else
  165. /* rgbx0 = R0 G0 R1 G1 R2 G2 R3 G3 B0 X0 B1 X1 B2 X2 B3 X3
  166. * rgbx1 = R4 G4 R5 G5 R6 G6 R7 G7 B4 X4 B5 X5 B6 X6 B7 X7
  167. * rgbx2 = R8 G8 R9 G9 Ra Ga Rb Gb B8 X8 B9 X9 Ba Xa Bb Xb
  168. * rgbx3 = Rc Gc Rd Gd Re Ge Rf Gf Bc Xc Bd Xd Be Xe Bf Xf
  169. *
  170. * rgb0 = R0 G0 B0 X0 R1 G1 B1 X1 R2 G2 B2 X2 R3 G3 B3 X3
  171. * rgb1 = R4 G4 B4 X4 R5 G5 B5 X5 R6 G6 B6 X6 R7 G7 B7 X7
  172. * rgb2 = R8 G8 B8 X8 R9 G9 B9 X9 Ra Ga Ba Xa Rb Gb Bb Xb
  173. * rgb3 = Rc Gc Bc Xc Rd Gd Bd Xd Re Ge Be Xe Rf Gf Bf Xf
  174. */
  175. rgb0 = vec_perm(rgbx0, rgbx0, (__vector unsigned char)RGB_INDEX);
  176. rgb1 = vec_perm(rgbx1, rgbx1, (__vector unsigned char)RGB_INDEX);
  177. rgb2 = vec_perm(rgbx2, rgbx2, (__vector unsigned char)RGB_INDEX);
  178. rgb3 = vec_perm(rgbx3, rgbx3, (__vector unsigned char)RGB_INDEX);
  179. #endif
  180. #if __BIG_ENDIAN__
  181. offset = (size_t)outptr & 15;
  182. if (offset) {
  183. __vector unsigned char unaligned_shift_index;
  184. int bytes = num_cols + offset;
  185. if (bytes < (RGB_PIXELSIZE + 1) * 16 && (bytes & 15)) {
  186. /* Slow path to prevent buffer overwrite. Since there is no way to
  187. * write a partial AltiVec register, overwrite would occur on the
  188. * last chunk of the last image row if the right edge is not on a
  189. * 16-byte boundary. It could also occur on other rows if the bytes
  190. * per row is low enough. Since we can't determine whether we're on
  191. * the last image row, we have to assume every row is the last.
  192. */
  193. vec_st(rgb0, 0, tmpbuf);
  194. vec_st(rgb1, 16, tmpbuf);
  195. vec_st(rgb2, 32, tmpbuf);
  196. #if RGB_PIXELSIZE == 4
  197. vec_st(rgb3, 48, tmpbuf);
  198. #endif
  199. memcpy(outptr, tmpbuf, min(num_cols, RGB_PIXELSIZE * 16));
  200. } else {
  201. /* Fast path */
  202. unaligned_shift_index = vec_lvsl(0, outptr);
  203. edgel = vec_ld(0, outptr);
  204. edgeh = vec_ld(min(num_cols - 1, RGB_PIXELSIZE * 16), outptr);
  205. edges = vec_perm(edgeh, edgel, unaligned_shift_index);
  206. unaligned_shift_index = vec_lvsr(0, outptr);
  207. out0 = vec_perm(edges, rgb0, unaligned_shift_index);
  208. out1 = vec_perm(rgb0, rgb1, unaligned_shift_index);
  209. out2 = vec_perm(rgb1, rgb2, unaligned_shift_index);
  210. #if RGB_PIXELSIZE == 4
  211. out3 = vec_perm(rgb2, rgb3, unaligned_shift_index);
  212. out4 = vec_perm(rgb3, edges, unaligned_shift_index);
  213. #else
  214. out3 = vec_perm(rgb2, edges, unaligned_shift_index);
  215. #endif
  216. vec_st(out0, 0, outptr);
  217. if (bytes > 16)
  218. vec_st(out1, 16, outptr);
  219. if (bytes > 32)
  220. vec_st(out2, 32, outptr);
  221. if (bytes > 48)
  222. vec_st(out3, 48, outptr);
  223. #if RGB_PIXELSIZE == 4
  224. if (bytes > 64)
  225. vec_st(out4, 64, outptr);
  226. #endif
  227. }
  228. } else {
  229. #endif /* __BIG_ENDIAN__ */
  230. if (num_cols < RGB_PIXELSIZE * 16 && (num_cols & 15)) {
  231. /* Slow path */
  232. VEC_ST(rgb0, 0, tmpbuf);
  233. VEC_ST(rgb1, 16, tmpbuf);
  234. VEC_ST(rgb2, 32, tmpbuf);
  235. #if RGB_PIXELSIZE == 4
  236. VEC_ST(rgb3, 48, tmpbuf);
  237. #endif
  238. memcpy(outptr, tmpbuf, min(num_cols, RGB_PIXELSIZE * 16));
  239. } else {
  240. /* Fast path */
  241. VEC_ST(rgb0, 0, outptr);
  242. if (num_cols > 16)
  243. VEC_ST(rgb1, 16, outptr);
  244. if (num_cols > 32)
  245. VEC_ST(rgb2, 32, outptr);
  246. #if RGB_PIXELSIZE == 4
  247. if (num_cols > 48)
  248. VEC_ST(rgb3, 48, outptr);
  249. #endif
  250. }
  251. #if __BIG_ENDIAN__
  252. }
  253. #endif
  254. }
  255. }
  256. }