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: reodft010e-r2hc.c,v 1.37 2006-01-27 02:10:50 athena Exp $ */
23
/* Do an R{E,O}DFT{01,10} problem via an R2HC problem, with some
24
pre/post-processing ala FFTPACK. */
43
/* A real-even-01 DFT operates logically on a size-4N array:
44
I 0 -r(I*) -I 0 r(I*),
45
where r denotes reversal and * denotes deletion of the 0th element.
46
To compute the transform of this, we imagine performing a radix-4
47
(real-input) DIF step, which turns the size-4N DFT into 4 size-N
48
(contiguous) DFTs, two of which are zero and two of which are
49
conjugates. The non-redundant size-N DFT has halfcomplex input, so
50
we can do it with a size-N hc2r transform. (In order to share
51
plans with the re10 (inverse) transform, however, we use the DHT
52
trick to re-express the hc2r problem as r2hc. This has little cost
53
since we are already pre- and post-processing the data in {i,n-i}
54
order.) Finally, we have to write out the data in the correct
55
order...the two size-N redundant (conjugate) hc2r DFTs correspond
56
to the even and odd outputs in O (i.e. the usual interleaved output
57
of DIF transforms); since this data has even symmetry, we only
58
write the first half of it.
60
The real-even-10 DFT is just the reverse of these steps, i.e. a
61
radix-4 DIT transform. There, however, we just use the r2hc
62
transform naturally without resorting to the DHT trick.
64
A real-odd-01 DFT is very similar, except that the input is
65
0 I (rI)* 0 -I -(rI)*. This format, however, can be transformed
66
into precisely the real-even-01 format above by sending I -> rI
67
and shifting the array by N. The former swap is just another
68
transformation on the input during preprocessing; the latter
69
multiplies the even/odd outputs by i/-i, which combines with
70
the factor of -i (to take the imaginary part) to simply flip
71
the sign of the odd outputs. Vice-versa for real-odd-10.
73
The FFTPACK source code was very helpful in working this out.
74
(They do unnecessary passes over the array, though.) The same
75
algorithm is also described in:
77
John Makhoul, "A fast cosine transform in one and two dimensions,"
78
IEEE Trans. on Acoust. Speech and Sig. Proc., ASSP-28 (1), 27--34 (1980).
80
Note that Numerical Recipes suggests a different algorithm that
81
requires more operations and uses trig. functions for both the pre-
82
and post-processing passes.
85
static void apply_re01(const plan *ego_, R *I, R *O)
87
const P *ego = (const P *) ego_;
88
INT is = ego->is, os = ego->os;
91
INT ivs = ego->ivs, ovs = ego->ovs;
95
buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
97
for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
99
for (i = 1; i < n - i; ++i) {
100
E a, b, apb, amb, wa, wb;
107
buf[i] = wa * amb + wb * apb;
108
buf[n - i] = wa * apb - wb * amb;
111
buf[i] = K(2.0) * I[is * i] * W[2*i];
115
plan_rdft *cld = (plan_rdft *) ego->cld;
116
cld->apply((plan *) cld, buf, buf);
120
for (i = 1; i < n - i; ++i) {
126
O[os * (k - 1)] = a - b;
130
O[os * (n - 1)] = buf[i];
137
/* ro01 is same as re01, but with i <-> n - 1 - i in the input and
138
the sign of the odd output elements flipped. */
139
static void apply_ro01(const plan *ego_, R *I, R *O)
141
const P *ego = (const P *) ego_;
142
INT is = ego->is, os = ego->os;
144
INT iv, vl = ego->vl;
145
INT ivs = ego->ivs, ovs = ego->ovs;
149
buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
151
for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
152
buf[0] = I[is * (n - 1)];
153
for (i = 1; i < n - i; ++i) {
154
E a, b, apb, amb, wa, wb;
155
a = I[is * (n - 1 - i)];
161
buf[i] = wa * amb + wb * apb;
162
buf[n - i] = wa * apb - wb * amb;
165
buf[i] = K(2.0) * I[is * (i - 1)] * W[2*i];
169
plan_rdft *cld = (plan_rdft *) ego->cld;
170
cld->apply((plan *) cld, buf, buf);
174
for (i = 1; i < n - i; ++i) {
180
O[os * (k - 1)] = b - a;
184
O[os * (n - 1)] = -buf[i];
191
static void apply_re10(const plan *ego_, R *I, R *O)
193
const P *ego = (const P *) ego_;
194
INT is = ego->is, os = ego->os;
196
INT iv, vl = ego->vl;
197
INT ivs = ego->ivs, ovs = ego->ovs;
201
buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
203
for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
205
for (i = 1; i < n - i; ++i) {
214
buf[i] = I[is * (n - 1)];
218
plan_rdft *cld = (plan_rdft *) ego->cld;
219
cld->apply((plan *) cld, buf, buf);
222
O[0] = K(2.0) * buf[0];
223
for (i = 1; i < n - i; ++i) {
226
b = K(2.0) * buf[n - i];
229
O[os * i] = wa * a + wb * b;
230
O[os * (n - i)] = wb * a - wa * b;
233
O[os * i] = K(2.0) * buf[i] * W[2*i];
240
/* ro10 is same as re10, but with i <-> n - 1 - i in the output and
241
the sign of the odd input elements flipped. */
242
static void apply_ro10(const plan *ego_, R *I, R *O)
244
const P *ego = (const P *) ego_;
245
INT is = ego->is, os = ego->os;
247
INT iv, vl = ego->vl;
248
INT ivs = ego->ivs, ovs = ego->ovs;
252
buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
254
for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
256
for (i = 1; i < n - i; ++i) {
259
u = -I[is * (k - 1)];
265
buf[i] = -I[is * (n - 1)];
269
plan_rdft *cld = (plan_rdft *) ego->cld;
270
cld->apply((plan *) cld, buf, buf);
273
O[os * (n - 1)] = K(2.0) * buf[0];
274
for (i = 1; i < n - i; ++i) {
277
b = K(2.0) * buf[n - i];
280
O[os * (n - 1 - i)] = wa * a + wb * b;
281
O[os * (i - 1)] = wb * a - wa * b;
284
O[os * (i - 1)] = K(2.0) * buf[i] * W[2*i];
291
static void awake(plan *ego_, enum wakefulness wakefulness)
294
static const tw_instr reodft010e_tw[] = {
300
X(plan_awake)(ego->cld, wakefulness);
302
X(twiddle_awake)(wakefulness, &ego->td, reodft010e_tw,
303
4*ego->n, 1, ego->n/2+1);
306
static void destroy(plan *ego_)
309
X(plan_destroy_internal)(ego->cld);
312
static void print(const plan *ego_, printer *p)
314
const P *ego = (const P *) ego_;
315
p->print(p, "(%se-r2hc-%D%v%(%p%))",
316
X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld);
319
static int applicable0(const solver *ego_, const problem *p_)
321
const problem_rdft *p = (const problem_rdft *) p_;
326
&& p->vecsz->rnk <= 1
327
&& (p->kind[0] == REDFT01 || p->kind[0] == REDFT10
328
|| p->kind[0] == RODFT01 || p->kind[0] == RODFT10)
332
static int applicable(const solver *ego, const problem *p, const planner *plnr)
334
return (!NO_SLOWP(plnr) && applicable0(ego, p));
337
static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
340
const problem_rdft *p;
346
static const plan_adt padt = {
347
X(rdft_solve), awake, print, destroy
350
if (!applicable(ego_, p_, plnr))
353
p = (const problem_rdft *) p_;
355
n = p->sz->dims[0].n;
356
buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
358
cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1),
365
switch (p->kind[0]) {
366
case REDFT01: pln = MKPLAN_RDFT(P, &padt, apply_re01); break;
367
case REDFT10: pln = MKPLAN_RDFT(P, &padt, apply_re10); break;
368
case RODFT01: pln = MKPLAN_RDFT(P, &padt, apply_ro01); break;
369
case RODFT10: pln = MKPLAN_RDFT(P, &padt, apply_ro10); break;
370
default: A(0); return (plan*)0;
374
pln->is = p->sz->dims[0].is;
375
pln->os = p->sz->dims[0].os;
378
pln->kind = p->kind[0];
380
X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
383
ops.other = 4 + (n-1)/2 * 10 + (1 - n % 2) * 5;
384
if (p->kind[0] == REDFT01 || p->kind[0] == RODFT01) {
385
ops.add = (n-1)/2 * 6;
386
ops.mul = (n-1)/2 * 4 + (1 - n % 2) * 2;
388
else { /* 10 transforms */
389
ops.add = (n-1)/2 * 2;
390
ops.mul = 1 + (n-1)/2 * 6 + (1 - n % 2) * 2;
393
X(ops_zero)(&pln->super.super.ops);
394
X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops);
395
X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops);
397
return &(pln->super.super);
401
static solver *mksolver(void)
403
static const solver_adt sadt = { PROBLEM_RDFT, mkplan };
404
S *slv = MKSOLVER(S, &sadt);
405
return &(slv->super);
408
void X(reodft010e_r2hc_register)(planner *p)
410
REGISTER_SOLVER(p, mksolver());