jdcolext-altivec.c 10 KB

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