annotate src/fftw-3.3.5/dft/rader.c @ 42:2cd0e3b3e1fd

Current fftw source
author Chris Cannam
date Tue, 18 Oct 2016 13:40:26 +0100
parents
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 #include "dft.h"
Chris@42 22
Chris@42 23 /*
Chris@42 24 * Compute transforms of prime sizes using Rader's trick: turn them
Chris@42 25 * into convolutions of size n - 1, which you then perform via a pair
Chris@42 26 * of FFTs.
Chris@42 27 */
Chris@42 28
Chris@42 29 typedef struct {
Chris@42 30 solver super;
Chris@42 31 } S;
Chris@42 32
Chris@42 33 typedef struct {
Chris@42 34 plan_dft super;
Chris@42 35
Chris@42 36 plan *cld1, *cld2;
Chris@42 37 R *omega;
Chris@42 38 INT n, g, ginv;
Chris@42 39 INT is, os;
Chris@42 40 plan *cld_omega;
Chris@42 41 } P;
Chris@42 42
Chris@42 43 static rader_tl *omegas = 0;
Chris@42 44
Chris@42 45 static R *mkomega(enum wakefulness wakefulness, plan *p_, INT n, INT ginv)
Chris@42 46 {
Chris@42 47 plan_dft *p = (plan_dft *) p_;
Chris@42 48 R *omega;
Chris@42 49 INT i, gpower;
Chris@42 50 trigreal scale;
Chris@42 51 triggen *t;
Chris@42 52
Chris@42 53 if ((omega = X(rader_tl_find)(n, n, ginv, omegas)))
Chris@42 54 return omega;
Chris@42 55
Chris@42 56 omega = (R *)MALLOC(sizeof(R) * (n - 1) * 2, TWIDDLES);
Chris@42 57
Chris@42 58 scale = n - 1.0; /* normalization for convolution */
Chris@42 59
Chris@42 60 t = X(mktriggen)(wakefulness, n);
Chris@42 61 for (i = 0, gpower = 1; i < n-1; ++i, gpower = MULMOD(gpower, ginv, n)) {
Chris@42 62 trigreal w[2];
Chris@42 63 t->cexpl(t, gpower, w);
Chris@42 64 omega[2*i] = w[0] / scale;
Chris@42 65 omega[2*i+1] = FFT_SIGN * w[1] / scale;
Chris@42 66 }
Chris@42 67 X(triggen_destroy)(t);
Chris@42 68 A(gpower == 1);
Chris@42 69
Chris@42 70 p->apply(p_, omega, omega + 1, omega, omega + 1);
Chris@42 71
Chris@42 72 X(rader_tl_insert)(n, n, ginv, omega, &omegas);
Chris@42 73 return omega;
Chris@42 74 }
Chris@42 75
Chris@42 76 static void free_omega(R *omega)
Chris@42 77 {
Chris@42 78 X(rader_tl_delete)(omega, &omegas);
Chris@42 79 }
Chris@42 80
Chris@42 81
Chris@42 82 /***************************************************************************/
Chris@42 83
Chris@42 84 /* Below, we extensively use the identity that fft(x*)* = ifft(x) in
Chris@42 85 order to share data between forward and backward transforms and to
Chris@42 86 obviate the necessity of having separate forward and backward
Chris@42 87 plans. (Although we often compute separate plans these days anyway
Chris@42 88 due to the differing strides, etcetera.)
Chris@42 89
Chris@42 90 Of course, since the new FFTW gives us separate pointers to
Chris@42 91 the real and imaginary parts, we could have instead used the
Chris@42 92 fft(r,i) = ifft(i,r) form of this identity, but it was easier to
Chris@42 93 reuse the code from our old version. */
Chris@42 94
Chris@42 95 static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io)
Chris@42 96 {
Chris@42 97 const P *ego = (const P *) ego_;
Chris@42 98 INT is, os;
Chris@42 99 INT k, gpower, g, r;
Chris@42 100 R *buf;
Chris@42 101 R r0 = ri[0], i0 = ii[0];
Chris@42 102
Chris@42 103 r = ego->n; is = ego->is; os = ego->os; g = ego->g;
Chris@42 104 buf = (R *) MALLOC(sizeof(R) * (r - 1) * 2, BUFFERS);
Chris@42 105
Chris@42 106 /* First, permute the input, storing in buf: */
Chris@42 107 for (gpower = 1, k = 0; k < r - 1; ++k, gpower = MULMOD(gpower, g, r)) {
Chris@42 108 R rA, iA;
Chris@42 109 rA = ri[gpower * is];
Chris@42 110 iA = ii[gpower * is];
Chris@42 111 buf[2*k] = rA; buf[2*k + 1] = iA;
Chris@42 112 }
Chris@42 113 /* gpower == g^(r-1) mod r == 1 */;
Chris@42 114
Chris@42 115
Chris@42 116 /* compute DFT of buf, storing in output (except DC): */
Chris@42 117 {
Chris@42 118 plan_dft *cld = (plan_dft *) ego->cld1;
Chris@42 119 cld->apply(ego->cld1, buf, buf+1, ro+os, io+os);
Chris@42 120 }
Chris@42 121
Chris@42 122 /* set output DC component: */
Chris@42 123 {
Chris@42 124 ro[0] = r0 + ro[os];
Chris@42 125 io[0] = i0 + io[os];
Chris@42 126 }
Chris@42 127
Chris@42 128 /* now, multiply by omega: */
Chris@42 129 {
Chris@42 130 const R *omega = ego->omega;
Chris@42 131 for (k = 0; k < r - 1; ++k) {
Chris@42 132 E rB, iB, rW, iW;
Chris@42 133 rW = omega[2*k];
Chris@42 134 iW = omega[2*k+1];
Chris@42 135 rB = ro[(k+1)*os];
Chris@42 136 iB = io[(k+1)*os];
Chris@42 137 ro[(k+1)*os] = rW * rB - iW * iB;
Chris@42 138 io[(k+1)*os] = -(rW * iB + iW * rB);
Chris@42 139 }
Chris@42 140 }
Chris@42 141
Chris@42 142 /* this will add input[0] to all of the outputs after the ifft */
Chris@42 143 ro[os] += r0;
Chris@42 144 io[os] -= i0;
Chris@42 145
Chris@42 146 /* inverse FFT: */
Chris@42 147 {
Chris@42 148 plan_dft *cld = (plan_dft *) ego->cld2;
Chris@42 149 cld->apply(ego->cld2, ro+os, io+os, buf, buf+1);
Chris@42 150 }
Chris@42 151
Chris@42 152 /* finally, do inverse permutation to unshuffle the output: */
Chris@42 153 {
Chris@42 154 INT ginv = ego->ginv;
Chris@42 155 gpower = 1;
Chris@42 156 for (k = 0; k < r - 1; ++k, gpower = MULMOD(gpower, ginv, r)) {
Chris@42 157 ro[gpower * os] = buf[2*k];
Chris@42 158 io[gpower * os] = -buf[2*k+1];
Chris@42 159 }
Chris@42 160 A(gpower == 1);
Chris@42 161 }
Chris@42 162
Chris@42 163
Chris@42 164 X(ifree)(buf);
Chris@42 165 }
Chris@42 166
Chris@42 167 /***************************************************************************/
Chris@42 168
Chris@42 169 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@42 170 {
Chris@42 171 P *ego = (P *) ego_;
Chris@42 172
Chris@42 173 X(plan_awake)(ego->cld1, wakefulness);
Chris@42 174 X(plan_awake)(ego->cld2, wakefulness);
Chris@42 175 X(plan_awake)(ego->cld_omega, wakefulness);
Chris@42 176
Chris@42 177 switch (wakefulness) {
Chris@42 178 case SLEEPY:
Chris@42 179 free_omega(ego->omega);
Chris@42 180 ego->omega = 0;
Chris@42 181 break;
Chris@42 182 default:
Chris@42 183 ego->g = X(find_generator)(ego->n);
Chris@42 184 ego->ginv = X(power_mod)(ego->g, ego->n - 2, ego->n);
Chris@42 185 A(MULMOD(ego->g, ego->ginv, ego->n) == 1);
Chris@42 186
Chris@42 187 ego->omega = mkomega(wakefulness,
Chris@42 188 ego->cld_omega, ego->n, ego->ginv);
Chris@42 189 break;
Chris@42 190 }
Chris@42 191 }
Chris@42 192
Chris@42 193 static void destroy(plan *ego_)
Chris@42 194 {
Chris@42 195 P *ego = (P *) ego_;
Chris@42 196 X(plan_destroy_internal)(ego->cld_omega);
Chris@42 197 X(plan_destroy_internal)(ego->cld2);
Chris@42 198 X(plan_destroy_internal)(ego->cld1);
Chris@42 199 }
Chris@42 200
Chris@42 201 static void print(const plan *ego_, printer *p)
Chris@42 202 {
Chris@42 203 const P *ego = (const P *)ego_;
Chris@42 204 p->print(p, "(dft-rader-%D%ois=%oos=%(%p%)",
Chris@42 205 ego->n, ego->is, ego->os, ego->cld1);
Chris@42 206 if (ego->cld2 != ego->cld1)
Chris@42 207 p->print(p, "%(%p%)", ego->cld2);
Chris@42 208 if (ego->cld_omega != ego->cld1 && ego->cld_omega != ego->cld2)
Chris@42 209 p->print(p, "%(%p%)", ego->cld_omega);
Chris@42 210 p->putchr(p, ')');
Chris@42 211 }
Chris@42 212
Chris@42 213 static int applicable(const solver *ego_, const problem *p_,
Chris@42 214 const planner *plnr)
Chris@42 215 {
Chris@42 216 const problem_dft *p = (const problem_dft *) p_;
Chris@42 217 UNUSED(ego_);
Chris@42 218 return (1
Chris@42 219 && p->sz->rnk == 1
Chris@42 220 && p->vecsz->rnk == 0
Chris@42 221 && CIMPLIES(NO_SLOWP(plnr), p->sz->dims[0].n > RADER_MAX_SLOW)
Chris@42 222 && X(is_prime)(p->sz->dims[0].n)
Chris@42 223
Chris@42 224 /* proclaim the solver SLOW if p-1 is not easily factorizable.
Chris@42 225 Bluestein should take care of this case. */
Chris@42 226 && CIMPLIES(NO_SLOWP(plnr), X(factors_into_small_primes)(p->sz->dims[0].n - 1))
Chris@42 227 );
Chris@42 228 }
Chris@42 229
Chris@42 230 static int mkP(P *pln, INT n, INT is, INT os, R *ro, R *io,
Chris@42 231 planner *plnr)
Chris@42 232 {
Chris@42 233 plan *cld1 = (plan *) 0;
Chris@42 234 plan *cld2 = (plan *) 0;
Chris@42 235 plan *cld_omega = (plan *) 0;
Chris@42 236 R *buf = (R *) 0;
Chris@42 237
Chris@42 238 /* initial allocation for the purpose of planning */
Chris@42 239 buf = (R *) MALLOC(sizeof(R) * (n - 1) * 2, BUFFERS);
Chris@42 240
Chris@42 241 cld1 = X(mkplan_f_d)(plnr,
Chris@42 242 X(mkproblem_dft_d)(X(mktensor_1d)(n - 1, 2, os),
Chris@42 243 X(mktensor_1d)(1, 0, 0),
Chris@42 244 buf, buf + 1, ro + os, io + os),
Chris@42 245 NO_SLOW, 0, 0);
Chris@42 246 if (!cld1) goto nada;
Chris@42 247
Chris@42 248 cld2 = X(mkplan_f_d)(plnr,
Chris@42 249 X(mkproblem_dft_d)(X(mktensor_1d)(n - 1, os, 2),
Chris@42 250 X(mktensor_1d)(1, 0, 0),
Chris@42 251 ro + os, io + os, buf, buf + 1),
Chris@42 252 NO_SLOW, 0, 0);
Chris@42 253
Chris@42 254 if (!cld2) goto nada;
Chris@42 255
Chris@42 256 /* plan for omega array */
Chris@42 257 cld_omega = X(mkplan_f_d)(plnr,
Chris@42 258 X(mkproblem_dft_d)(X(mktensor_1d)(n - 1, 2, 2),
Chris@42 259 X(mktensor_1d)(1, 0, 0),
Chris@42 260 buf, buf + 1, buf, buf + 1),
Chris@42 261 NO_SLOW, ESTIMATE, 0);
Chris@42 262 if (!cld_omega) goto nada;
Chris@42 263
Chris@42 264 /* deallocate buffers; let awake() or apply() allocate them for real */
Chris@42 265 X(ifree)(buf);
Chris@42 266 buf = 0;
Chris@42 267
Chris@42 268 pln->cld1 = cld1;
Chris@42 269 pln->cld2 = cld2;
Chris@42 270 pln->cld_omega = cld_omega;
Chris@42 271 pln->omega = 0;
Chris@42 272 pln->n = n;
Chris@42 273 pln->is = is;
Chris@42 274 pln->os = os;
Chris@42 275
Chris@42 276 X(ops_add)(&cld1->ops, &cld2->ops, &pln->super.super.ops);
Chris@42 277 pln->super.super.ops.other += (n - 1) * (4 * 2 + 6) + 6;
Chris@42 278 pln->super.super.ops.add += (n - 1) * 2 + 4;
Chris@42 279 pln->super.super.ops.mul += (n - 1) * 4;
Chris@42 280
Chris@42 281 return 1;
Chris@42 282
Chris@42 283 nada:
Chris@42 284 X(ifree0)(buf);
Chris@42 285 X(plan_destroy_internal)(cld_omega);
Chris@42 286 X(plan_destroy_internal)(cld2);
Chris@42 287 X(plan_destroy_internal)(cld1);
Chris@42 288 return 0;
Chris@42 289 }
Chris@42 290
Chris@42 291 static plan *mkplan(const solver *ego, const problem *p_, planner *plnr)
Chris@42 292 {
Chris@42 293 const problem_dft *p = (const problem_dft *) p_;
Chris@42 294 P *pln;
Chris@42 295 INT n;
Chris@42 296 INT is, os;
Chris@42 297
Chris@42 298 static const plan_adt padt = {
Chris@42 299 X(dft_solve), awake, print, destroy
Chris@42 300 };
Chris@42 301
Chris@42 302 if (!applicable(ego, p_, plnr))
Chris@42 303 return (plan *) 0;
Chris@42 304
Chris@42 305 n = p->sz->dims[0].n;
Chris@42 306 is = p->sz->dims[0].is;
Chris@42 307 os = p->sz->dims[0].os;
Chris@42 308
Chris@42 309 pln = MKPLAN_DFT(P, &padt, apply);
Chris@42 310 if (!mkP(pln, n, is, os, p->ro, p->io, plnr)) {
Chris@42 311 X(ifree)(pln);
Chris@42 312 return (plan *) 0;
Chris@42 313 }
Chris@42 314 return &(pln->super.super);
Chris@42 315 }
Chris@42 316
Chris@42 317 static solver *mksolver(void)
Chris@42 318 {
Chris@42 319 static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 };
Chris@42 320 S *slv = MKSOLVER(S, &sadt);
Chris@42 321 return &(slv->super);
Chris@42 322 }
Chris@42 323
Chris@42 324 void X(dft_rader_register)(planner *p)
Chris@42 325 {
Chris@42 326 REGISTER_SOLVER(p, mksolver());
Chris@42 327 }