Imported Upstream version 1.5.3
[platform/upstream/libjpeg-turbo.git] / simd / jquanti-sse2.asm
1 ;
2 ; jquanti.asm - sample data conversion and quantization (SSE2)
3 ;
4 ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5 ;
6 ; Based on the x86 SIMD extension for IJG JPEG library
7 ; Copyright (C) 1999-2006, MIYASAKA Masaru.
8 ; For conditions of distribution and use, see copyright notice in jsimdext.inc
9 ;
10 ; This file should be assembled with NASM (Netwide Assembler),
11 ; can *not* be assembled with Microsoft's MASM or any compatible
12 ; assembler (including Borland's Turbo Assembler).
13 ; NASM is available from http://nasm.sourceforge.net/ or
14 ; http://sourceforge.net/project/showfiles.php?group_id=6208
15 ;
16 ; [TAB8]
17
18 %include "jsimdext.inc"
19 %include "jdct.inc"
20
21 ; --------------------------------------------------------------------------
22         SECTION SEG_TEXT
23         BITS    32
24 ;
25 ; Load data into workspace, applying unsigned->signed conversion
26 ;
27 ; GLOBAL(void)
28 ; jsimd_convsamp_sse2 (JSAMPARRAY sample_data, JDIMENSION start_col,
29 ;                      DCTELEM *workspace);
30 ;
31
32 %define sample_data     ebp+8           ; JSAMPARRAY sample_data
33 %define start_col       ebp+12          ; JDIMENSION start_col
34 %define workspace       ebp+16          ; DCTELEM *workspace
35
36         align   16
37         global  EXTN(jsimd_convsamp_sse2)
38
39 EXTN(jsimd_convsamp_sse2):
40         push    ebp
41         mov     ebp,esp
42         push    ebx
43 ;       push    ecx             ; need not be preserved
44 ;       push    edx             ; need not be preserved
45         push    esi
46         push    edi
47
48         pxor    xmm6,xmm6               ; xmm6=(all 0's)
49         pcmpeqw xmm7,xmm7
50         psllw   xmm7,7                  ; xmm7={0xFF80 0xFF80 0xFF80 0xFF80 ..}
51
52         mov     esi, JSAMPARRAY [sample_data]   ; (JSAMPROW *)
53         mov     eax, JDIMENSION [start_col]
54         mov     edi, POINTER [workspace]        ; (DCTELEM *)
55         mov     ecx, DCTSIZE/4
56         alignx  16,7
57 .convloop:
58         mov     ebx, JSAMPROW [esi+0*SIZEOF_JSAMPROW]   ; (JSAMPLE *)
59         mov     edx, JSAMPROW [esi+1*SIZEOF_JSAMPROW]   ; (JSAMPLE *)
60
61         movq    xmm0, XMM_MMWORD [ebx+eax*SIZEOF_JSAMPLE]       ; xmm0=(01234567)
62         movq    xmm1, XMM_MMWORD [edx+eax*SIZEOF_JSAMPLE]       ; xmm1=(89ABCDEF)
63
64         mov     ebx, JSAMPROW [esi+2*SIZEOF_JSAMPROW]   ; (JSAMPLE *)
65         mov     edx, JSAMPROW [esi+3*SIZEOF_JSAMPROW]   ; (JSAMPLE *)
66
67         movq    xmm2, XMM_MMWORD [ebx+eax*SIZEOF_JSAMPLE]       ; xmm2=(GHIJKLMN)
68         movq    xmm3, XMM_MMWORD [edx+eax*SIZEOF_JSAMPLE]       ; xmm3=(OPQRSTUV)
69
70         punpcklbw xmm0,xmm6             ; xmm0=(01234567)
71         punpcklbw xmm1,xmm6             ; xmm1=(89ABCDEF)
72         paddw     xmm0,xmm7
73         paddw     xmm1,xmm7
74         punpcklbw xmm2,xmm6             ; xmm2=(GHIJKLMN)
75         punpcklbw xmm3,xmm6             ; xmm3=(OPQRSTUV)
76         paddw     xmm2,xmm7
77         paddw     xmm3,xmm7
78
79         movdqa  XMMWORD [XMMBLOCK(0,0,edi,SIZEOF_DCTELEM)], xmm0
80         movdqa  XMMWORD [XMMBLOCK(1,0,edi,SIZEOF_DCTELEM)], xmm1
81         movdqa  XMMWORD [XMMBLOCK(2,0,edi,SIZEOF_DCTELEM)], xmm2
82         movdqa  XMMWORD [XMMBLOCK(3,0,edi,SIZEOF_DCTELEM)], xmm3
83
84         add     esi, byte 4*SIZEOF_JSAMPROW
85         add     edi, byte 4*DCTSIZE*SIZEOF_DCTELEM
86         dec     ecx
87         jnz     short .convloop
88
89         pop     edi
90         pop     esi
91 ;       pop     edx             ; need not be preserved
92 ;       pop     ecx             ; need not be preserved
93         pop     ebx
94         pop     ebp
95         ret
96
97 ; --------------------------------------------------------------------------
98 ;
99 ; Quantize/descale the coefficients, and store into coef_block
100 ;
101 ; This implementation is based on an algorithm described in
102 ;   "How to optimize for the Pentium family of microprocessors"
103 ;   (http://www.agner.org/assem/).
104 ;
105 ; GLOBAL(void)
106 ; jsimd_quantize_sse2 (JCOEFPTR coef_block, DCTELEM *divisors,
107 ;                      DCTELEM *workspace);
108 ;
109
110 %define RECIPROCAL(m,n,b) XMMBLOCK(DCTSIZE*0+(m),(n),(b),SIZEOF_DCTELEM)
111 %define CORRECTION(m,n,b) XMMBLOCK(DCTSIZE*1+(m),(n),(b),SIZEOF_DCTELEM)
112 %define SCALE(m,n,b)      XMMBLOCK(DCTSIZE*2+(m),(n),(b),SIZEOF_DCTELEM)
113
114 %define coef_block      ebp+8           ; JCOEFPTR coef_block
115 %define divisors        ebp+12          ; DCTELEM *divisors
116 %define workspace       ebp+16          ; DCTELEM *workspace
117
118         align   16
119         global  EXTN(jsimd_quantize_sse2)
120
121 EXTN(jsimd_quantize_sse2):
122         push    ebp
123         mov     ebp,esp
124 ;       push    ebx             ; unused
125 ;       push    ecx             ; unused
126 ;       push    edx             ; need not be preserved
127         push    esi
128         push    edi
129
130         mov     esi, POINTER [workspace]
131         mov     edx, POINTER [divisors]
132         mov     edi, JCOEFPTR [coef_block]
133         mov     eax, DCTSIZE2/32
134         alignx  16,7
135 .quantloop:
136         movdqa  xmm4, XMMWORD [XMMBLOCK(0,0,esi,SIZEOF_DCTELEM)]
137         movdqa  xmm5, XMMWORD [XMMBLOCK(1,0,esi,SIZEOF_DCTELEM)]
138         movdqa  xmm6, XMMWORD [XMMBLOCK(2,0,esi,SIZEOF_DCTELEM)]
139         movdqa  xmm7, XMMWORD [XMMBLOCK(3,0,esi,SIZEOF_DCTELEM)]
140         movdqa  xmm0,xmm4
141         movdqa  xmm1,xmm5
142         movdqa  xmm2,xmm6
143         movdqa  xmm3,xmm7
144         psraw   xmm4,(WORD_BIT-1)
145         psraw   xmm5,(WORD_BIT-1)
146         psraw   xmm6,(WORD_BIT-1)
147         psraw   xmm7,(WORD_BIT-1)
148         pxor    xmm0,xmm4
149         pxor    xmm1,xmm5
150         pxor    xmm2,xmm6
151         pxor    xmm3,xmm7
152         psubw   xmm0,xmm4               ; if (xmm0 < 0) xmm0 = -xmm0;
153         psubw   xmm1,xmm5               ; if (xmm1 < 0) xmm1 = -xmm1;
154         psubw   xmm2,xmm6               ; if (xmm2 < 0) xmm2 = -xmm2;
155         psubw   xmm3,xmm7               ; if (xmm3 < 0) xmm3 = -xmm3;
156
157         paddw   xmm0, XMMWORD [CORRECTION(0,0,edx)]  ; correction + roundfactor
158         paddw   xmm1, XMMWORD [CORRECTION(1,0,edx)]
159         paddw   xmm2, XMMWORD [CORRECTION(2,0,edx)]
160         paddw   xmm3, XMMWORD [CORRECTION(3,0,edx)]
161         pmulhuw xmm0, XMMWORD [RECIPROCAL(0,0,edx)]  ; reciprocal
162         pmulhuw xmm1, XMMWORD [RECIPROCAL(1,0,edx)]
163         pmulhuw xmm2, XMMWORD [RECIPROCAL(2,0,edx)]
164         pmulhuw xmm3, XMMWORD [RECIPROCAL(3,0,edx)]
165         pmulhuw xmm0, XMMWORD [SCALE(0,0,edx)]  ; scale
166         pmulhuw xmm1, XMMWORD [SCALE(1,0,edx)]
167         pmulhuw xmm2, XMMWORD [SCALE(2,0,edx)]
168         pmulhuw xmm3, XMMWORD [SCALE(3,0,edx)]
169
170         pxor    xmm0,xmm4
171         pxor    xmm1,xmm5
172         pxor    xmm2,xmm6
173         pxor    xmm3,xmm7
174         psubw   xmm0,xmm4
175         psubw   xmm1,xmm5
176         psubw   xmm2,xmm6
177         psubw   xmm3,xmm7
178         movdqa  XMMWORD [XMMBLOCK(0,0,edi,SIZEOF_DCTELEM)], xmm0
179         movdqa  XMMWORD [XMMBLOCK(1,0,edi,SIZEOF_DCTELEM)], xmm1
180         movdqa  XMMWORD [XMMBLOCK(2,0,edi,SIZEOF_DCTELEM)], xmm2
181         movdqa  XMMWORD [XMMBLOCK(3,0,edi,SIZEOF_DCTELEM)], xmm3
182
183         add     esi, byte 32*SIZEOF_DCTELEM
184         add     edx, byte 32*SIZEOF_DCTELEM
185         add     edi, byte 32*SIZEOF_JCOEF
186         dec     eax
187         jnz     near .quantloop
188
189         pop     edi
190         pop     esi
191 ;       pop     edx             ; need not be preserved
192 ;       pop     ecx             ; unused
193 ;       pop     ebx             ; unused
194         pop     ebp
195         ret
196
197 ; For some reason, the OS X linker does not honor the request to align the
198 ; segment unless we do this.
199         align   16