1;
2; jdmrgext.asm - merged upsampling/color conversion (SSE2)
3;
4; Copyright 2009, 2012 Pierre Ossman <ossman@cendio.se> for Cendio AB
5; Copyright 2012 D. R. Commander
6;
7; Based on
8; 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
11;
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
17;
18; [TAB8]
19
20%include "jcolsamp.inc"
21
22; --------------------------------------------------------------------------
23;
24; Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
25;
26; GLOBAL(void)
27; jsimd_h2v1_merged_upsample_sse2 (JDIMENSION output_width,
28;                                  JSAMPIMAGE input_buf,
29;                                  JDIMENSION in_row_group_ctr,
30;                                  JSAMPARRAY output_buf);
31;
32
33%define output_width(b) (b)+8                   ; JDIMENSION output_width
34%define input_buf(b)            (b)+12          ; JSAMPIMAGE input_buf
35%define in_row_group_ctr(b)     (b)+16          ; JDIMENSION in_row_group_ctr
36%define output_buf(b)           (b)+20          ; JSAMPARRAY output_buf
37
38%define original_ebp    ebp+0
39%define wk(i)           ebp-(WK_NUM-(i))*SIZEOF_XMMWORD ; xmmword wk[WK_NUM]
40%define WK_NUM          3
41%define gotptr          wk(0)-SIZEOF_POINTER    ; void * gotptr
42
43        align   16
44        global  EXTN(jsimd_h2v1_merged_upsample_sse2)
45
46EXTN(jsimd_h2v1_merged_upsample_sse2):
47        push    ebp
48        mov     eax,esp                         ; eax = original ebp
49        sub     esp, byte 4
50        and     esp, byte (-SIZEOF_XMMWORD)     ; align to 128 bits
51        mov     [esp],eax
52        mov     ebp,esp                         ; ebp = aligned ebp
53        lea     esp, [wk(0)]
54        pushpic eax             ; make a room for GOT address
55        push    ebx
56;       push    ecx             ; need not be preserved
57;       push    edx             ; need not be preserved
58        push    esi
59        push    edi
60
61        get_GOT ebx                     ; get GOT address
62        movpic  POINTER [gotptr], ebx   ; save GOT address
63
64        mov     ecx, JDIMENSION [output_width(eax)]     ; col
65        test    ecx,ecx
66        jz      near .return
67
68        push    ecx
69
70        mov     edi, JSAMPIMAGE [input_buf(eax)]
71        mov     ecx, JDIMENSION [in_row_group_ctr(eax)]
72        mov     esi, JSAMPARRAY [edi+0*SIZEOF_JSAMPARRAY]
73        mov     ebx, JSAMPARRAY [edi+1*SIZEOF_JSAMPARRAY]
74        mov     edx, JSAMPARRAY [edi+2*SIZEOF_JSAMPARRAY]
75        mov     edi, JSAMPARRAY [output_buf(eax)]
76        mov     esi, JSAMPROW [esi+ecx*SIZEOF_JSAMPROW]         ; inptr0
77        mov     ebx, JSAMPROW [ebx+ecx*SIZEOF_JSAMPROW]         ; inptr1
78        mov     edx, JSAMPROW [edx+ecx*SIZEOF_JSAMPROW]         ; inptr2
79        mov     edi, JSAMPROW [edi]                             ; outptr
80
81        pop     ecx                     ; col
82
83        alignx  16,7
84.columnloop:
85        movpic  eax, POINTER [gotptr]   ; load GOT address (eax)
86
87        movdqa    xmm6, XMMWORD [ebx]   ; xmm6=Cb(0123456789ABCDEF)
88        movdqa    xmm7, XMMWORD [edx]   ; xmm7=Cr(0123456789ABCDEF)
89
90        pxor      xmm1,xmm1             ; xmm1=(all 0's)
91        pcmpeqw   xmm3,xmm3
92        psllw     xmm3,7                ; xmm3={0xFF80 0xFF80 0xFF80 0xFF80 ..}
93
94        movdqa    xmm4,xmm6
95        punpckhbw xmm6,xmm1             ; xmm6=Cb(89ABCDEF)=CbH
96        punpcklbw xmm4,xmm1             ; xmm4=Cb(01234567)=CbL
97        movdqa    xmm0,xmm7
98        punpckhbw xmm7,xmm1             ; xmm7=Cr(89ABCDEF)=CrH
99        punpcklbw xmm0,xmm1             ; xmm0=Cr(01234567)=CrL
100
101        paddw     xmm6,xmm3
102        paddw     xmm4,xmm3
103        paddw     xmm7,xmm3
104        paddw     xmm0,xmm3
105
106        ; (Original)
107        ; R = Y                + 1.40200 * Cr
108        ; G = Y - 0.34414 * Cb - 0.71414 * Cr
109        ; B = Y + 1.77200 * Cb
110        ;
111        ; (This implementation)
112        ; R = Y                + 0.40200 * Cr + Cr
113        ; G = Y - 0.34414 * Cb + 0.28586 * Cr - Cr
114        ; B = Y - 0.22800 * Cb + Cb + Cb
115
116        movdqa  xmm5,xmm6               ; xmm5=CbH
117        movdqa  xmm2,xmm4               ; xmm2=CbL
118        paddw   xmm6,xmm6               ; xmm6=2*CbH
119        paddw   xmm4,xmm4               ; xmm4=2*CbL
120        movdqa  xmm1,xmm7               ; xmm1=CrH
121        movdqa  xmm3,xmm0               ; xmm3=CrL
122        paddw   xmm7,xmm7               ; xmm7=2*CrH
123        paddw   xmm0,xmm0               ; xmm0=2*CrL
124
125        pmulhw  xmm6,[GOTOFF(eax,PW_MF0228)]    ; xmm6=(2*CbH * -FIX(0.22800))
126        pmulhw  xmm4,[GOTOFF(eax,PW_MF0228)]    ; xmm4=(2*CbL * -FIX(0.22800))
127        pmulhw  xmm7,[GOTOFF(eax,PW_F0402)]     ; xmm7=(2*CrH * FIX(0.40200))
128        pmulhw  xmm0,[GOTOFF(eax,PW_F0402)]     ; xmm0=(2*CrL * FIX(0.40200))
129
130        paddw   xmm6,[GOTOFF(eax,PW_ONE)]
131        paddw   xmm4,[GOTOFF(eax,PW_ONE)]
132        psraw   xmm6,1                  ; xmm6=(CbH * -FIX(0.22800))
133        psraw   xmm4,1                  ; xmm4=(CbL * -FIX(0.22800))
134        paddw   xmm7,[GOTOFF(eax,PW_ONE)]
135        paddw   xmm0,[GOTOFF(eax,PW_ONE)]
136        psraw   xmm7,1                  ; xmm7=(CrH * FIX(0.40200))
137        psraw   xmm0,1                  ; xmm0=(CrL * FIX(0.40200))
138
139        paddw   xmm6,xmm5
140        paddw   xmm4,xmm2
141        paddw   xmm6,xmm5               ; xmm6=(CbH * FIX(1.77200))=(B-Y)H
142        paddw   xmm4,xmm2               ; xmm4=(CbL * FIX(1.77200))=(B-Y)L
143        paddw   xmm7,xmm1               ; xmm7=(CrH * FIX(1.40200))=(R-Y)H
144        paddw   xmm0,xmm3               ; xmm0=(CrL * FIX(1.40200))=(R-Y)L
145
146        movdqa  XMMWORD [wk(0)], xmm6   ; wk(0)=(B-Y)H
147        movdqa  XMMWORD [wk(1)], xmm7   ; wk(1)=(R-Y)H
148
149        movdqa    xmm6,xmm5
150        movdqa    xmm7,xmm2
151        punpcklwd xmm5,xmm1
152        punpckhwd xmm6,xmm1
153        pmaddwd   xmm5,[GOTOFF(eax,PW_MF0344_F0285)]
154        pmaddwd   xmm6,[GOTOFF(eax,PW_MF0344_F0285)]
155        punpcklwd xmm2,xmm3
156        punpckhwd xmm7,xmm3
157        pmaddwd   xmm2,[GOTOFF(eax,PW_MF0344_F0285)]
158        pmaddwd   xmm7,[GOTOFF(eax,PW_MF0344_F0285)]
159
160        paddd     xmm5,[GOTOFF(eax,PD_ONEHALF)]
161        paddd     xmm6,[GOTOFF(eax,PD_ONEHALF)]
162        psrad     xmm5,SCALEBITS
163        psrad     xmm6,SCALEBITS
164        paddd     xmm2,[GOTOFF(eax,PD_ONEHALF)]
165        paddd     xmm7,[GOTOFF(eax,PD_ONEHALF)]
166        psrad     xmm2,SCALEBITS
167        psrad     xmm7,SCALEBITS
168
169        packssdw  xmm5,xmm6     ; xmm5=CbH*-FIX(0.344)+CrH*FIX(0.285)
170        packssdw  xmm2,xmm7     ; xmm2=CbL*-FIX(0.344)+CrL*FIX(0.285)
171        psubw     xmm5,xmm1     ; xmm5=CbH*-FIX(0.344)+CrH*-FIX(0.714)=(G-Y)H
172        psubw     xmm2,xmm3     ; xmm2=CbL*-FIX(0.344)+CrL*-FIX(0.714)=(G-Y)L
173
174        movdqa  XMMWORD [wk(2)], xmm5   ; wk(2)=(G-Y)H
175
176        mov     al,2                    ; Yctr
177        jmp     short .Yloop_1st
178        alignx  16,7
179
180.Yloop_2nd:
181        movdqa  xmm0, XMMWORD [wk(1)]   ; xmm0=(R-Y)H
182        movdqa  xmm2, XMMWORD [wk(2)]   ; xmm2=(G-Y)H
183        movdqa  xmm4, XMMWORD [wk(0)]   ; xmm4=(B-Y)H
184        alignx  16,7
185
186.Yloop_1st:
187        movdqa  xmm7, XMMWORD [esi]     ; xmm7=Y(0123456789ABCDEF)
188
189        pcmpeqw xmm6,xmm6
190        psrlw   xmm6,BYTE_BIT           ; xmm6={0xFF 0x00 0xFF 0x00 ..}
191        pand    xmm6,xmm7               ; xmm6=Y(02468ACE)=YE
192        psrlw   xmm7,BYTE_BIT           ; xmm7=Y(13579BDF)=YO
193
194        movdqa  xmm1,xmm0               ; xmm1=xmm0=(R-Y)(L/H)
195        movdqa  xmm3,xmm2               ; xmm3=xmm2=(G-Y)(L/H)
196        movdqa  xmm5,xmm4               ; xmm5=xmm4=(B-Y)(L/H)
197
198        paddw     xmm0,xmm6             ; xmm0=((R-Y)+YE)=RE=R(02468ACE)
199        paddw     xmm1,xmm7             ; xmm1=((R-Y)+YO)=RO=R(13579BDF)
200        packuswb  xmm0,xmm0             ; xmm0=R(02468ACE********)
201        packuswb  xmm1,xmm1             ; xmm1=R(13579BDF********)
202
203        paddw     xmm2,xmm6             ; xmm2=((G-Y)+YE)=GE=G(02468ACE)
204        paddw     xmm3,xmm7             ; xmm3=((G-Y)+YO)=GO=G(13579BDF)
205        packuswb  xmm2,xmm2             ; xmm2=G(02468ACE********)
206        packuswb  xmm3,xmm3             ; xmm3=G(13579BDF********)
207
208        paddw     xmm4,xmm6             ; xmm4=((B-Y)+YE)=BE=B(02468ACE)
209        paddw     xmm5,xmm7             ; xmm5=((B-Y)+YO)=BO=B(13579BDF)
210        packuswb  xmm4,xmm4             ; xmm4=B(02468ACE********)
211        packuswb  xmm5,xmm5             ; xmm5=B(13579BDF********)
212
213%if RGB_PIXELSIZE == 3 ; ---------------
214
215        ; xmmA=(00 02 04 06 08 0A 0C 0E **), xmmB=(01 03 05 07 09 0B 0D 0F **)
216        ; xmmC=(10 12 14 16 18 1A 1C 1E **), xmmD=(11 13 15 17 19 1B 1D 1F **)
217        ; xmmE=(20 22 24 26 28 2A 2C 2E **), xmmF=(21 23 25 27 29 2B 2D 2F **)
218        ; xmmG=(** ** ** ** ** ** ** ** **), xmmH=(** ** ** ** ** ** ** ** **)
219
220        punpcklbw xmmA,xmmC     ; xmmA=(00 10 02 12 04 14 06 16 08 18 0A 1A 0C 1C 0E 1E)
221        punpcklbw xmmE,xmmB     ; xmmE=(20 01 22 03 24 05 26 07 28 09 2A 0B 2C 0D 2E 0F)
222        punpcklbw xmmD,xmmF     ; xmmD=(11 21 13 23 15 25 17 27 19 29 1B 2B 1D 2D 1F 2F)
223
224        movdqa    xmmG,xmmA
225        movdqa    xmmH,xmmA
226        punpcklwd xmmA,xmmE     ; xmmA=(00 10 20 01 02 12 22 03 04 14 24 05 06 16 26 07)
227        punpckhwd xmmG,xmmE     ; xmmG=(08 18 28 09 0A 1A 2A 0B 0C 1C 2C 0D 0E 1E 2E 0F)
228
229        psrldq    xmmH,2        ; xmmH=(02 12 04 14 06 16 08 18 0A 1A 0C 1C 0E 1E -- --)
230        psrldq    xmmE,2        ; xmmE=(22 03 24 05 26 07 28 09 2A 0B 2C 0D 2E 0F -- --)
231
232        movdqa    xmmC,xmmD
233        movdqa    xmmB,xmmD
234        punpcklwd xmmD,xmmH     ; xmmD=(11 21 02 12 13 23 04 14 15 25 06 16 17 27 08 18)
235        punpckhwd xmmC,xmmH     ; xmmC=(19 29 0A 1A 1B 2B 0C 1C 1D 2D 0E 1E 1F 2F -- --)
236
237        psrldq    xmmB,2        ; xmmB=(13 23 15 25 17 27 19 29 1B 2B 1D 2D 1F 2F -- --)
238
239        movdqa    xmmF,xmmE
240        punpcklwd xmmE,xmmB     ; xmmE=(22 03 13 23 24 05 15 25 26 07 17 27 28 09 19 29)
241        punpckhwd xmmF,xmmB     ; xmmF=(2A 0B 1B 2B 2C 0D 1D 2D 2E 0F 1F 2F -- -- -- --)
242
243        pshufd    xmmH,xmmA,0x4E; xmmH=(04 14 24 05 06 16 26 07 00 10 20 01 02 12 22 03)
244        movdqa    xmmB,xmmE
245        punpckldq xmmA,xmmD     ; xmmA=(00 10 20 01 11 21 02 12 02 12 22 03 13 23 04 14)
246        punpckldq xmmE,xmmH     ; xmmE=(22 03 13 23 04 14 24 05 24 05 15 25 06 16 26 07)
247        punpckhdq xmmD,xmmB     ; xmmD=(15 25 06 16 26 07 17 27 17 27 08 18 28 09 19 29)
248
249        pshufd    xmmH,xmmG,0x4E; xmmH=(0C 1C 2C 0D 0E 1E 2E 0F 08 18 28 09 0A 1A 2A 0B)
250        movdqa    xmmB,xmmF
251        punpckldq xmmG,xmmC     ; xmmG=(08 18 28 09 19 29 0A 1A 0A 1A 2A 0B 1B 2B 0C 1C)
252        punpckldq xmmF,xmmH     ; xmmF=(2A 0B 1B 2B 0C 1C 2C 0D 2C 0D 1D 2D 0E 1E 2E 0F)
253        punpckhdq xmmC,xmmB     ; xmmC=(1D 2D 0E 1E 2E 0F 1F 2F 1F 2F -- -- -- -- -- --)
254
255        punpcklqdq xmmA,xmmE    ; xmmA=(00 10 20 01 11 21 02 12 22 03 13 23 04 14 24 05)
256        punpcklqdq xmmD,xmmG    ; xmmD=(15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A)
257        punpcklqdq xmmF,xmmC    ; xmmF=(2A 0B 1B 2B 0C 1C 2C 0D 1D 2D 0E 1E 2E 0F 1F 2F)
258
259        cmp     ecx, byte SIZEOF_XMMWORD
260        jb      short .column_st32
261
262        test    edi, SIZEOF_XMMWORD-1
263        jnz     short .out1
264        ; --(aligned)-------------------
265        movntdq XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
266        movntdq XMMWORD [edi+1*SIZEOF_XMMWORD], xmmD
267        movntdq XMMWORD [edi+2*SIZEOF_XMMWORD], xmmF
268        jmp     short .out0
269.out1:  ; --(unaligned)-----------------
270        movdqu  XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
271        movdqu  XMMWORD [edi+1*SIZEOF_XMMWORD], xmmD
272        movdqu  XMMWORD [edi+2*SIZEOF_XMMWORD], xmmF
273.out0:
274        add     edi, byte RGB_PIXELSIZE*SIZEOF_XMMWORD  ; outptr
275        sub     ecx, byte SIZEOF_XMMWORD
276        jz      near .endcolumn
277
278        add     esi, byte SIZEOF_XMMWORD        ; inptr0
279        dec     al                      ; Yctr
280        jnz     near .Yloop_2nd
281
282        add     ebx, byte SIZEOF_XMMWORD        ; inptr1
283        add     edx, byte SIZEOF_XMMWORD        ; inptr2
284        jmp     near .columnloop
285        alignx  16,7
286
287.column_st32:
288        lea     ecx, [ecx+ecx*2]                ; imul ecx, RGB_PIXELSIZE
289        cmp     ecx, byte 2*SIZEOF_XMMWORD
290        jb      short .column_st16
291        movdqu  XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
292        movdqu  XMMWORD [edi+1*SIZEOF_XMMWORD], xmmD
293        add     edi, byte 2*SIZEOF_XMMWORD      ; outptr
294        movdqa  xmmA,xmmF
295        sub     ecx, byte 2*SIZEOF_XMMWORD
296        jmp     short .column_st15
297.column_st16:
298        cmp     ecx, byte SIZEOF_XMMWORD
299        jb      short .column_st15
300        movdqu  XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
301        add     edi, byte SIZEOF_XMMWORD        ; outptr
302        movdqa  xmmA,xmmD
303        sub     ecx, byte SIZEOF_XMMWORD
304.column_st15:
305        ; Store the lower 8 bytes of xmmA to the output when it has enough
306        ; space.
307        cmp     ecx, byte SIZEOF_MMWORD
308        jb      short .column_st7
309        movq    XMM_MMWORD [edi], xmmA
310        add     edi, byte SIZEOF_MMWORD
311        sub     ecx, byte SIZEOF_MMWORD
312        psrldq  xmmA, SIZEOF_MMWORD
313.column_st7:
314        ; Store the lower 4 bytes of xmmA to the output when it has enough
315        ; space.
316        cmp     ecx, byte SIZEOF_DWORD
317        jb      short .column_st3
318        movd    XMM_DWORD [edi], xmmA
319        add     edi, byte SIZEOF_DWORD
320        sub     ecx, byte SIZEOF_DWORD
321        psrldq  xmmA, SIZEOF_DWORD
322.column_st3:
323        ; Store the lower 2 bytes of eax to the output when it has enough
324        ; space.
325        movd    eax, xmmA
326        cmp     ecx, byte SIZEOF_WORD
327        jb      short .column_st1
328        mov     WORD [edi], ax
329        add     edi, byte SIZEOF_WORD
330        sub     ecx, byte SIZEOF_WORD
331        shr     eax, 16
332.column_st1:
333        ; Store the lower 1 byte of eax to the output when it has enough
334        ; space.
335        test    ecx, ecx
336        jz      short .endcolumn
337        mov     BYTE [edi], al
338
339%else ; RGB_PIXELSIZE == 4 ; -----------
340
341%ifdef RGBX_FILLER_0XFF
342        pcmpeqb   xmm6,xmm6             ; xmm6=XE=X(02468ACE********)
343        pcmpeqb   xmm7,xmm7             ; xmm7=XO=X(13579BDF********)
344%else
345        pxor      xmm6,xmm6             ; xmm6=XE=X(02468ACE********)
346        pxor      xmm7,xmm7             ; xmm7=XO=X(13579BDF********)
347%endif
348        ; xmmA=(00 02 04 06 08 0A 0C 0E **), xmmB=(01 03 05 07 09 0B 0D 0F **)
349        ; xmmC=(10 12 14 16 18 1A 1C 1E **), xmmD=(11 13 15 17 19 1B 1D 1F **)
350        ; xmmE=(20 22 24 26 28 2A 2C 2E **), xmmF=(21 23 25 27 29 2B 2D 2F **)
351        ; xmmG=(30 32 34 36 38 3A 3C 3E **), xmmH=(31 33 35 37 39 3B 3D 3F **)
352
353        punpcklbw xmmA,xmmC     ; xmmA=(00 10 02 12 04 14 06 16 08 18 0A 1A 0C 1C 0E 1E)
354        punpcklbw xmmE,xmmG     ; xmmE=(20 30 22 32 24 34 26 36 28 38 2A 3A 2C 3C 2E 3E)
355        punpcklbw xmmB,xmmD     ; xmmB=(01 11 03 13 05 15 07 17 09 19 0B 1B 0D 1D 0F 1F)
356        punpcklbw xmmF,xmmH     ; xmmF=(21 31 23 33 25 35 27 37 29 39 2B 3B 2D 3D 2F 3F)
357
358        movdqa    xmmC,xmmA
359        punpcklwd xmmA,xmmE     ; xmmA=(00 10 20 30 02 12 22 32 04 14 24 34 06 16 26 36)
360        punpckhwd xmmC,xmmE     ; xmmC=(08 18 28 38 0A 1A 2A 3A 0C 1C 2C 3C 0E 1E 2E 3E)
361        movdqa    xmmG,xmmB
362        punpcklwd xmmB,xmmF     ; xmmB=(01 11 21 31 03 13 23 33 05 15 25 35 07 17 27 37)
363        punpckhwd xmmG,xmmF     ; xmmG=(09 19 29 39 0B 1B 2B 3B 0D 1D 2D 3D 0F 1F 2F 3F)
364
365        movdqa    xmmD,xmmA
366        punpckldq xmmA,xmmB     ; xmmA=(00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33)
367        punpckhdq xmmD,xmmB     ; xmmD=(04 14 24 34 05 15 25 35 06 16 26 36 07 17 27 37)
368        movdqa    xmmH,xmmC
369        punpckldq xmmC,xmmG     ; xmmC=(08 18 28 38 09 19 29 39 0A 1A 2A 3A 0B 1B 2B 3B)
370        punpckhdq xmmH,xmmG     ; xmmH=(0C 1C 2C 3C 0D 1D 2D 3D 0E 1E 2E 3E 0F 1F 2F 3F)
371
372        cmp     ecx, byte SIZEOF_XMMWORD
373        jb      short .column_st32
374
375        test    edi, SIZEOF_XMMWORD-1
376        jnz     short .out1
377        ; --(aligned)-------------------
378        movntdq XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
379        movntdq XMMWORD [edi+1*SIZEOF_XMMWORD], xmmD
380        movntdq XMMWORD [edi+2*SIZEOF_XMMWORD], xmmC
381        movntdq XMMWORD [edi+3*SIZEOF_XMMWORD], xmmH
382        jmp     short .out0
383.out1:  ; --(unaligned)-----------------
384        movdqu  XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
385        movdqu  XMMWORD [edi+1*SIZEOF_XMMWORD], xmmD
386        movdqu  XMMWORD [edi+2*SIZEOF_XMMWORD], xmmC
387        movdqu  XMMWORD [edi+3*SIZEOF_XMMWORD], xmmH
388.out0:
389        add     edi, byte RGB_PIXELSIZE*SIZEOF_XMMWORD  ; outptr
390        sub     ecx, byte SIZEOF_XMMWORD
391        jz      near .endcolumn
392
393        add     esi, byte SIZEOF_XMMWORD        ; inptr0
394        dec     al                      ; Yctr
395        jnz     near .Yloop_2nd
396
397        add     ebx, byte SIZEOF_XMMWORD        ; inptr1
398        add     edx, byte SIZEOF_XMMWORD        ; inptr2
399        jmp     near .columnloop
400        alignx  16,7
401
402.column_st32:
403        cmp     ecx, byte SIZEOF_XMMWORD/2
404        jb      short .column_st16
405        movdqu  XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
406        movdqu  XMMWORD [edi+1*SIZEOF_XMMWORD], xmmD
407        add     edi, byte 2*SIZEOF_XMMWORD      ; outptr
408        movdqa  xmmA,xmmC
409        movdqa  xmmD,xmmH
410        sub     ecx, byte SIZEOF_XMMWORD/2
411.column_st16:
412        cmp     ecx, byte SIZEOF_XMMWORD/4
413        jb      short .column_st15
414        movdqu  XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
415        add     edi, byte SIZEOF_XMMWORD        ; outptr
416        movdqa  xmmA,xmmD
417        sub     ecx, byte SIZEOF_XMMWORD/4
418.column_st15:
419        ; Store two pixels (8 bytes) of xmmA to the output when it has enough
420        ; space.
421        cmp     ecx, byte SIZEOF_XMMWORD/8
422        jb      short .column_st7
423        movq    XMM_MMWORD [edi], xmmA
424        add     edi, byte SIZEOF_XMMWORD/8*4
425        sub     ecx, byte SIZEOF_XMMWORD/8
426        psrldq  xmmA, SIZEOF_XMMWORD/8*4
427.column_st7:
428        ; Store one pixel (4 bytes) of xmmA to the output when it has enough
429        ; space.
430        test    ecx, ecx
431        jz      short .endcolumn
432        movd    XMM_DWORD [edi], xmmA
433
434%endif ; RGB_PIXELSIZE ; ---------------
435
436.endcolumn:
437        sfence          ; flush the write buffer
438
439.return:
440        pop     edi
441        pop     esi
442;       pop     edx             ; need not be preserved
443;       pop     ecx             ; need not be preserved
444        pop     ebx
445        mov     esp,ebp         ; esp <- aligned ebp
446        pop     esp             ; esp <- original ebp
447        pop     ebp
448        ret
449
450; --------------------------------------------------------------------------
451;
452; Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
453;
454; GLOBAL(void)
455; jsimd_h2v2_merged_upsample_sse2 (JDIMENSION output_width,
456;                                  JSAMPIMAGE input_buf,
457;                                  JDIMENSION in_row_group_ctr,
458;                                  JSAMPARRAY output_buf);
459;
460
461%define output_width(b) (b)+8                   ; JDIMENSION output_width
462%define input_buf(b)            (b)+12          ; JSAMPIMAGE input_buf
463%define in_row_group_ctr(b)     (b)+16          ; JDIMENSION in_row_group_ctr
464%define output_buf(b)           (b)+20          ; JSAMPARRAY output_buf
465
466        align   16
467        global  EXTN(jsimd_h2v2_merged_upsample_sse2)
468
469EXTN(jsimd_h2v2_merged_upsample_sse2):
470        push    ebp
471        mov     ebp,esp
472        push    ebx
473;       push    ecx             ; need not be preserved
474;       push    edx             ; need not be preserved
475        push    esi
476        push    edi
477
478        mov     eax, POINTER [output_width(ebp)]
479
480        mov     edi, JSAMPIMAGE [input_buf(ebp)]
481        mov     ecx, JDIMENSION [in_row_group_ctr(ebp)]
482        mov     esi, JSAMPARRAY [edi+0*SIZEOF_JSAMPARRAY]
483        mov     ebx, JSAMPARRAY [edi+1*SIZEOF_JSAMPARRAY]
484        mov     edx, JSAMPARRAY [edi+2*SIZEOF_JSAMPARRAY]
485        mov     edi, JSAMPARRAY [output_buf(ebp)]
486        lea     esi, [esi+ecx*SIZEOF_JSAMPROW]
487
488        push    edx                     ; inptr2
489        push    ebx                     ; inptr1
490        push    esi                     ; inptr00
491        mov     ebx,esp
492
493        push    edi                     ; output_buf (outptr0)
494        push    ecx                     ; in_row_group_ctr
495        push    ebx                     ; input_buf
496        push    eax                     ; output_width
497
498        call    near EXTN(jsimd_h2v1_merged_upsample_sse2)
499
500        add     esi, byte SIZEOF_JSAMPROW       ; inptr01
501        add     edi, byte SIZEOF_JSAMPROW       ; outptr1
502        mov     POINTER [ebx+0*SIZEOF_POINTER], esi
503        mov     POINTER [ebx-1*SIZEOF_POINTER], edi
504
505        call    near EXTN(jsimd_h2v1_merged_upsample_sse2)
506
507        add     esp, byte 7*SIZEOF_DWORD
508
509        pop     edi
510        pop     esi
511;       pop     edx             ; need not be preserved
512;       pop     ecx             ; need not be preserved
513        pop     ebx
514        pop     ebp
515        ret
516
517; For some reason, the OS X linker does not honor the request to align the
518; segment unless we do this.
519        align   16
520