2 ; jcgryext.asm - grayscale colorspace conversion (64-bit SSE2)
4 ; Copyright (C) 2011, 2016, D. R. Commander.
5 ; Copyright (C) 2018, Matthias Räncker.
6 ; Copyright (C) 2023, Aliaksiej Kandracienka.
8 ; Based on the x86 SIMD extension for IJG JPEG library
9 ; Copyright (C) 1999-2006, MIYASAKA Masaru.
10 ; For conditions of distribution and use, see copyright notice in jsimdext.inc
12 ; This file should be assembled with NASM (Netwide Assembler),
13 ; can *not* be assembled with Microsoft's MASM or any compatible
14 ; assembler (including Borland's Turbo Assembler).
15 ; NASM is available from http://nasm.sourceforge.net/ or
16 ; http://sourceforge.net/project/showfiles.php?group_id=6208
18 %include "jcolsamp.inc"
20 ; --------------------------------------------------------------------------
22 ; Convert some rows of samples to the output colorspace.
25 ; jsimd_rgb_gray_convert_sse2(JDIMENSION img_width, JSAMPARRAY input_buf,
26 ; JSAMPIMAGE output_buf, JDIMENSION output_row,
30 ; r10d = JDIMENSION img_width
31 ; r11 = JSAMPARRAY input_buf
32 ; r12 = JSAMPIMAGE output_buf
33 ; r13d = JDIMENSION output_row
36 %define wk(i) r15 - (WK_NUM - (i)) * SIZEOF_XMMWORD ; xmmword wk[WK_NUM]
40 GLOBAL_FUNCTION(jsimd_rgb_gray_convert_sse2)
42 EXTN(jsimd_rgb_gray_convert_sse2):
46 and rsp, byte (-SIZEOF_XMMWORD) ; align to 128 bits
47 ; Allocate stack space for wk array. r15 is used to access it.
49 sub rsp, byte (SIZEOF_XMMWORD * WK_NUM)
61 mov rdip, JSAMPARRAY [rsi+0*SIZEOF_JSAMPARRAY]
62 lea rdi, [rdi+rcx*SIZEOF_JSAMPROW]
75 mov rsip, JSAMPROW [rsi] ; inptr
76 mov rdip, JSAMPROW [rdi] ; outptr0
78 cmp rcx, byte SIZEOF_XMMWORD
81 %if RGB_PIXELSIZE == 3 ; ---------------
86 lea rcx, [rcx+rcx*2] ; imul ecx,RGB_PIXELSIZE
89 sub rcx, byte SIZEOF_BYTE
90 movzx rax, byte [rsi+rcx]
94 sub rcx, byte SIZEOF_WORD
95 movzx rdx, word [rsi+rcx]
102 test cl, SIZEOF_DWORD
104 sub rcx, byte SIZEOF_DWORD
105 movd xmmF, XMM_DWORD [rsi+rcx]
106 pslldq xmmA, SIZEOF_DWORD
109 test cl, SIZEOF_MMWORD
110 jz short .column_ld16
111 sub rcx, byte SIZEOF_MMWORD
112 movq xmmB, XMM_MMWORD [rsi+rcx]
113 pslldq xmmA, SIZEOF_MMWORD
116 test cl, SIZEOF_XMMWORD
117 jz short .column_ld32
119 movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
120 mov rcx, SIZEOF_XMMWORD
121 jmp short .rgb_gray_cnv
123 test cl, 2*SIZEOF_XMMWORD
124 mov rcx, SIZEOF_XMMWORD
125 jz short .rgb_gray_cnv
127 movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
128 movdqu xmmF, XMMWORD [rsi+1*SIZEOF_XMMWORD]
129 jmp short .rgb_gray_cnv
132 movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
133 movdqu xmmF, XMMWORD [rsi+1*SIZEOF_XMMWORD]
134 movdqu xmmB, XMMWORD [rsi+2*SIZEOF_XMMWORD]
137 ; xmmA=(00 10 20 01 11 21 02 12 22 03 13 23 04 14 24 05)
138 ; xmmF=(15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A)
139 ; xmmB=(2A 0B 1B 2B 0C 1C 2C 0D 1D 2D 0E 1E 2E 0F 1F 2F)
142 pslldq xmmA, 8 ; xmmA=(-- -- -- -- -- -- -- -- 00 10 20 01 11 21 02 12)
143 psrldq xmmG, 8 ; xmmG=(22 03 13 23 04 14 24 05 -- -- -- -- -- -- -- --)
145 punpckhbw xmmA, xmmF ; xmmA=(00 08 10 18 20 28 01 09 11 19 21 29 02 0A 12 1A)
146 pslldq xmmF, 8 ; xmmF=(-- -- -- -- -- -- -- -- 15 25 06 16 26 07 17 27)
148 punpcklbw xmmG, xmmB ; xmmG=(22 2A 03 0B 13 1B 23 2B 04 0C 14 1C 24 2C 05 0D)
149 punpckhbw xmmF, xmmB ; xmmF=(15 1D 25 2D 06 0E 16 1E 26 2E 07 0F 17 1F 27 2F)
152 pslldq xmmA, 8 ; xmmA=(-- -- -- -- -- -- -- -- 00 08 10 18 20 28 01 09)
153 psrldq xmmD, 8 ; xmmD=(11 19 21 29 02 0A 12 1A -- -- -- -- -- -- -- --)
155 punpckhbw xmmA, xmmG ; xmmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 01 05 09 0D)
156 pslldq xmmG, 8 ; xmmG=(-- -- -- -- -- -- -- -- 22 2A 03 0B 13 1B 23 2B)
158 punpcklbw xmmD, xmmF ; xmmD=(11 15 19 1D 21 25 29 2D 02 06 0A 0E 12 16 1A 1E)
159 punpckhbw xmmG, xmmF ; xmmG=(22 26 2A 2E 03 07 0B 0F 13 17 1B 1F 23 27 2B 2F)
162 pslldq xmmA, 8 ; xmmA=(-- -- -- -- -- -- -- -- 00 04 08 0C 10 14 18 1C)
163 psrldq xmmE, 8 ; xmmE=(20 24 28 2C 01 05 09 0D -- -- -- -- -- -- -- --)
165 punpckhbw xmmA, xmmD ; xmmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E)
166 pslldq xmmD, 8 ; xmmD=(-- -- -- -- -- -- -- -- 11 15 19 1D 21 25 29 2D)
168 punpcklbw xmmE, xmmG ; xmmE=(20 22 24 26 28 2A 2C 2E 01 03 05 07 09 0B 0D 0F)
169 punpckhbw xmmD, xmmG ; xmmD=(11 13 15 17 19 1B 1D 1F 21 23 25 27 29 2B 2D 2F)
174 punpcklbw xmmA, xmmH ; xmmA=(00 02 04 06 08 0A 0C 0E)
175 punpckhbw xmmC, xmmH ; xmmC=(10 12 14 16 18 1A 1C 1E)
178 punpcklbw xmmE, xmmH ; xmmE=(20 22 24 26 28 2A 2C 2E)
179 punpckhbw xmmB, xmmH ; xmmB=(01 03 05 07 09 0B 0D 0F)
182 punpcklbw xmmD, xmmH ; xmmD=(11 13 15 17 19 1B 1D 1F)
183 punpckhbw xmmF, xmmH ; xmmF=(21 23 25 27 29 2B 2D 2F)
185 %else ; RGB_PIXELSIZE == 4 ; -----------
188 test cl, SIZEOF_XMMWORD/16
190 sub rcx, byte SIZEOF_XMMWORD/16
191 movd xmmA, XMM_DWORD [rsi+rcx*RGB_PIXELSIZE]
193 test cl, SIZEOF_XMMWORD/8
195 sub rcx, byte SIZEOF_XMMWORD/8
196 movq xmmE, XMM_MMWORD [rsi+rcx*RGB_PIXELSIZE]
197 pslldq xmmA, SIZEOF_MMWORD
200 test cl, SIZEOF_XMMWORD/4
202 sub rcx, byte SIZEOF_XMMWORD/4
204 movdqu xmmA, XMMWORD [rsi+rcx*RGB_PIXELSIZE]
206 test cl, SIZEOF_XMMWORD/2
207 mov rcx, SIZEOF_XMMWORD
208 jz short .rgb_gray_cnv
211 movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
212 movdqu xmmE, XMMWORD [rsi+1*SIZEOF_XMMWORD]
213 jmp short .rgb_gray_cnv
216 movdqu xmmA, XMMWORD [rsi+0*SIZEOF_XMMWORD]
217 movdqu xmmE, XMMWORD [rsi+1*SIZEOF_XMMWORD]
218 movdqu xmmF, XMMWORD [rsi+2*SIZEOF_XMMWORD]
219 movdqu xmmH, XMMWORD [rsi+3*SIZEOF_XMMWORD]
222 ; xmmA=(00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33)
223 ; xmmE=(04 14 24 34 05 15 25 35 06 16 26 36 07 17 27 37)
224 ; xmmF=(08 18 28 38 09 19 29 39 0A 1A 2A 3A 0B 1B 2B 3B)
225 ; xmmH=(0C 1C 2C 3C 0D 1D 2D 3D 0E 1E 2E 3E 0F 1F 2F 3F)
228 punpcklbw xmmA, xmmE ; xmmA=(00 04 10 14 20 24 30 34 01 05 11 15 21 25 31 35)
229 punpckhbw xmmD, xmmE ; xmmD=(02 06 12 16 22 26 32 36 03 07 13 17 23 27 33 37)
232 punpcklbw xmmF, xmmH ; xmmF=(08 0C 18 1C 28 2C 38 3C 09 0D 19 1D 29 2D 39 3D)
233 punpckhbw xmmC, xmmH ; xmmC=(0A 0E 1A 1E 2A 2E 3A 3E 0B 0F 1B 1F 2B 2F 3B 3F)
236 punpcklwd xmmA, xmmF ; xmmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C)
237 punpckhwd xmmB, xmmF ; xmmB=(01 05 09 0D 11 15 19 1D 21 25 29 2D 31 35 39 3D)
240 punpcklwd xmmD, xmmC ; xmmD=(02 06 0A 0E 12 16 1A 1E 22 26 2A 2E 32 36 3A 3E)
241 punpckhwd xmmG, xmmC ; xmmG=(03 07 0B 0F 13 17 1B 1F 23 27 2B 2F 33 37 3B 3F)
244 punpcklbw xmmA, xmmD ; xmmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E)
245 punpckhbw xmmE, xmmD ; xmmE=(20 22 24 26 28 2A 2C 2E 30 32 34 36 38 3A 3C 3E)
248 punpcklbw xmmB, xmmG ; xmmB=(01 03 05 07 09 0B 0D 0F 11 13 15 17 19 1B 1D 1F)
249 punpckhbw xmmH, xmmG ; xmmH=(21 23 25 27 29 2B 2D 2F 31 33 35 37 39 3B 3D 3F)
254 punpcklbw xmmA, xmmF ; xmmA=(00 02 04 06 08 0A 0C 0E)
255 punpckhbw xmmC, xmmF ; xmmC=(10 12 14 16 18 1A 1C 1E)
258 punpcklbw xmmB, xmmF ; xmmB=(01 03 05 07 09 0B 0D 0F)
259 punpckhbw xmmD, xmmF ; xmmD=(11 13 15 17 19 1B 1D 1F)
262 punpcklbw xmmE, xmmF ; xmmE=(20 22 24 26 28 2A 2C 2E)
263 punpckhbw xmmG, xmmF ; xmmG=(30 32 34 36 38 3A 3C 3E)
267 psrlw xmmF, BYTE_BIT ; xmmF=(21 23 25 27 29 2B 2D 2F)
268 psrlw xmmH, BYTE_BIT ; xmmH=(31 33 35 37 39 3B 3D 3F)
270 %endif ; RGB_PIXELSIZE ; ---------------
272 ; xmm0=R(02468ACE)=RE, xmm2=G(02468ACE)=GE, xmm4=B(02468ACE)=BE
273 ; xmm1=R(13579BDF)=RO, xmm3=G(13579BDF)=GO, xmm5=B(13579BDF)=BO
276 ; Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
278 ; (This implementation)
279 ; Y = 0.29900 * R + 0.33700 * G + 0.11400 * B + 0.25000 * G
284 pmaddwd xmm1, [rel PW_F0299_F0337] ; xmm1=ROL*FIX(0.299)+GOL*FIX(0.337)
285 pmaddwd xmm6, [rel PW_F0299_F0337] ; xmm6=ROH*FIX(0.299)+GOH*FIX(0.337)
287 movdqa xmm7, xmm6 ; xmm7=ROH*FIX(0.299)+GOH*FIX(0.337)
292 pmaddwd xmm0, [rel PW_F0299_F0337] ; xmm0=REL*FIX(0.299)+GEL*FIX(0.337)
293 pmaddwd xmm6, [rel PW_F0299_F0337] ; xmm6=REH*FIX(0.299)+GEH*FIX(0.337)
295 movdqa XMMWORD [wk(0)], xmm0 ; wk(0)=REL*FIX(0.299)+GEL*FIX(0.337)
296 movdqa XMMWORD [wk(1)], xmm6 ; wk(1)=REH*FIX(0.299)+GEH*FIX(0.337)
298 movdqa xmm0, xmm5 ; xmm0=BO
299 movdqa xmm6, xmm4 ; xmm6=BE
304 pmaddwd xmm0, [rel PW_F0114_F0250] ; xmm0=BOL*FIX(0.114)+GOL*FIX(0.250)
305 pmaddwd xmm4, [rel PW_F0114_F0250] ; xmm4=BOH*FIX(0.114)+GOH*FIX(0.250)
307 movdqa xmm3, [rel PD_ONEHALF] ; xmm3=[PD_ONEHALF]
313 psrld xmm0, SCALEBITS ; xmm0=YOL
314 psrld xmm4, SCALEBITS ; xmm4=YOH
315 packssdw xmm0, xmm4 ; xmm0=YO
320 pmaddwd xmm6, [rel PW_F0114_F0250] ; xmm6=BEL*FIX(0.114)+GEL*FIX(0.250)
321 pmaddwd xmm4, [rel PW_F0114_F0250] ; xmm4=BEH*FIX(0.114)+GEH*FIX(0.250)
323 movdqa xmm2, [rel PD_ONEHALF] ; xmm2=[PD_ONEHALF]
325 paddd xmm6, XMMWORD [wk(0)]
326 paddd xmm4, XMMWORD [wk(1)]
329 psrld xmm6, SCALEBITS ; xmm6=YEL
330 psrld xmm4, SCALEBITS ; xmm4=YEH
331 packssdw xmm6, xmm4 ; xmm6=YE
334 por xmm6, xmm0 ; xmm6=Y
335 movdqa XMMWORD [rdi], xmm6 ; Save Y
337 sub rcx, byte SIZEOF_XMMWORD
338 add rsi, byte RGB_PIXELSIZE*SIZEOF_XMMWORD ; inptr
339 add rdi, byte SIZEOF_XMMWORD ; outptr0
340 cmp rcx, byte SIZEOF_XMMWORD
349 add rsi, byte SIZEOF_JSAMPROW ; input_buf
350 add rdi, byte SIZEOF_JSAMPROW
362 ; For some reason, the OS X linker does not honor the request to align the
363 ; segment unless we do this.