2 ; jfdctfst.asm - fast integer FDCT (64-bit SSE2)
4 ; Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
5 ; Copyright (C) 2009, 2016, D. R. Commander.
6 ; Copyright (C) 2023, Aliaksiej Kandracienka.
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
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
18 ; This file contains a fast, not so accurate integer implementation of
19 ; the forward DCT (Discrete Cosine Transform). The following code is
20 ; based directly on the IJG's original jfdctfst.c; see the jfdctfst.c
23 %include "jsimdext.inc"
26 ; --------------------------------------------------------------------------
28 %define CONST_BITS 8 ; 14 is also OK.
31 F_0_382 equ 98 ; FIX(0.382683433)
32 F_0_541 equ 139 ; FIX(0.541196100)
33 F_0_707 equ 181 ; FIX(0.707106781)
34 F_1_306 equ 334 ; FIX(1.306562965)
36 ; NASM cannot do compile-time arithmetic on floating-point constants.
37 %define DESCALE(x, n) (((x) + (1 << ((n) - 1))) >> (n))
38 F_0_382 equ DESCALE( 410903207, 30 - CONST_BITS) ; FIX(0.382683433)
39 F_0_541 equ DESCALE( 581104887, 30 - CONST_BITS) ; FIX(0.541196100)
40 F_0_707 equ DESCALE( 759250124, 30 - CONST_BITS) ; FIX(0.707106781)
41 F_1_306 equ DESCALE(1402911301, 30 - CONST_BITS) ; FIX(1.306562965)
44 ; --------------------------------------------------------------------------
47 ; PRE_MULTIPLY_SCALE_BITS <= 2 (to avoid overflow)
48 ; CONST_BITS + CONST_SHIFT + PRE_MULTIPLY_SCALE_BITS == 16 (for pmulhw)
50 %define PRE_MULTIPLY_SCALE_BITS 2
51 %define CONST_SHIFT (16 - PRE_MULTIPLY_SCALE_BITS - CONST_BITS)
54 GLOBAL_DATA(jconst_fdct_ifast_sse2)
56 EXTN(jconst_fdct_ifast_sse2):
58 PW_F0707 times 8 dw F_0_707 << CONST_SHIFT
59 PW_F0382 times 8 dw F_0_382 << CONST_SHIFT
60 PW_F0541 times 8 dw F_0_541 << CONST_SHIFT
61 PW_F1306 times 8 dw F_1_306 << CONST_SHIFT
65 ; --------------------------------------------------------------------------
69 ; Perform the forward DCT on one block of samples.
72 ; jsimd_fdct_ifast_sse2(DCTELEM *data)
77 %define wk(i) r15 - (WK_NUM - (i)) * SIZEOF_XMMWORD ; xmmword wk[WK_NUM]
81 GLOBAL_FUNCTION(jsimd_fdct_ifast_sse2)
83 EXTN(jsimd_fdct_ifast_sse2):
87 and rsp, byte (-SIZEOF_XMMWORD) ; align to 128 bits
88 ; Allocate stack space for wk array. r15 is used to access it.
90 sub rsp, byte (SIZEOF_XMMWORD * WK_NUM)
93 ; ---- Pass 1: process rows.
95 mov rdx, r10 ; (DCTELEM *)
97 movdqa xmm0, XMMWORD [XMMBLOCK(0,0,rdx,SIZEOF_DCTELEM)]
98 movdqa xmm1, XMMWORD [XMMBLOCK(1,0,rdx,SIZEOF_DCTELEM)]
99 movdqa xmm2, XMMWORD [XMMBLOCK(2,0,rdx,SIZEOF_DCTELEM)]
100 movdqa xmm3, XMMWORD [XMMBLOCK(3,0,rdx,SIZEOF_DCTELEM)]
102 ; xmm0=(00 01 02 03 04 05 06 07), xmm2=(20 21 22 23 24 25 26 27)
103 ; xmm1=(10 11 12 13 14 15 16 17), xmm3=(30 31 32 33 34 35 36 37)
105 movdqa xmm4, xmm0 ; transpose coefficients(phase 1)
106 punpcklwd xmm0, xmm1 ; xmm0=(00 10 01 11 02 12 03 13)
107 punpckhwd xmm4, xmm1 ; xmm4=(04 14 05 15 06 16 07 17)
108 movdqa xmm5, xmm2 ; transpose coefficients(phase 1)
109 punpcklwd xmm2, xmm3 ; xmm2=(20 30 21 31 22 32 23 33)
110 punpckhwd xmm5, xmm3 ; xmm5=(24 34 25 35 26 36 27 37)
112 movdqa xmm6, XMMWORD [XMMBLOCK(4,0,rdx,SIZEOF_DCTELEM)]
113 movdqa xmm7, XMMWORD [XMMBLOCK(5,0,rdx,SIZEOF_DCTELEM)]
114 movdqa xmm1, XMMWORD [XMMBLOCK(6,0,rdx,SIZEOF_DCTELEM)]
115 movdqa xmm3, XMMWORD [XMMBLOCK(7,0,rdx,SIZEOF_DCTELEM)]
117 ; xmm6=( 4 12 20 28 36 44 52 60), xmm1=( 6 14 22 30 38 46 54 62)
118 ; xmm7=( 5 13 21 29 37 45 53 61), xmm3=( 7 15 23 31 39 47 55 63)
120 movdqa XMMWORD [wk(0)], xmm2 ; wk(0)=(20 30 21 31 22 32 23 33)
121 movdqa XMMWORD [wk(1)], xmm5 ; wk(1)=(24 34 25 35 26 36 27 37)
123 movdqa xmm2, xmm6 ; transpose coefficients(phase 1)
124 punpcklwd xmm6, xmm7 ; xmm6=(40 50 41 51 42 52 43 53)
125 punpckhwd xmm2, xmm7 ; xmm2=(44 54 45 55 46 56 47 57)
126 movdqa xmm5, xmm1 ; transpose coefficients(phase 1)
127 punpcklwd xmm1, xmm3 ; xmm1=(60 70 61 71 62 72 63 73)
128 punpckhwd xmm5, xmm3 ; xmm5=(64 74 65 75 66 76 67 77)
130 movdqa xmm7, xmm6 ; transpose coefficients(phase 2)
131 punpckldq xmm6, xmm1 ; xmm6=(40 50 60 70 41 51 61 71)
132 punpckhdq xmm7, xmm1 ; xmm7=(42 52 62 72 43 53 63 73)
133 movdqa xmm3, xmm2 ; transpose coefficients(phase 2)
134 punpckldq xmm2, xmm5 ; xmm2=(44 54 64 74 45 55 65 75)
135 punpckhdq xmm3, xmm5 ; xmm3=(46 56 66 76 47 57 67 77)
137 movdqa xmm1, XMMWORD [wk(0)] ; xmm1=(20 30 21 31 22 32 23 33)
138 movdqa xmm5, XMMWORD [wk(1)] ; xmm5=(24 34 25 35 26 36 27 37)
139 movdqa XMMWORD [wk(0)], xmm7 ; wk(0)=(42 52 62 72 43 53 63 73)
140 movdqa XMMWORD [wk(1)], xmm2 ; wk(1)=(44 54 64 74 45 55 65 75)
142 movdqa xmm7, xmm0 ; transpose coefficients(phase 2)
143 punpckldq xmm0, xmm1 ; xmm0=(00 10 20 30 01 11 21 31)
144 punpckhdq xmm7, xmm1 ; xmm7=(02 12 22 32 03 13 23 33)
145 movdqa xmm2, xmm4 ; transpose coefficients(phase 2)
146 punpckldq xmm4, xmm5 ; xmm4=(04 14 24 34 05 15 25 35)
147 punpckhdq xmm2, xmm5 ; xmm2=(06 16 26 36 07 17 27 37)
149 movdqa xmm1, xmm0 ; transpose coefficients(phase 3)
150 punpcklqdq xmm0, xmm6 ; xmm0=(00 10 20 30 40 50 60 70)=data0
151 punpckhqdq xmm1, xmm6 ; xmm1=(01 11 21 31 41 51 61 71)=data1
152 movdqa xmm5, xmm2 ; transpose coefficients(phase 3)
153 punpcklqdq xmm2, xmm3 ; xmm2=(06 16 26 36 46 56 66 76)=data6
154 punpckhqdq xmm5, xmm3 ; xmm5=(07 17 27 37 47 57 67 77)=data7
158 psubw xmm1, xmm2 ; xmm1=data1-data6=tmp6
159 psubw xmm0, xmm5 ; xmm0=data0-data7=tmp7
160 paddw xmm6, xmm2 ; xmm6=data1+data6=tmp1
161 paddw xmm3, xmm5 ; xmm3=data0+data7=tmp0
163 movdqa xmm2, XMMWORD [wk(0)] ; xmm2=(42 52 62 72 43 53 63 73)
164 movdqa xmm5, XMMWORD [wk(1)] ; xmm5=(44 54 64 74 45 55 65 75)
165 movdqa XMMWORD [wk(0)], xmm1 ; wk(0)=tmp6
166 movdqa XMMWORD [wk(1)], xmm0 ; wk(1)=tmp7
168 movdqa xmm1, xmm7 ; transpose coefficients(phase 3)
169 punpcklqdq xmm7, xmm2 ; xmm7=(02 12 22 32 42 52 62 72)=data2
170 punpckhqdq xmm1, xmm2 ; xmm1=(03 13 23 33 43 53 63 73)=data3
171 movdqa xmm0, xmm4 ; transpose coefficients(phase 3)
172 punpcklqdq xmm4, xmm5 ; xmm4=(04 14 24 34 44 54 64 74)=data4
173 punpckhqdq xmm0, xmm5 ; xmm0=(05 15 25 35 45 55 65 75)=data5
177 paddw xmm1, xmm4 ; xmm1=data3+data4=tmp3
178 paddw xmm7, xmm0 ; xmm7=data2+data5=tmp2
179 psubw xmm2, xmm4 ; xmm2=data3-data4=tmp4
180 psubw xmm5, xmm0 ; xmm5=data2-data5=tmp5
186 psubw xmm3, xmm1 ; xmm3=tmp13
187 psubw xmm6, xmm7 ; xmm6=tmp12
188 paddw xmm4, xmm1 ; xmm4=tmp10
189 paddw xmm0, xmm7 ; xmm0=tmp11
192 psllw xmm6, PRE_MULTIPLY_SCALE_BITS
193 pmulhw xmm6, [rel PW_F0707] ; xmm6=z1
197 psubw xmm4, xmm0 ; xmm4=data4
198 psubw xmm3, xmm6 ; xmm3=data6
199 paddw xmm1, xmm0 ; xmm1=data0
200 paddw xmm7, xmm6 ; xmm7=data2
202 movdqa xmm0, XMMWORD [wk(0)] ; xmm0=tmp6
203 movdqa xmm6, XMMWORD [wk(1)] ; xmm6=tmp7
204 movdqa XMMWORD [wk(0)], xmm4 ; wk(0)=data4
205 movdqa XMMWORD [wk(1)], xmm3 ; wk(1)=data6
209 paddw xmm2, xmm5 ; xmm2=tmp10
210 paddw xmm5, xmm0 ; xmm5=tmp11
211 paddw xmm0, xmm6 ; xmm0=tmp12, xmm6=tmp7
213 psllw xmm2, PRE_MULTIPLY_SCALE_BITS
214 psllw xmm0, PRE_MULTIPLY_SCALE_BITS
216 psllw xmm5, PRE_MULTIPLY_SCALE_BITS
217 pmulhw xmm5, [rel PW_F0707] ; xmm5=z3
219 movdqa xmm4, xmm2 ; xmm4=tmp10
221 pmulhw xmm2, [rel PW_F0382] ; xmm2=z5
222 pmulhw xmm4, [rel PW_F0541] ; xmm4=MULTIPLY(tmp10,FIX_0_541196)
223 pmulhw xmm0, [rel PW_F1306] ; xmm0=MULTIPLY(tmp12,FIX_1_306562)
224 paddw xmm4, xmm2 ; xmm4=z2
225 paddw xmm0, xmm2 ; xmm0=z4
228 psubw xmm6, xmm5 ; xmm6=z13
229 paddw xmm3, xmm5 ; xmm3=z11
233 psubw xmm6, xmm4 ; xmm6=data3
234 psubw xmm3, xmm0 ; xmm3=data7
235 paddw xmm2, xmm4 ; xmm2=data5
236 paddw xmm5, xmm0 ; xmm5=data1
238 ; ---- Pass 2: process columns.
240 ; xmm1=(00 10 20 30 40 50 60 70), xmm7=(02 12 22 32 42 52 62 72)
241 ; xmm5=(01 11 21 31 41 51 61 71), xmm6=(03 13 23 33 43 53 63 73)
243 movdqa xmm4, xmm1 ; transpose coefficients(phase 1)
244 punpcklwd xmm1, xmm5 ; xmm1=(00 01 10 11 20 21 30 31)
245 punpckhwd xmm4, xmm5 ; xmm4=(40 41 50 51 60 61 70 71)
246 movdqa xmm0, xmm7 ; transpose coefficients(phase 1)
247 punpcklwd xmm7, xmm6 ; xmm7=(02 03 12 13 22 23 32 33)
248 punpckhwd xmm0, xmm6 ; xmm0=(42 43 52 53 62 63 72 73)
250 movdqa xmm5, XMMWORD [wk(0)] ; xmm5=col4
251 movdqa xmm6, XMMWORD [wk(1)] ; xmm6=col6
253 ; xmm5=(04 14 24 34 44 54 64 74), xmm6=(06 16 26 36 46 56 66 76)
254 ; xmm2=(05 15 25 35 45 55 65 75), xmm3=(07 17 27 37 47 57 67 77)
256 movdqa XMMWORD [wk(0)], xmm7 ; wk(0)=(02 03 12 13 22 23 32 33)
257 movdqa XMMWORD [wk(1)], xmm0 ; wk(1)=(42 43 52 53 62 63 72 73)
259 movdqa xmm7, xmm5 ; transpose coefficients(phase 1)
260 punpcklwd xmm5, xmm2 ; xmm5=(04 05 14 15 24 25 34 35)
261 punpckhwd xmm7, xmm2 ; xmm7=(44 45 54 55 64 65 74 75)
262 movdqa xmm0, xmm6 ; transpose coefficients(phase 1)
263 punpcklwd xmm6, xmm3 ; xmm6=(06 07 16 17 26 27 36 37)
264 punpckhwd xmm0, xmm3 ; xmm0=(46 47 56 57 66 67 76 77)
266 movdqa xmm2, xmm5 ; transpose coefficients(phase 2)
267 punpckldq xmm5, xmm6 ; xmm5=(04 05 06 07 14 15 16 17)
268 punpckhdq xmm2, xmm6 ; xmm2=(24 25 26 27 34 35 36 37)
269 movdqa xmm3, xmm7 ; transpose coefficients(phase 2)
270 punpckldq xmm7, xmm0 ; xmm7=(44 45 46 47 54 55 56 57)
271 punpckhdq xmm3, xmm0 ; xmm3=(64 65 66 67 74 75 76 77)
273 movdqa xmm6, XMMWORD [wk(0)] ; xmm6=(02 03 12 13 22 23 32 33)
274 movdqa xmm0, XMMWORD [wk(1)] ; xmm0=(42 43 52 53 62 63 72 73)
275 movdqa XMMWORD [wk(0)], xmm2 ; wk(0)=(24 25 26 27 34 35 36 37)
276 movdqa XMMWORD [wk(1)], xmm7 ; wk(1)=(44 45 46 47 54 55 56 57)
278 movdqa xmm2, xmm1 ; transpose coefficients(phase 2)
279 punpckldq xmm1, xmm6 ; xmm1=(00 01 02 03 10 11 12 13)
280 punpckhdq xmm2, xmm6 ; xmm2=(20 21 22 23 30 31 32 33)
281 movdqa xmm7, xmm4 ; transpose coefficients(phase 2)
282 punpckldq xmm4, xmm0 ; xmm4=(40 41 42 43 50 51 52 53)
283 punpckhdq xmm7, xmm0 ; xmm7=(60 61 62 63 70 71 72 73)
285 movdqa xmm6, xmm1 ; transpose coefficients(phase 3)
286 punpcklqdq xmm1, xmm5 ; xmm1=(00 01 02 03 04 05 06 07)=data0
287 punpckhqdq xmm6, xmm5 ; xmm6=(10 11 12 13 14 15 16 17)=data1
288 movdqa xmm0, xmm7 ; transpose coefficients(phase 3)
289 punpcklqdq xmm7, xmm3 ; xmm7=(60 61 62 63 64 65 66 67)=data6
290 punpckhqdq xmm0, xmm3 ; xmm0=(70 71 72 73 74 75 76 77)=data7
294 psubw xmm6, xmm7 ; xmm6=data1-data6=tmp6
295 psubw xmm1, xmm0 ; xmm1=data0-data7=tmp7
296 paddw xmm5, xmm7 ; xmm5=data1+data6=tmp1
297 paddw xmm3, xmm0 ; xmm3=data0+data7=tmp0
299 movdqa xmm7, XMMWORD [wk(0)] ; xmm7=(24 25 26 27 34 35 36 37)
300 movdqa xmm0, XMMWORD [wk(1)] ; xmm0=(44 45 46 47 54 55 56 57)
301 movdqa XMMWORD [wk(0)], xmm6 ; wk(0)=tmp6
302 movdqa XMMWORD [wk(1)], xmm1 ; wk(1)=tmp7
304 movdqa xmm6, xmm2 ; transpose coefficients(phase 3)
305 punpcklqdq xmm2, xmm7 ; xmm2=(20 21 22 23 24 25 26 27)=data2
306 punpckhqdq xmm6, xmm7 ; xmm6=(30 31 32 33 34 35 36 37)=data3
307 movdqa xmm1, xmm4 ; transpose coefficients(phase 3)
308 punpcklqdq xmm4, xmm0 ; xmm4=(40 41 42 43 44 45 46 47)=data4
309 punpckhqdq xmm1, xmm0 ; xmm1=(50 51 52 53 54 55 56 57)=data5
313 paddw xmm6, xmm4 ; xmm6=data3+data4=tmp3
314 paddw xmm2, xmm1 ; xmm2=data2+data5=tmp2
315 psubw xmm7, xmm4 ; xmm7=data3-data4=tmp4
316 psubw xmm0, xmm1 ; xmm0=data2-data5=tmp5
322 psubw xmm3, xmm6 ; xmm3=tmp13
323 psubw xmm5, xmm2 ; xmm5=tmp12
324 paddw xmm4, xmm6 ; xmm4=tmp10
325 paddw xmm1, xmm2 ; xmm1=tmp11
328 psllw xmm5, PRE_MULTIPLY_SCALE_BITS
329 pmulhw xmm5, [rel PW_F0707] ; xmm5=z1
333 psubw xmm4, xmm1 ; xmm4=data4
334 psubw xmm3, xmm5 ; xmm3=data6
335 paddw xmm6, xmm1 ; xmm6=data0
336 paddw xmm2, xmm5 ; xmm2=data2
338 movdqa XMMWORD [XMMBLOCK(4,0,rdx,SIZEOF_DCTELEM)], xmm4
339 movdqa XMMWORD [XMMBLOCK(6,0,rdx,SIZEOF_DCTELEM)], xmm3
340 movdqa XMMWORD [XMMBLOCK(0,0,rdx,SIZEOF_DCTELEM)], xmm6
341 movdqa XMMWORD [XMMBLOCK(2,0,rdx,SIZEOF_DCTELEM)], xmm2
345 movdqa xmm1, XMMWORD [wk(0)] ; xmm1=tmp6
346 movdqa xmm5, XMMWORD [wk(1)] ; xmm5=tmp7
348 paddw xmm7, xmm0 ; xmm7=tmp10
349 paddw xmm0, xmm1 ; xmm0=tmp11
350 paddw xmm1, xmm5 ; xmm1=tmp12, xmm5=tmp7
352 psllw xmm7, PRE_MULTIPLY_SCALE_BITS
353 psllw xmm1, PRE_MULTIPLY_SCALE_BITS
355 psllw xmm0, PRE_MULTIPLY_SCALE_BITS
356 pmulhw xmm0, [rel PW_F0707] ; xmm0=z3
358 movdqa xmm4, xmm7 ; xmm4=tmp10
360 pmulhw xmm7, [rel PW_F0382] ; xmm7=z5
361 pmulhw xmm4, [rel PW_F0541] ; xmm4=MULTIPLY(tmp10,FIX_0_541196)
362 pmulhw xmm1, [rel PW_F1306] ; xmm1=MULTIPLY(tmp12,FIX_1_306562)
363 paddw xmm4, xmm7 ; xmm4=z2
364 paddw xmm1, xmm7 ; xmm1=z4
367 psubw xmm5, xmm0 ; xmm5=z13
368 paddw xmm3, xmm0 ; xmm3=z11
372 psubw xmm5, xmm4 ; xmm5=data3
373 psubw xmm3, xmm1 ; xmm3=data7
374 paddw xmm6, xmm4 ; xmm6=data5
375 paddw xmm2, xmm1 ; xmm2=data1
377 movdqa XMMWORD [XMMBLOCK(3,0,rdx,SIZEOF_DCTELEM)], xmm5
378 movdqa XMMWORD [XMMBLOCK(7,0,rdx,SIZEOF_DCTELEM)], xmm3
379 movdqa XMMWORD [XMMBLOCK(5,0,rdx,SIZEOF_DCTELEM)], xmm6
380 movdqa XMMWORD [XMMBLOCK(1,0,rdx,SIZEOF_DCTELEM)], xmm2
388 ; For some reason, the OS X linker does not honor the request to align the
389 ; segment unless we do this.