/src/fftw3/dft/bluestein.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2003, 2007-14 Matteo Frigo |
3 | | * Copyright (c) 2003, 2007-14 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 | | #include "dft/dft.h" |
22 | | |
23 | | typedef struct { |
24 | | solver super; |
25 | | } S; |
26 | | |
27 | | typedef struct { |
28 | | plan_dft super; |
29 | | INT n; /* problem size */ |
30 | | INT nb; /* size of convolution */ |
31 | | R *w; /* lambda k . exp(2*pi*i*k^2/(2*n)) */ |
32 | | R *W; /* DFT(w) */ |
33 | | plan *cldf; |
34 | | INT is, os; |
35 | | } P; |
36 | | |
37 | | static void bluestein_sequence(enum wakefulness wakefulness, INT n, R *w) |
38 | 24 | { |
39 | 24 | INT k, ksq, n2 = 2 * n; |
40 | 24 | triggen *t = X(mktriggen)(wakefulness, n2); |
41 | | |
42 | 24 | ksq = 0; |
43 | 3.79k | for (k = 0; k < n; ++k) { |
44 | 3.77k | t->cexp(t, ksq, w+2*k); |
45 | | /* careful with overflow */ |
46 | 5.64k | ksq += 2*k + 1; while (ksq > n2) ksq -= n2; |
47 | 3.77k | } |
48 | | |
49 | 24 | X(triggen_destroy)(t); |
50 | 24 | } |
51 | | |
52 | | static void mktwiddle(enum wakefulness wakefulness, P *p) |
53 | 24 | { |
54 | 24 | INT i; |
55 | 24 | INT n = p->n, nb = p->nb; |
56 | 24 | R *w, *W; |
57 | 24 | E nbf = (E)nb; |
58 | | |
59 | 24 | p->w = w = (R *) MALLOC(2 * n * sizeof(R), TWIDDLES); |
60 | 24 | p->W = W = (R *) MALLOC(2 * nb * sizeof(R), TWIDDLES); |
61 | | |
62 | 24 | bluestein_sequence(wakefulness, n, w); |
63 | | |
64 | 7.71k | for (i = 0; i < nb; ++i) |
65 | 7.68k | W[2*i] = W[2*i+1] = K(0.0); |
66 | | |
67 | 24 | W[0] = w[0] / nbf; |
68 | 24 | W[1] = w[1] / nbf; |
69 | | |
70 | 3.77k | for (i = 1; i < n; ++i) { |
71 | 3.74k | W[2*i] = W[2*(nb-i)] = w[2*i] / nbf; |
72 | 3.74k | W[2*i+1] = W[2*(nb-i)+1] = w[2*i+1] / nbf; |
73 | 3.74k | } |
74 | | |
75 | 24 | { |
76 | 24 | plan_dft *cldf = (plan_dft *)p->cldf; |
77 | | /* cldf must be awake */ |
78 | 24 | cldf->apply(p->cldf, W, W+1, W, W+1); |
79 | 24 | } |
80 | 24 | } |
81 | | |
82 | | static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io) |
83 | 26 | { |
84 | 26 | const P *ego = (const P *) ego_; |
85 | 26 | INT i, n = ego->n, nb = ego->nb, is = ego->is, os = ego->os; |
86 | 26 | R *w = ego->w, *W = ego->W; |
87 | 26 | R *b = (R *) MALLOC(2 * nb * sizeof(R), BUFFERS); |
88 | | |
89 | | /* multiply input by conjugate bluestein sequence */ |
90 | 4.01k | for (i = 0; i < n; ++i) { |
91 | 3.99k | E xr = ri[i*is], xi = ii[i*is]; |
92 | 3.99k | E wr = w[2*i], wi = w[2*i+1]; |
93 | 3.99k | b[2*i] = xr * wr + xi * wi; |
94 | 3.99k | b[2*i+1] = xi * wr - xr * wi; |
95 | 3.99k | } |
96 | | |
97 | 4.16k | for (; i < nb; ++i) b[2*i] = b[2*i+1] = K(0.0); |
98 | | |
99 | | /* convolution: FFT */ |
100 | 26 | { |
101 | 26 | plan_dft *cldf = (plan_dft *)ego->cldf; |
102 | 26 | cldf->apply(ego->cldf, b, b+1, b, b+1); |
103 | 26 | } |
104 | | |
105 | | /* convolution: pointwise multiplication */ |
106 | 8.15k | for (i = 0; i < nb; ++i) { |
107 | 8.12k | E xr = b[2*i], xi = b[2*i+1]; |
108 | 8.12k | E wr = W[2*i], wi = W[2*i+1]; |
109 | 8.12k | b[2*i] = xi * wr + xr * wi; |
110 | 8.12k | b[2*i+1] = xr * wr - xi * wi; |
111 | 8.12k | } |
112 | | |
113 | | /* convolution: IFFT by FFT with real/imag input/output swapped */ |
114 | 26 | { |
115 | 26 | plan_dft *cldf = (plan_dft *)ego->cldf; |
116 | 26 | cldf->apply(ego->cldf, b, b+1, b, b+1); |
117 | 26 | } |
118 | | |
119 | | /* multiply output by conjugate bluestein sequence */ |
120 | 4.01k | for (i = 0; i < n; ++i) { |
121 | 3.99k | E xi = b[2*i], xr = b[2*i+1]; |
122 | 3.99k | E wr = w[2*i], wi = w[2*i+1]; |
123 | 3.99k | ro[i*os] = xr * wr + xi * wi; |
124 | 3.99k | io[i*os] = xi * wr - xr * wi; |
125 | 3.99k | } |
126 | | |
127 | 26 | X(ifree)(b); |
128 | 26 | } |
129 | | |
130 | | static void awake(plan *ego_, enum wakefulness wakefulness) |
131 | 48 | { |
132 | 48 | P *ego = (P *) ego_; |
133 | | |
134 | 48 | X(plan_awake)(ego->cldf, wakefulness); |
135 | | |
136 | 48 | switch (wakefulness) { |
137 | 24 | case SLEEPY: |
138 | 24 | X(ifree0)(ego->w); ego->w = 0; |
139 | 24 | X(ifree0)(ego->W); ego->W = 0; |
140 | 24 | break; |
141 | 24 | default: |
142 | 24 | A(!ego->w); |
143 | 24 | mktwiddle(wakefulness, ego); |
144 | 24 | break; |
145 | 48 | } |
146 | 48 | } |
147 | | |
148 | | static int applicable(const solver *ego, const problem *p_, |
149 | | const planner *plnr) |
150 | 1.11k | { |
151 | 1.11k | const problem_dft *p = (const problem_dft *) p_; |
152 | 1.11k | UNUSED(ego); |
153 | 1.11k | return (1 |
154 | 1.11k | && p->sz->rnk == 1 |
155 | 1.11k | && p->vecsz->rnk == 0 |
156 | | /* FIXME: allow other sizes */ |
157 | 1.11k | && X(is_prime)(p->sz->dims[0].n) |
158 | | |
159 | | /* FIXME: avoid infinite recursion of bluestein with itself. |
160 | | This works because all factors in child problems are 2, 3, 5 */ |
161 | 1.11k | && p->sz->dims[0].n > 16 |
162 | | |
163 | 1.11k | && CIMPLIES(NO_SLOWP(plnr), p->sz->dims[0].n > BLUESTEIN_MAX_SLOW) |
164 | 1.11k | ); |
165 | 1.11k | } |
166 | | |
167 | | static void destroy(plan *ego_) |
168 | 107 | { |
169 | 107 | P *ego = (P *) ego_; |
170 | 107 | X(plan_destroy_internal)(ego->cldf); |
171 | 107 | } |
172 | | |
173 | | static void print(const plan *ego_, printer *p) |
174 | 0 | { |
175 | 0 | const P *ego = (const P *)ego_; |
176 | 0 | p->print(p, "(dft-bluestein-%D/%D%(%p%))", |
177 | 0 | ego->n, ego->nb, ego->cldf); |
178 | 0 | } |
179 | | |
180 | | static INT choose_transform_size(INT minsz) |
181 | 107 | { |
182 | 774 | while (!X(factors_into_small_primes)(minsz)) |
183 | 667 | ++minsz; |
184 | 107 | return minsz; |
185 | 107 | } |
186 | | |
187 | | static plan *mkplan(const solver *ego, const problem *p_, planner *plnr) |
188 | 1.11k | { |
189 | 1.11k | const problem_dft *p = (const problem_dft *) p_; |
190 | 1.11k | P *pln; |
191 | 1.11k | INT n, nb; |
192 | 1.11k | plan *cldf = 0; |
193 | 1.11k | R *buf = (R *) 0; |
194 | | |
195 | 1.11k | static const plan_adt padt = { |
196 | 1.11k | X(dft_solve), awake, print, destroy |
197 | 1.11k | }; |
198 | | |
199 | 1.11k | if (!applicable(ego, p_, plnr)) |
200 | 1.01k | return (plan *) 0; |
201 | | |
202 | 107 | n = p->sz->dims[0].n; |
203 | 107 | nb = choose_transform_size(2 * n - 1); |
204 | 107 | buf = (R *) MALLOC(2 * nb * sizeof(R), BUFFERS); |
205 | | |
206 | 107 | cldf = X(mkplan_f_d)(plnr, |
207 | 107 | X(mkproblem_dft_d)(X(mktensor_1d)(nb, 2, 2), |
208 | 107 | X(mktensor_1d)(1, 0, 0), |
209 | 107 | buf, buf+1, |
210 | 107 | buf, buf+1), |
211 | 107 | NO_SLOW, 0, 0); |
212 | 107 | if (!cldf) goto nada; |
213 | | |
214 | 107 | X(ifree)(buf); |
215 | | |
216 | 107 | pln = MKPLAN_DFT(P, &padt, apply); |
217 | | |
218 | 107 | pln->n = n; |
219 | 107 | pln->nb = nb; |
220 | 107 | pln->w = 0; |
221 | 107 | pln->W = 0; |
222 | 107 | pln->cldf = cldf; |
223 | 107 | pln->is = p->sz->dims[0].is; |
224 | 107 | pln->os = p->sz->dims[0].os; |
225 | | |
226 | 107 | X(ops_add)(&cldf->ops, &cldf->ops, &pln->super.super.ops); |
227 | 107 | pln->super.super.ops.add += 4 * n + 2 * nb; |
228 | 107 | pln->super.super.ops.mul += 8 * n + 4 * nb; |
229 | 107 | pln->super.super.ops.other += 6 * (n + nb); |
230 | | |
231 | 107 | return &(pln->super.super); |
232 | | |
233 | 0 | nada: |
234 | 0 | X(ifree0)(buf); |
235 | 0 | X(plan_destroy_internal)(cldf); |
236 | 0 | return (plan *)0; |
237 | 107 | } |
238 | | |
239 | | |
240 | | static solver *mksolver(void) |
241 | 1 | { |
242 | 1 | static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 }; |
243 | 1 | S *slv = MKSOLVER(S, &sadt); |
244 | 1 | return &(slv->super); |
245 | 1 | } |
246 | | |
247 | | void X(dft_bluestein_register)(planner *p) |
248 | 1 | { |
249 | 1 | REGISTER_SOLVER(p, mksolver()); |
250 | 1 | } |