1;
2; jdmrgext.asm - merged upsampling/color conversion (AVX2)
3;
4; Copyright 2009, 2012 Pierre Ossman <ossman@cendio.se> for Cendio AB
5; Copyright (C) 2012, 2016, D. R. Commander.
6; Copyright (C) 2015, Intel Corporation.
7;
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
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_avx2(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_YMMWORD
40                                        ; ymmword wk[WK_NUM]
41%define WK_NUM        3
42%define gotptr        wk(0) - SIZEOF_POINTER  ; void * gotptr
43
44    align       32
45    GLOBAL_FUNCTION(jsimd_h2v1_merged_upsample_avx2)
46
47EXTN(jsimd_h2v1_merged_upsample_avx2):
48    push        ebp
49    mov         eax, esp                     ; eax = original ebp
50    sub         esp, byte 4
51    and         esp, byte (-SIZEOF_YMMWORD)  ; align to 256 bits
52    mov         [esp], eax
53    mov         ebp, esp                     ; ebp = aligned ebp
54    lea         esp, [wk(0)]
55    pushpic     eax                     ; make a room for GOT address
56    push        ebx
57;   push        ecx                     ; need not be preserved
58;   push        edx                     ; need not be preserved
59    push        esi
60    push        edi
61
62    get_GOT     ebx                     ; get GOT address
63    movpic      POINTER [gotptr], ebx   ; save GOT address
64
65    mov         ecx, JDIMENSION [output_width(eax)]  ; col
66    test        ecx, ecx
67    jz          near .return
68
69    push        ecx
70
71    mov         edi, JSAMPIMAGE [input_buf(eax)]
72    mov         ecx, JDIMENSION [in_row_group_ctr(eax)]
73    mov         esi, JSAMPARRAY [edi+0*SIZEOF_JSAMPARRAY]
74    mov         ebx, JSAMPARRAY [edi+1*SIZEOF_JSAMPARRAY]
75    mov         edx, JSAMPARRAY [edi+2*SIZEOF_JSAMPARRAY]
76    mov         edi, JSAMPARRAY [output_buf(eax)]
77    mov         esi, JSAMPROW [esi+ecx*SIZEOF_JSAMPROW]  ; inptr0
78    mov         ebx, JSAMPROW [ebx+ecx*SIZEOF_JSAMPROW]  ; inptr1
79    mov         edx, JSAMPROW [edx+ecx*SIZEOF_JSAMPROW]  ; inptr2
80    mov         edi, JSAMPROW [edi]                      ; outptr
81
82    pop         ecx                     ; col
83
84    alignx      16, 7
85.columnloop:
86    movpic      eax, POINTER [gotptr]   ; load GOT address (eax)
87
88    vmovdqu     ymm6, YMMWORD [ebx]     ; ymm6=Cb(0123456789ABCDEFGHIJKLMNOPQRSTUV)
89    vmovdqu     ymm7, YMMWORD [edx]     ; ymm7=Cr(0123456789ABCDEFGHIJKLMNOPQRSTUV)
90
91    vpxor       ymm1, ymm1, ymm1        ; ymm1=(all 0's)
92    vpcmpeqw    ymm3, ymm3, ymm3
93    vpsllw      ymm3, ymm3, 7           ; ymm3={0xFF80 0xFF80 0xFF80 0xFF80 ..}
94
95    vpermq      ymm6, ymm6, 0xd8        ; ymm6=Cb(01234567GHIJKLMN89ABCDEFOPQRSTUV)
96    vpermq      ymm7, ymm7, 0xd8        ; ymm7=Cr(01234567GHIJKLMN89ABCDEFOPQRSTUV)
97    vpunpcklbw  ymm4, ymm6, ymm1        ; ymm4=Cb(0123456789ABCDEF)=CbL
98    vpunpckhbw  ymm6, ymm6, ymm1        ; ymm6=Cb(GHIJKLMNOPQRSTUV)=CbH
99    vpunpcklbw  ymm0, ymm7, ymm1        ; ymm0=Cr(0123456789ABCDEF)=CrL
100    vpunpckhbw  ymm7, ymm7, ymm1        ; ymm7=Cr(GHIJKLMNOPQRSTUV)=CrH
101
102    vpaddw      ymm5, ymm6, ymm3
103    vpaddw      ymm2, ymm4, ymm3
104    vpaddw      ymm1, ymm7, ymm3
105    vpaddw      ymm3, ymm0, ymm3
106
107    ; (Original)
108    ; R = Y                + 1.40200 * Cr
109    ; G = Y - 0.34414 * Cb - 0.71414 * Cr
110    ; B = Y + 1.77200 * Cb
111    ;
112    ; (This implementation)
113    ; R = Y                + 0.40200 * Cr + Cr
114    ; G = Y - 0.34414 * Cb + 0.28586 * Cr - Cr
115    ; B = Y - 0.22800 * Cb + Cb + Cb
116
117    vpaddw      ymm6, ymm5, ymm5             ; ymm6=2*CbH
118    vpaddw      ymm4, ymm2, ymm2             ; ymm4=2*CbL
119    vpaddw      ymm7, ymm1, ymm1             ; ymm7=2*CrH
120    vpaddw      ymm0, ymm3, ymm3             ; ymm0=2*CrL
121
122    vpmulhw     ymm6, ymm6, [GOTOFF(eax,PW_MF0228)]  ; ymm6=(2*CbH * -FIX(0.22800))
123    vpmulhw     ymm4, ymm4, [GOTOFF(eax,PW_MF0228)]  ; ymm4=(2*CbL * -FIX(0.22800))
124    vpmulhw     ymm7, ymm7, [GOTOFF(eax,PW_F0402)]   ; ymm7=(2*CrH * FIX(0.40200))
125    vpmulhw     ymm0, ymm0, [GOTOFF(eax,PW_F0402)]   ; ymm0=(2*CrL * FIX(0.40200))
126
127    vpaddw      ymm6, ymm6, [GOTOFF(eax,PW_ONE)]
128    vpaddw      ymm4, ymm4, [GOTOFF(eax,PW_ONE)]
129    vpsraw      ymm6, ymm6, 1                     ; ymm6=(CbH * -FIX(0.22800))
130    vpsraw      ymm4, ymm4, 1                     ; ymm4=(CbL * -FIX(0.22800))
131    vpaddw      ymm7, ymm7, [GOTOFF(eax,PW_ONE)]
132    vpaddw      ymm0, ymm0, [GOTOFF(eax,PW_ONE)]
133    vpsraw      ymm7, ymm7, 1                     ; ymm7=(CrH * FIX(0.40200))
134    vpsraw      ymm0, ymm0, 1                     ; ymm0=(CrL * FIX(0.40200))
135
136    vpaddw      ymm6, ymm6, ymm5
137    vpaddw      ymm4, ymm4, ymm2
138    vpaddw      ymm6, ymm6, ymm5                  ; ymm6=(CbH * FIX(1.77200))=(B-Y)H
139    vpaddw      ymm4, ymm4, ymm2                  ; ymm4=(CbL * FIX(1.77200))=(B-Y)L
140    vpaddw      ymm7, ymm7, ymm1                  ; ymm7=(CrH * FIX(1.40200))=(R-Y)H
141    vpaddw      ymm0, ymm0, ymm3                  ; ymm0=(CrL * FIX(1.40200))=(R-Y)L
142
143    vmovdqa     YMMWORD [wk(0)], ymm6             ; wk(0)=(B-Y)H
144    vmovdqa     YMMWORD [wk(1)], ymm7             ; wk(1)=(R-Y)H
145
146    vpunpckhwd  ymm6, ymm5, ymm1
147    vpunpcklwd  ymm5, ymm5, ymm1
148    vpmaddwd    ymm5, ymm5, [GOTOFF(eax,PW_MF0344_F0285)]
149    vpmaddwd    ymm6, ymm6, [GOTOFF(eax,PW_MF0344_F0285)]
150    vpunpckhwd  ymm7, ymm2, ymm3
151    vpunpcklwd  ymm2, ymm2, ymm3
152    vpmaddwd    ymm2, ymm2, [GOTOFF(eax,PW_MF0344_F0285)]
153    vpmaddwd    ymm7, ymm7, [GOTOFF(eax,PW_MF0344_F0285)]
154
155    vpaddd      ymm5, ymm5, [GOTOFF(eax,PD_ONEHALF)]
156    vpaddd      ymm6, ymm6, [GOTOFF(eax,PD_ONEHALF)]
157    vpsrad      ymm5, ymm5, SCALEBITS
158    vpsrad      ymm6, ymm6, SCALEBITS
159    vpaddd      ymm2, ymm2, [GOTOFF(eax,PD_ONEHALF)]
160    vpaddd      ymm7, ymm7, [GOTOFF(eax,PD_ONEHALF)]
161    vpsrad      ymm2, ymm2, SCALEBITS
162    vpsrad      ymm7, ymm7, SCALEBITS
163
164    vpackssdw   ymm5, ymm5, ymm6        ; ymm5=CbH*-FIX(0.344)+CrH*FIX(0.285)
165    vpackssdw   ymm2, ymm2, ymm7        ; ymm2=CbL*-FIX(0.344)+CrL*FIX(0.285)
166    vpsubw      ymm5, ymm5, ymm1        ; ymm5=CbH*-FIX(0.344)+CrH*-FIX(0.714)=(G-Y)H
167    vpsubw      ymm2, ymm2, ymm3        ; ymm2=CbL*-FIX(0.344)+CrL*-FIX(0.714)=(G-Y)L
168
169    vmovdqa     YMMWORD [wk(2)], ymm5   ; wk(2)=(G-Y)H
170
171    mov         al, 2                   ; Yctr
172    jmp         short .Yloop_1st
173    alignx      16, 7
174
175.Yloop_2nd:
176    vmovdqa     ymm0, YMMWORD [wk(1)]   ; ymm0=(R-Y)H
177    vmovdqa     ymm2, YMMWORD [wk(2)]   ; ymm2=(G-Y)H
178    vmovdqa     ymm4, YMMWORD [wk(0)]   ; ymm4=(B-Y)H
179    alignx      16, 7
180
181.Yloop_1st:
182    vmovdqu     ymm7, YMMWORD [esi]     ; ymm7=Y(0123456789ABCDEFGHIJKLMNOPQRSTUV)
183
184    vpcmpeqw    ymm6, ymm6, ymm6
185    vpsrlw      ymm6, ymm6, BYTE_BIT    ; ymm6={0xFF 0x00 0xFF 0x00 ..}
186    vpand       ymm6, ymm6, ymm7        ; ymm6=Y(02468ACEGIKMOQSU)=YE
187    vpsrlw      ymm7, ymm7, BYTE_BIT    ; ymm7=Y(13579BDFHJLNPRTV)=YO
188
189    vmovdqa     ymm1, ymm0              ; ymm1=ymm0=(R-Y)(L/H)
190    vmovdqa     ymm3, ymm2              ; ymm3=ymm2=(G-Y)(L/H)
191    vmovdqa     ymm5, ymm4              ; ymm5=ymm4=(B-Y)(L/H)
192
193    vpaddw      ymm0, ymm0, ymm6        ; ymm0=((R-Y)+YE)=RE=R(02468ACEGIKMOQSU)
194    vpaddw      ymm1, ymm1, ymm7        ; ymm1=((R-Y)+YO)=RO=R(13579BDFHJLNPRTV)
195    vpackuswb   ymm0, ymm0, ymm0        ; ymm0=R(02468ACE********GIKMOQSU********)
196    vpackuswb   ymm1, ymm1, ymm1        ; ymm1=R(13579BDF********HJLNPRTV********)
197
198    vpaddw      ymm2, ymm2, ymm6        ; ymm2=((G-Y)+YE)=GE=G(02468ACEGIKMOQSU)
199    vpaddw      ymm3, ymm3, ymm7        ; ymm3=((G-Y)+YO)=GO=G(13579BDFHJLNPRTV)
200    vpackuswb   ymm2, ymm2, ymm2        ; ymm2=G(02468ACE********GIKMOQSU********)
201    vpackuswb   ymm3, ymm3, ymm3        ; ymm3=G(13579BDF********HJLNPRTV********)
202
203    vpaddw      ymm4, ymm4, ymm6        ; ymm4=((B-Y)+YE)=BE=B(02468ACEGIKMOQSU)
204    vpaddw      ymm5, ymm5, ymm7        ; ymm5=((B-Y)+YO)=BO=B(13579BDFHJLNPRTV)
205    vpackuswb   ymm4, ymm4, ymm4        ; ymm4=B(02468ACE********GIKMOQSU********)
206    vpackuswb   ymm5, ymm5, ymm5        ; ymm5=B(13579BDF********HJLNPRTV********)
207
208%if RGB_PIXELSIZE == 3  ; ---------------
209
210    ; ymmA=(00 02 04 06 08 0A 0C 0E ** 0G 0I 0K 0M 0O 0Q 0S 0U **)
211    ; ymmB=(01 03 05 07 09 0B 0D 0F ** 0H 0J 0L 0N 0P 0R 0T 0V **)
212    ; ymmC=(10 12 14 16 18 1A 1C 1E ** 1G 1I 1K 1M 1O 1Q 1S 1U **)
213    ; ymmD=(11 13 15 17 19 1B 1D 1F ** 1H 1J 1L 1N 1P 1R 1T 1V **)
214    ; ymmE=(20 22 24 26 28 2A 2C 2E ** 2G 2I 2K 2M 2O 2Q 2S 2U **)
215    ; ymmF=(21 23 25 27 29 2B 2D 2F ** 2H 2J 2L 2N 2P 2R 2T 2V **)
216    ; ymmG=(** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **)
217    ; ymmH=(** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **)
218
219    vpunpcklbw  ymmA, ymmA, ymmC        ; ymmA=(00 10 02 12 04 14 06 16 08 18 0A 1A 0C 1C 0E 1E
220                                        ;       0G 1G 0I 1I 0K 1K 0M 1M 0O 1O 0Q 1Q 0S 1S 0U 1U)
221    vpunpcklbw  ymmE, ymmE, ymmB        ; ymmE=(20 01 22 03 24 05 26 07 28 09 2A 0B 2C 0D 2E 0F
222                                        ;       2G 0H 2I 0J 2K 0L 2M 0N 2O 0P 2Q 0R 2S 0T 2U 0V)
223    vpunpcklbw  ymmD, ymmD, ymmF        ; ymmD=(11 21 13 23 15 25 17 27 19 29 1B 2B 1D 2D 1F 2F
224                                        ;       1H 2H 1J 2J 1L 2L 1N 2N 1P 2P 1R 2R 1T 2T 1V 2V)
225
226    vpsrldq     ymmH, ymmA, 2           ; ymmH=(02 12 04 14 06 16 08 18 0A 1A 0C 1C 0E 1E 0G 1G
227                                        ;       0I 1I 0K 1K 0M 1M 0O 1O 0Q 1Q 0S 1S 0U 1U -- --)
228    vpunpckhwd  ymmG, ymmA, ymmE        ; ymmG=(08 18 28 09 0A 1A 2A 0B 0C 1C 2C 0D 0E 1E 2E 0F
229                                        ;       0O 1O 2O 0P 0Q 1Q 2Q 0R 0S 1S 2S 0T 0U 1U 2U 0V)
230    vpunpcklwd  ymmA, ymmA, ymmE        ; ymmA=(00 10 20 01 02 12 22 03 04 14 24 05 06 16 26 07
231                                        ;       0G 1G 2G 0H 0I 1I 2I 0J 0K 1K 2K 0L 0M 1M 2M 0N)
232
233    vpsrldq     ymmE, ymmE, 2           ; ymmE=(22 03 24 05 26 07 28 09 2A 0B 2C 0D 2E 0F 2G 0H
234                                        ;       2I 0J 2K 0L 2M 0N 2O 0P 2Q 0R 2S 0T 2U 0V -- --)
235
236    vpsrldq     ymmB, ymmD, 2           ; ymmB=(13 23 15 25 17 27 19 29 1B 2B 1D 2D 1F 2F 1H 2H
237                                        ;       1J 2J 1L 2L 1N 2N 1P 2P 1R 2R 1T 2T 1V 2V -- --)
238    vpunpckhwd  ymmC, ymmD, ymmH        ; ymmC=(19 29 0A 1A 1B 2B 0C 1C 1D 2D 0E 1E 1F 2F 0G 1G
239                                        ;       1P 2P 0Q 1Q 1R 2R 0S 1S 1T 2T 0U 1U 1V 2V -- --)
240    vpunpcklwd  ymmD, ymmD, ymmH        ; ymmD=(11 21 02 12 13 23 04 14 15 25 06 16 17 27 08 18
241                                        ;       1H 2H 0I 1I 1J 2J 0K 1K 1L 2L 0M 1M 1N 2N 0O 1O)
242
243    vpunpckhwd  ymmF, ymmE, ymmB        ; ymmF=(2A 0B 1B 2B 2C 0D 1D 2D 2E 0F 1F 2F 2G 0H 1H 2H
244                                        ;       2Q 0R 1R 2R 2S 0T 1T 2T 2U 0V 1V 2V -- -- -- --)
245    vpunpcklwd  ymmE, ymmE, ymmB        ; ymmE=(22 03 13 23 24 05 15 25 26 07 17 27 28 09 19 29
246                                        ;       2I 0J 1J 2J 2K 0L 1L 2L 2M 0N 1N 2N 2O 0P 1P 2P)
247
248    vpshufd     ymmH, ymmA, 0x4E        ; ymmH=(04 14 24 05 06 16 26 07 00 10 20 01 02 12 22 03
249                                        ;       0K 1K 2K 0L 0M 1M 2M 0N 0G 1G 2G 0H 0I 1I 2I 0J)
250    vpunpckldq  ymmA, ymmA, ymmD        ; ymmA=(00 10 20 01 11 21 02 12 02 12 22 03 13 23 04 14
251                                        ;       0G 1G 2G 0H 1H 2H 0I 1I 0I 1I 2I 0J 1J 2J 0K 1K)
252    vpunpckhdq  ymmD, ymmD, ymmE        ; ymmD=(15 25 06 16 26 07 17 27 17 27 08 18 28 09 19 29
253                                        ;       1L 2L 0M 1M 2M 0N 1N 2N 1N 2N 0O 1O 2O 0P 1P 2P)
254    vpunpckldq  ymmE, ymmE, ymmH        ; ymmE=(22 03 13 23 04 14 24 05 24 05 15 25 06 16 26 07
255                                        ;       2I 0J 1J 2J 0K 1K 2K 0L 2K 0L 1L 2L 0M 1M 2M 0N)
256
257    vpshufd     ymmH, ymmG, 0x4E        ; ymmH=(0C 1C 2C 0D 0E 1E 2E 0F 08 18 28 09 0A 1A 2A 0B
258                                        ;       0S 1S 2S 0T 0U 1U 2U 0V 0O 1O 2O 0P 0Q 1Q 2Q 0R)
259    vpunpckldq  ymmG, ymmG, ymmC        ; ymmG=(08 18 28 09 19 29 0A 1A 0A 1A 2A 0B 1B 2B 0C 1C
260                                        ;       0O 1O 2O 0P 1P 2P 0Q 1Q 0Q 1Q 2Q 0R 1R 2R 0S 1S)
261    vpunpckhdq  ymmC, ymmC, ymmF        ; ymmC=(1D 2D 0E 1E 2E 0F 1F 2F 1F 2F 0G 1G 2G 0H 1H 2H
262                                        ;       1T 2T 0U 1U 2U 0V 1V 2V 1V 2V -- -- -- -- -- --)
263    vpunpckldq  ymmF, ymmF, ymmH        ; ymmF=(2A 0B 1B 2B 0C 1C 2C 0D 2C 0D 1D 2D 0E 1E 2E 0F
264                                        ;       2Q 0R 1R 2R 0S 1S 2S 0T 2S 0T 1T 2T 0U 1U 2U 0V)
265
266    vpunpcklqdq ymmH, ymmA, ymmE        ; ymmH=(00 10 20 01 11 21 02 12 22 03 13 23 04 14 24 05
267                                        ;       0G 1G 2G 0H 1H 2H 0I 1I 2I 0J 1J 2J 0K 1K 2K 0L)
268    vpunpcklqdq ymmG, ymmD, ymmG        ; ymmG=(15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A
269                                        ;       1L 2L 0M 1M 2M 0N 1N 2N 0O 1O 2O 0P 1P 2P 0Q 1Q)
270    vpunpcklqdq ymmC, ymmF, ymmC        ; ymmC=(2A 0B 1B 2B 0C 1C 2C 0D 1D 2D 0E 1E 2E 0F 1F 2F
271                                        ;       2Q 0R 1R 2R 0S 1S 2S 0T 1T 2T 0U 1U 2U 0V 1V 2V)
272
273    vperm2i128  ymmA, ymmH, ymmG, 0x20  ; ymmA=(00 10 20 01 11 21 02 12 22 03 13 23 04 14 24 05
274                                        ;       15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A)
275    vperm2i128  ymmD, ymmC, ymmH, 0x30  ; ymmD=(2A 0B 1B 2B 0C 1C 2C 0D 1D 2D 0E 1E 2E 0F 1F 2F
276                                        ;       0G 1G 2G 0H 1H 2H 0I 1I 2I 0J 1J 2J 0K 1K 2K 0L)
277    vperm2i128  ymmF, ymmG, ymmC, 0x31  ; ymmF=(1L 2L 0M 1M 2M 0N 1N 2N 0O 1O 2O 0P 1P 2P 0Q 1Q
278                                        ;       2Q 0R 1R 2R 0S 1S 2S 0T 1T 2T 0U 1U 2U 0V 1V 2V)
279
280    cmp         ecx, byte SIZEOF_YMMWORD
281    jb          short .column_st64
282
283    test        edi, SIZEOF_YMMWORD-1
284    jnz         short .out1
285    ; --(aligned)-------------------
286    vmovntdq    YMMWORD [edi+0*SIZEOF_YMMWORD], ymmA
287    vmovntdq    YMMWORD [edi+1*SIZEOF_YMMWORD], ymmD
288    vmovntdq    YMMWORD [edi+2*SIZEOF_YMMWORD], ymmF
289    jmp         short .out0
290.out1:  ; --(unaligned)-----------------
291    vmovdqu     YMMWORD [edi+0*SIZEOF_YMMWORD], ymmA
292    vmovdqu     YMMWORD [edi+1*SIZEOF_YMMWORD], ymmD
293    vmovdqu     YMMWORD [edi+2*SIZEOF_YMMWORD], ymmF
294.out0:
295    add         edi, byte RGB_PIXELSIZE*SIZEOF_YMMWORD  ; outptr
296    sub         ecx, byte SIZEOF_YMMWORD
297    jz          near .endcolumn
298
299    add         esi, byte SIZEOF_YMMWORD  ; inptr0
300    dec         al                        ; Yctr
301    jnz         near .Yloop_2nd
302
303    add         ebx, byte SIZEOF_YMMWORD  ; inptr1
304    add         edx, byte SIZEOF_YMMWORD  ; inptr2
305    jmp         near .columnloop
306    alignx      16, 7
307
308.column_st64:
309    lea         ecx, [ecx+ecx*2]            ; imul ecx, RGB_PIXELSIZE
310    cmp         ecx, byte 2*SIZEOF_YMMWORD
311    jb          short .column_st32
312    vmovdqu     YMMWORD [edi+0*SIZEOF_YMMWORD], ymmA
313    vmovdqu     YMMWORD [edi+1*SIZEOF_YMMWORD], ymmD
314    add         edi, byte 2*SIZEOF_YMMWORD  ; outptr
315    vmovdqa     ymmA, ymmF
316    sub         ecx, byte 2*SIZEOF_YMMWORD
317    jmp         short .column_st31
318.column_st32:
319    cmp         ecx, byte SIZEOF_YMMWORD
320    jb          short .column_st31
321    vmovdqu     YMMWORD [edi+0*SIZEOF_YMMWORD], ymmA
322    add         edi, byte SIZEOF_YMMWORD    ; outptr
323    vmovdqa     ymmA, ymmD
324    sub         ecx, byte SIZEOF_YMMWORD
325    jmp         short .column_st31
326.column_st31:
327    cmp         ecx, byte SIZEOF_XMMWORD
328    jb          short .column_st15
329    vmovdqu     XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
330    add         edi, byte SIZEOF_XMMWORD    ; outptr
331    vperm2i128  ymmA, ymmA, ymmA, 1
332    sub         ecx, byte SIZEOF_XMMWORD
333.column_st15:
334    ; Store the lower 8 bytes of xmmA to the output when it has enough
335    ; space.
336    cmp         ecx, byte SIZEOF_MMWORD
337    jb          short .column_st7
338    vmovq       XMM_MMWORD [edi], xmmA
339    add         edi, byte SIZEOF_MMWORD
340    sub         ecx, byte SIZEOF_MMWORD
341    vpsrldq     xmmA, xmmA, SIZEOF_MMWORD
342.column_st7:
343    ; Store the lower 4 bytes of xmmA to the output when it has enough
344    ; space.
345    cmp         ecx, byte SIZEOF_DWORD
346    jb          short .column_st3
347    vmovd       XMM_DWORD [edi], xmmA
348    add         edi, byte SIZEOF_DWORD
349    sub         ecx, byte SIZEOF_DWORD
350    vpsrldq     xmmA, xmmA, SIZEOF_DWORD
351.column_st3:
352    ; Store the lower 2 bytes of eax to the output when it has enough
353    ; space.
354    vmovd       eax, xmmA
355    cmp         ecx, byte SIZEOF_WORD
356    jb          short .column_st1
357    mov         WORD [edi], ax
358    add         edi, byte SIZEOF_WORD
359    sub         ecx, byte SIZEOF_WORD
360    shr         eax, 16
361.column_st1:
362    ; Store the lower 1 byte of eax to the output when it has enough
363    ; space.
364    test        ecx, ecx
365    jz          short .endcolumn
366    mov         BYTE [edi], al
367
368%else  ; RGB_PIXELSIZE == 4 ; -----------
369
370%ifdef RGBX_FILLER_0XFF
371    vpcmpeqb    ymm6, ymm6, ymm6        ; ymm6=XE=X(02468ACE********GIKMOQSU********)
372    vpcmpeqb    ymm7, ymm7, ymm7        ; ymm7=XO=X(13579BDF********HJLNPRTV********)
373%else
374    vpxor       ymm6, ymm6, ymm6        ; ymm6=XE=X(02468ACE********GIKMOQSU********)
375    vpxor       ymm7, ymm7, ymm7        ; ymm7=XO=X(13579BDF********HJLNPRTV********)
376%endif
377    ; ymmA=(00 02 04 06 08 0A 0C 0E ** 0G 0I 0K 0M 0O 0Q 0S 0U **)
378    ; ymmB=(01 03 05 07 09 0B 0D 0F ** 0H 0J 0L 0N 0P 0R 0T 0V **)
379    ; ymmC=(10 12 14 16 18 1A 1C 1E ** 1G 1I 1K 1M 1O 1Q 1S 1U **)
380    ; ymmD=(11 13 15 17 19 1B 1D 1F ** 1H 1J 1L 1N 1P 1R 1T 1V **)
381    ; ymmE=(20 22 24 26 28 2A 2C 2E ** 2G 2I 2K 2M 2O 2Q 2S 2U **)
382    ; ymmF=(21 23 25 27 29 2B 2D 2F ** 2H 2J 2L 2N 2P 2R 2T 2V **)
383    ; ymmG=(30 32 34 36 38 3A 3C 3E ** 3G 3I 3K 3M 3O 3Q 3S 3U **)
384    ; ymmH=(31 33 35 37 39 3B 3D 3F ** 3H 3J 3L 3N 3P 3R 3T 3V **)
385
386    vpunpcklbw  ymmA, ymmA, ymmC        ; ymmA=(00 10 02 12 04 14 06 16 08 18 0A 1A 0C 1C 0E 1E
387                                        ;       0G 1G 0I 1I 0K 1K 0M 1M 0O 1O 0Q 1Q 0S 1S 0U 1U)
388    vpunpcklbw  ymmE, ymmE, ymmG        ; ymmE=(20 30 22 32 24 34 26 36 28 38 2A 3A 2C 3C 2E 3E
389                                        ;       2G 3G 2I 3I 2K 3K 2M 3M 2O 3O 2Q 3Q 2S 3S 2U 3U)
390    vpunpcklbw  ymmB, ymmB, ymmD        ; ymmB=(01 11 03 13 05 15 07 17 09 19 0B 1B 0D 1D 0F 1F
391                                        ;       0H 1H 0J 1J 0L 1L 0N 1N 0P 1P 0R 1R 0T 1T 0V 1V)
392    vpunpcklbw  ymmF, ymmF, ymmH        ; ymmF=(21 31 23 33 25 35 27 37 29 39 2B 3B 2D 3D 2F 3F
393                                        ;       2H 3H 2J 3J 2L 3L 2N 3N 2P 3P 2R 3R 2T 3T 2V 3V)
394
395    vpunpckhwd  ymmC, ymmA, ymmE        ; ymmC=(08 18 28 38 0A 1A 2A 3A 0C 1C 2C 3C 0E 1E 2E 3E
396                                        ;       0O 1O 2O 3O 0Q 1Q 2Q 3Q 0S 1S 2S 3S 0U 1U 2U 3U)
397    vpunpcklwd  ymmA, ymmA, ymmE        ; ymmA=(00 10 20 30 02 12 22 32 04 14 24 34 06 16 26 36
398                                        ;       0G 1G 2G 3G 0I 1I 2I 3I 0K 1K 2K 3K 0M 1M 2M 3M)
399    vpunpckhwd  ymmG, ymmB, ymmF        ; ymmG=(09 19 29 39 0B 1B 2B 3B 0D 1D 2D 3D 0F 1F 2F 3F
400                                        ;       0P 1P 2P 3P 0R 1R 2R 3R 0T 1T 2T 3T 0V 1V 2V 3V)
401    vpunpcklwd  ymmB, ymmB, ymmF        ; ymmB=(01 11 21 31 03 13 23 33 05 15 25 35 07 17 27 37
402                                        ;       0H 1H 2H 3H 0J 1J 2J 3J 0L 1L 2L 3L 0N 1N 2N 3N)
403
404    vpunpckhdq  ymmE, ymmA, ymmB        ; ymmE=(04 14 24 34 05 15 25 35 06 16 26 36 07 17 27 37
405                                        ;       0K 1K 2K 3K 0L 1L 2L 3L 0M 1M 2M 3M 0N 1N 2N 3N)
406    vpunpckldq  ymmB, ymmA, ymmB        ; ymmB=(00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33
407                                        ;       0G 1G 2G 3G 0H 1H 2H 3H 0I 1I 2I 3I 0J 1J 2J 3J)
408    vpunpckhdq  ymmF, ymmC, ymmG        ; ymmF=(0C 1C 2C 3C 0D 1D 2D 3D 0E 1E 2E 3E 0F 1F 2F 3F
409                                        ;       0S 1S 2S 3S 0T 1T 2T 3T 0U 1U 2U 3U 0V 1V 2V 3V)
410    vpunpckldq  ymmG, ymmC, ymmG        ; ymmG=(08 18 28 38 09 19 29 39 0A 1A 2A 3A 0B 1B 2B 3B
411                                        ;       0O 1O 2O 3O 0P 1P 2P 3P 0Q 1Q 2Q 3Q 0R 1R 2R 3R)
412
413    vperm2i128  ymmA, ymmB, ymmE, 0x20  ; ymmA=(00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33
414                                        ;       04 14 24 34 05 15 25 35 06 16 26 36 07 17 27 37)
415    vperm2i128  ymmD, ymmG, ymmF, 0x20  ; ymmD=(08 18 28 38 09 19 29 39 0A 1A 2A 3A 0B 1B 2B 3B
416                                        ;       0C 1C 2C 3C 0D 1D 2D 3D 0E 1E 2E 3E 0F 1F 2F 3F)
417    vperm2i128  ymmC, ymmB, ymmE, 0x31  ; ymmC=(0G 1G 2G 3G 0H 1H 2H 3H 0I 1I 2I 3I 0J 1J 2J 3J
418                                        ;       0K 1K 2K 3K 0L 1L 2L 3L 0M 1M 2M 3M 0N 1N 2N 3N)
419    vperm2i128  ymmH, ymmG, ymmF, 0x31  ; ymmH=(0O 1O 2O 3O 0P 1P 2P 3P 0Q 1Q 2Q 3Q 0R 1R 2R 3R
420                                        ;       0S 1S 2S 3S 0T 1T 2T 3T 0U 1U 2U 3U 0V 1V 2V 3V)
421
422    cmp         ecx, byte SIZEOF_YMMWORD
423    jb          short .column_st64
424
425    test        edi, SIZEOF_YMMWORD-1
426    jnz         short .out1
427    ; --(aligned)-------------------
428    vmovntdq    YMMWORD [edi+0*SIZEOF_YMMWORD], ymmA
429    vmovntdq    YMMWORD [edi+1*SIZEOF_YMMWORD], ymmD
430    vmovntdq    YMMWORD [edi+2*SIZEOF_YMMWORD], ymmC
431    vmovntdq    YMMWORD [edi+3*SIZEOF_YMMWORD], ymmH
432    jmp         short .out0
433.out1:  ; --(unaligned)-----------------
434    vmovdqu     YMMWORD [edi+0*SIZEOF_YMMWORD], ymmA
435    vmovdqu     YMMWORD [edi+1*SIZEOF_YMMWORD], ymmD
436    vmovdqu     YMMWORD [edi+2*SIZEOF_YMMWORD], ymmC
437    vmovdqu     YMMWORD [edi+3*SIZEOF_YMMWORD], ymmH
438.out0:
439    add         edi, RGB_PIXELSIZE*SIZEOF_YMMWORD  ; outptr
440    sub         ecx, byte SIZEOF_YMMWORD
441    jz          near .endcolumn
442
443    add         esi, byte SIZEOF_YMMWORD  ; inptr0
444    dec         al
445    jnz         near .Yloop_2nd
446
447    add         ebx, byte SIZEOF_YMMWORD  ; inptr1
448    add         edx, byte SIZEOF_YMMWORD  ; inptr2
449    jmp         near .columnloop
450    alignx      16, 7
451
452.column_st64:
453    cmp         ecx, byte SIZEOF_YMMWORD/2
454    jb          short .column_st32
455    vmovdqu     YMMWORD [edi+0*SIZEOF_YMMWORD], ymmA
456    vmovdqu     YMMWORD [edi+1*SIZEOF_YMMWORD], ymmD
457    add         edi, byte 2*SIZEOF_YMMWORD  ; outptr
458    vmovdqa     ymmA, ymmC
459    vmovdqa     ymmD, ymmH
460    sub         ecx, byte SIZEOF_YMMWORD/2
461.column_st32:
462    cmp         ecx, byte SIZEOF_YMMWORD/4
463    jb          short .column_st16
464    vmovdqu     YMMWORD [edi+0*SIZEOF_YMMWORD], ymmA
465    add         edi, byte SIZEOF_YMMWORD    ; outptr
466    vmovdqa     ymmA, ymmD
467    sub         ecx, byte SIZEOF_YMMWORD/4
468.column_st16:
469    cmp         ecx, byte SIZEOF_YMMWORD/8
470    jb          short .column_st15
471    vmovdqu     XMMWORD [edi+0*SIZEOF_XMMWORD], xmmA
472    add         edi, byte SIZEOF_XMMWORD    ; outptr
473    vperm2i128  ymmA, ymmA, ymmA, 1
474    sub         ecx, byte SIZEOF_YMMWORD/8
475.column_st15:
476    ; Store two pixels (8 bytes) of ymmA to the output when it has enough
477    ; space.
478    cmp         ecx, byte SIZEOF_YMMWORD/16
479    jb          short .column_st7
480    vmovq       MMWORD [edi], xmmA
481    add         edi, byte SIZEOF_YMMWORD/16*4
482    sub         ecx, byte SIZEOF_YMMWORD/16
483    vpsrldq     xmmA, SIZEOF_YMMWORD/16*4
484.column_st7:
485    ; Store one pixel (4 bytes) of ymmA to the output when it has enough
486    ; space.
487    test        ecx, ecx
488    jz          short .endcolumn
489    vmovd       XMM_DWORD [edi], xmmA
490
491%endif  ; RGB_PIXELSIZE ; ---------------
492
493.endcolumn:
494    sfence                              ; flush the write buffer
495
496.return:
497    vzeroupper
498    pop         edi
499    pop         esi
500;   pop         edx                     ; need not be preserved
501;   pop         ecx                     ; need not be preserved
502    pop         ebx
503    mov         esp, ebp                ; esp <- aligned ebp
504    pop         esp                     ; esp <- original ebp
505    pop         ebp
506    ret
507
508; --------------------------------------------------------------------------
509;
510; Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
511;
512; GLOBAL(void)
513; jsimd_h2v2_merged_upsample_avx2(JDIMENSION output_width,
514;                                 JSAMPIMAGE input_buf,
515;                                 JDIMENSION in_row_group_ctr,
516;                                 JSAMPARRAY output_buf);
517;
518
519%define output_width(b)      (b) + 8    ; JDIMENSION output_width
520%define input_buf(b)         (b) + 12   ; JSAMPIMAGE input_buf
521%define in_row_group_ctr(b)  (b) + 16   ; JDIMENSION in_row_group_ctr
522%define output_buf(b)        (b) + 20   ; JSAMPARRAY output_buf
523
524    align       32
525    GLOBAL_FUNCTION(jsimd_h2v2_merged_upsample_avx2)
526
527EXTN(jsimd_h2v2_merged_upsample_avx2):
528    push        ebp
529    mov         ebp, esp
530    push        ebx
531;   push        ecx                     ; need not be preserved
532;   push        edx                     ; need not be preserved
533    push        esi
534    push        edi
535
536    mov         eax, POINTER [output_width(ebp)]
537
538    mov         edi, JSAMPIMAGE [input_buf(ebp)]
539    mov         ecx, JDIMENSION [in_row_group_ctr(ebp)]
540    mov         esi, JSAMPARRAY [edi+0*SIZEOF_JSAMPARRAY]
541    mov         ebx, JSAMPARRAY [edi+1*SIZEOF_JSAMPARRAY]
542    mov         edx, JSAMPARRAY [edi+2*SIZEOF_JSAMPARRAY]
543    mov         edi, JSAMPARRAY [output_buf(ebp)]
544    lea         esi, [esi+ecx*SIZEOF_JSAMPROW]
545
546    push        edx                     ; inptr2
547    push        ebx                     ; inptr1
548    push        esi                     ; inptr00
549    mov         ebx, esp
550
551    push        edi                     ; output_buf (outptr0)
552    push        ecx                     ; in_row_group_ctr
553    push        ebx                     ; input_buf
554    push        eax                     ; output_width
555
556    call        near EXTN(jsimd_h2v1_merged_upsample_avx2)
557
558    add         esi, byte SIZEOF_JSAMPROW  ; inptr01
559    add         edi, byte SIZEOF_JSAMPROW  ; outptr1
560    mov         POINTER [ebx+0*SIZEOF_POINTER], esi
561    mov         POINTER [ebx-1*SIZEOF_POINTER], edi
562
563    call        near EXTN(jsimd_h2v1_merged_upsample_avx2)
564
565    add         esp, byte 7*SIZEOF_DWORD
566
567    pop         edi
568    pop         esi
569;   pop         edx                     ; need not be preserved
570;   pop         ecx                     ; need not be preserved
571    pop         ebx
572    pop         ebp
573    ret
574
575; For some reason, the OS X linker does not honor the request to align the
576; segment unless we do this.
577    align       32
578