jcgryext-sse2.asm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. ;
  2. ; jcgryext.asm - grayscale colorspace conversion (64-bit SSE2)
  3. ;
  4. ; Copyright (C) 2011, 2016, D. R. Commander.
  5. ; Copyright (C) 2018, Matthias Räncker.
  6. ;
  7. ; Based on the x86 SIMD extension for IJG JPEG library
  8. ; Copyright (C) 1999-2006, MIYASAKA Masaru.
  9. ; For conditions of distribution and use, see copyright notice in jsimdext.inc
  10. ;
  11. ; This file should be assembled with NASM (Netwide Assembler),
  12. ; can *not* be assembled with Microsoft's MASM or any compatible
  13. ; assembler (including Borland's Turbo Assembler).
  14. ; NASM is available from http://nasm.sourceforge.net/ or
  15. ; http://sourceforge.net/project/showfiles.php?group_id=6208
  16. %include "jcolsamp.inc"
  17. ; --------------------------------------------------------------------------
  18. ;
  19. ; Convert some rows of samples to the output colorspace.
  20. ;
  21. ; GLOBAL(void)
  22. ; jsimd_rgb_gray_convert_sse2(JDIMENSION img_width, JSAMPARRAY input_buf,
  23. ; JSAMPIMAGE output_buf, JDIMENSION output_row,
  24. ; int num_rows);
  25. ;
  26. ; r10d = JDIMENSION img_width
  27. ; r11 = JSAMPARRAY input_buf
  28. ; r12 = JSAMPIMAGE output_buf
  29. ; r13d = JDIMENSION output_row
  30. ; r14d = int num_rows
  31. %define wk(i) rbp - (WK_NUM - (i)) * SIZEOF_XMMWORD ; xmmword wk[WK_NUM]
  32. %define WK_NUM 2
  33. align 32
  34. GLOBAL_FUNCTION(jsimd_rgb_gray_convert_sse2)
  35. EXTN(jsimd_rgb_gray_convert_sse2):
  36. push rbp
  37. mov rax, rsp ; rax = original rbp
  38. sub rsp, byte 4
  39. and rsp, byte (-SIZEOF_XMMWORD) ; align to 128 bits
  40. mov [rsp], rax
  41. mov rbp, rsp ; rbp = aligned rbp
  42. lea rsp, [wk(0)]
  43. collect_args 5
  44. push rbx
  45. mov ecx, r10d
  46. test rcx, rcx
  47. jz near .return
  48. push rcx
  49. mov rsi, r12
  50. mov ecx, r13d
  51. mov rdip, JSAMPARRAY [rsi+0*SIZEOF_JSAMPARRAY]
  52. lea rdi, [rdi+rcx*SIZEOF_JSAMPROW]
  53. pop rcx
  54. mov rsi, r11
  55. mov eax, r14d
  56. test rax, rax
  57. jle near .return
  58. .rowloop:
  59. push rdi
  60. push rsi
  61. push rcx ; col
  62. mov rsip, JSAMPROW [rsi] ; inptr
  63. mov rdip, JSAMPROW [rdi] ; outptr0
  64. cmp rcx, byte SIZEOF_XMMWORD
  65. jae near .columnloop
  66. %if RGB_PIXELSIZE == 3 ; ---------------
  67. .column_ld1:
  68. push rax
  69. push rdx
  70. lea rcx, [rcx+rcx*2] ; imul ecx,RGB_PIXELSIZE
  71. test cl, SIZEOF_BYTE
  72. jz short .column_ld2
  73. sub rcx, byte SIZEOF_BYTE
  74. movzx rax, byte [rsi+rcx]
  75. .column_ld2:
  76. test cl, SIZEOF_WORD
  77. jz short .column_ld4
  78. sub rcx, byte SIZEOF_WORD
  79. movzx rdx, word [rsi+rcx]
  80. shl rax, WORD_BIT
  81. or rax, rdx
  82. .column_ld4:
  83. movd xmmA, eax
  84. pop rdx
  85. pop rax
  86. test cl, SIZEOF_DWORD
  87. jz short .column_ld8
  88. sub rcx, byte SIZEOF_DWORD
  89. movd xmmF, XMM_DWORD [rsi+rcx]
  90. pslldq xmmA, SIZEOF_DWORD
  91. por xmmA, xmmF
  92. .column_ld8:
  93. test cl, SIZEOF_MMWORD
  94. jz short .column_ld16
  95. sub rcx, byte SIZEOF_MMWORD
  96. movq xmmB, XMM_MMWORD [rsi+rcx]
  97. pslldq xmmA, SIZEOF_MMWORD
  98. por xmmA, xmmB
  99. .column_ld16:
  100. test cl, SIZEOF_XMMWORD
  101. jz short .column_ld32
  102. movdqa xmmF, xmmA
  103. movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
  104. mov rcx, SIZEOF_XMMWORD
  105. jmp short .rgb_gray_cnv
  106. .column_ld32:
  107. test cl, 2*SIZEOF_XMMWORD
  108. mov rcx, SIZEOF_XMMWORD
  109. jz short .rgb_gray_cnv
  110. movdqa xmmB, xmmA
  111. movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
  112. movdqu xmmF, XMMWORD [rsi+1*SIZEOF_XMMWORD]
  113. jmp short .rgb_gray_cnv
  114. .columnloop:
  115. movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
  116. movdqu xmmF, XMMWORD [rsi+1*SIZEOF_XMMWORD]
  117. movdqu xmmB, XMMWORD [rsi+2*SIZEOF_XMMWORD]
  118. .rgb_gray_cnv:
  119. ; xmmA=(00 10 20 01 11 21 02 12 22 03 13 23 04 14 24 05)
  120. ; xmmF=(15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A)
  121. ; xmmB=(2A 0B 1B 2B 0C 1C 2C 0D 1D 2D 0E 1E 2E 0F 1F 2F)
  122. movdqa xmmG, xmmA
  123. pslldq xmmA, 8 ; xmmA=(-- -- -- -- -- -- -- -- 00 10 20 01 11 21 02 12)
  124. psrldq xmmG, 8 ; xmmG=(22 03 13 23 04 14 24 05 -- -- -- -- -- -- -- --)
  125. punpckhbw xmmA, xmmF ; xmmA=(00 08 10 18 20 28 01 09 11 19 21 29 02 0A 12 1A)
  126. pslldq xmmF, 8 ; xmmF=(-- -- -- -- -- -- -- -- 15 25 06 16 26 07 17 27)
  127. punpcklbw xmmG, xmmB ; xmmG=(22 2A 03 0B 13 1B 23 2B 04 0C 14 1C 24 2C 05 0D)
  128. punpckhbw xmmF, xmmB ; xmmF=(15 1D 25 2D 06 0E 16 1E 26 2E 07 0F 17 1F 27 2F)
  129. movdqa xmmD, xmmA
  130. pslldq xmmA, 8 ; xmmA=(-- -- -- -- -- -- -- -- 00 08 10 18 20 28 01 09)
  131. psrldq xmmD, 8 ; xmmD=(11 19 21 29 02 0A 12 1A -- -- -- -- -- -- -- --)
  132. punpckhbw xmmA, xmmG ; xmmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 01 05 09 0D)
  133. pslldq xmmG, 8 ; xmmG=(-- -- -- -- -- -- -- -- 22 2A 03 0B 13 1B 23 2B)
  134. punpcklbw xmmD, xmmF ; xmmD=(11 15 19 1D 21 25 29 2D 02 06 0A 0E 12 16 1A 1E)
  135. punpckhbw xmmG, xmmF ; xmmG=(22 26 2A 2E 03 07 0B 0F 13 17 1B 1F 23 27 2B 2F)
  136. movdqa xmmE, xmmA
  137. pslldq xmmA, 8 ; xmmA=(-- -- -- -- -- -- -- -- 00 04 08 0C 10 14 18 1C)
  138. psrldq xmmE, 8 ; xmmE=(20 24 28 2C 01 05 09 0D -- -- -- -- -- -- -- --)
  139. punpckhbw xmmA, xmmD ; xmmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E)
  140. pslldq xmmD, 8 ; xmmD=(-- -- -- -- -- -- -- -- 11 15 19 1D 21 25 29 2D)
  141. punpcklbw xmmE, xmmG ; xmmE=(20 22 24 26 28 2A 2C 2E 01 03 05 07 09 0B 0D 0F)
  142. punpckhbw xmmD, xmmG ; xmmD=(11 13 15 17 19 1B 1D 1F 21 23 25 27 29 2B 2D 2F)
  143. pxor xmmH, xmmH
  144. movdqa xmmC, xmmA
  145. punpcklbw xmmA, xmmH ; xmmA=(00 02 04 06 08 0A 0C 0E)
  146. punpckhbw xmmC, xmmH ; xmmC=(10 12 14 16 18 1A 1C 1E)
  147. movdqa xmmB, xmmE
  148. punpcklbw xmmE, xmmH ; xmmE=(20 22 24 26 28 2A 2C 2E)
  149. punpckhbw xmmB, xmmH ; xmmB=(01 03 05 07 09 0B 0D 0F)
  150. movdqa xmmF, xmmD
  151. punpcklbw xmmD, xmmH ; xmmD=(11 13 15 17 19 1B 1D 1F)
  152. punpckhbw xmmF, xmmH ; xmmF=(21 23 25 27 29 2B 2D 2F)
  153. %else ; RGB_PIXELSIZE == 4 ; -----------
  154. .column_ld1:
  155. test cl, SIZEOF_XMMWORD/16
  156. jz short .column_ld2
  157. sub rcx, byte SIZEOF_XMMWORD/16
  158. movd xmmA, XMM_DWORD [rsi+rcx*RGB_PIXELSIZE]
  159. .column_ld2:
  160. test cl, SIZEOF_XMMWORD/8
  161. jz short .column_ld4
  162. sub rcx, byte SIZEOF_XMMWORD/8
  163. movq xmmE, XMM_MMWORD [rsi+rcx*RGB_PIXELSIZE]
  164. pslldq xmmA, SIZEOF_MMWORD
  165. por xmmA, xmmE
  166. .column_ld4:
  167. test cl, SIZEOF_XMMWORD/4
  168. jz short .column_ld8
  169. sub rcx, byte SIZEOF_XMMWORD/4
  170. movdqa xmmE, xmmA
  171. movdqu xmmA, XMMWORD [rsi+rcx*RGB_PIXELSIZE]
  172. .column_ld8:
  173. test cl, SIZEOF_XMMWORD/2
  174. mov rcx, SIZEOF_XMMWORD
  175. jz short .rgb_gray_cnv
  176. movdqa xmmF, xmmA
  177. movdqa xmmH, xmmE
  178. movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
  179. movdqu xmmE, XMMWORD [rsi+1*SIZEOF_XMMWORD]
  180. jmp short .rgb_gray_cnv
  181. .columnloop:
  182. movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
  183. movdqu xmmE, XMMWORD [rsi+1*SIZEOF_XMMWORD]
  184. movdqu xmmF, XMMWORD [rsi+2*SIZEOF_XMMWORD]
  185. movdqu xmmH, XMMWORD [rsi+3*SIZEOF_XMMWORD]
  186. .rgb_gray_cnv:
  187. ; xmmA=(00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33)
  188. ; xmmE=(04 14 24 34 05 15 25 35 06 16 26 36 07 17 27 37)
  189. ; xmmF=(08 18 28 38 09 19 29 39 0A 1A 2A 3A 0B 1B 2B 3B)
  190. ; xmmH=(0C 1C 2C 3C 0D 1D 2D 3D 0E 1E 2E 3E 0F 1F 2F 3F)
  191. movdqa xmmD, xmmA
  192. punpcklbw xmmA, xmmE ; xmmA=(00 04 10 14 20 24 30 34 01 05 11 15 21 25 31 35)
  193. punpckhbw xmmD, xmmE ; xmmD=(02 06 12 16 22 26 32 36 03 07 13 17 23 27 33 37)
  194. movdqa xmmC, xmmF
  195. punpcklbw xmmF, xmmH ; xmmF=(08 0C 18 1C 28 2C 38 3C 09 0D 19 1D 29 2D 39 3D)
  196. punpckhbw xmmC, xmmH ; xmmC=(0A 0E 1A 1E 2A 2E 3A 3E 0B 0F 1B 1F 2B 2F 3B 3F)
  197. movdqa xmmB, xmmA
  198. punpcklwd xmmA, xmmF ; xmmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C)
  199. punpckhwd xmmB, xmmF ; xmmB=(01 05 09 0D 11 15 19 1D 21 25 29 2D 31 35 39 3D)
  200. movdqa xmmG, xmmD
  201. punpcklwd xmmD, xmmC ; xmmD=(02 06 0A 0E 12 16 1A 1E 22 26 2A 2E 32 36 3A 3E)
  202. punpckhwd xmmG, xmmC ; xmmG=(03 07 0B 0F 13 17 1B 1F 23 27 2B 2F 33 37 3B 3F)
  203. movdqa xmmE, xmmA
  204. punpcklbw xmmA, xmmD ; xmmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E)
  205. punpckhbw xmmE, xmmD ; xmmE=(20 22 24 26 28 2A 2C 2E 30 32 34 36 38 3A 3C 3E)
  206. movdqa xmmH, xmmB
  207. punpcklbw xmmB, xmmG ; xmmB=(01 03 05 07 09 0B 0D 0F 11 13 15 17 19 1B 1D 1F)
  208. punpckhbw xmmH, xmmG ; xmmH=(21 23 25 27 29 2B 2D 2F 31 33 35 37 39 3B 3D 3F)
  209. pxor xmmF, xmmF
  210. movdqa xmmC, xmmA
  211. punpcklbw xmmA, xmmF ; xmmA=(00 02 04 06 08 0A 0C 0E)
  212. punpckhbw xmmC, xmmF ; xmmC=(10 12 14 16 18 1A 1C 1E)
  213. movdqa xmmD, xmmB
  214. punpcklbw xmmB, xmmF ; xmmB=(01 03 05 07 09 0B 0D 0F)
  215. punpckhbw xmmD, xmmF ; xmmD=(11 13 15 17 19 1B 1D 1F)
  216. movdqa xmmG, xmmE
  217. punpcklbw xmmE, xmmF ; xmmE=(20 22 24 26 28 2A 2C 2E)
  218. punpckhbw xmmG, xmmF ; xmmG=(30 32 34 36 38 3A 3C 3E)
  219. punpcklbw xmmF, xmmH
  220. punpckhbw xmmH, xmmH
  221. psrlw xmmF, BYTE_BIT ; xmmF=(21 23 25 27 29 2B 2D 2F)
  222. psrlw xmmH, BYTE_BIT ; xmmH=(31 33 35 37 39 3B 3D 3F)
  223. %endif ; RGB_PIXELSIZE ; ---------------
  224. ; xmm0=R(02468ACE)=RE, xmm2=G(02468ACE)=GE, xmm4=B(02468ACE)=BE
  225. ; xmm1=R(13579BDF)=RO, xmm3=G(13579BDF)=GO, xmm5=B(13579BDF)=BO
  226. ; (Original)
  227. ; Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
  228. ;
  229. ; (This implementation)
  230. ; Y = 0.29900 * R + 0.33700 * G + 0.11400 * B + 0.25000 * G
  231. movdqa xmm6, xmm1
  232. punpcklwd xmm1, xmm3
  233. punpckhwd xmm6, xmm3
  234. pmaddwd xmm1, [rel PW_F0299_F0337] ; xmm1=ROL*FIX(0.299)+GOL*FIX(0.337)
  235. pmaddwd xmm6, [rel PW_F0299_F0337] ; xmm6=ROH*FIX(0.299)+GOH*FIX(0.337)
  236. movdqa xmm7, xmm6 ; xmm7=ROH*FIX(0.299)+GOH*FIX(0.337)
  237. movdqa xmm6, xmm0
  238. punpcklwd xmm0, xmm2
  239. punpckhwd xmm6, xmm2
  240. pmaddwd xmm0, [rel PW_F0299_F0337] ; xmm0=REL*FIX(0.299)+GEL*FIX(0.337)
  241. pmaddwd xmm6, [rel PW_F0299_F0337] ; xmm6=REH*FIX(0.299)+GEH*FIX(0.337)
  242. movdqa XMMWORD [wk(0)], xmm0 ; wk(0)=REL*FIX(0.299)+GEL*FIX(0.337)
  243. movdqa XMMWORD [wk(1)], xmm6 ; wk(1)=REH*FIX(0.299)+GEH*FIX(0.337)
  244. movdqa xmm0, xmm5 ; xmm0=BO
  245. movdqa xmm6, xmm4 ; xmm6=BE
  246. movdqa xmm4, xmm0
  247. punpcklwd xmm0, xmm3
  248. punpckhwd xmm4, xmm3
  249. pmaddwd xmm0, [rel PW_F0114_F0250] ; xmm0=BOL*FIX(0.114)+GOL*FIX(0.250)
  250. pmaddwd xmm4, [rel PW_F0114_F0250] ; xmm4=BOH*FIX(0.114)+GOH*FIX(0.250)
  251. movdqa xmm3, [rel PD_ONEHALF] ; xmm3=[PD_ONEHALF]
  252. paddd xmm0, xmm1
  253. paddd xmm4, xmm7
  254. paddd xmm0, xmm3
  255. paddd xmm4, xmm3
  256. psrld xmm0, SCALEBITS ; xmm0=YOL
  257. psrld xmm4, SCALEBITS ; xmm4=YOH
  258. packssdw xmm0, xmm4 ; xmm0=YO
  259. movdqa xmm4, xmm6
  260. punpcklwd xmm6, xmm2
  261. punpckhwd xmm4, xmm2
  262. pmaddwd xmm6, [rel PW_F0114_F0250] ; xmm6=BEL*FIX(0.114)+GEL*FIX(0.250)
  263. pmaddwd xmm4, [rel PW_F0114_F0250] ; xmm4=BEH*FIX(0.114)+GEH*FIX(0.250)
  264. movdqa xmm2, [rel PD_ONEHALF] ; xmm2=[PD_ONEHALF]
  265. paddd xmm6, XMMWORD [wk(0)]
  266. paddd xmm4, XMMWORD [wk(1)]
  267. paddd xmm6, xmm2
  268. paddd xmm4, xmm2
  269. psrld xmm6, SCALEBITS ; xmm6=YEL
  270. psrld xmm4, SCALEBITS ; xmm4=YEH
  271. packssdw xmm6, xmm4 ; xmm6=YE
  272. psllw xmm0, BYTE_BIT
  273. por xmm6, xmm0 ; xmm6=Y
  274. movdqa XMMWORD [rdi], xmm6 ; Save Y
  275. sub rcx, byte SIZEOF_XMMWORD
  276. add rsi, byte RGB_PIXELSIZE*SIZEOF_XMMWORD ; inptr
  277. add rdi, byte SIZEOF_XMMWORD ; outptr0
  278. cmp rcx, byte SIZEOF_XMMWORD
  279. jae near .columnloop
  280. test rcx, rcx
  281. jnz near .column_ld1
  282. pop rcx ; col
  283. pop rsi
  284. pop rdi
  285. add rsi, byte SIZEOF_JSAMPROW ; input_buf
  286. add rdi, byte SIZEOF_JSAMPROW
  287. dec rax ; num_rows
  288. jg near .rowloop
  289. .return:
  290. pop rbx
  291. uncollect_args 5
  292. mov rsp, rbp ; rsp <- aligned rbp
  293. pop rsp ; rsp <- original rbp
  294. pop rbp
  295. ret
  296. ; For some reason, the OS X linker does not honor the request to align the
  297. ; segment unless we do this.
  298. align 32