jdsample-mmi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Loongson MMI optimizations for libjpeg-turbo
  3. *
  4. * Copyright (C) 2015, 2018-2019, D. R. Commander. All Rights Reserved.
  5. * Copyright (C) 2016-2018, Loongson Technology Corporation Limited, BeiJing.
  6. * All Rights Reserved.
  7. * Authors: ZhuChen <zhuchen@loongson.cn>
  8. * CaiWanwei <caiwanwei@loongson.cn>
  9. * SunZhangzhi <sunzhangzhi-cq@loongson.cn>
  10. * ZhangLixia <zhanglixia-hf@loongson.cn>
  11. *
  12. * Based on the x86 SIMD extension for IJG JPEG library
  13. * Copyright (C) 1999-2006, MIYASAKA Masaru.
  14. *
  15. * This software is provided 'as-is', without any express or implied
  16. * warranty. In no event will the authors be held liable for any damages
  17. * arising from the use of this software.
  18. *
  19. * Permission is granted to anyone to use this software for any purpose,
  20. * including commercial applications, and to alter it and redistribute it
  21. * freely, subject to the following restrictions:
  22. *
  23. * 1. The origin of this software must not be misrepresented; you must not
  24. * claim that you wrote the original software. If you use this software
  25. * in a product, an acknowledgment in the product documentation would be
  26. * appreciated but is not required.
  27. * 2. Altered source versions must be plainly marked as such, and must not be
  28. * misrepresented as being the original software.
  29. * 3. This notice may not be removed or altered from any source distribution.
  30. */
  31. /* CHROMA UPSAMPLING */
  32. #include "jsimd_mmi.h"
  33. enum const_index {
  34. index_PW_ONE,
  35. index_PW_TWO,
  36. index_PW_THREE,
  37. index_PW_SEVEN,
  38. index_PW_EIGHT,
  39. };
  40. static uint64_t const_value[] = {
  41. _uint64_set_pi16(1, 1, 1, 1),
  42. _uint64_set_pi16(2, 2, 2, 2),
  43. _uint64_set_pi16(3, 3, 3, 3),
  44. _uint64_set_pi16(7, 7, 7, 7),
  45. _uint64_set_pi16(8, 8, 8, 8),
  46. };
  47. #define PW_ONE get_const_value(index_PW_ONE)
  48. #define PW_TWO get_const_value(index_PW_TWO)
  49. #define PW_THREE get_const_value(index_PW_THREE)
  50. #define PW_SEVEN get_const_value(index_PW_SEVEN)
  51. #define PW_EIGHT get_const_value(index_PW_EIGHT)
  52. #define PROCESS_ROW(row, wkoffset, bias1, bias2, shift) { \
  53. __m64 samp123X, samp3XXX, samp1234, sampX012, samp_1012; \
  54. __m64 sampXXX4, sampX456, samp3456, samp567X, samp7XXX, samp5678; \
  55. __m64 outle, outhe, outlo, outho, outl, outh; \
  56. \
  57. samp123X = _mm_srli_si64(samp0123, 2 * BYTE_BIT); /* ( 1 2 3 -) */ \
  58. sampXXX4 = _mm_slli_si64(samp4567, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* ( - - - 4) */ \
  59. samp3XXX = _mm_srli_si64(samp0123, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* ( 3 - - -) */ \
  60. sampX456 = _mm_slli_si64(samp4567, 2 * BYTE_BIT); /* ( - 4 5 6) */ \
  61. \
  62. samp1234 = _mm_or_si64(samp123X, sampXXX4); /* ( 1 2 3 4) */ \
  63. samp3456 = _mm_or_si64(samp3XXX, sampX456); /* ( 3 4 5 6) */ \
  64. \
  65. sampX012 = _mm_slli_si64(samp0123, 2 * BYTE_BIT); /* ( - 0 1 2) */ \
  66. samp567X = _mm_srli_si64(samp4567, 2 * BYTE_BIT); /* ( 5 6 7 -) */ \
  67. samp7XXX = _mm_srli_si64(samp4567, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* ( 7 - - -) */ \
  68. \
  69. samp_1012 = _mm_or_si64(sampX012, wk[row]); /* (-1 0 1 2) */ \
  70. samp5678 = _mm_or_si64(samp567X, wk[row + wkoffset]); /* ( 5 6 7 8) */ \
  71. \
  72. wk[row] = samp7XXX; \
  73. \
  74. samp0123 = _mm_mullo_pi16(samp0123, PW_THREE); \
  75. samp4567 = _mm_mullo_pi16(samp4567, PW_THREE); \
  76. samp_1012 = _mm_add_pi16(samp_1012, bias1); \
  77. samp3456 = _mm_add_pi16(samp3456, bias1); \
  78. samp1234 = _mm_add_pi16(samp1234, bias2); \
  79. samp5678 = _mm_add_pi16(samp5678, bias2); \
  80. \
  81. outle = _mm_add_pi16(samp_1012, samp0123); \
  82. outhe = _mm_add_pi16(samp3456, samp4567); \
  83. outle = _mm_srli_pi16(outle, shift); /* ( 0 2 4 6) */ \
  84. outhe = _mm_srli_pi16(outhe, shift); /* ( 8 10 12 14) */ \
  85. outlo = _mm_add_pi16(samp1234, samp0123); \
  86. outho = _mm_add_pi16(samp5678, samp4567); \
  87. outlo = _mm_srli_pi16(outlo, shift); /* ( 1 3 5 7) */ \
  88. outho = _mm_srli_pi16(outho, shift); /* ( 9 11 13 15) */ \
  89. \
  90. outlo = _mm_slli_pi16(outlo, BYTE_BIT); \
  91. outho = _mm_slli_pi16(outho, BYTE_BIT); \
  92. outl = _mm_or_si64(outle, outlo); /* ( 0 1 2 3 4 5 6 7) */ \
  93. outh = _mm_or_si64(outhe, outho); /* ( 8 9 10 11 12 13 14 15) */ \
  94. \
  95. _mm_store_si64((__m64 *)outptr##row, outl); \
  96. _mm_store_si64((__m64 *)outptr##row + 1, outh); \
  97. }
  98. void jsimd_h2v2_fancy_upsample_mmi(int max_v_samp_factor,
  99. JDIMENSION downsampled_width,
  100. JSAMPARRAY input_data,
  101. JSAMPARRAY *output_data_ptr)
  102. {
  103. JSAMPARRAY output_data = *output_data_ptr;
  104. JSAMPROW inptr_1, inptr0, inptr1, outptr0, outptr1;
  105. int inrow, outrow, incol, tmp, tmp1;
  106. __m64 this_1l, this_1h, this_1, thiscolsum_1l, thiscolsum_1h;
  107. __m64 this0l, this0h, this0;
  108. __m64 this1l, this1h, this1, thiscolsum1l, thiscolsum1h;
  109. __m64 next_1l, next_1h, next_1, nextcolsum_1l, nextcolsum_1h;
  110. __m64 next0l, next0h, next0;
  111. __m64 next1l, next1h, next1, nextcolsum1l, nextcolsum1h;
  112. __m64 mask0 = 0.0, masklast, samp0123, samp4567, wk[4], zero = 0.0;
  113. mask0 = _mm_cmpeq_pi8(mask0, mask0);
  114. masklast = _mm_slli_si64(mask0, (SIZEOF_MMWORD - 2) * BYTE_BIT);
  115. mask0 = _mm_srli_si64(mask0, (SIZEOF_MMWORD - 2) * BYTE_BIT);
  116. for (inrow = 0, outrow = 0; outrow < max_v_samp_factor; inrow++) {
  117. inptr_1 = input_data[inrow - 1];
  118. inptr0 = input_data[inrow];
  119. inptr1 = input_data[inrow + 1];
  120. outptr0 = output_data[outrow++];
  121. outptr1 = output_data[outrow++];
  122. if (downsampled_width & 7) {
  123. tmp = (downsampled_width - 1) * sizeof(JSAMPLE);
  124. tmp1 = downsampled_width * sizeof(JSAMPLE);
  125. asm(PTR_ADDU "$8, %3, %6\r\n"
  126. "lb $9, ($8)\r\n"
  127. PTR_ADDU "$8, %3, %7\r\n"
  128. "sb $9, ($8)\r\n"
  129. PTR_ADDU "$8, %4, %6\r\n"
  130. "lb $9, ($8)\r\n"
  131. PTR_ADDU "$8, %4, %7\r\n"
  132. "sb $9, ($8)\r\n"
  133. PTR_ADDU "$8, %5, %6\r\n"
  134. "lb $9, ($8)\r\n"
  135. PTR_ADDU "$8, %5, %7\r\n"
  136. "sb $9, ($8)\r\n"
  137. : "=m" (*inptr_1), "=m" (*inptr0), "=m" (*inptr1)
  138. : "r" (inptr_1), "r" (inptr0), "r" (inptr1), "r" (tmp), "r" (tmp1)
  139. : "$8", "$9"
  140. );
  141. }
  142. /* process the first column block */
  143. this0 = _mm_load_si64((__m64 *)inptr0); /* row[ 0][0] */
  144. this_1 = _mm_load_si64((__m64 *)inptr_1); /* row[-1][0] */
  145. this1 = _mm_load_si64((__m64 *)inptr1); /* row[ 1][0] */
  146. this0l = _mm_unpacklo_pi8(this0, zero); /* row[ 0][0]( 0 1 2 3) */
  147. this0h = _mm_unpackhi_pi8(this0, zero); /* row[ 0][0]( 4 5 6 7) */
  148. this_1l = _mm_unpacklo_pi8(this_1, zero); /* row[-1][0]( 0 1 2 3) */
  149. this_1h = _mm_unpackhi_pi8(this_1, zero); /* row[-1][0]( 4 5 6 7) */
  150. this1l = _mm_unpacklo_pi8(this1, zero); /* row[+1][0]( 0 1 2 3) */
  151. this1h = _mm_unpackhi_pi8(this1, zero); /* row[+1][0]( 4 5 6 7) */
  152. this0l = _mm_mullo_pi16(this0l, PW_THREE);
  153. this0h = _mm_mullo_pi16(this0h, PW_THREE);
  154. thiscolsum_1l = _mm_add_pi16(this_1l, this0l); /* ( 0 1 2 3) */
  155. thiscolsum_1h = _mm_add_pi16(this_1h, this0h); /* ( 4 5 6 7) */
  156. thiscolsum1l = _mm_add_pi16(this0l, this1l); /* ( 0 1 2 3) */
  157. thiscolsum1h = _mm_add_pi16(this0h, this1h); /* ( 4 5 6 7) */
  158. /* temporarily save the intermediate data */
  159. _mm_store_si64((__m64 *)outptr0, thiscolsum_1l);
  160. _mm_store_si64((__m64 *)outptr0 + 1, thiscolsum_1h);
  161. _mm_store_si64((__m64 *)outptr1, thiscolsum1l);
  162. _mm_store_si64((__m64 *)outptr1 + 1, thiscolsum1h);
  163. wk[0] = _mm_and_si64(thiscolsum_1l, mask0); /* ( 0 - - -) */
  164. wk[1] = _mm_and_si64(thiscolsum1l, mask0); /* ( 0 - - -) */
  165. for (incol = downsampled_width; incol > 0;
  166. incol -= 8, inptr_1 += 8, inptr0 += 8, inptr1 += 8,
  167. outptr0 += 16, outptr1 += 16) {
  168. if (incol > 8) {
  169. /* process the next column block */
  170. next0 = _mm_load_si64((__m64 *)inptr0 + 1); /* row[ 0][1] */
  171. next_1 = _mm_load_si64((__m64 *)inptr_1 + 1); /* row[-1][1] */
  172. next1 = _mm_load_si64((__m64 *)inptr1 + 1); /* row[+1][1] */
  173. next0l = _mm_unpacklo_pi8(next0, zero); /* row[ 0][1]( 0 1 2 3) */
  174. next0h = _mm_unpackhi_pi8(next0, zero); /* row[ 0][1]( 4 5 6 7) */
  175. next_1l = _mm_unpacklo_pi8(next_1, zero); /* row[-1][1]( 0 1 2 3) */
  176. next_1h = _mm_unpackhi_pi8(next_1, zero); /* row[-1][1]( 4 5 6 7) */
  177. next1l = _mm_unpacklo_pi8(next1, zero); /* row[+1][1]( 0 1 2 3) */
  178. next1h = _mm_unpackhi_pi8(next1, zero); /* row[+1][1]( 4 5 6 7) */
  179. next0l = _mm_mullo_pi16(next0l, PW_THREE);
  180. next0h = _mm_mullo_pi16(next0h, PW_THREE);
  181. nextcolsum_1l = _mm_add_pi16(next_1l, next0l); /* ( 0 1 2 3) */
  182. nextcolsum_1h = _mm_add_pi16(next_1h, next0h); /* ( 4 5 6 7) */
  183. nextcolsum1l = _mm_add_pi16(next0l, next1l); /* ( 0 1 2 3) */
  184. nextcolsum1h = _mm_add_pi16(next0h, next1h); /* ( 4 5 6 7) */
  185. /* temporarily save the intermediate data */
  186. _mm_store_si64((__m64 *)outptr0 + 2, nextcolsum_1l);
  187. _mm_store_si64((__m64 *)outptr0 + 3, nextcolsum_1h);
  188. _mm_store_si64((__m64 *)outptr1 + 2, nextcolsum1l);
  189. _mm_store_si64((__m64 *)outptr1 + 3, nextcolsum1h);
  190. wk[2] = _mm_slli_si64(nextcolsum_1l, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* ( - - - 0) */
  191. wk[3] = _mm_slli_si64(nextcolsum1l, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* ( - - - 0) */
  192. } else {
  193. __m64 tmp;
  194. /* process the last column block */
  195. tmp = _mm_load_si64((__m64 *)outptr0 + 1);
  196. wk[2] = _mm_and_si64(masklast, tmp); /* ( - - - 7) */
  197. tmp = _mm_load_si64((__m64 *)outptr1 + 1);
  198. wk[3] = _mm_and_si64(masklast, tmp); /* ( - - - 7) */
  199. }
  200. /* process the upper row */
  201. samp0123 = _mm_load_si64((__m64 *)outptr0); /* ( 0 1 2 3) */ \
  202. samp4567 = _mm_load_si64((__m64 *)outptr0 + 1); /* ( 4 5 6 7) */ \
  203. PROCESS_ROW(0, 2, PW_EIGHT, PW_SEVEN, 4)
  204. /* process the lower row */
  205. samp0123 = _mm_load_si64((__m64 *)outptr1); /* ( 0 1 2 3) */ \
  206. samp4567 = _mm_load_si64((__m64 *)outptr1 + 1); /* ( 4 5 6 7) */ \
  207. PROCESS_ROW(1, 2, PW_EIGHT, PW_SEVEN, 4)
  208. }
  209. }
  210. }
  211. void jsimd_h2v1_fancy_upsample_mmi(int max_v_samp_factor,
  212. JDIMENSION downsampled_width,
  213. JSAMPARRAY input_data,
  214. JSAMPARRAY *output_data_ptr)
  215. {
  216. JSAMPARRAY output_data = *output_data_ptr;
  217. JSAMPROW inptr0, outptr0;
  218. int inrow, incol, tmp, tmp1;
  219. __m64 thisl, this, nextl, next;
  220. __m64 mask0 = 0.0, masklast, samp0123, samp4567, wk[2], zero = 0.0;
  221. mask0 = _mm_cmpeq_pi8(mask0, mask0);
  222. masklast = _mm_slli_si64(mask0, (SIZEOF_MMWORD - 2) * BYTE_BIT);
  223. mask0 = _mm_srli_si64(mask0, (SIZEOF_MMWORD - 2) * BYTE_BIT);
  224. for (inrow = 0; inrow < max_v_samp_factor; inrow++) {
  225. inptr0 = input_data[inrow];
  226. outptr0 = output_data[inrow];
  227. if (downsampled_width & 7) {
  228. tmp = (downsampled_width - 1) * sizeof(JSAMPLE);
  229. tmp1 = downsampled_width * sizeof(JSAMPLE);
  230. asm(PTR_ADDU "$8, %1, %2\r\n"
  231. "lb $9, ($8)\r\n"
  232. PTR_ADDU "$8, %1, %3\r\n"
  233. "sb $9, ($8)\r\n"
  234. : "=m" (*inptr0)
  235. : "r" (inptr0), "r" (tmp), "r" (tmp1)
  236. : "$8", "$9"
  237. );
  238. }
  239. /* process the first column block */
  240. this = _mm_load_si64((__m64 *)inptr0); /* row[ 0][0] */
  241. thisl = _mm_unpacklo_pi8(this, zero); /* row[ 0][0]( 0 1 2 3) */
  242. wk[0] = _mm_and_si64(thisl, mask0); /* ( 0 - - -) */
  243. for (incol = downsampled_width; incol > 0;
  244. incol -= 8, inptr0 += 8, outptr0 += 16) {
  245. if (incol > 8) {
  246. /* process the next column block */
  247. next = _mm_load_si64((__m64 *)inptr0 + 1); /* row[ 0][1] */
  248. nextl = _mm_unpacklo_pi8(next, zero); /* row[ 0][1]( 0 1 2 3) */
  249. wk[1] = _mm_slli_si64(nextl, (SIZEOF_MMWORD - 2) * BYTE_BIT); /* ( - - - 0) */
  250. } else {
  251. __m64 thish;
  252. /* process the last column block */
  253. this = _mm_load_si64((__m64 *)inptr0); /* row[ 0][0] */
  254. thish = _mm_unpackhi_pi8(this, zero); /* row[ 0][1]( 4 5 6 7) */
  255. wk[1] = _mm_and_si64(masklast, thish); /* ( - - - 7) */
  256. }
  257. /* process the row */
  258. this = _mm_load_si64((__m64 *)inptr0); /* row[ 0][0] */
  259. samp0123 = _mm_unpacklo_pi8(this, zero); /* ( 0 1 2 3) */
  260. samp4567 = _mm_unpackhi_pi8(this, zero); /* ( 4 5 6 7) */
  261. PROCESS_ROW(0, 1, PW_ONE, PW_TWO, 2)
  262. }
  263. }
  264. }