annotate src/fftw-3.3.5/reodft/reodft010e-r2hc.c @ 148:b4bfdf10c4b3

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