Coverage Report

Created: 2025-08-26 06:35

/src/fftw3/reodft/reodft00e-splitradix.c
Line
Count
Source (jump to first uncovered line)
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/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
0
{
53
0
     const P *ego = (const P *) ego_;
54
0
     INT is = ego->is, os = ego->os;
55
0
     INT i, j, n = ego->n + 1, n2 = (n-1)/2;
56
0
     INT iv, vl = ego->vl;
57
0
     INT ivs = ego->ivs, ovs = ego->ovs;
58
0
     R *W = ego->td->W - 2;
59
0
     R *buf;
60
61
0
     buf = (R *) MALLOC(sizeof(R) * n2, BUFFERS);
62
63
0
     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
0
    for (j = 0, i = 1; i < n; i += 4)
68
0
         buf[j++] = I[is * i];
69
0
    for (i = 2*n-2-i; i > 0; i -= 4)
70
0
         buf[j++] = I[is * i];
71
0
    {
72
0
         plan_rdft *cld = (plan_rdft *) ego->cldo;
73
0
         cld->apply((plan *) cld, buf, buf);
74
0
    }
75
76
    /* do size (n+1)/2 redft00 of the even-indexed elements,
77
       writing to O: */
78
0
    {
79
0
         plan_rdft *cld = (plan_rdft *) ego->clde;
80
0
         cld->apply((plan *) cld, I, O);
81
0
    }
82
83
    /* combine the results with the twiddle factors to get output */
84
0
    { /* DC element */
85
0
         E b20 = O[0], b0 = K(2.0) * buf[0];
86
0
         O[0] = b20 + b0;
87
0
         O[2*(n2*os)] = b20 - b0;
88
         /* O[n2*os] = O[n2*os]; */
89
0
    }
90
0
    for (i = 1; i < n2 - i; ++i) {
91
0
         E ap, am, br, bi, wr, wi, wbr, wbi;
92
0
         br = buf[i];
93
0
         bi = buf[n2 - i];
94
0
         wr = W[2*i];
95
0
         wi = W[2*i+1];
96
0
#if FFT_SIGN == -1
97
0
         wbr = K(2.0) * (wr*br + wi*bi);
98
0
         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
0
         ap = O[i*os];
104
0
         O[i*os] = ap + wbr;
105
0
         O[(2*n2 - i)*os] = ap - wbr;
106
0
         am = O[(n2 - i)*os];
107
0
#if FFT_SIGN == -1
108
0
         O[(n2 - i)*os] = am - wbi;
109
0
         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
0
    }
115
0
    if (i == n2 - i) { /* Nyquist element */
116
0
         E ap, wbr;
117
0
         wbr = K(2.0) * (W[2*i] * buf[i]);
118
0
         ap = O[i*os];
119
0
         O[i*os] = ap + wbr;
120
0
         O[(2*n2 - i)*os] = ap - wbr;
121
0
    }
122
0
     }
123
124
0
     X(ifree)(buf);
