2 ; jcgryext.asm - grayscale colorspace conversion (MMX)
4 ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5 ; Copyright (C) 2011, 2016, D. R. Commander.
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
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
19 %include "jcolsamp.inc"
21 ; --------------------------------------------------------------------------
23 ; Convert some rows of samples to the output colorspace.
26 ; jsimd_rgb_gray_convert_mmx(JDIMENSION img_width, JSAMPARRAY input_buf,
27 ; JSAMPIMAGE output_buf, JDIMENSION output_row,
31 %define img_width(b) (b) + 8 ; JDIMENSION img_width
32 %define input_buf(b) (b) + 12 ; JSAMPARRAY input_buf
33 %define output_buf(b) (b) + 16 ; JSAMPIMAGE output_buf
34 %define output_row(b) (b) + 20 ; JDIMENSION output_row
35 %define num_rows(b) (b) + 24 ; int num_rows
37 %define original_ebp ebp + 0
38 %define wk(i) ebp - (WK_NUM - (i)) * SIZEOF_MMWORD
41 %define gotptr wk(0) - SIZEOF_POINTER ; void * gotptr
44 GLOBAL_FUNCTION(jsimd_rgb_gray_convert_mmx)
46 EXTN(jsimd_rgb_gray_convert_mmx):
48 mov eax, esp ; eax = original ebp
50 and esp, byte (-SIZEOF_MMWORD) ; align to 64 bits
52 mov ebp, esp ; ebp = aligned ebp
54 pushpic eax ; make a room for GOT address
56 ; push ecx ; need not be preserved
57 ; push edx ; need not be preserved
61 get_GOT ebx ; get GOT address
62 movpic POINTER [gotptr], ebx ; save GOT address
64 mov ecx, JDIMENSION [img_width(eax)] ; num_cols
70 mov esi, JSAMPIMAGE [output_buf(eax)]
71 mov ecx, JDIMENSION [output_row(eax)]
72 mov edi, JSAMPARRAY [esi+0*SIZEOF_JSAMPARRAY]
73 lea edi, [edi+ecx*SIZEOF_JSAMPROW]
77 mov esi, JSAMPARRAY [input_buf(eax)]
78 mov eax, INT [num_rows(eax)]
88 mov esi, JSAMPROW [esi] ; inptr
89 mov edi, JSAMPROW [edi] ; outptr0
90 movpic eax, POINTER [gotptr] ; load GOT address (eax)
92 cmp ecx, byte SIZEOF_MMWORD
96 %if RGB_PIXELSIZE == 3 ; ---------------
101 lea ecx, [ecx+ecx*2] ; imul ecx,RGB_PIXELSIZE
104 sub ecx, byte SIZEOF_BYTE
106 mov al, BYTE [esi+ecx]
110 sub ecx, byte SIZEOF_WORD
112 mov dx, WORD [esi+ecx]
119 test cl, SIZEOF_DWORD
121 sub ecx, byte SIZEOF_DWORD
122 movd mmG, DWORD [esi+ecx]
126 test cl, SIZEOF_MMWORD
127 jz short .column_ld16
129 movq mmA, MMWORD [esi+0*SIZEOF_MMWORD]
130 mov ecx, SIZEOF_MMWORD
131 jmp short .rgb_gray_cnv
133 test cl, 2*SIZEOF_MMWORD
134 mov ecx, SIZEOF_MMWORD
135 jz short .rgb_gray_cnv
137 movq mmA, MMWORD [esi+0*SIZEOF_MMWORD]
138 movq mmG, MMWORD [esi+1*SIZEOF_MMWORD]
139 jmp short .rgb_gray_cnv
143 movq mmA, MMWORD [esi+0*SIZEOF_MMWORD]
144 movq mmG, MMWORD [esi+1*SIZEOF_MMWORD]
145 movq mmF, MMWORD [esi+2*SIZEOF_MMWORD]
148 ; mmA=(00 10 20 01 11 21 02 12)
149 ; mmG=(22 03 13 23 04 14 24 05)
150 ; mmF=(15 25 06 16 26 07 17 27)
153 psllq mmA, 4*BYTE_BIT ; mmA=(-- -- -- -- 00 10 20 01)
154 psrlq mmD, 4*BYTE_BIT ; mmD=(11 21 02 12 -- -- -- --)
156 punpckhbw mmA, mmG ; mmA=(00 04 10 14 20 24 01 05)
157 psllq mmG, 4*BYTE_BIT ; mmG=(-- -- -- -- 22 03 13 23)
159 punpcklbw mmD, mmF ; mmD=(11 15 21 25 02 06 12 16)
160 punpckhbw mmG, mmF ; mmG=(22 26 03 07 13 17 23 27)
163 psllq mmA, 4*BYTE_BIT ; mmA=(-- -- -- -- 00 04 10 14)
164 psrlq mmE, 4*BYTE_BIT ; mmE=(20 24 01 05 -- -- -- --)
166 punpckhbw mmA, mmD ; mmA=(00 02 04 06 10 12 14 16)
167 psllq mmD, 4*BYTE_BIT ; mmD=(-- -- -- -- 11 15 21 25)
169 punpcklbw mmE, mmG ; mmE=(20 22 24 26 01 03 05 07)
170 punpckhbw mmD, mmG ; mmD=(11 13 15 17 21 23 25 27)
175 punpcklbw mmA, mmH ; mmA=(00 02 04 06)
176 punpckhbw mmC, mmH ; mmC=(10 12 14 16)
179 punpcklbw mmE, mmH ; mmE=(20 22 24 26)
180 punpckhbw mmB, mmH ; mmB=(01 03 05 07)
183 punpcklbw mmD, mmH ; mmD=(11 13 15 17)
184 punpckhbw mmF, mmH ; mmF=(21 23 25 27)
186 %else ; RGB_PIXELSIZE == 4 ; -----------
189 test cl, SIZEOF_MMWORD/8
191 sub ecx, byte SIZEOF_MMWORD/8
192 movd mmA, DWORD [esi+ecx*RGB_PIXELSIZE]
194 test cl, SIZEOF_MMWORD/4
196 sub ecx, byte SIZEOF_MMWORD/4
198 movq mmA, MMWORD [esi+ecx*RGB_PIXELSIZE]
200 test cl, SIZEOF_MMWORD/2
201 mov ecx, SIZEOF_MMWORD
202 jz short .rgb_gray_cnv
205 movq mmA, MMWORD [esi+0*SIZEOF_MMWORD]
206 movq mmF, MMWORD [esi+1*SIZEOF_MMWORD]
207 jmp short .rgb_gray_cnv
211 movq mmA, MMWORD [esi+0*SIZEOF_MMWORD]
212 movq mmF, MMWORD [esi+1*SIZEOF_MMWORD]
213 movq mmD, MMWORD [esi+2*SIZEOF_MMWORD]
214 movq mmC, MMWORD [esi+3*SIZEOF_MMWORD]
217 ; mmA=(00 10 20 30 01 11 21 31)
218 ; mmF=(02 12 22 32 03 13 23 33)
219 ; mmD=(04 14 24 34 05 15 25 35)
220 ; mmC=(06 16 26 36 07 17 27 37)
223 punpcklbw mmA, mmF ; mmA=(00 02 10 12 20 22 30 32)
224 punpckhbw mmB, mmF ; mmB=(01 03 11 13 21 23 31 33)
227 punpcklbw mmD, mmC ; mmD=(04 06 14 16 24 26 34 36)
228 punpckhbw mmG, mmC ; mmG=(05 07 15 17 25 27 35 37)
231 punpcklwd mmA, mmD ; mmA=(00 02 04 06 10 12 14 16)
232 punpckhwd mmE, mmD ; mmE=(20 22 24 26 30 32 34 36)
235 punpcklwd mmB, mmG ; mmB=(01 03 05 07 11 13 15 17)
236 punpckhwd mmH, mmG ; mmH=(21 23 25 27 31 33 35 37)
241 punpcklbw mmA, mmF ; mmA=(00 02 04 06)
242 punpckhbw mmC, mmF ; mmC=(10 12 14 16)
245 punpcklbw mmB, mmF ; mmB=(01 03 05 07)
246 punpckhbw mmD, mmF ; mmD=(11 13 15 17)
249 punpcklbw mmE, mmF ; mmE=(20 22 24 26)
250 punpckhbw mmG, mmF ; mmG=(30 32 34 36)
254 psrlw mmF, BYTE_BIT ; mmF=(21 23 25 27)
255 psrlw mmH, BYTE_BIT ; mmH=(31 33 35 37)
257 %endif ; RGB_PIXELSIZE ; ---------------
259 ; mm0=(R0 R2 R4 R6)=RE, mm2=(G0 G2 G4 G6)=GE, mm4=(B0 B2 B4 B6)=BE
260 ; mm1=(R1 R3 R5 R7)=RO, mm3=(G1 G3 G5 G7)=GO, mm5=(B1 B3 B5 B7)=BO
263 ; Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
265 ; (This implementation)
266 ; Y = 0.29900 * R + 0.33700 * G + 0.11400 * B + 0.25000 * G
271 pmaddwd mm1, [GOTOFF(eax,PW_F0299_F0337)] ; mm1=ROL*FIX(0.299)+GOL*FIX(0.337)
272 pmaddwd mm6, [GOTOFF(eax,PW_F0299_F0337)] ; mm6=ROH*FIX(0.299)+GOH*FIX(0.337)
274 movq mm7, mm6 ; mm7=ROH*FIX(0.299)+GOH*FIX(0.337)
279 pmaddwd mm0, [GOTOFF(eax,PW_F0299_F0337)] ; mm0=REL*FIX(0.299)+GEL*FIX(0.337)
280 pmaddwd mm6, [GOTOFF(eax,PW_F0299_F0337)] ; mm6=REH*FIX(0.299)+GEH*FIX(0.337)
282 movq MMWORD [wk(0)], mm0 ; wk(0)=REL*FIX(0.299)+GEL*FIX(0.337)
283 movq MMWORD [wk(1)], mm6 ; wk(1)=REH*FIX(0.299)+GEH*FIX(0.337)
285 movq mm0, mm5 ; mm0=BO
286 movq mm6, mm4 ; mm6=BE
291 pmaddwd mm0, [GOTOFF(eax,PW_F0114_F0250)] ; mm0=BOL*FIX(0.114)+GOL*FIX(0.250)
292 pmaddwd mm4, [GOTOFF(eax,PW_F0114_F0250)] ; mm4=BOH*FIX(0.114)+GOH*FIX(0.250)
294 movq mm3, [GOTOFF(eax,PD_ONEHALF)] ; mm3=[PD_ONEHALF]
300 psrld mm0, SCALEBITS ; mm0=YOL
301 psrld mm4, SCALEBITS ; mm4=YOH
302 packssdw mm0, mm4 ; mm0=YO
307 pmaddwd mm6, [GOTOFF(eax,PW_F0114_F0250)] ; mm6=BEL*FIX(0.114)+GEL*FIX(0.250)
308 pmaddwd mm4, [GOTOFF(eax,PW_F0114_F0250)] ; mm4=BEH*FIX(0.114)+GEH*FIX(0.250)
310 movq mm2, [GOTOFF(eax,PD_ONEHALF)] ; mm2=[PD_ONEHALF]
312 paddd mm6, MMWORD [wk(0)]
313 paddd mm4, MMWORD [wk(1)]
316 psrld mm6, SCALEBITS ; mm6=YEL
317 psrld mm4, SCALEBITS ; mm4=YEH
318 packssdw mm6, mm4 ; mm6=YE
322 movq MMWORD [edi], mm6 ; Save Y
324 sub ecx, byte SIZEOF_MMWORD
325 add esi, byte RGB_PIXELSIZE*SIZEOF_MMWORD ; inptr
326 add edi, byte SIZEOF_MMWORD ; outptr0
327 cmp ecx, byte SIZEOF_MMWORD
337 add esi, byte SIZEOF_JSAMPROW ; input_buf
338 add edi, byte SIZEOF_JSAMPROW
342 emms ; empty MMX state
347 ; pop edx ; need not be preserved
348 ; pop ecx ; need not be preserved
350 mov esp, ebp ; esp <- aligned ebp
351 pop esp ; esp <- original ebp
355 ; For some reason, the OS X linker does not honor the request to align the
356 ; segment unless we do this.