2
* Copyright (c) 2003, 2006 Matteo Frigo
3
* Copyright (c) 2003, 2006 Massachusetts Institute of Technology
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
/* $Id: rdft2-radix2.c,v 1.32 2006-01-27 02:10:50 athena Exp $ */
24
Compute RDFT2 of even size via either a DFT or a vector RDFT of
27
This file is meant as a temporary hack until we do the right thing.
29
The right thing is: 1) get rid of reduction to DFT, and 2) implement
30
arbitrary even-radix reduction to RDFT. We currently reduce to DFT
31
so as to exploit the SIMD code. We currently do only radix-2 in
32
order to avoid generating yet another set of codelets.
39
int (*applicable) (const problem *p_, const planner *plnr);
40
void (*apply) (const plan *ego_, R *r, R *rio, R *iio);
41
problem *(*mkcld) (const problem_rdft2 *p);
60
/* common applicability function of forward problems */
61
static int applicable_f(const problem *p_, const planner *plnr)
63
const problem_rdft2 *p = (const problem_rdft2 *) p_;
69
&& (p->sz->dims[0].n % 2) == 0
73
static int applicable_f_dft(const problem *p_, const planner *plnr)
76
if (applicable_f(p_, plnr)) {
77
const problem_rdft2 *p = (const problem_rdft2 *) p_;
79
|| (p->iio == p->rio + p->sz->dims[0].is
80
&& p->sz->dims[0].os == 2 * p->sz->dims[0].is));
85
/* common applicability function of backward problems */
86
static int applicable_b(const problem *p_, const planner *plnr)
88
const problem_rdft2 *p = (const problem_rdft2 *) p_;
91
&& (p->r == p->rio || !NO_DESTROY_INPUTP(plnr))
94
&& (p->sz->dims[0].n % 2) == 0
98
static int applicable_b_dft(const problem *p_, const planner *plnr)
101
if (applicable_b(p_, plnr)) {
102
const problem_rdft2 *p = (const problem_rdft2 *) p_;
103
return(p->r != p->rio
104
|| (p->iio == p->rio + p->sz->dims[0].os
105
&& p->sz->dims[0].is == 2 * p->sz->dims[0].os));
111
* forward rdft2 via dft
113
static void k_f_dft(R *rio, R *iio, const R *W, INT n, INT dist)
116
R *pp = rio, *pm = rio + n * dist;
119
/* i = 0 and i = n */
121
E rop = pp[0], iop = pp[im];
126
pp += dist; pm -= dist;
129
/* middle elements */
130
for (W += 2, i = 2; i < n; i += 2, W += 2) {
131
E rop = pp[0], iop = pp[im], rom = pm[0], iom = pm[im];
132
E wr = W[0], wi = W[1];
137
E tr = rd * wr - id * wi;
138
E ti = id * wr + rd * wi;
139
pp[0] = K(0.5) * (re + ti);
140
pp[im] = K(0.5) * (ie + tr);
141
pm[0] = K(0.5) * (re - ti);
142
pm[im] = K(0.5) * (tr - ie);
143
pp += dist; pm -= dist;
146
/* i = n/2 when n is even */
147
if (!(n & 1)) pp[im] = -pp[im];
150
static void apply_f_dft(const plan *ego_, R *r, R *rio, R *iio)
152
const P *ego = (const P *) ego_;
155
/* transform input as a vector of complex numbers */
156
plan_dft *cld = (plan_dft *) ego->cld;
157
cld->apply((plan *) cld, r, r + ego->is, rio, iio);
161
INT i, vl = ego->vl, n2 = ego->n / 2;
162
INT ovs = ego->ovs, os = ego->os;
163
const R *W = ego->td->W;
164
for (i = 0; i < vl; ++i, rio += ovs, iio += ovs)
165
k_f_dft(rio, iio, W, n2, os);
169
static problem *mkcld_f_dft(const problem_rdft2 *p)
171
const iodim *d = p->sz->dims;
172
return X(mkproblem_dft_d) (
173
X(mktensor_1d)(d[0].n / 2, d[0].is * 2, d[0].os),
174
X(tensor_copy)(p->vecsz),
175
p->r, p->r + d[0].is, p->rio, p->iio);
178
static const madt adt_f_dft = {
179
applicable_f_dft, apply_f_dft, mkcld_f_dft, {10, 8, 0, 0}, "r2hc2-dft"
183
* forward rdft2 via rdft
185
static void k_f_rdft(R *rio, R *iio, const R *W, INT n, INT dist)
188
R *pp = rio, *pm = rio + n * dist;
191
/* i = 0 and i = n */
193
E rop = pp[0], iop = pp[im];
198
pp += dist; pm -= dist;
201
/* middle elements */
202
for (W += 2, i = 2; i < n; i += 2, W += 2) {
203
E r0 = pp[0], r1 = pp[im], i0 = pm[0], i1 = pm[im];
204
E wr = W[0], wi = W[1];
205
E tr = r1 * wr + i1 * wi;
206
E ti = i1 * wr - r1 * wi;
211
pp += dist; pm -= dist;
214
/* i = n/2 when n is even */
215
if (!(n & 1)) pp[im] = -pp[im];
218
static void apply_f_rdft(const plan *ego_, R *r, R *rio, R *iio)
220
const P *ego = (const P *) ego_;
223
plan_rdft *cld = (plan_rdft *) ego->cld;
224
cld->apply((plan *) cld, r, rio);
228
INT i, vl = ego->vl, n2 = ego->n / 2;
229
INT ovs = ego->ovs, os = ego->os;
230
const R *W = ego->td->W;
231
for (i = 0; i < vl; ++i, rio += ovs, iio += ovs)
232
k_f_rdft(rio, iio, W, n2, os);
236
static problem *mkcld_f_rdft(const problem_rdft2 *p)
238
const iodim *d = p->sz->dims;
240
tensor *radix = X(mktensor_1d)(2, d[0].is, p->iio - p->rio);
241
tensor *cld_vec = X(tensor_append)(radix, p->vecsz);
242
X(tensor_destroy)(radix);
244
return X(mkproblem_rdft_1_d) (
245
X(mktensor_1d)(d[0].n / 2, 2 * d[0].is, d[0].os),
246
cld_vec, p->r, p->rio, R2HC);
249
static const madt adt_f_rdft = {
250
applicable_f, apply_f_rdft, mkcld_f_rdft, {6, 4, 0, 0}, "r2hc2-rdft"
255
* backward rdft2 via dft
257
static void k_b_dft(R *rio, R *iio, const R *W, INT n, INT dist)
260
R *pp = rio, *pm = rio + n * dist;
263
/* i = 0 and i = n */
265
E rop = pp[0], iop = pm[0];
268
pp += dist; pm -= dist;
271
/* middle elements */
272
for (W += 2, i = 2; i < n; i += 2, W += 2) {
273
E a = pp[0], b = pp[im], c = pm[0], d = pm[im];
274
E wr = W[0], wi = W[1];
275
E re = a + c, ti = a - c, ie = b - d, tr = b + d;
276
E rd = tr * wr + ti * wi;
277
E id = ti * wr - tr * wi;
282
pp += dist; pm -= dist;
285
/* i = n/2 when n is even */
286
if (!(n & 1)) { pp[0] *= K(2.0); pp[im] *= -K(2.0); }
289
static void apply_b_dft(const plan *ego_, R *r, R *rio, R *iio)
291
const P *ego = (const P *) ego_;
293
INT i, vl = ego->vl, n2 = ego->n / 2;
294
INT ivs = ego->ivs, is = ego->is;
295
const R *W = ego->td->W;
296
R *rio1 = rio, *iio1 = iio;
297
for (i = 0; i < vl; ++i, rio1 += ivs, iio1 += ivs)
298
k_b_dft(rio1, iio1, W, n2, is);
302
plan_dft *cld = (plan_dft *) ego->cld;
303
/* swap r/i because of backward transform */
304
cld->apply((plan *) cld, iio, rio, r + ego->os, r);
308
static problem *mkcld_b_dft(const problem_rdft2 *p)
310
const iodim *d = p->sz->dims;
312
return X(mkproblem_dft_d) (
313
X(mktensor_1d)(d[0].n / 2, d[0].is, 2 * d[0].os),
314
X(tensor_copy)(p->vecsz),
315
p->iio, p->rio, p->r + d[0].os, p->r);
318
static const madt adt_b_dft = {
319
applicable_b_dft, apply_b_dft, mkcld_b_dft, {10, 8, 0, 0}, "hc2r2-dft"
323
* backward rdft2 via backward rdft
325
static void k_b_rdft(R *rio, R *iio, const R *W, INT n, INT dist)
328
R *pp = rio, *pm = rio + n * dist;
331
/* i = 0 and i = n */
333
E rop = pp[0], iop = pm[0];
336
pp += dist; pm -= dist;
339
/* middle elements */
340
for (W += 2, i = 2; i < n; i += 2, W += 2) {
341
E a = pp[0], b = pp[im], c = pm[0], d = pm[im];
342
E wr = W[0], wi = W[1];
343
E r0 = a + c, r1 = a - c, i0 = b - d, i1 = b + d;
346
pp[im] = r1 * wr - i1 * wi;
347
pm[im] = i1 * wr + r1 * wi;
348
pp += dist; pm -= dist;
351
/* i = n/2 when n is even */
352
if (!(n & 1)) { pp[0] *= K(2.0); pp[im] *= -K(2.0); }
355
static void apply_b_rdft(const plan *ego_, R *r, R *rio, R *iio)
357
const P *ego = (const P *) ego_;
360
INT i, vl = ego->vl, n2 = ego->n / 2;
361
INT ivs = ego->ivs, is = ego->is;
362
const R *W = ego->td->W;
363
R *rio1 = rio, *iio1 = iio;
364
for (i = 0; i < vl; ++i, rio1 += ivs, iio1 += ivs)
365
k_b_rdft(rio1, iio1, W, n2, is);
369
plan_rdft *cld = (plan_rdft *) ego->cld;
370
cld->apply((plan *) cld, rio, r);
374
static problem *mkcld_b_rdft(const problem_rdft2 *p)
376
const iodim *d = p->sz->dims;
378
tensor *radix = X(mktensor_1d)(2, p->iio - p->rio, d[0].os);
379
tensor *cld_vec = X(tensor_append)(radix, p->vecsz);
380
X(tensor_destroy)(radix);
382
return X(mkproblem_rdft_1_d) (
383
X(mktensor_1d)(d[0].n / 2, d[0].is, 2 * d[0].os),
384
cld_vec, p->rio, p->r, HC2R);
387
static const madt adt_b_rdft = {
388
applicable_b, apply_b_rdft, mkcld_b_rdft, {6, 4, 0, 0}, "hc2r2-rdft"
394
static void awake(plan *ego_, enum wakefulness wakefulness)
397
static const tw_instr twinstr[] = { {TW_FULL, 0, 2}, {TW_NEXT, 1, 0} };
398
X(plan_awake)(ego->cld, wakefulness);
399
X(twiddle_awake)(wakefulness, &ego->td, twinstr,
400
ego->n, 2, (ego->n / 2 + 1) / 2);
403
static void destroy(plan *ego_)
406
X(plan_destroy_internal) (ego->cld);
409
static void print(const plan *ego_, printer * p)
411
const P *ego = (const P *) ego_;
412
p->print(p, "(%s-%D%v%(%p%))", ego->slv->adt->nam,
413
ego->n, ego->vl, ego->cld);
416
static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
418
const S *ego = (const S *) ego_;
420
const problem_rdft2 *p;
424
static const plan_adt padt = {
425
X(rdft2_solve), awake, print, destroy
428
if (!ego->adt->applicable(p_, plnr))
431
p = (const problem_rdft2 *) p_;
433
cld = X(mkplan_d)(plnr, ego->adt->mkcld(p));
434
if (!cld) return (plan *) 0;
436
pln = MKPLAN_RDFT2(P, &padt, ego->adt->apply);
442
X(tensor_tornk1) (p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
448
X(ops_madd)(pln->vl * ((pln->n/2 + 1) / 2), &ego->adt->ops,
449
&cld->ops, &pln->super.super.ops);
451
return &(pln->super.super);
454
static solver *mksolver(const madt *adt)
456
static const solver_adt sadt = { PROBLEM_RDFT2, mkplan };
457
S *slv = MKSOLVER(S, &sadt);
459
return &(slv->super);
462
void X(rdft2_radix2_register)(planner *p)
465
static const madt *const adts[] = {
466
&adt_f_dft, &adt_f_rdft,
467
&adt_b_dft, &adt_b_rdft
470
for (i = 0; i < sizeof(adts) / sizeof(adts[0]); ++i)
471
REGISTER_SOLVER(p, mksolver(adts[i]));