annotate src/fftw-3.3.8/rdft/generic.c @ 82:d0c2a83c1364

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam
date Tue, 19 Nov 2019 14:52:55 +0000
parents
children
rev   line source
Chris@82 1 /*
Chris@82 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@82 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@82 4 *
Chris@82 5 * This program is free software; you can redistribute it and/or modify
Chris@82 6 * it under the terms of the GNU General Public License as published by
Chris@82 7 * the Free Software Foundation; either version 2 of the License, or
Chris@82 8 * (at your option) any later version.
Chris@82 9 *
Chris@82 10 * This program is distributed in the hope that it will be useful,
Chris@82 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@82 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@82 13 * GNU General Public License for more details.
Chris@82 14 *
Chris@82 15 * You should have received a copy of the GNU General Public License
Chris@82 16 * along with this program; if not, write to the Free Software
Chris@82 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@82 18 *
Chris@82 19 */
Chris@82 20
Chris@82 21 #include "rdft/rdft.h"
Chris@82 22
Chris@82 23 typedef struct {
Chris@82 24 solver super;
Chris@82 25 rdft_kind kind;
Chris@82 26 } S;
Chris@82 27
Chris@82 28 typedef struct {
Chris@82 29 plan_rdft super;
Chris@82 30 twid *td;
Chris@82 31 INT n, is, os;
Chris@82 32 rdft_kind kind;
Chris@82 33 } P;
Chris@82 34
Chris@82 35 /***************************************************************************/
Chris@82 36
Chris@82 37 static void cdot_r2hc(INT n, const E *x, const R *w, R *or0, R *oi1)
Chris@82 38 {
Chris@82 39 INT i;
Chris@82 40
Chris@82 41 E rr = x[0], ri = 0;
Chris@82 42 x += 1;
Chris@82 43 for (i = 1; i + i < n; ++i) {
Chris@82 44 rr += x[0] * w[0];
Chris@82 45 ri += x[1] * w[1];
Chris@82 46 x += 2; w += 2;
Chris@82 47 }
Chris@82 48 *or0 = rr;
Chris@82 49 *oi1 = ri;
Chris@82 50 }
Chris@82 51
Chris@82 52 static void hartley_r2hc(INT n, const R *xr, INT xs, E *o, R *pr)
Chris@82 53 {
Chris@82 54 INT i;
Chris@82 55 E sr;
Chris@82 56 o[0] = sr = xr[0]; o += 1;
Chris@82 57 for (i = 1; i + i < n; ++i) {
Chris@82 58 R a, b;
Chris@82 59 a = xr[i * xs];
Chris@82 60 b = xr[(n - i) * xs];
Chris@82 61 sr += (o[0] = a + b);
Chris@82 62 #if FFT_SIGN == -1
Chris@82 63 o[1] = b - a;
Chris@82 64 #else
Chris@82 65 o[1] = a - b;
Chris@82 66 #endif
Chris@82 67 o += 2;
Chris@82 68 }
Chris@82 69 *pr = sr;
Chris@82 70 }
Chris@82 71
Chris@82 72 static void apply_r2hc(const plan *ego_, R *I, R *O)
Chris@82 73 {
Chris@82 74 const P *ego = (const P *) ego_;
Chris@82 75 INT i;
Chris@82 76 INT n = ego->n, is = ego->is, os = ego->os;
Chris@82 77 const R *W = ego->td->W;
Chris@82 78 E *buf;
Chris@82 79 size_t bufsz = n * sizeof(E);
Chris@82 80
Chris@82 81 BUF_ALLOC(E *, buf, bufsz);
Chris@82 82 hartley_r2hc(n, I, is, buf, O);
Chris@82 83
Chris@82 84 for (i = 1; i + i < n; ++i) {
Chris@82 85 cdot_r2hc(n, buf, W, O + i * os, O + (n - i) * os);
Chris@82 86 W += n - 1;
Chris@82 87 }
Chris@82 88
Chris@82 89 BUF_FREE(buf, bufsz);
Chris@82 90 }
Chris@82 91
Chris@82 92
Chris@82 93 static void cdot_hc2r(INT n, const E *x, const R *w, R *or0, R *or1)
Chris@82 94 {
Chris@82 95 INT i;
Chris@82 96
Chris@82 97 E rr = x[0], ii = 0;
Chris@82 98 x += 1;
Chris@82 99 for (i = 1; i + i < n; ++i) {
Chris@82 100 rr += x[0] * w[0];
Chris@82 101 ii += x[1] * w[1];
Chris@82 102 x += 2; w += 2;
Chris@82 103 }
Chris@82 104 #if FFT_SIGN == -1
Chris@82 105 *or0 = rr - ii;
Chris@82 106 *or1 = rr + ii;
Chris@82 107 #else
Chris@82 108 *or0 = rr + ii;
Chris@82 109 *or1 = rr - ii;
Chris@82 110 #endif
Chris@82 111 }
Chris@82 112
Chris@82 113 static void hartley_hc2r(INT n, const R *x, INT xs, E *o, R *pr)
Chris@82 114 {
Chris@82 115 INT i;
Chris@82 116 E sr;
Chris@82 117
Chris@82 118 o[0] = sr = x[0]; o += 1;
Chris@82 119 for (i = 1; i + i < n; ++i) {
Chris@82 120 sr += (o[0] = x[i * xs] + x[i * xs]);
Chris@82 121 o[1] = x[(n - i) * xs] + x[(n - i) * xs];
Chris@82 122 o += 2;
Chris@82 123 }
Chris@82 124 *pr = sr;
Chris@82 125 }
Chris@82 126
Chris@82 127 static void apply_hc2r(const plan *ego_, R *I, R *O)
Chris@82 128 {
Chris@82 129 const P *ego = (const P *) ego_;
Chris@82 130 INT i;
Chris@82 131 INT n = ego->n, is = ego->is, os = ego->os;
Chris@82 132 const R *W = ego->td->W;
Chris@82 133 E *buf;
Chris@82 134 size_t bufsz = n * sizeof(E);
Chris@82 135
Chris@82 136 BUF_ALLOC(E *, buf, bufsz);
Chris@82 137 hartley_hc2r(n, I, is, buf, O);
Chris@82 138
Chris@82 139 for (i = 1; i + i < n; ++i) {
Chris@82 140 cdot_hc2r(n, buf, W, O + i * os, O + (n - i) * os);
Chris@82 141 W += n - 1;
Chris@82 142 }
Chris@82 143
Chris@82 144 BUF_FREE(buf, bufsz);
Chris@82 145 }
Chris@82 146
Chris@82 147
Chris@82 148 /***************************************************************************/
Chris@82 149
Chris@82 150 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@82 151 {
Chris@82 152 P *ego = (P *) ego_;
Chris@82 153 static const tw_instr half_tw[] = {
Chris@82 154 { TW_HALF, 1, 0 },
Chris@82 155 { TW_NEXT, 1, 0 }
Chris@82 156 };
Chris@82 157
Chris@82 158 X(twiddle_awake)(wakefulness, &ego->td, half_tw, ego->n, ego->n,
Chris@82 159 (ego->n - 1) / 2);
Chris@82 160 }
Chris@82 161
Chris@82 162 static void print(const plan *ego_, printer *p)
Chris@82 163 {
Chris@82 164 const P *ego = (const P *) ego_;
Chris@82 165
Chris@82 166 p->print(p, "(rdft-generic-%s-%D)",
Chris@82 167 ego->kind == R2HC ? "r2hc" : "hc2r",
Chris@82 168 ego->n);
Chris@82 169 }
Chris@82 170
Chris@82 171 static int applicable(const S *ego, const problem *p_,
Chris@82 172 const planner *plnr)
Chris@82 173 {
Chris@82 174 const problem_rdft *p = (const problem_rdft *) p_;
Chris@82 175 return (1
Chris@82 176 && p->sz->rnk == 1
Chris@82 177 && p->vecsz->rnk == 0
Chris@82 178 && (p->sz->dims[0].n % 2) == 1
Chris@82 179 && CIMPLIES(NO_LARGE_GENERICP(plnr), p->sz->dims[0].n < GENERIC_MIN_BAD)
Chris@82 180 && CIMPLIES(NO_SLOWP(plnr), p->sz->dims[0].n > GENERIC_MAX_SLOW)
Chris@82 181 && X(is_prime)(p->sz->dims[0].n)
Chris@82 182 && p->kind[0] == ego->kind
Chris@82 183 );
Chris@82 184 }
Chris@82 185
Chris@82 186 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@82 187 {
Chris@82 188 const S *ego = (const S *)ego_;
Chris@82 189 const problem_rdft *p;
Chris@82 190 P *pln;
Chris@82 191 INT n;
Chris@82 192
Chris@82 193 static const plan_adt padt = {
Chris@82 194 X(rdft_solve), awake, print, X(plan_null_destroy)
Chris@82 195 };
Chris@82 196
Chris@82 197 if (!applicable(ego, p_, plnr))
Chris@82 198 return (plan *)0;
Chris@82 199
Chris@82 200 p = (const problem_rdft *) p_;
Chris@82 201 pln = MKPLAN_RDFT(P, &padt,
Chris@82 202 R2HC_KINDP(p->kind[0]) ? apply_r2hc : apply_hc2r);
Chris@82 203
Chris@82 204 pln->n = n = p->sz->dims[0].n;
Chris@82 205 pln->is = p->sz->dims[0].is;
Chris@82 206 pln->os = p->sz->dims[0].os;
Chris@82 207 pln->td = 0;
Chris@82 208 pln->kind = ego->kind;
Chris@82 209
Chris@82 210 pln->super.super.ops.add = (n-1) * 2.5;
Chris@82 211 pln->super.super.ops.mul = 0;
Chris@82 212 pln->super.super.ops.fma = 0.5 * (n-1) * (n-1) ;
Chris@82 213 #if 0 /* these are nice pipelined sequential loads and should cost nothing */
Chris@82 214 pln->super.super.ops.other = (n-1)*(2 + 1 + (n-1)); /* approximate */
Chris@82 215 #endif
Chris@82 216
Chris@82 217 return &(pln->super.super);
Chris@82 218 }
Chris@82 219
Chris@82 220 static solver *mksolver(rdft_kind kind)
Chris@82 221 {
Chris@82 222 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
Chris@82 223 S *slv = MKSOLVER(S, &sadt);
Chris@82 224 slv->kind = kind;
Chris@82 225 return &(slv->super);
Chris@82 226 }
Chris@82 227
Chris@82 228 void X(rdft_generic_register)(planner *p)
Chris@82 229 {
Chris@82 230 REGISTER_SOLVER(p, mksolver(R2HC));
Chris@82 231 REGISTER_SOLVER(p, mksolver(HC2R));
Chris@82 232 }