fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and...
[libav.git] / libavcodec / ppc / fft_altivec.c
1 /*
2 * FFT/IFFT transforms
3 * AltiVec-enabled
4 * Copyright (c) 2002 Romain Dolbeau <romain@dolbeau.org>
5 * Based on code Copyright (c) 2002 Fabrice Bellard.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
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
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 #include "../dsputil.h"
22
23 #include "dsputil_altivec.h"
24
25 // used to build registers permutation vectors (vcprm)
26 // the 's' are for words in the _s_econd vector
27 #define WORD_0 0x00,0x01,0x02,0x03
28 #define WORD_1 0x04,0x05,0x06,0x07
29 #define WORD_2 0x08,0x09,0x0a,0x0b
30 #define WORD_3 0x0c,0x0d,0x0e,0x0f
31 #define WORD_s0 0x10,0x11,0x12,0x13
32 #define WORD_s1 0x14,0x15,0x16,0x17
33 #define WORD_s2 0x18,0x19,0x1a,0x1b
34 #define WORD_s3 0x1c,0x1d,0x1e,0x1f
35
36 #define vcprm(a,b,c,d) (const vector unsigned char)(WORD_ ## a, WORD_ ## b, WORD_ ## c, WORD_ ## d)
37
38 // vcprmle is used to keep the same index as in the SSE version.
39 // it's the same as vcprm, with the index inversed
40 // ('le' is Little Endian)
41 #define vcprmle(a,b,c,d) vcprm(d,c,b,a)
42
43 // used to build inverse/identity vectors (vcii)
44 // n is _n_egative, p is _p_ositive
45 #define FLOAT_n -1.
46 #define FLOAT_p 1.
47
48 #define vcii(a,b,c,d) (const vector float)(FLOAT_ ## a, FLOAT_ ## b, FLOAT_ ## c, FLOAT_ ## d)
49
50 /**
51 * Do a complex FFT with the parameters defined in fft_init(). The
52 * input data must be permuted before with s->revtab table. No
53 * 1.0/sqrt(n) normalization is done.
54 * AltiVec-enabled
55 * This code assumes that the 'z' pointer is 16 bytes-aligned
56 * It also assumes all FFTComplex are 8 bytes-aligned pair of float
57 * The code is exactly the same as the SSE version, except
58 * that successive MUL + ADD/SUB have been fusionned into
59 * fused multiply-add ('vec_madd' in altivec)
60 *
61 * To test this code you can use fft-test in libavcodec ; use
62 * the following line in libavcodec to compile (MacOS X):
63 * #####
64 * gcc -I. -Ippc -no-cpp-precomp -pipe -O3 -fomit-frame-pointer -mdynamic-no-pic -Wall
65 * -faltivec -DARCH_POWERPC -DHAVE_ALTIVEC -DCONFIG_DARWIN fft-test.c fft.c
66 * ppc/fft_altivec.c ppc/dsputil_altivec.c mdct.c -DHAVE_LRINTF -o fft-test
67 * #####
68 */
69 void fft_calc_altivec(FFTContext *s, FFTComplex *z)
70 {
71 register const vector float vczero = (vector float)( 0., 0., 0., 0.);
72
73 int ln = s->nbits;
74 int j, np, np2;
75 int nblocks, nloops;
76 register FFTComplex *p, *q;
77 FFTComplex *cptr, *cptr1;
78 int k;
79
80 np = 1 << ln;
81
82 {
83 vector float *r, a, b, a1, c1, c2;
84
85 r = (vector float *)&z[0];
86
87 c1 = vcii(p,p,n,n);
88
89 if (s->inverse)
90 {
91 c2 = vcii(p,p,n,p);
92 }
93 else
94 {
95 c2 = vcii(p,p,p,n);
96 }
97
98 j = (np >> 2);
99 do {
100 a = vec_ld(0, r);
101 a1 = vec_ld(sizeof(vector float), r);
102
103 b = vec_perm(a,a,vcprmle(1,0,3,2));
104 a = vec_madd(a,c1,b);
105 /* do the pass 0 butterfly */
106
107 b = vec_perm(a1,a1,vcprmle(1,0,3,2));
108 b = vec_madd(a1,c1,b);
109 /* do the pass 0 butterfly */
110
111 /* multiply third by -i */
112 b = vec_perm(b,b,vcprmle(2,3,1,0));
113
114 /* do the pass 1 butterfly */
115 vec_st(vec_madd(b,c2,a), 0, r);
116 vec_st(vec_nmsub(b,c2,a), sizeof(vector float), r);
117
118 r += 2;
119 } while (--j != 0);
120 }
121 /* pass 2 .. ln-1 */
122
123 nblocks = np >> 3;
124 nloops = 1 << 2;
125 np2 = np >> 1;
126
127 cptr1 = s->exptab1;
128 do {
129 p = z;
130 q = z + nloops;
131 j = nblocks;
132 do {
133 cptr = cptr1;
134 k = nloops >> 1;
135 do {
136 vector float a,b,c,t1;
137
138 a = vec_ld(0, (float*)p);
139 b = vec_ld(0, (float*)q);
140
141 /* complex mul */
142 c = vec_ld(0, (float*)cptr);
143 /* cre*re cim*re */
144 t1 = vec_madd(c, vec_perm(b,b,vcprmle(2,2,0,0)),vczero);
145 c = vec_ld(sizeof(vector float), (float*)cptr);
146 /* -cim*im cre*im */
147 b = vec_madd(c, vec_perm(b,b,vcprmle(3,3,1,1)),t1);
148
149 /* butterfly */
150 vec_st(vec_add(a,b), 0, (float*)p);
151 vec_st(vec_sub(a,b), 0, (float*)q);
152
153 p += 2;
154 q += 2;
155 cptr += 4;
156 } while (--k);
157
158 p += nloops;
159 q += nloops;
160 } while (--j);
161 cptr1 += nloops * 2;
162 nblocks = nblocks >> 1;
163 nloops = nloops << 1;
164 } while (nblocks != 0);
165 }
166