annotate src/fftw-3.3.5/reodft/reodft010e-r2hc.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
Chris@42 22 /* Do an R{E,O}DFT{01,10} problem via an R2HC problem, with some
Chris@42 23 pre/post-processing ala FFTPACK. */
Chris@42 24
Chris@42 25 #include "reodft.h"
Chris@42 26
Chris@42 27 typedef struct {
Chris@42 28 solver super;
Chris@42 29 } S;
Chris@42 30
Chris@42 31 typedef struct {
Chris@42 32 plan_rdft super;
Chris@42 33 plan *cld;
Chris@42 34 twid *td;
Chris@42 35 INT is, os;
Chris@42 36 INT n;
Chris@42 37 INT vl;
Chris@42 38 INT ivs, ovs;
Chris@42 39 rdft_kind kind;
Chris@42 40 } P;
Chris@42 41
Chris@42 42 /* A real-even-01 DFT operates logically on a size-4N array:
Chris@42 43 I 0 -r(I*) -I 0 r(I*),
Chris@42 44 where r denotes reversal and * denotes deletion of the 0th element.
Chris@42 45 To compute the transform of this, we imagine performing a radix-4
Chris@42 46 (real-input) DIF step, which turns the size-4N DFT into 4 size-N
Chris@42 47 (contiguous) DFTs, two of which are zero and two of which are
Chris@42 48 conjugates. The non-redundant size-N DFT has halfcomplex input, so
Chris@42 49 we can do it with a size-N hc2r transform. (In order to share
Chris@42 50 plans with the re10 (inverse) transform, however, we use the DHT
Chris@42 51 trick to re-express the hc2r problem as r2hc. This has little cost
Chris@42 52 since we are already pre- and post-processing the data in {i,n-i}
Chris@42 53 order.) Finally, we have to write out the data in the correct
Chris@42 54 order...the two size-N redundant (conjugate) hc2r DFTs correspond
Chris@42 55 to the even and odd outputs in O (i.e. the usual interleaved output
Chris@42 56 of DIF transforms); since this data has even symmetry, we only
Chris@42 57 write the first half of it.
Chris@42 58
Chris@42 59 The real-even-10 DFT is just the reverse of these steps, i.e. a
Chris@42 60 radix-4 DIT transform. There, however, we just use the r2hc
Chris@42 61 transform naturally without resorting to the DHT trick.
Chris@42 62
Chris@42 63 A real-odd-01 DFT is very similar, except that the input is
Chris@42 64 0 I (rI)* 0 -I -(rI)*. This format, however, can be transformed
Chris@42 65 into precisely the real-even-01 format above by sending I -> rI
Chris@42 66 and shifting the array by N. The former swap is just another
Chris@42 67 transformation on the input during preprocessing; the latter
Chris@42 68 multiplies the even/odd outputs by i/-i, which combines with
Chris@42 69 the factor of -i (to take the imaginary part) to simply flip
Chris@42 70 the sign of the odd outputs. Vice-versa for real-odd-10.
Chris@42 71
Chris@42 72 The FFTPACK source code was very helpful in working this out.
Chris@42 73 (They do unnecessary passes over the array, though.) The same
Chris@42 74 algorithm is also described in:
Chris@42 75
Chris@42 76 John Makhoul, "A fast cosine transform in one and two dimensions,"
Chris@42 77 IEEE Trans. on Acoust. Speech and Sig. Proc., ASSP-28 (1), 27--34 (1980).
Chris@42 78
Chris@42 79 Note that Numerical Recipes suggests a different algorithm that
Chris@42 80 requires more operations and uses trig. functions for both the pre-
Chris@42 81 and post-processing passes.
Chris@42 82 */
Chris@42 83
Chris@42 84 static void apply_re01(const plan *ego_, R *I, R *O)
Chris@42 85 {
Chris@42 86 const P *ego = (const P *) ego_;
Chris@42 87 INT is = ego->is, os = ego->os;
Chris@42 88 INT i, n = ego->n;
Chris@42 89 INT iv, vl = ego->vl;
Chris@42 90 INT ivs = ego->ivs, ovs = ego->ovs;
Chris@42 91 R *W = ego->td->W;
Chris@42 92 R *buf;
Chris@42 93
Chris@42 94 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
Chris@42 95
Chris@42 96 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
Chris@42 97 buf[0] = I[0];
Chris@42 98 for (i = 1; i < n - i; ++i) {
Chris@42 99 E a, b, apb, amb, wa, wb;
Chris@42 100 a = I[is * i];
Chris@42 101 b = I[is * (n - i)];
Chris@42 102 apb = a + b;
Chris@42 103 amb = a - b;
Chris@42 104 wa = W[2*i];
Chris@42 105 wb = W[2*i + 1];
Chris@42 106 buf[i] = wa * amb + wb * apb;
Chris@42 107 buf[n - i] = wa * apb - wb * amb;
Chris@42 108 }
Chris@42 109 if (i == n - i) {
Chris@42 110 buf[i] = K(2.0) * I[is * i] * W[2*i];
Chris@42 111 }
Chris@42 112
Chris@42 113 {
Chris@42 114 plan_rdft *cld = (plan_rdft *) ego->cld;
Chris@42 115 cld->apply((plan *) cld, buf, buf);
Chris@42 116 }
Chris@42 117
Chris@42 118 O[0] = buf[0];
Chris@42 119 for (i = 1; i < n - i; ++i) {
Chris@42 120 E a, b;
Chris@42 121 INT k;
Chris@42 122 a = buf[i];
Chris@42 123 b = buf[n - i];
Chris@42 124 k = i + i;
Chris@42 125 O[os * (k - 1)] = a - b;
Chris@42 126 O[os * k] = a + b;
Chris@42 127 }
Chris@42 128 if (i == n - i) {
Chris@42 129 O[os * (n - 1)] = buf[i];
Chris@42 130 }
Chris@42 131 }
Chris@42 132
Chris@42 133 X(ifree)(buf);
Chris@42 134 }
Chris@42 135
Chris@42 136 /* ro01 is same as re01, but with i <-> n - 1 - i in the input and
Chris@42 137 the sign of the odd output elements flipped. */
Chris@42 138 static void apply_ro01(const plan *ego_, R *I, R *O)
Chris@42 139 {
Chris@42 140 const P *ego = (const P *) ego_;
Chris@42 141 INT is = ego->is, os = ego->os;
Chris@42 142 INT i, n = ego->n;
Chris@42 143 INT iv, vl = ego->vl;
Chris@42 144 INT ivs = ego->ivs, ovs = ego->ovs;
Chris@42 145 R *W = ego->td->W;
Chris@42 146 R *buf;
Chris@42 147
Chris@42 148 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
Chris@42 149
Chris@42 150 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
Chris@42 151 buf[0] = I[is * (n - 1)];
Chris@42 152 for (i = 1; i < n - i; ++i) {
Chris@42 153 E a, b, apb, amb, wa, wb;
Chris@42 154 a = I[is * (n - 1 - i)];
Chris@42 155 b = I[is * (i - 1)];
Chris@42 156 apb = a + b;
Chris@42 157 amb = a - b;
Chris@42 158 wa = W[2*i];
Chris@42 159 wb = W[2*i+1];
Chris@42 160 buf[i] = wa * amb + wb * apb;
Chris@42 161 buf[n - i] = wa * apb - wb * amb;
Chris@42 162 }
Chris@42 163 if (i == n - i) {
Chris@42 164 buf[i] = K(2.0) * I[is * (i - 1)] * W[2*i];
Chris@42 165 }
Chris@42 166
Chris@42 167 {
Chris@42 168 plan_rdft *cld = (plan_rdft *) ego->cld;
Chris@42 169 cld->apply((plan *) cld, buf, buf);
Chris@42 170 }
Chris@42 171
Chris@42 172 O[0] = buf[0];
Chris@42 173 for (i = 1; i < n - i; ++i) {
Chris@42 174 E a, b;
Chris@42 175 INT k;
Chris@42 176 a = buf[i];
Chris@42 177 b = buf[n - i];
Chris@42 178 k = i + i;
Chris@42 179 O[os * (k - 1)] = b - a;
Chris@42 180 O[os * k] = a + b;
Chris@42 181 }
Chris@42 182 if (i == n - i) {
Chris@42 183 O[os * (n - 1)] = -buf[i];
Chris@42 184 }
Chris@42 185 }
Chris@42 186
Chris@42 187 X(ifree)(buf);
Chris@42 188 }
Chris@42 189
Chris@42 190 static void apply_re10(const plan *ego_, R *I, R *O)
Chris@42 191 {
Chris@42 192 const P *ego = (const P *) ego_;
Chris@42 193 INT is = ego->is, os = ego->os;
Chris@42 194 INT i, n = ego->n;
Chris@42 195 INT iv, vl = ego->vl;
Chris@42 196 INT ivs = ego->ivs, ovs = ego->ovs;
Chris@42 197 R *W = ego->td->W;
Chris@42 198 R *buf;
Chris@42 199
Chris@42 200 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
Chris@42 201
Chris@42 202 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
Chris@42 203 buf[0] = I[0];
Chris@42 204 for (i = 1; i < n - i; ++i) {
Chris@42 205 E u, v;
Chris@42 206 INT k = i + i;
Chris@42 207 u = I[is * (k - 1)];
Chris@42 208 v = I[is * k];
Chris@42 209 buf[n - i] = u;
Chris@42 210 buf[i] = v;
Chris@42 211 }
Chris@42 212 if (i == n - i) {
Chris@42 213 buf[i] = I[is * (n - 1)];
Chris@42 214 }
Chris@42 215
Chris@42 216 {
Chris@42 217 plan_rdft *cld = (plan_rdft *) ego->cld;
Chris@42 218 cld->apply((plan *) cld, buf, buf);
Chris@42 219 }
Chris@42 220
Chris@42 221 O[0] = K(2.0) * buf[0];
Chris@42 222 for (i = 1; i < n - i; ++i) {
Chris@42 223 E a, b, wa, wb;
Chris@42 224 a = K(2.0) * buf[i];
Chris@42 225 b = K(2.0) * buf[n - i];
Chris@42 226 wa = W[2*i];
Chris@42 227 wb = W[2*i + 1];
Chris@42 228 O[os * i] = wa * a + wb * b;
Chris@42 229 O[os * (n - i)] = wb * a - wa * b;
Chris@42 230 }
Chris@42 231 if (i == n - i) {
Chris@42 232 O[os * i] = K(2.0) * buf[i] * W[2*i];
Chris@42 233 }
Chris@42 234 }
Chris@42 235
Chris@42 236 X(ifree)(buf);
Chris@42 237 }
Chris@42 238
Chris@42 239 /* ro10 is same as re10, but with i <-> n - 1 - i in the output and
Chris@42 240 the sign of the odd input elements flipped. */
Chris@42 241 static void apply_ro10(const plan *ego_, R *I, R *O)
Chris@42 242 {
Chris@42 243 const P *ego = (const P *) ego_;
Chris@42 244 INT is = ego->is, os = ego->os;
Chris@42 245 INT i, n = ego->n;
Chris@42 246 INT iv, vl = ego->vl;
Chris@42 247 INT ivs = ego->ivs, ovs = ego->ovs;
Chris@42 248 R *W = ego->td->W;
Chris@42 249 R *buf;
Chris@42 250
Chris@42 251 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
Chris@42 252
Chris@42 253 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
Chris@42 254 buf[0] = I[0];
Chris@42 255 for (i = 1; i < n - i; ++i) {
Chris@42 256 E u, v;
Chris@42 257 INT k = i + i;
Chris@42 258 u = -I[is * (k - 1)];
Chris@42 259 v = I[is * k];
Chris@42 260 buf[n - i] = u;
Chris@42 261 buf[i] = v;
Chris@42 262 }
Chris@42 263 if (i == n - i) {
Chris@42 264 buf[i] = -I[is * (n - 1)];
Chris@42 265 }
Chris@42 266
Chris@42 267 {
Chris@42 268 plan_rdft *cld = (plan_rdft *) ego->cld;
Chris@42 269 cld->apply((plan *) cld, buf, buf);
Chris@42 270 }
Chris@42 271
Chris@42 272 O[os * (n - 1)] = K(2.0) * buf[0];
Chris@42 273 for (i = 1; i < n - i; ++i) {
Chris@42 274 E a, b, wa, wb;
Chris@42 275 a = K(2.0) * buf[i];
Chris@42 276 b = K(2.0) * buf[n - i];
Chris@42 277 wa = W[2*i];
Chris@42 278 wb = W[2*i + 1];
Chris@42 279 O[os * (n - 1 - i)] = wa * a + wb * b;
Chris@42 280 O[os * (i - 1)] = wb * a - wa * b;
Chris@42 281 }
Chris@42 282 if (i == n - i) {
Chris@42 283 O[os * (i - 1)] = K(2.0) * buf[i] * W[2*i];
Chris@42 284 }
Chris@42 285 }
Chris@42 286
Chris@42 287 X(ifree)(buf);
Chris@42 288 }
Chris@42 289
Chris@42 290 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@42 291 {
Chris@42 292 P *ego = (P *) ego_;
Chris@42 293 static const tw_instr reodft010e_tw[] = {
Chris@42 294 { TW_COS, 0, 1 },
Chris@42 295 { TW_SIN, 0, 1 },
Chris@42 296 { TW_NEXT, 1, 0 }
Chris@42 297 };
Chris@42 298
Chris@42 299 X(plan_awake)(ego->cld, wakefulness);
Chris@42 300
Chris@42 301 X(twiddle_awake)(wakefulness, &ego->td, reodft010e_tw,
Chris@42 302 4*ego->n, 1, ego->n/2+1);
Chris@42 303 }
Chris@42 304
Chris@42 305 static void destroy(plan *ego_)
Chris@42 306 {
Chris@42 307 P *ego = (P *) ego_;
Chris@42 308 X(plan_destroy_internal)(ego->cld);
Chris@42 309 }
Chris@42 310
Chris@42 311 static void print(const plan *ego_, printer *p)
Chris@42 312 {
Chris@42 313 const P *ego = (const P *) ego_;
Chris@42 314 p->print(p, "(%se-r2hc-%D%v%(%p%))",
Chris@42 315 X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld);
Chris@42 316 }
Chris@42 317
Chris@42 318 static int applicable0(const solver *ego_, const problem *p_)
Chris@42 319 {
Chris@42 320 const problem_rdft *p = (const problem_rdft *) p_;
Chris@42 321 UNUSED(ego_);
Chris@42 322
Chris@42 323 return (1
Chris@42 324 && p->sz->rnk == 1
Chris@42 325 && p->vecsz->rnk <= 1
Chris@42 326 && (p->kind[0] == REDFT01 || p->kind[0] == REDFT10
Chris@42 327 || p->kind[0] == RODFT01 || p->kind[0] == RODFT10)
Chris@42 328 );
Chris@42 329 }
Chris@42 330
Chris@42 331 static int applicable(const solver *ego, const problem *p, const planner *plnr)
Chris@42 332 {
Chris@42 333 return (!NO_SLOWP(plnr) && applicable0(ego, p));
Chris@42 334 }
Chris@42 335
Chris@42 336 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@42 337 {
Chris@42 338 P *pln;
Chris@42 339 const problem_rdft *p;
Chris@42 340 plan *cld;
Chris@42 341 R *buf;
Chris@42 342 INT n;
Chris@42 343 opcnt ops;
Chris@42 344
Chris@42 345 static const plan_adt padt = {
Chris@42 346 X(rdft_solve), awake, print, destroy
Chris@42 347 };
Chris@42 348
Chris@42 349 if (!applicable(ego_, p_, plnr))
Chris@42 350 return (plan *)0;
Chris@42 351
Chris@42 352 p = (const problem_rdft *) p_;
Chris@42 353
Chris@42 354 n = p->sz->dims[0].n;
Chris@42 355 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
Chris@42 356
Chris@42 357 cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1),
Chris@42 358 X(mktensor_0d)(),
Chris@42 359 buf, buf, R2HC));
Chris@42 360 X(ifree)(buf);
Chris@42 361 if (!cld)
Chris@42 362 return (plan *)0;
Chris@42 363
Chris@42 364 switch (p->kind[0]) {
Chris@42 365 case REDFT01: pln = MKPLAN_RDFT(P, &padt, apply_re01); break;
Chris@42 366 case REDFT10: pln = MKPLAN_RDFT(P, &padt, apply_re10); break;
Chris@42 367 case RODFT01: pln = MKPLAN_RDFT(P, &padt, apply_ro01); break;
Chris@42 368 case RODFT10: pln = MKPLAN_RDFT(P, &padt, apply_ro10); break;
Chris@42 369 default: A(0); return (plan*)0;
Chris@42 370 }
Chris@42 371
Chris@42 372 pln->n = n;
Chris@42 373 pln->is = p->sz->dims[0].is;
Chris@42 374 pln->os = p->sz->dims[0].os;
Chris@42 375 pln->cld = cld;
Chris@42 376 pln->td = 0;
Chris@42 377 pln->kind = p->kind[0];
Chris@42 378
Chris@42 379 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
Chris@42 380
Chris@42 381 X(ops_zero)(&ops);
Chris@42 382 ops.other = 4 + (n-1)/2 * 10 + (1 - n % 2) * 5;
Chris@42 383 if (p->kind[0] == REDFT01 || p->kind[0] == RODFT01) {
Chris@42 384 ops.add = (n-1)/2 * 6;
Chris@42 385 ops.mul = (n-1)/2 * 4 + (1 - n % 2) * 2;
Chris@42 386 }
Chris@42 387 else { /* 10 transforms */
Chris@42 388 ops.add = (n-1)/2 * 2;
Chris@42 389 ops.mul = 1 + (n-1)/2 * 6 + (1 - n % 2) * 2;
Chris@42 390 }
Chris@42 391
Chris@42 392 X(ops_zero)(&pln->super.super.ops);
Chris@42 393 X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops);
Chris@42 394 X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops);
Chris@42 395
Chris@42 396 return &(pln->super.super);
Chris@42 397 }
Chris@42 398
Chris@42 399 /* constructor */
Chris@42 400 static solver *mksolver(void)
Chris@42 401 {
Chris@42 402 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
Chris@42 403 S *slv = MKSOLVER(S, &sadt);
Chris@42 404 return &(slv->super);
Chris@42 405 }
Chris@42 406
Chris@42 407 void X(reodft010e_r2hc_register)(planner *p)
Chris@42 408 {
Chris@42 409 REGISTER_SOLVER(p, mksolver());
Chris@42 410 }