annotate fft/fftw/fftw-3.3.4/rdft/dht-rader.c @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 /*
Chris@19 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 4 *
Chris@19 5 * This program is free software; you can redistribute it and/or modify
Chris@19 6 * it under the terms of the GNU General Public License as published by
Chris@19 7 * the Free Software Foundation; either version 2 of the License, or
Chris@19 8 * (at your option) any later version.
Chris@19 9 *
Chris@19 10 * This program is distributed in the hope that it will be useful,
Chris@19 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 13 * GNU General Public License for more details.
Chris@19 14 *
Chris@19 15 * You should have received a copy of the GNU General Public License
Chris@19 16 * along with this program; if not, write to the Free Software
Chris@19 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 18 *
Chris@19 19 */
Chris@19 20
Chris@19 21 #include "rdft.h"
Chris@19 22
Chris@19 23 /*
Chris@19 24 * Compute DHTs of prime sizes using Rader's trick: turn them
Chris@19 25 * into convolutions of size n - 1, which we then perform via a pair
Chris@19 26 * of FFTs. (We can then do prime real FFTs via rdft-dht.c.)
Chris@19 27 *
Chris@19 28 * Optionally (determined by the "pad" field of the solver), we can
Chris@19 29 * perform the (cyclic) convolution by zero-padding to a size
Chris@19 30 * >= 2*(n-1) - 1. This is advantageous if n-1 has large prime factors.
Chris@19 31 *
Chris@19 32 */
Chris@19 33
Chris@19 34 typedef struct {
Chris@19 35 solver super;
Chris@19 36 int pad;
Chris@19 37 } S;
Chris@19 38
Chris@19 39 typedef struct {
Chris@19 40 plan_rdft super;
Chris@19 41
Chris@19 42 plan *cld1, *cld2;
Chris@19 43 R *omega;
Chris@19 44 INT n, npad, g, ginv;
Chris@19 45 INT is, os;
Chris@19 46 plan *cld_omega;
Chris@19 47 } P;
Chris@19 48
Chris@19 49 static rader_tl *omegas = 0;
Chris@19 50
Chris@19 51 /***************************************************************************/
Chris@19 52
Chris@19 53 /* If R2HC_ONLY_CONV is 1, we use a trick to perform the convolution
Chris@19 54 purely in terms of R2HC transforms, as opposed to R2HC followed by H2RC.
Chris@19 55 This requires a few more operations, but allows us to share the same
Chris@19 56 plan/codelets for both Rader children. */
Chris@19 57 #define R2HC_ONLY_CONV 1
Chris@19 58
Chris@19 59 static void apply(const plan *ego_, R *I, R *O)
Chris@19 60 {
Chris@19 61 const P *ego = (const P *) ego_;
Chris@19 62 INT n = ego->n; /* prime */
Chris@19 63 INT npad = ego->npad; /* == n - 1 for unpadded Rader; always even */
Chris@19 64 INT is = ego->is, os;
Chris@19 65 INT k, gpower, g;
Chris@19 66 R *buf, *omega;
Chris@19 67 R r0;
Chris@19 68
Chris@19 69 buf = (R *) MALLOC(sizeof(R) * npad, BUFFERS);
Chris@19 70
Chris@19 71 /* First, permute the input, storing in buf: */
Chris@19 72 g = ego->g;
Chris@19 73 for (gpower = 1, k = 0; k < n - 1; ++k, gpower = MULMOD(gpower, g, n)) {
Chris@19 74 buf[k] = I[gpower * is];
Chris@19 75 }
Chris@19 76 /* gpower == g^(n-1) mod n == 1 */;
Chris@19 77
Chris@19 78 A(n - 1 <= npad);
Chris@19 79 for (k = n - 1; k < npad; ++k) /* optionally, zero-pad convolution */
Chris@19 80 buf[k] = 0;
Chris@19 81
Chris@19 82 os = ego->os;
Chris@19 83
Chris@19 84 /* compute RDFT of buf, storing in buf (i.e., in-place): */
Chris@19 85 {
Chris@19 86 plan_rdft *cld = (plan_rdft *) ego->cld1;
Chris@19 87 cld->apply((plan *) cld, buf, buf);
Chris@19 88 }
Chris@19 89
Chris@19 90 /* set output DC component: */
Chris@19 91 O[0] = (r0 = I[0]) + buf[0];
Chris@19 92
Chris@19 93 /* now, multiply by omega: */
Chris@19 94 omega = ego->omega;
Chris@19 95 buf[0] *= omega[0];
Chris@19 96 for (k = 1; k < npad/2; ++k) {
Chris@19 97 E rB, iB, rW, iW, a, b;
Chris@19 98 rW = omega[k];
Chris@19 99 iW = omega[npad - k];
Chris@19 100 rB = buf[k];
Chris@19 101 iB = buf[npad - k];
Chris@19 102 a = rW * rB - iW * iB;
Chris@19 103 b = rW * iB + iW * rB;
Chris@19 104 #if R2HC_ONLY_CONV
Chris@19 105 buf[k] = a + b;
Chris@19 106 buf[npad - k] = a - b;
Chris@19 107 #else
Chris@19 108 buf[k] = a;
Chris@19 109 buf[npad - k] = b;
Chris@19 110 #endif
Chris@19 111 }
Chris@19 112 /* Nyquist component: */
Chris@19 113 A(k + k == npad); /* since npad is even */
Chris@19 114 buf[k] *= omega[k];
Chris@19 115
Chris@19 116 /* this will add input[0] to all of the outputs after the ifft */
Chris@19 117 buf[0] += r0;
Chris@19 118
Chris@19 119 /* inverse FFT: */
Chris@19 120 {
Chris@19 121 plan_rdft *cld = (plan_rdft *) ego->cld2;
Chris@19 122 cld->apply((plan *) cld, buf, buf);
Chris@19 123 }
Chris@19 124
Chris@19 125 /* do inverse permutation to unshuffle the output: */
Chris@19 126 A(gpower == 1);
Chris@19 127 #if R2HC_ONLY_CONV
Chris@19 128 O[os] = buf[0];
Chris@19 129 gpower = g = ego->ginv;
Chris@19 130 A(npad == n - 1 || npad/2 >= n - 1);
Chris@19 131 if (npad == n - 1) {
Chris@19 132 for (k = 1; k < npad/2; ++k, gpower = MULMOD(gpower, g, n)) {
Chris@19 133 O[gpower * os] = buf[k] + buf[npad - k];
Chris@19 134 }
Chris@19 135 O[gpower * os] = buf[k];
Chris@19 136 ++k, gpower = MULMOD(gpower, g, n);
Chris@19 137 for (; k < npad; ++k, gpower = MULMOD(gpower, g, n)) {
Chris@19 138 O[gpower * os] = buf[npad - k] - buf[k];
Chris@19 139 }
Chris@19 140 }
Chris@19 141 else {
Chris@19 142 for (k = 1; k < n - 1; ++k, gpower = MULMOD(gpower, g, n)) {
Chris@19 143 O[gpower * os] = buf[k] + buf[npad - k];
Chris@19 144 }
Chris@19 145 }
Chris@19 146 #else
Chris@19 147 g = ego->ginv;
Chris@19 148 for (k = 0; k < n - 1; ++k, gpower = MULMOD(gpower, g, n)) {
Chris@19 149 O[gpower * os] = buf[k];
Chris@19 150 }
Chris@19 151 #endif
Chris@19 152 A(gpower == 1);
Chris@19 153
Chris@19 154 X(ifree)(buf);
Chris@19 155 }
Chris@19 156
Chris@19 157 static R *mkomega(enum wakefulness wakefulness,
Chris@19 158 plan *p_, INT n, INT npad, INT ginv)
Chris@19 159 {
Chris@19 160 plan_rdft *p = (plan_rdft *) p_;
Chris@19 161 R *omega;
Chris@19 162 INT i, gpower;
Chris@19 163 trigreal scale;
Chris@19 164 triggen *t;
Chris@19 165
Chris@19 166 if ((omega = X(rader_tl_find)(n, npad + 1, ginv, omegas)))
Chris@19 167 return omega;
Chris@19 168
Chris@19 169 omega = (R *)MALLOC(sizeof(R) * npad, TWIDDLES);
Chris@19 170
Chris@19 171 scale = npad; /* normalization for convolution */
Chris@19 172
Chris@19 173 t = X(mktriggen)(wakefulness, n);
Chris@19 174 for (i = 0, gpower = 1; i < n-1; ++i, gpower = MULMOD(gpower, ginv, n)) {
Chris@19 175 trigreal w[2];
Chris@19 176 t->cexpl(t, gpower, w);
Chris@19 177 omega[i] = (w[0] + w[1]) / scale;
Chris@19 178 }
Chris@19 179 X(triggen_destroy)(t);
Chris@19 180 A(gpower == 1);
Chris@19 181
Chris@19 182 A(npad == n - 1 || npad >= 2*(n - 1) - 1);
Chris@19 183
Chris@19 184 for (; i < npad; ++i)
Chris@19 185 omega[i] = K(0.0);
Chris@19 186 if (npad > n - 1)
Chris@19 187 for (i = 1; i < n-1; ++i)
Chris@19 188 omega[npad - i] = omega[n - 1 - i];
Chris@19 189
Chris@19 190 p->apply(p_, omega, omega);
Chris@19 191
Chris@19 192 X(rader_tl_insert)(n, npad + 1, ginv, omega, &omegas);
Chris@19 193 return omega;
Chris@19 194 }
Chris@19 195
Chris@19 196 static void free_omega(R *omega)
Chris@19 197 {
Chris@19 198 X(rader_tl_delete)(omega, &omegas);
Chris@19 199 }
Chris@19 200
Chris@19 201 /***************************************************************************/
Chris@19 202
Chris@19 203 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@19 204 {
Chris@19 205 P *ego = (P *) ego_;
Chris@19 206
Chris@19 207 X(plan_awake)(ego->cld1, wakefulness);
Chris@19 208 X(plan_awake)(ego->cld2, wakefulness);
Chris@19 209 X(plan_awake)(ego->cld_omega, wakefulness);
Chris@19 210
Chris@19 211 switch (wakefulness) {
Chris@19 212 case SLEEPY:
Chris@19 213 free_omega(ego->omega);
Chris@19 214 ego->omega = 0;
Chris@19 215 break;
Chris@19 216 default:
Chris@19 217 ego->g = X(find_generator)(ego->n);
Chris@19 218 ego->ginv = X(power_mod)(ego->g, ego->n - 2, ego->n);
Chris@19 219 A(MULMOD(ego->g, ego->ginv, ego->n) == 1);
Chris@19 220
Chris@19 221 A(!ego->omega);
Chris@19 222 ego->omega = mkomega(wakefulness,
Chris@19 223 ego->cld_omega,ego->n,ego->npad,ego->ginv);
Chris@19 224 break;
Chris@19 225 }
Chris@19 226 }
Chris@19 227
Chris@19 228 static void destroy(plan *ego_)
Chris@19 229 {
Chris@19 230 P *ego = (P *) ego_;
Chris@19 231 X(plan_destroy_internal)(ego->cld_omega);
Chris@19 232 X(plan_destroy_internal)(ego->cld2);
Chris@19 233 X(plan_destroy_internal)(ego->cld1);
Chris@19 234 }
Chris@19 235
Chris@19 236 static void print(const plan *ego_, printer *p)
Chris@19 237 {
Chris@19 238 const P *ego = (const P *) ego_;
Chris@19 239
Chris@19 240 p->print(p, "(dht-rader-%D/%D%ois=%oos=%(%p%)",
Chris@19 241 ego->n, ego->npad, ego->is, ego->os, ego->cld1);
Chris@19 242 if (ego->cld2 != ego->cld1)
Chris@19 243 p->print(p, "%(%p%)", ego->cld2);
Chris@19 244 if (ego->cld_omega != ego->cld1 && ego->cld_omega != ego->cld2)
Chris@19 245 p->print(p, "%(%p%)", ego->cld_omega);
Chris@19 246 p->putchr(p, ')');
Chris@19 247 }
Chris@19 248
Chris@19 249 static int applicable(const solver *ego, const problem *p_, const planner *plnr)
Chris@19 250 {
Chris@19 251 const problem_rdft *p = (const problem_rdft *) p_;
Chris@19 252 UNUSED(ego);
Chris@19 253 return (1
Chris@19 254 && p->sz->rnk == 1
Chris@19 255 && p->vecsz->rnk == 0
Chris@19 256 && p->kind[0] == DHT
Chris@19 257 && X(is_prime)(p->sz->dims[0].n)
Chris@19 258 && p->sz->dims[0].n > 2
Chris@19 259 && CIMPLIES(NO_SLOWP(plnr), p->sz->dims[0].n > RADER_MAX_SLOW)
Chris@19 260 /* proclaim the solver SLOW if p-1 is not easily
Chris@19 261 factorizable. Unlike in the complex case where
Chris@19 262 Bluestein can solve the problem, in the DHT case we
Chris@19 263 may have no other choice */
Chris@19 264 && CIMPLIES(NO_SLOWP(plnr), X(factors_into_small_primes)(p->sz->dims[0].n - 1))
Chris@19 265 );
Chris@19 266 }
Chris@19 267
Chris@19 268 static INT choose_transform_size(INT minsz)
Chris@19 269 {
Chris@19 270 static const INT primes[] = { 2, 3, 5, 0 };
Chris@19 271 while (!X(factors_into)(minsz, primes) || minsz % 2)
Chris@19 272 ++minsz;
Chris@19 273 return minsz;
Chris@19 274 }
Chris@19 275
Chris@19 276 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@19 277 {
Chris@19 278 const S *ego = (const S *) ego_;
Chris@19 279 const problem_rdft *p = (const problem_rdft *) p_;
Chris@19 280 P *pln;
Chris@19 281 INT n, npad;
Chris@19 282 INT is, os;
Chris@19 283 plan *cld1 = (plan *) 0;
Chris@19 284 plan *cld2 = (plan *) 0;
Chris@19 285 plan *cld_omega = (plan *) 0;
Chris@19 286 R *buf = (R *) 0;
Chris@19 287 problem *cldp;
Chris@19 288
Chris@19 289 static const plan_adt padt = {
Chris@19 290 X(rdft_solve), awake, print, destroy
Chris@19 291 };
Chris@19 292
Chris@19 293 if (!applicable(ego_, p_, plnr))
Chris@19 294 return (plan *) 0;
Chris@19 295
Chris@19 296 n = p->sz->dims[0].n;
Chris@19 297 is = p->sz->dims[0].is;
Chris@19 298 os = p->sz->dims[0].os;
Chris@19 299
Chris@19 300 if (ego->pad)
Chris@19 301 npad = choose_transform_size(2 * (n - 1) - 1);
Chris@19 302 else
Chris@19 303 npad = n - 1;
Chris@19 304
Chris@19 305 /* initial allocation for the purpose of planning */
Chris@19 306 buf = (R *) MALLOC(sizeof(R) * npad, BUFFERS);
Chris@19 307
Chris@19 308 cld1 = X(mkplan_f_d)(plnr,
Chris@19 309 X(mkproblem_rdft_1_d)(X(mktensor_1d)(npad, 1, 1),
Chris@19 310 X(mktensor_1d)(1, 0, 0),
Chris@19 311 buf, buf,
Chris@19 312 R2HC),
Chris@19 313 NO_SLOW, 0, 0);
Chris@19 314 if (!cld1) goto nada;
Chris@19 315
Chris@19 316 cldp =
Chris@19 317 X(mkproblem_rdft_1_d)(
Chris@19 318 X(mktensor_1d)(npad, 1, 1),
Chris@19 319 X(mktensor_1d)(1, 0, 0),
Chris@19 320 buf, buf,
Chris@19 321 #if R2HC_ONLY_CONV
Chris@19 322 R2HC
Chris@19 323 #else
Chris@19 324 HC2R
Chris@19 325 #endif
Chris@19 326 );
Chris@19 327 if (!(cld2 = X(mkplan_f_d)(plnr, cldp, NO_SLOW, 0, 0)))
Chris@19 328 goto nada;
Chris@19 329
Chris@19 330 /* plan for omega */
Chris@19 331 cld_omega = X(mkplan_f_d)(plnr,
Chris@19 332 X(mkproblem_rdft_1_d)(
Chris@19 333 X(mktensor_1d)(npad, 1, 1),
Chris@19 334 X(mktensor_1d)(1, 0, 0),
Chris@19 335 buf, buf, R2HC),
Chris@19 336 NO_SLOW, ESTIMATE, 0);
Chris@19 337 if (!cld_omega) goto nada;
Chris@19 338
Chris@19 339 /* deallocate buffers; let awake() or apply() allocate them for real */
Chris@19 340 X(ifree)(buf);
Chris@19 341 buf = 0;
Chris@19 342
Chris@19 343 pln = MKPLAN_RDFT(P, &padt, apply);
Chris@19 344 pln->cld1 = cld1;
Chris@19 345 pln->cld2 = cld2;
Chris@19 346 pln->cld_omega = cld_omega;
Chris@19 347 pln->omega = 0;
Chris@19 348 pln->n = n;
Chris@19 349 pln->npad = npad;
Chris@19 350 pln->is = is;
Chris@19 351 pln->os = os;
Chris@19 352
Chris@19 353 X(ops_add)(&cld1->ops, &cld2->ops, &pln->super.super.ops);
Chris@19 354 pln->super.super.ops.other += (npad/2-1)*6 + npad + n + (n-1) * ego->pad;
Chris@19 355 pln->super.super.ops.add += (npad/2-1)*2 + 2 + (n-1) * ego->pad;
Chris@19 356 pln->super.super.ops.mul += (npad/2-1)*4 + 2 + ego->pad;
Chris@19 357 #if R2HC_ONLY_CONV
Chris@19 358 pln->super.super.ops.other += n-2 - ego->pad;
Chris@19 359 pln->super.super.ops.add += (npad/2-1)*2 + (n-2) - ego->pad;
Chris@19 360 #endif
Chris@19 361
Chris@19 362 return &(pln->super.super);
Chris@19 363
Chris@19 364 nada:
Chris@19 365 X(ifree0)(buf);
Chris@19 366 X(plan_destroy_internal)(cld_omega);
Chris@19 367 X(plan_destroy_internal)(cld2);
Chris@19 368 X(plan_destroy_internal)(cld1);
Chris@19 369 return 0;
Chris@19 370 }
Chris@19 371
Chris@19 372 /* constructors */
Chris@19 373
Chris@19 374 static solver *mksolver(int pad)
Chris@19 375 {
Chris@19 376 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
Chris@19 377 S *slv = MKSOLVER(S, &sadt);
Chris@19 378 slv->pad = pad;
Chris@19 379 return &(slv->super);
Chris@19 380 }
Chris@19 381
Chris@19 382 void X(dht_rader_register)(planner *p)
Chris@19 383 {
Chris@19 384 REGISTER_SOLVER(p, mksolver(0));
Chris@19 385 REGISTER_SOLVER(p, mksolver(1));
Chris@19 386 }