• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1;
2; jcgryext.asm - grayscale colorspace conversion (AVX2)
3;
4; Copyright (C) 2011, 2016, D. R. Commander.
5; Copyright (C) 2015, Intel Corporation.
6;
7; Based on the x86 SIMD extension for IJG JPEG library
8; Copyright (C) 1999-2006, MIYASAKA Masaru.
9; For conditions of distribution and use, see copyright notice in jsimdext.inc
10;
11; This file should be assembled with NASM (Netwide Assembler),
12; can *not* be assembled with Microsoft's MASM or any compatible
13; assembler (including Borland's Turbo Assembler).
14; NASM is available from http://nasm.sourceforge.net/ or
15; http://sourceforge.net/project/showfiles.php?group_id=6208
16;
17; [TAB8]
18
19%include "jcolsamp.inc"
20
21; --------------------------------------------------------------------------
22;
23; Convert some rows of samples to the output colorspace.
24;
25; GLOBAL(void)
26; jsimd_rgb_gray_convert_avx2(JDIMENSION img_width, JSAMPARRAY input_buf,
27;                             JSAMPIMAGE output_buf, JDIMENSION output_row,
28;                             int num_rows);
29;
30
31%define img_width(b)   (b) + 8          ; JDIMENSION img_width
32%define input_buf(b)   (b) + 12         ; JSAMPARRAY input_buf
33%define output_buf(b)  (b) + 16         ; JSAMPIMAGE output_buf
34%define output_row(b)  (b) + 20         ; JDIMENSION output_row
35%define num_rows(b)    (b) + 24         ; int num_rows
36
37%define original_ebp   ebp + 0
38%define wk(i)          ebp - (WK_NUM - (i)) * SIZEOF_YMMWORD
39                                        ; ymmword wk[WK_NUM]
40%define WK_NUM         2
41%define gotptr         wk(0) - SIZEOF_POINTER  ; void * gotptr
42
43    align       32
44    GLOBAL_FUNCTION(jsimd_rgb_gray_convert_avx2)
45
46EXTN(jsimd_rgb_gray_convert_avx2):
47    push        ebp
48    mov         eax, esp                     ; eax = original ebp
49    sub         esp, byte 4
50    and         esp, byte (-SIZEOF_YMMWORD)  ; align to 256 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 [img_width(eax)]
65    test        ecx, ecx
66    jz          near .return
67
68    push        ecx
69
70    mov         esi, JSAMPIMAGE [output_buf(eax)]
71    mov         ecx, JDIMENSION [output_row(eax)]
72    mov         edi, JSAMPARRAY [esi+0*SIZEOF_JSAMPARRAY]
73    lea         edi, [edi+ecx*SIZEOF_JSAMPROW]
74
75    pop         ecx
76
77    mov         esi, JSAMPARRAY [input_buf(eax)]
78    mov         eax, INT [num_rows(eax)]
79    test        eax, eax
80    jle         near .return
81    alignx      16, 7
82.rowloop:
83    pushpic     eax
84    push        edi
85    push        esi
86    push        ecx                     ; col
87
88    mov         esi, JSAMPROW [esi]     ; inptr
89    mov         edi, JSAMPROW [edi]     ; outptr0
90    movpic      eax, POINTER [gotptr]   ; load GOT address (eax)
91
92    cmp         ecx, byte SIZEOF_YMMWORD
93    jae         near .columnloop
94    alignx      16, 7
95
96%if RGB_PIXELSIZE == 3  ; ---------------
97
98.column_ld1:
99    push        eax
100    push        edx
101    lea         ecx, [ecx+ecx*2]        ; imul ecx,RGB_PIXELSIZE
102    test        cl, SIZEOF_BYTE
103    jz          short .column_ld2
104    sub         ecx, byte SIZEOF_BYTE
105    movzx       eax, BYTE [esi+ecx]
106.column_ld2:
107    test        cl, SIZEOF_WORD
108    jz          short .column_ld4
109    sub         ecx, byte SIZEOF_WORD
110    movzx       edx, WORD [esi+ecx]
111    shl         eax, WORD_BIT
112    or          eax, edx
113.column_ld4:
114    vmovd       xmmA, eax
115    pop         edx
116    pop         eax
117    test        cl, SIZEOF_DWORD
118    jz          short .column_ld8
119    sub         ecx, byte SIZEOF_DWORD
120    vmovd       xmmF, XMM_DWORD [esi+ecx]
121    vpslldq     xmmA, xmmA, SIZEOF_DWORD
122    vpor        xmmA, xmmA, xmmF
123.column_ld8:
124    test        cl, SIZEOF_MMWORD
125    jz          short .column_ld16
126    sub         ecx, byte SIZEOF_MMWORD
127    vmovq       xmmB, XMM_MMWORD [esi+ecx]
128    vpslldq     xmmA, xmmA, SIZEOF_MMWORD
129    vpor        xmmA, xmmA, xmmB
130.column_ld16:
131    test        cl, SIZEOF_XMMWORD
132    jz          short .column_ld32
133    sub         ecx, byte SIZEOF_XMMWORD
134    vmovdqu     xmmB, XMM_MMWORD [esi+ecx]
135    vperm2i128  ymmA, ymmA, ymmA, 1
136    vpor        ymmA, ymmB
137.column_ld32:
138    test        cl, SIZEOF_YMMWORD
139    jz          short .column_ld64
140    sub         ecx, byte SIZEOF_YMMWORD
141    vmovdqa     ymmF, ymmA
142    vmovdqu     ymmA, YMMWORD [esi+0*SIZEOF_YMMWORD]
143.column_ld64:
144    test        cl, 2*SIZEOF_YMMWORD
145    mov         ecx, SIZEOF_YMMWORD
146    jz          short .rgb_gray_cnv
147    vmovdqa     ymmB, ymmA
148    vmovdqu     ymmA, YMMWORD [esi+0*SIZEOF_YMMWORD]
149    vmovdqu     ymmF, YMMWORD [esi+1*SIZEOF_YMMWORD]
150    jmp         short .rgb_gray_cnv
151    alignx      16, 7
152
153.columnloop:
154    vmovdqu     ymmA, YMMWORD [esi+0*SIZEOF_YMMWORD]
155    vmovdqu     ymmF, YMMWORD [esi+1*SIZEOF_YMMWORD]
156    vmovdqu     ymmB, YMMWORD [esi+2*SIZEOF_YMMWORD]
157
158.rgb_gray_cnv:
159    ; ymmA=(00 10 20 01 11 21 02 12 22 03 13 23 04 14 24 05
160    ;       15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A)
161    ; ymmF=(2A 0B 1B 2B 0C 1C 2C 0D 1D 2D 0E 1E 2E 0F 1F 2F
162    ;       0G 1G 2G 0H 1H 2H 0I 1I 2I 0J 1J 2J 0K 1K 2K 0L)
163    ; ymmB=(1L 2L 0M 1M 2M 0N 1N 2N 0O 1O 2O 0P 1P 2P 0Q 1Q
164    ;       2Q 0R 1R 2R 0S 1S 2S 0T 1T 2T 0U 1U 2U 0V 1V 2V)
165
166    vmovdqu     ymmC, ymmA
167    vinserti128 ymmA, ymmF, xmmA, 0  ; ymmA=(00 10 20 01 11 21 02 12 22 03 13 23 04 14 24 05
168                                     ;       0G 1G 2G 0H 1H 2H 0I 1I 2I 0J 1J 2J 0K 1K 2K 0L)
169    vinserti128 ymmC, ymmC, xmmB, 0  ; ymmC=(1L 2L 0M 1M 2M 0N 1N 2N 0O 1O 2O 0P 1P 2P 0Q 1Q
170                                     ;       15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A)
171    vinserti128 ymmB, ymmB, xmmF, 0  ; ymmB=(2A 0B 1B 2B 0C 1C 2C 0D 1D 2D 0E 1E 2E 0F 1F 2F
172                                     ;       2Q 0R 1R 2R 0S 1S 2S 0T 1T 2T 0U 1U 2U 0V 1V 2V)
173    vperm2i128  ymmF, ymmC, ymmC, 1  ; ymmF=(15 25 06 16 26 07 17 27 08 18 28 09 19 29 0A 1A
174                                     ;       1L 2L 0M 1M 2M 0N 1N 2N 0O 1O 2O 0P 1P 2P 0Q 1Q)
175
176    vmovdqa     ymmG, ymmA
177    vpslldq     ymmA, ymmA, 8     ; ymmA=(-- -- -- -- -- -- -- -- 00 10 20 01 11 21 02 12
178                                  ;       22 03 13 23 04 14 24 05 0G 1G 2G 0H 1H 2H 0I 1I)
179    vpsrldq     ymmG, ymmG, 8     ; ymmG=(22 03 13 23 04 14 24 05 0G 1G 2G 0H 1H 2H 0I 1I
180                                  ;       2I 0J 1J 2J 0K 1K 2K 0L -- -- -- -- -- -- -- --)
181
182    vpunpckhbw  ymmA, ymmA, ymmF  ; ymmA=(00 08 10 18 20 28 01 09 11 19 21 29 02 0A 12 1A
183                                  ;       0G 0O 1G 1O 2G 2O 0H 0P 1H 1P 2H 2P 0I 0Q 1I 1Q)
184    vpslldq     ymmF, ymmF, 8     ; ymmF=(-- -- -- -- -- -- -- -- 15 25 06 16 26 07 17 27
185                                  ;       08 18 28 09 19 29 0A 1A 1L 2L 0M 1M 2M 0N 1N 2N)
186
187    vpunpcklbw  ymmG, ymmG, ymmB  ; ymmG=(22 2A 03 0B 13 1B 23 2B 04 0C 14 1C 24 2C 05 0D
188                                  ;       2I 2Q 0J 0R 1J 1R 2J 2R 0K 0S 1K 1S 2K 2S 0L 0T)
189    vpunpckhbw  ymmF, ymmF, ymmB  ; ymmF=(15 1D 25 2D 06 0E 16 1E 26 2E 07 0F 17 1F 27 2F
190                                  ;       1L 1T 2L 2T 0M 0U 1M 1U 2M 2U 0N 0V 1N 1V 2N 2V)
191
192    vmovdqa     ymmD, ymmA
193    vpslldq     ymmA, ymmA, 8     ; ymmA=(-- -- -- -- -- -- -- -- 00 08 10 18 20 28 01 09
194                                  ;       11 19 21 29 02 0A 12 1A 0G 0O 1G 1O 2G 2O 0H 0P)
195    vpsrldq     ymmD, ymmD, 8     ; ymmD=(11 19 21 29 02 0A 12 1A 0G 0O 1G 1O 2G 2O 0H 0P
196                                  ;       1H 1P 2H 2P 0I 0Q 1I 1Q -- -- -- -- -- -- -- --)
197
198    vpunpckhbw  ymmA, ymmA, ymmG  ; ymmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 01 05 09 0D
199                                  ;       0G 0K 0O 0S 1G 1K 1O 1S 2G 2K 2O 2S 0H 0L 0P 0T)
200    vpslldq     ymmG, ymmG, 8     ; ymmG=(-- -- -- -- -- -- -- -- 22 2A 03 0B 13 1B 23 2B
201                                  ;       04 0C 14 1C 24 2C 05 0D 2I 2Q 0J 0R 1J 1R 2J 2R)
202
203    vpunpcklbw  ymmD, ymmD, ymmF  ; ymmD=(11 15 19 1D 21 25 29 2D 02 06 0A 0E 12 16 1A 1E
204                                  ;       1H 1L 1P 1T 2H 2L 2P 2T 0I 0M 0Q 0U 1I 1M 1Q 1U)
205    vpunpckhbw  ymmG, ymmG, ymmF  ; ymmG=(22 26 2A 2E 03 07 0B 0F 13 17 1B 1F 23 27 2B 2F
206                                  ;       2I 2M 2Q 2U 0J 0N 0R 0V 1J 1N 1R 1V 2J 2N 2R 2V)
207
208    vmovdqa     ymmE, ymmA
209    vpslldq     ymmA, ymmA, 8     ; ymmA=(-- -- -- -- -- -- -- -- 00 04 08 0C 10 14 18 1C
210                                  ;       20 24 28 2C 01 05 09 0D 0G 0K 0O 0S 1G 1K 1O 1S)
211    vpsrldq     ymmE, ymmE, 8     ; ymmE=(20 24 28 2C 01 05 09 0D 0G 0K 0O 0S 1G 1K 1O 1S
212                                  ;       2G 2K 2O 2S 0H 0L 0P 0T -- -- -- -- -- -- -- --)
213
214    vpunpckhbw  ymmA, ymmA, ymmD  ; ymmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E
215                                  ;       0G 0I 0K 0M 0O 0Q 0S 0U 1G 1I 1K 1M 1O 1Q 1S 1U)
216    vpslldq     ymmD, ymmD, 8     ; ymmD=(-- -- -- -- -- -- -- -- 11 15 19 1D 21 25 29 2D
217                                  ;       02 06 0A 0E 12 16 1A 1E 1H 1L 1P 1T 2H 2L 2P 2T)
218
219    vpunpcklbw  ymmE, ymmE, ymmG  ; ymmE=(20 22 24 26 28 2A 2C 2E 01 03 05 07 09 0B 0D 0F
220                                  ;       2G 2I 2K 2M 2O 2Q 2S 2U 0H 0J 0L 0N 0P 0R 0T 0V)
221    vpunpckhbw  ymmD, ymmD, ymmG  ; ymmD=(11 13 15 17 19 1B 1D 1F 21 23 25 27 29 2B 2D 2F
222                                  ;       1H 1J 1L 1N 1P 1R 1T 1V 2H 2J 2L 2N 2P 2R 2T 2V)
223
224    vpxor       ymmH, ymmH, ymmH
225
226    vmovdqa     ymmC, ymmA
227    vpunpcklbw  ymmA, ymmA, ymmH  ; ymmA=(00 02 04 06 08 0A 0C 0E 0G 0I 0K 0M 0O 0Q 0S 0U)
228    vpunpckhbw  ymmC, ymmC, ymmH  ; ymmC=(10 12 14 16 18 1A 1C 1E 1G 1I 1K 1M 1O 1Q 1S 1U)
229
230    vmovdqa     ymmB, ymmE
231    vpunpcklbw  ymmE, ymmE, ymmH  ; ymmE=(20 22 24 26 28 2A 2C 2E 2G 2I 2K 2M 2O 2Q 2S 2U)
232    vpunpckhbw  ymmB, ymmB, ymmH  ; ymmB=(01 03 05 07 09 0B 0D 0F 0H 0J 0L 0N 0P 0R 0T 0V)
233
234    vmovdqa     ymmF, ymmD
235    vpunpcklbw  ymmD, ymmD, ymmH  ; ymmD=(11 13 15 17 19 1B 1D 1F 1H 1J 1L 1N 1P 1R 1T 1V)
236    vpunpckhbw  ymmF, ymmF, ymmH  ; ymmF=(21 23 25 27 29 2B 2D 2F 2H 2J 2L 2N 2P 2R 2T 2V)
237
238%else  ; RGB_PIXELSIZE == 4 ; -----------
239
240.column_ld1:
241    test        cl, SIZEOF_XMMWORD/16
242    jz          short .column_ld2
243    sub         ecx, byte SIZEOF_XMMWORD/16
244    vmovd       xmmA, XMM_DWORD [esi+ecx*RGB_PIXELSIZE]
245.column_ld2:
246    test        cl, SIZEOF_XMMWORD/8
247    jz          short .column_ld4
248    sub         ecx, byte SIZEOF_XMMWORD/8
249    vmovq       xmmF, XMM_MMWORD [esi+ecx*RGB_PIXELSIZE]
250    vpslldq     xmmA, xmmA, SIZEOF_MMWORD
251    vpor        xmmA, xmmA, xmmF
252.column_ld4:
253    test        cl, SIZEOF_XMMWORD/4
254    jz          short .column_ld8
255    sub         ecx, byte SIZEOF_XMMWORD/4
256    vmovdqa     xmmF, xmmA
257    vperm2i128  ymmF, ymmF, ymmF, 1
258    vmovdqu     xmmA, XMMWORD [esi+ecx*RGB_PIXELSIZE]
259    vpor        ymmA, ymmA, ymmF
260.column_ld8:
261    test        cl, SIZEOF_XMMWORD/2
262    jz          short .column_ld16
263    sub         ecx, byte SIZEOF_XMMWORD/2
264    vmovdqa     ymmF, ymmA
265    vmovdqu     ymmA, YMMWORD [esi+ecx*RGB_PIXELSIZE]
266.column_ld16:
267    test        cl, SIZEOF_XMMWORD
268    mov         ecx, SIZEOF_YMMWORD
269    jz          short .rgb_gray_cnv
270    vmovdqa     ymmE, ymmA
271    vmovdqa     ymmH, ymmF
272    vmovdqu     ymmA, YMMWORD [esi+0*SIZEOF_YMMWORD]
273    vmovdqu     ymmF, YMMWORD [esi+1*SIZEOF_YMMWORD]
274    jmp         short .rgb_gray_cnv
275    alignx      16, 7
276
277.columnloop:
278    vmovdqu     ymmA, YMMWORD [esi+0*SIZEOF_YMMWORD]
279    vmovdqu     ymmF, YMMWORD [esi+1*SIZEOF_YMMWORD]
280    vmovdqu     ymmE, YMMWORD [esi+2*SIZEOF_YMMWORD]
281    vmovdqu     ymmH, YMMWORD [esi+3*SIZEOF_YMMWORD]
282
283.rgb_gray_cnv:
284    ; ymmA=(00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33
285    ;       04 14 24 34 05 15 25 35 06 16 26 36 07 17 27 37)
286    ; ymmF=(08 18 28 38 09 19 29 39 0A 1A 2A 3A 0B 1B 2B 3B
287    ;       0C 1C 2C 3C 0D 1D 2D 3D 0E 1E 2E 3E 0F 1F 2F 3F)
288    ; ymmE=(0G 1G 2G 3G 0H 1H 2H 3H 0I 1I 2I 3I 0J 1J 2J 3J
289    ;       0K 1K 2K 3K 0L 1L 2L 3L 0M 1M 2M 3M 0N 1N 2N 3N)
290    ; ymmH=(0O 1O 2O 3O 0P 1P 2P 3P 0Q 1Q 2Q 3Q 0R 1R 2R 3R
291    ;       0S 1S 2S 3S 0T 1T 2T 3T 0U 1U 2U 3U 0V 1V 2V 3V)
292
293    vmovdqa     ymmB, ymmA
294    vinserti128 ymmA, ymmA, xmmE, 1     ; ymmA=(00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33
295                                        ;       0G 1G 2G 3G 0H 1H 2H 3H 0I 1I 2I 3I 0J 1J 2J 3J)
296    vperm2i128  ymmE, ymmB, ymmE, 0x31  ; ymmE=(04 14 24 34 05 15 25 35 06 16 26 36 07 17 27 37
297                                        ;       0K 1K 2K 3K 0L 1L 2L 3L 0M 1M 2M 3M 0N 1N 2N 3N)
298
299    vmovdqa     ymmB, ymmF
300    vinserti128 ymmF, ymmF, xmmH, 1     ; ymmF=(08 18 28 38 09 19 29 39 0A 1A 2A 3A 0B 1B 2B 3B
301                                        ;       0O 1O 2O 3O 0P 1P 2P 3P 0Q 1Q 2Q 3Q 0R 1R 2R 3R)
302    vperm2i128  ymmH, ymmB, ymmH, 0x31  ; ymmH=(0C 1C 2C 3C 0D 1D 2D 3D 0E 1E 2E 3E 0F 1F 2F 3F
303                                        ;       0S 1S 2S 3S 0T 1T 2T 3T 0U 1U 2U 3U 0V 1V 2V 3V)
304
305    vmovdqa     ymmD, ymmA
306    vpunpcklbw  ymmA, ymmA, ymmE      ; ymmA=(00 04 10 14 20 24 30 34 01 05 11 15 21 25 31 35
307                                      ;       0G 0K 1G 1K 2G 2K 3G 3K 0H 0L 1H 1L 2H 2L 3H 3L)
308    vpunpckhbw  ymmD, ymmD, ymmE      ; ymmD=(02 06 12 16 22 26 32 36 03 07 13 17 23 27 33 37
309                                      ;       0I 0M 1I 1M 2I 2M 3I 3M 0J 0N 1J 1N 2J 2N 3J 3N)
310
311    vmovdqa     ymmC, ymmF
312    vpunpcklbw  ymmF, ymmF, ymmH      ; ymmF=(08 0C 18 1C 28 2C 38 3C 09 0D 19 1D 29 2D 39 3D
313                                      ;       0O 0S 1O 1S 2O 2S 3O 3S 0P 0T 1P 1T 2P 2T 3P 3T)
314    vpunpckhbw  ymmC, ymmC, ymmH      ; ymmC=(0A 0E 1A 1E 2A 2E 3A 3E 0B 0F 1B 1F 2B 2F 3B 3F
315                                      ;       0Q 0U 1Q 1U 2Q 2U 3Q 3U 0R 0V 1R 1V 2R 2V 3R 3V)
316
317    vmovdqa     ymmB, ymmA
318    vpunpcklwd  ymmA, ymmA, ymmF      ; ymmA=(00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C
319                                      ;       0G 0K 0O 0S 1G 1K 1O 1S 2G 2K 2O 2S 3G 3K 3O 3S)
320    vpunpckhwd  ymmB, ymmB, ymmF      ; ymmB=(01 05 09 0D 11 15 19 1D 21 25 29 2D 31 35 39 3D
321                                      ;       0H 0L 0P 0T 1H 1L 1P 1T 2H 2L 2P 2T 3H 3L 3P 3T)
322
323    vmovdqa     ymmG, ymmD
324    vpunpcklwd  ymmD, ymmD, ymmC      ; ymmD=(02 06 0A 0E 12 16 1A 1E 22 26 2A 2E 32 36 3A 3E
325                                      ;       0I 0M 0Q 0U 1I 1M 1Q 1U 2I 2M 2Q 2U 3I 3M 3Q 3U)
326    vpunpckhwd  ymmG, ymmG, ymmC      ; ymmG=(03 07 0B 0F 13 17 1B 1F 23 27 2B 2F 33 37 3B 3F
327                                      ;       0J 0N 0R 0V 1J 1N 1R 1V 2J 2N 2R 2V 3J 3N 3R 3V)
328
329    vmovdqa     ymmE, ymmA
330    vpunpcklbw  ymmA, ymmA, ymmD      ; ymmA=(00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E
331                                      ;       0G 0I 0K 0M 0O 0Q 0S 0U 1G 1I 1K 1M 1O 1Q 1S 1U)
332    vpunpckhbw  ymmE, ymmE, ymmD      ; ymmE=(20 22 24 26 28 2A 2C 2E 30 32 34 36 38 3A 3C 3E
333                                      ;       2G 2I 2K 2M 2O 2Q 2S 2U 3G 3I 3K 3M 3O 3Q 3S 3U)
334
335    vmovdqa     ymmH, ymmB
336    vpunpcklbw  ymmB, ymmB, ymmG      ; ymmB=(01 03 05 07 09 0B 0D 0F 11 13 15 17 19 1B 1D 1F
337                                      ;       0H 0J 0L 0N 0P 0R 0T 0V 1H 1J 1L 1N 1P 1R 1T 1V)
338    vpunpckhbw  ymmH, ymmH, ymmG      ; ymmH=(21 23 25 27 29 2B 2D 2F 31 33 35 37 39 3B 3D 3F
339                                      ;       2H 2J 2L 2N 2P 2R 2T 2V 3H 3J 3L 3N 3P 3R 3T 3V)
340
341    vpxor       ymmF, ymmF, ymmF
342
343    vmovdqa     ymmC, ymmA
344    vpunpcklbw  ymmA, ymmA, ymmF      ; ymmA=(00 02 04 06 08 0A 0C 0E 0G 0I 0K 0M 0O 0Q 0S 0U)
345    vpunpckhbw  ymmC, ymmC, ymmF      ; ymmC=(10 12 14 16 18 1A 1C 1E 1G 1I 1K 1M 1O 1Q 1S 1U)
346
347    vmovdqa     ymmD, ymmB
348    vpunpcklbw  ymmB, ymmB, ymmF      ; ymmB=(01 03 05 07 09 0B 0D 0F 0H 0J 0L 0N 0P 0R 0T 0V)
349    vpunpckhbw  ymmD, ymmD, ymmF      ; ymmD=(11 13 15 17 19 1B 1D 1F 1H 1J 1L 1N 1P 1R 1T 1V)
350
351    vmovdqa     ymmG, ymmE
352    vpunpcklbw  ymmE, ymmE, ymmF      ; ymmE=(20 22 24 26 28 2A 2C 2E 2G 2I 2K 2M 2O 2Q 2S 2U)
353    vpunpckhbw  ymmG, ymmG, ymmF      ; ymmG=(30 32 34 36 38 3A 3C 3E 3G 3I 3K 3M 3O 3Q 3S 3U)
354
355    vpunpcklbw  ymmF, ymmF, ymmH
356    vpunpckhbw  ymmH, ymmH, ymmH
357    vpsrlw      ymmF, ymmF, BYTE_BIT  ; ymmF=(21 23 25 27 29 2B 2D 2F 2H 2J 2L 2N 2P 2R 2T 2V)
358    vpsrlw      ymmH, ymmH, BYTE_BIT  ; ymmH=(31 33 35 37 39 3B 3D 3F 3H 3J 3L 3N 3P 3R 3T 3V)
359
360%endif  ; RGB_PIXELSIZE ; ---------------
361
362    ; ymm0=R(02468ACEGIKMOQSU)=RE, ymm2=G(02468ACEGIKMOQSU)=GE, ymm4=B(02468ACEGIKMOQSU)=BE
363    ; ymm1=R(13579BDFHJLNPRTV)=RO, ymm3=G(13579BDFHJLNPRTV)=GO, ymm5=B(13579BDFHJLNPRTV)=BO
364
365    ; (Original)
366    ; Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
367    ;
368    ; (This implementation)
369    ; Y  =  0.29900 * R + 0.33700 * G + 0.11400 * B + 0.25000 * G
370
371    vmovdqa     ymm6, ymm1
372    vpunpcklwd  ymm1, ymm1, ymm3
373    vpunpckhwd  ymm6, ymm6, ymm3
374    vpmaddwd    ymm1, ymm1, [GOTOFF(eax,PW_F0299_F0337)]  ; ymm1=ROL*FIX(0.299)+GOL*FIX(0.337)
375    vpmaddwd    ymm6, ymm6, [GOTOFF(eax,PW_F0299_F0337)]  ; ymm6=ROH*FIX(0.299)+GOH*FIX(0.337)
376
377    vmovdqa     ymm7, ymm6              ; ymm7=ROH*FIX(0.299)+GOH*FIX(0.337)
378
379    vmovdqa     ymm6, ymm0
380    vpunpcklwd  ymm0, ymm0, ymm2
381    vpunpckhwd  ymm6, ymm6, ymm2
382    vpmaddwd    ymm0, ymm0, [GOTOFF(eax,PW_F0299_F0337)]  ; ymm0=REL*FIX(0.299)+GEL*FIX(0.337)
383    vpmaddwd    ymm6, ymm6, [GOTOFF(eax,PW_F0299_F0337)]  ; ymm6=REH*FIX(0.299)+GEH*FIX(0.337)
384
385    vmovdqa     YMMWORD [wk(0)], ymm0   ; wk(0)=REL*FIX(0.299)+GEL*FIX(0.337)
386    vmovdqa     YMMWORD [wk(1)], ymm6   ; wk(1)=REH*FIX(0.299)+GEH*FIX(0.337)
387
388    vmovdqa     ymm0, ymm5              ; ymm0=BO
389    vmovdqa     ymm6, ymm4              ; ymm6=BE
390
391    vmovdqa     ymm4, ymm0
392    vpunpcklwd  ymm0, ymm0, ymm3
393    vpunpckhwd  ymm4, ymm4, ymm3
394    vpmaddwd    ymm0, ymm0, [GOTOFF(eax,PW_F0114_F0250)]  ; ymm0=BOL*FIX(0.114)+GOL*FIX(0.250)
395    vpmaddwd    ymm4, ymm4, [GOTOFF(eax,PW_F0114_F0250)]  ; ymm4=BOH*FIX(0.114)+GOH*FIX(0.250)
396
397    vmovdqa     ymm3, [GOTOFF(eax,PD_ONEHALF)]            ; ymm3=[PD_ONEHALF]
398
399    vpaddd      ymm0, ymm0, ymm1
400    vpaddd      ymm4, ymm4, ymm7
401    vpaddd      ymm0, ymm0, ymm3
402    vpaddd      ymm4, ymm4, ymm3
403    vpsrld      ymm0, ymm0, SCALEBITS   ; ymm0=YOL
404    vpsrld      ymm4, ymm4, SCALEBITS   ; ymm4=YOH
405    vpackssdw   ymm0, ymm0, ymm4        ; ymm0=YO
406
407    vmovdqa     ymm4, ymm6
408    vpunpcklwd  ymm6, ymm6, ymm2
409    vpunpckhwd  ymm4, ymm4, ymm2
410    vpmaddwd    ymm6, ymm6, [GOTOFF(eax,PW_F0114_F0250)]  ; ymm6=BEL*FIX(0.114)+GEL*FIX(0.250)
411    vpmaddwd    ymm4, ymm4, [GOTOFF(eax,PW_F0114_F0250)]  ; ymm4=BEH*FIX(0.114)+GEH*FIX(0.250)
412
413    vmovdqa     ymm2, [GOTOFF(eax,PD_ONEHALF)]            ; ymm2=[PD_ONEHALF]
414
415    vpaddd      ymm6, ymm6, YMMWORD [wk(0)]
416    vpaddd      ymm4, ymm4, YMMWORD [wk(1)]
417    vpaddd      ymm6, ymm6, ymm2
418    vpaddd      ymm4, ymm4, ymm2
419    vpsrld      ymm6, ymm6, SCALEBITS   ; ymm6=YEL
420    vpsrld      ymm4, ymm4, SCALEBITS   ; ymm4=YEH
421    vpackssdw   ymm6, ymm6, ymm4        ; ymm6=YE
422
423    vpsllw      ymm0, ymm0, BYTE_BIT
424    vpor        ymm6, ymm6, ymm0        ; ymm6=Y
425    vmovdqu     YMMWORD [edi], ymm6     ; Save Y
426
427    sub         ecx, byte SIZEOF_YMMWORD
428    add         esi, RGB_PIXELSIZE*SIZEOF_YMMWORD  ; inptr
429    add         edi, byte SIZEOF_YMMWORD           ; outptr0
430    cmp         ecx, byte SIZEOF_YMMWORD
431    jae         near .columnloop
432    test        ecx, ecx
433    jnz         near .column_ld1
434
435    pop         ecx                     ; col
436    pop         esi
437    pop         edi
438    poppic      eax
439
440    add         esi, byte SIZEOF_JSAMPROW  ; input_buf
441    add         edi, byte SIZEOF_JSAMPROW
442    dec         eax                        ; num_rows
443    jg          near .rowloop
444
445.return:
446    vzeroupper
447    pop         edi
448    pop         esi
449;   pop         edx                     ; need not be preserved
450;   pop         ecx                     ; need not be preserved
451    pop         ebx
452    mov         esp, ebp                ; esp <- aligned ebp
453    pop         esp                     ; esp <- original ebp
454    pop         ebp
455    ret
456
457; For some reason, the OS X linker does not honor the request to align the
458; segment unless we do this.
459    align       32
460