~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/i386/fft_sse.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

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