Chris@19: /* Chris@19: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@19: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@19: * Chris@19: * This program is free software; you can redistribute it and/or modify Chris@19: * it under the terms of the GNU General Public License as published by Chris@19: * the Free Software Foundation; either version 2 of the License, or Chris@19: * (at your option) any later version. Chris@19: * Chris@19: * This program is distributed in the hope that it will be useful, Chris@19: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@19: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@19: * GNU General Public License for more details. Chris@19: * Chris@19: * You should have received a copy of the GNU General Public License Chris@19: * along with this program; if not, write to the Free Software Chris@19: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@19: * Chris@19: */ Chris@19: Chris@19: Chris@19: /* Do a REDFT00 problem via an R2HC problem, padded symmetrically to Chris@19: twice the size. This is asymptotically a factor of ~2 worse than Chris@19: redft00e-r2hc.c (the algorithm used in e.g. FFTPACK and Numerical Chris@19: Recipes), but we abandoned the latter after we discovered that it Chris@19: has intrinsic accuracy problems. */ Chris@19: Chris@19: #include "reodft.h" Chris@19: Chris@19: typedef struct { Chris@19: solver super; Chris@19: } S; Chris@19: Chris@19: typedef struct { Chris@19: plan_rdft super; Chris@19: plan *cld, *cldcpy; Chris@19: INT is; Chris@19: INT n; Chris@19: INT vl; Chris@19: INT ivs, ovs; Chris@19: } P; Chris@19: Chris@19: static void apply(const plan *ego_, R *I, R *O) Chris@19: { Chris@19: const P *ego = (const P *) ego_; Chris@19: INT is = ego->is; Chris@19: INT i, n = ego->n; Chris@19: INT iv, vl = ego->vl; Chris@19: INT ivs = ego->ivs, ovs = ego->ovs; Chris@19: R *buf; Chris@19: Chris@19: buf = (R *) MALLOC(sizeof(R) * (2*n), BUFFERS); Chris@19: Chris@19: for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { Chris@19: buf[0] = I[0]; Chris@19: for (i = 1; i < n; ++i) { Chris@19: R a = I[i * is]; Chris@19: buf[i] = a; Chris@19: buf[2*n - i] = a; Chris@19: } Chris@19: buf[i] = I[i * is]; /* i == n, Nyquist */ Chris@19: Chris@19: /* r2hc transform of size 2*n */ Chris@19: { Chris@19: plan_rdft *cld = (plan_rdft *) ego->cld; Chris@19: cld->apply((plan *) cld, buf, buf); Chris@19: } Chris@19: Chris@19: /* copy n+1 real numbers (real parts of hc array) from buf to O */ Chris@19: { Chris@19: plan_rdft *cldcpy = (plan_rdft *) ego->cldcpy; Chris@19: cldcpy->apply((plan *) cldcpy, buf, O); Chris@19: } Chris@19: } Chris@19: Chris@19: X(ifree)(buf); Chris@19: } Chris@19: Chris@19: static void awake(plan *ego_, enum wakefulness wakefulness) Chris@19: { Chris@19: P *ego = (P *) ego_; Chris@19: X(plan_awake)(ego->cld, wakefulness); Chris@19: X(plan_awake)(ego->cldcpy, wakefulness); Chris@19: } Chris@19: Chris@19: static void destroy(plan *ego_) Chris@19: { Chris@19: P *ego = (P *) ego_; Chris@19: X(plan_destroy_internal)(ego->cldcpy); Chris@19: X(plan_destroy_internal)(ego->cld); Chris@19: } Chris@19: Chris@19: static void print(const plan *ego_, printer *p) Chris@19: { Chris@19: const P *ego = (const P *) ego_; Chris@19: p->print(p, "(redft00e-r2hc-pad-%D%v%(%p%)%(%p%))", Chris@19: ego->n + 1, ego->vl, ego->cld, ego->cldcpy); Chris@19: } Chris@19: Chris@19: static int applicable0(const solver *ego_, const problem *p_) Chris@19: { Chris@19: const problem_rdft *p = (const problem_rdft *) p_; Chris@19: UNUSED(ego_); Chris@19: Chris@19: return (1 Chris@19: && p->sz->rnk == 1 Chris@19: && p->vecsz->rnk <= 1 Chris@19: && p->kind[0] == REDFT00 Chris@19: && p->sz->dims[0].n > 1 /* n == 1 is not well-defined */ Chris@19: ); Chris@19: } Chris@19: Chris@19: static int applicable(const solver *ego, const problem *p, const planner *plnr) Chris@19: { Chris@19: return (!NO_SLOWP(plnr) && applicable0(ego, p)); Chris@19: } Chris@19: Chris@19: static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) Chris@19: { Chris@19: P *pln; Chris@19: const problem_rdft *p; Chris@19: plan *cld = (plan *) 0, *cldcpy; Chris@19: R *buf = (R *) 0; Chris@19: INT n; Chris@19: INT vl, ivs, ovs; Chris@19: opcnt ops; Chris@19: Chris@19: static const plan_adt padt = { Chris@19: X(rdft_solve), awake, print, destroy Chris@19: }; Chris@19: Chris@19: if (!applicable(ego_, p_, plnr)) Chris@19: goto nada; Chris@19: Chris@19: p = (const problem_rdft *) p_; Chris@19: Chris@19: n = p->sz->dims[0].n - 1; Chris@19: A(n > 0); Chris@19: buf = (R *) MALLOC(sizeof(R) * (2*n), BUFFERS); Chris@19: Chris@19: cld = X(mkplan_d)(plnr,X(mkproblem_rdft_1_d)(X(mktensor_1d)(2*n,1,1), Chris@19: X(mktensor_0d)(), Chris@19: buf, buf, R2HC)); Chris@19: if (!cld) Chris@19: goto nada; Chris@19: Chris@19: X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); Chris@19: cldcpy = Chris@19: X(mkplan_d)(plnr, Chris@19: X(mkproblem_rdft_1_d)(X(mktensor_0d)(), Chris@19: X(mktensor_1d)(n+1,1, Chris@19: p->sz->dims[0].os), Chris@19: buf, TAINT(p->O, ovs), R2HC)); Chris@19: if (!cldcpy) Chris@19: goto nada; Chris@19: Chris@19: X(ifree)(buf); Chris@19: Chris@19: pln = MKPLAN_RDFT(P, &padt, apply); Chris@19: Chris@19: pln->n = n; Chris@19: pln->is = p->sz->dims[0].is; Chris@19: pln->cld = cld; Chris@19: pln->cldcpy = cldcpy; Chris@19: pln->vl = vl; Chris@19: pln->ivs = ivs; Chris@19: pln->ovs = ovs; Chris@19: Chris@19: X(ops_zero)(&ops); Chris@19: ops.other = n + 2*n; /* loads + stores (input -> buf) */ Chris@19: Chris@19: X(ops_zero)(&pln->super.super.ops); Chris@19: X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); Chris@19: X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); Chris@19: X(ops_madd2)(pln->vl, &cldcpy->ops, &pln->super.super.ops); Chris@19: Chris@19: return &(pln->super.super); Chris@19: Chris@19: nada: Chris@19: X(ifree0)(buf); Chris@19: if (cld) Chris@19: X(plan_destroy_internal)(cld); Chris@19: return (plan *)0; Chris@19: } Chris@19: Chris@19: /* constructor */ Chris@19: static solver *mksolver(void) Chris@19: { Chris@19: static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; Chris@19: S *slv = MKSOLVER(S, &sadt); Chris@19: return &(slv->super); Chris@19: } Chris@19: Chris@19: void X(redft00e_r2hc_pad_register)(planner *p) Chris@19: { Chris@19: REGISTER_SOLVER(p, mksolver()); Chris@19: }