Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / fft_sse.c @ be449fca

History | View | Annotate | Download (6.75 KB)

1 bb6f5690 Fabrice Bellard
/*
2
 * FFT/MDCT transform with SSE optimizations
3 46803f4f Loren Merritt
 * Copyright (c) 2008 Loren Merritt
4 bb6f5690 Fabrice Bellard
 *
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 245976da Diego Biurrun
22
#include "libavutil/x86_cpu.h"
23
#include "libavcodec/dsputil.h"
24 bb6f5690 Fabrice Bellard
25 ebbafcb4 Loren Merritt
static const int m1m1m1m1[4] __attribute__((aligned(16))) =
26
    { 1 << 31, 1 << 31, 1 << 31, 1 << 31 };
27
28 5d0ddd1a Loren Merritt
void ff_fft_dispatch_sse(FFTComplex *z, int nbits);
29
void ff_fft_dispatch_interleave_sse(FFTComplex *z, int nbits);
30 bb6f5690 Fabrice Bellard
31 68951ecf Gildas Bazin
void ff_fft_calc_sse(FFTContext *s, FFTComplex *z)
32 bb6f5690 Fabrice Bellard
{
33 5d0ddd1a Loren Merritt
    int n = 1 << s->nbits;
34
35
    ff_fft_dispatch_interleave_sse(z, s->nbits);
36
37
    if(n <= 16) {
38
        x86_reg i = -8*n;
39 be449fca Diego Pettenò
        __asm__ volatile(
40 5d0ddd1a Loren Merritt
            "1: \n"
41
            "movaps     (%0,%1), %%xmm0 \n"
42
            "movaps      %%xmm0, %%xmm1 \n"
43
            "unpcklps 16(%0,%1), %%xmm0 \n"
44
            "unpckhps 16(%0,%1), %%xmm1 \n"
45
            "movaps      %%xmm0,   (%0,%1) \n"
46
            "movaps      %%xmm1, 16(%0,%1) \n"
47
            "add $32, %0 \n"
48
            "jl 1b \n"
49
            :"+r"(i)
50
            :"r"(z+n)
51
            :"memory"
52
        );
53
    }
54
}
55 bb6f5690 Fabrice Bellard
56 5d0ddd1a Loren Merritt
void ff_fft_permute_sse(FFTContext *s, FFTComplex *z)
57
{
58
    int n = 1 << s->nbits;
59
    int i;
60
    for(i=0; i<n; i+=2) {
61 be449fca Diego Pettenò
        __asm__ volatile(
62 5d0ddd1a Loren Merritt
            "movaps %2, %%xmm0 \n"
63
            "movlps %%xmm0, %0 \n"
64
            "movhps %%xmm0, %1 \n"
65
            :"=m"(s->tmp_buf[s->revtab[i]]),
66
             "=m"(s->tmp_buf[s->revtab[i+1]])
67
            :"m"(z[i])
68
        );
69
    }
70
    memcpy(z, s->tmp_buf, n*sizeof(FFTComplex));
71 bb6f5690 Fabrice Bellard
}
72 e1958604 Fabrice Bellard
73 46803f4f Loren Merritt
void ff_imdct_half_sse(MDCTContext *s, FFTSample *output, const FFTSample *input)
74 ebbafcb4 Loren Merritt
{
75 46803f4f Loren Merritt
    av_unused x86_reg i, j, k, l;
76
    long n = 1 << s->nbits;
77
    long n2 = n >> 1;
78
    long n4 = n >> 2;
79
    long n8 = n >> 3;
80
    const uint16_t *revtab = s->fft.revtab + n8;
81 ebbafcb4 Loren Merritt
    const FFTSample *tcos = s->tcos;
82
    const FFTSample *tsin = s->tsin;
83 46803f4f Loren Merritt
    FFTComplex *z = (FFTComplex *)output;
84 ebbafcb4 Loren Merritt
85
    /* pre rotation */
