cannam@95: /* cannam@95: * Copyright (c) 2003, 2007-11 Matteo Frigo cannam@95: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology cannam@95: * cannam@95: * This program is free software; you can redistribute it and/or modify cannam@95: * it under the terms of the GNU General Public License as published by cannam@95: * the Free Software Foundation; either version 2 of the License, or cannam@95: * (at your option) any later version. cannam@95: * cannam@95: * This program is distributed in the hope that it will be useful, cannam@95: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@95: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@95: * GNU General Public License for more details. cannam@95: * cannam@95: * You should have received a copy of the GNU General Public License cannam@95: * along with this program; if not, write to the Free Software cannam@95: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@95: * cannam@95: */ cannam@95: cannam@95: cannam@95: /* Do an R{E,O}DFT{01,10} problem via an R2HC problem, with some cannam@95: pre/post-processing ala FFTPACK. */ cannam@95: cannam@95: #include "reodft.h" cannam@95: cannam@95: typedef struct { cannam@95: solver super; cannam@95: } S; cannam@95: cannam@95: typedef struct { cannam@95: plan_rdft super; cannam@95: plan *cld; cannam@95: twid *td; cannam@95: INT is, os; cannam@95: INT n; cannam@95: INT vl; cannam@95: INT ivs, ovs; cannam@95: rdft_kind kind; cannam@95: } P; cannam@95: cannam@95: /* A real-even-01 DFT operates logically on a size-4N array: cannam@95: I 0 -r(I*) -I 0 r(I*), cannam@95: where r denotes reversal and * denotes deletion of the 0th element. cannam@95: To compute the transform of this, we imagine performing a radix-4 cannam@95: (real-input) DIF step, which turns the size-4N DFT into 4 size-N cannam@95: (contiguous) DFTs, two of which are zero and two of which are cannam@95: conjugates. The non-redundant size-N DFT has halfcomplex input, so cannam@95: we can do it with a size-N hc2r transform. (In order to share cannam@95: plans with the re10 (inverse) transform, however, we use the DHT cannam@95: trick to re-express the hc2r problem as r2hc. This has little cost cannam@95: since we are already pre- and post-processing the data in {i,n-i} cannam@95: order.) Finally, we have to write out the data in the correct cannam@95: order...the two size-N redundant (conjugate) hc2r DFTs correspond cannam@95: to the even and odd outputs in O (i.e. the usual interleaved output cannam@95: of DIF transforms); since this data has even symmetry, we only cannam@95: write the first half of it. cannam@95: cannam@95: The real-even-10 DFT is just the reverse of these steps, i.e. a cannam@95: radix-4 DIT transform. There, however, we just use the r2hc cannam@95: transform naturally without resorting to the DHT trick. cannam@95: cannam@95: A real-odd-01 DFT is very similar, except that the input is cannam@95: 0 I (rI)* 0 -I -(rI)*. This format, however, can be transformed cannam@95: into precisely the real-even-01 format above by sending I -> rI cannam@95: and shifting the array by N. The former swap is just another cannam@95: transformation on the input during preprocessing; the latter cannam@95: multiplies the even/odd outputs by i/-i, which combines with cannam@95: the factor of -i (to take the imaginary part) to simply flip cannam@95: the sign of the odd outputs. Vice-versa for real-odd-10. cannam@95: cannam@95: The FFTPACK source code was very helpful in working this out. cannam@95: (They do unnecessary passes over the array, though.) The same cannam@95: algorithm is also described in: cannam@95: cannam@95: John Makhoul, "A fast cosine transform in one and two dimensions," cannam@95: IEEE Trans. on Acoust. Speech and Sig. Proc., ASSP-28 (1), 27--34 (1980). cannam@95: cannam@95: Note that Numerical Recipes suggests a different algorithm that cannam@95: requires more operations and uses trig. functions for both the pre- cannam@95: and post-processing passes. cannam@95: */ cannam@95: cannam@95: static void apply_re01(const plan *ego_, R *I, R *O) cannam@95: { cannam@95: const P *ego = (const P *) ego_; cannam@95: INT is = ego->is, os = ego->os; cannam@95: INT i, n = ego->n; cannam@95: INT iv, vl = ego->vl; cannam@95: INT ivs = ego->ivs, ovs = ego->ovs; cannam@95: R *W = ego->td->W; cannam@95: R *buf; cannam@95: cannam@95: buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); cannam@95: cannam@95: for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { cannam@95: buf[0] = I[0]; cannam@95: for (i = 1; i < n - i; ++i) { cannam@95: E a, b, apb, amb, wa, wb; cannam@95: a = I[is * i]; cannam@95: b = I[is * (n - i)]; cannam@95: apb = a + b; cannam@95: amb = a - b; cannam@95: wa = W[2*i]; cannam@95: wb = W[2*i + 1]; cannam@95: buf[i] = wa * amb + wb * apb; cannam@95: buf[n - i] = wa * apb - wb * amb; cannam@95: } cannam@95: if (i == n - i) { cannam@95: buf[i] = K(2.0) * I[is * i] * W[2*i]; cannam@95: } cannam@95: cannam@95: { cannam@95: plan_rdft *cld = (plan_rdft *) ego->cld; cannam@95: cld->apply((plan *) cld, buf, buf); cannam@95: } cannam@95: cannam@95: O[0] = buf[0]; cannam@95: for (i = 1; i < n - i; ++i) { cannam@95: E a, b; cannam@95: INT k; cannam@95: a = buf[i]; cannam@95: b = buf[n - i]; cannam@95: k = i + i; cannam@95: O[os * (k - 1)] = a - b; cannam@95: O[os * k] = a + b; cannam@95: } cannam@95: if (i == n - i) { cannam@95: O[os * (n - 1)] = buf[i]; cannam@95: } cannam@95: } cannam@95: cannam@95: X(ifree)(buf); cannam@95: } cannam@95: cannam@95: /* ro01 is same as re01, but with i <-> n - 1 - i in the input and cannam@95: the sign of the odd output elements flipped. */ cannam@95: static void apply_ro01(const plan *ego_, R *I, R *O) cannam@95: { cannam@95: const P *ego = (const P *) ego_; cannam@95: INT is = ego->is, os = ego->os; cannam@95: INT i, n = ego->n; cannam@95: INT iv, vl = ego->vl; cannam@95: INT ivs = ego->ivs, ovs = ego->ovs; cannam@95: R *W = ego->td->W; cannam@95: R *buf; cannam@95: cannam@95: buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); cannam@95: cannam@95: for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { cannam@95: buf[0] = I[is * (n - 1)]; cannam@95: for (i = 1; i < n - i; ++i) { cannam@95: E a, b, apb, amb, wa, wb; cannam@95: a = I[is * (n - 1 - i)]; cannam@95: b = I[is * (i - 1)]; cannam@95: apb = a + b; cannam@95: amb = a - b; cannam@95: wa = W[2*i]; cannam@95: wb = W[2*i+1]; cannam@95: buf[i] = wa * amb + wb * apb; cannam@95: buf[n - i] = wa * apb - wb * amb; cannam@95: } cannam@95: if (i == n - i) { cannam@95: buf[i] = K(2.0) * I[is * (i - 1)] * W[2*i]; cannam@95: } cannam@95: cannam@95: { cannam@95: plan_rdft *cld = (plan_rdft *) ego->cld; cannam@95: cld->apply((plan *) cld, buf, buf); cannam@95: } cannam@95: cannam@95: O[0] = buf[0]; cannam@95: for (i = 1; i < n - i; ++i) { cannam@95: E a, b; cannam@95: INT k; cannam@95: a = buf[i]; cannam@95: b = buf[n - i]; cannam@95: k = i + i; cannam@95: O[os * (k - 1)] = b - a; cannam@95: O[os * k] = a + b; cannam@95: } cannam@95: if (i == n - i) { cannam@95: O[os * (n - 1)] = -buf[i]; cannam@95: } cannam@95: } cannam@95: cannam@95: X(ifree)(buf); cannam@95: } cannam@95: cannam@95: static void apply_re10(const plan *ego_, R *I, R *O) cannam@95: { cannam@95: const P *ego = (const P *) ego_; cannam@95: INT is = ego->is, os = ego->os; cannam@95: INT i, n = ego->n; cannam@95: INT iv, vl = ego->vl; cannam@95: INT ivs = ego->ivs, ovs = ego->ovs; cannam@95: R *W = ego->td->W; cannam@95: R *buf; cannam@95: cannam@95: buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); cannam@95: cannam@95: for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { cannam@95: buf[0] = I[0]; cannam@95: for (i = 1; i < n - i; ++i) { cannam@95: E u, v; cannam@95: INT k = i + i; cannam@95: u = I[is * (k - 1)]; cannam@95: v = I[is * k]; cannam@95: buf[n - i] = u; cannam@95: buf[i] = v; cannam@95: } cannam@95: if (i == n - i) { cannam@95: buf[i] = I[is * (n - 1)]; cannam@95: } cannam@95: cannam@95: { cannam@95: plan_rdft *cld = (plan_rdft *) ego->cld; cannam@95: cld->apply((plan *) cld, buf, buf); cannam@95: } cannam@95: cannam@95: O[0] = K(2.0) * buf[0]; cannam@95: for (i = 1; i < n - i; ++i) { cannam@95: E a, b, wa, wb; cannam@95: a = K(2.0) * buf[i]; cannam@95: b = K(2.0) * buf[n - i]; cannam@95: wa = W[2*i]; cannam@95: wb = W[2*i + 1]; cannam@95: O[os * i] = wa * a + wb * b; cannam@95: O[os * (n - i)] = wb * a - wa * b; cannam@95: } cannam@95: if (i == n - i) { cannam@95: O[os * i] = K(2.0) * buf[i] * W[2*i]; cannam@95: } cannam@95: } cannam@95: cannam@95: X(ifree)(buf); cannam@95: } cannam@95: cannam@95: /* ro10 is same as re10, but with i <-> n - 1 - i in the output and cannam@95: the sign of the odd input elements flipped. */ cannam@95: static void apply_ro10(const plan *ego_, R *I, R *O) cannam@95: { cannam@95: const P *ego = (const P *) ego_; cannam@95: INT is = ego->is, os = ego->os; cannam@95: INT i, n = ego->n; cannam@95: INT iv, vl = ego->vl; cannam@95: INT ivs = ego->ivs, ovs = ego->ovs; cannam@95: R *W = ego->td->W; cannam@95: R *buf; cannam@95: cannam@95: buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); cannam@95: cannam@95: for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { cannam@95: buf[0] = I[0]; cannam@95: for (i = 1; i < n - i; ++i) { cannam@95: E u, v; cannam@95: INT k = i + i; cannam@95: u = -I[is * (k - 1)]; cannam@95: v = I[is * k]; cannam@95: buf[n - i] = u; cannam@95: buf[i] = v; cannam@95: } cannam@95: if (i == n - i) { cannam@95: buf[i] = -I[is * (n - 1)]; cannam@95: } cannam@95: cannam@95: { cannam@95: plan_rdft *cld = (plan_rdft *) ego->cld; cannam@95: cld->apply((plan *) cld, buf, buf); cannam@95: } cannam@95: cannam@95: O[os * (n - 1)] = K(2.0) * buf[0]; cannam@95: for (i = 1; i < n - i; ++i) { cannam@95: E a, b, wa, wb; cannam@95: a = K(2.0) * buf[i]; cannam@95: b = K(2.0) * buf[n - i]; cannam@95: wa = W[2*i]; cannam@95: wb = W[2*i + 1]; cannam@95: O[os * (n - 1 - i)] = wa * a + wb * b; cannam@95: O[os * (i - 1)] = wb * a - wa * b; cannam@95: } cannam@95: if (i == n - i) { cannam@95: O[os * (i - 1)] = K(2.0) * buf[i] * W[2*i]; cannam@95: } cannam@95: } cannam@95: cannam@95: X(ifree)(buf); cannam@95: } cannam@95: cannam@95: static void awake(plan *ego_, enum wakefulness wakefulness) cannam@95: { cannam@95: P *ego = (P *) ego_; cannam@95: static const tw_instr reodft010e_tw[] = { cannam@95: { TW_COS, 0, 1 }, cannam@95: { TW_SIN, 0, 1 }, cannam@95: { TW_NEXT, 1, 0 } cannam@95: }; cannam@95: cannam@95: X(plan_awake)(ego->cld, wakefulness); cannam@95: cannam@95: X(twiddle_awake)(wakefulness, &ego->td, reodft010e_tw, cannam@95: 4*ego->n, 1, ego->n/2+1); cannam@95: } cannam@95: cannam@95: static void destroy(plan *ego_) cannam@95: { cannam@95: P *ego = (P *) ego_; cannam@95: X(plan_destroy_internal)(ego->cld); cannam@95: } cannam@95: cannam@95: static void print(const plan *ego_, printer *p) cannam@95: { cannam@95: const P *ego = (const P *) ego_; cannam@95: p->print(p, "(%se-r2hc-%D%v%(%p%))", cannam@95: X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld); cannam@95: } cannam@95: cannam@95: static int applicable0(const solver *ego_, const problem *p_) cannam@95: { cannam@95: const problem_rdft *p = (const problem_rdft *) p_; cannam@95: UNUSED(ego_); cannam@95: cannam@95: return (1 cannam@95: && p->sz->rnk == 1 cannam@95: && p->vecsz->rnk <= 1 cannam@95: && (p->kind[0] == REDFT01 || p->kind[0] == REDFT10 cannam@95: || p->kind[0] == RODFT01 || p->kind[0] == RODFT10) cannam@95: ); cannam@95: } cannam@95: cannam@95: static int applicable(const solver *ego, const problem *p, const planner *plnr) cannam@95: { cannam@95: return (!NO_SLOWP(plnr) && applicable0(ego, p)); cannam@95: } cannam@95: cannam@95: static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) cannam@95: { cannam@95: P *pln; cannam@95: const problem_rdft *p; cannam@95: plan *cld; cannam@95: R *buf; cannam@95: INT n; cannam@95: opcnt ops; cannam@95: cannam@95: static const plan_adt padt = { cannam@95: X(rdft_solve), awake, print, destroy cannam@95: }; cannam@95: cannam@95: if (!applicable(ego_, p_, plnr)) cannam@95: return (plan *)0; cannam@95: cannam@95: p = (const problem_rdft *) p_; cannam@95: cannam@95: n = p->sz->dims[0].n; cannam@95: buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); cannam@95: cannam@95: cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1), cannam@95: X(mktensor_0d)(), cannam@95: buf, buf, R2HC)); cannam@95: X(ifree)(buf); cannam@95: if (!cld) cannam@95: return (plan *)0; cannam@95: cannam@95: switch (p->kind[0]) { cannam@95: case REDFT01: pln = MKPLAN_RDFT(P, &padt, apply_re01); break; cannam@95: case REDFT10: pln = MKPLAN_RDFT(P, &padt, apply_re10); break; cannam@95: case RODFT01: pln = MKPLAN_RDFT(P, &padt, apply_ro01); break; cannam@95: case RODFT10: pln = MKPLAN_RDFT(P, &padt, apply_ro10); break; cannam@95: default: A(0); return (plan*)0; cannam@95: } cannam@95: cannam@95: pln->n = n; cannam@95: pln->is = p->sz->dims[0].is; cannam@95: pln->os = p->sz->dims[0].os; cannam@95: pln->cld = cld; cannam@95: pln->td = 0; cannam@95: pln->kind = p->kind[0]; cannam@95: cannam@95: X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); cannam@95: cannam@95: X(ops_zero)(&ops); cannam@95: ops.other = 4 + (n-1)/2 * 10 + (1 - n % 2) * 5; cannam@95: if (p->kind[0] == REDFT01 || p->kind[0] == RODFT01) { cannam@95: ops.add = (n-1)/2 * 6; cannam@95: ops.mul = (n-1)/2 * 4 + (1 - n % 2) * 2; cannam@95: } cannam@95: else { /* 10 transforms */ cannam@95: ops.add = (n-1)/2 * 2; cannam@95: ops.mul = 1 + (n-1)/2 * 6 + (1 - n % 2) * 2; cannam@95: } cannam@95: cannam@95: X(ops_zero)(&pln->super.super.ops); cannam@95: X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); cannam@95: X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); cannam@95: cannam@95: return &(pln->super.super); cannam@95: } cannam@95: cannam@95: /* constructor */ cannam@95: static solver *mksolver(void) cannam@95: { cannam@95: static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; cannam@95: S *slv = MKSOLVER(S, &sadt); cannam@95: return &(slv->super); cannam@95: } cannam@95: cannam@95: void X(reodft010e_r2hc_register)(planner *p) cannam@95: { cannam@95: REGISTER_SOLVER(p, mksolver()); cannam@95: }