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