annotate src/fftw-3.3.8/reodft/reodft11e-r2hc-odd.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}DFT11 problem via an R2HC problem of the same *odd* size,
cannam@167 23 with some permutations and post-processing, as described in:
cannam@167 24
cannam@167 25 S. C. Chan and K. L. Ho, "Fast algorithms for computing the
cannam@167 26 discrete cosine transform," IEEE Trans. Circuits Systems II:
cannam@167 27 Analog & Digital Sig. Proc. 39 (3), 185--190 (1992).
cannam@167 28
cannam@167 29 (For even sizes, see reodft11e-radix2.c.)
cannam@167 30
cannam@167 31 This algorithm is related to the 8 x n prime-factor-algorithm (PFA)
cannam@167 32 decomposition of the size 8n "logical" DFT corresponding to the
cannam@167 33 R{EO}DFT11.
cannam@167 34
cannam@167 35 Aside from very confusing notation (several symbols are redefined
cannam@167 36 from one line to the next), be aware that this paper has some
cannam@167 37 errors. In particular, the signs are wrong in Eqs. (34-35). Also,
cannam@167 38 Eqs. (36-37) should be simply C(k) = C(2k + 1 mod N), and similarly
cannam@167 39 for S (or, equivalently, the second cases should have 2*N - 2*k - 1
cannam@167 40 instead of N - k - 1). Note also that in their definition of the
cannam@167 41 DFT, similarly to FFTW's, the exponent's sign is -1, but they
cannam@167 42 forgot to correspondingly multiply S (the sine terms) by -1.
cannam@167 43 */
cannam@167 44
cannam@167 45 #include "reodft/reodft.h"
cannam@167 46
cannam@167 47 typedef struct {
cannam@167 48 solver super;
cannam@167 49 } S;
cannam@167 50
cannam@167 51 typedef struct {
cannam@167 52 plan_rdft super;
cannam@167 53 plan *cld;
cannam@167 54 INT is, os;
cannam@167 55 INT n;
cannam@167 56 INT vl;
cannam@167 57 INT ivs, ovs;
cannam@167 58 rdft_kind kind;
cannam@167 59 } P;
cannam@167 60
cannam@167 61 static DK(SQRT2, +1.4142135623730950488016887242096980785696718753769);
cannam@167 62
cannam@167 63 #define SGN_SET(x, i) ((i) % 2 ? -(x) : (x))
cannam@167 64
cannam@167 65 static void apply_re11(const plan *ego_, R *I, R *O)
cannam@167 66 {
cannam@167 67 const P *ego = (const P *) ego_;
cannam@167 68 INT is = ego->is, os = ego->os;
cannam@167 69 INT i, n = ego->n, n2 = n/2;
cannam@167 70 INT iv, vl = ego->vl;
cannam@167 71 INT ivs = ego->ivs, ovs = ego->ovs;
cannam@167 72 R *buf;
cannam@167 73
cannam@167 74 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
cannam@167 75
cannam@167 76 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
cannam@167 77 {
cannam@167 78 INT m;
cannam@167 79 for (i = 0, m = n2; m < n; ++i, m += 4)
cannam@167 80 buf[i] = I[is * m];
cannam@167 81 for (; m < 2 * n; ++i, m += 4)
cannam@167 82 buf[i] = -I[is * (2*n - m - 1)];
cannam@167 83 for (; m < 3 * n; ++i, m += 4)
cannam@167 84 buf[i] = -I[is * (m - 2*n)];
cannam@167 85 for (; m < 4 * n; ++i, m += 4)
cannam@167 86 buf[i] = I[is * (4*n - m - 1)];
cannam@167 87 m -= 4 * n;
cannam@167 88 for (; i < n; ++i, m += 4)
cannam@167 89 buf[i] = I[is * m];
cannam@167 90 }
cannam@167 91
cannam@167 92 { /* child plan: R2HC of size n */
cannam@167 93 plan_rdft *cld = (plan_rdft *) ego->cld;
cannam@167 94 cld->apply((plan *) cld, buf, buf);
cannam@167 95 }
cannam@167 96
cannam@167 97 /* FIXME: strength-reduce loop by 4 to eliminate ugly sgn_set? */
cannam@167 98 for (i = 0; i + i + 1 < n2; ++i) {
cannam@167 99 INT k = i + i + 1;
cannam@167 100 E c1, s1;
cannam@167 101 E c2, s2;
cannam@167 102 c1 = buf[k];
cannam@167 103 c2 = buf[k + 1];
cannam@167 104 s2 = buf[n - (k + 1)];
cannam@167 105 s1 = buf[n - k];
cannam@167 106
cannam@167 107 O[os * i] = SQRT2 * (SGN_SET(c1, (i+1)/2) +
cannam@167 108 SGN_SET(s1, i/2));
cannam@167 109 O[os * (n - (i+1))] = SQRT2 * (SGN_SET(c1, (n-i)/2) -
cannam@167 110 SGN_SET(s1, (n-(i+1))/2));
cannam@167 111
cannam@167 112 O[os * (n2 - (i+1))] = SQRT2 * (SGN_SET(c2, (n2-i)/2) -
cannam@167 113 SGN_SET(s2, (n2-(i+1))/2));
cannam@167 114 O[os * (n2 + (i+1))] = SQRT2 * (SGN_SET(c2, (n2+i+2)/2) +
cannam@167 115 SGN_SET(s2, (n2+(i+1))/2));
cannam@167 116 }
cannam@167 117 if (i + i + 1 == n2) {
cannam@167 118 E c, s;
cannam@167 119 c = buf[n2];
cannam@167 120 s = buf[n - n2];
cannam@167 121 O[os * i] = SQRT2 * (SGN_SET(c, (i+1)/2) +
cannam@167 122 SGN_SET(s, i/2));
cannam@167 123 O[os * (n - (i+1))] = SQRT2 * (SGN_SET(c, (i+2)/2) +
cannam@167 124 SGN_SET(s, (i+1)/2));
cannam@167 125 }
cannam@167 126 O[os * n2] = SQRT2 * SGN_SET(buf[0], (n2+1)/2);
cannam@167 127 }
cannam@167 128
cannam@167 129 X(ifree)(buf);
cannam@167 130 }
cannam@167 131
cannam@167 132 /* like for rodft01, rodft11 is obtained from redft11 by
cannam@167 133 reversing the input and flipping the sign of every other output. */
cannam@167 134 static void apply_ro11(const plan *ego_, R *I, R *O)
cannam@167 135 {
cannam@167 136 const P *ego = (const P *) ego_;
cannam@167 137 INT is = ego->is, os = ego->os;
cannam@167 138 INT i, n = ego->n, n2 = n/2;
cannam@167 139 INT iv, vl = ego->vl;
cannam@167 140 INT ivs = ego->ivs, ovs = ego->ovs;
cannam@167 141 R *buf;
cannam@167 142
cannam@167 143 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
cannam@167 144
cannam@167 145 for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
cannam@167 146 {
cannam@167 147 INT m;
cannam@167 148 for (i = 0, m = n2; m < n; ++i, m += 4)
cannam@167 149 buf[i] = I[is * (n - 1 - m)];
cannam@167 150 for (; m < 2 * n; ++i, m += 4)
cannam@167 151 buf[i] = -I[is * (m - n)];
cannam@167 152 for (; m < 3 * n; ++i, m += 4)
cannam@167 153 buf[i] = -I[is * (3*n - 1 - m)];
cannam@167 154 for (; m < 4 * n; ++i, m += 4)
cannam@167 155 buf[i] = I[is * (m - 3*n)];
cannam@167 156 m -= 4 * n;
cannam@167 157 for (; i < n; ++i, m += 4)
cannam@167 158 buf[i] = I[is * (n - 1 - m)];
cannam@167 159 }
cannam@167 160
cannam@167 161 { /* child plan: R2HC of size n */
cannam@167 162 plan_rdft *cld = (plan_rdft *) ego->cld;
cannam@167 163 cld->apply((plan *) cld, buf, buf);
cannam@167 164 }
cannam@167 165
cannam@167 166 /* FIXME: strength-reduce loop by 4 to eliminate ugly sgn_set? */
cannam@167 167 for (i = 0; i + i + 1 < n2; ++i) {
cannam@167 168 INT k = i + i + 1;
cannam@167 169 INT j;
cannam@167 170 E c1, s1;
cannam@167 171 E c2, s2;
cannam@167 172 c1 = buf[k];
cannam@167 173 c2 = buf[k + 1];
cannam@167 174 s2 = buf[n - (k + 1)];
cannam@167 175 s1 = buf[n - k];
cannam@167 176
cannam@167 177 O[os * i] = SQRT2 * (SGN_SET(c1, (i+1)/2 + i) +
cannam@167 178 SGN_SET(s1, i/2 + i));
cannam@167 179 O[os * (n - (i+1))] = SQRT2 * (SGN_SET(c1, (n-i)/2 + i) -
cannam@167 180 SGN_SET(s1, (n-(i+1))/2 + i));
cannam@167 181
cannam@167 182 j = n2 - (i+1);
cannam@167 183 O[os * j] = SQRT2 * (SGN_SET(c2, (n2-i)/2 + j) -
cannam@167 184 SGN_SET(s2, (n2-(i+1))/2 + j));
cannam@167 185 O[os * (n2 + (i+1))] = SQRT2 * (SGN_SET(c2, (n2+i+2)/2 + j) +
cannam@167 186 SGN_SET(s2, (n2+(i+1))/2 + j));
cannam@167 187 }
cannam@167 188 if (i + i + 1 == n2) {
cannam@167 189 E c, s;
cannam@167 190 c = buf[n2];
cannam@167 191 s = buf[n - n2];
cannam@167 192 O[os * i] = SQRT2 * (SGN_SET(c, (i+1)/2 + i) +
cannam@167 193 SGN_SET(s, i/2 + i));
cannam@167 194 O[os * (n - (i+1))] = SQRT2 * (SGN_SET(c, (i+2)/2 + i) +
cannam@167 195 SGN_SET(s, (i+1)/2 + i));
cannam@167 196 }
cannam@167 197 O[os * n2] = SQRT2 * SGN_SET(buf[0], (n2+1)/2 + n2);
cannam@167 198 }
cannam@167 199
cannam@167 200 X(ifree)(buf);
cannam@167 201 }
cannam@167 202
cannam@167 203 static void awake(plan *ego_, enum wakefulness wakefulness)
cannam@167 204 {
cannam@167 205 P *ego = (P *) ego_;
cannam@167 206 X(plan_awake)(ego->cld, wakefulness);
cannam@167 207 }
cannam@167 208
cannam@167 209 static void destroy(plan *ego_)
cannam@167 210 {
cannam@167 211 P *ego = (P *) ego_;
cannam@167 212 X(plan_destroy_internal)(ego->cld);
cannam@167 213 }
cannam@167 214
cannam@167 215 static void print(const plan *ego_, printer *p)
cannam@167 216 {
cannam@167 217 const P *ego = (const P *) ego_;
cannam@167 218 p->print(p, "(%se-r2hc-odd-%D%v%(%p%))",
cannam@167 219 X(rdft_kind_str)(ego->kind), ego->n, ego->vl, ego->cld);
cannam@167 220 }
cannam@167 221
cannam@167 222 static int applicable0(const solver *ego_, const problem *p_)
cannam@167 223 {
cannam@167 224 const problem_rdft *p = (const problem_rdft *) p_;
cannam@167 225 UNUSED(ego_);
cannam@167 226
cannam@167 227 return (1
cannam@167 228 && p->sz->rnk == 1
cannam@167 229 && p->vecsz->rnk <= 1
cannam@167 230 && p->sz->dims[0].n % 2 == 1
cannam@167 231 && (p->kind[0] == REDFT11 || p->kind[0] == RODFT11)
cannam@167 232 );
cannam@167 233 }
cannam@167 234
cannam@167 235 static int applicable(const solver *ego, const problem *p, const planner *plnr)
cannam@167 236 {
cannam@167 237 return (!NO_SLOWP(plnr) && applicable0(ego, p));
cannam@167 238 }
cannam@167 239
cannam@167 240 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
cannam@167 241 {
cannam@167 242 P *pln;
cannam@167 243 const problem_rdft *p;
cannam@167 244 plan *cld;
cannam@167 245 R *buf;
cannam@167 246 INT n;
cannam@167 247 opcnt ops;
cannam@167 248
cannam@167 249 static const plan_adt padt = {
cannam@167 250 X(rdft_solve), awake, print, destroy
cannam@167 251 };
cannam@167 252
cannam@167 253 if (!applicable(ego_, p_, plnr))
cannam@167 254 return (plan *)0;
cannam@167 255
cannam@167 256 p = (const problem_rdft *) p_;
cannam@167 257
cannam@167 258 n = p->sz->dims[0].n;
cannam@167 259 buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
cannam@167 260
cannam@167 261 cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1),
cannam@167 262 X(mktensor_0d)(),
cannam@167 263 buf, buf, R2HC));
cannam@167 264 X(ifree)(buf);
cannam@167 265 if (!cld)
cannam@167 266 return (plan *)0;
cannam@167 267
cannam@167 268 pln = MKPLAN_RDFT(P, &padt, p->kind[0]==REDFT11 ? apply_re11:apply_ro11);
cannam@167 269 pln->n = n;
cannam@167 270 pln->is = p->sz->dims[0].is;
cannam@167 271 pln->os = p->sz->dims[0].os;
cannam@167 272 pln->cld = cld;
cannam@167 273 pln->kind = p->kind[0];
cannam@167 274
cannam@167 275 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
cannam@167 276
cannam@167 277 X(ops_zero)(&ops);
cannam@167 278 ops.add = n - 1;
cannam@167 279 ops.mul = n;
cannam@167 280 ops.other = 4*n;
cannam@167 281
cannam@167 282 X(ops_zero)(&pln->super.super.ops);
cannam@167 283 X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops);
cannam@167 284 X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops);
cannam@167 285
cannam@167 286 return &(pln->super.super);
cannam@167 287 }
cannam@167 288
cannam@167 289 /* constructor */
cannam@167 290 static solver *mksolver(void)
cannam@167 291 {
cannam@167 292 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
cannam@167 293 S *slv = MKSOLVER(S, &sadt);
cannam@167 294 return &(slv->super);
cannam@167 295 }
cannam@167 296
cannam@167 297 void X(reodft11e_r2hc_odd_register)(planner *p)
cannam@167 298 {
cannam@167 299 REGISTER_SOLVER(p, mksolver());
cannam@167 300 }