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