9068dff24ba306b549c1bbf734f697818f619295
[libav.git] / libavcodec / i386 / fft_3dn2.c
1 /*
2 * FFT/MDCT transform with Extended 3DNow! optimizations
3 * Copyright (c) 2006 Zuxy MENG Jie, Loren Merritt
4 * Based on fft_sse.c copyright (c) 2002 Fabrice Bellard.
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg 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 GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "libavutil/x86_cpu.h"
24 #include "libavcodec/dsputil.h"
25
26 static const int p1m1[2] __attribute__((aligned(8))) =
27 { 0, 1 << 31 };
28
29 static const int m1p1[2] __attribute__((aligned(8))) =
30 { 1 << 31, 0 };
31
32 void ff_fft_calc_3dn2(FFTContext *s, FFTComplex *z)
33 {
34 int ln = s->nbits;
35 long j;
36 x86_reg i;
37 long nblocks, nloops;
38 FFTComplex *p, *cptr;
39
40 asm volatile(
41 /* FEMMS is not a must here but recommended by AMD */
42 "femms \n\t"
43 "movq %0, %%mm7 \n\t"
44 ::"m"(*(s->inverse ? m1p1 : p1m1))
45 );
46
47 i = 8 << ln;
48 asm volatile(
49 "1: \n\t"
50 "sub $32, %0 \n\t"
51 "movq (%0,%1), %%mm0 \n\t"
52 "movq 16(%0,%1), %%mm1 \n\t"
53 "movq 8(%0,%1), %%mm2 \n\t"
54 "movq 24(%0,%1), %%mm3 \n\t"
55 "movq %%mm0, %%mm4 \n\t"
56 "movq %%mm1, %%mm5 \n\t"
57 "pfadd %%mm2, %%mm0 \n\t"
58 "pfadd %%mm3, %%mm1 \n\t"
59 "pfsub %%mm2, %%mm4 \n\t"
60 "pfsub %%mm3, %%mm5 \n\t"
61 "movq %%mm0, %%mm2 \n\t"
62 "pswapd %%mm5, %%mm5 \n\t"
63 "movq %%mm4, %%mm3 \n\t"
64 "pxor %%mm7, %%mm5 \n\t"
65 "pfadd %%mm1, %%mm0 \n\t"
66 "pfadd %%mm5, %%mm4 \n\t"
67 "pfsub %%mm1, %%mm2 \n\t"
68 "pfsub %%mm5, %%mm3 \n\t"
69 "movq %%mm0, (%0,%1) \n\t"
70 "movq %%mm4, 8(%0,%1) \n\t"
71 "movq %%mm2, 16(%0,%1) \n\t"
72 "movq %%mm3, 24(%0,%1) \n\t"
73 "jg 1b \n\t"
74 :"+r"(i)
75 :"r"(z)
76 );
77 /* pass 2 .. ln-1 */
78
79 nblocks = 1 << (ln-3);
80 nloops = 1 << 2;
81 cptr = s->exptab1;
82 do {
83 p = z;
84 j = nblocks;
85 do {
86 i = nloops*8;
87 asm volatile(
88 "1: \n\t"
89 "sub $16, %0 \n\t"
90 "movq (%1,%0), %%mm0 \n\t"
91 "movq 8(%1,%0), %%mm1 \n\t"
92 "movq (%2,%0), %%mm2 \n\t"
93 "movq 8(%2,%0), %%mm3 \n\t"
94 "movq (%3,%0,2), %%mm4 \n\t"
95 "movq 8(%3,%0,2), %%mm5 \n\t"
96 "pswapd %%mm4, %%mm6 \n\t" // no need for cptr[2] & cptr[3]
97 "pswapd %%mm5, %%mm7 \n\t"
98 "pfmul %%mm2, %%mm4 \n\t" // cre*re cim*im
99 "pfmul %%mm3, %%mm5 \n\t"
100 "pfmul %%mm2, %%mm6 \n\t" // cim*re cre*im
101 "pfmul %%mm3, %%mm7 \n\t"
102 "pfpnacc %%mm6, %%mm4 \n\t" // cre*re-cim*im cim*re+cre*im
103 "pfpnacc %%mm7, %%mm5 \n\t"
104 "movq %%mm0, %%mm2 \n\t"
105 "movq %%mm1, %%mm3 \n\t"
106 "pfadd %%mm4, %%mm0 \n\t"
107 "pfadd %%mm5, %%mm1 \n\t"
108 "pfsub %%mm4, %%mm2 \n\t"
109 "pfsub %%mm5, %%mm3 \n\t"
110 "movq %%mm0, (%1,%0) \n\t"
111 "movq %%mm1, 8(%1,%0) \n\t"
112 "movq %%mm2, (%2,%0) \n\t"
113 "movq %%mm3, 8(%2,%0) \n\t"
114 "jg 1b \n\t"
115 :"+r"(i)
116 :"r"(p), "r"(p + nloops), "r"(cptr)
117 );
118 p += nloops*2;
119 } while (--j);
120 cptr += nloops*2;
121 nblocks >>= 1;
122 nloops <<= 1;
123 } while (nblocks != 0);
124 asm volatile("femms");
125 }
126
127 static void imdct_3dn2(MDCTContext *s, const FFTSample *input, FFTSample *tmp)
128 {
129 long n4, n2, n;
130 x86_reg k;
131 const uint16_t *revtab = s->fft.revtab;
132 const FFTSample *tcos = s->tcos;
133 const FFTSample *tsin = s->tsin;
134 const FFTSample *in1, *in2;
135 FFTComplex *z = (FFTComplex *)tmp;
136
137 n = 1 << s->nbits;
138 n2 = n >> 1;
139 n4 = n >> 2;
140
141 /* pre rotation */
142 in1 = input;
143 in2 = input + n2 - 1;
144 for(k = 0; k < n4; k++) {
145 // FIXME a single block is faster, but gcc 2.95 and 3.4.x on 32bit can't compile it
146 asm volatile(
147 "movd %0, %%mm0 \n\t"
148 "movd %2, %%mm1 \n\t"
149 "punpckldq %1, %%mm0 \n\t"
150 "punpckldq %3, %%mm1 \n\t"
151 "movq %%mm0, %%mm2 \n\t"
152 "pfmul %%mm1, %%mm0 \n\t"
153 "pswapd %%mm1, %%mm1 \n\t"
154 "pfmul %%mm1, %%mm2 \n\t"
155 "pfpnacc %%mm2, %%mm0 \n\t"
156 ::"m"(in2[-2*k]), "m"(in1[2*k]),
157 "m"(tcos[k]), "m"(tsin[k])
158 );
159 asm volatile(
160 "movq %%mm0, %0 \n\t"
161 :"=m"(z[revtab[k]])
162 );
163 }
164
165 ff_fft_calc(&s->fft, z);
166
167 /* post rotation + reordering */
168 for(k = 0; k < n4; k++) {
169 asm volatile(
170 "movq %0, %%mm0 \n\t"
171 "movd %1, %%mm1 \n\t"
172 "punpckldq %2, %%mm1 \n\t"
173 "movq %%mm0, %%mm2 \n\t"
174 "pfmul %%mm1, %%mm0 \n\t"
175 "pswapd %%mm1, %%mm1 \n\t"
176 "pfmul %%mm1, %%mm2 \n\t"
177 "pfpnacc %%mm2, %%mm0 \n\t"
178 "movq %%mm0, %0 \n\t"
179 :"+m"(z[k])
180 :"m"(tcos[k]), "m"(tsin[k])
181 );
182 }
183 }
184
185 void ff_imdct_calc_3dn2(MDCTContext *s, FFTSample *output,
186 const FFTSample *input, FFTSample *tmp)
187 {
188 x86_reg k;
189 long n8, n2, n;
190 FFTComplex *z = (FFTComplex *)tmp;
191
192 n = 1 << s->nbits;
193 n2 = n >> 1;
194 n8 = n >> 3;
195
196 imdct_3dn2(s, input, tmp);
197
198 k = n-8;
199 asm volatile("movd %0, %%mm7" ::"r"(1<<31));
200 asm volatile(
201 "1: \n\t"
202 "movq (%4,%0), %%mm0 \n\t" // z[n8+k]
203 "neg %0 \n\t"
204 "pswapd -8(%4,%0), %%mm1 \n\t" // z[n8-1-k]
205 "movq %%mm0, %%mm2 \n\t"
206 "pxor %%mm7, %%mm2 \n\t"
207 "punpckldq %%mm1, %%mm2 \n\t"
208 "pswapd %%mm2, %%mm3 \n\t"
209 "punpckhdq %%mm1, %%mm0 \n\t"
210 "pswapd %%mm0, %%mm4 \n\t"
211 "pxor %%mm7, %%mm0 \n\t"
212 "pxor %%mm7, %%mm4 \n\t"
213 "movq %%mm3, -8(%3,%0) \n\t" // output[n-2-2*k] = { z[n8-1-k].im, -z[n8+k].re }
214 "movq %%mm4, -8(%2,%0) \n\t" // output[n2-2-2*k]= { -z[n8-1-k].re, z[n8+k].im }
215 "neg %0 \n\t"
216 "movq %%mm0, (%1,%0) \n\t" // output[2*k] = { -z[n8+k].im, z[n8-1-k].re }
217 "movq %%mm2, (%2,%0) \n\t" // output[n2+2*k] = { -z[n8+k].re, z[n8-1-k].im }
218 "sub $8, %0 \n\t"
219 "jge 1b \n\t"
220 :"+r"(k)
221 :"r"(output), "r"(output+n2), "r"(output+n), "r"(z+n8)
222 :"memory"
223 );
224 asm volatile("femms");
225 }
226
227 void ff_imdct_half_3dn2(MDCTContext *s, FFTSample *output,
228 const FFTSample *input, FFTSample *tmp)
229 {
230 x86_reg j, k;
231 long n8, n4, n;
232 FFTComplex *z = (FFTComplex *)tmp;
233
234 n = 1 << s->nbits;
235 n4 = n >> 2;
236 n8 = n >> 3;
237
238 imdct_3dn2(s, input, tmp);
239
240 j = -n;
241 k = n-8;
242 asm volatile("movd %0, %%mm7" ::"r"(1<<31));
243 asm volatile(
244 "1: \n\t"
245 "movq (%3,%1), %%mm0 \n\t" // z[n8+k]
246 "pswapd (%3,%0), %%mm1 \n\t" // z[n8-1-k]
247 "movq %%mm0, %%mm2 \n\t"
248 "punpckldq %%mm1, %%mm0 \n\t"
249 "punpckhdq %%mm2, %%mm1 \n\t"
250 "pxor %%mm7, %%mm0 \n\t"
251 "pxor %%mm7, %%mm1 \n\t"
252 "movq %%mm0, (%2,%1) \n\t" // output[n4+2*k] = { -z[n8+k].re, z[n8-1-k].im }
253 "movq %%mm1, (%2,%0) \n\t" // output[n4-2-2*k] = { -z[n8-1-k].re, z[n8+k].im }
254 "sub $8, %1 \n\t"
255 "add $8, %0 \n\t"
256 "jl 1b \n\t"
257 :"+r"(j), "+r"(k)
258 :"r"(output+n4), "r"(z+n8)
259 :"memory"
260 );
261 asm volatile("femms");
262 }
263