annotate src/fftw-3.3.3/dft/rader.c @ 169:223a55898ab9 tip default

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