2 * 3DNOW and 3DNOWEX optimized IMDCT
3 * Copyright (C) 2002 Nick Kurshev
5 * This file is part of a52dec, a free ATSC A-52 stream decoder.
6 * See http://liba52.sourceforge.net/ for updates.
8 * a52dec is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * a52dec is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #undef FFT_ASMB16_3DNOW
30 #define FFT_4_3DNOW fft_4_3dnowex
31 #define FFT_8_3DNOW fft_8_3dnowex
32 #define FFT_ASMB_3DNOW fft_asmb_3dnowex
33 #define FFT_ASMB16_3DNOW fft_asmb16_3dnowex
34 #define FFT_128P_3DNOW fft_128p_3dnowex
36 #define FFT_4_3DNOW fft_4_3dnow
37 #define FFT_8_3DNOW fft_8_3dnow
38 #define FFT_ASMB_3DNOW fft_asmb_3dnow
39 #define FFT_ASMB16_3DNOW fft_asmb16_3dnow
40 #define FFT_128P_3DNOW fft_128p_3dnow
43 static void FFT_4_3DNOW(complex_t
*x
)
45 /* delta_p = 1 here */
46 /* x[k] = sum_{i=0..3} x[i] * w^{i*k}, w=e^{-2*pi/4}
49 "movq 24(%1), %%mm3\n\t"
50 "movq 8(%1), %%mm1\n\t"
51 "pxor %2, %%mm3\n\t" /* mm3.re | -mm3.im */
52 "pxor %3, %%mm1\n\t" /* -mm1.re | mm1.im */
53 "pfadd %%mm1, %%mm3\n\t" /* vi.im = x[3].re - x[1].re; */
54 "movq %%mm3, %%mm4\n\t" /* vi.re =-x[3].im + x[1].im; mm4 = vi */
56 "pswapd %%mm4, %%mm4\n\t"
58 "punpckldq %%mm4, %%mm5\n\t"
59 "punpckhdq %%mm5, %%mm4\n\t"
61 "movq (%1), %%mm5\n\t" /* yb.re = x[0].re - x[2].re; */
62 "movq (%1), %%mm6\n\t" /* yt.re = x[0].re + x[2].re; */
63 "movq 24(%1), %%mm7\n\t" /* u.re = x[3].re + x[1].re; */
64 "pfsub 16(%1), %%mm5\n\t" /* yb.im = x[0].im - x[2].im; mm5 = yb */
65 "pfadd 16(%1), %%mm6\n\t" /* yt.im = x[0].im + x[2].im; mm6 = yt */
66 "pfadd 8(%1), %%mm7\n\t" /* u.im = x[3].im + x[1].im; mm7 = u */
68 "movq %%mm6, %%mm0\n\t" /* x[0].re = yt.re + u.re; */
69 "movq %%mm5, %%mm1\n\t" /* x[1].re = yb.re + vi.re; */
70 "pfadd %%mm7, %%mm0\n\t" /*x[0].im = yt.im + u.im; */
71 "pfadd %%mm4, %%mm1\n\t" /* x[1].im = yb.im + vi.im; */
72 "movq %%mm0, (%0)\n\t"
73 "movq %%mm1, 8(%0)\n\t"
75 "pfsub %%mm7, %%mm6\n\t" /* x[2].re = yt.re - u.re; */
76 "pfsub %%mm4, %%mm5\n\t" /* x[3].re = yb.re - vi.re; */
77 "movq %%mm6, 16(%0)\n\t" /* x[2].im = yt.im - u.im; */
78 "movq %%mm5, 24(%0)" /* x[3].im = yb.im - vi.im; */
81 "m"(x_plus_minus_3dnow
),
82 "m"(x_minus_plus_3dnow
)
86 static void FFT_8_3DNOW(complex_t
*x
)
88 /* delta_p = diag{1, sqrt(i)} here */
89 /* x[k] = sum_{i=0..7} x[i] * w^{i*k}, w=e^{-2*pi/8}
91 complex_t wT1
, wB1
, wB2
;
94 "movq 8(%2), %%mm0\n\t"
95 "movq 24(%2), %%mm1\n\t"
96 "movq %%mm0, %0\n\t" /* wT1 = x[1]; */
97 "movq %%mm1, %1\n\t" /* wB1 = x[3]; */
102 __asm__
__volatile__(
103 "movq 16(%0), %%mm2\n\t"
104 "movq 32(%0), %%mm3\n\t"
105 "movq %%mm2, 8(%0)\n\t" /* x[1] = x[2]; */
106 "movq 48(%0), %%mm4\n\t"
107 "movq %%mm3, 16(%0)\n\t" /* x[2] = x[4]; */
108 "movq %%mm4, 24(%0)\n\t" /* x[3] = x[6]; */
115 /* x[0] x[4] x[2] x[6] */
117 __asm__
__volatile__(
118 "movq 40(%1), %%mm0\n\t"
119 "movq %%mm0, %%mm3\n\t"
120 "movq 56(%1), %%mm1\n\t"
121 "pfadd %%mm1, %%mm0\n\t"
122 "pfsub %%mm1, %%mm3\n\t"
123 "movq (%2), %%mm2\n\t"
124 "pfadd %%mm2, %%mm0\n\t"
125 "pfadd %%mm2, %%mm3\n\t"
126 "movq (%3), %%mm1\n\t"
127 "pfadd %%mm1, %%mm0\n\t"
128 "pfsub %%mm1, %%mm3\n\t"
129 "movq (%1), %%mm1\n\t"
130 "movq 16(%1), %%mm4\n\t"
131 "movq %%mm1, %%mm2\n\t"
133 "pswapd %%mm3, %%mm3\n\t"
135 "punpckldq %%mm3, %%mm6\n\t"
136 "punpckhdq %%mm6, %%mm3\n\t"
138 "pfadd %%mm0, %%mm1\n\t"
139 "movq %%mm4, %%mm5\n\t"
140 "pfsub %%mm0, %%mm2\n\t"
141 "pfadd %%mm3, %%mm4\n\t"
142 "movq %%mm1, (%0)\n\t"
143 "pfsub %%mm3, %%mm5\n\t"
144 "movq %%mm2, 32(%0)\n\t"
145 "movd %%mm4, 16(%0)\n\t"
146 "movd %%mm5, 48(%0)\n\t"
147 "psrlq $32, %%mm4\n\t"
148 "psrlq $32, %%mm5\n\t"
149 "movd %%mm4, 52(%0)\n\t"
152 :"0"(x
), "r"(&wT1
), "r"(&wB1
)
156 __asm__
__volatile__ (
161 "movq 56(%3), %%mm3\n\t"
162 "pfsub 40(%3), %%mm0\n\t"
164 "pswapd %%mm1, %%mm1\n\t"
166 "punpckldq %%mm1, %%mm2\n\t"
167 "punpckhdq %%mm2, %%mm1\n\t"
169 "pxor %%mm7, %%mm1\n\t"
170 "pfadd %%mm1, %%mm0\n\t"
172 "pswapd %%mm3, %%mm3\n\t"
174 "punpckldq %%mm3, %%mm2\n\t"
175 "punpckhdq %%mm2, %%mm3\n\t"
177 "pxor %%mm6, %%mm3\n\t"
178 "pfadd %%mm3, %%mm0\n\t"
179 "movq %%mm0, %%mm1\n\t"
180 "pxor %%mm6, %%mm1\n\t"
181 "pfacc %%mm1, %%mm0\n\t"
182 "pfmul %4, %%mm0\n\t"
184 "movq 40(%3), %%mm5\n\t"
186 "pswapd %%mm5, %%mm5\n\t"
188 "punpckldq %%mm5, %%mm1\n\t"
189 "punpckhdq %%mm1, %%mm5\n\t"
193 "movq 8(%3), %%mm1\n\t"
194 "movq %%mm1, %%mm2\n\t"
195 "pfsub %%mm0, %%mm1\n\t"
196 "pfadd %%mm0, %%mm2\n\t"
197 "movq %%mm1, 40(%3)\n\t"
198 "movq %%mm2, 8(%3)\n\t"
200 :"m"(wT1
), "m"(wB1
), "r"(x
), "m"(HSQRT2_3DNOW
),
201 "m"(x_plus_minus_3dnow
), "m"(x_minus_plus_3dnow
)
206 __asm__
__volatile__(
209 "pswapd %3, %%mm1\n\t"
212 "punpckldq %%mm1, %%mm2\n\t"
213 "punpckhdq %%mm2, %%mm1\n\t"
215 "pxor %%mm6, %%mm1\n\t"
216 "pfadd %%mm1, %%mm0\n\t"
218 "movq 56(%4), %%mm3\n\t"
219 "pxor %%mm7, %%mm3\n\t"
220 "pfadd %%mm3, %%mm2\n\t"
222 "pswapd %%mm2, %%mm2\n\t"
224 "punpckldq %%mm2, %%mm5\n\t"
225 "punpckhdq %%mm5, %%mm2\n\t"
227 "movq 24(%4), %%mm3\n\t"
228 "pfsub %%mm2, %%mm0\n\t"
229 "movq %%mm3, %%mm4\n\t"
230 "movq %%mm0, %%mm1\n\t"
231 "pxor %%mm6, %%mm0\n\t"
232 "pfacc %%mm1, %%mm0\n\t"
233 "pfmul %5, %%mm0\n\t"
234 "movq %%mm0, %%mm1\n\t"
235 "pxor %%mm6, %%mm1\n\t"
236 "pxor %%mm7, %%mm0\n\t"
237 "pfadd %%mm1, %%mm3\n\t"
238 "pfadd %%mm0, %%mm4\n\t"
239 "movq %%mm4, 24(%0)\n\t"
240 "movq %%mm3, 56(%0)\n\t"
242 :"m"(wT1
), "m"(wB2
), "m"(wB1
), "0"(x
), "m"(HSQRT2_3DNOW
)
246 static void FFT_ASMB_3DNOW(int k
, complex_t
*x
, complex_t
*wTB
,
247 const complex_t
*d
, const complex_t
*d_3
)
249 register complex_t
*x2k
, *x3k
, *x4k
, *wB
;
251 TRANS_FILL_MM6_MM7_3DNOW();
257 TRANSZERO_3DNOW(x
[0],x2k
[0],x3k
[0],x4k
[0]);
258 TRANS_3DNOW(x
[1],x2k
[1],x3k
[1],x4k
[1],wTB
[1],wB
[1],d
[1],d_3
[1]);
262 TRANS_3DNOW(x
[2],x2k
[2],x3k
[2],x4k
[2],wTB
[2],wB
[2],d
[2],d_3
[2]);
263 TRANS_3DNOW(x
[3],x2k
[3],x3k
[3],x4k
[3],wTB
[3],wB
[3],d
[3],d_3
[3]);
277 void FFT_ASMB16_3DNOW(complex_t
*x
, complex_t
*wTB
)
281 TRANS_FILL_MM6_MM7_3DNOW();
282 /* transform x[0], x[8], x[4], x[12] */
283 TRANSZERO_3DNOW(x
[0],x
[4],x
[8],x
[12]);
285 /* transform x[1], x[9], x[5], x[13] */
286 TRANS_3DNOW(x
[1],x
[5],x
[9],x
[13],wTB
[1],wTB
[5],delta16
[1],delta16_3
[1]);
288 /* transform x[2], x[10], x[6], x[14] */
289 TRANSHALF_16_3DNOW(x
[2],x
[6],x
[10],x
[14]);
291 /* transform x[3], x[11], x[7], x[15] */
292 TRANS_3DNOW(x
[3],x
[7],x
[11],x
[15],wTB
[3],wTB
[7],delta16
[3],delta16_3
[3]);
296 static void FFT_128P_3DNOW(complex_t
*a
)
298 FFT_8_3DNOW(&a
[0]); FFT_4_3DNOW(&a
[8]); FFT_4_3DNOW(&a
[12]);
299 FFT_ASMB16_3DNOW(&a
[0], &a
[8]);
301 FFT_8_3DNOW(&a
[16]), FFT_8_3DNOW(&a
[24]);
302 FFT_ASMB_3DNOW(4, &a
[0], &a
[16],&delta32
[0], &delta32_3
[0]);
304 FFT_8_3DNOW(&a
[32]); FFT_4_3DNOW(&a
[40]); FFT_4_3DNOW(&a
[44]);
305 FFT_ASMB16_3DNOW(&a
[32], &a
[40]);
307 FFT_8_3DNOW(&a
[48]); FFT_4_3DNOW(&a
[56]); FFT_4_3DNOW(&a
[60]);
308 FFT_ASMB16_3DNOW(&a
[48], &a
[56]);
310 FFT_ASMB_3DNOW(8, &a
[0], &a
[32],&delta64
[0], &delta64_3
[0]);
312 FFT_8_3DNOW(&a
[64]); FFT_4_3DNOW(&a
[72]); FFT_4_3DNOW(&a
[76]);
313 /* FFT_16(&a[64]); */
314 FFT_ASMB16_3DNOW(&a
[64], &a
[72]);
316 FFT_8_3DNOW(&a
[80]); FFT_8_3DNOW(&a
[88]);
318 /* FFT_32(&a[64]); */
319 FFT_ASMB_3DNOW(4, &a
[64], &a
[80],&delta32
[0], &delta32_3
[0]);
321 FFT_8_3DNOW(&a
[96]); FFT_4_3DNOW(&a
[104]), FFT_4_3DNOW(&a
[108]);
322 /* FFT_16(&a[96]); */
323 FFT_ASMB16_3DNOW(&a
[96], &a
[104]);
325 FFT_8_3DNOW(&a
[112]), FFT_8_3DNOW(&a
[120]);
326 /* FFT_32(&a[96]); */
327 FFT_ASMB_3DNOW(4, &a
[96], &a
[112], &delta32
[0], &delta32_3
[0]);
329 /* FFT_128(&a[0]); */
330 FFT_ASMB_3DNOW(16, &a
[0], &a
[64], &delta128
[0], &delta128_3
[0]);
339 (sample_t data
[],sample_t delay
[], sample_t bias
)
355 sample_t
*window_ptr
;
357 /* 512 IMDCT with source and dest data in 'data' */
359 /* Pre IFFT complex multiply plus IFFT cmplx conjugate & reordering*/
361 __asm__
__volatile__ (
363 ::"m"(x_plus_minus_3dnow
)
365 for( i
=0; i
< 128; i
++) {
367 __asm__
__volatile__ (
370 "punpckldq %2, %%mm0\n\t" /* mm0 = data[256-2*j-1] | data[2*j]*/
371 "punpckldq %4, %%mm1\n\t" /* mm1 = xcos[j] | xsin[j] */
372 "movq %%mm0, %%mm2\n\t"
373 "pfmul %%mm1, %%mm0\n\t"
375 "pswapd %%mm1, %%mm1\n\t"
377 "punpckldq %%mm1, %%mm5\n\t"
378 "punpckhdq %%mm5, %%mm1\n\t"
380 "pfmul %%mm1, %%mm2\n\t"
382 "pfpnacc %%mm2, %%mm0\n\t"
384 "pxor %%mm7, %%mm0\n\t"
385 "pfacc %%mm2, %%mm0\n\t"
387 "pxor %%mm7, %%mm0\n\t"
390 :"m"(data
[256-2*j
-1]), "m"(data
[2*j
]), "m"(xcos1
[j
]), "m"(xsin1
[j
])
393 /* buf[i].re = (data[256-2*j-1] * xcos1[j] - data[2*j] * xsin1[j]);
394 buf[i].im = (data[256-2*j-1] * xsin1[j] + data[2*j] * xcos1[j])*(-1.0);*/
397 __asm__
__volatile__ ("femms":::"memory");
398 for( i
=0; i
< 128; i
++) {
399 /* z[i] = (X[256-2*i-1] + j * X[2*i]) * (xcos1[i] + j * xsin1[i]) ; */
401 buf
[i
].real
= (data
[256-2*j
-1] * xcos1
[j
]) - (data
[2*j
] * xsin1
[j
]);
402 buf
[i
].imag
= -1.0 * ((data
[2*j
] * xcos1
[j
]) + (data
[256-2*j
-1] * xsin1
[j
]));
407 /* unoptimized variant
408 for (m=1; m < 7; m++) {
414 two_m_plus_one = (1 << (m+1));
416 for(i = 0; i < 128; i += two_m_plus_one) {
417 for(k = 0; k < two_m; k++) {
420 tmp_a_r = buf[p].real;
421 tmp_a_i = buf[p].imag;
422 tmp_b_r = buf[q].real * w[m][k].real - buf[q].imag * w[m][k].imag;
423 tmp_b_i = buf[q].imag * w[m][k].real + buf[q].real * w[m][k].imag;
424 buf[p].real = tmp_a_r + tmp_b_r;
425 buf[p].imag = tmp_a_i + tmp_b_i;
426 buf[q].real = tmp_a_r - tmp_b_r;
427 buf[q].imag = tmp_a_i - tmp_b_i;
433 FFT_128P_3DNOW (&buf
[0]);
434 // asm volatile ("femms \n\t":::"memory");
436 /* Post IFFT complex multiply plus IFFT complex conjugate*/
438 __asm__
__volatile__ (
441 ::"m"(x_plus_minus_3dnow
),
442 "m"(x_minus_plus_3dnow
)
444 for (i
=0; i
< 128; i
++) {
445 __asm__
__volatile__ (
446 "movq %1, %%mm0\n\t" /* ac3_buf[i].re | ac3_buf[i].im */
447 "movq %%mm0, %%mm1\n\t" /* ac3_buf[i].re | ac3_buf[i].im */
449 "punpckldq %%mm1, %%mm2\n\t"
450 "punpckhdq %%mm2, %%mm1\n\t"
452 "pswapd %%mm1, %%mm1\n\t" /* ac3_buf[i].re | ac3_buf[i].im */
454 "movd %3, %%mm3\n\t" /* ac3_xsin[i] */
455 "punpckldq %2, %%mm3\n\t" /* ac3_xsin[i] | ac3_xcos[i] */
456 "pfmul %%mm3, %%mm0\n\t"
457 "pfmul %%mm3, %%mm1\n\t"
459 "pxor %%mm7, %%mm0\n\t"
460 "pfacc %%mm1, %%mm0\n\t"
461 "punpckldq %%mm0, %%mm1\n\t"
462 "punpckhdq %%mm1, %%mm0\n\t"
465 "pfpnacc %%mm1, %%mm0\n\t" /* mm0 = mm0[0] - mm0[1] | mm1[0] + mm1[1] */
466 "pswapd %%mm0, %%mm0\n\t"
470 :"m"(buf
[i
]),"m"(xcos1
[i
]),"m"(xsin1
[i
])
472 /* ac3_buf[i].re =(tmp_a_r * ac3_xcos1[i]) + (tmp_a_i * ac3_xsin1[i]);
473 ac3_buf[i].im =(tmp_a_r * ac3_xsin1[i]) - (tmp_a_i * ac3_xcos1[i]);*/
476 __asm__
__volatile__ ("femms":::"memory");
477 for( i
=0; i
< 128; i
++) {
478 /* y[n] = z[n] * (xcos1[n] + j * xsin1[n]) ; */
479 tmp_a_r
= buf
[i
].real
;
480 tmp_a_i
= -1.0 * buf
[i
].imag
;
481 buf
[i
].real
=(tmp_a_r
* xcos1
[i
]) - (tmp_a_i
* xsin1
[i
]);
482 buf
[i
].imag
=(tmp_a_r
* xsin1
[i
]) + (tmp_a_i
* xcos1
[i
]);
488 window_ptr
= a52_imdct_window
;
490 /* Window and convert to real valued signal */
493 "movd (%0), %%mm3 \n\t"
494 "punpckldq %%mm3, %%mm3 \n\t"
497 for (i
=0; i
< 64; i
++) {
498 /* merge two loops in one to enable working of 2 decoders */
499 __asm__
__volatile__ (
500 "movd 516(%1), %%mm0\n\t"
501 "movd (%1), %%mm1\n\t" /**data_ptr++=-buf[64+i].im**window_ptr+++*delay_ptr++;*/
502 "punpckldq (%2), %%mm0\n\t"/*data_ptr[128]=-buf[i].re*window_ptr[128]+delay_ptr[128];*/
503 "punpckldq 516(%2), %%mm1\n\t"
504 "pfmul (%3), %%mm0\n\t"/**data_ptr++=buf[64-i-1].re**window_ptr+++*delay_ptr++;*/
505 "pfmul 512(%3), %%mm1\n\t"
506 "pxor %%mm6, %%mm0\n\t"/*data_ptr[128]=buf[128-i-1].im*window_ptr[128]+delay_ptr[128];*/
507 "pxor %%mm6, %%mm1\n\t"
508 "pfadd (%4), %%mm0\n\t"
509 "pfadd 512(%4), %%mm1\n\t"
510 "pfadd %%mm3, %%mm0\n\t"
511 "pfadd %%mm3, %%mm1\n\t"
512 "movq %%mm0, (%0)\n\t"
513 "movq %%mm1, 512(%0)"
515 :"r"(&buf
[i
].real
), "r"(&buf
[64-i
-1].real
), "r"(window_ptr
), "r"(delay_ptr
), "0"(data_ptr
)
523 __asm__
__volatile__ ("femms":::"memory");
524 for(i
=0; i
< 64; i
++) {
525 *data_ptr
++ = -buf
[64+i
].imag
* *window_ptr
++ + *delay_ptr
++ + bias
;
526 *data_ptr
++ = buf
[64-i
-1].real
* *window_ptr
++ + *delay_ptr
++ + bias
;
529 for(i
=0; i
< 64; i
++) {
530 *data_ptr
++ = -buf
[i
].real
* *window_ptr
++ + *delay_ptr
++ + bias
;
531 *data_ptr
++ = buf
[128-i
-1].imag
* *window_ptr
++ + *delay_ptr
++ + bias
;
535 /* The trailing edge of the window goes into the delay line */
538 for(i
=0; i
< 64; i
++) {
539 /* merge two loops in one to enable working of 2 decoders */
541 __asm__
__volatile__(
542 "movd 508(%1), %%mm0\n\t"
543 "movd (%1), %%mm1\n\t"
544 "punpckldq (%2), %%mm0\n\t"
545 "punpckldq 508(%2), %%mm1\n\t"
547 "pswapd (%3), %%mm3\n\t"
548 "pswapd -512(%3), %%mm4\n\t"
550 "movq (%3), %%mm3\n\t"
551 "punpckldq %%mm3, %%mm2\n\t"
552 "punpckhdq %%mm2, %%mm3\n\t"
553 "movq -512(%3), %%mm4\n\t"
554 "punpckldq %%mm4, %%mm2\n\t"
555 "punpckhdq %%mm2, %%mm4\n\t"
557 "pfmul %%mm3, %%mm0\n\t"
558 "pfmul %%mm4, %%mm1\n\t"
559 "pxor %%mm6, %%mm0\n\t"
560 "pxor %%mm7, %%mm1\n\t"
561 "movq %%mm0, (%0)\n\t"
562 "movq %%mm1, 512(%0)"
564 :"r"(&buf
[i
].imag
), "r"(&buf
[64-i
-1].imag
), "r"(window_ptr
), "0"(delay_ptr
)
568 __asm__
__volatile__ ("femms":::"memory");
570 __asm__
__volatile__ ("femms":::"memory");
571 for(i
=0; i
< 64; i
++) {
572 *delay_ptr
++ = -buf
[64+i
].real
* *--window_ptr
;
573 *delay_ptr
++ = buf
[64-i
-1].imag
* *--window_ptr
;
576 for(i
=0; i
<64; i
++) {
577 *delay_ptr
++ = buf
[i
].imag
* *--window_ptr
;
578 *delay_ptr
++ = -buf
[128-i
-1].real
* *--window_ptr
;