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