jcgryext-sse2.asm 13 KB

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