() 10l
[libav.git] / libavcodec / i386 / fft_sse.c
CommitLineData
bb6f5690
FB
1/*
2 * FFT/MDCT transform with SSE optimizations
3 * Copyright (c) 2002 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
5509bffa 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bb6f5690
FB
18 */
19#include "../dsputil.h"
bb6f5690 20
2ffb22d2
ZM
21static const int p1p1p1m1[4] __attribute__((aligned(16))) =
22 { 0, 0, 0, 1 << 31 };
bb6f5690 23
2ffb22d2
ZM
24static const int p1p1m1p1[4] __attribute__((aligned(16))) =
25 { 0, 0, 1 << 31, 0 };
e1958604 26
2ffb22d2
ZM
27static const int p1p1m1m1[4] __attribute__((aligned(16))) =
28 { 0, 0, 1 << 31, 1 << 31 };
bb6f5690 29
ebbafcb4
LM
30static const int p1m1p1m1[4] __attribute__((aligned(16))) =
31 { 0, 1 << 31, 0, 1 << 31 };
32
33static const int m1m1m1m1[4] __attribute__((aligned(16))) =
34 { 1 << 31, 1 << 31, 1 << 31, 1 << 31 };
35
bb6f5690
FB
36#if 0
37static void print_v4sf(const char *str, __m128 a)
38{
39 float *p = (float *)&a;
40 printf("%s: %f %f %f %f\n",
41 str, p[0], p[1], p[2], p[3]);
42}
43#endif
44
45/* XXX: handle reverse case */
68951ecf 46void ff_fft_calc_sse(FFTContext *s, FFTComplex *z)
bb6f5690
FB
47{
48 int ln = s->nbits;
1e4ecf26
LM
49 long i, j;
50 long nblocks, nloops;
51 FFTComplex *p, *cptr;
52
53 asm volatile(
54 "movaps %0, %%xmm4 \n\t"
55 "movaps %1, %%xmm5 \n\t"
56 ::"m"(*p1p1m1m1),
57 "m"(*(s->inverse ? p1p1m1p1 : p1p1p1m1))
58 );
59
60 i = 8 << ln;
61 asm volatile(
62 "1: \n\t"
63 "sub $32, %0 \n\t"
64 /* do the pass 0 butterfly */
65 "movaps (%0,%1), %%xmm0 \n\t"
66 "movaps %%xmm0, %%xmm1 \n\t"
67 "shufps $0x4E, %%xmm0, %%xmm0 \n\t"
68 "xorps %%xmm4, %%xmm1 \n\t"
69 "addps %%xmm1, %%xmm0 \n\t"
70 "movaps 16(%0,%1), %%xmm2 \n\t"
71 "movaps %%xmm2, %%xmm3 \n\t"
72 "shufps $0x4E, %%xmm2, %%xmm2 \n\t"
73 "xorps %%xmm4, %%xmm3 \n\t"
74 "addps %%xmm3, %%xmm2 \n\t"
75 /* multiply third by -i */
76 /* by toggling the sign bit */
77 "shufps $0xB4, %%xmm2, %%xmm2 \n\t"
78 "xorps %%xmm5, %%xmm2 \n\t"
79 /* do the pass 1 butterfly */
80 "movaps %%xmm0, %%xmm1 \n\t"
81 "addps %%xmm2, %%xmm0 \n\t"
82 "subps %%xmm2, %%xmm1 \n\t"
83 "movaps %%xmm0, (%0,%1) \n\t"
84 "movaps %%xmm1, 16(%0,%1) \n\t"
85 "jg 1b \n\t"
86 :"+r"(i)
87 :"r"(z)
88 );
bb6f5690
FB
89 /* pass 2 .. ln-1 */
90
1e4ecf26 91 nblocks = 1 << (ln-3);
bb6f5690 92 nloops = 1 << 2;
1e4ecf26 93 cptr = s->exptab1;
bb6f5690
FB
94 do {
95 p = z;
bb6f5690
FB
96 j = nblocks;
97 do {
1e4ecf26
LM
98 i = nloops*8;
99 asm volatile(
100 "1: \n\t"
101 "sub $16, %0 \n\t"
102 "movaps (%2,%0), %%xmm1 \n\t"
103 "movaps (%1,%0), %%xmm0 \n\t"
104 "movaps %%xmm1, %%xmm2 \n\t"
105 "shufps $0xA0, %%xmm1, %%xmm1 \n\t"
106 "shufps $0xF5, %%xmm2, %%xmm2 \n\t"
107 "mulps (%3,%0,2), %%xmm1 \n\t" // cre*re cim*re
108 "mulps 16(%3,%0,2), %%xmm2 \n\t" // -cim*im cre*im
109 "addps %%xmm2, %%xmm1 \n\t"
110 "movaps %%xmm0, %%xmm3 \n\t"
111 "addps %%xmm1, %%xmm0 \n\t"
112 "subps %%xmm1, %%xmm3 \n\t"
113 "movaps %%xmm0, (%1,%0) \n\t"
114 "movaps %%xmm3, (%2,%0) \n\t"
115 "jg 1b \n\t"
116 :"+r"(i)
117 :"r"(p), "r"(p + nloops), "r"(cptr)
118 );
119 p += nloops*2;
bb6f5690 120 } while (--j);
1e4ecf26
LM
121 cptr += nloops*2;
122 nblocks >>= 1;
123 nloops <<= 1;
bb6f5690
FB
124 } while (nblocks != 0);
125}
e1958604 126
ebbafcb4
LM
127void ff_imdct_calc_sse(MDCTContext *s, FFTSample *output,
128 const FFTSample *input, FFTSample *tmp)
129{
130 long k, n8, n4, n2, n;
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 n8 = n >> 3;
141
142 asm volatile ("movaps %0, %%xmm7\n\t"::"m"(*p1m1p1m1));
143
144 /* pre rotation */
145 in1 = input;
146 in2 = input + n2 - 4;
147
148 /* Complex multiplication
149 Two complex products per iteration, we could have 4 with 8 xmm
150 registers, 8 with 16 xmm registers.
151 Maybe we should unroll more.
152 */
153 for (k = 0; k < n4; k += 2) {
154 asm volatile (
155 "movaps %0, %%xmm0 \n\t" // xmm0 = r0 X r1 X : in2
156 "movaps %1, %%xmm3 \n\t" // xmm3 = X i1 X i0: in1
157 "movlps %2, %%xmm1 \n\t" // xmm1 = X X R1 R0: tcos
158 "movlps %3, %%xmm2 \n\t" // xmm2 = X X I1 I0: tsin
159 "shufps $95, %%xmm0, %%xmm0 \n\t" // xmm0 = r1 r1 r0 r0
160 "shufps $160,%%xmm3, %%xmm3 \n\t" // xmm3 = i1 i1 i0 i0
161 "unpcklps %%xmm2, %%xmm1 \n\t" // xmm1 = I1 R1 I0 R0
162 "movaps %%xmm1, %%xmm2 \n\t" // xmm2 = I1 R1 I0 R0
163 "xorps %%xmm7, %%xmm2 \n\t" // xmm2 = -I1 R1 -I0 R0
164 "mulps %%xmm1, %%xmm0 \n\t" // xmm0 = rI rR rI rR
165 "shufps $177,%%xmm2, %%xmm2 \n\t" // xmm2 = R1 -I1 R0 -I0
166 "mulps %%xmm2, %%xmm3 \n\t" // xmm3 = Ri -Ii Ri -Ii
167 "addps %%xmm3, %%xmm0 \n\t" // xmm0 = result
168 ::"m"(in2[-2*k]), "m"(in1[2*k]),
169 "m"(tcos[k]), "m"(tsin[k])
170 );
171 /* Should be in the same block, hack for gcc2.95 & gcc3 */
172 asm (
173 "movlps %%xmm0, %0 \n\t"
174 "movhps %%xmm0, %1 \n\t"
175 :"=m"(z[revtab[k]]), "=m"(z[revtab[k + 1]])
176 );
177 }
178
179 ff_fft_calc_sse(&s->fft, z);
180
181 /* Not currently needed, added for safety */
182 asm volatile ("movaps %0, %%xmm7\n\t"::"m"(*p1m1p1m1));
183
184 /* post rotation + reordering */
185 for (k = 0; k < n4; k += 2) {
186 asm (
187 "movaps %0, %%xmm0 \n\t" // xmm0 = i1 r1 i0 r0: z
188 "movlps %1, %%xmm1 \n\t" // xmm1 = X X R1 R0: tcos
189 "movaps %%xmm0, %%xmm3 \n\t" // xmm3 = i1 r1 i0 r0
190 "movlps %2, %%xmm2 \n\t" // xmm2 = X X I1 I0: tsin
191 "shufps $160,%%xmm0, %%xmm0 \n\t" // xmm0 = r1 r1 r0 r0
192 "shufps $245,%%xmm3, %%xmm3 \n\t" // xmm3 = i1 i1 i0 i0
193 "unpcklps %%xmm2, %%xmm1 \n\t" // xmm1 = I1 R1 I0 R0
194 "movaps %%xmm1, %%xmm2 \n\t" // xmm2 = I1 R1 I0 R0
195 "xorps %%xmm7, %%xmm2 \n\t" // xmm2 = -I1 R1 -I0 R0
196 "mulps %%xmm1, %%xmm0 \n\t" // xmm0 = rI rR rI rR
197 "shufps $177,%%xmm2, %%xmm2 \n\t" // xmm2 = R1 -I1 R0 -I0
198 "mulps %%xmm2, %%xmm3 \n\t" // xmm3 = Ri -Ii Ri -Ii
199 "addps %%xmm3, %%xmm0 \n\t" // xmm0 = result
200 "movaps %%xmm0, %0 \n\t"
201 :"+m"(z[k])
202 :"m"(tcos[k]), "m"(tsin[k])
203 );
204 }
205
206 /*
207 Mnemonics:
208 0 = z[k].re
209 1 = z[k].im
210 2 = z[k + 1].re
211 3 = z[k + 1].im
212 4 = z[-k - 2].re
213 5 = z[-k - 2].im
214 6 = z[-k - 1].re
215 7 = z[-k - 1].im
216 */
217 k = 16-n;
218 asm volatile("movaps %0, %%xmm7 \n\t"::"m"(*m1m1m1m1));
219 asm volatile(
220 "1: \n\t"
221 "movaps -16(%4,%0), %%xmm1 \n\t" // xmm1 = 4 5 6 7 = z[-2-k]
222 "neg %0 \n\t"
223 "movaps (%4,%0), %%xmm0 \n\t" // xmm0 = 0 1 2 3 = z[k]
224 "xorps %%xmm7, %%xmm0 \n\t" // xmm0 = -0 -1 -2 -3
225 "movaps %%xmm0, %%xmm2 \n\t" // xmm2 = -0 -1 -2 -3
226 "shufps $141,%%xmm1, %%xmm0 \n\t" // xmm0 = -1 -3 4 6
227 "shufps $216,%%xmm1, %%xmm2 \n\t" // xmm2 = -0 -2 5 7
228 "shufps $156,%%xmm0, %%xmm0 \n\t" // xmm0 = -1 6 -3 4 !
229 "shufps $156,%%xmm2, %%xmm2 \n\t" // xmm2 = -0 7 -2 5 !
230 "movaps %%xmm0, (%1,%0) \n\t" // output[2*k]
231 "movaps %%xmm2, (%2,%0) \n\t" // output[n2+2*k]
232 "neg %0 \n\t"
233 "shufps $27, %%xmm0, %%xmm0 \n\t" // xmm0 = 4 -3 6 -1
234 "xorps %%xmm7, %%xmm0 \n\t" // xmm0 = -4 3 -6 1 !
235 "shufps $27, %%xmm2, %%xmm2 \n\t" // xmm2 = 5 -2 7 -0 !
236 "movaps %%xmm0, -16(%2,%0) \n\t" // output[n2-4-2*k]
237 "movaps %%xmm2, -16(%3,%0) \n\t" // output[n-4-2*k]
238 "add $16, %0 \n\t"
239 "jle 1b \n\t"
240 :"+r"(k)
241 :"r"(output), "r"(output+n2), "r"(output+n), "r"(z+n8)
242 :"memory"
243 );
244}
245