jcgryext-altivec.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * AltiVec optimizations for libjpeg-turbo
  3. *
  4. * Copyright (C) 2014-2015, D. R. Commander. All Rights Reserved.
  5. * Copyright (C) 2014, Jay Foad. All Rights Reserved.
  6. *
  7. * This software is provided 'as-is', without any express or implied
  8. * warranty. In no event will the authors be held liable for any damages
  9. * arising from the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software. If you use this software
  17. * in a product, an acknowledgment in the product documentation would be
  18. * appreciated but is not required.
  19. * 2. Altered source versions must be plainly marked as such, and must not be
  20. * misrepresented as being the original software.
  21. * 3. This notice may not be removed or altered from any source distribution.
  22. */
  23. /* This file is included by jcgray-altivec.c */
  24. void jsimd_rgb_gray_convert_altivec(JDIMENSION img_width, JSAMPARRAY input_buf,
  25. JSAMPIMAGE output_buf,
  26. JDIMENSION output_row, int num_rows)
  27. {
  28. JSAMPROW inptr, outptr;
  29. int pitch = img_width * RGB_PIXELSIZE, num_cols;
  30. #if __BIG_ENDIAN__
  31. int offset;
  32. unsigned char __attribute__((aligned(16))) tmpbuf[RGB_PIXELSIZE * 16];
  33. #endif
  34. __vector unsigned char rgb0, rgb1 = { 0 }, rgb2 = { 0 },
  35. rgbg0, rgbg1, rgbg2, rgbg3, y;
  36. #if __BIG_ENDIAN__ || RGB_PIXELSIZE == 4
  37. __vector unsigned char rgb3 = { 0 };
  38. #endif
  39. #if __BIG_ENDIAN__ && RGB_PIXELSIZE == 4
  40. __vector unsigned char rgb4 = { 0 };
  41. #endif
  42. __vector short rg0, rg1, rg2, rg3, bg0, bg1, bg2, bg3;
  43. __vector unsigned short yl, yh;
  44. __vector int y0, y1, y2, y3;
  45. /* Constants */
  46. __vector short pw_f0299_f0337 = { __4X2(F_0_299, F_0_337) },
  47. pw_f0114_f0250 = { __4X2(F_0_114, F_0_250) };
  48. __vector int pd_onehalf = { __4X(ONE_HALF) };
  49. __vector unsigned char pb_zero = { __16X(0) },
  50. #if __BIG_ENDIAN__
  51. shift_pack_index =
  52. { 0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29 };
  53. #else
  54. shift_pack_index =
  55. { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 };
  56. #endif
  57. while (--num_rows >= 0) {
  58. inptr = *input_buf++;
  59. outptr = output_buf[0][output_row];
  60. output_row++;
  61. for (num_cols = pitch; num_cols > 0;
  62. num_cols -= RGB_PIXELSIZE * 16, inptr += RGB_PIXELSIZE * 16,
  63. outptr += 16) {
  64. #if __BIG_ENDIAN__
  65. /* Load 16 pixels == 48 or 64 bytes */
  66. offset = (size_t)inptr & 15;
  67. if (offset) {
  68. __vector unsigned char unaligned_shift_index;
  69. int bytes = num_cols + offset;
  70. if (bytes < (RGB_PIXELSIZE + 1) * 16 && (bytes & 15)) {
  71. /* Slow path to prevent buffer overread. Since there is no way to
  72. * read a partial AltiVec register, overread would occur on the last
  73. * chunk of the last image row if the right edge is not on a 16-byte
  74. * boundary. It could also occur on other rows if the bytes per row
  75. * is low enough. Since we can't determine whether we're on the last
  76. * image row, we have to assume every row is the last.
  77. */
  78. memcpy(tmpbuf, inptr, min(num_cols, RGB_PIXELSIZE * 16));
  79. rgb0 = vec_ld(0, tmpbuf);
  80. rgb1 = vec_ld(16, tmpbuf);
  81. rgb2 = vec_ld(32, tmpbuf);
  82. #if RGB_PIXELSIZE == 4
  83. rgb3 = vec_ld(48, tmpbuf);
  84. #endif
  85. } else {
  86. /* Fast path */
  87. rgb0 = vec_ld(0, inptr);
  88. if (bytes > 16)
  89. rgb1 = vec_ld(16, inptr);
  90. if (bytes > 32)
  91. rgb2 = vec_ld(32, inptr);
  92. if (bytes > 48)
  93. rgb3 = vec_ld(48, inptr);
  94. #if RGB_PIXELSIZE == 4
  95. if (bytes > 64)
  96. rgb4 = vec_ld(64, inptr);
  97. #endif
  98. unaligned_shift_index = vec_lvsl(0, inptr);
  99. rgb0 = vec_perm(rgb0, rgb1, unaligned_shift_index);
  100. rgb1 = vec_perm(rgb1, rgb2, unaligned_shift_index);
  101. rgb2 = vec_perm(rgb2, rgb3, unaligned_shift_index);
  102. #if RGB_PIXELSIZE == 4
  103. rgb3 = vec_perm(rgb3, rgb4, unaligned_shift_index);
  104. #endif
  105. }
  106. } else {
  107. if (num_cols < RGB_PIXELSIZE * 16 && (num_cols & 15)) {
  108. /* Slow path */
  109. memcpy(tmpbuf, inptr, min(num_cols, RGB_PIXELSIZE * 16));
  110. rgb0 = vec_ld(0, tmpbuf);
  111. rgb1 = vec_ld(16, tmpbuf);
  112. rgb2 = vec_ld(32, tmpbuf);
  113. #if RGB_PIXELSIZE == 4
  114. rgb3 = vec_ld(48, tmpbuf);
  115. #endif
  116. } else {
  117. /* Fast path */
  118. rgb0 = vec_ld(0, inptr);
  119. if (num_cols > 16)
  120. rgb1 = vec_ld(16, inptr);
  121. if (num_cols > 32)
  122. rgb2 = vec_ld(32, inptr);
  123. #if RGB_PIXELSIZE == 4
  124. if (num_cols > 48)
  125. rgb3 = vec_ld(48, inptr);
  126. #endif
  127. }
  128. }
  129. #else
  130. /* Little endian */
  131. rgb0 = vec_vsx_ld(0, inptr);
  132. if (num_cols > 16)
  133. rgb1 = vec_vsx_ld(16, inptr);
  134. if (num_cols > 32)
  135. rgb2 = vec_vsx_ld(32, inptr);
  136. #if RGB_PIXELSIZE == 4
  137. if (num_cols > 48)
  138. rgb3 = vec_vsx_ld(48, inptr);
  139. #endif
  140. #endif
  141. #if RGB_PIXELSIZE == 3
  142. /* rgb0 = R0 G0 B0 R1 G1 B1 R2 G2 B2 R3 G3 B3 R4 G4 B4 R5
  143. * rgb1 = G5 B5 R6 G6 B6 R7 G7 B7 R8 G8 B8 R9 G9 B9 Ra Ga
  144. * rgb2 = Ba Rb Gb Bb Rc Gc Bc Rd Gd Bd Re Ge Be Rf Gf Bf
  145. *
  146. * rgbg0 = R0 G0 R1 G1 R2 G2 R3 G3 B0 G0 B1 G1 B2 G2 B3 G3
  147. * rgbg1 = R4 G4 R5 G5 R6 G6 R7 G7 B4 G4 B5 G5 B6 G6 B7 G7
  148. * rgbg2 = R8 G8 R9 G9 Ra Ga Rb Gb B8 G8 B9 G9 Ba Ga Bb Gb
  149. * rgbg3 = Rc Gc Rd Gd Re Ge Rf Gf Bc Gc Bd Gd Be Ge Bf Gf
  150. */
  151. rgbg0 = vec_perm(rgb0, rgb0, (__vector unsigned char)RGBG_INDEX0);
  152. rgbg1 = vec_perm(rgb0, rgb1, (__vector unsigned char)RGBG_INDEX1);
  153. rgbg2 = vec_perm(rgb1, rgb2, (__vector unsigned char)RGBG_INDEX2);
  154. rgbg3 = vec_perm(rgb2, rgb2, (__vector unsigned char)RGBG_INDEX3);
  155. #else
  156. /* rgb0 = R0 G0 B0 X0 R1 G1 B1 X1 R2 G2 B2 X2 R3 G3 B3 X3
  157. * rgb1 = R4 G4 B4 X4 R5 G5 B5 X5 R6 G6 B6 X6 R7 G7 B7 X7
  158. * rgb2 = R8 G8 B8 X8 R9 G9 B9 X9 Ra Ga Ba Xa Rb Gb Bb Xb
  159. * rgb3 = Rc Gc Bc Xc Rd Gd Bd Xd Re Ge Be Xe Rf Gf Bf Xf
  160. *
  161. * rgbg0 = R0 G0 R1 G1 R2 G2 R3 G3 B0 G0 B1 G1 B2 G2 B3 G3
  162. * rgbg1 = R4 G4 R5 G5 R6 G6 R7 G7 B4 G4 B5 G5 B6 G6 B7 G7
  163. * rgbg2 = R8 G8 R9 G9 Ra Ga Rb Gb B8 G8 B9 G9 Ba Ga Bb Gb
  164. * rgbg3 = Rc Gc Rd Gd Re Ge Rf Gf Bc Gc Bd Gd Be Ge Bf Gf
  165. */
  166. rgbg0 = vec_perm(rgb0, rgb0, (__vector unsigned char)RGBG_INDEX);
  167. rgbg1 = vec_perm(rgb1, rgb1, (__vector unsigned char)RGBG_INDEX);
  168. rgbg2 = vec_perm(rgb2, rgb2, (__vector unsigned char)RGBG_INDEX);
  169. rgbg3 = vec_perm(rgb3, rgb3, (__vector unsigned char)RGBG_INDEX);
  170. #endif
  171. /* rg0 = R0 G0 R1 G1 R2 G2 R3 G3
  172. * bg0 = B0 G0 B1 G1 B2 G2 B3 G3
  173. * ...
  174. *
  175. * NOTE: We have to use vec_merge*() here because vec_unpack*() doesn't
  176. * support unsigned vectors.
  177. */
  178. rg0 = (__vector signed short)VEC_UNPACKHU(rgbg0);
  179. bg0 = (__vector signed short)VEC_UNPACKLU(rgbg0);
  180. rg1 = (__vector signed short)VEC_UNPACKHU(rgbg1);
  181. bg1 = (__vector signed short)VEC_UNPACKLU(rgbg1);
  182. rg2 = (__vector signed short)VEC_UNPACKHU(rgbg2);
  183. bg2 = (__vector signed short)VEC_UNPACKLU(rgbg2);
  184. rg3 = (__vector signed short)VEC_UNPACKHU(rgbg3);
  185. bg3 = (__vector signed short)VEC_UNPACKLU(rgbg3);
  186. /* (Original)
  187. * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
  188. *
  189. * (This implementation)
  190. * Y = 0.29900 * R + 0.33700 * G + 0.11400 * B + 0.25000 * G
  191. */
  192. /* Calculate Y values */
  193. y0 = vec_msums(rg0, pw_f0299_f0337, pd_onehalf);
  194. y1 = vec_msums(rg1, pw_f0299_f0337, pd_onehalf);
  195. y2 = vec_msums(rg2, pw_f0299_f0337, pd_onehalf);
  196. y3 = vec_msums(rg3, pw_f0299_f0337, pd_onehalf);
  197. y0 = vec_msums(bg0, pw_f0114_f0250, y0);
  198. y1 = vec_msums(bg1, pw_f0114_f0250, y1);
  199. y2 = vec_msums(bg2, pw_f0114_f0250, y2);
  200. y3 = vec_msums(bg3, pw_f0114_f0250, y3);
  201. /* Clever way to avoid 4 shifts + 2 packs. This packs the high word from
  202. * each dword into a new 16-bit vector, which is the equivalent of
  203. * descaling the 32-bit results (right-shifting by 16 bits) and then
  204. * packing them.
  205. */
  206. yl = vec_perm((__vector unsigned short)y0, (__vector unsigned short)y1,
  207. shift_pack_index);
  208. yh = vec_perm((__vector unsigned short)y2, (__vector unsigned short)y3,
  209. shift_pack_index);
  210. y = vec_pack(yl, yh);
  211. vec_st(y, 0, outptr);
  212. }
  213. }
  214. }