annotate src/fftw-3.3.8/reodft/reodft010e-r2hc.c @ 168:ceec0dd9ec9c

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