mlpdec: Check for bits left before each read of End-of-Stream indicator and
[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 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
bb6f5690
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
bb6f5690 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
bb6f5690
FB
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bb6f5690 20 */
245976da
DB
21
22#include "libavutil/x86_cpu.h"
23#include "libavcodec/dsputil.h"
bb6f5690 24
2ffb22d2
ZM
25static const int p1p1p1m1[4] __attribute__((aligned(16))) =
26 { 0, 0, 0, 1 << 31 };
bb6f5690 27
2ffb22d2
ZM
28static const int p1p1m1p1[4] __attribute__((aligned(16))) =
29 { 0, 0, 1 << 31, 0 };
e1958604 30
2ffb22d2
ZM
31static const int p1p1m1m1[4] __attribute__((aligned(16))) =
32 { 0, 0, 1 << 31, 1 << 31 };
bb6f5690 33
ebbafcb4
LM
34static const int p1m1p1m1[4] __attribute__((aligned(16))) =
35 { 0, 1 << 31, 0, 1 << 31 };
36
37static const int m1m1m1m1[4] __attribute__((aligned(16))) =
38 { 1 << 31, 1 << 31, 1 << 31, 1 << 31 };
39
bb6f5690
FB
40#if 0
41static void print_v4sf(const char *str, __m128 a)
42{
43 float *p = (float *)&a;
44 printf("%s: %f %f %f %f\n",
45 str, p[0], p[1], p[2], p[3]);
46}
47#endif
48
49/* XXX: handle reverse case */
68951ecf 50void ff_fft_calc_sse(FFTContext *s, FFTComplex *z)
bb6f5690
FB
51{
52 int ln = s->nbits;
40d0e665
RP
53 x86_reg i;
54 long j;
1e4ecf26
LM
55 long nblocks, nloops;
56 FFTComplex *p, *cptr;
57
58 asm volatile(
59 "movaps %0, %%xmm4 \n\t"
60 "movaps %1, %%xmm5 \n\t"
61 ::"m"(*p1p1m1m1),
62 "m"(*(s->inverse ? p1p1m1p1 : p1p1p1m1))
63 );
64
65 i = 8 << ln;
66 asm volatile(
67 "1: \n\t"
68 "sub $32, %0 \n\t"
69 /* do the pass 0 butterfly */
70 "movaps (%0,%1), %%xmm0 \n\t"
71 "movaps %%xmm0, %%xmm1 \n\t"
72 "shufps $0x4E, %%xmm0, %%xmm0 \n\t"
73 "xorps %%xmm4, %%xmm1 \n\t"
74 "addps %%xmm1, %%xmm0 \n\t"
75 "movaps 16(%0,%1), %%xmm2 \n\t"
76 "movaps %%xmm2, %%xmm3 \n\t"
77 "shufps $0x4E, %%xmm2, %%xmm2 \n\t"
78 "xorps %%xmm4, %%xmm3 \n\t"
79 "addps %%xmm3, %%xmm2 \n\t"
80 /* multiply third by -i */
81 /* by toggling the sign bit */
82 "shufps $0xB4, %%xmm2, %%xmm2 \n\t"
83 "xorps %%xmm5, %%xmm2 \n\t"
84 /* do the pass 1 butterfly */
85 "movaps %%xmm0, %%xmm1 \n\t"
86 "addps %%xmm2, %%xmm0 \n\t"
87 "subps %%xmm2, %%xmm1 \n\t"
88 "movaps %%xmm0, (%0,%1) \n\t"
89 "movaps %%xmm1, 16(%0,%1) \n\t"
90 "jg 1b \n\t"
91 :"+r"(i)
92 :"r"(z)
93 );
bb6f5690
FB
94 /* pass 2 .. ln-1 */
95
1e4ecf26 96 nblocks = 1 << (ln-3);
bb6f5690 97 nloops = 1 << 2;
1e4ecf26 98 cptr = s->exptab1;
bb6f5690
FB
99 do {
100 p = z;
bb6f5690
FB
101 j = nblocks;
102 do {
1e4ecf26
LM
103 i = nloops*8;
104 asm volatile(
105 "1: \n\t"
25e4f8aa 106 "sub $32, %0 \n\t"
1e4ecf26
LM
107 "movaps (%2,%0), %%xmm1 \n\t"
108 "movaps (%1,%0), %%xmm0 \n\t"
25e4f8aa
ZM
109 "movaps 16(%2,%0), %%xmm5 \n\t"
110 "movaps 16(%1,%0), %%xmm4 \n\t"
1e4ecf26 111 "movaps %%xmm1, %%xmm2 \n\t"
25e4f8aa 112 "movaps %%xmm5, %%xmm6 \n\t"
1e4ecf26
LM
113 "shufps $0xA0, %%xmm1, %%xmm1 \n\t"
114 "shufps $0xF5, %%xmm2, %%xmm2 \n\t"
25e4f8aa
ZM
115 "shufps $0xA0, %%xmm5, %%xmm5 \n\t"
116 "shufps $0xF5, %%xmm6, %%xmm6 \n\t"
1e4ecf26
LM
117 "mulps (%3,%0,2), %%xmm1 \n\t" // cre*re cim*re
118 "mulps 16(%3,%0,2), %%xmm2 \n\t" // -cim*im cre*im
25e4f8aa
ZM
119 "mulps 32(%3,%0,2), %%xmm5 \n\t" // cre*re cim*re
120 "mulps 48(%3,%0,2), %%xmm6 \n\t" // -cim*im cre*im
1e4ecf26 121 "addps %%xmm2, %%xmm1 \n\t"
25e4f8aa 122 "addps %%xmm6, %%xmm5 \n\t"
1e4ecf26 123 "movaps %%xmm0, %%xmm3 \n\t"
25e4f8aa 124 "movaps %%xmm4, %%xmm7 \n\t"
1e4ecf26
LM
125 "addps %%xmm1, %%xmm0 \n\t"
126 "subps %%xmm1, %%xmm3 \n\t"
25e4f8aa
ZM
127 "addps %%xmm5, %%xmm4 \n\t"
128 "subps %%xmm5, %%xmm7 \n\t"
1e4ecf26
LM
129 "movaps %%xmm0, (%1,%0) \n\t"
130 "movaps %%xmm3, (%2,%0) \n\t"
25e4f8aa
ZM
131 "movaps %%xmm4, 16(%1,%0) \n\t"
132 "movaps %%xmm7, 16(%2,%0) \n\t"
1e4ecf26
LM
133 "jg 1b \n\t"
134 :"+r"(i)
135 :"r"(p), "r"(p + nloops), "r"(cptr)
136 );
137 p += nloops*2;
bb6f5690 138 } while (--j);
1e4ecf26
LM
139 cptr += nloops*2;
140 nblocks >>= 1;
141 nloops <<= 1;
bb6f5690
FB
142 } while (nblocks != 0);
143}
e1958604 144
ebbafcb4
LM
145void ff_imdct_calc_sse(MDCTContext *s, FFTSample *output,
146 const FFTSample *input, FFTSample *tmp)
147{
40d0e665
RP
148 x86_reg k;
149 long n8, n4, n2, n;
ebbafcb4
LM
150 const uint16_t *revtab = s->fft.revtab;
151 const FFTSample *tcos = s->tcos;
152 const FFTSample *tsin = s->tsin;
153 const FFTSample *in1, *in2;
154 FFTComplex *z = (FFTComplex *)tmp;
155
156 n = 1 << s->nbits;
157 n2 = n >> 1;
158 n4 = n >> 2;
159 n8 = n >> 3;
160
25e4f8aa
ZM
161#ifdef ARCH_X86_64
162 asm volatile ("movaps %0, %%xmm8\n\t"::"m"(*p1m1p1m1));
163#define P1M1P1M1 "%%xmm8"
164#else
165#define P1M1P1M1 "%4"
166#endif
ebbafcb4
LM
167
168 /* pre rotation */
169 in1 = input;
170 in2 = input + n2 - 4;
171
25e4f8aa
ZM
172 /* Complex multiplication */
173 for (k = 0; k < n4; k += 4) {
ebbafcb4
LM
174 asm volatile (
175 "movaps %0, %%xmm0 \n\t" // xmm0 = r0 X r1 X : in2
176 "movaps %1, %%xmm3 \n\t" // xmm3 = X i1 X i0: in1
ee387b57
GB
177 "movaps -16+1*%0, %%xmm4 \n\t" // xmm4 = r0 X r1 X : in2
178 "movaps 16+1*%1, %%xmm7 \n\t" // xmm7 = X i1 X i0: in1
ebbafcb4
LM
179 "movlps %2, %%xmm1 \n\t" // xmm1 = X X R1 R0: tcos
180 "movlps %3, %%xmm2 \n\t" // xmm2 = X X I1 I0: tsin
ee387b57
GB
181 "movlps 8+1*%2, %%xmm5 \n\t" // xmm5 = X X R1 R0: tcos
182 "movlps 8+1*%3, %%xmm6 \n\t" // xmm6 = X X I1 I0: tsin
ebbafcb4
LM
183 "shufps $95, %%xmm0, %%xmm0 \n\t" // xmm0 = r1 r1 r0 r0
184 "shufps $160,%%xmm3, %%xmm3 \n\t" // xmm3 = i1 i1 i0 i0
25e4f8aa
ZM
185 "shufps $95, %%xmm4, %%xmm4 \n\t" // xmm4 = r1 r1 r0 r0
186 "shufps $160,%%xmm7, %%xmm7 \n\t" // xmm7 = i1 i1 i0 i0
ebbafcb4 187 "unpcklps %%xmm2, %%xmm1 \n\t" // xmm1 = I1 R1 I0 R0
25e4f8aa 188 "unpcklps %%xmm6, %%xmm5 \n\t" // xmm5 = I1 R1 I0 R0
ebbafcb4 189 "movaps %%xmm1, %%xmm2 \n\t" // xmm2 = I1 R1 I0 R0
25e4f8aa
ZM
190 "movaps %%xmm5, %%xmm6 \n\t" // xmm6 = I1 R1 I0 R0
191 "xorps "P1M1P1M1", %%xmm2 \n\t" // xmm2 = -I1 R1 -I0 R0
192 "xorps "P1M1P1M1", %%xmm6 \n\t" // xmm6 = -I1 R1 -I0 R0
ebbafcb4 193 "mulps %%xmm1, %%xmm0 \n\t" // xmm0 = rI rR rI rR
25e4f8aa 194 "mulps %%xmm5, %%xmm4 \n\t" // xmm4 = rI rR rI rR
ebbafcb4 195 "shufps $177,%%xmm2, %%xmm2 \n\t" // xmm2 = R1 -I1 R0 -I0
25e4f8aa 196 "shufps $177,%%xmm6, %%xmm6 \n\t" // xmm6 = R1 -I1 R0 -I0
ebbafcb4 197 "mulps %%xmm2, %%xmm3 \n\t" // xmm3 = Ri -Ii Ri -Ii
25e4f8aa 198 "mulps %%xmm6, %%xmm7 \n\t" // xmm7 = Ri -Ii Ri -Ii
ebbafcb4 199 "addps %%xmm3, %%xmm0 \n\t" // xmm0 = result
25e4f8aa 200 "addps %%xmm7, %%xmm4 \n\t" // xmm4 = result
ebbafcb4
LM
201 ::"m"(in2[-2*k]), "m"(in1[2*k]),
202 "m"(tcos[k]), "m"(tsin[k])
25e4f8aa
ZM
203#ifndef ARCH_X86_64
204 ,"m"(*p1m1p1m1)
205#endif
ebbafcb4
LM
206 );
207 /* Should be in the same block, hack for gcc2.95 & gcc3 */
208 asm (
209 "movlps %%xmm0, %0 \n\t"
210 "movhps %%xmm0, %1 \n\t"
25e4f8aa
ZM
211 "movlps %%xmm4, %2 \n\t"
212 "movhps %%xmm4, %3 \n\t"
213 :"=m"(z[revtab[k]]), "=m"(z[revtab[k + 1]]),
214 "=m"(z[revtab[k + 2]]), "=m"(z[revtab[k + 3]])
ebbafcb4
LM
215 );
216 }
217
218 ff_fft_calc_sse(&s->fft, z);
219
25e4f8aa
ZM
220#ifndef ARCH_X86_64
221#undef P1M1P1M1
222#define P1M1P1M1 "%3"
223#endif
ebbafcb4
LM
224
225 /* post rotation + reordering */
25e4f8aa 226 for (k = 0; k < n4; k += 4) {
ebbafcb4
LM
227 asm (
228 "movaps %0, %%xmm0 \n\t" // xmm0 = i1 r1 i0 r0: z
ee387b57 229 "movaps 16+1*%0, %%xmm4 \n\t" // xmm4 = i1 r1 i0 r0: z
ebbafcb4 230 "movlps %1, %%xmm1 \n\t" // xmm1 = X X R1 R0: tcos
ee387b57 231 "movlps 8+1*%1, %%xmm5 \n\t" // xmm5 = X X R1 R0: tcos
ebbafcb4 232 "movaps %%xmm0, %%xmm3 \n\t" // xmm3 = i1 r1 i0 r0
25e4f8aa 233 "movaps %%xmm4, %%xmm7 \n\t" // xmm7 = i1 r1 i0 r0
ebbafcb4 234 "movlps %2, %%xmm2 \n\t" // xmm2 = X X I1 I0: tsin
ee387b57 235 "movlps 8+1*%2, %%xmm6 \n\t" // xmm6 = X X I1 I0: tsin
ebbafcb4
LM
236 "shufps $160,%%xmm0, %%xmm0 \n\t" // xmm0 = r1 r1 r0 r0
237 "shufps $245,%%xmm3, %%xmm3 \n\t" // xmm3 = i1 i1 i0 i0
25e4f8aa
ZM
238 "shufps $160,%%xmm4, %%xmm4 \n\t" // xmm4 = r1 r1 r0 r0
239 "shufps $245,%%xmm7, %%xmm7 \n\t" // xmm7 = i1 i1 i0 i0
ebbafcb4 240 "unpcklps %%xmm2, %%xmm1 \n\t" // xmm1 = I1 R1 I0 R0
25e4f8aa 241 "unpcklps %%xmm6, %%xmm5 \n\t" // xmm5 = I1 R1 I0 R0
ebbafcb4 242 "movaps %%xmm1, %%xmm2 \n\t" // xmm2 = I1 R1 I0 R0
25e4f8aa
ZM
243 "movaps %%xmm5, %%xmm6 \n\t" // xmm6 = I1 R1 I0 R0
244 "xorps "P1M1P1M1", %%xmm2 \n\t" // xmm2 = -I1 R1 -I0 R0
ebbafcb4 245 "mulps %%xmm1, %%xmm0 \n\t" // xmm0 = rI rR rI rR
25e4f8aa
ZM
246 "xorps "P1M1P1M1", %%xmm6 \n\t" // xmm6 = -I1 R1 -I0 R0
247 "mulps %%xmm5, %%xmm4 \n\t" // xmm4 = rI rR rI rR
ebbafcb4 248 "shufps $177,%%xmm2, %%xmm2 \n\t" // xmm2 = R1 -I1 R0 -I0
25e4f8aa 249 "shufps $177,%%xmm6, %%xmm6 \n\t" // xmm6 = R1 -I1 R0 -I0
ebbafcb4 250 "mulps %%xmm2, %%xmm3 \n\t" // xmm3 = Ri -Ii Ri -Ii
25e4f8aa 251 "mulps %%xmm6, %%xmm7 \n\t" // xmm7 = Ri -Ii Ri -Ii
ebbafcb4 252 "addps %%xmm3, %%xmm0 \n\t" // xmm0 = result
25e4f8aa 253 "addps %%xmm7, %%xmm4 \n\t" // xmm4 = result
ebbafcb4 254 "movaps %%xmm0, %0 \n\t"
ee387b57 255 "movaps %%xmm4, 16+1*%0\n\t"
ebbafcb4
LM
256 :"+m"(z[k])
257 :"m"(tcos[k]), "m"(tsin[k])
25e4f8aa
ZM
258#ifndef ARCH_X86_64
259 ,"m"(*p1m1p1m1)
260#endif
ebbafcb4
LM
261 );
262 }
263
264 /*
265 Mnemonics:
266 0 = z[k].re
267 1 = z[k].im
268 2 = z[k + 1].re
269 3 = z[k + 1].im
270 4 = z[-k - 2].re
271 5 = z[-k - 2].im
272 6 = z[-k - 1].re
273 7 = z[-k - 1].im
274 */
275 k = 16-n;
276 asm volatile("movaps %0, %%xmm7 \n\t"::"m"(*m1m1m1m1));
277 asm volatile(
278 "1: \n\t"
279 "movaps -16(%4,%0), %%xmm1 \n\t" // xmm1 = 4 5 6 7 = z[-2-k]
280 "neg %0 \n\t"
281 "movaps (%4,%0), %%xmm0 \n\t" // xmm0 = 0 1 2 3 = z[k]
282 "xorps %%xmm7, %%xmm0 \n\t" // xmm0 = -0 -1 -2 -3
283 "movaps %%xmm0, %%xmm2 \n\t" // xmm2 = -0 -1 -2 -3
284 "shufps $141,%%xmm1, %%xmm0 \n\t" // xmm0 = -1 -3 4 6
285 "shufps $216,%%xmm1, %%xmm2 \n\t" // xmm2 = -0 -2 5 7
286 "shufps $156,%%xmm0, %%xmm0 \n\t" // xmm0 = -1 6 -3 4 !
287 "shufps $156,%%xmm2, %%xmm2 \n\t" // xmm2 = -0 7 -2 5 !
288 "movaps %%xmm0, (%1,%0) \n\t" // output[2*k]
289 "movaps %%xmm2, (%2,%0) \n\t" // output[n2+2*k]
290 "neg %0 \n\t"
291 "shufps $27, %%xmm0, %%xmm0 \n\t" // xmm0 = 4 -3 6 -1
292 "xorps %%xmm7, %%xmm0 \n\t" // xmm0 = -4 3 -6 1 !
293 "shufps $27, %%xmm2, %%xmm2 \n\t" // xmm2 = 5 -2 7 -0 !
294 "movaps %%xmm0, -16(%2,%0) \n\t" // output[n2-4-2*k]
295 "movaps %%xmm2, -16(%3,%0) \n\t" // output[n-4-2*k]
296 "add $16, %0 \n\t"
297 "jle 1b \n\t"
298 :"+r"(k)
299 :"r"(output), "r"(output+n2), "r"(output+n), "r"(z+n8)
300 :"memory"
301 );
302}
303