annotate src/fftw-3.3.5/rdft/dht-rader.c @ 56:af97cad61ff0

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