cannam@127: /* cannam@127: * Copyright (c) 2003, 2007-14 Matteo Frigo cannam@127: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology cannam@127: * cannam@127: * This program is free software; you can redistribute it and/or modify cannam@127: * it under the terms of the GNU General Public License as published by cannam@127: * the Free Software Foundation; either version 2 of the License, or cannam@127: * (at your option) any later version. cannam@127: * cannam@127: * This program is distributed in the hope that it will be useful, cannam@127: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@127: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@127: * GNU General Public License for more details. cannam@127: * cannam@127: * You should have received a copy of the GNU General Public License cannam@127: * along with this program; if not, write to the Free Software cannam@127: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@127: * cannam@127: */ cannam@127: cannam@127: cannam@127: /* Do a REDFT00 problem via an R2HC problem, with some pre/post-processing. cannam@127: cannam@127: This code uses the trick from FFTPACK, also documented in a similar cannam@127: form by Numerical Recipes. Unfortunately, this algorithm seems to cannam@127: have intrinsic numerical problems (similar to those in cannam@127: reodft11e-r2hc.c), possibly due to the fact that it multiplies its cannam@127: input by a cosine, causing a loss of precision near the zero. For cannam@127: transforms of 16k points, it has already lost three or four decimal cannam@127: places of accuracy, which we deem unacceptable. cannam@127: cannam@127: So, we have abandoned this algorithm in favor of the one in cannam@127: redft00-r2hc-pad.c, which unfortunately sacrifices 30-50% in speed. cannam@127: The only other alternative in the literature that does not have cannam@127: similar numerical difficulties seems to be the direct adaptation of cannam@127: the Cooley-Tukey decomposition for symmetric data, but this would cannam@127: require a whole new set of codelets and it's not clear that it's cannam@127: worth it at this point. However, we did implement the latter cannam@127: algorithm for the specific case of odd n (logically adapting the cannam@127: split-radix algorithm); see reodft00e-splitradix.c. */ cannam@127: cannam@127: #include "reodft.h" cannam@127: cannam@127: typedef struct { cannam@127: solver super; cannam@127: } S; cannam@127: cannam@127: typedef struct { cannam@127: plan_rdft super; cannam@127: plan *cld; cannam@127: twid *td; cannam@127: INT is, os; cannam@127: INT n; cannam@127: INT vl; cannam@127: INT ivs, ovs; cannam@127: } P; cannam@127: cannam@127: static void apply(const plan *ego_, R *I, R *O) cannam@127: { cannam@127: const P *ego = (const P *) ego_; cannam@127: INT is = ego->is, os = ego->os; cannam@127: INT i, n = ego->n; cannam@127: INT iv, vl = ego->vl; cannam@127: INT ivs = ego->ivs, ovs = ego->ovs; cannam@127: R *W = ego->td->W; cannam@127: R *buf; cannam@127: E csum; cannam@127: cannam@127: buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); cannam@127: cannam@127: for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { cannam@127: buf[0] = I[0] + I[is * n]; cannam@127: csum = I[0] - I[is * n]; cannam@127: for (i = 1; i < n - i; ++i) { cannam@127: E a, b, apb, amb; cannam@127: a = I[is * i]; cannam@127: b = I[is * (n - i)]; cannam@127: csum += W[2*i] * (amb = K(2.0)*(a - b)); cannam@127: amb = W[2*i+1] * amb; cannam@127: apb = (a + b); cannam@127: buf[i] = apb - amb; cannam@127: buf[n - i] = apb + amb; cannam@127: } cannam@127: if (i == n - i) { cannam@127: buf[i] = K(2.0) * I[is * i]; cannam@127: } cannam@127: cannam@127: { cannam@127: plan_rdft *cld = (plan_rdft *) ego->cld; cannam@127: cld->apply((plan *) cld, buf, buf); cannam@127: } cannam@127: cannam@127: /* FIXME: use recursive/cascade summation for better stability? */ cannam@127: O[0] = buf[0]; cannam@127: O[os] = csum; cannam@127: for (i = 1; i + i < n; ++i) { cannam@127: INT k = i + i; cannam@127: O[os * k] = buf[i]; cannam@127: O[os * (k + 1)] = O[os * (k - 1)] - buf[n - i]; cannam@127: } cannam@127: if (i + i == n) { cannam@127: O[os * n] = buf[i]; cannam@127: } cannam@127: } cannam@127: cannam@127: X(ifree)(buf); cannam@127: } cannam@127: cannam@127: static void awake(plan *ego_, enum wakefulness wakefulness) cannam@127: { cannam@127: P *ego = (P *) ego_; cannam@127: static const tw_instr redft00e_tw[] = { cannam@127: { TW_COS, 0, 1 }, cannam@127: { TW_SIN, 0, 1 }, cannam@127: { TW_NEXT, 1, 0 } cannam@127: }; cannam@127: cannam@127: X(plan_awake)(ego->cld, wakefulness); cannam@127: X(twiddle_awake)(wakefulness, cannam@127: &ego->td, redft00e_tw, 2*ego->n, 1, (ego->n+1)/2); cannam@127: } cannam@127: cannam@127: static void destroy(plan *ego_) cannam@127: { cannam@127: P *ego = (P *) ego_; cannam@127: X(plan_destroy_internal)(ego->cld); cannam@127: } cannam@127: cannam@127: static void print(const plan *ego_, printer *p) cannam@127: { cannam@127: const P *ego = (const P *) ego_; cannam@127: p->print(p, "(redft00e-r2hc-%D%v%(%p%))", ego->n + 1, ego->vl, ego->cld); cannam@127: } cannam@127: cannam@127: static int applicable0(const solver *ego_, const problem *p_) cannam@127: { cannam@127: const problem_rdft *p = (const problem_rdft *) p_; cannam@127: UNUSED(ego_); cannam@127: cannam@127: return (1 cannam@127: && p->sz->rnk == 1 cannam@127: && p->vecsz->rnk <= 1 cannam@127: && p->kind[0] == REDFT00 cannam@127: && p->sz->dims[0].n > 1 /* n == 1 is not well-defined */ cannam@127: ); cannam@127: } cannam@127: cannam@127: static int applicable(const solver *ego, const problem *p, const planner *plnr) cannam@127: { cannam@127: return (!NO_SLOWP(plnr) && applicable0(ego, p)); cannam@127: } cannam@127: cannam@127: static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) cannam@127: { cannam@127: P *pln; cannam@127: const problem_rdft *p; cannam@127: plan *cld; cannam@127: R *buf; cannam@127: INT n; cannam@127: opcnt ops; cannam@127: cannam@127: static const plan_adt padt = { cannam@127: X(rdft_solve), awake, print, destroy cannam@127: }; cannam@127: cannam@127: if (!applicable(ego_, p_, plnr)) cannam@127: return (plan *)0; cannam@127: cannam@127: p = (const problem_rdft *) p_; cannam@127: cannam@127: n = p->sz->dims[0].n - 1; cannam@127: A(n > 0); cannam@127: buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); cannam@127: cannam@127: cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1), cannam@127: X(mktensor_0d)(), cannam@127: buf, buf, R2HC)); cannam@127: X(ifree)(buf); cannam@127: if (!cld) cannam@127: return (plan *)0; cannam@127: cannam@127: pln = MKPLAN_RDFT(P, &padt, apply); cannam@127: cannam@127: pln->n = n; cannam@127: pln->is = p->sz->dims[0].is; cannam@127: pln->os = p->sz->dims[0].os; cannam@127: pln->cld = cld; cannam@127: pln->td = 0; cannam@127: cannam@127: X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); cannam@127: cannam@127: X(ops_zero)(&ops); cannam@127: ops.other = 8 + (n-1)/2 * 11 + (1 - n % 2) * 5; cannam@127: ops.add = 2 + (n-1)/2 * 5; cannam@127: ops.mul = (n-1)/2 * 3 + (1 - n % 2) * 1; cannam@127: cannam@127: X(ops_zero)(&pln->super.super.ops); cannam@127: X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); cannam@127: X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); cannam@127: cannam@127: return &(pln->super.super); cannam@127: } cannam@127: cannam@127: /* constructor */ cannam@127: static solver *mksolver(void) cannam@127: { cannam@127: static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; cannam@127: S *slv = MKSOLVER(S, &sadt); cannam@127: return &(slv->super); cannam@127: } cannam@127: cannam@127: void X(redft00e_r2hc_register)(planner *p) cannam@127: { cannam@127: REGISTER_SOLVER(p, mksolver()); cannam@127: }