jfdctfst-mmi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Loongson MMI optimizations for libjpeg-turbo
  3. *
  4. * Copyright (C) 2014, 2018-2019, D. R. Commander. All Rights Reserved.
  5. * Copyright (C) 2016-2018, Loongson Technology Corporation Limited, BeiJing.
  6. * All Rights Reserved.
  7. * Authors: LiuQingfa <liuqingfa-hf@loongson.cn>
  8. *
  9. * Based on the x86 SIMD extension for IJG JPEG library
  10. * Copyright (C) 1999-2006, MIYASAKA Masaru.
  11. *
  12. * This software is provided 'as-is', without any express or implied
  13. * warranty. In no event will the authors be held liable for any damages
  14. * arising from the use of this software.
  15. *
  16. * Permission is granted to anyone to use this software for any purpose,
  17. * including commercial applications, and to alter it and redistribute it
  18. * freely, subject to the following restrictions:
  19. *
  20. * 1. The origin of this software must not be misrepresented; you must not
  21. * claim that you wrote the original software. If you use this software
  22. * in a product, an acknowledgment in the product documentation would be
  23. * appreciated but is not required.
  24. * 2. Altered source versions must be plainly marked as such, and must not be
  25. * misrepresented as being the original software.
  26. * 3. This notice may not be removed or altered from any source distribution.
  27. */
  28. /* FAST INTEGER FORWARD DCT */
  29. #include "jsimd_mmi.h"
  30. #define CONST_BITS 8
  31. #define F_0_382 ((short)98) /* FIX(0.382683433) */
  32. #define F_0_541 ((short)139) /* FIX(0.541196100) */
  33. #define F_0_707 ((short)181) /* FIX(0.707106781) */
  34. #define F_1_306 ((short)334) /* FIX(1.306562965) */
  35. #define PRE_MULTIPLY_SCALE_BITS 2
  36. #define CONST_SHIFT (16 - PRE_MULTIPLY_SCALE_BITS - CONST_BITS)
  37. enum const_index {
  38. index_PW_F0707,
  39. index_PW_F0382,
  40. index_PW_F0541,
  41. index_PW_F1306
  42. };
  43. static uint64_t const_value[] = {
  44. _uint64_set1_pi16(F_0_707),
  45. _uint64_set1_pi16(F_0_382),
  46. _uint64_set1_pi16(F_0_541),
  47. _uint64_set1_pi16(F_1_306)
  48. };
  49. #define PW_F0707 get_const_value(index_PW_F0707)
  50. #define PW_F0382 get_const_value(index_PW_F0382)
  51. #define PW_F0541 get_const_value(index_PW_F0541)
  52. #define PW_F1306 get_const_value(index_PW_F1306)
  53. #define DO_FDCT_MULTIPLY(out, in, multiplier) { \
  54. __m64 mulhi, mullo, mul12, mul34; \
  55. \
  56. mullo = _mm_mullo_pi16(in, multiplier); \
  57. mulhi = _mm_mulhi_pi16(in, multiplier); \
  58. mul12 = _mm_unpacklo_pi16(mullo, mulhi); \
  59. mul34 = _mm_unpackhi_pi16(mullo, mulhi); \
  60. mul12 = _mm_srai_pi32(mul12, CONST_BITS); \
  61. mul34 = _mm_srai_pi32(mul34, CONST_BITS); \
  62. out = _mm_packs_pi32(mul12, mul34); \
  63. }
  64. #define DO_FDCT_COMMON() { \
  65. \
  66. /* Even part */ \
  67. \
  68. tmp10 = _mm_add_pi16(tmp0, tmp3); \
  69. tmp13 = _mm_sub_pi16(tmp0, tmp3); \
  70. tmp11 = _mm_add_pi16(tmp1, tmp2); \
  71. tmp12 = _mm_sub_pi16(tmp1, tmp2); \
  72. \
  73. out0 = _mm_add_pi16(tmp10, tmp11); \
  74. out4 = _mm_sub_pi16(tmp10, tmp11); \
  75. \
  76. z1 = _mm_add_pi16(tmp12, tmp13); \
  77. DO_FDCT_MULTIPLY(z1, z1, PW_F0707) \
  78. \
  79. out2 = _mm_add_pi16(tmp13, z1); \
  80. out6 = _mm_sub_pi16(tmp13, z1); \
  81. \
  82. /* Odd part */ \
  83. \
  84. tmp10 = _mm_add_pi16(tmp4, tmp5); \
  85. tmp11 = _mm_add_pi16(tmp5, tmp6); \
  86. tmp12 = _mm_add_pi16(tmp6, tmp7); \
  87. \
  88. z5 = _mm_sub_pi16(tmp10, tmp12); \
  89. DO_FDCT_MULTIPLY(z5, z5, PW_F0382) \
  90. \
  91. DO_FDCT_MULTIPLY(z2, tmp10, PW_F0541) \
  92. z2 = _mm_add_pi16(z2, z5); \
  93. \
  94. DO_FDCT_MULTIPLY(z4, tmp12, PW_F1306) \
  95. z4 = _mm_add_pi16(z4, z5); \
  96. \
  97. DO_FDCT_MULTIPLY(z3, tmp11, PW_F0707) \
  98. \
  99. z11 = _mm_add_pi16(tmp7, z3); \
  100. z13 = _mm_sub_pi16(tmp7, z3); \
  101. \
  102. out5 = _mm_add_pi16(z13, z2); \
  103. out3 = _mm_sub_pi16(z13, z2); \
  104. out1 = _mm_add_pi16(z11, z4); \
  105. out7 = _mm_sub_pi16(z11, z4); \
  106. }
  107. #define DO_FDCT_PASS1() { \
  108. __m64 row0l, row0h, row1l, row1h, row2l, row2h, row3l, row3h; \
  109. __m64 row01a, row01b, row01c, row01d, row23a, row23b, row23c, row23d; \
  110. __m64 col0, col1, col2, col3, col4, col5, col6, col7; \
  111. \
  112. row0l = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 0]); /* (00 01 02 03) */ \
  113. row0h = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 0 + 4]); /* (04 05 06 07) */ \
  114. row1l = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 1]); /* (10 11 12 13) */ \
  115. row1h = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 1 + 4]); /* (14 15 16 17) */ \
  116. row2l = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 2]); /* (20 21 22 23) */ \
  117. row2h = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 2 + 4]); /* (24 25 26 27) */ \
  118. row3l = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 3]); /* (30 31 32 33) */ \
  119. row3h = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 3 + 4]); /* (34 35 36 37) */ \
  120. \
  121. /* Transpose coefficients */ \
  122. \
  123. row23a = _mm_unpacklo_pi16(row2l, row3l); /* row23a=(20 30 21 31) */ \
  124. row23b = _mm_unpackhi_pi16(row2l, row3l); /* row23b=(22 32 23 33) */ \
  125. row23c = _mm_unpacklo_pi16(row2h, row3h); /* row23c=(24 34 25 35) */ \
  126. row23d = _mm_unpackhi_pi16(row2h, row3h); /* row23d=(26 36 27 37) */ \
  127. \
  128. row01a = _mm_unpacklo_pi16(row0l, row1l); /* row01a=(00 10 01 11) */ \
  129. row01b = _mm_unpackhi_pi16(row0l, row1l); /* row01b=(02 12 03 13) */ \
  130. row01c = _mm_unpacklo_pi16(row0h, row1h); /* row01c=(04 14 05 15) */ \
  131. row01d = _mm_unpackhi_pi16(row0h, row1h); /* row01d=(06 16 07 17) */ \
  132. \
  133. col0 = _mm_unpacklo_pi32(row01a, row23a); /* col0=(00 10 20 30) */ \
  134. col1 = _mm_unpackhi_pi32(row01a, row23a); /* col1=(01 11 21 31) */ \
  135. col6 = _mm_unpacklo_pi32(row01d, row23d); /* col6=(06 16 26 36) */ \
  136. col7 = _mm_unpackhi_pi32(row01d, row23d); /* col7=(07 17 27 37) */ \
  137. \
  138. tmp6 = _mm_sub_pi16(col1, col6); /* tmp6=col1-col6 */ \
  139. tmp7 = _mm_sub_pi16(col0, col7); /* tmp7=col0-col7 */ \
  140. tmp1 = _mm_add_pi16(col1, col6); /* tmp1=col1+col6 */ \
  141. tmp0 = _mm_add_pi16(col0, col7); /* tmp0=col0+col7 */ \
  142. \
  143. col2 = _mm_unpacklo_pi32(row01b, row23b); /* col2=(02 12 22 32) */ \
  144. col3 = _mm_unpackhi_pi32(row01b, row23b); /* col3=(03 13 23 33) */ \
  145. col4 = _mm_unpacklo_pi32(row01c, row23c); /* col4=(04 14 24 34) */ \
  146. col5 = _mm_unpackhi_pi32(row01c, row23c); /* col5=(05 15 25 35) */ \
  147. \
  148. tmp3 = _mm_add_pi16(col3, col4); /* tmp3=col3+col4 */ \
  149. tmp2 = _mm_add_pi16(col2, col5); /* tmp2=col2+col5 */ \
  150. tmp4 = _mm_sub_pi16(col3, col4); /* tmp4=col3-col4 */ \
  151. tmp5 = _mm_sub_pi16(col2, col5); /* tmp5=col2-col5 */ \
  152. \
  153. DO_FDCT_COMMON() \
  154. \
  155. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 0], out0); \
  156. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 0 + 4], out4); \
  157. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 1], out1); \
  158. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 1 + 4], out5); \
  159. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 2], out2); \
  160. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 2 + 4], out6); \
  161. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 3], out3); \
  162. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 3 + 4], out7); \
  163. }
  164. #define DO_FDCT_PASS2() { \
  165. __m64 col0l, col0h, col1l, col1h, col2l, col2h, col3l, col3h; \
  166. __m64 col01a, col01b, col01c, col01d, col23a, col23b, col23c, col23d; \
  167. __m64 row0, row1, row2, row3, row4, row5, row6, row7; \
  168. \
  169. col0l = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 0]); /* (00 10 20 30) */ \
  170. col1l = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 1]); /* (01 11 21 31) */ \
  171. col2l = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 2]); /* (02 12 22 32) */ \
  172. col3l = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 3]); /* (03 13 23 33) */ \
  173. col0h = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 4]); /* (40 50 60 70) */ \
  174. col1h = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 5]); /* (41 51 61 71) */ \
  175. col2h = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 6]); /* (42 52 62 72) */ \
  176. col3h = _mm_load_si64((__m64 *)&dataptr[DCTSIZE * 7]); /* (43 53 63 73) */ \
  177. \
  178. /* Transpose coefficients */ \
  179. \
  180. col23a = _mm_unpacklo_pi16(col2l, col3l); /* col23a=(02 03 12 13) */ \
  181. col23b = _mm_unpackhi_pi16(col2l, col3l); /* col23b=(22 23 32 33) */ \
  182. col23c = _mm_unpacklo_pi16(col2h, col3h); /* col23c=(42 43 52 53) */ \
  183. col23d = _mm_unpackhi_pi16(col2h, col3h); /* col23d=(62 63 72 73) */ \
  184. \
  185. col01a = _mm_unpacklo_pi16(col0l, col1l); /* col01a=(00 01 10 11) */ \
  186. col01b = _mm_unpackhi_pi16(col0l, col1l); /* col01b=(20 21 30 31) */ \
  187. col01c = _mm_unpacklo_pi16(col0h, col1h); /* col01c=(40 41 50 51) */ \
  188. col01d = _mm_unpackhi_pi16(col0h, col1h); /* col01d=(60 61 70 71) */ \
  189. \
  190. row0 = _mm_unpacklo_pi32(col01a, col23a); /* row0=(00 01 02 03) */ \
  191. row1 = _mm_unpackhi_pi32(col01a, col23a); /* row1=(10 11 12 13) */ \
  192. row6 = _mm_unpacklo_pi32(col01d, col23d); /* row6=(60 61 62 63) */ \
  193. row7 = _mm_unpackhi_pi32(col01d, col23d); /* row7=(70 71 72 73) */ \
  194. \
  195. tmp6 = _mm_sub_pi16(row1, row6); /* tmp6=row1-row6 */ \
  196. tmp7 = _mm_sub_pi16(row0, row7); /* tmp7=row0-row7 */ \
  197. tmp1 = _mm_add_pi16(row1, row6); /* tmp1=row1+row6 */ \
  198. tmp0 = _mm_add_pi16(row0, row7); /* tmp0=row0+row7 */ \
  199. \
  200. row2 = _mm_unpacklo_pi32(col01b, col23b); /* row2=(20 21 22 23) */ \
  201. row3 = _mm_unpackhi_pi32(col01b, col23b); /* row3=(30 31 32 33) */ \
  202. row4 = _mm_unpacklo_pi32(col01c, col23c); /* row4=(40 41 42 43) */ \
  203. row5 = _mm_unpackhi_pi32(col01c, col23c); /* row5=(50 51 52 53) */ \
  204. \
  205. tmp3 = _mm_add_pi16(row3, row4); /* tmp3=row3+row4 */ \
  206. tmp2 = _mm_add_pi16(row2, row5); /* tmp2=row2+row5 */ \
  207. tmp4 = _mm_sub_pi16(row3, row4); /* tmp4=row3-row4 */ \
  208. tmp5 = _mm_sub_pi16(row2, row5); /* tmp5=row2-row5 */ \
  209. \
  210. DO_FDCT_COMMON() \
  211. \
  212. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 0], out0); \
  213. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 1], out1); \
  214. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 2], out2); \
  215. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 3], out3); \
  216. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 4], out4); \
  217. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 5], out5); \
  218. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 6], out6); \
  219. _mm_store_si64((__m64 *)&dataptr[DCTSIZE * 7], out7); \
  220. }
  221. void jsimd_fdct_ifast_mmi(DCTELEM *data)
  222. {
  223. __m64 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  224. __m64 out0, out1, out2, out3, out4, out5, out6, out7;
  225. __m64 tmp10, tmp11, tmp12, tmp13, z1, z2, z3, z4, z5, z11, z13;
  226. DCTELEM *dataptr = data;
  227. /* Pass 1: process rows. */
  228. DO_FDCT_PASS1()
  229. dataptr += DCTSIZE * 4;
  230. DO_FDCT_PASS1()
  231. /* Pass 2: process columns. */
  232. dataptr = data;
  233. DO_FDCT_PASS2()
  234. dataptr += 4;
  235. DO_FDCT_PASS2()
  236. }