diff options
author | scuri <scuri> | 2008-10-17 06:10:15 +0000 |
---|---|---|
committer | scuri <scuri> | 2008-10-17 06:10:15 +0000 |
commit | 5a422aba704c375a307a902bafe658342e209906 (patch) | |
tree | 5005011e086bb863d8fb587ad3319bbec59b2447 /src/fftw3/reodft |
First commit - moving from LuaForge to SourceForge
Diffstat (limited to 'src/fftw3/reodft')
-rw-r--r-- | src/fftw3/reodft/redft00e-r2hc-pad.c | 201 | ||||
-rw-r--r-- | src/fftw3/reodft/redft00e-r2hc.c | 216 | ||||
-rw-r--r-- | src/fftw3/reodft/reoconf.c | 42 | ||||
-rw-r--r-- | src/fftw3/reodft/reodft.h | 41 | ||||
-rw-r--r-- | src/fftw3/reodft/reodft010e-r2hc.c | 409 | ||||
-rw-r--r-- | src/fftw3/reodft/reodft11e-r2hc-odd.c | 304 | ||||
-rw-r--r-- | src/fftw3/reodft/reodft11e-r2hc.c | 295 | ||||
-rw-r--r-- | src/fftw3/reodft/reodft11e-radix2.c | 515 | ||||
-rw-r--r-- | src/fftw3/reodft/rodft00e-r2hc-pad.c | 200 | ||||
-rw-r--r-- | src/fftw3/reodft/rodft00e-r2hc.c | 212 |
10 files changed, 2435 insertions, 0 deletions
diff --git a/src/fftw3/reodft/redft00e-r2hc-pad.c b/src/fftw3/reodft/redft00e-r2hc-pad.c new file mode 100644 index 0000000..ec3fa35 --- /dev/null +++ b/src/fftw3/reodft/redft00e-r2hc-pad.c @@ -0,0 +1,201 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* $Id: redft00e-r2hc-pad.c,v 1.1 2008/10/17 06:13:18 scuri Exp $ */ + +/* Do a REDFT00 problem via an R2HC problem, padded symmetrically to + twice the size. This is asymptotically a factor of ~2 worse than + redft00e-r2hc.c (the algorithm used in e.g. FFTPACK and Numerical + Recipes), but we abandoned the latter after we discovered that it + has intrinsic accuracy problems. */ + +#include "reodft.h" + +typedef struct { + solver super; +} S; + +typedef struct { + plan_rdft super; + plan *cld, *cldcpy; + int is; + int n; + int vl; + int ivs, ovs; +} P; + +static void apply(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * (2*n), BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = I[0]; + for (i = 1; i < n; ++i) { + R a = I[i * is]; + buf[i] = a; + buf[2*n - i] = a; + } + buf[i] = I[i * is]; /* i == n, Nyquist */ + + /* r2hc transform of size 2*n */ + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + /* copy n+1 real numbers (real parts of hc array) from buf to O */ + { + plan_rdft *cldcpy = (plan_rdft *) ego->cldcpy; + cldcpy->apply((plan *) cldcpy, buf, O); + } + } + + X(ifree)(buf); +} + +static void awake(plan *ego_, int flg) +{ + P *ego = (P *) ego_; + AWAKE(ego->cld, flg); + AWAKE(ego->cldcpy, flg); +} + +static void destroy(plan *ego_) +{ + P *ego = (P *) ego_; + X(plan_destroy_internal)(ego->cldcpy); + X(plan_destroy_internal)(ego->cld); +} + +static void print(const plan *ego_, printer *p) +{ + const P *ego = (const P *) ego_; + p->print(p, "(redft00e-r2hc-pad-%d%v%(%p%)%(%p%))", + ego->n + 1, ego->vl, ego->cld, ego->cldcpy); +} + +static int applicable0(const solver *ego_, const problem *p_) +{ + UNUSED(ego_); + if (RDFTP(p_)) { + const problem_rdft *p = (const problem_rdft *) p_; + return (1 + && p->sz->rnk == 1 + && p->vecsz->rnk <= 1 + && p->kind[0] == REDFT00 + && p->sz->dims[0].n > 1 /* n == 1 is not well-defined */ + ); + } + + return 0; +} + +static int applicable(const solver *ego, const problem *p, const planner *plnr) +{ + return (!NO_UGLYP(plnr) && applicable0(ego, p)); +} + +static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) +{ + P *pln; + const problem_rdft *p; + plan *cld = (plan *) 0, *cldcpy; + R *buf = (R *) 0; + int n; + int vl, ivs, ovs; + opcnt ops; + + static const plan_adt padt = { + X(rdft_solve), awake, print, destroy + }; + + if (!applicable(ego_, p_, plnr)) + goto nada; + + p = (const problem_rdft *) p_; + + n = p->sz->dims[0].n - 1; + A(n > 0); + buf = (R *) MALLOC(sizeof(R) * (2*n), BUFFERS); + + cld = X(mkplan_d)(plnr,X(mkproblem_rdft_1_d)(X(mktensor_1d)(2*n,1,1), + X(mktensor_0d)(), + buf, buf, R2HC)); + if (!cld) + goto nada; + + X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); + cldcpy = + X(mkplan_d)(plnr, + X(mkproblem_rdft_1_d)(X(mktensor_0d)(), + X(mktensor_1d)(n+1,1, + p->sz->dims[0].os), + buf, TAINT(p->O, ovs), R2HC)); + if (!cldcpy) + goto nada; + + X(ifree)(buf); + + pln = MKPLAN_RDFT(P, &padt, apply); + + pln->n = n; + pln->is = p->sz->dims[0].is; + pln->cld = cld; + pln->cldcpy = cldcpy; + pln->vl = vl; + pln->ivs = ivs; + pln->ovs = ovs; + + X(ops_zero)(&ops); + ops.other = n + 2*n; /* loads + stores (input -> buf) */ + + X(ops_zero)(&pln->super.super.ops); + X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cldcpy->ops, &pln->super.super.ops); + + return &(pln->super.super); + + nada: + X(ifree0)(buf); + if (cld) + X(plan_destroy_internal)(cld); + return (plan *)0; +} + +/* constructor */ +static solver *mksolver(void) +{ + static const solver_adt sadt = { mkplan }; + S *slv = MKSOLVER(S, &sadt); + return &(slv->super); +} + +void X(redft00e_r2hc_pad_register)(planner *p) +{ + REGISTER_SOLVER(p, mksolver()); +} diff --git a/src/fftw3/reodft/redft00e-r2hc.c b/src/fftw3/reodft/redft00e-r2hc.c new file mode 100644 index 0000000..0cd742f --- /dev/null +++ b/src/fftw3/reodft/redft00e-r2hc.c @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* $Id: redft00e-r2hc.c,v 1.1 2008/10/17 06:13:18 scuri Exp $ */ + +/* Do a REDFT00 problem via an R2HC problem, with some pre/post-processing. + + This code uses the trick from FFTPACK, also documented in a similar + form by Numerical Recipes. Unfortunately, this algorithm seems to + have intrinsic numerical problems (similar to those in + reodft11e-r2hc.c), possibly due to the fact that it multiplies its + input by a cosine, causing a loss of precision near the zero. For + transforms of 16k points, it has already lost three or four decimal + places of accuracy, which we deem unacceptable. + + So, we have abandoned this algorithm in favor of the one in + redft00-r2hc-pad.c, which unfortunately sacrifices 30-50% in speed. + The only other alternative in the literature that does not have + similar numerical difficulties seems to be the direct adaptation of + the Cooley-Tukey decomposition for symmetric data, but this would + require a whole new set of codelets and it's not clear that it's + worth it at this point. */ + +#include "reodft.h" + +typedef struct { + solver super; +} S; + +typedef struct { + plan_rdft super; + plan *cld; + twid *td; + int is, os; + int n; + int vl; + int ivs, ovs; +} P; + +static void apply(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W = ego->td->W; + R *buf; + E csum; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = I[0] + I[is * n]; + csum = I[0] - I[is * n]; + for (i = 1; i < n - i; ++i) { + E a, b, apb, amb; + a = I[is * i]; + b = I[is * (n - i)]; + csum += W[2*i] * (amb = K(2.0)*(a - b)); + amb = W[2*i+1] * amb; + apb = (a + b); + buf[i] = apb - amb; + buf[n - i] = apb + amb; + } + if (i == n - i) { + buf[i] = K(2.0) * I[is * i]; + } + + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + /* FIXME: use recursive/cascade summation for better stability? */ + O[0] = buf[0]; + O[os] = csum; + for (i = 1; i + i < n; ++i) { + int k = i + i; + O[os * k] = buf[i]; + O[os * (k + 1)] = O[os * (k - 1)] - buf[n - i]; + } + if (i + i == n) { + O[os * n] = buf[i]; + } + } + + X(ifree)(buf); +} + +static void awake(plan *ego_, int flg) +{ + P *ego = (P *) ego_; + static const tw_instr redft00e_tw[] = { + { TW_COS, 0, 1 }, + { TW_SIN, 0, 1 }, + { TW_NEXT, 1, 0 } + }; + + AWAKE(ego->cld, flg); + X(twiddle_awake)(flg, &ego->td, redft00e_tw, 2*ego->n, 1, (ego->n+1)/2); +} + +static void destroy(plan *ego_) +{ + P *ego = (P *) ego_; + X(plan_destroy_internal)(ego->cld); +} + +static void print(const plan *ego_, printer *p) +{ + const P *ego = (const P *) ego_; + p->print(p, "(redft00e-r2hc-%d%v%(%p%))", ego->n + 1, ego->vl, ego->cld); +} + +static int applicable0(const solver *ego_, const problem *p_) +{ + UNUSED(ego_); + if (RDFTP(p_)) { + const problem_rdft *p = (const problem_rdft *) p_; + return (1 + && p->sz->rnk == 1 + && p->vecsz->rnk <= 1 + && p->kind[0] == REDFT00 + && p->sz->dims[0].n > 1 /* n == 1 is not well-defined */ + ); + } + + return 0; +} + +static int applicable(const solver *ego, const problem *p, const planner *plnr) +{ + return (!NO_UGLYP(plnr) && applicable0(ego, p)); +} + +static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) +{ + P *pln; + const problem_rdft *p; + plan *cld; + R *buf; + int n; + opcnt ops; + + static const plan_adt padt = { + X(rdft_solve), awake, print, destroy + }; + + if (!applicable(ego_, p_, plnr)) + return (plan *)0; + + p = (const problem_rdft *) p_; + + n = p->sz->dims[0].n - 1; + A(n > 0); + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1), + X(mktensor_0d)(), + buf, buf, R2HC)); + X(ifree)(buf); + if (!cld) + return (plan *)0; + + pln = MKPLAN_RDFT(P, &padt, apply); + + pln->n = n; + pln->is = p->sz->dims[0].is; + pln->os = p->sz->dims[0].os; + pln->cld = cld; + pln->td = 0; + + X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); + + X(ops_zero)(&ops); + ops.other = 8 + (n-1)/2 * 11 + (1 - n % 2) * 5; + ops.add = 2 + (n-1)/2 * 5; + ops.mul = (n-1)/2 * 3 + (1 - n % 2) * 1; + + X(ops_zero)(&pln->super.super.ops); + X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); + + return &(pln->super.super); +} + +/* constructor */ +static solver *mksolver(void) +{ + static const solver_adt sadt = { mkplan }; + S *slv = MKSOLVER(S, &sadt); + return &(slv->super); +} + +void X(redft00e_r2hc_register)(planner *p) +{ + REGISTER_SOLVER(p, mksolver()); +} diff --git a/src/fftw3/reodft/reoconf.c b/src/fftw3/reodft/reoconf.c new file mode 100644 index 0000000..1cd41b6 --- /dev/null +++ b/src/fftw3/reodft/reoconf.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* $Id: reoconf.c,v 1.1 2008/10/17 06:13:18 scuri Exp $ */ + +#include "reodft.h" + +static const solvtab s = +{ + /* SOLVTAB(X(redft00e_r2hc_register)), + SOLVTAB(X(rodft00e_r2hc_register)), */ + SOLVTAB(X(redft00e_r2hc_pad_register)), + SOLVTAB(X(rodft00e_r2hc_pad_register)), + SOLVTAB(X(reodft010e_r2hc_register)), + /* SOLVTAB(X(reodft11e_r2hc_register)), */ + SOLVTAB(X(reodft11e_radix2_r2hc_register)), + SOLVTAB(X(reodft11e_r2hc_odd_register)), + + SOLVTAB_END +}; + +void X(reodft_conf_standard)(planner *p) +{ + X(solvtab_exec)(s, p); +} diff --git a/src/fftw3/reodft/reodft.h b/src/fftw3/reodft/reodft.h new file mode 100644 index 0000000..8c67144 --- /dev/null +++ b/src/fftw3/reodft/reodft.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef __REODFT_H__ +#define __REODFT_H__ + +#include "ifftw.h" +#include "rdft.h" + +#define REODFT_KINDP(k) ((k) >= REDFT00 && (k) <= RODFT11) + +void X(redft00e_r2hc_register)(planner *p); +void X(redft00e_r2hc_pad_register)(planner *p); +void X(rodft00e_r2hc_register)(planner *p); +void X(rodft00e_r2hc_pad_register)(planner *p); +void X(reodft010e_r2hc_register)(planner *p); +void X(reodft11e_r2hc_register)(planner *p); +void X(reodft11e_radix2_r2hc_register)(planner *p); +void X(reodft11e_r2hc_odd_register)(planner *p); + +/* configurations */ +void X(reodft_conf_standard)(planner *p); + +#endif /* __REODFT_H__ */ diff --git a/src/fftw3/reodft/reodft010e-r2hc.c b/src/fftw3/reodft/reodft010e-r2hc.c new file mode 100644 index 0000000..ace14de --- /dev/null +++ b/src/fftw3/reodft/reodft010e-r2hc.c @@ -0,0 +1,409 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* $Id: reodft010e-r2hc.c,v 1.1 2008/10/17 06:13:18 scuri Exp $ */ + +/* Do an R{E,O}DFT{01,10} problem via an R2HC problem, with some + pre/post-processing ala FFTPACK. */ + +#include "reodft.h" + +typedef struct { + solver super; +} S; + +typedef struct { + plan_rdft super; + plan *cld; + twid *td; + int is, os; + int n; + int vl; + int ivs, ovs; + rdft_kind kind; +} P; + +/* A real-even-01 DFT operates logically on a size-4N array: + I 0 -r(I*) -I 0 r(I*), + where r denotes reversal and * denotes deletion of the 0th element. + To compute the transform of this, we imagine performing a radix-4 + (real-input) DIF step, which turns the size-4N DFT into 4 size-N + (contiguous) DFTs, two of which are zero and two of which are + conjugates. The non-redundant size-N DFT has halfcomplex input, so + we can do it with a size-N hc2r transform. (In order to share + plans with the re10 (inverse) transform, however, we use the DHT + trick to re-express the hc2r problem as r2hc. This has little cost + since we are already pre- and post-processing the data in {i,n-i} + order.) Finally, we have to write out the data in the correct + order...the two size-N redundant (conjugate) hc2r DFTs correspond + to the even and odd outputs in O (i.e. the usual interleaved output + of DIF transforms); since this data has even symmetry, we only + write the first half of it. + + The real-even-10 DFT is just the reverse of these steps, i.e. a + radix-4 DIT transform. There, however, we just use the r2hc + transform naturally without resorting to the DHT trick. + + A real-odd-01 DFT is very similar, except that the input is + 0 I (rI)* 0 -I -(rI)*. This format, however, can be transformed + into precisely the real-even-01 format above by sending I -> rI + and shifting the array by N. The former swap is just another + transformation on the input during preprocessing; the latter + multiplies the even/odd outputs by i/-i, which combines with + the factor of -i (to take the imaginary part) to simply flip + the sign of the odd outputs. Vice-versa for real-odd-10. + + The FFTPACK source code was very helpful in working this out. + (They do unnecessary passes over the array, though.) + + Note that Numerical Recipes suggests a different algorithm that + requires more operations and uses trig. functions for both the pre- + and post-processing passes. +*/ + +static void apply_re01(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W = ego->td->W; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = I[0]; + for (i = 1; i < n - i; ++i) { + E a, b, apb, amb, wa, wb; + a = I[is * i]; + b = I[is * (n - i)]; + apb = a + b; + amb = a - b; + wa = W[2*i]; + wb = W[2*i + 1]; + buf[i] = wa * amb + wb * apb; + buf[n - i] = wa * apb - wb * amb; + } + if (i == n - i) { + buf[i] = K(2.0) * I[is * i] * W[2*i]; + } + + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + O[0] = buf[0]; + for (i = 1; i < n - i; ++i) { + E a, b; + int k; + a = buf[i]; + b = buf[n - i]; + k = i + i; + O[os * (k - 1)] = a - b; + O[os * k] = a + b; + } + if (i == n - i) { + O[os * (n - 1)] = buf[i]; + } + } + + X(ifree)(buf); +} + +/* ro01 is same as re01, but with i <-> n - 1 - i in the input and + the sign of the odd output elements flipped. */ +static void apply_ro01(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W = ego->td->W; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = I[is * (n - 1)]; + for (i = 1; i < n - i; ++i) { + E a, b, apb, amb, wa, wb; + a = I[is * (n - 1 - i)]; + b = I[is * (i - 1)]; + apb = a + b; + amb = a - b; + wa = W[2*i]; + wb = W[2*i+1]; + buf[i] = wa * amb + wb * apb; + buf[n - i] = wa * apb - wb * amb; + } + if (i == n - i) { + buf[i] = K(2.0) * I[is * (i - 1)] * W[2*i]; + } + + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + O[0] = buf[0]; + for (i = 1; i < n - i; ++i) { + E a, b; + int k; + a = buf[i]; + b = buf[n - i]; + k = i + i; + O[os * (k - 1)] = b - a; + O[os * k] = a + b; + } + if (i == n - i) { + O[os * (n - 1)] = -buf[i]; + } + } + + X(ifree)(buf); +} + +static void apply_re10(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W = ego->td->W; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = I[0]; + for (i = 1; i < n - i; ++i) { + E u, v; + int k = i + i; + u = I[is * (k - 1)]; + v = I[is * k]; + buf[n - i] = u; + buf[i] = v; + } + if (i == n - i) { + buf[i] = I[is * (n - 1)]; + } + + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + O[0] = K(2.0) * buf[0]; + for (i = 1; i < n - i; ++i) { + E a, b, wa, wb; + a = K(2.0) * buf[i]; + b = K(2.0) * buf[n - i]; + wa = W[2*i]; + wb = W[2*i + 1]; + O[os * i] = wa * a + wb * b; + O[os * (n - i)] = wb * a - wa * b; + } + if (i == n - i) { + O[os * i] = K(2.0) * buf[i] * W[2*i]; + } + } + + X(ifree)(buf); +} + +/* ro10 is same as re10, but with i <-> n - 1 - i in the output and + the sign of the odd input elements flipped. */ +static void apply_ro10(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W = ego->td->W; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = I[0]; + for (i = 1; i < n - i; ++i) { + E u, v; + int k = i + i; + u = -I[is * (k - 1)]; + v = I[is * k]; + buf[n - i] = u; + buf[i] = v; + } + if (i == n - i) { + buf[i] = -I[is * (n - 1)]; + } + + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + O[os * (n - 1)] = K(2.0) * buf[0]; + for (i = 1; i < n - i; ++i) { + E a, b, wa, wb; + a = K(2.0) * buf[i]; + b = K(2.0) * buf[n - i]; + wa = W[2*i]; + wb = W[2*i + 1]; + O[os * (n - 1 - i)] = wa * a + wb * b; + O[os * (i - 1)] = wb * a - wa * b; + } + if (i == n - i) { + O[os * (i - 1)] = K(2.0) * buf[i] * W[2*i]; + } + } + + X(ifree)(buf); +} + +static void awake(plan *ego_, int flg) +{ + P *ego = (P *) ego_; + static const tw_instr reodft010e_tw[] = { + { TW_COS, 0, 1 }, + { TW_SIN, 0, 1 }, + { TW_NEXT, 1, 0 } + }; + + AWAKE(ego->cld, flg); + + X(twiddle_awake)(flg, &ego->td, reodft010e_tw, 4*ego->n, 1, ego->n/2+1); +} + +static void destroy(plan *ego_) +{ + P *ego = (P *) ego_; + X(plan_destroy_internal)(ego->cld); +} + +static void print(const plan *ego_, printer *p) +{ + const P *ego = (const P *) ego_; + p->print(p, "(%se-r2hc-%d%v%(%p%))", + X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld); +} + +static int applicable0(const solver *ego_, const problem *p_) +{ + UNUSED(ego_); + if (RDFTP(p_)) { + const problem_rdft *p = (const problem_rdft *) p_; + return (1 + && p->sz->rnk == 1 + && p->vecsz->rnk <= 1 + && (p->kind[0] == REDFT01 || p->kind[0] == REDFT10 + || p->kind[0] == RODFT01 || p->kind[0] == RODFT10) + ); + } + + return 0; +} + +static int applicable(const solver *ego, const problem *p, const planner *plnr) +{ + return (!NO_UGLYP(plnr) && applicable0(ego, p)); +} + +static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) +{ + P *pln; + const problem_rdft *p; + plan *cld; + R *buf; + int n; + opcnt ops; + + static const plan_adt padt = { + X(rdft_solve), awake, print, destroy + }; + + if (!applicable(ego_, p_, plnr)) + return (plan *)0; + + p = (const problem_rdft *) p_; + + n = p->sz->dims[0].n; + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1), + X(mktensor_0d)(), + buf, buf, R2HC)); + X(ifree)(buf); + if (!cld) + return (plan *)0; + + switch (p->kind[0]) { + case REDFT01: pln = MKPLAN_RDFT(P, &padt, apply_re01); break; + case REDFT10: pln = MKPLAN_RDFT(P, &padt, apply_re10); break; + case RODFT01: pln = MKPLAN_RDFT(P, &padt, apply_ro01); break; + case RODFT10: pln = MKPLAN_RDFT(P, &padt, apply_ro10); break; + default: A(0); return (plan*)0; + } + + pln->n = n; + pln->is = p->sz->dims[0].is; + pln->os = p->sz->dims[0].os; + pln->cld = cld; + pln->td = 0; + pln->kind = p->kind[0]; + + X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); + + X(ops_zero)(&ops); + ops.other = 4 + (n-1)/2 * 10 + (1 - n % 2) * 5; + if (p->kind[0] == REDFT01 || p->kind[0] == RODFT01) { + ops.add = (n-1)/2 * 6; + ops.mul = (n-1)/2 * 4 + (1 - n % 2) * 2; + } + else { /* 10 transforms */ + ops.add = (n-1)/2 * 2; + ops.mul = 1 + (n-1)/2 * 6 + (1 - n % 2) * 2; + } + + X(ops_zero)(&pln->super.super.ops); + X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); + + return &(pln->super.super); +} + +/* constructor */ +static solver *mksolver(void) +{ + static const solver_adt sadt = { mkplan }; + S *slv = MKSOLVER(S, &sadt); + return &(slv->super); +} + +void X(reodft010e_r2hc_register)(planner *p) +{ + REGISTER_SOLVER(p, mksolver()); +} diff --git a/src/fftw3/reodft/reodft11e-r2hc-odd.c b/src/fftw3/reodft/reodft11e-r2hc-odd.c new file mode 100644 index 0000000..471f7ca --- /dev/null +++ b/src/fftw3/reodft/reodft11e-r2hc-odd.c @@ -0,0 +1,304 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* $Id: reodft11e-r2hc-odd.c,v 1.1 2008/10/17 06:13:18 scuri Exp $ */ + +/* Do an R{E,O}DFT11 problem via an R2HC problem of the same *odd* size, + with some permutations and post-processing, as described in: + + S. C. Chan and K. L. Ho, "Fast algorithms for computing the + discrete cosine transform," IEEE Trans. Circuits Systems II: + Analog & Digital Sig. Proc. 39 (3), 185--190 (1992). + + (For even sizes, see reodft11e-radix2.c.) + + This algorithm is related to the 8 x n prime-factor-algorithm (PFA) + decomposition of the size 8n "logical" DFT corresponding to the + R{EO}DFT11. + + Aside from very confusing notation (several symbols are redefined + from one line to the next), be aware that this paper has some + errors. In particular, the signs are wrong in Eqs. (34-35). Also, + Eqs. (36-37) should be simply C(k) = C(2k + 1 mod N), and similarly + for S (or, equivalently, the second cases should have 2*N - 2*k - 1 + instead of N - k - 1). Note also that in their definition of the + DFT, similarly to FFTW's, the exponent's sign is -1, but they + forgot to correspondingly multiply S (the sine terms) by -1. +*/ + +#include "reodft.h" + +typedef struct { + solver super; +} S; + +typedef struct { + plan_rdft super; + plan *cld; + int is, os; + int n; + int vl; + int ivs, ovs; + rdft_kind kind; +} P; + +static DK(SQRT2, +1.4142135623730950488016887242096980785696718753769); + +#define SGN_SET(x, i) ((i) % 2 ? -(x) : (x)) + +static void apply_re11(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n, n2 = n/2; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + { + int m; + for (i = 0, m = n2; m < n; ++i, m += 4) + buf[i] = I[is * m]; + for (; m < 2 * n; ++i, m += 4) + buf[i] = -I[is * (2*n - m - 1)]; + for (; m < 3 * n; ++i, m += 4) + buf[i] = -I[is * (m - 2*n)]; + for (; m < 4 * n; ++i, m += 4) + buf[i] = I[is * (4*n - m - 1)]; + m -= 4 * n; + for (; i < n; ++i, m += 4) + buf[i] = I[is * m]; + } + + { /* child plan: R2HC of size n */ + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + /* FIXME: strength-reduce loop by 4 to eliminate ugly sgn_set? */ + for (i = 0; i + i + 1 < n2; ++i) { + int k = i + i + 1; + E c1, s1; + E c2, s2; + c1 = buf[k]; + c2 = buf[k + 1]; + s2 = buf[n - (k + 1)]; + s1 = buf[n - k]; + + O[os * i] = SQRT2 * (SGN_SET(c1, (i+1)/2) + + SGN_SET(s1, i/2)); + O[os * (n - (i+1))] = SQRT2 * (SGN_SET(c1, (n-i)/2) - + SGN_SET(s1, (n-(i+1))/2)); + + O[os * (n2 - (i+1))] = SQRT2 * (SGN_SET(c2, (n2-i)/2) - + SGN_SET(s2, (n2-(i+1))/2)); + O[os * (n2 + (i+1))] = SQRT2 * (SGN_SET(c2, (n2+i+2)/2) + + SGN_SET(s2, (n2+(i+1))/2)); + } + if (i + i + 1 == n2) { + E c, s; + c = buf[n2]; + s = buf[n - n2]; + O[os * i] = SQRT2 * (SGN_SET(c, (i+1)/2) + + SGN_SET(s, i/2)); + O[os * (n - (i+1))] = SQRT2 * (SGN_SET(c, (i+2)/2) + + SGN_SET(s, (i+1)/2)); + } + O[os * n2] = SQRT2 * SGN_SET(buf[0], (n2+1)/2); + } + + X(ifree)(buf); +} + +/* like for rodft01, rodft11 is obtained from redft11 by + reversing the input and flipping the sign of every other output. */ +static void apply_ro11(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n, n2 = n/2; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + { + int m; + for (i = 0, m = n2; m < n; ++i, m += 4) + buf[i] = I[is * (n - 1 - m)]; + for (; m < 2 * n; ++i, m += 4) + buf[i] = -I[is * (m - n)]; + for (; m < 3 * n; ++i, m += 4) + buf[i] = -I[is * (3*n - 1 - m)]; + for (; m < 4 * n; ++i, m += 4) + buf[i] = I[is * (m - 3*n)]; + m -= 4 * n; + for (; i < n; ++i, m += 4) + buf[i] = I[is * (n - 1 - m)]; + } + + { /* child plan: R2HC of size n */ + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + /* FIXME: strength-reduce loop by 4 to eliminate ugly sgn_set? */ + for (i = 0; i + i + 1 < n2; ++i) { + int k = i + i + 1; + int j; + E c1, s1; + E c2, s2; + c1 = buf[k]; + c2 = buf[k + 1]; + s2 = buf[n - (k + 1)]; + s1 = buf[n - k]; + + O[os * i] = SQRT2 * (SGN_SET(c1, (i+1)/2 + i) + + SGN_SET(s1, i/2 + i)); + O[os * (n - (i+1))] = SQRT2 * (SGN_SET(c1, (n-i)/2 + i) - + SGN_SET(s1, (n-(i+1))/2 + i)); + + j = n2 - (i+1); + O[os * j] = SQRT2 * (SGN_SET(c2, (n2-i)/2 + j) - + SGN_SET(s2, (n2-(i+1))/2 + j)); + O[os * (n2 + (i+1))] = SQRT2 * (SGN_SET(c2, (n2+i+2)/2 + j) + + SGN_SET(s2, (n2+(i+1))/2 + j)); + } + if (i + i + 1 == n2) { + E c, s; + c = buf[n2]; + s = buf[n - n2]; + O[os * i] = SQRT2 * (SGN_SET(c, (i+1)/2 + i) + + SGN_SET(s, i/2 + i)); + O[os * (n - (i+1))] = SQRT2 * (SGN_SET(c, (i+2)/2 + i) + + SGN_SET(s, (i+1)/2 + i)); + } + O[os * n2] = SQRT2 * SGN_SET(buf[0], (n2+1)/2 + n2); + } + + X(ifree)(buf); +} + +static void awake(plan *ego_, int flg) +{ + P *ego = (P *) ego_; + AWAKE(ego->cld, flg); +} + +static void destroy(plan *ego_) +{ + P *ego = (P *) ego_; + X(plan_destroy_internal)(ego->cld); +} + +static void print(const plan *ego_, printer *p) +{ + const P *ego = (const P *) ego_; + p->print(p, "(%se-r2hc-odd-%d%v%(%p%))", + X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld); +} + +static int applicable0(const solver *ego_, const problem *p_) +{ + UNUSED(ego_); + if (RDFTP(p_)) { + const problem_rdft *p = (const problem_rdft *) p_; + return (1 + && p->sz->rnk == 1 + && p->vecsz->rnk <= 1 + && p->sz->dims[0].n % 2 == 1 + && (p->kind[0] == REDFT11 || p->kind[0] == RODFT11) + ); + } + + return 0; +} + +static int applicable(const solver *ego, const problem *p, const planner *plnr) +{ + return (!NO_UGLYP(plnr) && applicable0(ego, p)); +} + +static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) +{ + P *pln; + const problem_rdft *p; + plan *cld; + R *buf; + int n; + opcnt ops; + + static const plan_adt padt = { + X(rdft_solve), awake, print, destroy + }; + + if (!applicable(ego_, p_, plnr)) + return (plan *)0; + + p = (const problem_rdft *) p_; + + n = p->sz->dims[0].n; + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1), + X(mktensor_0d)(), + buf, buf, R2HC)); + X(ifree)(buf); + if (!cld) + return (plan *)0; + + pln = MKPLAN_RDFT(P, &padt, p->kind[0]==REDFT11 ? apply_re11:apply_ro11); + pln->n = n; + pln->is = p->sz->dims[0].is; + pln->os = p->sz->dims[0].os; + pln->cld = cld; + pln->kind = p->kind[0]; + + X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); + + X(ops_zero)(&ops); + ops.add = n - 1; + ops.mul = n; + ops.other = 4*n; + + X(ops_zero)(&pln->super.super.ops); + X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); + + return &(pln->super.super); +} + +/* constructor */ +static solver *mksolver(void) +{ + static const solver_adt sadt = { mkplan }; + S *slv = MKSOLVER(S, &sadt); + return &(slv->super); +} + +void X(reodft11e_r2hc_odd_register)(planner *p) +{ + REGISTER_SOLVER(p, mksolver()); +} diff --git a/src/fftw3/reodft/reodft11e-r2hc.c b/src/fftw3/reodft/reodft11e-r2hc.c new file mode 100644 index 0000000..d4366e3 --- /dev/null +++ b/src/fftw3/reodft/reodft11e-r2hc.c @@ -0,0 +1,295 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* $Id: reodft11e-r2hc.c,v 1.1 2008/10/17 06:13:18 scuri Exp $ */ + +/* Do an R{E,O}DFT11 problem via an R2HC problem, with some + pre/post-processing ala FFTPACK. Use a trick from: + + S. C. Chan and K. L. Ho, "Direct methods for computing discrete + sinusoidal transforms," IEE Proceedings F 137 (6), 433--442 (1990). + + to re-express as an REDFT01 (DCT-III) problem. + + NOTE: We no longer use this algorithm, because it turns out to suffer + a catastrophic loss of accuracy for certain inputs, apparently because + its post-processing multiplies the output by a cosine. Near the zero + of the cosine, the REDFT01 must produce a near-singular output. +*/ + +#include "reodft.h" + +typedef struct { + solver super; +} S; + +typedef struct { + plan_rdft super; + plan *cld; + twid *td, *td2; + int is, os; + int n; + int vl; + int ivs, ovs; + rdft_kind kind; +} P; + +static void apply_re11(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W; + R *buf; + E cur; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + /* I wish that this didn't require an extra pass. */ + /* FIXME: use recursive/cascade summation for better stability? */ + buf[n - 1] = cur = K(2.0) * I[is * (n - 1)]; + for (i = n - 1; i > 0; --i) { + E curnew; + buf[(i - 1)] = curnew = K(2.0) * I[is * (i - 1)] - cur; + cur = curnew; + } + + W = ego->td->W; + for (i = 1; i < n - i; ++i) { + E a, b, apb, amb, wa, wb; + a = buf[i]; + b = buf[n - i]; + apb = a + b; + amb = a - b; + wa = W[2*i]; + wb = W[2*i + 1]; + buf[i] = wa * amb + wb * apb; + buf[n - i] = wa * apb - wb * amb; + } + if (i == n - i) { + buf[i] = K(2.0) * buf[i] * W[2*i]; + } + + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + W = ego->td2->W; + O[0] = W[0] * buf[0]; + for (i = 1; i < n - i; ++i) { + E a, b; + int k; + a = buf[i]; + b = buf[n - i]; + k = i + i; + O[os * (k - 1)] = W[k - 1] * (a - b); + O[os * k] = W[k] * (a + b); + } + if (i == n - i) { + O[os * (n - 1)] = W[n - 1] * buf[i]; + } + } + + X(ifree)(buf); +} + +/* like for rodft01, rodft11 is obtained from redft11 by + reversing the input and flipping the sign of every other output. */ +static void apply_ro11(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W; + R *buf; + E cur; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + /* I wish that this didn't require an extra pass. */ + /* FIXME: use recursive/cascade summation for better stability? */ + buf[n - 1] = cur = K(2.0) * I[0]; + for (i = n - 1; i > 0; --i) { + E curnew; + buf[(i - 1)] = curnew = K(2.0) * I[is * (n - i)] - cur; + cur = curnew; + } + + W = ego->td->W; + for (i = 1; i < n - i; ++i) { + E a, b, apb, amb, wa, wb; + a = buf[i]; + b = buf[n - i]; + apb = a + b; + amb = a - b; + wa = W[2*i]; + wb = W[2*i + 1]; + buf[i] = wa * amb + wb * apb; + buf[n - i] = wa * apb - wb * amb; + } + if (i == n - i) { + buf[i] = K(2.0) * buf[i] * W[2*i]; + } + + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + W = ego->td2->W; + O[0] = W[0] * buf[0]; + for (i = 1; i < n - i; ++i) { + E a, b; + int k; + a = buf[i]; + b = buf[n - i]; + k = i + i; + O[os * (k - 1)] = W[k - 1] * (b - a); + O[os * k] = W[k] * (a + b); + } + if (i == n - i) { + O[os * (n - 1)] = -W[n - 1] * buf[i]; + } + } + + X(ifree)(buf); +} + +static void awake(plan *ego_, int flg) +{ + P *ego = (P *) ego_; + static const tw_instr reodft010e_tw[] = { + { TW_COS, 0, 1 }, + { TW_SIN, 0, 1 }, + { TW_NEXT, 1, 0 } + }; + static const tw_instr reodft11e_tw[] = { + { TW_COS, 1, 1 }, + { TW_NEXT, 2, 0 } + }; + + AWAKE(ego->cld, flg); + + X(twiddle_awake)(flg, &ego->td, reodft010e_tw, 4*ego->n, 1, ego->n/2+1); + X(twiddle_awake)(flg, &ego->td2, reodft11e_tw, 8*ego->n, 1, ego->n * 2); +} + +static void destroy(plan *ego_) +{ + P *ego = (P *) ego_; + X(plan_destroy_internal)(ego->cld); +} + +static void print(const plan *ego_, printer *p) +{ + const P *ego = (const P *) ego_; + p->print(p, "(%se-r2hc-%d%v%(%p%))", + X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld); +} + +static int applicable0(const solver *ego_, const problem *p_) +{ + UNUSED(ego_); + if (RDFTP(p_)) { + const problem_rdft *p = (const problem_rdft *) p_; + return (1 + && p->sz->rnk == 1 + && p->vecsz->rnk <= 1 + && (p->kind[0] == REDFT11 || p->kind[0] == RODFT11) + ); + } + + return 0; +} + +static int applicable(const solver *ego, const problem *p, const planner *plnr) +{ + return (!NO_UGLYP(plnr) && applicable0(ego, p)); +} + +static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) +{ + P *pln; + const problem_rdft *p; + plan *cld; + R *buf; + int n; + opcnt ops; + + static const plan_adt padt = { + X(rdft_solve), awake, print, destroy + }; + + if (!applicable(ego_, p_, plnr)) + return (plan *)0; + + p = (const problem_rdft *) p_; + + n = p->sz->dims[0].n; + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1), + X(mktensor_0d)(), + buf, buf, R2HC)); + X(ifree)(buf); + if (!cld) + return (plan *)0; + + pln = MKPLAN_RDFT(P, &padt, p->kind[0]==REDFT11 ? apply_re11:apply_ro11); + pln->n = n; + pln->is = p->sz->dims[0].is; + pln->os = p->sz->dims[0].os; + pln->cld = cld; + pln->td = pln->td2 = 0; + pln->kind = p->kind[0]; + + X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); + + X(ops_zero)(&ops); + ops.other = 5 + (n-1) * 2 + (n-1)/2 * 12 + (1 - n % 2) * 6; + ops.add = (n - 1) * 1 + (n-1)/2 * 6; + ops.mul = 2 + (n-1) * 1 + (n-1)/2 * 6 + (1 - n % 2) * 3; + + X(ops_zero)(&pln->super.super.ops); + X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); + + return &(pln->super.super); +} + +/* constructor */ +static solver *mksolver(void) +{ + static const solver_adt sadt = { mkplan }; + S *slv = MKSOLVER(S, &sadt); + return &(slv->super); +} + +void X(reodft11e_r2hc_register)(planner *p) +{ + REGISTER_SOLVER(p, mksolver()); +} diff --git a/src/fftw3/reodft/reodft11e-radix2.c b/src/fftw3/reodft/reodft11e-radix2.c new file mode 100644 index 0000000..674f7b4 --- /dev/null +++ b/src/fftw3/reodft/reodft11e-radix2.c @@ -0,0 +1,515 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* $Id: reodft11e-radix2.c,v 1.1 2008/10/17 06:13:18 scuri Exp $ */ + +/* Do an R{E,O}DFT11 problem of *even* size by a pair of R2HC problems + of half the size, plus some pre/post-processing. Use a trick from: + + Zhongde Wang, "On computing the discrete Fourier and cosine transforms," + IEEE Trans. Acoust. Speech Sig. Proc. ASSP-33 (4), 1341--1344 (1985). + + to re-express as a pair of half-size REDFT01 (DCT-III) problems. Our + implementation looks quite a bit different from the algorithm described + in the paper because we combined the paper's pre/post-processing with + the pre/post-processing used to turn REDFT01 into R2HC. (Also, the + paper uses a DCT/DST pair, but we turn the DST into a DCT via the + usual reordering/sign-flip trick. We additionally combined a couple + of the matrices/transformations of the paper into a single pass.) + + NOTE: We originally used a simpler method by S. C. Chan and K. L. Ho + that turned out to have numerical problems; see reodft11e-r2hc.c. + + (For odd sizes, see reodft11e-r2hc-odd.c.) +*/ + +#include "reodft.h" + +typedef struct { + solver super; +} S; + +typedef struct { + plan_rdft super; + plan *cld; + twid *td, *td2; + int is, os; + int n; + int vl; + int ivs, ovs; + rdft_kind kind; +} P; + +static void apply_re11(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n, n2 = n/2; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W = ego->td->W; + R *W2; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = K(2.0) * I[0]; + buf[n2] = K(2.0) * I[is * (n - 1)]; + for (i = 1; i + i < n2; ++i) { + int k = i + i; + E a, b, a2, b2; + { + E u, v; + u = I[is * (k - 1)]; + v = I[is * k]; + a = u + v; + b2 = u - v; + } + { + E u, v; + u = I[is * (n - k - 1)]; + v = I[is * (n - k)]; + b = u + v; + a2 = u - v; + } + { + E wa, wb; + wa = W[2*i]; + wb = W[2*i + 1]; + { + E apb, amb; + apb = a + b; + amb = a - b; + buf[i] = wa * amb + wb * apb; + buf[n2 - i] = wa * apb - wb * amb; + } + { + E apb, amb; + apb = a2 + b2; + amb = a2 - b2; + buf[n2 + i] = wa * amb + wb * apb; + buf[n - i] = wa * apb - wb * amb; + } + } + } + if (i + i == n2) { + E u, v; + u = I[is * (n2 - 1)]; + v = I[is * n2]; + buf[i] = K(2.0) * (u + v) * W[2*i]; + buf[n - i] = K(2.0) * (u - v) * W[2*i]; + } + + + /* child plan: two r2hc's of size n/2 */ + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + W2 = ego->td2->W; + { /* i == 0 case */ + E wa, wb; + E a, b; + wa = W2[0]; /* cos */ + wb = W2[1]; /* sin */ + a = buf[0]; + b = buf[n2]; + O[0] = wa * a + wb * b; + O[os * (n - 1)] = wb * a - wa * b; + } + W2 += 2; + for (i = 1; i + i < n2; ++i, W2 += 2) { + int k; + E u, v, u2, v2; + u = buf[i]; + v = buf[n2 - i]; + u2 = buf[n2 + i]; + v2 = buf[n - i]; + k = (i + i) - 1; + { + E wa, wb; + E a, b; + wa = W2[0]; /* cos */ + wb = W2[1]; /* sin */ + a = u - v; + b = v2 - u2; + O[os * k] = wa * a + wb * b; + O[os * (n - 1 - k)] = wb * a - wa * b; + } + ++k; + W2 += 2; + { + E wa, wb; + E a, b; + wa = W2[0]; /* cos */ + wb = W2[1]; /* sin */ + a = u + v; + b = u2 + v2; + O[os * k] = wa * a + wb * b; + O[os * (n - 1 - k)] = wb * a - wa * b; + } + } + if (i + i == n2) { + int k = (i + i) - 1; + E wa, wb; + E a, b; + wa = W2[0]; /* cos */ + wb = W2[1]; /* sin */ + a = buf[i]; + b = buf[n2 + i]; + O[os * k] = wa * a - wb * b; + O[os * (n - 1 - k)] = wb * a + wa * b; + } + } + + X(ifree)(buf); +} + +#if 0 + +/* This version of apply_re11 uses REDFT01 child plans, more similar + to the original paper by Z. Wang. We keep it around for reference + (it is simpler) and because it may become more efficient if we + ever implement REDFT01 codelets. */ + +static void apply_re11(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = K(2.0) * I[0]; + buf[n/2] = K(2.0) * I[is * (n - 1)]; + for (i = 1; i + i < n; ++i) { + int k = i + i; + E a, b; + a = I[is * (k - 1)]; + b = I[is * k]; + buf[i] = a + b; + buf[n - i] = a - b; + } + + /* child plan: two redft01's (DCT-III) */ + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + W = ego->td2->W; + for (i = 0; i + 1 < n/2; ++i, W += 2) { + { + E wa, wb; + E a, b; + wa = W[0]; /* cos */ + wb = W[1]; /* sin */ + a = buf[i]; + b = buf[n/2 + i]; + O[os * i] = wa * a + wb * b; + O[os * (n - 1 - i)] = wb * a - wa * b; + } + ++i; + W += 2; + { + E wa, wb; + E a, b; + wa = W[0]; /* cos */ + wb = W[1]; /* sin */ + a = buf[i]; + b = buf[n/2 + i]; + O[os * i] = wa * a - wb * b; + O[os * (n - 1 - i)] = wb * a + wa * b; + } + } + if (i < n/2) { + E wa, wb; + E a, b; + wa = W[0]; /* cos */ + wb = W[1]; /* sin */ + a = buf[i]; + b = buf[n/2 + i]; + O[os * i] = wa * a + wb * b; + O[os * (n - 1 - i)] = wb * a - wa * b; + } + } + + X(ifree)(buf); +} + +#endif /* 0 */ + +/* like for rodft01, rodft11 is obtained from redft11 by + reversing the input and flipping the sign of every other output. */ +static void apply_ro11(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n, n2 = n/2; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W = ego->td->W; + R *W2; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = K(2.0) * I[is * (n - 1)]; + buf[n2] = K(2.0) * I[0]; + for (i = 1; i + i < n2; ++i) { + int k = i + i; + E a, b, a2, b2; + { + E u, v; + u = I[is * (n - k)]; + v = I[is * (n - 1 - k)]; + a = u + v; + b2 = u - v; + } + { + E u, v; + u = I[is * (k)]; + v = I[is * (k - 1)]; + b = u + v; + a2 = u - v; + } + { + E wa, wb; + wa = W[2*i]; + wb = W[2*i + 1]; + { + E apb, amb; + apb = a + b; + amb = a - b; + buf[i] = wa * amb + wb * apb; + buf[n2 - i] = wa * apb - wb * amb; + } + { + E apb, amb; + apb = a2 + b2; + amb = a2 - b2; + buf[n2 + i] = wa * amb + wb * apb; + buf[n - i] = wa * apb - wb * amb; + } + } + } + if (i + i == n2) { + E u, v; + u = I[is * n2]; + v = I[is * (n2 - 1)]; + buf[i] = K(2.0) * (u + v) * W[2*i]; + buf[n - i] = K(2.0) * (u - v) * W[2*i]; + } + + + /* child plan: two r2hc's of size n/2 */ + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + W2 = ego->td2->W; + { /* i == 0 case */ + E wa, wb; + E a, b; + wa = W2[0]; /* cos */ + wb = W2[1]; /* sin */ + a = buf[0]; + b = buf[n2]; + O[0] = wa * a + wb * b; + O[os * (n - 1)] = wa * b - wb * a; + } + W2 += 2; + for (i = 1; i + i < n2; ++i, W2 += 2) { + int k; + E u, v, u2, v2; + u = buf[i]; + v = buf[n2 - i]; + u2 = buf[n2 + i]; + v2 = buf[n - i]; + k = (i + i) - 1; + { + E wa, wb; + E a, b; + wa = W2[0]; /* cos */ + wb = W2[1]; /* sin */ + a = v - u; + b = u2 - v2; + O[os * k] = wa * a + wb * b; + O[os * (n - 1 - k)] = wa * b - wb * a; + } + ++k; + W2 += 2; + { + E wa, wb; + E a, b; + wa = W2[0]; /* cos */ + wb = W2[1]; /* sin */ + a = u + v; + b = u2 + v2; + O[os * k] = wa * a + wb * b; + O[os * (n - 1 - k)] = wa * b - wb * a; + } + } + if (i + i == n2) { + int k = (i + i) - 1; + E wa, wb; + E a, b; + wa = W2[0]; /* cos */ + wb = W2[1]; /* sin */ + a = buf[i]; + b = buf[n2 + i]; + O[os * k] = wb * b - wa * a; + O[os * (n - 1 - k)] = wa * b + wb * a; + } + } + + X(ifree)(buf); +} + +static void awake(plan *ego_, int flg) +{ + P *ego = (P *) ego_; + static const tw_instr reodft010e_tw[] = { + { TW_COS, 0, 1 }, + { TW_SIN, 0, 1 }, + { TW_NEXT, 1, 0 } + }; + static const tw_instr reodft11e_tw[] = { + { TW_COS, 1, 1 }, + { TW_SIN, 1, 1 }, + { TW_NEXT, 2, 0 } + }; + + AWAKE(ego->cld, flg); + + X(twiddle_awake)(flg, &ego->td, reodft010e_tw, 2*ego->n, 1, ego->n/4+1); + X(twiddle_awake)(flg, &ego->td2, reodft11e_tw, 8*ego->n, 1, ego->n); +} + +static void destroy(plan *ego_) +{ + P *ego = (P *) ego_; + X(plan_destroy_internal)(ego->cld); +} + +static void print(const plan *ego_, printer *p) +{ + const P *ego = (const P *) ego_; + p->print(p, "(%se-radix2-r2hc-%d%v%(%p%))", + X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld); +} + +static int applicable0(const solver *ego_, const problem *p_) +{ + UNUSED(ego_); + if (RDFTP(p_)) { + const problem_rdft *p = (const problem_rdft *) p_; + return (1 + && p->sz->rnk == 1 + && p->vecsz->rnk <= 1 + && p->sz->dims[0].n % 2 == 0 + && (p->kind[0] == REDFT11 || p->kind[0] == RODFT11) + ); + } + + return 0; +} + +static int applicable(const solver *ego, const problem *p, const planner *plnr) +{ + return (!NO_UGLYP(plnr) && applicable0(ego, p)); +} + +static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) +{ + P *pln; + const problem_rdft *p; + plan *cld; + R *buf; + int n; + opcnt ops; + + static const plan_adt padt = { + X(rdft_solve), awake, print, destroy + }; + + if (!applicable(ego_, p_, plnr)) + return (plan *)0; + + p = (const problem_rdft *) p_; + + n = p->sz->dims[0].n; + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n/2, 1, 1), + X(mktensor_1d)(2, n/2, n/2), + buf, buf, R2HC)); + X(ifree)(buf); + if (!cld) + return (plan *)0; + + pln = MKPLAN_RDFT(P, &padt, p->kind[0]==REDFT11 ? apply_re11:apply_ro11); + pln->n = n; + pln->is = p->sz->dims[0].is; + pln->os = p->sz->dims[0].os; + pln->cld = cld; + pln->td = pln->td2 = 0; + pln->kind = p->kind[0]; + + X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); + + X(ops_zero)(&ops); + ops.add = 2 + (n/2 - 1)/2 * 20; + ops.mul = 6 + (n/2 - 1)/2 * 16; + ops.other = 4*n + 2 + (n/2 - 1)/2 * 6; + if ((n/2) % 2 == 0) { + ops.add += 4; + ops.mul += 8; + ops.other += 4; + } + + X(ops_zero)(&pln->super.super.ops); + X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); + + return &(pln->super.super); +} + +/* constructor */ +static solver *mksolver(void) +{ + static const solver_adt sadt = { mkplan }; + S *slv = MKSOLVER(S, &sadt); + return &(slv->super); +} + +void X(reodft11e_radix2_r2hc_register)(planner *p) +{ + REGISTER_SOLVER(p, mksolver()); +} diff --git a/src/fftw3/reodft/rodft00e-r2hc-pad.c b/src/fftw3/reodft/rodft00e-r2hc-pad.c new file mode 100644 index 0000000..0b48585 --- /dev/null +++ b/src/fftw3/reodft/rodft00e-r2hc-pad.c @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* $Id: rodft00e-r2hc-pad.c,v 1.1 2008/10/17 06:13:18 scuri Exp $ */ + +/* Do a RODFT00 problem via an R2HC problem, padded antisymmetrically to + twice the size. This is asymptotically a factor of ~2 worse than + rodft00e-r2hc.c (the algorithm used in e.g. FFTPACK and Numerical + Recipes), but we abandoned the latter after we discovered that it + has intrinsic accuracy problems. */ + +#include "reodft.h" + +typedef struct { + solver super; +} S; + +typedef struct { + plan_rdft super; + plan *cld, *cldcpy; + int is; + int n; + int vl; + int ivs, ovs; +} P; + +static void apply(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * (2*n), BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = 0.0; + for (i = 1; i < n; ++i) { + R a = I[(i-1) * is]; + buf[i] = -a; + buf[2*n - i] = a; + } + buf[i] = 0.0; /* i == n, Nyquist */ + + /* r2hc transform of size 2*n */ + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + /* copy n-1 real numbers (imag. parts of hc array) from buf to O */ + { + plan_rdft *cldcpy = (plan_rdft *) ego->cldcpy; + cldcpy->apply((plan *) cldcpy, buf+2*n-1, O); + } + } + + X(ifree)(buf); +} + +static void awake(plan *ego_, int flg) +{ + P *ego = (P *) ego_; + AWAKE(ego->cld, flg); + AWAKE(ego->cldcpy, flg); +} + +static void destroy(plan *ego_) +{ + P *ego = (P *) ego_; + X(plan_destroy_internal)(ego->cldcpy); + X(plan_destroy_internal)(ego->cld); +} + +static void print(const plan *ego_, printer *p) +{ + const P *ego = (const P *) ego_; + p->print(p, "(rodft00e-r2hc-pad-%d%v%(%p%)%(%p%))", + ego->n - 1, ego->vl, ego->cld, ego->cldcpy); +} + +static int applicable0(const solver *ego_, const problem *p_) +{ + UNUSED(ego_); + if (RDFTP(p_)) { + const problem_rdft *p = (const problem_rdft *) p_; + return (1 + && p->sz->rnk == 1 + && p->vecsz->rnk <= 1 + && p->kind[0] == RODFT00 + ); + } + + return 0; +} + +static int applicable(const solver *ego, const problem *p, const planner *plnr) +{ + return (!NO_UGLYP(plnr) && applicable0(ego, p)); +} + +static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) +{ + P *pln; + const problem_rdft *p; + plan *cld = (plan *) 0, *cldcpy; + R *buf = (R *) 0; + int n; + int vl, ivs, ovs; + opcnt ops; + + static const plan_adt padt = { + X(rdft_solve), awake, print, destroy + }; + + if (!applicable(ego_, p_, plnr)) + goto nada; + + p = (const problem_rdft *) p_; + + n = p->sz->dims[0].n + 1; + A(n > 0); + buf = (R *) MALLOC(sizeof(R) * (2*n), BUFFERS); + + cld = X(mkplan_d)(plnr,X(mkproblem_rdft_1_d)(X(mktensor_1d)(2*n,1,1), + X(mktensor_0d)(), + buf, buf, R2HC)); + if (!cld) + goto nada; + + X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); + cldcpy = + X(mkplan_d)(plnr, + X(mkproblem_rdft_1_d)(X(mktensor_0d)(), + X(mktensor_1d)(n-1,-1, + p->sz->dims[0].os), + buf+2*n-1,TAINT(p->O, ovs), R2HC)); + if (!cldcpy) + goto nada; + + X(ifree)(buf); + + pln = MKPLAN_RDFT(P, &padt, apply); + + pln->n = n; + pln->is = p->sz->dims[0].is; + pln->cld = cld; + pln->cldcpy = cldcpy; + pln->vl = vl; + pln->ivs = ivs; + pln->ovs = ovs; + + X(ops_zero)(&ops); + ops.other = n-1 + 2*n; /* loads + stores (input -> buf) */ + + X(ops_zero)(&pln->super.super.ops); + X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cldcpy->ops, &pln->super.super.ops); + + return &(pln->super.super); + + nada: + X(ifree0)(buf); + if (cld) + X(plan_destroy_internal)(cld); + return (plan *)0; +} + +/* constructor */ +static solver *mksolver(void) +{ + static const solver_adt sadt = { mkplan }; + S *slv = MKSOLVER(S, &sadt); + return &(slv->super); +} + +void X(rodft00e_r2hc_pad_register)(planner *p) +{ + REGISTER_SOLVER(p, mksolver()); +} diff --git a/src/fftw3/reodft/rodft00e-r2hc.c b/src/fftw3/reodft/rodft00e-r2hc.c new file mode 100644 index 0000000..46bb299 --- /dev/null +++ b/src/fftw3/reodft/rodft00e-r2hc.c @@ -0,0 +1,212 @@ +/* + * Copyright (c) 2003 Matteo Frigo + * Copyright (c) 2003 Massachusetts Institute of Technology + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* $Id: rodft00e-r2hc.c,v 1.1 2008/10/17 06:13:18 scuri Exp $ */ + +/* Do a RODFT00 problem via an R2HC problem, with some pre/post-processing. + + This code uses the trick from FFTPACK, also documented in a similar + form by Numerical Recipes. Unfortunately, this algorithm seems to + have intrinsic numerical problems (similar to those in + reodft11e-r2hc.c), possibly due to the fact that it multiplies its + input by a sine, causing a loss of precision near the zero. For + transforms of 16k points, it has already lost three or four decimal + places of accuracy, which we deem unacceptable. + + So, we have abandoned this algorithm in favor of the one in + rodft00-r2hc-pad.c, which unfortunately sacrifices 30-50% in speed. + The only other alternative in the literature that does not have + similar numerical difficulties seems to be the direct adaptation of + the Cooley-Tukey decomposition for antisymmetric data, but this + would require a whole new set of codelets and it's not clear that + it's worth it at this point. */ + +#include "reodft.h" + +typedef struct { + solver super; +} S; + +typedef struct { + plan_rdft super; + plan *cld; + twid *td; + int is, os; + int n; + int vl; + int ivs, ovs; +} P; + +static void apply(const plan *ego_, R *I, R *O) +{ + const P *ego = (const P *) ego_; + int is = ego->is, os = ego->os; + int i, n = ego->n; + int iv, vl = ego->vl; + int ivs = ego->ivs, ovs = ego->ovs; + R *W = ego->td->W; + R *buf; + + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) { + buf[0] = 0; + for (i = 1; i < n - i; ++i) { + E a, b, apb, amb; + a = I[is * (i - 1)]; + b = I[is * ((n - i) - 1)]; + apb = K(2.0) * W[i] * (a + b); + amb = (a - b); + buf[i] = apb + amb; + buf[n - i] = apb - amb; + } + if (i == n - i) { + buf[i] = K(4.0) * I[is * (i - 1)]; + } + + { + plan_rdft *cld = (plan_rdft *) ego->cld; + cld->apply((plan *) cld, buf, buf); + } + + /* FIXME: use recursive/cascade summation for better stability? */ + O[0] = buf[0] * 0.5; + for (i = 1; i + i < n - 1; ++i) { + int k = i + i; + O[os * (k - 1)] = -buf[n - i]; + O[os * k] = O[os * (k - 2)] + buf[i]; + } + if (i + i == n - 1) { + O[os * (n - 2)] = -buf[n - i]; + } + } + + X(ifree)(buf); +} + +static void awake(plan *ego_, int flg) +{ + P *ego = (P *) ego_; + static const tw_instr rodft00e_tw[] = { + { TW_SIN, 0, 1 }, + { TW_NEXT, 1, 0 } + }; + + AWAKE(ego->cld, flg); + + X(twiddle_awake)(flg, &ego->td, rodft00e_tw, 2*ego->n, 1, (ego->n+1)/2); +} + +static void destroy(plan *ego_) +{ + P *ego = (P *) ego_; + X(plan_destroy_internal)(ego->cld); +} + +static void print(const plan *ego_, printer *p) +{ + const P *ego = (const P *) ego_; + p->print(p, "(rodft00e-r2hc-%d%v%(%p%))", ego->n - 1, ego->vl, ego->cld); +} + +static int applicable0(const solver *ego_, const problem *p_) +{ + UNUSED(ego_); + if (RDFTP(p_)) { + const problem_rdft *p = (const problem_rdft *) p_; + return (1 + && p->sz->rnk == 1 + && p->vecsz->rnk <= 1 + && p->kind[0] == RODFT00 + ); + } + + return 0; +} + +static int applicable(const solver *ego, const problem *p, const planner *plnr) +{ + return (!NO_UGLYP(plnr) && applicable0(ego, p)); +} + +static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) +{ + P *pln; + const problem_rdft *p; + plan *cld; + R *buf; + int n; + opcnt ops; + + static const plan_adt padt = { + X(rdft_solve), awake, print, destroy + }; + + if (!applicable(ego_, p_, plnr)) + return (plan *)0; + + p = (const problem_rdft *) p_; + + n = p->sz->dims[0].n + 1; + buf = (R *) MALLOC(sizeof(R) * n, BUFFERS); + + cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1), + X(mktensor_0d)(), + buf, buf, R2HC)); + X(ifree)(buf); + if (!cld) + return (plan *)0; + + pln = MKPLAN_RDFT(P, &padt, apply); + + pln->n = n; + pln->is = p->sz->dims[0].is; + pln->os = p->sz->dims[0].os; + pln->cld = cld; + pln->td = 0; + + X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); + + X(ops_zero)(&ops); + ops.other = 4 + (n-1)/2 * 5 + (n-2)/2 * 5; + ops.add = (n-1)/2 * 4 + (n-2)/2 * 1; + ops.mul = 1 + (n-1)/2 * 2; + if (n % 2 == 0) + ops.mul += 1; + + X(ops_zero)(&pln->super.super.ops); + X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops); + X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); + + return &(pln->super.super); +} + +/* constructor */ +static solver *mksolver(void) +{ + static const solver_adt sadt = { mkplan }; + S *slv = MKSOLVER(S, &sadt); + return &(slv->super); +} + +void X(rodft00e_r2hc_register)(planner *p) +{ + REGISTER_SOLVER(p, mksolver()); +} |