Coverage Report

Created: 2025-11-16 06:54

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