jcgryext-sse2.asm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. ;
  2. ; jcgryext.asm - grayscale colorspace conversion (SSE2)
  3. ;
  4. ; Copyright (C) 2011, 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. ;
  16. ; [TAB8]
  17. %include "jcolsamp.inc"
  18. ; --------------------------------------------------------------------------
  19. ;
  20. ; Convert some rows of samples to the output colorspace.
  21. ;
  22. ; GLOBAL(void)
  23. ; jsimd_rgb_gray_convert_sse2 (JDIMENSION img_width,
  24. ; JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  25. ; JDIMENSION output_row, int num_rows);
  26. ;
  27. %define img_width(b) (b)+8 ; JDIMENSION img_width
  28. %define input_buf(b) (b)+12 ; JSAMPARRAY input_buf
  29. %define output_buf(b) (b)+16 ; JSAMPIMAGE output_buf
  30. %define output_row(b) (b)+20 ; JDIMENSION output_row
  31. %define num_rows(b) (b)+24 ; int num_rows
  32. %define original_ebp ebp+0
  33. %define wk(i) ebp-(WK_NUM-(i))*SIZEOF_XMMWORD ; xmmword wk[WK_NUM]
  34. %define WK_NUM 2
  35. %define gotptr wk(0)-SIZEOF_POINTER ; void * gotptr
  36. align 16
  37. global EXTN(jsimd_rgb_gray_convert_sse2)
  38. EXTN(jsimd_rgb_gray_convert_sse2):
  39. push ebp
  40. mov eax,esp ; eax = original ebp
  41. sub esp, byte 4
  42. and esp, byte (-SIZEOF_XMMWORD) ; align to 128 bits
  43. mov [esp],eax
  44. mov ebp,esp ; ebp = aligned ebp
  45. lea esp, [wk(0)]
  46. pushpic eax ; make a room for GOT address
  47. push ebx
  48. ; push ecx ; need not be preserved
  49. ; push edx ; need not be preserved
  50. push esi
  51. push edi
  52. get_GOT ebx ; get GOT address
  53. movpic POINTER [gotptr], ebx ; save GOT address
  54. mov ecx, JDIMENSION [img_width(eax)]
  55. test ecx,ecx
  56. jz near .return
  57. push ecx
  58. mov esi, JSAMPIMAGE [output_buf(eax)]
  59. mov ecx, JDIMENSION [output_row(eax)]
  60. mov edi, JSAMPARRAY [esi+0*SIZEOF_JSAMPARRAY]
  61. lea edi, [edi+ecx*SIZEOF_JSAMPROW]
  62. pop ecx
  63. mov esi, JSAMPARRAY [input_buf(eax)]
  64. mov eax, INT [num_rows(eax)]
  65. test eax,eax
  66. jle near .return
  67. alignx 16,7
  68. .rowloop:
  69. pushpic eax
  70. push edi
  71. push esi
  72. push ecx ; col
  73. mov esi, JSAMPROW [esi] ; inptr
  74. mov edi, JSAMPROW [edi] ; outptr0
  75. movpic eax, POINTER [gotptr] ; load GOT address (eax)
  76. cmp ecx, byte SIZEOF_XMMWORD
  77. jae near .columnloop
  78. alignx 16,7
  79. %if RGB_PIXELSIZE == 3 ; ---------------
  80. .column_ld1:
  81. push eax
  82. push edx
  83. lea ecx,[ecx+ecx*2] ; imul ecx,RGB_PIXELSIZE
  84. test cl, SIZEOF_BYTE
  85. jz short .column_ld2
  86. sub ecx, byte SIZEOF_BYTE
  87. movzx eax, BYTE [esi+ecx]
  88. .column_ld2:
  89. test cl, SIZEOF_WORD
  90. jz short .column_ld4
  91. sub ecx, byte SIZEOF_WORD
  92. movzx edx, WORD [esi+ecx]
  93. shl eax, WORD_BIT
  94. or eax,edx
  95. .column_ld4:
  96. movd xmmA,eax
  97. pop edx
  98. pop eax
  99. test cl, SIZEOF_DWORD
  100. jz short .column_ld8
  101. sub ecx, byte SIZEOF_DWORD
  102. movd xmmF, XMM_DWORD [esi+ecx]
  103. pslldq xmmA, SIZEOF_DWORD
  104. por xmmA,xmmF
  105. .column_ld8:
  106. test cl, SIZEOF_MMWORD
  107. jz short .column_ld16
  108. sub ecx, byte SIZEOF_MMWORD
  109. movq xmmB, XMM_MMWORD [esi+ecx]
  110. pslldq xmmA, SIZEOF_MMWORD
  111. por xmmA,xmmB
  112. .column_ld16:
  113. test cl, SIZEOF_XMMWORD
  114. jz short .column_ld32
  115. movdqa xmmF,xmmA
  116. movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD]
  117. mov ecx, SIZEOF_XMMWORD
  118. jmp short .rgb_gray_cnv
  119. .column_ld32:
  120. test cl, 2*SIZEOF_XMMWORD
  121. mov ecx, SIZEOF_XMMWORD
  122. jz short .rgb_gray_cnv
  123. movdqa xmmB,xmmA
  124. movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD]
  125. movdqu xmmF, XMMWORD [esi+1*SIZEOF_XMMWORD]
  126. jmp short .rgb_gray_cnv
  127. alignx 16,7
  128. .columnloop:
  129. movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD]
  130. movdqu xmmF, XMMWORD [esi+1*SIZEOF_XMMWORD]
  131. movdqu xmmB, XMMWORD [esi+2*SIZEOF_XMMWORD]
  132. .rgb_gray_cnv:
  133. ; xmmA=(00 10 20 01 11 21 02 12 22 03 13 23 04 14 24 05)
  134. ; xmmF=(15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A)
  135. ; xmmB=(2A 0B 1B 2B 0C 1C 2C 0D 1D 2D 0E 1E 2E 0F 1F 2F)
  136. movdqa xmmG,xmmA
  137. pslldq xmmA,8 ; xmmA=(-- -- -- -- -- -- -- -- 00 10 20 01 11 21 02 12)
  138. psrldq xmmG,8 ; xmmG=(22 03 13 23 04 14 24 05 -- -- -- -- -- -- -- --)
  139. punpckhbw xmmA,xmmF ; xmmA=(00 08 10 18 20 28 01 09 11 19 21 29 02 0A 12 1A)
  140. pslldq xmmF,8 ; xmmF=(-- -- -- -- -- -- -- -- 15 25 06 16 26 07 17 27)
  141. punpcklbw xmmG,xmmB ; xmmG=(22 2A 03 0B 13 1B 23 2B 04 0C 14 1C 24 2C 05 0D)
  142. punpckhbw xmmF,xmmB ; xmmF=(15 1D 25 2D 06 0E 16 1E 26 2E 07 0F 17 1F 27 2F)
  143. movdqa xmmD,xmmA
  144. pslldq xmmA,8 ; xmmA=(-- -- -- -- -- -- -- -- 00 08 10 18 20 28 01 09)
  145. psrldq xmmD,8 ; xmmD=(11 19 21 29 02 0A 12 1A -- -- -- -- -- -- -- --)
  146. punpckhbw xmmA,xmmG ; xmmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 01 05 09 0D)
  147. pslldq xmmG,8 ; xmmG=(-- -- -- -- -- -- -- -- 22 2A 03 0B 13 1B 23 2B)
  148. punpcklbw xmmD,xmmF ; xmmD=(11 15 19 1D 21 25 29 2D 02 06 0A 0E 12 16 1A 1E)
  149. punpckhbw xmmG,xmmF ; xmmG=(22 26 2A 2E 03 07 0B 0F 13 17 1B 1F 23 27 2B 2F)
  150. movdqa xmmE,xmmA
  151. pslldq xmmA,8 ; xmmA=(-- -- -- -- -- -- -- -- 00 04 08 0C 10 14 18 1C)
  152. psrldq xmmE,8 ; xmmE=(20 24 28 2C 01 05 09 0D -- -- -- -- -- -- -- --)
  153. punpckhbw xmmA,xmmD ; xmmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E)
  154. pslldq xmmD,8 ; xmmD=(-- -- -- -- -- -- -- -- 11 15 19 1D 21 25 29 2D)
  155. punpcklbw xmmE,xmmG ; xmmE=(20 22 24 26 28 2A 2C 2E 01 03 05 07 09 0B 0D 0F)
  156. punpckhbw xmmD,xmmG ; xmmD=(11 13 15 17 19 1B 1D 1F 21 23 25 27 29 2B 2D 2F)
  157. pxor xmmH,xmmH
  158. movdqa xmmC,xmmA
  159. punpcklbw xmmA,xmmH ; xmmA=(00 02 04 06 08 0A 0C 0E)
  160. punpckhbw xmmC,xmmH ; xmmC=(10 12 14 16 18 1A 1C 1E)
  161. movdqa xmmB,xmmE
  162. punpcklbw xmmE,xmmH ; xmmE=(20 22 24 26 28 2A 2C 2E)
  163. punpckhbw xmmB,xmmH ; xmmB=(01 03 05 07 09 0B 0D 0F)
  164. movdqa xmmF,xmmD
  165. punpcklbw xmmD,xmmH ; xmmD=(11 13 15 17 19 1B 1D 1F)
  166. punpckhbw xmmF,xmmH ; xmmF=(21 23 25 27 29 2B 2D 2F)
  167. %else ; RGB_PIXELSIZE == 4 ; -----------
  168. .column_ld1:
  169. test cl, SIZEOF_XMMWORD/16
  170. jz short .column_ld2
  171. sub ecx, byte SIZEOF_XMMWORD/16
  172. movd xmmA, XMM_DWORD [esi+ecx*RGB_PIXELSIZE]
  173. .column_ld2:
  174. test cl, SIZEOF_XMMWORD/8
  175. jz short .column_ld4
  176. sub ecx, byte SIZEOF_XMMWORD/8
  177. movq xmmE, XMM_MMWORD [esi+ecx*RGB_PIXELSIZE]
  178. pslldq xmmA, SIZEOF_MMWORD
  179. por xmmA,xmmE
  180. .column_ld4:
  181. test cl, SIZEOF_XMMWORD/4
  182. jz short .column_ld8
  183. sub ecx, byte SIZEOF_XMMWORD/4
  184. movdqa xmmE,xmmA
  185. movdqu xmmA, XMMWORD [esi+ecx*RGB_PIXELSIZE]
  186. .column_ld8:
  187. test cl, SIZEOF_XMMWORD/2
  188. mov ecx, SIZEOF_XMMWORD
  189. jz short .rgb_gray_cnv
  190. movdqa xmmF,xmmA
  191. movdqa xmmH,xmmE
  192. movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD]
  193. movdqu xmmE, XMMWORD [esi+1*SIZEOF_XMMWORD]
  194. jmp short .rgb_gray_cnv
  195. alignx 16,7
  196. .columnloop:
  197. movdqu xmmA, XMMWORD [esi+0*SIZEOF_XMMWORD]
  198. movdqu xmmE, XMMWORD [esi+1*SIZEOF_XMMWORD]
  199. movdqu xmmF, XMMWORD [esi+2*SIZEOF_XMMWORD]
  200. movdqu xmmH, XMMWORD [esi+3*SIZEOF_XMMWORD]
  201. .rgb_gray_cnv:
  202. ; xmmA=(00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33)
  203. ; xmmE=(04 14 24 34 05 15 25 35 06 16 26 36 07 17 27 37)
  204. ; xmmF=(08 18 28 38 09 19 29 39 0A 1A 2A 3A 0B 1B 2B 3B)
  205. ; xmmH=(0C 1C 2C 3C 0D 1D 2D 3D 0E 1E 2E 3E 0F 1F 2F 3F)
  206. movdqa xmmD,xmmA
  207. punpcklbw xmmA,xmmE ; xmmA=(00 04 10 14 20 24 30 34 01 05 11 15 21 25 31 35)
  208. punpckhbw xmmD,xmmE ; xmmD=(02 06 12 16 22 26 32 36 03 07 13 17 23 27 33 37)
  209. movdqa xmmC,xmmF
  210. punpcklbw xmmF,xmmH ; xmmF=(08 0C 18 1C 28 2C 38 3C 09 0D 19 1D 29 2D 39 3D)
  211. punpckhbw xmmC,xmmH ; xmmC=(0A 0E 1A 1E 2A 2E 3A 3E 0B 0F 1B 1F 2B 2F 3B 3F)
  212. movdqa xmmB,xmmA
  213. punpcklwd xmmA,xmmF ; xmmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C)
  214. punpckhwd xmmB,xmmF ; xmmB=(01 05 09 0D 11 15 19 1D 21 25 29 2D 31 35 39 3D)
  215. movdqa xmmG,xmmD
  216. punpcklwd xmmD,xmmC ; xmmD=(02 06 0A 0E 12 16 1A 1E 22 26 2A 2E 32 36 3A 3E)
  217. punpckhwd xmmG,xmmC ; xmmG=(03 07 0B 0F 13 17 1B 1F 23 27 2B 2F 33 37 3B 3F)
  218. movdqa xmmE,xmmA
  219. punpcklbw xmmA,xmmD ; xmmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E)
  220. punpckhbw xmmE,xmmD ; xmmE=(20 22 24 26 28 2A 2C 2E 30 32 34 36 38 3A 3C 3E)
  221. movdqa xmmH,xmmB
  222. punpcklbw xmmB,xmmG ; xmmB=(01 03 05 07 09 0B 0D 0F 11 13 15 17 19 1B 1D 1F)
  223. punpckhbw xmmH,xmmG ; xmmH=(21 23 25 27 29 2B 2D 2F 31 33 35 37 39 3B 3D 3F)
  224. pxor xmmF,xmmF
  225. movdqa xmmC,xmmA
  226. punpcklbw xmmA,xmmF ; xmmA=(00 02 04 06 08 0A 0C 0E)
  227. punpckhbw xmmC,xmmF ; xmmC=(10 12 14 16 18 1A 1C 1E)
  228. movdqa xmmD,xmmB
  229. punpcklbw xmmB,xmmF ; xmmB=(01 03 05 07 09 0B 0D 0F)
  230. punpckhbw xmmD,xmmF ; xmmD=(11 13 15 17 19 1B 1D 1F)
  231. movdqa xmmG,xmmE
  232. punpcklbw xmmE,xmmF ; xmmE=(20 22 24 26 28 2A 2C 2E)
  233. punpckhbw xmmG,xmmF ; xmmG=(30 32 34 36 38 3A 3C 3E)
  234. punpcklbw xmmF,xmmH
  235. punpckhbw xmmH,xmmH
  236. psrlw xmmF,BYTE_BIT ; xmmF=(21 23 25 27 29 2B 2D 2F)
  237. psrlw xmmH,BYTE_BIT ; xmmH=(31 33 35 37 39 3B 3D 3F)
  238. %endif ; RGB_PIXELSIZE ; ---------------
  239. ; xmm0=R(02468ACE)=RE, xmm2=G(02468ACE)=GE, xmm4=B(02468ACE)=BE
  240. ; xmm1=R(13579BDF)=RO, xmm3=G(13579BDF)=GO, xmm5=B(13579BDF)=BO
  241. ; (Original)
  242. ; Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
  243. ;
  244. ; (This implementation)
  245. ; Y = 0.29900 * R + 0.33700 * G + 0.11400 * B + 0.25000 * G
  246. movdqa xmm6,xmm1
  247. punpcklwd xmm1,xmm3
  248. punpckhwd xmm6,xmm3
  249. pmaddwd xmm1,[GOTOFF(eax,PW_F0299_F0337)] ; xmm1=ROL*FIX(0.299)+GOL*FIX(0.337)
  250. pmaddwd xmm6,[GOTOFF(eax,PW_F0299_F0337)] ; xmm6=ROH*FIX(0.299)+GOH*FIX(0.337)
  251. movdqa xmm7, xmm6 ; xmm7=ROH*FIX(0.299)+GOH*FIX(0.337)
  252. movdqa xmm6,xmm0
  253. punpcklwd xmm0,xmm2
  254. punpckhwd xmm6,xmm2
  255. pmaddwd xmm0,[GOTOFF(eax,PW_F0299_F0337)] ; xmm0=REL*FIX(0.299)+GEL*FIX(0.337)
  256. pmaddwd xmm6,[GOTOFF(eax,PW_F0299_F0337)] ; xmm6=REH*FIX(0.299)+GEH*FIX(0.337)
  257. movdqa XMMWORD [wk(0)], xmm0 ; wk(0)=REL*FIX(0.299)+GEL*FIX(0.337)
  258. movdqa XMMWORD [wk(1)], xmm6 ; wk(1)=REH*FIX(0.299)+GEH*FIX(0.337)
  259. movdqa xmm0, xmm5 ; xmm0=BO
  260. movdqa xmm6, xmm4 ; xmm6=BE
  261. movdqa xmm4,xmm0
  262. punpcklwd xmm0,xmm3
  263. punpckhwd xmm4,xmm3
  264. pmaddwd xmm0,[GOTOFF(eax,PW_F0114_F0250)] ; xmm0=BOL*FIX(0.114)+GOL*FIX(0.250)
  265. pmaddwd xmm4,[GOTOFF(eax,PW_F0114_F0250)] ; xmm4=BOH*FIX(0.114)+GOH*FIX(0.250)
  266. movdqa xmm3,[GOTOFF(eax,PD_ONEHALF)] ; xmm3=[PD_ONEHALF]
  267. paddd xmm0, xmm1
  268. paddd xmm4, xmm7
  269. paddd xmm0,xmm3
  270. paddd xmm4,xmm3
  271. psrld xmm0,SCALEBITS ; xmm0=YOL
  272. psrld xmm4,SCALEBITS ; xmm4=YOH
  273. packssdw xmm0,xmm4 ; xmm0=YO
  274. movdqa xmm4,xmm6
  275. punpcklwd xmm6,xmm2
  276. punpckhwd xmm4,xmm2
  277. pmaddwd xmm6,[GOTOFF(eax,PW_F0114_F0250)] ; xmm6=BEL*FIX(0.114)+GEL*FIX(0.250)
  278. pmaddwd xmm4,[GOTOFF(eax,PW_F0114_F0250)] ; xmm4=BEH*FIX(0.114)+GEH*FIX(0.250)
  279. movdqa xmm2,[GOTOFF(eax,PD_ONEHALF)] ; xmm2=[PD_ONEHALF]
  280. paddd xmm6, XMMWORD [wk(0)]
  281. paddd xmm4, XMMWORD [wk(1)]
  282. paddd xmm6,xmm2
  283. paddd xmm4,xmm2
  284. psrld xmm6,SCALEBITS ; xmm6=YEL
  285. psrld xmm4,SCALEBITS ; xmm4=YEH
  286. packssdw xmm6,xmm4 ; xmm6=YE
  287. psllw xmm0,BYTE_BIT
  288. por xmm6,xmm0 ; xmm6=Y
  289. movdqa XMMWORD [edi], xmm6 ; Save Y
  290. sub ecx, byte SIZEOF_XMMWORD
  291. add esi, byte RGB_PIXELSIZE*SIZEOF_XMMWORD ; inptr
  292. add edi, byte SIZEOF_XMMWORD ; outptr0
  293. cmp ecx, byte SIZEOF_XMMWORD
  294. jae near .columnloop
  295. test ecx,ecx
  296. jnz near .column_ld1
  297. pop ecx ; col
  298. pop esi
  299. pop edi
  300. poppic eax
  301. add esi, byte SIZEOF_JSAMPROW ; input_buf
  302. add edi, byte SIZEOF_JSAMPROW
  303. dec eax ; num_rows
  304. jg near .rowloop
  305. .return:
  306. pop edi
  307. pop esi
  308. ; pop edx ; need not be preserved
  309. ; pop ecx ; need not be preserved
  310. pop ebx
  311. mov esp,ebp ; esp <- aligned ebp
  312. pop esp ; esp <- original ebp
  313. pop ebp
  314. ret
  315. ; For some reason, the OS X linker does not honor the request to align the
  316. ; segment unless we do this.
  317. align 16