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