jdcolext-mmx.asm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. ;
  2. ; jdcolext.asm - colorspace conversion (MMX)
  3. ;
  4. ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
  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_ycc_rgb_convert_mmx (JDIMENSION out_width,
  24. ; JSAMPIMAGE input_buf, JDIMENSION input_row,
  25. ; JSAMPARRAY output_buf, int num_rows)
  26. ;
  27. %define out_width(b) (b)+8 ; JDIMENSION out_width
  28. %define input_buf(b) (b)+12 ; JSAMPIMAGE input_buf
  29. %define input_row(b) (b)+16 ; JDIMENSION input_row
  30. %define output_buf(b) (b)+20 ; JSAMPARRAY output_buf
  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_MMWORD ; mmword wk[WK_NUM]
  34. %define WK_NUM 2
  35. %define gotptr wk(0)-SIZEOF_POINTER ; void * gotptr
  36. align 16
  37. global EXTN(jsimd_ycc_rgb_convert_mmx)
  38. EXTN(jsimd_ycc_rgb_convert_mmx):
  39. push ebp
  40. mov eax,esp ; eax = original ebp
  41. sub esp, byte 4
  42. and esp, byte (-SIZEOF_MMWORD) ; align to 64 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 [out_width(eax)] ; num_cols
  55. test ecx,ecx
  56. jz near .return
  57. push ecx
  58. mov edi, JSAMPIMAGE [input_buf(eax)]
  59. mov ecx, JDIMENSION [input_row(eax)]
  60. mov esi, JSAMPARRAY [edi+0*SIZEOF_JSAMPARRAY]
  61. mov ebx, JSAMPARRAY [edi+1*SIZEOF_JSAMPARRAY]
  62. mov edx, JSAMPARRAY [edi+2*SIZEOF_JSAMPARRAY]
  63. lea esi, [esi+ecx*SIZEOF_JSAMPROW]
  64. lea ebx, [ebx+ecx*SIZEOF_JSAMPROW]
  65. lea edx, [edx+ecx*SIZEOF_JSAMPROW]
  66. pop ecx
  67. mov edi, JSAMPARRAY [output_buf(eax)]
  68. mov eax, INT [num_rows(eax)]
  69. test eax,eax
  70. jle near .return
  71. alignx 16,7
  72. .rowloop:
  73. push eax
  74. push edi
  75. push edx
  76. push ebx
  77. push esi
  78. push ecx ; col
  79. mov esi, JSAMPROW [esi] ; inptr0
  80. mov ebx, JSAMPROW [ebx] ; inptr1
  81. mov edx, JSAMPROW [edx] ; inptr2
  82. mov edi, JSAMPROW [edi] ; outptr
  83. movpic eax, POINTER [gotptr] ; load GOT address (eax)
  84. alignx 16,7
  85. .columnloop:
  86. movq mm5, MMWORD [ebx] ; mm5=Cb(01234567)
  87. movq mm1, MMWORD [edx] ; mm1=Cr(01234567)
  88. pcmpeqw mm4,mm4
  89. pcmpeqw mm7,mm7
  90. psrlw mm4,BYTE_BIT
  91. psllw mm7,7 ; mm7={0xFF80 0xFF80 0xFF80 0xFF80}
  92. movq mm0,mm4 ; mm0=mm4={0xFF 0x00 0xFF 0x00 ..}
  93. pand mm4,mm5 ; mm4=Cb(0246)=CbE
  94. psrlw mm5,BYTE_BIT ; mm5=Cb(1357)=CbO
  95. pand mm0,mm1 ; mm0=Cr(0246)=CrE
  96. psrlw mm1,BYTE_BIT ; mm1=Cr(1357)=CrO
  97. paddw mm4,mm7
  98. paddw mm5,mm7
  99. paddw mm0,mm7
  100. paddw mm1,mm7
  101. ; (Original)
  102. ; R = Y + 1.40200 * Cr
  103. ; G = Y - 0.34414 * Cb - 0.71414 * Cr
  104. ; B = Y + 1.77200 * Cb
  105. ;
  106. ; (This implementation)
  107. ; R = Y + 0.40200 * Cr + Cr
  108. ; G = Y - 0.34414 * Cb + 0.28586 * Cr - Cr
  109. ; B = Y - 0.22800 * Cb + Cb + Cb
  110. movq mm2,mm4 ; mm2=CbE
  111. movq mm3,mm5 ; mm3=CbO
  112. paddw mm4,mm4 ; mm4=2*CbE
  113. paddw mm5,mm5 ; mm5=2*CbO
  114. movq mm6,mm0 ; mm6=CrE
  115. movq mm7,mm1 ; mm7=CrO
  116. paddw mm0,mm0 ; mm0=2*CrE
  117. paddw mm1,mm1 ; mm1=2*CrO
  118. pmulhw mm4,[GOTOFF(eax,PW_MF0228)] ; mm4=(2*CbE * -FIX(0.22800))
  119. pmulhw mm5,[GOTOFF(eax,PW_MF0228)] ; mm5=(2*CbO * -FIX(0.22800))
  120. pmulhw mm0,[GOTOFF(eax,PW_F0402)] ; mm0=(2*CrE * FIX(0.40200))
  121. pmulhw mm1,[GOTOFF(eax,PW_F0402)] ; mm1=(2*CrO * FIX(0.40200))
  122. paddw mm4,[GOTOFF(eax,PW_ONE)]
  123. paddw mm5,[GOTOFF(eax,PW_ONE)]
  124. psraw mm4,1 ; mm4=(CbE * -FIX(0.22800))
  125. psraw mm5,1 ; mm5=(CbO * -FIX(0.22800))
  126. paddw mm0,[GOTOFF(eax,PW_ONE)]
  127. paddw mm1,[GOTOFF(eax,PW_ONE)]
  128. psraw mm0,1 ; mm0=(CrE * FIX(0.40200))
  129. psraw mm1,1 ; mm1=(CrO * FIX(0.40200))
  130. paddw mm4,mm2
  131. paddw mm5,mm3
  132. paddw mm4,mm2 ; mm4=(CbE * FIX(1.77200))=(B-Y)E
  133. paddw mm5,mm3 ; mm5=(CbO * FIX(1.77200))=(B-Y)O
  134. paddw mm0,mm6 ; mm0=(CrE * FIX(1.40200))=(R-Y)E
  135. paddw mm1,mm7 ; mm1=(CrO * FIX(1.40200))=(R-Y)O
  136. movq MMWORD [wk(0)], mm4 ; wk(0)=(B-Y)E
  137. movq MMWORD [wk(1)], mm5 ; wk(1)=(B-Y)O
  138. movq mm4,mm2
  139. movq mm5,mm3
  140. punpcklwd mm2,mm6
  141. punpckhwd mm4,mm6
  142. pmaddwd mm2,[GOTOFF(eax,PW_MF0344_F0285)]
  143. pmaddwd mm4,[GOTOFF(eax,PW_MF0344_F0285)]
  144. punpcklwd mm3,mm7
  145. punpckhwd mm5,mm7
  146. pmaddwd mm3,[GOTOFF(eax,PW_MF0344_F0285)]
  147. pmaddwd mm5,[GOTOFF(eax,PW_MF0344_F0285)]
  148. paddd mm2,[GOTOFF(eax,PD_ONEHALF)]
  149. paddd mm4,[GOTOFF(eax,PD_ONEHALF)]
  150. psrad mm2,SCALEBITS
  151. psrad mm4,SCALEBITS
  152. paddd mm3,[GOTOFF(eax,PD_ONEHALF)]
  153. paddd mm5,[GOTOFF(eax,PD_ONEHALF)]
  154. psrad mm3,SCALEBITS
  155. psrad mm5,SCALEBITS
  156. packssdw mm2,mm4 ; mm2=CbE*-FIX(0.344)+CrE*FIX(0.285)
  157. packssdw mm3,mm5 ; mm3=CbO*-FIX(0.344)+CrO*FIX(0.285)
  158. psubw mm2,mm6 ; mm2=CbE*-FIX(0.344)+CrE*-FIX(0.714)=(G-Y)E
  159. psubw mm3,mm7 ; mm3=CbO*-FIX(0.344)+CrO*-FIX(0.714)=(G-Y)O
  160. movq mm5, MMWORD [esi] ; mm5=Y(01234567)
  161. pcmpeqw mm4,mm4
  162. psrlw mm4,BYTE_BIT ; mm4={0xFF 0x00 0xFF 0x00 ..}
  163. pand mm4,mm5 ; mm4=Y(0246)=YE
  164. psrlw mm5,BYTE_BIT ; mm5=Y(1357)=YO
  165. paddw mm0,mm4 ; mm0=((R-Y)E+YE)=RE=(R0 R2 R4 R6)
  166. paddw mm1,mm5 ; mm1=((R-Y)O+YO)=RO=(R1 R3 R5 R7)
  167. packuswb mm0,mm0 ; mm0=(R0 R2 R4 R6 ** ** ** **)
  168. packuswb mm1,mm1 ; mm1=(R1 R3 R5 R7 ** ** ** **)
  169. paddw mm2,mm4 ; mm2=((G-Y)E+YE)=GE=(G0 G2 G4 G6)
  170. paddw mm3,mm5 ; mm3=((G-Y)O+YO)=GO=(G1 G3 G5 G7)
  171. packuswb mm2,mm2 ; mm2=(G0 G2 G4 G6 ** ** ** **)
  172. packuswb mm3,mm3 ; mm3=(G1 G3 G5 G7 ** ** ** **)
  173. paddw mm4, MMWORD [wk(0)] ; mm4=(YE+(B-Y)E)=BE=(B0 B2 B4 B6)
  174. paddw mm5, MMWORD [wk(1)] ; mm5=(YO+(B-Y)O)=BO=(B1 B3 B5 B7)
  175. packuswb mm4,mm4 ; mm4=(B0 B2 B4 B6 ** ** ** **)
  176. packuswb mm5,mm5 ; mm5=(B1 B3 B5 B7 ** ** ** **)
  177. %if RGB_PIXELSIZE == 3 ; ---------------
  178. ; mmA=(00 02 04 06 ** ** ** **), mmB=(01 03 05 07 ** ** ** **)
  179. ; mmC=(10 12 14 16 ** ** ** **), mmD=(11 13 15 17 ** ** ** **)
  180. ; mmE=(20 22 24 26 ** ** ** **), mmF=(21 23 25 27 ** ** ** **)
  181. ; mmG=(** ** ** ** ** ** ** **), mmH=(** ** ** ** ** ** ** **)
  182. punpcklbw mmA,mmC ; mmA=(00 10 02 12 04 14 06 16)
  183. punpcklbw mmE,mmB ; mmE=(20 01 22 03 24 05 26 07)
  184. punpcklbw mmD,mmF ; mmD=(11 21 13 23 15 25 17 27)
  185. movq mmG,mmA
  186. movq mmH,mmA
  187. punpcklwd mmA,mmE ; mmA=(00 10 20 01 02 12 22 03)
  188. punpckhwd mmG,mmE ; mmG=(04 14 24 05 06 16 26 07)
  189. psrlq mmH,2*BYTE_BIT ; mmH=(02 12 04 14 06 16 -- --)
  190. psrlq mmE,2*BYTE_BIT ; mmE=(22 03 24 05 26 07 -- --)
  191. movq mmC,mmD
  192. movq mmB,mmD
  193. punpcklwd mmD,mmH ; mmD=(11 21 02 12 13 23 04 14)
  194. punpckhwd mmC,mmH ; mmC=(15 25 06 16 17 27 -- --)
  195. psrlq mmB,2*BYTE_BIT ; mmB=(13 23 15 25 17 27 -- --)
  196. movq mmF,mmE
  197. punpcklwd mmE,mmB ; mmE=(22 03 13 23 24 05 15 25)
  198. punpckhwd mmF,mmB ; mmF=(26 07 17 27 -- -- -- --)
  199. punpckldq mmA,mmD ; mmA=(00 10 20 01 11 21 02 12)
  200. punpckldq mmE,mmG ; mmE=(22 03 13 23 04 14 24 05)
  201. punpckldq mmC,mmF ; mmC=(15 25 06 16 26 07 17 27)
  202. cmp ecx, byte SIZEOF_MMWORD
  203. jb short .column_st16
  204. movq MMWORD [edi+0*SIZEOF_MMWORD], mmA
  205. movq MMWORD [edi+1*SIZEOF_MMWORD], mmE
  206. movq MMWORD [edi+2*SIZEOF_MMWORD], mmC
  207. sub ecx, byte SIZEOF_MMWORD
  208. jz short .nextrow
  209. add esi, byte SIZEOF_MMWORD ; inptr0
  210. add ebx, byte SIZEOF_MMWORD ; inptr1
  211. add edx, byte SIZEOF_MMWORD ; inptr2
  212. add edi, byte RGB_PIXELSIZE*SIZEOF_MMWORD ; outptr
  213. jmp near .columnloop
  214. alignx 16,7
  215. .column_st16:
  216. lea ecx, [ecx+ecx*2] ; imul ecx, RGB_PIXELSIZE
  217. cmp ecx, byte 2*SIZEOF_MMWORD
  218. jb short .column_st8
  219. movq MMWORD [edi+0*SIZEOF_MMWORD], mmA
  220. movq MMWORD [edi+1*SIZEOF_MMWORD], mmE
  221. movq mmA,mmC
  222. sub ecx, byte 2*SIZEOF_MMWORD
  223. add edi, byte 2*SIZEOF_MMWORD
  224. jmp short .column_st4
  225. .column_st8:
  226. cmp ecx, byte SIZEOF_MMWORD
  227. jb short .column_st4
  228. movq MMWORD [edi+0*SIZEOF_MMWORD], mmA
  229. movq mmA,mmE
  230. sub ecx, byte SIZEOF_MMWORD
  231. add edi, byte SIZEOF_MMWORD
  232. .column_st4:
  233. movd eax,mmA
  234. cmp ecx, byte SIZEOF_DWORD
  235. jb short .column_st2
  236. mov DWORD [edi+0*SIZEOF_DWORD], eax
  237. psrlq mmA,DWORD_BIT
  238. movd eax,mmA
  239. sub ecx, byte SIZEOF_DWORD
  240. add edi, byte SIZEOF_DWORD
  241. .column_st2:
  242. cmp ecx, byte SIZEOF_WORD
  243. jb short .column_st1
  244. mov WORD [edi+0*SIZEOF_WORD], ax
  245. shr eax,WORD_BIT
  246. sub ecx, byte SIZEOF_WORD
  247. add edi, byte SIZEOF_WORD
  248. .column_st1:
  249. cmp ecx, byte SIZEOF_BYTE
  250. jb short .nextrow
  251. mov BYTE [edi+0*SIZEOF_BYTE], al
  252. %else ; RGB_PIXELSIZE == 4 ; -----------
  253. %ifdef RGBX_FILLER_0XFF
  254. pcmpeqb mm6,mm6 ; mm6=(X0 X2 X4 X6 ** ** ** **)
  255. pcmpeqb mm7,mm7 ; mm7=(X1 X3 X5 X7 ** ** ** **)
  256. %else
  257. pxor mm6,mm6 ; mm6=(X0 X2 X4 X6 ** ** ** **)
  258. pxor mm7,mm7 ; mm7=(X1 X3 X5 X7 ** ** ** **)
  259. %endif
  260. ; mmA=(00 02 04 06 ** ** ** **), mmB=(01 03 05 07 ** ** ** **)
  261. ; mmC=(10 12 14 16 ** ** ** **), mmD=(11 13 15 17 ** ** ** **)
  262. ; mmE=(20 22 24 26 ** ** ** **), mmF=(21 23 25 27 ** ** ** **)
  263. ; mmG=(30 32 34 36 ** ** ** **), mmH=(31 33 35 37 ** ** ** **)
  264. punpcklbw mmA,mmC ; mmA=(00 10 02 12 04 14 06 16)
  265. punpcklbw mmE,mmG ; mmE=(20 30 22 32 24 34 26 36)
  266. punpcklbw mmB,mmD ; mmB=(01 11 03 13 05 15 07 17)
  267. punpcklbw mmF,mmH ; mmF=(21 31 23 33 25 35 27 37)
  268. movq mmC,mmA
  269. punpcklwd mmA,mmE ; mmA=(00 10 20 30 02 12 22 32)
  270. punpckhwd mmC,mmE ; mmC=(04 14 24 34 06 16 26 36)
  271. movq mmG,mmB
  272. punpcklwd mmB,mmF ; mmB=(01 11 21 31 03 13 23 33)
  273. punpckhwd mmG,mmF ; mmG=(05 15 25 35 07 17 27 37)
  274. movq mmD,mmA
  275. punpckldq mmA,mmB ; mmA=(00 10 20 30 01 11 21 31)
  276. punpckhdq mmD,mmB ; mmD=(02 12 22 32 03 13 23 33)
  277. movq mmH,mmC
  278. punpckldq mmC,mmG ; mmC=(04 14 24 34 05 15 25 35)
  279. punpckhdq mmH,mmG ; mmH=(06 16 26 36 07 17 27 37)
  280. cmp ecx, byte SIZEOF_MMWORD
  281. jb short .column_st16
  282. movq MMWORD [edi+0*SIZEOF_MMWORD], mmA
  283. movq MMWORD [edi+1*SIZEOF_MMWORD], mmD
  284. movq MMWORD [edi+2*SIZEOF_MMWORD], mmC
  285. movq MMWORD [edi+3*SIZEOF_MMWORD], mmH
  286. sub ecx, byte SIZEOF_MMWORD
  287. jz short .nextrow
  288. add esi, byte SIZEOF_MMWORD ; inptr0
  289. add ebx, byte SIZEOF_MMWORD ; inptr1
  290. add edx, byte SIZEOF_MMWORD ; inptr2
  291. add edi, byte RGB_PIXELSIZE*SIZEOF_MMWORD ; outptr
  292. jmp near .columnloop
  293. alignx 16,7
  294. .column_st16:
  295. cmp ecx, byte SIZEOF_MMWORD/2
  296. jb short .column_st8
  297. movq MMWORD [edi+0*SIZEOF_MMWORD], mmA
  298. movq MMWORD [edi+1*SIZEOF_MMWORD], mmD
  299. movq mmA,mmC
  300. movq mmD,mmH
  301. sub ecx, byte SIZEOF_MMWORD/2
  302. add edi, byte 2*SIZEOF_MMWORD
  303. .column_st8:
  304. cmp ecx, byte SIZEOF_MMWORD/4
  305. jb short .column_st4
  306. movq MMWORD [edi+0*SIZEOF_MMWORD], mmA
  307. movq mmA,mmD
  308. sub ecx, byte SIZEOF_MMWORD/4
  309. add edi, byte 1*SIZEOF_MMWORD
  310. .column_st4:
  311. cmp ecx, byte SIZEOF_MMWORD/8
  312. jb short .nextrow
  313. movd DWORD [edi+0*SIZEOF_DWORD], mmA
  314. %endif ; RGB_PIXELSIZE ; ---------------
  315. alignx 16,7
  316. .nextrow:
  317. pop ecx
  318. pop esi
  319. pop ebx
  320. pop edx
  321. pop edi
  322. pop eax
  323. add esi, byte SIZEOF_JSAMPROW
  324. add ebx, byte SIZEOF_JSAMPROW
  325. add edx, byte SIZEOF_JSAMPROW
  326. add edi, byte SIZEOF_JSAMPROW ; output_buf
  327. dec eax ; num_rows
  328. jg near .rowloop
  329. emms ; empty MMX state
  330. .return:
  331. pop edi
  332. pop esi
  333. ; pop edx ; need not be preserved
  334. ; pop ecx ; need not be preserved
  335. pop ebx
  336. mov esp,ebp ; esp <- aligned ebp
  337. pop esp ; esp <- original ebp
  338. pop ebp
  339. ret
  340. ; For some reason, the OS X linker does not honor the request to align the
  341. ; segment unless we do this.
  342. align 16