Mercurial > hg > sv-dependency-builds
comparison src/fftw-3.3.3/rdft/rdft2-rdft.c @ 10:37bf6b4a2645
Add FFTW3
author | Chris Cannam |
---|---|
date | Wed, 20 Mar 2013 15:35:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:c0fb53affa76 | 10:37bf6b4a2645 |
---|---|
1 /* | |
2 * Copyright (c) 2003, 2007-11 Matteo Frigo | |
3 * Copyright (c) 2003, 2007-11 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 "rdft.h" | |
23 | |
24 typedef struct { | |
25 solver super; | |
26 } S; | |
27 | |
28 typedef struct { | |
29 plan_rdft2 super; | |
30 | |
31 plan *cld, *cldrest; | |
32 INT n, vl, nbuf, bufdist; | |
33 INT cs, ivs, ovs; | |
34 } P; | |
35 | |
36 /***************************************************************************/ | |
37 | |
38 /* FIXME: have alternate copy functions that push a vector loop inside | |
39 the n loops? */ | |
40 | |
41 /* copy halfcomplex array r (contiguous) to complex (strided) array rio/iio. */ | |
42 static void hc2c(INT n, R *r, R *rio, R *iio, INT os) | |
43 { | |
44 INT i; | |
45 | |
46 rio[0] = r[0]; | |
47 iio[0] = 0; | |
48 | |
49 for (i = 1; i + i < n; ++i) { | |
50 rio[i * os] = r[i]; | |
51 iio[i * os] = r[n - i]; | |
52 } | |
53 | |
54 if (i + i == n) { /* store the Nyquist frequency */ | |
55 rio[i * os] = r[i]; | |
56 iio[i * os] = K(0.0); | |
57 } | |
58 } | |
59 | |
60 /* reverse of hc2c */ | |
61 static void c2hc(INT n, R *rio, R *iio, INT is, R *r) | |
62 { | |
63 INT i; | |
64 | |
65 r[0] = rio[0]; | |
66 | |
67 for (i = 1; i + i < n; ++i) { | |
68 r[i] = rio[i * is]; | |
69 r[n - i] = iio[i * is]; | |
70 } | |
71 | |
72 if (i + i == n) /* store the Nyquist frequency */ | |
73 r[i] = rio[i * is]; | |
74 } | |
75 | |
76 /***************************************************************************/ | |
77 | |
78 static void apply_r2hc(const plan *ego_, R *r0, R *r1, R *cr, R *ci) | |
79 { | |
80 const P *ego = (const P *) ego_; | |
81 plan_rdft *cld = (plan_rdft *) ego->cld; | |
82 INT i, j, vl = ego->vl, nbuf = ego->nbuf, bufdist = ego->bufdist; | |
83 INT n = ego->n; | |
84 INT ivs = ego->ivs, ovs = ego->ovs, os = ego->cs; | |
85 R *bufs = (R *)MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS); | |
86 plan_rdft2 *cldrest; | |
87 | |
88 for (i = nbuf; i <= vl; i += nbuf) { | |
89 /* transform to bufs: */ | |
90 cld->apply((plan *) cld, r0, bufs); | |
91 r0 += ivs * nbuf; r1 += ivs * nbuf; | |
92 | |
93 /* copy back */ | |
94 for (j = 0; j < nbuf; ++j, cr += ovs, ci += ovs) | |
95 hc2c(n, bufs + j*bufdist, cr, ci, os); | |
96 } | |
97 | |
98 X(ifree)(bufs); | |
99 | |
100 /* Do the remaining transforms, if any: */ | |
101 cldrest = (plan_rdft2 *) ego->cldrest; | |
102 cldrest->apply((plan *) cldrest, r0, r1, cr, ci); | |
103 } | |
104 | |
105 static void apply_hc2r(const plan *ego_, R *r0, R *r1, R *cr, R *ci) | |
106 { | |
107 const P *ego = (const P *) ego_; | |
108 plan_rdft *cld = (plan_rdft *) ego->cld; | |
109 INT i, j, vl = ego->vl, nbuf = ego->nbuf, bufdist = ego->bufdist; | |
110 INT n = ego->n; | |
111 INT ivs = ego->ivs, ovs = ego->ovs, is = ego->cs; | |
112 R *bufs = (R *)MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS); | |
113 plan_rdft2 *cldrest; | |
114 | |
115 for (i = nbuf; i <= vl; i += nbuf) { | |
116 /* copy to bufs */ | |
117 for (j = 0; j < nbuf; ++j, cr += ivs, ci += ivs) | |
118 c2hc(n, cr, ci, is, bufs + j*bufdist); | |
119 | |
120 /* transform back: */ | |
121 cld->apply((plan *) cld, bufs, r0); | |
122 r0 += ovs * nbuf; r1 += ovs * nbuf; | |
123 } | |
124 | |
125 X(ifree)(bufs); | |
126 | |
127 /* Do the remaining transforms, if any: */ | |
128 cldrest = (plan_rdft2 *) ego->cldrest; | |
129 cldrest->apply((plan *) cldrest, r0, r1, cr, ci); | |
130 } | |
131 | |
132 static void awake(plan *ego_, enum wakefulness wakefulness) | |
133 { | |
134 P *ego = (P *) ego_; | |
135 | |
136 X(plan_awake)(ego->cld, wakefulness); | |
137 X(plan_awake)(ego->cldrest, wakefulness); | |
138 } | |
139 | |
140 static void destroy(plan *ego_) | |
141 { | |
142 P *ego = (P *) ego_; | |
143 X(plan_destroy_internal)(ego->cldrest); | |
144 X(plan_destroy_internal)(ego->cld); | |
145 } | |
146 | |
147 static void print(const plan *ego_, printer *p) | |
148 { | |
149 const P *ego = (const P *) ego_; | |
150 p->print(p, "(rdft2-rdft-%s-%D%v/%D-%D%(%p%)%(%p%))", | |
151 ego->super.apply == apply_r2hc ? "r2hc" : "hc2r", | |
152 ego->n, ego->nbuf, | |
153 ego->vl, ego->bufdist % ego->n, | |
154 ego->cld, ego->cldrest); | |
155 } | |
156 | |
157 static INT min_nbuf(const problem_rdft2 *p, INT n, INT vl) | |
158 { | |
159 INT is, os, ivs, ovs; | |
160 | |
161 if (p->r0 != p->cr) | |
162 return 1; | |
163 if (X(rdft2_inplace_strides(p, RNK_MINFTY))) | |
164 return 1; | |
165 A(p->vecsz->rnk == 1); /* rank 0 and MINFTY are inplace */ | |
166 | |
167 X(rdft2_strides)(p->kind, p->sz->dims, &is, &os); | |
168 X(rdft2_strides)(p->kind, p->vecsz->dims, &ivs, &ovs); | |
169 | |
170 /* handle one potentially common case: "contiguous" real and | |
171 complex arrays, which overlap because of the differing sizes. */ | |
172 if (n * X(iabs)(is) <= X(iabs)(ivs) | |
173 && (n/2 + 1) * X(iabs)(os) <= X(iabs)(ovs) | |
174 && ( ((p->cr - p->ci) <= X(iabs)(os)) || | |
175 ((p->ci - p->cr) <= X(iabs)(os)) ) | |
176 && ivs > 0 && ovs > 0) { | |
177 INT vsmin = X(imin)(ivs, ovs); | |
178 INT vsmax = X(imax)(ivs, ovs); | |
179 return(((vsmax - vsmin) * vl + vsmin - 1) / vsmin); | |
180 } | |
181 | |
182 return vl; /* punt: just buffer the whole vector */ | |
183 } | |
184 | |
185 static int applicable0(const problem *p_, const S *ego, const planner *plnr) | |
186 { | |
187 const problem_rdft2 *p = (const problem_rdft2 *) p_; | |
188 UNUSED(ego); | |
189 return(1 | |
190 && p->vecsz->rnk <= 1 | |
191 && p->sz->rnk == 1 | |
192 | |
193 /* FIXME: does it make sense to do R2HCII ? */ | |
194 && (p->kind == R2HC || p->kind == HC2R) | |
195 | |
196 /* real strides must allow for reduction to rdft */ | |
197 && (2 * (p->r1 - p->r0) == | |
198 (((p->kind == R2HC) ? p->sz->dims[0].is : p->sz->dims[0].os))) | |
199 | |
200 && !(X(toobig)(p->sz->dims[0].n) && CONSERVE_MEMORYP(plnr)) | |
201 ); | |
202 } | |
203 | |
204 static int applicable(const problem *p_, const S *ego, const planner *plnr) | |
205 { | |
206 const problem_rdft2 *p; | |
207 | |
208 if (NO_BUFFERINGP(plnr)) return 0; | |
209 | |
210 if (!applicable0(p_, ego, plnr)) return 0; | |
211 | |
212 p = (const problem_rdft2 *) p_; | |
213 if (NO_UGLYP(plnr)) { | |
214 if (p->r0 != p->cr) return 0; | |
215 if (X(toobig)(p->sz->dims[0].n)) return 0; | |
216 } | |
217 return 1; | |
218 } | |
219 | |
220 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) | |
221 { | |
222 const S *ego = (const S *) ego_; | |
223 P *pln; | |
224 plan *cld = (plan *) 0; | |
225 plan *cldrest = (plan *) 0; | |
226 const problem_rdft2 *p = (const problem_rdft2 *) p_; | |
227 R *bufs = (R *) 0; | |
228 INT nbuf = 0, bufdist, n, vl; | |
229 INT ivs, ovs, rs, id, od; | |
230 | |
231 static const plan_adt padt = { | |
232 X(rdft2_solve), awake, print, destroy | |
233 }; | |
234 | |
235 if (!applicable(p_, ego, plnr)) | |
236 goto nada; | |
237 | |
238 n = p->sz->dims[0].n; | |
239 X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); | |
240 | |
241 nbuf = X(imax)(X(nbuf)(n, vl, 0), min_nbuf(p, n, vl)); | |
242 bufdist = X(bufdist)(n, vl); | |
243 A(nbuf > 0); | |
244 | |
245 /* initial allocation for the purpose of planning */ | |
246 bufs = (R *) MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS); | |
247 | |
248 id = ivs * (nbuf * (vl / nbuf)); | |
249 od = ovs * (nbuf * (vl / nbuf)); | |
250 | |
251 if (p->kind == R2HC) { | |
252 cld = X(mkplan_f_d)( | |
253 plnr, | |
254 X(mkproblem_rdft_d)( | |
255 X(mktensor_1d)(n, p->sz->dims[0].is/2, 1), | |
256 X(mktensor_1d)(nbuf, ivs, bufdist), | |
257 TAINT(p->r0, ivs * nbuf), bufs, &p->kind), | |
258 0, 0, (p->r0 == p->cr) ? NO_DESTROY_INPUT : 0); | |
259 if (!cld) goto nada; | |
260 X(ifree)(bufs); bufs = 0; | |
261 | |
262 cldrest = X(mkplan_d)(plnr, | |
263 X(mkproblem_rdft2_d)( | |
264 X(tensor_copy)(p->sz), | |
265 X(mktensor_1d)(vl % nbuf, ivs, ovs), | |
266 p->r0 + id, p->r1 + id, | |
267 p->cr + od, p->ci + od, | |
268 p->kind)); | |
269 if (!cldrest) goto nada; | |
270 | |
271 pln = MKPLAN_RDFT2(P, &padt, apply_r2hc); | |
272 } else { | |
273 A(p->kind == HC2R); | |
274 cld = X(mkplan_f_d)( | |
275 plnr, | |
276 X(mkproblem_rdft_d)( | |
277 X(mktensor_1d)(n, 1, p->sz->dims[0].os/2), | |
278 X(mktensor_1d)(nbuf, bufdist, ovs), | |
279 bufs, TAINT(p->r0, ovs * nbuf), &p->kind), | |
280 0, 0, NO_DESTROY_INPUT); /* always ok to destroy bufs */ | |
281 if (!cld) goto nada; | |
282 X(ifree)(bufs); bufs = 0; | |
283 | |
284 cldrest = X(mkplan_d)(plnr, | |
285 X(mkproblem_rdft2_d)( | |
286 X(tensor_copy)(p->sz), | |
287 X(mktensor_1d)(vl % nbuf, ivs, ovs), | |
288 p->r0 + od, p->r1 + od, | |
289 p->cr + id, p->ci + id, | |
290 p->kind)); | |
291 if (!cldrest) goto nada; | |
292 pln = MKPLAN_RDFT2(P, &padt, apply_hc2r); | |
293 } | |
294 | |
295 pln->cld = cld; | |
296 pln->cldrest = cldrest; | |
297 pln->n = n; | |
298 pln->vl = vl; | |
299 pln->ivs = ivs; | |
300 pln->ovs = ovs; | |
301 X(rdft2_strides)(p->kind, &p->sz->dims[0], &rs, &pln->cs); | |
302 pln->nbuf = nbuf; | |
303 pln->bufdist = bufdist; | |
304 | |
305 X(ops_madd)(vl / nbuf, &cld->ops, &cldrest->ops, | |
306 &pln->super.super.ops); | |
307 pln->super.super.ops.other += (p->kind == R2HC ? (n + 2) : n) * vl; | |
308 | |
309 return &(pln->super.super); | |
310 | |
311 nada: | |
312 X(ifree0)(bufs); | |
313 X(plan_destroy_internal)(cldrest); | |
314 X(plan_destroy_internal)(cld); | |
315 return (plan *) 0; | |
316 } | |
317 | |
318 static solver *mksolver(void) | |
319 { | |
320 static const solver_adt sadt = { PROBLEM_RDFT2, mkplan, 0 }; | |
321 S *slv = MKSOLVER(S, &sadt); | |
322 return &(slv->super); | |
323 } | |
324 | |
325 void X(rdft2_rdft_register)(planner *p) | |
326 { | |
327 REGISTER_SOLVER(p, mksolver()); | |
328 } |