annotate fft/fftw/fftw-3.3.4/reodft/redft00e-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 REDFT00 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 cosine, 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 redft00-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 symmetric data, but this would
Chris@19 37 require a whole new set of codelets and it's not clear that it's
Chris@19 38 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 E csum;
Chris@19 68
Chris@19 69 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
Chris@19 70
Chris@19 71 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
Chris@19 72 buf[0] = I[0] + I[is * n];
Chris@19 73 csum = I[0] - I[is * n];
Chris@19 74 for (i = 1; i < n - i; ++i) {
Chris@19 75 E a, b, apb, amb;
Chris@19 76 a = I[is * i];
Chris@19 77 b = I[is * (n - i)];
Chris@19 78 csum += W[2*i] * (amb = K(2.0)*(a - b));
Chris@19 79 amb = W[2*i+1] * amb;
Chris@19 80 apb = (a + b);
Chris@19 81 buf[i] = apb - amb;
Chris@19 82 buf[n - i] = apb + amb;
Chris@19 83 }
Chris@19 84 if (i == n - i) {
Chris@19 85 buf[i] = K(2.0) * I[is * i];
Chris@19 86 }
Chris@19 87
Chris@19 88 {
Chris@19 89 plan_rdft *cld = (plan_rdft *) ego->cld;
Chris@19 90 cld->apply((plan *) cld, buf, buf);
Chris@19 91 }
Chris@19 92
Chris@19 93 /* FIXME: use recursive/cascade summation for better stability? */
Chris@19 94 O[0] = buf[0];
Chris@19 95 O[os] = csum;
Chris@19 96 for (i = 1; i + i < n; ++i) {
Chris@19 97 INT k = i + i;
Chris@19 98 O[os * k] = buf[i];
Chris@19 99 O[os * (k + 1)] = O[os * (k - 1)] - buf[n - i];
Chris@19 100 }
Chris@19 101 if (i + i == n) {
Chris@19 102 O[os * n] = buf[i];
Chris@19 103 }
Chris@19 104 }
Chris@19 105
Chris@19 106 X(ifree)(buf);
Chris@19 107 }
Chris@19 108
Chris@19 109 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@19 110 {
Chris@19 111 P *ego = (P *) ego_;
Chris@19 112 static const tw_instr redft00e_tw[] = {
Chris@19 113 { TW_COS, 0, 1 },
Chris@19 114 { TW_SIN, 0, 1 },
Chris@19 115 { TW_NEXT, 1, 0 }
Chris@19 116 };
Chris@19 117
Chris@19 118 X(plan_awake)(ego->cld, wakefulness);
Chris@19 119 X(twiddle_awake)(wakefulness,
Chris@19 120 &ego->td, redft00e_tw, 2*ego->n, 1, (ego->n+1)/2);
Chris@19 121 }
Chris@19 122
Chris@19 123 static void destroy(plan *ego_)
Chris@19 124 {
Chris@19 125 P *ego = (P *) ego_;
Chris@19 126 X(plan_destroy_internal)(ego->cld);
Chris@19 127 }
Chris@19 128
Chris@19 129 static void print(const plan *ego_, printer *p)
Chris@19 130 {
Chris@19 131 const P *ego = (const P *) ego_;
Chris@19 132 p->print(p, "(redft00e-r2hc-%D%v%(%p%))", ego->n + 1, ego->vl, ego->cld);
Chris@19 133 }
Chris@19 134
Chris@19 135 static int applicable0(const solver *ego_, const problem *p_)
Chris@19 136 {
Chris@19 137 const problem_rdft *p = (const problem_rdft *) p_;
Chris@19 138 UNUSED(ego_);
Chris@19 139
Chris@19 140 return (1
Chris@19 141 && p->sz->rnk == 1
Chris@19 142 && p->vecsz->rnk <= 1
Chris@19 143 && p->kind[0] == REDFT00
Chris@19 144 && p->sz->dims[0].n > 1 /* n == 1 is not well-defined */
Chris@19 145 );
Chris@19 146 }
Chris@19 147
Chris@19 148 static int applicable(const solver *ego, const problem *p, const planner *plnr)
Chris@19 149 {
Chris@19 150 return (!NO_SLOWP(plnr) && applicable0(ego, p));
Chris@19 151 }
Chris@19 152
Chris@19 153 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@19 154 {
Chris@19 155 P *pln;
Chris@19 156 const problem_rdft *p;
Chris@19 157 plan *cld;
Chris@19 158 R *buf;
Chris@19 159 INT n;
Chris@19 160 opcnt ops;
Chris@19 161
Chris@19 162 static const plan_adt padt = {
Chris@19 163 X(rdft_solve), awake, print, destroy
Chris@19 164 };
Chris@19 165
Chris@19 166 if (!applicable(ego_, p_, plnr))
Chris@19 167 return (plan *)0;
Chris@19 168
Chris@19 169 p = (const problem_rdft *) p_;
Chris@19 170
Chris@19 171 n = p->sz->dims[0].n - 1;
Chris@19 172 A(n > 0);
Chris@19 173 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
Chris@19 174
Chris@19 175 cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1),
Chris@19 176 X(mktensor_0d)(),
Chris@19 177 buf, buf, R2HC));
Chris@19 178 X(ifree)(buf);
Chris@19 179 if (!cld)
Chris@19 180 return (plan *)0;
Chris@19 181
Chris@19 182 pln = MKPLAN_RDFT(P, &padt, apply);
Chris@19 183
Chris@19 184 pln->n = n;
Chris@19 185 pln->is = p->sz->dims[0].is;
Chris@19 186 pln->os = p->sz->dims[0].os;
Chris@19 187 pln->cld = cld;
Chris@19 188 pln->td = 0;
Chris@19 189
Chris@19 190 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
Chris@19 191
Chris@19 192 X(ops_zero)(&ops);
Chris@19 193 ops.other = 8 + (n-1)/2 * 11 + (1 - n % 2) * 5;
Chris@19 194 ops.add = 2 + (n-1)/2 * 5;
Chris@19 195 ops.mul = (n-1)/2 * 3 + (1 - n % 2) * 1;
Chris@19 196
Chris@19 197 X(ops_zero)(&pln->super.super.ops);
Chris@19 198 X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops);
Chris@19 199 X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops);
Chris@19 200
Chris@19 201 return &(pln->super.super);
Chris@19 202 }
Chris@19 203
Chris@19 204 /* constructor */
Chris@19 205 static solver *mksolver(void)
Chris@19 206 {
Chris@19 207 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
Chris@19 208 S *slv = MKSOLVER(S, &sadt);
Chris@19 209 return &(slv->super);
Chris@19 210 }
Chris@19 211
Chris@19 212 void X(redft00e_r2hc_register)(planner *p)
Chris@19 213 {
Chris@19 214 REGISTER_SOLVER(p, mksolver());
Chris@19 215 }