Coverage Report

Created: 2025-08-03 06:50

/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
22
{
39
22
     INT k, ksq, n2 = 2 * n;
40
22
     triggen *t = X(mktriggen)(wakefulness, n2);
41
42
22
     ksq = 0;
43
3.55k
     for (k = 0; k < n; ++k) {
44
3.53k
    t->cexp(t, ksq, w+2*k);
45
          /* careful with overflow */
46
5.28k
          ksq += 2*k + 1; while (ksq > n2) ksq -= n2;
47
3.53k
     }
48
49
22
     X(triggen_destroy)(t);
50
22
}
51
52
static void mktwiddle(enum wakefulness wakefulness, P *p)
53
22
{
54
22
     INT i;
55
22
     INT n = p->n, nb = p->nb;
56
22
     R *w, *W;
57
22
     E nbf = (E)nb;
58
59
22
     p->w = w = (R *) MALLOC(2 * n * sizeof(R), TWIDDLES);
60
22
     p->W = W = (R *) MALLOC(2 * nb * sizeof(R), TWIDDLES);
61
62
22
     bluestein_sequence(wakefulness, n, w);
63
64
7.22k
     for (i = 0; i < nb; ++i)
65
7.20k
          W[2*i] = W[2*i+1] = K(0.0);
66
67
22
     W[0] = w[0] / nbf;
68
22
     W[1] = w[1] / nbf;
69
70
3.53k
     for (i = 1; i < n; ++i) {
71
3.51k
          W[2*i] = W[2*(nb-i)] = w[2*i] / nbf;
72
3.51k
          W[2*i+1] = W[2*(nb-i)+1] = w[2*i+1] / nbf;
73
3.51k
     }
74
75
22
     {
76
22
          plan_dft *cldf = (plan_dft *)p->cldf;
77
    /* cldf must be awake */
78
22
          cldf->apply(p->cldf, W, W+1, W, W+1);
79
22
     }
80
22
}
81
82
static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io)
83
24
{
84
24
     const P *ego = (const P *) ego_;
85
24
     INT i, n = ego->n, nb = ego->nb, is = ego->is, os = ego->os;
86
24
     R *w = ego->w, *W = ego->W;
87
24
     R *b = (R *) MALLOC(2 * nb * sizeof(R), BUFFERS);
88
89
     /* multiply input by conjugate bluestein sequence */
90
3.77k
     for (i = 0; i < n; ++i) {
91
3.75k
    E xr = ri[i*is], xi = ii[i*is];
92
3.75k
          E wr = w[2*i], wi = w[2*i+1];
93
3.75k
          b[2*i] = xr * wr + xi * wi;
94
3.75k
          b[2*i+1] = xi * wr - xr * wi;
95
3.75k
     }
96
97
3.92k
     for (; i < nb; ++i) b[2*i] = b[2*i+1] = K(0.0);
98
99
     /* convolution: FFT */
100
24
     {
101
24
          plan_dft *cldf = (plan_dft *)ego->cldf;
102
24
          cldf->apply(ego->cldf, b, b+1, b, b+1);
103
24
     }
104
105
     /* convolution: pointwise multiplication */
106
7.67k
     for (i = 0; i < nb; ++i) {
107
7.64k
    E xr = b[2*i], xi = b[2*i+1];
108
7.64k
          E wr = W[2*i], wi = W[2*i+1];
109
7.64k
          b[2*i] = xi * wr + xr * wi;
110
7.64k
          b[2*i+1] = xr * wr - xi * wi;
111
7.64k
     }
112
113
     /* convolution: IFFT by FFT with real/imag input/output swapped */
114
24
     {
115
24
          plan_dft *cldf = (plan_dft *)ego->cldf;
116
24
          cldf->apply(ego->cldf, b, b+1, b, b+1);
117
24
     }
118
119
     /* multiply output by conjugate bluestein sequence */
120
3.77k
     for (i = 0; i < n; ++i) {
121
3.75k
    E xi = b[2*i], xr = b[2*i+1];
122
3.75k
          E wr = w[2*i], wi = w[2*i+1];
123
3.75k
          ro[i*os] = xr * wr + xi * wi;
124
3.75k
          io[i*os] = xi * wr - xr * wi;
125
3.75k
     }
126
127
24
     X(ifree)(b);   
128
24
}
129
130
static void awake(plan *ego_, enum wakefulness wakefulness)
131
44
{
132
44
     P *ego = (P *) ego_;
133
134
44
     X(plan_awake)(ego->cldf, wakefulness);
135
136
44
     switch (wakefulness) {
137
22
   case SLEEPY:
138
22
        X(ifree0)(ego->w); ego->w = 0;
139
22
        X(ifree0)(ego->W); ego->W = 0;
140
22
        break;
141
22
   default:
142
22
        A(!ego->w);
143
22
        mktwiddle(wakefulness, ego);
144
22
        break;
145
44
     }
146
44
}
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
103
{
169
103
     P *ego = (P *) ego_;
170
103
     X(plan_destroy_internal)(ego->cldf);
171
103
}
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
103
{
182
753
     while (!X(factors_into_small_primes)(minsz))
183
650
    ++minsz;
184
103
     return minsz;
185
103
}
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
103
     n = p->sz->dims[0].n;
203
103
     nb = choose_transform_size(2 * n - 1);
204
103
     buf = (R *) MALLOC(2 * nb * sizeof(R), BUFFERS);
205
206
103
     cldf = X(mkplan_f_d)(plnr, 
207
103
        X(mkproblem_dft_d)(X(mktensor_1d)(nb, 2, 2),
208
103
               X(mktensor_1d)(1, 0, 0),
209
103
               buf, buf+1, 
210
103
               buf, buf+1),
211
103
        NO_SLOW, 0, 0);
212
103
     if (!cldf) goto nada;
213
214
103
     X(ifree)(buf);
215
216
103
     pln = MKPLAN_DFT(P, &padt, apply);
217
218
103
     pln->n = n;
219
103
     pln->nb = nb;
220
103
     pln->w = 0;
221
103
     pln->W = 0;
222
103
     pln->cldf = cldf;
223
103
     pln->is = p->sz->dims[0].is;
224
103
     pln->os = p->sz->dims[0].os;
225
226
103
     X(ops_add)(&cldf->ops, &cldf->ops, &pln->super.super.ops);
227
103
     pln->super.super.ops.add += 4 * n + 2 * nb;
228
103
     pln->super.super.ops.mul += 8 * n + 4 * nb;
229
103
     pln->super.super.ops.other += 6 * (n + nb);
230
231
103
     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
103
}
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
}