/src/fftw3/reodft/rodft00e-r2hc-pad.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 | | /* Do a RODFT00 problem via an R2HC problem, padded antisymmetrically to |
23 | | twice the size. This is asymptotically a factor of ~2 worse than |
24 | | rodft00e-r2hc.c (the algorithm used in e.g. FFTPACK and Numerical |
25 | | Recipes), but we abandoned the latter after we discovered that it |
26 | | has intrinsic accuracy problems. */ |
27 | | |
28 | | #include "reodft/reodft.h" |
29 | | |
30 | | typedef struct { |
31 | | solver super; |
32 | | } S; |
33 | | |
34 | | typedef struct { |
35 | | plan_rdft super; |
36 | | plan *cld, *cldcpy; |
37 | | INT is; |
38 | | INT n; |
39 | | INT vl; |
40 | | INT ivs, ovs; |
41 | | } P; |
42 | | |
43 | | static void apply(const plan *ego_, R *I, R *O) |
44 | 0 | { |
45 | 0 | const P *ego = (const P *) ego_; |
46 | 0 | INT is = ego->is; |
47 | 0 | INT i, n = ego->n; |
48 | 0 | INT iv, vl = ego->vl; |
49 | 0 | INT ivs = ego->ivs, ovs = ego->ovs; |
50 | 0 | R *buf; |
51 | |
|
52 | 0 | buf = (R *) MALLOC(sizeof(R) * (2*n), BUFFERS); |
53 | |
|
54 | 0 | for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { |
55 | 0 | buf[0] = K(0.0); |
56 | 0 | for (i = 1; i < n; ++i) { |
57 | 0 | R a = I[(i-1) * is]; |
58 | 0 | buf[i] = -a; |
59 | 0 | buf[2*n - i] = a; |
60 | 0 | } |
61 | 0 | buf[i] = K(0.0); /* i == n, Nyquist */ |
62 | | |
63 | | /* r2hc transform of size 2*n */ |
64 | 0 | { |
65 | 0 | plan_rdft *cld = (plan_rdft *) ego->cld; |
66 | 0 | cld->apply((plan *) cld, buf, buf); |
67 | 0 | } |
68 | | |
69 | | /* copy n-1 real numbers (imag. parts of hc array) from buf to O */ |
70 | 0 | { |
71 | 0 | plan_rdft *cldcpy = (plan_rdft *) ego->cldcpy; |
72 | 0 | cldcpy->apply((plan *) cldcpy, buf+2*n-1, O); |
73 | 0 | } |
74 | 0 | } |
75 | |
|
76 | 0 | X(ifree)(buf); |
77 | 0 | } |
78 | | |
79 | | static void awake(plan *ego_, enum wakefulness wakefulness) |
80 | 0 | { |
81 | 0 | P *ego = (P *) ego_; |
82 | 0 | X(plan_awake)(ego->cld, wakefulness); |
83 | 0 | X(plan_awake)(ego->cldcpy, wakefulness); |
84 | 0 | } |
85 | | |
86 | | static void destroy(plan *ego_) |
87 | 0 | { |
88 | 0 | P *ego = (P *) ego_; |
89 | 0 | X(plan_destroy_internal)(ego->cldcpy); |
90 | 0 | X(plan_destroy_internal)(ego->cld); |
91 | 0 | } |
92 | | |
93 | | static void print(const plan *ego_, printer *p) |
94 | 0 | { |
95 | 0 | const P *ego = (const P *) ego_; |
96 | 0 | p->print(p, "(rodft00e-r2hc-pad-%D%v%(%p%)%(%p%))", |
97 | 0 | ego->n - 1, ego->vl, ego->cld, ego->cldcpy); |
98 | 0 | } |
99 | | |
100 | | static int applicable0(const solver *ego_, const problem *p_) |
101 | 0 | { |
102 | 0 | const problem_rdft *p = (const problem_rdft *) p_; |
103 | 0 | UNUSED(ego_); |
104 | 0 | return (1 |
105 | 0 | && p->sz->rnk == 1 |
106 | 0 | && p->vecsz->rnk <= 1 |
107 | 0 | && p->kind[0] == RODFT00 |
108 | 0 | ); |
109 | 0 | } |
110 | | |
111 | | static int applicable(const solver *ego, const problem *p, const planner *plnr) |
112 | 327 | { |
113 | 327 | return (!NO_SLOWP(plnr) && applicable0(ego, p)); |
114 | 327 | } |
115 | | |
116 | | static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) |
117 | 327 | { |
118 | 327 | P *pln; |
119 | 327 | const problem_rdft *p; |
120 | 327 | plan *cld = (plan *) 0, *cldcpy; |
121 | 327 | R *buf = (R *) 0; |
122 | 327 | INT n; |
123 | 327 | INT vl, ivs, ovs; |
124 | 327 | opcnt ops; |
125 | | |
126 | 327 | static const plan_adt padt = { |
127 | 327 | X(rdft_solve), awake, print, destroy |
128 | 327 | }; |
129 | | |
130 | 327 | if (!applicable(ego_, p_, plnr)) |
131 | 327 | goto nada; |
132 | | |
133 | 0 | p = (const problem_rdft *) p_; |
134 | |
|
135 | 0 | n = p->sz->dims[0].n + 1; |
136 | 0 | A(n > 0); |
137 | 0 | buf = (R *) MALLOC(sizeof(R) * (2*n), BUFFERS); |
138 | |
|
139 | 0 | cld = X(mkplan_d)(plnr,X(mkproblem_rdft_1_d)(X(mktensor_1d)(2*n,1,1), |
140 | 0 | X(mktensor_0d)(), |
141 | 0 | buf, buf, R2HC)); |
142 | 0 | if (!cld) |
143 | 0 | goto nada; |
144 | | |
145 | 0 | X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); |
146 | 0 | cldcpy = |
147 | 0 | X(mkplan_d)(plnr, |
148 | 0 | X(mkproblem_rdft_1_d)(X(mktensor_0d)(), |
149 | 0 | X(mktensor_1d)(n-1,-1, |
150 | 0 | p->sz->dims[0].os), |
151 | 0 | buf+2*n-1,TAINT(p->O, ovs), R2HC)); |
152 | 0 | if (!cldcpy) |
153 | 0 | goto nada; |
154 | | |
155 | 0 | X(ifree)(buf); |
156 | |
|
157 | 0 | pln = MKPLAN_RDFT(P, &padt, apply); |
158 | |
|
159 | 0 | pln->n = n; |
160 | 0 | pln->is = p->sz->dims[0].is; |
161 | 0 | pln->cld = cld; |
162 | 0 | pln->cldcpy = cldcpy; |
163 | 0 | pln->vl = vl; |
164 | 0 | pln->ivs = ivs; |
165 | 0 | pln->ovs = ovs; |
166 | | |
167 | 0 | X(ops_zero)(&ops); |
168 | 0 | ops.other = n-1 + 2*n; /* loads + stores (input -> buf) */ |
169 | |
|
170 | 0 | X(ops_zero)(&pln->super.super.ops); |
171 | 0 | X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); |
172 | 0 | X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); |
173 | 0 | X(ops_madd2)(pln->vl, &cldcpy->ops, &pln->super.super.ops); |
174 | |
|
175 | 0 | return &(pln->super.super); |
176 | | |
177 | 327 | nada: |
178 | 327 | X(ifree0)(buf); |
179 | 327 | if (cld) |
180 | 0 | X(plan_destroy_internal)(cld); |
181 | 327 | return (plan *)0; |
182 | 0 | } |
183 | | |
184 | | /* constructor */ |
185 | | static solver *mksolver(void) |
186 | 1 | { |
187 | 1 | static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; |
188 | 1 | S *slv = MKSOLVER(S, &sadt); |
189 | 1 | return &(slv->super); |
190 | 1 | } |
191 | | |
192 | | void X(rodft00e_r2hc_pad_register)(planner *p) |
193 | 1 | { |
194 | 1 | REGISTER_SOLVER(p, mksolver()); |
195 | 1 | } |