/src/fftw3/rdft/ct-hc2c.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 "ct-hc2c.h" |
22 | | #include "dft/dft.h" |
23 | | |
24 | | typedef struct { |
25 | | plan_rdft2 super; |
26 | | plan *cld; |
27 | | plan *cldw; |
28 | | INT r; |
29 | | } P; |
30 | | |
31 | | static void apply_dit(const plan *ego_, R *r0, R *r1, R *cr, R *ci) |
32 | 0 | { |
33 | 0 | const P *ego = (const P *) ego_; |
34 | 0 | plan_rdft *cld; |
35 | 0 | plan_hc2c *cldw; |
36 | 0 | UNUSED(r1); |
37 | |
|
38 | 0 | cld = (plan_rdft *) ego->cld; |
39 | 0 | cld->apply(ego->cld, r0, cr); |
40 | |
|
41 | 0 | cldw = (plan_hc2c *) ego->cldw; |
42 | 0 | cldw->apply(ego->cldw, cr, ci); |
43 | 0 | } |
44 | | |
45 | | static void apply_dif(const plan *ego_, R *r0, R *r1, R *cr, R *ci) |
46 | 0 | { |
47 | 0 | const P *ego = (const P *) ego_; |
48 | 0 | plan_rdft *cld; |
49 | 0 | plan_hc2c *cldw; |
50 | 0 | UNUSED(r1); |
51 | |
|
52 | 0 | cldw = (plan_hc2c *) ego->cldw; |
53 | 0 | cldw->apply(ego->cldw, cr, ci); |
54 | |
|
55 | 0 | cld = (plan_rdft *) ego->cld; |
56 | 0 | cld->apply(ego->cld, cr, r0); |
57 | 0 | } |
58 | | |
59 | | static void apply_dit_dft(const plan *ego_, R *r0, R *r1, R *cr, R *ci) |
60 | 0 | { |
61 | 0 | const P *ego = (const P *) ego_; |
62 | 0 | plan_dft *cld; |
63 | 0 | plan_hc2c *cldw; |
64 | |
|
65 | 0 | cld = (plan_dft *) ego->cld; |
66 | 0 | cld->apply(ego->cld, r0, r1, cr, ci); |
67 | |
|
68 | 0 | cldw = (plan_hc2c *) ego->cldw; |
69 | 0 | cldw->apply(ego->cldw, cr, ci); |
70 | 0 | } |
71 | | |
72 | | static void apply_dif_dft(const plan *ego_, R *r0, R *r1, R *cr, R *ci) |
73 | 0 | { |
74 | 0 | const P *ego = (const P *) ego_; |
75 | 0 | plan_dft *cld; |
76 | 0 | plan_hc2c *cldw; |
77 | |
|
78 | 0 | cldw = (plan_hc2c *) ego->cldw; |
79 | 0 | cldw->apply(ego->cldw, cr, ci); |
80 | |
|
81 | 0 | cld = (plan_dft *) ego->cld; |
82 | 0 | cld->apply(ego->cld, ci, cr, r1, r0); |
83 | 0 | } |
84 | | |
85 | | static void awake(plan *ego_, enum wakefulness wakefulness) |
86 | 0 | { |
87 | 0 | P *ego = (P *) ego_; |
88 | 0 | X(plan_awake)(ego->cld, wakefulness); |
89 | 0 | X(plan_awake)(ego->cldw, wakefulness); |
90 | 0 | } |
91 | | |
92 | | static void destroy(plan *ego_) |
93 | 0 | { |
94 | 0 | P *ego = (P *) ego_; |
95 | 0 | X(plan_destroy_internal)(ego->cldw); |
96 | 0 | X(plan_destroy_internal)(ego->cld); |
97 | 0 | } |
98 | | |
99 | | static void print(const plan *ego_, printer *p) |
100 | 0 | { |
101 | 0 | const P *ego = (const P *) ego_; |
102 | 0 | p->print(p, "(rdft2-ct-%s/%D%(%p%)%(%p%))", |
103 | 0 | (ego->super.apply == apply_dit || |
104 | 0 | ego->super.apply == apply_dit_dft) |
105 | 0 | ? "dit" : "dif", |
106 | 0 | ego->r, ego->cldw, ego->cld); |
107 | 0 | } |
108 | | |
109 | | static int applicable0(const hc2c_solver *ego, const problem *p_, planner *plnr) |
110 | 0 | { |
111 | 0 | const problem_rdft2 *p = (const problem_rdft2 *) p_; |
112 | 0 | INT r; |
113 | |
|
114 | 0 | return (1 |
115 | 0 | && p->sz->rnk == 1 |
116 | 0 | && p->vecsz->rnk <= 1 |
117 | | |
118 | 0 | && (/* either the problem is R2HC, which is solved by DIT */ |
119 | 0 | (p->kind == R2HC) |
120 | 0 | || |
121 | | /* or the problem is HC2R, in which case it is solved |
122 | | by DIF, which destroys the input */ |
123 | 0 | (p->kind == HC2R && |
124 | 0 | (p->r0 == p->cr || !NO_DESTROY_INPUTP(plnr)))) |
125 | | |
126 | 0 | && ((r = X(choose_radix)(ego->r, p->sz->dims[0].n)) > 0) |
127 | 0 | && p->sz->dims[0].n > r); |
128 | 0 | } |
129 | | |
130 | | static int hc2c_applicable(const hc2c_solver *ego, const problem *p_, |
131 | | planner *plnr) |
132 | 0 | { |
133 | 0 | const problem_rdft2 *p; |
134 | |
|
135 | 0 | if (!applicable0(ego, p_, plnr)) |
136 | 0 | return 0; |
137 | | |
138 | 0 | p = (const problem_rdft2 *) p_; |
139 | |
|
140 | 0 | return (0 |
141 | 0 | || p->vecsz->rnk == 0 |
142 | 0 | || !NO_VRECURSEP(plnr) |
143 | 0 | ); |
144 | 0 | } |
145 | | |
146 | | static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) |
147 | 0 | { |
148 | 0 | const hc2c_solver *ego = (const hc2c_solver *) ego_; |
149 | 0 | const problem_rdft2 *p; |
150 | 0 | P *pln = 0; |
151 | 0 | plan *cld = 0, *cldw = 0; |
152 | 0 | INT n, r, m, v, ivs, ovs; |
153 | 0 | iodim *d; |
154 | |
|
155 | 0 | static const plan_adt padt = { |
156 | 0 | X(rdft2_solve), awake, print, destroy |
157 | 0 | }; |
158 | |
|
159 | 0 | if (!hc2c_applicable(ego, p_, plnr)) |
160 | 0 | return (plan *) 0; |
161 | | |
162 | 0 | p = (const problem_rdft2 *) p_; |
163 | 0 | d = p->sz->dims; |
164 | 0 | n = d[0].n; |
165 | 0 | r = X(choose_radix)(ego->r, n); |
166 | 0 | A((r % 2) == 0); |
167 | 0 | m = n / r; |
168 | |
|
169 | 0 | X(tensor_tornk1)(p->vecsz, &v, &ivs, &ovs); |
170 | |
|
171 | 0 | switch (p->kind) { |
172 | 0 | case R2HC: |
173 | 0 | cldw = ego->mkcldw(ego, R2HC, |
174 | 0 | r, m * d[0].os, |
175 | 0 | m, d[0].os, |
176 | 0 | v, ovs, |
177 | 0 | p->cr, p->ci, plnr); |
178 | 0 | if (!cldw) goto nada; |
179 | | |
180 | 0 | switch (ego->hc2ckind) { |
181 | 0 | case HC2C_VIA_RDFT: |
182 | 0 | cld = X(mkplan_d)( |
183 | 0 | plnr, |
184 | 0 | X(mkproblem_rdft_1_d)( |
185 | 0 | X(mktensor_1d)(m, (r/2)*d[0].is, d[0].os), |
186 | 0 | X(mktensor_3d)( |
187 | 0 | 2, p->r1 - p->r0, p->ci - p->cr, |
188 | 0 | r / 2, d[0].is, m * d[0].os, |
189 | 0 | v, ivs, ovs), |
190 | 0 | p->r0, p->cr, R2HC) |
191 | 0 | ); |
192 | 0 | if (!cld) goto nada; |
193 | | |
194 | 0 | pln = MKPLAN_RDFT2(P, &padt, apply_dit); |
195 | 0 | break; |
196 | | |
197 | 0 | case HC2C_VIA_DFT: |
198 | 0 | cld = X(mkplan_d)( |
199 | 0 | plnr, |
200 | 0 | X(mkproblem_dft_d)( |
201 | 0 | X(mktensor_1d)(m, (r/2)*d[0].is, d[0].os), |
202 | 0 | X(mktensor_2d)( |
203 | 0 | r / 2, d[0].is, m * d[0].os, |
204 | 0 | v, ivs, ovs), |
205 | 0 | p->r0, p->r1, p->cr, p->ci) |
206 | 0 | ); |
207 | 0 | if (!cld) goto nada; |
208 | | |
209 | 0 | pln = MKPLAN_RDFT2(P, &padt, apply_dit_dft); |
210 | 0 | break; |
211 | 0 | } |
212 | 0 | break; |
213 | | |
214 | 0 | case HC2R: |
215 | 0 | cldw = ego->mkcldw(ego, HC2R, |
216 | 0 | r, m * d[0].is, |
217 | 0 | m, d[0].is, |
218 | 0 | v, ivs, |
219 | 0 | p->cr, p->ci, plnr); |
220 | 0 | if (!cldw) goto nada; |
221 | | |
222 | 0 | switch (ego->hc2ckind) { |
223 | 0 | case HC2C_VIA_RDFT: |
224 | 0 | cld = X(mkplan_d)( |
225 | 0 | plnr, |
226 | 0 | X(mkproblem_rdft_1_d)( |
227 | 0 | X(mktensor_1d)(m, d[0].is, (r/2)*d[0].os), |
228 | 0 | X(mktensor_3d)( |
229 | 0 | 2, p->ci - p->cr, p->r1 - p->r0, |
230 | 0 | r / 2, m * d[0].is, d[0].os, |
231 | 0 | v, ivs, ovs), |
232 | 0 | p->cr, p->r0, HC2R) |
233 | 0 | ); |
234 | 0 | if (!cld) goto nada; |
235 | | |
236 | 0 | pln = MKPLAN_RDFT2(P, &padt, apply_dif); |
237 | 0 | break; |
238 | | |
239 | 0 | case HC2C_VIA_DFT: |
240 | 0 | cld = X(mkplan_d)( |
241 | 0 | plnr, |
242 | 0 | X(mkproblem_dft_d)( |
243 | 0 | X(mktensor_1d)(m, d[0].is, (r/2)*d[0].os), |
244 | 0 | X(mktensor_2d)( |
245 | 0 | r / 2, m * d[0].is, d[0].os, |
246 | 0 | v, ivs, ovs), |
247 | 0 | p->ci, p->cr, p->r1, p->r0) |
248 | 0 | ); |
249 | 0 | if (!cld) goto nada; |
250 | | |
251 | 0 | pln = MKPLAN_RDFT2(P, &padt, apply_dif_dft); |
252 | 0 | break; |
253 | 0 | } |
254 | 0 | break; |
255 | | |
256 | 0 | default: |
257 | 0 | A(0); |
258 | 0 | } |
259 | | |
260 | 0 | pln->cld = cld; |
261 | 0 | pln->cldw = cldw; |
262 | 0 | pln->r = r; |
263 | 0 | X(ops_add)(&cld->ops, &cldw->ops, &pln->super.super.ops); |
264 | | |
265 | | /* inherit could_prune_now_p attribute from cldw */ |
266 | 0 | pln->super.super.could_prune_now_p = cldw->could_prune_now_p; |
267 | |
|
268 | 0 | return &(pln->super.super); |
269 | | |
270 | 0 | nada: |
271 | 0 | X(plan_destroy_internal)(cldw); |
272 | 0 | X(plan_destroy_internal)(cld); |
273 | 0 | return (plan *) 0; |
274 | 0 | } |
275 | | |
276 | | hc2c_solver *X(mksolver_hc2c)(size_t size, INT r, |
277 | | hc2c_kind hc2ckind, |
278 | | hc2c_mkinferior mkcldw) |
279 | 112 | { |
280 | 112 | static const solver_adt sadt = { PROBLEM_RDFT2, mkplan, 0 }; |
281 | 112 | hc2c_solver *slv = (hc2c_solver *)X(mksolver)(size, &sadt); |
282 | 112 | slv->r = r; |
283 | 112 | slv->hc2ckind = hc2ckind; |
284 | 112 | slv->mkcldw = mkcldw; |
285 | 112 | return slv; |
286 | 112 | } |
287 | | |
288 | | plan *X(mkplan_hc2c)(size_t size, const plan_adt *adt, hc2capply apply) |
289 | 0 | { |
290 | 0 | plan_hc2c *ego; |
291 | |
|
292 | 0 | ego = (plan_hc2c *) X(mkplan)(size, adt); |
293 | 0 | ego->apply = apply; |
294 | |
|
295 | 0 | return &(ego->super); |
296 | 0 | } |