125
0
}
126
127
/* rodft00 */
128
static void apply_o(const plan *ego_, R *I, R *O)
129
0
{
130
0
     const P *ego = (const P *) ego_;
131
0
     INT is = ego->is, os = ego->os;
132
0
     INT i, j, n = ego->n - 1, n2 = (n+1)/2;
133
0
     INT iv, vl = ego->vl;
134
0
     INT ivs = ego->ivs, ovs = ego->ovs;
135
0
     R *W = ego->td->W - 2;
136
0
     R *buf;
137
138
0
     buf = (R *) MALLOC(sizeof(R) * n2, BUFFERS);
139
140
0
     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
0
    for (j = 0, i = 0; i < n; i += 4)
145
0
         buf[j++] = I[is * i];
146
0
    for (i = 2*n-i; i > 0; i -= 4)
147
0
         buf[j++] = -I[is * i];
148
0
    {
149
0
         plan_rdft *cld = (plan_rdft *) ego->cldo;
150
0
         cld->apply((plan *) cld, buf, buf);
151
0
    }
152
153
    /* do size (n-1)/2 rodft00 of the odd-indexed elements,
154
       writing to O: */
155
0
    {
156
0
         plan_rdft *cld = (plan_rdft *) ego->clde;
157
0
         if (I == O) {
158
        /* can't use I+is and I, subplan would lose in-placeness */
159
0
        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
0
        A(is >= os);
163
0
        for (i = 0; i < n2-1; ++i)
164
0
       O[os*i] = I[is*(i+1)];
165
0
         }
166
0
         else
167
0
        cld->apply((plan *) cld, I + is, O);
168
0
    }
169
170
    /* combine the results with the twiddle factors to get output */
171
0
    O[(n2-1)*os] = K(2.0) * buf[0];
172
0
    for (i = 1; i < n2 - i; ++i) {
173
0
         E ap, am, br, bi, wr, wi, wbr, wbi;
174
0
         br = buf[i];
175
0
         bi = buf[n2 - i];
176
0
         wr = W[2*i];
177
0
         wi = W[2*i+1];
178
0
#if FFT_SIGN == -1
179
0
         wbr = K(2.0) * (wr*br + wi*bi);
180
0
         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
0
         ap = O[(i-1)*os];
186
0
         O[(i-1)*os] = wbi + ap;
187
0
         O[(2*n2-1 - i)*os] = wbi - ap;
188
0
         am = O[(n2-1 - i)*os];
189
0
#if FFT_SIGN == -1
190
0
         O[(n2-1 - i)*os] = wbr + am;
191
0
         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
0
    }
197
0
    if (i == n2 - i) { /* Nyquist element */
198
0
         E ap, wbi;
199
0
         wbi = K(2.0) * (W[2*i+1] * buf[i]);
200
0
         ap = O[(i-1)*os];
201
0
         O[(i-1)*os] = wbi + ap;
202
0
         O[(2*n2-1 - i)*os] = wbi - ap;
203
0
    }
204
0
     }
205
206
0
     X(ifree)(buf);
207
0
}
208
209
static void awake(plan *ego_, enum wakefulness wakefulness)
210
0
{
211
0
     P *ego = (P *) ego_;
212
0
     static const tw_instr reodft00e_tw[] = {
213
0
          { TW_COS, 1, 1 },
214
0
          { TW_SIN, 1, 1 },
215
0
          { TW_NEXT, 1, 0 }
216
0
     };
217
218
0
     X(plan_awake)(ego->clde, wakefulness);
219
0
     X(plan_awake)(ego->cldo, wakefulness);
220
0
     X(twiddle_awake)(wakefulness, &ego->td, reodft00e_tw, 
221
0
          2*ego->n, 1, ego->n/4);
222
0
}
223
224
static void destroy(plan *ego_)
225
0
{
226
0
     P *ego = (P *) ego_;
227
0
     X(plan_destroy_internal)(ego->cldo);
228
0
     X(plan_destroy_internal)(ego->clde);
229
0
}
230
231
static void print(const plan *ego_, printer *p)
232
0
{
233
0
     const P *ego = (const P *) ego_;
234
0
     if (ego->super.apply == apply_e)
235
0
    p->print(p, "(redft00e-splitradix-%D%v%(%p%)%(%p%))", 
236
0
       ego->n + 1, ego->vl, ego->clde, ego->cldo);
237
0
     else
238
0
    p->print(p, "(rodft00e-splitradix-%D%v%(%p%)%(%p%))", 
239
0
       ego->n - 1, ego->vl, ego->clde, ego->cldo);
240
0
}
241
242
static int applicable0(const solver *ego_, const problem *p_)
243
0
{
244
0
     const problem_rdft *p = (const problem_rdft *) p_;
245
0
     UNUSED(ego_);
246
247
0
     return (1
248
0
       && p->sz->rnk == 1
249
0
       && p->vecsz->rnk <= 1
250
0
       && (p->kind[0] == REDFT00 || p->kind[0] == RODFT00)
251
0
       && p->sz->dims[0].n > 1  /* don't create size-0 sub-plans */
252
0
       && p->sz->dims[0].n % 2  /* odd: 4 divides "logical" DFT */
253
0
       && (p->I != p->O || p->vecsz->rnk == 0
254
0
     || p->vecsz->dims[0].is == p->vecsz->dims[0].os)
255
0
       && (p->kind[0] != RODFT00 || p->I != p->O || 
256
0
     p->sz->dims[0].is >= p->sz->dims[0].os) /* laziness */
257
0
    );
258
0
}
259
260
static int applicable(const solver *ego, const problem *p, const planner *plnr)
261
330
{
262
330
     return (!NO_SLOWP(plnr) && applicable0(ego, p));
263
330
}
264
265
static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
266
330
{
267
330
     P *pln;
268
330
     const problem_rdft *p;
269
330
     plan *clde, *cldo;
270
330
     R *buf;
271
330
     INT n, n0;
272
330
     opcnt ops;
273
330
     int inplace_odd;
274
275
330
     static const plan_adt padt = {
276
330
    X(rdft_solve), awake, print, destroy
277
330
     };
278
279
330
     if (!applicable(ego_, p_, plnr))
280
330
          return (plan *)0;
281
282
0
     p = (const problem_rdft *) p_;
283
284
0
     n = (n0 = p->sz->dims[0].n) + (p->kind[0] == REDFT00 ? (INT)-1 : (INT)1);
285
0
     A(n > 0 && n % 2 == 0);
286
0
     buf = (R *) MALLOC(sizeof(R) * (n/2), BUFFERS);
287
288
0
     inplace_odd = p->kind[0]==RODFT00 && p->I == p->O;
289
0
     clde = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(
290
0
           X(mktensor_1d)(n0-n/2, 2*p->sz->dims[0].is, 
291
0
              inplace_odd ? p->sz->dims[0].is
292
0
              : p->sz->dims[0].os), 
293
0
           X(mktensor_0d)(), 
294
0
           TAINT(p->I 
295
0
           + p->sz->dims[0].is * (p->kind[0]==RODFT00),
296
0
           p->vecsz->rnk ? p->vecsz->dims[0].is : 0),
297
0
           TAINT(p->O
298
0
           + p->sz->dims[0].is * inplace_odd,
299
0
           p->vecsz->rnk ? p->vecsz->dims[0].os : 0),
300
0
           p->kind[0]));
