jdcolext-mmi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Loongson MMI optimizations for libjpeg-turbo
  3. *
  4. * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
  5. * Copyright (C) 2015, 2019, D. R. Commander. All Rights Reserved.
  6. * Copyright (C) 2016-2018, Loongson Technology Corporation Limited, BeiJing.
  7. * All Rights Reserved.
  8. * Authors: ZhuChen <zhuchen@loongson.cn>
  9. * SunZhangzhi <sunzhangzhi-cq@loongson.cn>
  10. * CaiWanwei <caiwanwei@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. /* This file is included by jdcolor-mmi.c */
  32. #if RGB_RED == 0
  33. #define mmA re
  34. #define mmB ro
  35. #elif RGB_GREEN == 0
  36. #define mmA ge
  37. #define mmB go
  38. #elif RGB_BLUE == 0
  39. #define mmA be
  40. #define mmB bo
  41. #else
  42. #define mmA xe
  43. #define mmB xo
  44. #endif
  45. #if RGB_RED == 1
  46. #define mmC re
  47. #define mmD ro
  48. #elif RGB_GREEN == 1
  49. #define mmC ge
  50. #define mmD go
  51. #elif RGB_BLUE == 1
  52. #define mmC be
  53. #define mmD bo
  54. #else
  55. #define mmC xe
  56. #define mmD xo
  57. #endif
  58. #if RGB_RED == 2
  59. #define mmE re
  60. #define mmF ro
  61. #elif RGB_GREEN == 2
  62. #define mmE ge
  63. #define mmF go
  64. #elif RGB_BLUE == 2
  65. #define mmE be
  66. #define mmF bo
  67. #else
  68. #define mmE xe
  69. #define mmF xo
  70. #endif
  71. #if RGB_RED == 3
  72. #define mmG re
  73. #define mmH ro
  74. #elif RGB_GREEN == 3
  75. #define mmG ge
  76. #define mmH go
  77. #elif RGB_BLUE == 3
  78. #define mmG be
  79. #define mmH bo
  80. #else
  81. #define mmG xe
  82. #define mmH xo
  83. #endif
  84. void jsimd_ycc_rgb_convert_mmi(JDIMENSION out_width, JSAMPIMAGE input_buf,
  85. JDIMENSION input_row, JSAMPARRAY output_buf,
  86. int num_rows)
  87. {
  88. JSAMPROW outptr, inptr0, inptr1, inptr2;
  89. int num_cols, col;
  90. __m64 ye, yo, y, cbe, cbe2, cbo, cbo2, cb, cre, cre2, cro, cro2, cr;
  91. __m64 re, ro, gle, ghe, ge, glo, gho, go, be, bo, xe = 0.0, xo = 0.0;
  92. __m64 decenter, mask;
  93. while (--num_rows >= 0) {
  94. inptr0 = input_buf[0][input_row];
  95. inptr1 = input_buf[1][input_row];
  96. inptr2 = input_buf[2][input_row];
  97. input_row++;
  98. outptr = *output_buf++;
  99. for (num_cols = out_width; num_cols > 0; num_cols -= 8,
  100. inptr0 += 8, inptr1 += 8, inptr2 += 8) {
  101. cb = _mm_load_si64((__m64 *)inptr1);
  102. cr = _mm_load_si64((__m64 *)inptr2);
  103. y = _mm_load_si64((__m64 *)inptr0);
  104. mask = decenter = 0.0;
  105. mask = _mm_cmpeq_pi16(mask, mask);
  106. decenter = _mm_cmpeq_pi16(decenter, decenter);
  107. mask = _mm_srli_pi16(mask, BYTE_BIT); /* {0xFF 0x00 0xFF 0x00 ..} */
  108. decenter = _mm_slli_pi16(decenter, 7); /* {0xFF80 0xFF80 0xFF80 0xFF80} */
  109. cbe = _mm_and_si64(mask, cb); /* Cb(0246) */
  110. cbo = _mm_srli_pi16(cb, BYTE_BIT); /* Cb(1357) */
  111. cre = _mm_and_si64(mask, cr); /* Cr(0246) */
  112. cro = _mm_srli_pi16(cr, BYTE_BIT); /* Cr(1357) */
  113. cbe = _mm_add_pi16(cbe, decenter);
  114. cbo = _mm_add_pi16(cbo, decenter);
  115. cre = _mm_add_pi16(cre, decenter);
  116. cro = _mm_add_pi16(cro, decenter);
  117. /* (Original)
  118. * R = Y + 1.40200 * Cr
  119. * G = Y - 0.34414 * Cb - 0.71414 * Cr
  120. * B = Y + 1.77200 * Cb
  121. *
  122. * (This implementation)
  123. * R = Y + 0.40200 * Cr + Cr
  124. * G = Y - 0.34414 * Cb + 0.28586 * Cr - Cr
  125. * B = Y - 0.22800 * Cb + Cb + Cb
  126. */
  127. cbe2 = _mm_add_pi16(cbe, cbe); /* 2*CbE */
  128. cbo2 = _mm_add_pi16(cbo, cbo); /* 2*CbO */
  129. cre2 = _mm_add_pi16(cre, cre); /* 2*CrE */
  130. cro2 = _mm_add_pi16(cro, cro); /* 2*CrO */
  131. be = _mm_mulhi_pi16(cbe2, PW_MF0228); /* (2*CbE * -FIX(0.22800) */
  132. bo = _mm_mulhi_pi16(cbo2, PW_MF0228); /* (2*CbO * -FIX(0.22800) */
  133. re = _mm_mulhi_pi16(cre2, PW_F0402); /* (2*CrE * FIX(0.40200)) */
  134. ro = _mm_mulhi_pi16(cro2, PW_F0402); /* (2*CrO * FIX(0.40200)) */
  135. be = _mm_add_pi16(be, PW_ONE);
  136. bo = _mm_add_pi16(bo, PW_ONE);
  137. be = _mm_srai_pi16(be, 1); /* (CbE * -FIX(0.22800)) */
  138. bo = _mm_srai_pi16(bo, 1); /* (CbO * -FIX(0.22800)) */
  139. re = _mm_add_pi16(re, PW_ONE);
  140. ro = _mm_add_pi16(ro, PW_ONE);
  141. re = _mm_srai_pi16(re, 1); /* (CrE * FIX(0.40200)) */
  142. ro = _mm_srai_pi16(ro, 1); /* (CrO * FIX(0.40200)) */
  143. be = _mm_add_pi16(be, cbe);
  144. bo = _mm_add_pi16(bo, cbo);
  145. be = _mm_add_pi16(be, cbe); /* (CbE * FIX(1.77200))=(B-Y)E */
  146. bo = _mm_add_pi16(bo, cbo); /* (CbO * FIX(1.77200))=(B-Y)O */
  147. re = _mm_add_pi16(re, cre); /* (CrE * FIX(1.40200))=(R-Y)E */
  148. ro = _mm_add_pi16(ro, cro); /* (CrO * FIX(1.40200))=(R-Y)O */
  149. gle = _mm_unpacklo_pi16(cbe, cre);
  150. ghe = _mm_unpackhi_pi16(cbe, cre);
  151. gle = _mm_madd_pi16(gle, PW_MF0344_F0285);
  152. ghe = _mm_madd_pi16(ghe, PW_MF0344_F0285);
  153. glo = _mm_unpacklo_pi16(cbo, cro);
  154. gho = _mm_unpackhi_pi16(cbo, cro);
  155. glo = _mm_madd_pi16(glo, PW_MF0344_F0285);
  156. gho = _mm_madd_pi16(gho, PW_MF0344_F0285);
  157. gle = _mm_add_pi32(gle, PD_ONEHALF);
  158. ghe = _mm_add_pi32(ghe, PD_ONEHALF);
  159. gle = _mm_srai_pi32(gle, SCALEBITS);
  160. ghe = _mm_srai_pi32(ghe, SCALEBITS);
  161. glo = _mm_add_pi32(glo, PD_ONEHALF);
  162. gho = _mm_add_pi32(gho, PD_ONEHALF);
  163. glo = _mm_srai_pi32(glo, SCALEBITS);
  164. gho = _mm_srai_pi32(gho, SCALEBITS);
  165. ge = _mm_packs_pi32(gle, ghe); /* CbE*-FIX(0.344)+CrE*FIX(0.285) */
  166. go = _mm_packs_pi32(glo, gho); /* CbO*-FIX(0.344)+CrO*FIX(0.285) */
  167. ge = _mm_sub_pi16(ge, cre); /* CbE*-FIX(0.344)+CrE*-FIX(0.714)=(G-Y)E */
  168. go = _mm_sub_pi16(go, cro); /* CbO*-FIX(0.344)+CrO*-FIX(0.714)=(G-Y)O */
  169. ye = _mm_and_si64(mask, y); /* Y(0246) */
  170. yo = _mm_srli_pi16(y, BYTE_BIT); /* Y(1357) */
  171. re = _mm_add_pi16(re, ye); /* ((R-Y)E+YE)=(R0 R2 R4 R6) */
  172. ro = _mm_add_pi16(ro, yo); /* ((R-Y)O+YO)=(R1 R3 R5 R7) */
  173. re = _mm_packs_pu16(re, re); /* (R0 R2 R4 R6 ** ** ** **) */
  174. ro = _mm_packs_pu16(ro, ro); /* (R1 R3 R5 R7 ** ** ** **) */
  175. ge = _mm_add_pi16(ge, ye); /* ((G-Y)E+YE)=(G0 G2 G4 G6) */
  176. go = _mm_add_pi16(go, yo); /* ((G-Y)O+YO)=(G1 G3 G5 G7) */
  177. ge = _mm_packs_pu16(ge, ge); /* (G0 G2 G4 G6 ** ** ** **) */
  178. go = _mm_packs_pu16(go, go); /* (G1 G3 G5 G7 ** ** ** **) */
  179. be = _mm_add_pi16(be, ye); /* (YE+(B-Y)E)=(B0 B2 B4 B6) */
  180. bo = _mm_add_pi16(bo, yo); /* (YO+(B-Y)O)=(B1 B3 B5 B7) */
  181. be = _mm_packs_pu16(be, be); /* (B0 B2 B4 B6 ** ** ** **) */
  182. bo = _mm_packs_pu16(bo, bo); /* (B1 B3 B5 B7 ** ** ** **) */
  183. #if RGB_PIXELSIZE == 3
  184. /* mmA=(00 02 04 06 ** ** ** **), mmB=(01 03 05 07 ** ** ** **) */
  185. /* mmC=(10 12 14 16 ** ** ** **), mmD=(11 13 15 17 ** ** ** **) */
  186. mmA = _mm_unpacklo_pi8(mmA, mmC); /* (00 10 02 12 04 14 06 16) */
  187. mmE = _mm_unpacklo_pi8(mmE, mmB); /* (20 01 22 03 24 05 26 07) */
  188. mmD = _mm_unpacklo_pi8(mmD, mmF); /* (11 21 13 23 15 25 17 27) */
  189. mmH = _mm_srli_si64(mmA, 2 * BYTE_BIT);
  190. mmG = _mm_unpackhi_pi16(mmA, mmE); /* (04 14 24 05 06 16 26 07) */
  191. mmA = _mm_unpacklo_pi16(mmA, mmE); /* (00 10 20 01 02 12 22 03) */
  192. mmE = _mm_srli_si64(mmE, 2 * BYTE_BIT);
  193. mmB = _mm_srli_si64(mmD, 2 * BYTE_BIT); /* (13 23 15 25 17 27 -- --) */
  194. mmC = _mm_unpackhi_pi16(mmD, mmH); /* (15 25 06 16 17 27 -- --) */
  195. mmD = _mm_unpacklo_pi16(mmD, mmH); /* (11 21 02 12 13 23 04 14) */
  196. mmF = _mm_unpackhi_pi16(mmE, mmB); /* (26 07 17 27 -- -- -- --) */
  197. mmE = _mm_unpacklo_pi16(mmE, mmB); /* (22 03 13 23 24 05 15 25) */
  198. mmA = _mm_unpacklo_pi32(mmA, mmD); /* (00 10 20 01 11 21 02 12) */
  199. mmE = _mm_unpacklo_pi32(mmE, mmG); /* (22 03 13 23 04 14 24 05) */
  200. mmC = _mm_unpacklo_pi32(mmC, mmF); /* (15 25 06 16 26 07 17 27) */
  201. if (num_cols >= 8) {
  202. if (!(((long)outptr) & 7)) {
  203. _mm_store_si64((__m64 *)outptr, mmA);
  204. _mm_store_si64((__m64 *)(outptr + 8), mmE);
  205. _mm_store_si64((__m64 *)(outptr + 16), mmC);
  206. } else {
  207. _mm_storeu_si64((__m64 *)outptr, mmA);
  208. _mm_storeu_si64((__m64 *)(outptr + 8), mmE);
  209. _mm_storeu_si64((__m64 *)(outptr + 16), mmC);
  210. }
  211. outptr += RGB_PIXELSIZE * 8;
  212. } else {
  213. col = num_cols * 3;
  214. asm(".set noreorder\r\n"
  215. "li $8, 16\r\n"
  216. "move $9, %4\r\n"
  217. "mov.s $f4, %1\r\n"
  218. "mov.s $f6, %3\r\n"
  219. "move $10, %5\r\n"
  220. "bltu $9, $8, 1f\r\n"
  221. "nop \r\n"
  222. "gssdlc1 $f4, 7($10)\r\n"
  223. "gssdrc1 $f4, 0($10)\r\n"
  224. "gssdlc1 $f6, 7+8($10)\r\n"
  225. "gssdrc1 $f6, 8($10)\r\n"
  226. "mov.s $f4, %2\r\n"
  227. "subu $9, $9, 16\r\n"
  228. PTR_ADDU "$10, $10, 16\r\n"
  229. "b 2f\r\n"
  230. "nop \r\n"
  231. "1: \r\n"
  232. "li $8, 8\r\n" /* st8 */
  233. "bltu $9, $8, 2f\r\n"
  234. "nop \r\n"
  235. "gssdlc1 $f4, 7($10)\r\n"
  236. "gssdrc1 $f4, 0($10)\r\n"
  237. "mov.s $f4, %3\r\n"
  238. "subu $9, $9, 8\r\n"
  239. PTR_ADDU "$10, $10, 8\r\n"
  240. "2: \r\n"
  241. "li $8, 4\r\n" /* st4 */
  242. "mfc1 $11, $f4\r\n"
  243. "bltu $9, $8, 3f\r\n"
  244. "nop \r\n"
  245. "swl $11, 3($10)\r\n"
  246. "swr $11, 0($10)\r\n"
  247. "li $8, 32\r\n"
  248. "mtc1 $8, $f6\r\n"
  249. "dsrl $f4, $f4, $f6\r\n"
  250. "mfc1 $11, $f4\r\n"
  251. "subu $9, $9, 4\r\n"
  252. PTR_ADDU "$10, $10, 4\r\n"
  253. "3: \r\n"
  254. "li $8, 2\r\n" /* st2 */
  255. "bltu $9, $8, 4f\r\n"
  256. "nop \r\n"
  257. "ush $11, 0($10)\r\n"
  258. "srl $11, 16\r\n"
  259. "subu $9, $9, 2\r\n"
  260. PTR_ADDU "$10, $10, 2\r\n"
  261. "4: \r\n"
  262. "li $8, 1\r\n" /* st1 */
  263. "bltu $9, $8, 5f\r\n"
  264. "nop \r\n"
  265. "sb $11, 0($10)\r\n"
  266. "5: \r\n"
  267. "nop \r\n" /* end */
  268. : "=m" (*outptr)
  269. : "f" (mmA), "f" (mmC), "f" (mmE), "r" (col), "r" (outptr)
  270. : "$f4", "$f6", "$8", "$9", "$10", "$11", "memory"
  271. );
  272. }
  273. #else /* RGB_PIXELSIZE == 4 */
  274. #ifdef RGBX_FILLER_0XFF
  275. xe = _mm_cmpeq_pi8(xe, xe);
  276. xo = _mm_cmpeq_pi8(xo, xo);
  277. #else
  278. xe = _mm_xor_si64(xe, xe);
  279. xo = _mm_xor_si64(xo, xo);
  280. #endif
  281. /* mmA=(00 02 04 06 ** ** ** **), mmB=(01 03 05 07 ** ** ** **) */
  282. /* mmC=(10 12 14 16 ** ** ** **), mmD=(11 13 15 17 ** ** ** **) */
  283. /* mmE=(20 22 24 26 ** ** ** **), mmF=(21 23 25 27 ** ** ** **) */
  284. /* mmG=(30 32 34 36 ** ** ** **), mmH=(31 33 35 37 ** ** ** **) */
  285. mmA = _mm_unpacklo_pi8(mmA, mmC); /* (00 10 02 12 04 14 06 16) */
  286. mmE = _mm_unpacklo_pi8(mmE, mmG); /* (20 30 22 32 24 34 26 36) */
  287. mmB = _mm_unpacklo_pi8(mmB, mmD); /* (01 11 03 13 05 15 07 17) */
  288. mmF = _mm_unpacklo_pi8(mmF, mmH); /* (21 31 23 33 25 35 27 37) */
  289. mmC = _mm_unpackhi_pi16(mmA, mmE); /* (04 14 24 34 06 16 26 36) */
  290. mmA = _mm_unpacklo_pi16(mmA, mmE); /* (00 10 20 30 02 12 22 32) */
  291. mmG = _mm_unpackhi_pi16(mmB, mmF); /* (05 15 25 35 07 17 27 37) */
  292. mmB = _mm_unpacklo_pi16(mmB, mmF); /* (01 11 21 31 03 13 23 33) */
  293. mmD = _mm_unpackhi_pi32(mmA, mmB); /* (02 12 22 32 03 13 23 33) */
  294. mmA = _mm_unpacklo_pi32(mmA, mmB); /* (00 10 20 30 01 11 21 31) */
  295. mmH = _mm_unpackhi_pi32(mmC, mmG); /* (06 16 26 36 07 17 27 37) */
  296. mmC = _mm_unpacklo_pi32(mmC, mmG); /* (04 14 24 34 05 15 25 35) */
  297. if (num_cols >= 8) {
  298. if (!(((long)outptr) & 7)) {
  299. _mm_store_si64((__m64 *)outptr, mmA);
  300. _mm_store_si64((__m64 *)(outptr + 8), mmD);
  301. _mm_store_si64((__m64 *)(outptr + 16), mmC);
  302. _mm_store_si64((__m64 *)(outptr + 24), mmH);
  303. } else {
  304. _mm_storeu_si64((__m64 *)outptr, mmA);
  305. _mm_storeu_si64((__m64 *)(outptr + 8), mmD);
  306. _mm_storeu_si64((__m64 *)(outptr + 16), mmC);
  307. _mm_storeu_si64((__m64 *)(outptr + 24), mmH);
  308. }
  309. outptr += RGB_PIXELSIZE * 8;
  310. } else {
  311. col = num_cols;
  312. asm(".set noreorder\r\n" /* st16 */
  313. "li $8, 4\r\n"
  314. "move $9, %6\r\n"
  315. "move $10, %7\r\n"
  316. "mov.s $f4, %2\r\n"
  317. "mov.s $f6, %4\r\n"
  318. "bltu $9, $8, 1f\r\n"
  319. "nop \r\n"
  320. "gssdlc1 $f4, 7($10)\r\n"
  321. "gssdrc1 $f4, 0($10)\r\n"
  322. "gssdlc1 $f6, 7+8($10)\r\n"
  323. "gssdrc1 $f6, 8($10)\r\n"
  324. "mov.s $f4, %3\r\n"
  325. "mov.s $f6, %5\r\n"
  326. "subu $9, $9, 4\r\n"
  327. PTR_ADDU "$10, $10, 16\r\n"
  328. "1: \r\n"
  329. "li $8, 2\r\n" /* st8 */
  330. "bltu $9, $8, 2f\r\n"
  331. "nop \r\n"
  332. "gssdlc1 $f4, 7($10)\r\n"
  333. "gssdrc1 $f4, 0($10)\r\n"
  334. "mov.s $f4, $f6\r\n"
  335. "subu $9, $9, 2\r\n"
  336. PTR_ADDU "$10, $10, 8\r\n"
  337. "2: \r\n"
  338. "li $8, 1\r\n" /* st4 */
  339. "bltu $9, $8, 3f\r\n"
  340. "nop \r\n"
  341. "gsswlc1 $f4, 3($10)\r\n"
  342. "gsswrc1 $f4, 0($10)\r\n"
  343. "3: \r\n"
  344. "li %1, 0\r\n" /* end */
  345. : "=m" (*outptr), "=r" (col)
  346. : "f" (mmA), "f" (mmC), "f" (mmD), "f" (mmH), "r" (col),
  347. "r" (outptr)
  348. : "$f4", "$f6", "$8", "$9", "$10", "memory"
  349. );
  350. }
  351. #endif
  352. }
  353. }
  354. }
  355. #undef mmA
  356. #undef mmB
  357. #undef mmC
  358. #undef mmD
  359. #undef mmE
  360. #undef mmF
  361. #undef mmG
  362. #undef mmH