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