comparison fft/fftw/fftw-3.3.4/reodft/reodft00e-splitradix.c @ 19:26056e866c29

Add FFTW to comparison table
author Chris Cannam
date Tue, 06 Oct 2015 13:08:39 +0100
parents
children
comparison
equal deleted inserted replaced
18:8db794ca3e0b 19:26056e866c29
1 /*
2 * Copyright (c) 2005 Matteo Frigo
3 * Copyright (c) 2005 Massachusetts Institute of Technology
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21
22 /* Do an R{E,O}DFT00 problem (of an odd length n) recursively via an
23 R{E,O}DFT00 problem and an RDFT problem of half the length.
24
25 This works by "logically" expanding the array to a real-even/odd DFT of
26 length 2n-/+2 and then applying the split-radix algorithm.
27
28 In this way, we can avoid having to pad to twice the length
29 (ala redft00-r2hc-pad), saving a factor of ~2 for n=2^m+/-1,
30 but don't incur the accuracy loss that the "ordinary" algorithm
31 sacrifices (ala redft00-r2hc.c).
32 */
33
34 #include "reodft.h"
35
36 typedef struct {
37 solver super;
38 } S;
39
40 typedef struct {
41 plan_rdft super;
42 plan *clde, *cldo;
43 twid *td;
44 INT is, os;
45 INT n;
46 INT vl;
47 INT ivs, ovs;
48 } P;
49
50 /* redft00 */
51 static void apply_e(const plan *ego_, R *I, R *O)
52 {
53 const P *ego = (const P *) ego_;
54 INT is = ego->is, os = ego->os;
55 INT i, j, n = ego->n + 1, n2 = (n-1)/2;
56 INT iv, vl = ego->vl;
57 INT ivs = ego->ivs, ovs = ego->ovs;
58 R *W = ego->td->W - 2;
59 R *buf;
60
61 buf = (R *) MALLOC(sizeof(R) * n2, BUFFERS);
62
63 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
64 /* do size (n-1)/2 r2hc transform of odd-indexed elements
65 with stride 4, "wrapping around" end of array with even
66 boundary conditions */
67 for (j = 0, i = 1; i < n; i += 4)
68 buf[j++] = I[is * i];
69 for (i = 2*n-2-i; i > 0; i -= 4)
70 buf[j++] = I[is * i];
71 {
72 plan_rdft *cld = (plan_rdft *) ego->cldo;
73 cld->apply((plan *) cld, buf, buf);
74 }
75
76 /* do size (n+1)/2 redft00 of the even-indexed elements,
77 writing to O: */
78 {
79 plan_rdft *cld = (plan_rdft *) ego->clde;
80 cld->apply((plan *) cld, I, O);
81 }
82
83 /* combine the results with the twiddle factors to get output */
84 { /* DC element */
85 E b20 = O[0], b0 = K(2.0) * buf[0];
86 O[0] = b20 + b0;
87 O[2*(n2*os)] = b20 - b0;
88 /* O[n2*os] = O[n2*os]; */
89 }
90 for (i = 1; i < n2 - i; ++i) {
91 E ap, am, br, bi, wr, wi, wbr, wbi;
92 br = buf[i];
93 bi = buf[n2 - i];
94 wr = W[2*i];
95 wi = W[2*i+1];
96 #if FFT_SIGN == -1
97 wbr = K(2.0) * (wr*br + wi*bi);
98 wbi = K(2.0) * (wr*bi - wi*br);
99 #else
100 wbr = K(2.0) * (wr*br - wi*bi);
101 wbi = K(2.0) * (wr*bi + wi*br);
102 #endif
103 ap = O[i*os];
104 O[i*os] = ap + wbr;
105 O[(2*n2 - i)*os] = ap - wbr;
106 am = O[(n2 - i)*os];
107 #if FFT_SIGN == -1
108 O[(n2 - i)*os] = am - wbi;
109 O[(n2 + i)*os] = am + wbi;
110 #else
111 O[(n2 - i)*os] = am + wbi;
112 O[(n2 + i)*os] = am - wbi;
113 #endif
114 }
115 if (i == n2 - i) { /* Nyquist element */
116 E ap, wbr;
117 wbr = K(2.0) * (W[2*i] * buf[i]);
118 ap = O[i*os];
119 O[i*os] = ap + wbr;
120 O[(2*n2 - i)*os] = ap - wbr;
121 }
122 }
123
124 X(ifree)(buf);
125 }
126
127 /* rodft00 */
128 static void apply_o(const plan *ego_, R *I, R *O)
129 {
130 const P *ego = (const P *) ego_;
131 INT is = ego->is, os = ego->os;
132 INT i, j, n = ego->n - 1, n2 = (n+1)/2;
133 INT iv, vl = ego->vl;
134 INT ivs = ego->ivs, ovs = ego->ovs;
135 R *W = ego->td->W - 2;
136 R *buf;
137
138 buf = (R *) MALLOC(sizeof(R) * n2, BUFFERS);
139
140 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
141 /* do size (n+1)/2 r2hc transform of even-indexed elements
142 with stride 4, "wrapping around" end of array with odd
143 boundary conditions */
144 for (j = 0, i = 0; i < n; i += 4)
145 buf[j++] = I[is * i];
146 for (i = 2*n-i; i > 0; i -= 4)
147 buf[j++] = -I[is * i];
148 {
149 plan_rdft *cld = (plan_rdft *) ego->cldo;
150 cld->apply((plan *) cld, buf, buf);
151 }
152
153 /* do size (n-1)/2 rodft00 of the odd-indexed elements,
154 writing to O: */
155 {
156 plan_rdft *cld = (plan_rdft *) ego->clde;
157 if (I == O) {
158 /* can't use I+is and I, subplan would lose in-placeness */
159 cld->apply((plan *) cld, I + is, I + is);
160 /* we could maybe avoid this copy by modifying the
161 twiddle loop, but currently I can't be bothered. */
162 A(is >= os);
163 for (i = 0; i < n2-1; ++i)
164 O[os*i] = I[is*(i+1)];
165 }
166 else
167 cld->apply((plan *) cld, I + is, O);
168 }
169
170 /* combine the results with the twiddle factors to get output */
171 O[(n2-1)*os] = K(2.0) * buf[0];
172 for (i = 1; i < n2 - i; ++i) {
173 E ap, am, br, bi, wr, wi, wbr, wbi;
174 br = buf[i];
175 bi = buf[n2 - i];
176 wr = W[2*i];
177 wi = W[2*i+1];
178 #if FFT_SIGN == -1
179 wbr = K(2.0) * (wr*br + wi*bi);
180 wbi = K(2.0) * (wi*br - wr*bi);
181 #else
182 wbr = K(2.0) * (wr*br - wi*bi);
183 wbi = K(2.0) * (wr*bi + wi*br);
184 #endif
185 ap = O[(i-1)*os];
186 O[(i-1)*os] = wbi + ap;
187 O[(2*n2-1 - i)*os] = wbi - ap;
188 am = O[(n2-1 - i)*os];
189 #if FFT_SIGN == -1
190 O[(n2-1 - i)*os] = wbr + am;
191 O[(n2-1 + i)*os] = wbr - am;
192 #else
193 O[(n2-1 - i)*os] = wbr + am;
194 O[(n2-1 + i)*os] = wbr - am;
195 #endif
196 }
197 if (i == n2 - i) { /* Nyquist element */
198 E ap, wbi;
199 wbi = K(2.0) * (W[2*i+1] * buf[i]);
200 ap = O[(i-1)*os];
201 O[(i-1)*os] = wbi + ap;
202 O[(2*n2-1 - i)*os] = wbi - ap;
203 }
204 }
205
206 X(ifree)(buf);
207 }
208
209 static void awake(plan *ego_, enum wakefulness wakefulness)
210 {
211 P *ego = (P *) ego_;
212 static const tw_instr reodft00e_tw[] = {
213 { TW_COS, 1, 1 },
214 { TW_SIN, 1, 1 },
215 { TW_NEXT, 1, 0 }
216 };
217
218 X(plan_awake)(ego->clde, wakefulness);
219 X(plan_awake)(ego->cldo, wakefulness);
220 X(twiddle_awake)(wakefulness, &ego->td, reodft00e_tw,
221 2*ego->n, 1, ego->n/4);
222 }
223
224 static void destroy(plan *ego_)
225 {
226 P *ego = (P *) ego_;
227 X(plan_destroy_internal)(ego->cldo);
228 X(plan_destroy_internal)(ego->clde);
229 }
230
231 static void print(const plan *ego_, printer *p)
232 {
233 const P *ego = (const P *) ego_;
234 if (ego->super.apply == apply_e)
235 p->print(p, "(redft00e-splitradix-%D%v%(%p%)%(%p%))",
236 ego->n + 1, ego->vl, ego->clde, ego->cldo);
237 else
238 p->print(p, "(rodft00e-splitradix-%D%v%(%p%)%(%p%))",
239 ego->n - 1, ego->vl, ego->clde, ego->cldo);
240 }
241
242 static int applicable0(const solver *ego_, const problem *p_)
243 {
244 const problem_rdft *p = (const problem_rdft *) p_;
245 UNUSED(ego_);
246
247 return (1
248 && p->sz->rnk == 1
249 && p->vecsz->rnk <= 1
250 && (p->kind[0] == REDFT00 || p->kind[0] == RODFT00)
251 && p->sz->dims[0].n > 1 /* don't create size-0 sub-plans */
252 && p->sz->dims[0].n % 2 /* odd: 4 divides "logical" DFT */
253 && (p->I != p->O || p->vecsz->rnk == 0
254 || p->vecsz->dims[0].is == p->vecsz->dims[0].os)
255 && (p->kind[0] != RODFT00 || p->I != p->O ||
256 p->sz->dims[0].is >= p->sz->dims[0].os) /* laziness */
257 );
258 }
259
260 static int applicable(const solver *ego, const problem *p, const planner *plnr)
261 {
262 return (!NO_SLOWP(plnr) && applicable0(ego, p));
263 }
264
265 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
266 {
267 P *pln;
268 const problem_rdft *p;
269 plan *clde, *cldo;
270 R *buf;
271 INT n, n0;
272 opcnt ops;
273 int inplace_odd;
274
275 static const plan_adt padt = {
276 X(rdft_solve), awake, print, destroy
277 };
278
279 if (!applicable(ego_, p_, plnr))
280 return (plan *)0;
281
282 p = (const problem_rdft *) p_;
283
284 n = (n0 = p->sz->dims[0].n) + (p->kind[0] == REDFT00 ? (INT)-1 : (INT)1);
285 A(n > 0 && n % 2 == 0);
286 buf = (R *) MALLOC(sizeof(R) * (n/2), BUFFERS);
287
288 inplace_odd = p->kind[0]==RODFT00 && p->I == p->O;
289 clde = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(
290 X(mktensor_1d)(n0-n/2, 2*p->sz->dims[0].is,
291 inplace_odd ? p->sz->dims[0].is
292 : p->sz->dims[0].os),
293 X(mktensor_0d)(),
294 TAINT(p->I
295 + p->sz->dims[0].is * (p->kind[0]==RODFT00),
296 p->vecsz->rnk ? p->vecsz->dims[0].is : 0),
297 TAINT(p->O
298 + p->sz->dims[0].is * inplace_odd,
299 p->vecsz->rnk ? p->vecsz->dims[0].os : 0),
300 p->kind[0]));
301 if (!clde) {
302 X(ifree)(buf);
303 return (plan *)0;
304 }
305
306 cldo = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(
307 X(mktensor_1d)(n/2, 1, 1),
308 X(mktensor_0d)(),
309 buf, buf, R2HC));
310 X(ifree)(buf);
311 if (!cldo)
312 return (plan *)0;
313
314 pln = MKPLAN_RDFT(P, &padt, p->kind[0] == REDFT00 ? apply_e : apply_o);
315
316 pln->n = n;
317 pln->is = p->sz->dims[0].is;
318 pln->os = p->sz->dims[0].os;
319 pln->clde = clde;
320 pln->cldo = cldo;
321 pln->td = 0;
322
323 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
324
325 X(ops_zero)(&ops);
326 ops.other = n/2;
327 ops.add = (p->kind[0]==REDFT00 ? (INT)2 : (INT)0) +
328 (n/2-1)/2 * 6 + ((n/2)%2==0) * 2;
329 ops.mul = 1 + (n/2-1)/2 * 6 + ((n/2)%2==0) * 2;
330
331 /* tweak ops.other so that r2hc-pad is used for small sizes, which
332 seems to be a lot faster on my machine: */
333 ops.other += 256;
334
335 X(ops_zero)(&pln->super.super.ops);
336 X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops);
337 X(ops_madd2)(pln->vl, &clde->ops, &pln->super.super.ops);
338 X(ops_madd2)(pln->vl, &cldo->ops, &pln->super.super.ops);
339
340 return &(pln->super.super);
341 }
342
343 /* constructor */
344 static solver *mksolver(void)
345 {
346 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
347 S *slv = MKSOLVER(S, &sadt);
348 return &(slv->super);
349 }
350
351 void X(reodft00e_splitradix_register)(planner *p)
352 {
353 REGISTER_SOLVER(p, mksolver());
354 }