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