jcgryext-mmx.asm 12 KB

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