annotate src/fftw-3.3.5/reodft/redft00e-r2hc.c @ 56:af97cad61ff0

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