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