301
0
     if (!clde) {
302
0
    X(ifree)(buf);
303
0
          return (plan *)0;
304
0
     }
305
306
0
     cldo = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(
307
0
           X(mktensor_1d)(n/2, 1, 1), 
308
0
           X(mktensor_0d)(), 
309
0
           buf, buf, R2HC));
310
0
     X(ifree)(buf);
311
0
     if (!cldo)
312
0
          return (plan *)0;
313
314
0
     pln = MKPLAN_RDFT(P, &padt, p->kind[0] == REDFT00 ? apply_e : apply_o);
315
316
0
     pln->n = n;
317
0
     pln->is = p->sz->dims[0].is;
318
0
     pln->os = p->sz->dims[0].os;
319
0
     pln->clde = clde;
320
0
     pln->cldo = cldo;
321
0
     pln->td = 0;
322
323
0
     X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
324
     
325
0
     X(ops_zero)(&ops);
326
0
     ops.other = n/2;
327
0
     ops.add = (p->kind[0]==REDFT00 ? (INT)2 : (INT)0) +
328
0
    (n/2-1)/2 * 6 + ((n/2)%2==0) * 2;
329
0
     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
0
     ops.other += 256;
334
335
0
     X(ops_zero)(&pln->super.super.ops);
336
0
     X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops);
337
0
     X(ops_madd2)(pln->vl, &clde->ops, &pln->super.super.ops);
338
0
     X(ops_madd2)(pln->vl, &cldo->ops, &pln->super.super.ops);
339
340
0
     return &(pln->super.super);
341
0
}
342
343
/* constructor */
344
static solver *mksolver(void)
345
1
{
346
1
     static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
347
1
     S *slv = MKSOLVER(S, &sadt);
348
1
     return &(slv->super);
349
1
}
350
351
void X(reodft00e_splitradix_register)(planner *p)
352
1
{
353
1
     REGISTER_SOLVER(p, mksolver());
354
1
}