Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / fft_sse.c @ 40d0e665

History | View | Annotate | Download (11.5 KB)

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