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 a RODFT00 problem via an R2HC problem, with some pre/post-processing. cannam@95: cannam@95: This code uses the trick from FFTPACK, also documented in a similar cannam@95: form by Numerical Recipes. Unfortunately, this algorithm seems to cannam@95: have intrinsic numerical problems (similar to those in cannam@95: reodft11e-r2hc.c), possibly due to the fact that it multiplies its cannam@95: input by a sine, causing a loss of precision near the zero. For cannam@95: transforms of 16k points, it has already lost three or four decimal cannam@95: places of accuracy, which we deem unacceptable. cannam@95: cannam@95: So, we have abandoned this algorithm in favor of the one in cannam@95: rodft00-r2hc-pad.c, which unfortunately sacrifices 30-50% in speed. cannam@95: The only other alternative in the literature that does not have cannam@95: similar numerical difficulties seems to be the direct adaptation of cannam@95: the Cooley-Tukey decomposition for antisymmetric data, but this cannam@95: would require a whole new set of codelets and it's not clear that cannam@95: it's worth it at this point. However, we did implement the latter cannam@95: algorithm for the specific case of odd n (logically adapting the cannam@95: split-radix algorithm); see reodft00e-splitradix.c. */ 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: } P; cannam@95: cannam@95: static void apply(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] = 0; cannam@95: for (i = 1; i < n - i; ++i) { cannam@95: E a, b, apb, amb; cannam@95: a = I[is * (i - 1)]; cannam@95: b = I[is * ((n - i) - 1)]; cannam@95: apb = K(2.0) * W[i] * (a + b); cannam@95: amb = (a - b); cannam@95: buf[i] = apb + amb; cannam@95: buf[n - i] = apb - amb; cannam@95: } cannam@95: if (i == n - i) { cannam@95: buf[i] = K(4.0) * I[is * (i - 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: /* FIXME: use recursive/cascade summation for better stability? */ cannam@95: O[0] = buf[0] * 0.5; cannam@95: for (i = 1; i + i < n - 1; ++i) { cannam@95: INT k = i + i; cannam@95: O[os * (k - 1)] = -buf[n - i]; cannam@95: O[os * k] = O[os * (k - 2)] + buf[i]; cannam@95: } cannam@95: if (i + i == n - 1) { cannam@95: O[os * (n - 2)] = -buf[n - 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 rodft00e_tw[] = { 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, cannam@95: &ego->td, rodft00e_tw, 2*ego->n, 1, (ego->n+1)/2); 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, "(rodft00e-r2hc-%D%v%(%p%))", ego->n - 1, 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] == RODFT00 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 + 1; 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: pln = MKPLAN_RDFT(P, &padt, apply); 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: 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 * 5 + (n-2)/2 * 5; cannam@95: ops.add = (n-1)/2 * 4 + (n-2)/2 * 1; cannam@95: ops.mul = 1 + (n-1)/2 * 2; cannam@95: if (n % 2 == 0) cannam@95: ops.mul += 1; 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(rodft00e_r2hc_register)(planner *p) cannam@95: { cannam@95: REGISTER_SOLVER(p, mksolver()); cannam@95: }