Coverage Report

Created: 2025-07-23 07:03

/src/fftw3/dft/ct.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
22
#include "dft/ct.h"
23
24
ct_solver *(*X(mksolver_ct_hook))(size_t, INT, int, 
25
          ct_mkinferior, ct_force_vrecursion) = 0;
26
27
typedef struct {
28
     plan_dft super;
29
     plan *cld;
30
     plan *cldw;
31
     INT r;
32
} P;
33
34
static void apply_dit(const plan *ego_, R *ri, R *ii, R *ro, R *io)
35
1.30k
{
36
1.30k
     const P *ego = (const P *) ego_;
37
1.30k
     plan_dft *cld;
38
1.30k
     plan_dftw *cldw;
39
40
1.30k
     cld = (plan_dft *) ego->cld;
41
1.30k
     cld->apply(ego->cld, ri, ii, ro, io);
42
43
1.30k
     cldw = (plan_dftw *) ego->cldw;
44
1.30k
     cldw->apply(ego->cldw, ro, io);
45
1.30k
}
46
47
static void apply_dif(const plan *ego_, R *ri, R *ii, R *ro, R *io)
48
11
{
49
11
     const P *ego = (const P *) ego_;
50
11
     plan_dft *cld;
51
11
     plan_dftw *cldw;
52
53
11
     cldw = (plan_dftw *) ego->cldw;
54
11
     cldw->apply(ego->cldw, ri, ii);
55
56
11
     cld = (plan_dft *) ego->cld;
57
11
     cld->apply(ego->cld, ri, ii, ro, io);
58
11
}
59
60
static void awake(plan *ego_, enum wakefulness wakefulness)
61
704
{
62
704
     P *ego = (P *) ego_;
63
704
     X(plan_awake)(ego->cld, wakefulness);
64
704
     X(plan_awake)(ego->cldw, wakefulness);
65
704
}
66
67
static void destroy(plan *ego_)
68
1.14k
{
69
1.14k
     P *ego = (P *) ego_;
70
1.14k
     X(plan_destroy_internal)(ego->cldw);
71
1.14k
     X(plan_destroy_internal)(ego->cld);
72
1.14k
}
73
74
static void print(const plan *ego_, printer *p)
75
0
{
76
0
     const P *ego = (const P *) ego_;
77
0
     p->print(p, "(dft-ct-%s/%D%(%p%)%(%p%))",
78
0
        ego->super.apply == apply_dit ? "dit" : "dif",
79
0
        ego->r, ego->cldw, ego->cld);
80
0
}
81
82
static int applicable0(const ct_solver *ego, const problem *p_, planner *plnr)
83
132k
{
84
132k
     const problem_dft *p = (const problem_dft *) p_;
85
132k
     INT r;
86
87
132k
     return (1
88
132k
       && p->sz->rnk == 1
89
132k
       && p->vecsz->rnk <= 1
90
91
       /* DIF destroys the input and we don't like it */
92
132k
       && (ego->dec == DECDIT ||
93
84.2k
     p->ri == p->ro ||
94
84.2k
     !NO_DESTROY_INPUTP(plnr))
95
96
132k
       && ((r = X(choose_radix)(ego->r, p->sz->dims[0].n)) > 1)
97
132k
       && p->sz->dims[0].n > r);
98
132k
}
99
100
101
int X(ct_applicable)(const ct_solver *ego, const problem *p_, planner *plnr)
102
132k
{
103
132k
     const problem_dft *p;
104
105
132k
     if (!applicable0(ego, p_, plnr))
106
125k
          return 0;
107
108
6.79k
     p = (const problem_dft *) p_;
109
110
6.79k
     return (0
111
6.79k
       || ego->dec == DECDIF+TRANSPOSE
112
6.79k
       || p->vecsz->rnk == 0
113
6.79k
       || !NO_VRECURSEP(plnr)
114
6.79k
       || (ego->force_vrecursionp && ego->force_vrecursionp(ego, p))
115
6.79k
    );
116
132k
}
117
118
119
static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
120
132k
{
121
132k
     const ct_solver *ego = (const ct_solver *) ego_;
122
132k
     const problem_dft *p;
123
132k
     P *pln = 0;
124
132k
     plan *cld = 0, *cldw = 0;
125
132k
     INT n, r, m, v, ivs, ovs;
126
132k
     iodim *d;
127
128
132k
     static const plan_adt padt = {
129
132k
    X(dft_solve), awake, print, destroy
130
132k
     };
131
132
132k
     if ((NO_NONTHREADEDP(plnr)) || !X(ct_applicable)(ego, p_, plnr))
133
129k
          return (plan *) 0;
134
135
3.32k
     p = (const problem_dft *) p_;
136
3.32k
     d = p->sz->dims;
137
3.32k
     n = d[0].n;
138
3.32k
     r = X(choose_radix)(ego->r, n);
139
3.32k
     m = n / r;
140
141
3.32k
     X(tensor_tornk1)(p->vecsz, &v, &ivs, &ovs);
142
143
3.32k
     switch (ego->dec) {
144
2.18k
   case DECDIT:
145
2.18k
   {
146
2.18k
        cldw = ego->mkcldw(ego,
147
2.18k
         r, m * d[0].os, m * d[0].os,
148
2.18k
         m, d[0].os,
149
2.18k
         v, ovs, ovs,
150
2.18k
         0, m,
151
2.18k
         p->ro, p->io, plnr);
152
2.18k
        if (!cldw) goto nada;
153
154
1.13k
        cld = X(mkplan_d)(plnr,
155
1.13k
        X(mkproblem_dft_d)(
156
1.13k
             X(mktensor_1d)(m, r * d[0].is, d[0].os),
157
1.13k
             X(mktensor_2d)(r, d[0].is, m * d[0].os,
158
1.13k
                v, ivs, ovs),
159
1.13k
             p->ri, p->ii, p->ro, p->io)
160
1.13k
       );
161
1.13k
        if (!cld) goto nada;
162
163
1.13k
        pln = MKPLAN_DFT(P, &padt, apply_dit);
164
1.13k
        break;
165
1.13k
   }
166
28
   case DECDIF:
167
1.13k
   case DECDIF+TRANSPOSE:
168
1.13k
   {
169
1.13k
        INT cors, covs; /* cldw ors, ovs */
170
1.13k
        if (ego->dec == DECDIF+TRANSPOSE) {
171
1.10k
       cors = ivs;
172
1.10k
       covs = m * d[0].is;
173
       /* ensure that we generate well-formed dftw subproblems */
174
       /* FIXME: too conservative */
175
1.10k
       if (!(1
176
1.10k
       && r == v
177
1.10k
       && d[0].is == r * cors))
178
1.04k
      goto nada;
179
180
       /* FIXME: allow in-place only for now, like in
181
          fftw-3.[01] */
182
62
       if (!(1
183
62
       && p->ri == p->ro
184
62
       && d[0].is == r * d[0].os
185
62
       && cors == d[0].os
186
62
       && covs == ovs
187
62
          ))
188
43
      goto nada;
189
62
        } else {
190
28
       cors = m * d[0].is;
191
28
       covs = ivs;
192
28
        }
193
194
47
        cldw = ego->mkcldw(ego,
195
47
         r, m * d[0].is, cors,
196
47
         m, d[0].is,
197
47
         v, ivs, covs,
198
47
         0, m,
199
47
         p->ri, p->ii, plnr);
200
47
        if (!cldw) goto nada;
201
202
19
        cld = X(mkplan_d)(plnr,
203
19
        X(mkproblem_dft_d)(
204
19
             X(mktensor_1d)(m, d[0].is, r * d[0].os),
205
19
             X(mktensor_2d)(r, cors, d[0].os,
206
19
                v, covs, ovs),
207
19
             p->ri, p->ii, p->ro, p->io)
208
19
       );
209
19
        if (!cld) goto nada;
210
211
19
        pln = MKPLAN_DFT(P, &padt, apply_dif);
212
19
        break;
213
19
   }
214
215
0
   default: A(0);
216
217
3.32k
     }
218
219
1.14k
     pln->cld = cld;
220
1.14k
     pln->cldw = cldw;
221
1.14k
     pln->r = r;
222
1.14k
     X(ops_add)(&cld->ops, &cldw->ops, &pln->super.super.ops);
223
224
     /* inherit could_prune_now_p attribute from cldw */
225
1.14k
     pln->super.super.could_prune_now_p = cldw->could_prune_now_p;
226
1.14k
     return &(pln->super.super);
227
228
2.17k
 nada:
229
2.17k
     X(plan_destroy_internal)(cldw);
230
2.17k
     X(plan_destroy_internal)(cld);
231
2.17k
     return (plan *) 0;
232
3.32k
}
233
234
ct_solver *X(mksolver_ct)(size_t size, INT r, int dec, 
235
        ct_mkinferior mkcldw,
236
        ct_force_vrecursion force_vrecursionp)
237
93
{
238
93
     static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 };
239
93
     ct_solver *slv = (ct_solver *)X(mksolver)(size, &sadt);
240
93
     slv->r = r;
241
93
     slv->dec = dec;
242
93
     slv->mkcldw = mkcldw;
243
93
     slv->force_vrecursionp = force_vrecursionp;
244
93
     return slv;
245
93
}
246
247
plan *X(mkplan_dftw)(size_t size, const plan_adt *adt, dftwapply apply)
248
1.15k
{
249
1.15k
     plan_dftw *ego;
250
251
1.15k
     ego = (plan_dftw *) X(mkplan)(size, adt);
252
1.15k
     ego->apply = apply;
253
254
1.15k
     return &(ego->super);
255
1.15k
}