86 46803f4f Loren Merritt
    for(k=n8-2; k>=0; k-=2) {
87 be449fca Diego Pettenò
        __asm__ volatile(
88 46803f4f Loren Merritt
            "movaps     (%2,%1,2), %%xmm0 \n" // { z[k].re,    z[k].im,    z[k+1].re,  z[k+1].im  }
89
            "movaps  -16(%2,%0,2), %%xmm1 \n" // { z[-k-2].re, z[-k-2].im, z[-k-1].re, z[-k-1].im }
90
            "movaps        %%xmm0, %%xmm2 \n"
91
            "shufps $0x88, %%xmm1, %%xmm0 \n" // { z[k].re,    z[k+1].re,  z[-k-2].re, z[-k-1].re }
92
            "shufps $0x77, %%xmm2, %%xmm1 \n" // { z[-k-1].im, z[-k-2].im, z[k+1].im,  z[k].im    }
93
            "movlps       (%3,%1), %%xmm4 \n"
94
            "movlps       (%4,%1), %%xmm5 \n"
95
            "movhps     -8(%3,%0), %%xmm4 \n" // { cos[k],     cos[k+1],   cos[-k-2],  cos[-k-1]  }
96
            "movhps     -8(%4,%0), %%xmm5 \n" // { sin[k],     sin[k+1],   sin[-k-2],  sin[-k-1]  }
97
            "movaps        %%xmm0, %%xmm2 \n"
98
            "movaps        %%xmm1, %%xmm3 \n"
99
            "mulps         %%xmm5, %%xmm0 \n" // re*sin
100
            "mulps         %%xmm4, %%xmm1 \n" // im*cos
101
            "mulps         %%xmm4, %%xmm2 \n" // re*cos
102
            "mulps         %%xmm5, %%xmm3 \n" // im*sin
103
            "subps         %%xmm0, %%xmm1 \n" // -> re
104
            "addps         %%xmm3, %%xmm2 \n" // -> im
105
            "movaps        %%xmm1, %%xmm0 \n"
106
            "unpcklps      %%xmm2, %%xmm1 \n" // { z[k],    z[k+1]  }
107
            "unpckhps      %%xmm2, %%xmm0 \n" // { z[-k-2], z[-k-1] }
108
            ::"r"(-4*k), "r"(4*k),
109
              "r"(input+n4), "r"(tcos+n8), "r"(tsin+n8)
110 ebbafcb4 Loren Merritt
        );
111 46803f4f Loren Merritt
#ifdef ARCH_X86_64
112
        // if we have enough regs, don't let gcc make the luts latency-bound
113
        // but if not, latency is faster than spilling
114 be449fca Diego Pettenò
        __asm__("movlps %%xmm0, %0 \n"
115 46803f4f Loren Merritt
            "movhps %%xmm0, %1 \n"
116
            "movlps %%xmm1, %2 \n"
117
            "movhps %%xmm1, %3 \n"
118
            :"=m"(z[revtab[-k-2]]),
119
             "=m"(z[revtab[-k-1]]),
120
             "=m"(z[revtab[ k  ]]),
121
             "=m"(z[revtab[ k+1]])
122 ebbafcb4 Loren Merritt
        );
123 46803f4f Loren Merritt
#else
124 be449fca Diego Pettenò
        __asm__("movlps %%xmm0, %0" :"=m"(z[revtab[-k-2]]));
125
        __asm__("movhps %%xmm0, %0" :"=m"(z[revtab[-k-1]]));
126
        __asm__("movlps %%xmm1, %0" :"=m"(z[revtab[ k  ]]));
127
        __asm__("movhps %%xmm1, %0" :"=m"(z[revtab[ k+1]]));
128 25e4f8aa Zuxy Meng
#endif
129 ebbafcb4 Loren Merritt
    }
130 b9fa3208 Loren Merritt
131 46803f4f Loren Merritt
    ff_fft_dispatch_sse(z, s->fft.nbits);
132 b9fa3208 Loren Merritt
133 46803f4f Loren Merritt
    /* post rotation + reinterleave + reorder */
