2 ; jcsample.asm - downsampling (SSE2)
4 ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5 ; Copyright (C) 2016, D. R. Commander.
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
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
19 %include "jsimdext.inc"
21 ; --------------------------------------------------------------------------
25 ; Downsample pixel values of a single component.
26 ; This version handles the common case of 2:1 horizontal and 1:1 vertical,
30 ; jsimd_h2v1_downsample_sse2(JDIMENSION image_width, int max_v_samp_factor,
31 ; JDIMENSION v_samp_factor,
32 ; JDIMENSION width_in_blocks, JSAMPARRAY input_data,
33 ; JSAMPARRAY output_data);
36 %define img_width(b) (b) + 8 ; JDIMENSION image_width
37 %define max_v_samp(b) (b) + 12 ; int max_v_samp_factor
38 %define v_samp(b) (b) + 16 ; JDIMENSION v_samp_factor
39 %define width_blks(b) (b) + 20 ; JDIMENSION width_in_blocks
40 %define input_data(b) (b) + 24 ; JSAMPARRAY input_data
41 %define output_data(b) (b) + 28 ; JSAMPARRAY output_data
44 GLOBAL_FUNCTION(jsimd_h2v1_downsample_sse2)
46 EXTN(jsimd_h2v1_downsample_sse2):
50 ; push ecx ; need not be preserved
51 ; push edx ; need not be preserved
55 mov ecx, JDIMENSION [width_blks(ebp)]
56 shl ecx, 3 ; imul ecx,DCTSIZE (ecx = output_cols)
59 mov edx, JDIMENSION [img_width(ebp)]
61 ; -- expand_right_edge
64 shl ecx, 1 ; output_cols * 2
68 mov eax, INT [max_v_samp(ebp)]
73 mov esi, JSAMPARRAY [input_data(ebp)] ; input_data
79 mov edi, JSAMPROW [esi]
81 mov al, JSAMPLE [edi-1]
88 add esi, byte SIZEOF_JSAMPROW
97 mov eax, JDIMENSION [v_samp(ebp)] ; rowctr
101 mov edx, 0x00010000 ; bias pattern
104 pshufd xmm7, xmm7, 0x00 ; xmm7={0, 1, 0, 1, 0, 1, 0, 1}
105 psrlw xmm6, BYTE_BIT ; xmm6={0xFF 0x00 0xFF 0x00 ..}
107 mov esi, JSAMPARRAY [input_data(ebp)] ; input_data
108 mov edi, JSAMPARRAY [output_data(ebp)] ; output_data
115 mov esi, JSAMPROW [esi] ; inptr
116 mov edi, JSAMPROW [edi] ; outptr
118 cmp ecx, byte SIZEOF_XMMWORD
119 jae short .columnloop
123 movdqa xmm0, XMMWORD [esi+0*SIZEOF_XMMWORD]
125 mov ecx, SIZEOF_XMMWORD
126 jmp short .downsample
130 movdqa xmm0, XMMWORD [esi+0*SIZEOF_XMMWORD]
131 movdqa xmm1, XMMWORD [esi+1*SIZEOF_XMMWORD]
151 movdqa XMMWORD [edi+0*SIZEOF_XMMWORD], xmm0
153 sub ecx, byte SIZEOF_XMMWORD ; outcol
154 add esi, byte 2*SIZEOF_XMMWORD ; inptr
155 add edi, byte 1*SIZEOF_XMMWORD ; outptr
156 cmp ecx, byte SIZEOF_XMMWORD
157 jae short .columnloop
159 jnz short .columnloop_r8
165 add esi, byte SIZEOF_JSAMPROW ; input_data
166 add edi, byte SIZEOF_JSAMPROW ; output_data
173 ; pop edx ; need not be preserved
174 ; pop ecx ; need not be preserved
179 ; --------------------------------------------------------------------------
181 ; Downsample pixel values of a single component.
182 ; This version handles the standard case of 2:1 horizontal and 2:1 vertical,
186 ; jsimd_h2v2_downsample_sse2(JDIMENSION image_width, int max_v_samp_factor,
187 ; JDIMENSION v_samp_factor,
188 ; JDIMENSION width_in_blocks, JSAMPARRAY input_data,
189 ; JSAMPARRAY output_data);
192 %define img_width(b) (b) + 8 ; JDIMENSION image_width
193 %define max_v_samp(b) (b) + 12 ; int max_v_samp_factor
194 %define v_samp(b) (b) + 16 ; JDIMENSION v_samp_factor
195 %define width_blks(b) (b) + 20 ; JDIMENSION width_in_blocks
196 %define input_data(b) (b) + 24 ; JSAMPARRAY input_data
197 %define output_data(b) (b) + 28 ; JSAMPARRAY output_data
200 GLOBAL_FUNCTION(jsimd_h2v2_downsample_sse2)
202 EXTN(jsimd_h2v2_downsample_sse2):
206 ; push ecx ; need not be preserved
207 ; push edx ; need not be preserved
211 mov ecx, JDIMENSION [width_blks(ebp)]
212 shl ecx, 3 ; imul ecx,DCTSIZE (ecx = output_cols)
215 mov edx, JDIMENSION [img_width(ebp)]
217 ; -- expand_right_edge
220 shl ecx, 1 ; output_cols * 2
222 jle short .expand_end
224 mov eax, INT [max_v_samp(ebp)]
226 jle short .expand_end
229 mov esi, JSAMPARRAY [input_data(ebp)] ; input_data
235 mov edi, JSAMPROW [esi]
237 mov al, JSAMPLE [edi-1]
244 add esi, byte SIZEOF_JSAMPROW
249 pop ecx ; output_cols
253 mov eax, JDIMENSION [v_samp(ebp)] ; rowctr
257 mov edx, 0x00020001 ; bias pattern
260 pshufd xmm7, xmm7, 0x00 ; xmm7={1, 2, 1, 2, 1, 2, 1, 2}
261 psrlw xmm6, BYTE_BIT ; xmm6={0xFF 0x00 0xFF 0x00 ..}
263 mov esi, JSAMPARRAY [input_data(ebp)] ; input_data
264 mov edi, JSAMPARRAY [output_data(ebp)] ; output_data
271 mov edx, JSAMPROW [esi+0*SIZEOF_JSAMPROW] ; inptr0
272 mov esi, JSAMPROW [esi+1*SIZEOF_JSAMPROW] ; inptr1
273 mov edi, JSAMPROW [edi] ; outptr
275 cmp ecx, byte SIZEOF_XMMWORD
276 jae short .columnloop
280 movdqa xmm0, XMMWORD [edx+0*SIZEOF_XMMWORD]
281 movdqa xmm1, XMMWORD [esi+0*SIZEOF_XMMWORD]
284 mov ecx, SIZEOF_XMMWORD
285 jmp short .downsample
289 movdqa xmm0, XMMWORD [edx+0*SIZEOF_XMMWORD]
290 movdqa xmm1, XMMWORD [esi+0*SIZEOF_XMMWORD]
291 movdqa xmm2, XMMWORD [edx+1*SIZEOF_XMMWORD]
292 movdqa xmm3, XMMWORD [esi+1*SIZEOF_XMMWORD]
322 movdqa XMMWORD [edi+0*SIZEOF_XMMWORD], xmm0
324 sub ecx, byte SIZEOF_XMMWORD ; outcol
325 add edx, byte 2*SIZEOF_XMMWORD ; inptr0
326 add esi, byte 2*SIZEOF_XMMWORD ; inptr1
327 add edi, byte 1*SIZEOF_XMMWORD ; outptr
328 cmp ecx, byte SIZEOF_XMMWORD
331 jnz near .columnloop_r8
337 add esi, byte 2*SIZEOF_JSAMPROW ; input_data
338 add edi, byte 1*SIZEOF_JSAMPROW ; output_data
345 ; pop edx ; need not be preserved
346 ; pop ecx ; need not be preserved
351 ; For some reason, the OS X linker does not honor the request to align the
352 ; segment unless we do this.