annotate fft/fftw/fftw-3.3.4/reodft/rodft00e-r2hc.c @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 /*
Chris@19 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 4 *
Chris@19 5 * This program is free software; you can redistribute it and/or modify
Chris@19 6 * it under the terms of the GNU General Public License as published by
Chris@19 7 * the Free Software Foundation; either version 2 of the License, or
Chris@19 8 * (at your option) any later version.
Chris@19 9 *
Chris@19 10 * This program is distributed in the hope that it will be useful,
Chris@19 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 13 * GNU General Public License for more details.
Chris@19 14 *
Chris@19 15 * You should have received a copy of the GNU General Public License
Chris@19 16 * along with this program; if not, write to the Free Software
Chris@19 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 18 *
Chris@19 19 */
Chris@19 20
Chris@19 21
Chris@19 22 /* Do a RODFT00 problem via an R2HC problem, with some pre/post-processing.
Chris@19 23
Chris@19 24 This code uses the trick from FFTPACK, also documented in a similar
Chris@19 25 form by Numerical Recipes. Unfortunately, this algorithm seems to
Chris@19 26 have intrinsic numerical problems (similar to those in
Chris@19 27 reodft11e-r2hc.c), possibly due to the fact that it multiplies its
Chris@19 28 input by a sine, causing a loss of precision near the zero. For
Chris@19 29 transforms of 16k points, it has already lost three or four decimal
Chris@19 30 places of accuracy, which we deem unacceptable.
Chris@19 31
Chris@19 32 So, we have abandoned this algorithm in favor of the one in
Chris@19 33 rodft00-r2hc-pad.c, which unfortunately sacrifices 30-50% in speed.
Chris@19 34 The only other alternative in the literature that does not have
Chris@19 35 similar numerical difficulties seems to be the direct adaptation of
Chris@19 36 the Cooley-Tukey decomposition for antisymmetric data, but this
Chris@19 37 would require a whole new set of codelets and it's not clear that
Chris@19 38 it's worth it at this point. However, we did implement the latter
Chris@19 39 algorithm for the specific case of odd n (logically adapting the
Chris@19 40 split-radix algorithm); see reodft00e-splitradix.c. */
Chris@19 41
Chris@19 42 #include "reodft.h"
Chris@19 43
Chris@19 44 typedef struct {
Chris@19 45 solver super;
Chris@19 46 } S;
Chris@19 47
Chris@19 48 typedef struct {
Chris@19 49 plan_rdft super;
Chris@19 50 plan *cld;
Chris@19 51 twid *td;
Chris@19 52 INT is, os;
Chris@19 53 INT n;
Chris@19 54 INT vl;
Chris@19 55 INT ivs, ovs;
Chris@19 56 } P;
Chris@19 57
Chris@19 58 static void apply(const plan *ego_, R *I, R *O)
Chris@19 59 {
Chris@19 60 const P *ego = (const P *) ego_;
Chris@19 61 INT is = ego->is, os = ego->os;
Chris@19 62 INT i, n = ego->n;
Chris@19 63 INT iv, vl = ego->vl;
Chris@19 64 INT ivs = ego->ivs, ovs = ego->ovs;
Chris@19 65 R *W = ego->td->W;
Chris@19 66 R *buf;
Chris@19 67
Chris@19 68 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
Chris@19 69
Chris@19 70 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
Chris@19 71 buf[0] = 0;
Chris@19 72 for (i = 1; i < n - i; ++i) {
Chris@19 73 E a, b, apb, amb;
Chris@19 74 a = I[is * (i - 1)];
Chris@19 75 b = I[is * ((n - i) - 1)];
Chris@19 76 apb = K(2.0) * W[i] * (a + b);
Chris@19 77 amb = (a - b);
Chris@19 78 buf[i] = apb + amb;
Chris@19 79 buf[n - i] = apb - amb;
Chris@19 80 }
Chris@19 81 if (i == n - i) {
Chris@19 82 buf[i] = K(4.0) * I[is * (i - 1)];
Chris@19 83 }
Chris@19 84
Chris@19 85 {
Chris@19 86 plan_rdft *cld = (plan_rdft *) ego->cld;
Chris@19 87 cld->apply((plan *) cld, buf, buf);
Chris@19 88 }
Chris@19 89
Chris@19 90 /* FIXME: use recursive/cascade summation for better stability? */
Chris@19 91 O[0] = buf[0] * 0.5;
Chris@19 92 for (i = 1; i + i < n - 1; ++i) {
Chris@19 93 INT k = i + i;
Chris@19 94 O[os * (k - 1)] = -buf[n - i];
Chris@19 95 O[os * k] = O[os * (k - 2)] + buf[i];
Chris@19 96 }
Chris@19 97 if (i + i == n - 1) {
Chris@19 98 O[os * (n - 2)] = -buf[n - i];
Chris@19 99 }
Chris@19 100 }
Chris@19 101
Chris@19 102 X(ifree)(buf);
Chris@19 103 }
Chris@19 104
Chris@19 105 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@19 106 {
Chris@19 107 P *ego = (P *) ego_;
Chris@19 108 static const tw_instr rodft00e_tw[] = {
Chris@19 109 { TW_SIN, 0, 1 },
Chris@19 110 { TW_NEXT, 1, 0 }
Chris@19 111 };
Chris@19 112
Chris@19 113 X(plan_awake)(ego->cld, wakefulness);
Chris@19 114
Chris@19 115 X(twiddle_awake)(wakefulness,
Chris@19 116 &ego->td, rodft00e_tw, 2*ego->n, 1, (ego->n+1)/2);
Chris@19 117 }
Chris@19 118
Chris@19 119 static void destroy(plan *ego_)
Chris@19 120 {
Chris@19 121 P *ego = (P *) ego_;
Chris@19 122 X(plan_destroy_internal)(ego->cld);
Chris@19 123 }
Chris@19 124
Chris@19 125 static void print(const plan *ego_, printer *p)
Chris@19 126 {
Chris@19 127 const P *ego = (const P *) ego_;
Chris@19 128 p->print(p, "(rodft00e-r2hc-%D%v%(%p%))", ego->n - 1, ego->vl, ego->cld);
Chris@19 129 }
Chris@19 130
Chris@19 131 static int applicable0(const solver *ego_, const problem *p_)
Chris@19 132 {
Chris@19 133 const problem_rdft *p = (const problem_rdft *) p_;
Chris@19 134 UNUSED(ego_);
Chris@19 135
Chris@19 136 return (1
Chris@19 137 && p->sz->rnk == 1
Chris@19 138 && p->vecsz->rnk <= 1
Chris@19 139 && p->kind[0] == RODFT00
Chris@19 140 );
Chris@19 141 }
Chris@19 142
Chris@19 143 static int applicable(const solver *ego, const problem *p, const planner *plnr)
Chris@19 144 {
Chris@19 145 return (!NO_SLOWP(plnr) && applicable0(ego, p));
Chris@19 146 }
Chris@19 147
Chris@19 148 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@19 149 {
Chris@19 150 P *pln;
Chris@19 151 const problem_rdft *p;
Chris@19 152 plan *cld;
Chris@19 153 R *buf;
Chris@19 154 INT n;
Chris@19 155 opcnt ops;
Chris@19 156
Chris@19 157 static const plan_adt padt = {
Chris@19 158 X(rdft_solve), awake, print, destroy
Chris@19 159 };
Chris@19 160
Chris@19 161 if (!applicable(ego_, p_, plnr))
Chris@19 162 return (plan *)0;
Chris@19 163
Chris@19 164 p = (const problem_rdft *) p_;
Chris@19 165
Chris@19 166 n = p->sz->dims[0].n + 1;
Chris@19 167 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
Chris@19 168
Chris@19 169 cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1),
Chris@19 170 X(mktensor_0d)(),
Chris@19 171 buf, buf, R2HC));
Chris@19 172 X(ifree)(buf);
Chris@19 173 if (!cld)
Chris@19 174 return (plan *)0;
Chris@19 175
Chris@19 176 pln = MKPLAN_RDFT(P, &padt, apply);
Chris@19 177
Chris@19 178 pln->n = n;
Chris@19 179 pln->is = p->sz->dims[0].is;
Chris@19 180 pln->os = p->sz->dims[0].os;
Chris@19 181 pln->cld = cld;
Chris@19 182 pln->td = 0;
Chris@19 183
Chris@19 184 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
Chris@19 185
Chris@19 186 X(ops_zero)(&ops);
Chris@19 187 ops.other = 4 + (n-1)/2 * 5 + (n-2)/2 * 5;
Chris@19 188 ops.add = (n-1)/2 * 4 + (n-2)/2 * 1;
Chris@19 189 ops.mul = 1 + (n-1)/2 * 2;
Chris@19 190 if (n % 2 == 0)
Chris@19 191 ops.mul += 1;
Chris@19 192
Chris@19 193 X(ops_zero)(&pln->super.super.ops);
Chris@19 194 X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops);
Chris@19 195 X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops);
Chris@19 196
Chris@19 197 return &(pln->super.super);
Chris@19 198 }
Chris@19 199
Chris@19 200 /* constructor */
Chris@19 201 static solver *mksolver(void)
Chris@19 202 {
Chris@19 203 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
Chris@19 204 S *slv = MKSOLVER(S, &sadt);
Chris@19 205 return &(slv->super);
Chris@19 206 }
Chris@19 207
Chris@19 208 void X(rodft00e_r2hc_register)(planner *p)
Chris@19 209 {
Chris@19 210 REGISTER_SOLVER(p, mksolver());
Chris@19 211 }