134 b9fa3208 Loren Merritt
135 46803f4f Loren Merritt
#define CMUL(j,xmm0,xmm1)\
136
        "movaps   (%2,"#j",2), %%xmm6 \n"\
137
        "movaps 16(%2,"#j",2), "#xmm0"\n"\
138
        "movaps        %%xmm6, "#xmm1"\n"\
139
        "movaps        "#xmm0",%%xmm7 \n"\
140
        "mulps      (%3,"#j"), %%xmm6 \n"\
141
        "mulps      (%4,"#j"), "#xmm0"\n"\
142
        "mulps      (%4,"#j"), "#xmm1"\n"\
143
        "mulps      (%3,"#j"), %%xmm7 \n"\
144
        "subps         %%xmm6, "#xmm0"\n"\
145
        "addps         %%xmm7, "#xmm1"\n"
146 ebbafcb4 Loren Merritt
147 46803f4f Loren Merritt
    j = -n2;
148
    k = n2-16;
149 be449fca Diego Pettenò
    __asm__ volatile(
150 46803f4f Loren Merritt
        "1: \n"
151
        CMUL(%0, %%xmm0, %%xmm1)
152
        CMUL(%1, %%xmm4, %%xmm5)
153
        "shufps    $0x1b, %%xmm1, %%xmm1 \n"
154
        "shufps    $0x1b, %%xmm5, %%xmm5 \n"
155
        "movaps   %%xmm4, %%xmm6 \n"
156
        "unpckhps %%xmm1, %%xmm4 \n"
157
        "unpcklps %%xmm1, %%xmm6 \n"
158
        "movaps   %%xmm0, %%xmm2 \n"
159
        "unpcklps %%xmm5, %%xmm0 \n"
160
        "unpckhps %%xmm5, %%xmm2 \n"
161
        "movaps   %%xmm6,   (%2,%1,2) \n"
162
        "movaps   %%xmm4, 16(%2,%1,2) \n"
163
        "movaps   %%xmm0,   (%2,%0,2) \n"
164
        "movaps   %%xmm2, 16(%2,%0,2) \n"
165
        "sub $16, %1 \n"
166
        "add $16, %0 \n"
167
        "jl 1b \n"
168
        :"+&r"(j), "+&r"(k)
169
        :"r"(z+n8), "r"(tcos+n8), "r"(tsin+n8)
170 ebbafcb4 Loren Merritt
        :"memory"
171
    );
172
}
173
174 0a570e82 Loren Merritt
void ff_imdct_calc_sse(MDCTContext *s, FFTSample *output, const FFTSample *input)
175 b9fa3208 Loren Merritt
{
176
    x86_reg j, k;
177 46803f4f Loren Merritt
    long n = 1 << s->nbits;
178
    long n4 = n >> 2;
179 b9fa3208 Loren Merritt
180 46803f4f Loren Merritt
    ff_imdct_half_sse(s, output+n4, input);
181 b9fa3208 Loren Merritt
182
    j = -n;
183
    k = n-16;
184 be449fca Diego Pettenò
    __asm__ volatile(
185 46803f4f Loren Merritt
        "movaps %4, %%xmm7 \n"
186
        "1: \n"
187
        "movaps       (%2,%1), %%xmm0 \n"
188
        "movaps       (%3,%0), %%xmm1 \n"
189
        "shufps $0x1b, %%xmm0, %%xmm0 \n"
190
        "shufps $0x1b, %%xmm1, %%xmm1 \n"
191
        "xorps         %%xmm7, %%xmm0 \n"
192
        "movaps        %%xmm1, (%3,%1) \n"
193
        "movaps        %%xmm0, (%2,%0) \n"
194
        "sub $16, %1 \n"
195
        "add $16, %0 \n"
196
        "jl 1b \n"
197 b9fa3208 Loren Merritt
        :"+r"(j), "+r"(k)
198 46803f4f Loren Merritt
        :"r"(output+n4), "r"(output+n4*3),
199
         "m"(*m1m1m1m1)
200 b9fa3208 Loren Merritt
    );
201
}