annotate fft/fftw/fftw-3.3.4/rdft/generic.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 #include "rdft.h"
Chris@19 22
Chris@19 23 typedef struct {
Chris@19 24 solver super;
Chris@19 25 rdft_kind kind;
Chris@19 26 } S;
Chris@19 27
Chris@19 28 typedef struct {
Chris@19 29 plan_rdft super;
Chris@19 30 twid *td;
Chris@19 31 INT n, is, os;
Chris@19 32 rdft_kind kind;
Chris@19 33 } P;
Chris@19 34
Chris@19 35 /***************************************************************************/
Chris@19 36
Chris@19 37 static void cdot_r2hc(INT n, const E *x, const R *w, R *or0, R *oi1)
Chris@19 38 {
Chris@19 39 INT i;
Chris@19 40
Chris@19 41 E rr = x[0], ri = 0;
Chris@19 42 x += 1;
Chris@19 43 for (i = 1; i + i < n; ++i) {
Chris@19 44 rr += x[0] * w[0];
Chris@19 45 ri += x[1] * w[1];
Chris@19 46 x += 2; w += 2;
Chris@19 47 }
Chris@19 48 *or0 = rr;
Chris@19 49 *oi1 = ri;
Chris@19 50 }
Chris@19 51
Chris@19 52 static void hartley_r2hc(INT n, const R *xr, INT xs, E *o, R *pr)
Chris@19 53 {
Chris@19 54 INT i;
Chris@19 55 E sr;
Chris@19 56 o[0] = sr = xr[0]; o += 1;
Chris@19 57 for (i = 1; i + i < n; ++i) {
Chris@19 58 R a, b;
Chris@19 59 a = xr[i * xs];
Chris@19 60 b = xr[(n - i) * xs];
Chris@19 61 sr += (o[0] = a + b);
Chris@19 62 #if FFT_SIGN == -1
Chris@19 63 o[1] = b - a;
Chris@19 64 #else
Chris@19 65 o[1] = a - b;
Chris@19 66 #endif
Chris@19 67 o += 2;
Chris@19 68 }
Chris@19 69 *pr = sr;
Chris@19 70 }
Chris@19 71
Chris@19 72 static void apply_r2hc(const plan *ego_, R *I, R *O)
Chris@19 73 {
Chris@19 74 const P *ego = (const P *) ego_;
Chris@19 75 INT i;
Chris@19 76 INT n = ego->n, is = ego->is, os = ego->os;
Chris@19 77 const R *W = ego->td->W;
Chris@19 78 E *buf;
Chris@19 79 size_t bufsz = n * sizeof(E);
Chris@19 80
Chris@19 81 BUF_ALLOC(E *, buf, bufsz);
Chris@19 82 hartley_r2hc(n, I, is, buf, O);
Chris@19 83
Chris@19 84 for (i = 1; i + i < n; ++i) {
Chris@19 85 cdot_r2hc(n, buf, W, O + i * os, O + (n - i) * os);
Chris@19 86 W += n - 1;
Chris@19 87 }
Chris@19 88
Chris@19 89 BUF_FREE(buf, bufsz);
Chris@19 90 }
Chris@19 91
Chris@19 92
Chris@19 93 static void cdot_hc2r(INT n, const E *x, const R *w, R *or0, R *or1)
Chris@19 94 {
Chris@19 95 INT i;
Chris@19 96
Chris@19 97 E rr = x[0], ii = 0;
Chris@19 98 x += 1;
Chris@19 99 for (i = 1; i + i < n; ++i) {
Chris@19 100 rr += x[0] * w[0];
Chris@19 101 ii += x[1] * w[1];
Chris@19 102 x += 2; w += 2;
Chris@19 103 }
Chris@19 104 #if FFT_SIGN == -1
Chris@19 105 *or0 = rr - ii;
Chris@19 106 *or1 = rr + ii;
Chris@19 107 #else
Chris@19 108 *or0 = rr + ii;
Chris@19 109 *or1 = rr - ii;
Chris@19 110 #endif
Chris@19 111 }
Chris@19 112
Chris@19 113 static void hartley_hc2r(INT n, const R *x, INT xs, E *o, R *pr)
Chris@19 114 {
Chris@19 115 INT i;
Chris@19 116 E sr;
Chris@19 117
Chris@19 118 o[0] = sr = x[0]; o += 1;
Chris@19 119 for (i = 1; i + i < n; ++i) {
Chris@19 120 sr += (o[0] = x[i * xs] + x[i * xs]);
Chris@19 121 o[1] = x[(n - i) * xs] + x[(n - i) * xs];
Chris@19 122 o += 2;
Chris@19 123 }
Chris@19 124 *pr = sr;
Chris@19 125 }
Chris@19 126
Chris@19 127 static void apply_hc2r(const plan *ego_, R *I, R *O)
Chris@19 128 {
Chris@19 129 const P *ego = (const P *) ego_;
Chris@19 130 INT i;
Chris@19 131 INT n = ego->n, is = ego->is, os = ego->os;
Chris@19 132 const R *W = ego->td->W;
Chris@19 133 E *buf;
Chris@19 134 size_t bufsz = n * sizeof(E);
Chris@19 135
Chris@19 136 BUF_ALLOC(E *, buf, bufsz);
Chris@19 137 hartley_hc2r(n, I, is, buf, O);
Chris@19 138
Chris@19 139 for (i = 1; i + i < n; ++i) {
Chris@19 140 cdot_hc2r(n, buf, W, O + i * os, O + (n - i) * os);
Chris@19 141 W += n - 1;
Chris@19 142 }
Chris@19 143
Chris@19 144 BUF_FREE(buf, bufsz);
Chris@19 145 }
Chris@19 146
Chris@19 147
Chris@19 148 /***************************************************************************/
Chris@19 149
Chris@19 150 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@19 151 {
Chris@19 152 P *ego = (P *) ego_;
Chris@19 153 static const tw_instr half_tw[] = {
Chris@19 154 { TW_HALF, 1, 0 },
Chris@19 155 { TW_NEXT, 1, 0 }
Chris@19 156 };
Chris@19 157
Chris@19 158 X(twiddle_awake)(wakefulness, &ego->td, half_tw, ego->n, ego->n,
Chris@19 159 (ego->n - 1) / 2);
Chris@19 160 }
Chris@19 161
Chris@19 162 static void print(const plan *ego_, printer *p)
Chris@19 163 {
Chris@19 164 const P *ego = (const P *) ego_;
Chris@19 165
Chris@19 166 p->print(p, "(rdft-generic-%s-%D)",
Chris@19 167 ego->kind == R2HC ? "r2hc" : "hc2r",
Chris@19 168 ego->n);
Chris@19 169 }
Chris@19 170
Chris@19 171 static int applicable(const S *ego, const problem *p_,
Chris@19 172 const planner *plnr)
Chris@19 173 {
Chris@19 174 const problem_rdft *p = (const problem_rdft *) p_;
Chris@19 175 return (1
Chris@19 176 && p->sz->rnk == 1
Chris@19 177 && p->vecsz->rnk == 0
Chris@19 178 && (p->sz->dims[0].n % 2) == 1
Chris@19 179 && CIMPLIES(NO_LARGE_GENERICP(plnr), p->sz->dims[0].n < GENERIC_MIN_BAD)
Chris@19 180 && CIMPLIES(NO_SLOWP(plnr), p->sz->dims[0].n > GENERIC_MAX_SLOW)
Chris@19 181 && X(is_prime)(p->sz->dims[0].n)
Chris@19 182 && p->kind[0] == ego->kind
Chris@19 183 );
Chris@19 184 }
Chris@19 185
Chris@19 186 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@19 187 {
Chris@19 188 const S *ego = (const S *)ego_;
Chris@19 189 const problem_rdft *p;
Chris@19 190 P *pln;
Chris@19 191 INT n;
Chris@19 192
Chris@19 193 static const plan_adt padt = {
Chris@19 194 X(rdft_solve), awake, print, X(plan_null_destroy)
Chris@19 195 };
Chris@19 196
Chris@19 197 if (!applicable(ego, p_, plnr))
Chris@19 198 return (plan *)0;
Chris@19 199
Chris@19 200 p = (const problem_rdft *) p_;
Chris@19 201 pln = MKPLAN_RDFT(P, &padt,
Chris@19 202 R2HC_KINDP(p->kind[0]) ? apply_r2hc : apply_hc2r);
Chris@19 203
Chris@19 204 pln->n = n = p->sz->dims[0].n;
Chris@19 205 pln->is = p->sz->dims[0].is;
Chris@19 206 pln->os = p->sz->dims[0].os;
Chris@19 207 pln->td = 0;
Chris@19 208 pln->kind = ego->kind;
Chris@19 209
Chris@19 210 pln->super.super.ops.add = (n-1) * 2.5;
Chris@19 211 pln->super.super.ops.mul = 0;
Chris@19 212 pln->super.super.ops.fma = 0.5 * (n-1) * (n-1) ;
Chris@19 213 #if 0 /* these are nice pipelined sequential loads and should cost nothing */
Chris@19 214 pln->super.super.ops.other = (n-1)*(2 + 1 + (n-1)); /* approximate */
Chris@19 215 #endif
Chris@19 216
Chris@19 217 return &(pln->super.super);
Chris@19 218 }
Chris@19 219
Chris@19 220 static solver *mksolver(rdft_kind kind)
Chris@19 221 {
Chris@19 222 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
Chris@19 223 S *slv = MKSOLVER(S, &sadt);
Chris@19 224 slv->kind = kind;
Chris@19 225 return &(slv->super);
Chris@19 226 }
Chris@19 227
Chris@19 228 void X(rdft_generic_register)(planner *p)
Chris@19 229 {
Chris@19 230 REGISTER_SOLVER(p, mksolver(R2HC));
Chris@19 231 REGISTER_SOLVER(p, mksolver(HC2R));
Chris@19 232 }