annotate src/fftw-3.3.5/rdft/buffered2.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 /* buffering of rdft2. We always buffer the complex array */
Chris@42 23
Chris@42 24 #include "rdft.h"
Chris@42 25 #include "dft.h"
Chris@42 26
Chris@42 27 typedef struct {
Chris@42 28 solver super;
Chris@42 29 size_t maxnbuf_ndx;
Chris@42 30 } S;
Chris@42 31
Chris@42 32 static const INT maxnbufs[] = { 8, 256 };
Chris@42 33
Chris@42 34 typedef struct {
Chris@42 35 plan_rdft2 super;
Chris@42 36
Chris@42 37 plan *cld, *cldcpy, *cldrest;
Chris@42 38 INT n, vl, nbuf, bufdist;
Chris@42 39 INT ivs_by_nbuf, ovs_by_nbuf;
Chris@42 40 INT ioffset, roffset;
Chris@42 41 } P;
Chris@42 42
Chris@42 43 /* transform a vector input with the help of bufs */
Chris@42 44 static void apply_r2hc(const plan *ego_, R *r0, R *r1, R *cr, R *ci)
Chris@42 45 {
Chris@42 46 const P *ego = (const P *) ego_;
Chris@42 47 plan_rdft2 *cld = (plan_rdft2 *) ego->cld;
Chris@42 48 plan_dft *cldcpy = (plan_dft *) ego->cldcpy;
Chris@42 49 INT i, vl = ego->vl, nbuf = ego->nbuf;
Chris@42 50 INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf;
Chris@42 51 R *bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist, BUFFERS);
Chris@42 52 R *bufr = bufs + ego->roffset;
Chris@42 53 R *bufi = bufs + ego->ioffset;
Chris@42 54 plan_rdft2 *cldrest;
Chris@42 55
Chris@42 56 for (i = nbuf; i <= vl; i += nbuf) {
Chris@42 57 /* transform to bufs: */
Chris@42 58 cld->apply((plan *) cld, r0, r1, bufr, bufi);
Chris@42 59 r0 += ivs_by_nbuf; r1 += ivs_by_nbuf;
Chris@42 60
Chris@42 61 /* copy back */
Chris@42 62 cldcpy->apply((plan *) cldcpy, bufr, bufi, cr, ci);
Chris@42 63 cr += ovs_by_nbuf; ci += ovs_by_nbuf;
Chris@42 64 }
Chris@42 65
Chris@42 66 X(ifree)(bufs);
Chris@42 67
Chris@42 68 /* Do the remaining transforms, if any: */
Chris@42 69 cldrest = (plan_rdft2 *) ego->cldrest;
Chris@42 70 cldrest->apply((plan *) cldrest, r0, r1, cr, ci);
Chris@42 71 }
Chris@42 72
Chris@42 73 /* for hc2r problems, copy the input into buffer, and then
Chris@42 74 transform buffer->output, which allows for destruction of the
Chris@42 75 buffer */
Chris@42 76 static void apply_hc2r(const plan *ego_, R *r0, R *r1, R *cr, R *ci)
Chris@42 77 {
Chris@42 78 const P *ego = (const P *) ego_;
Chris@42 79 plan_rdft2 *cld = (plan_rdft2 *) ego->cld;
Chris@42 80 plan_dft *cldcpy = (plan_dft *) ego->cldcpy;
Chris@42 81 INT i, vl = ego->vl, nbuf = ego->nbuf;
Chris@42 82 INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf;
Chris@42 83 R *bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist, BUFFERS);
Chris@42 84 R *bufr = bufs + ego->roffset;
Chris@42 85 R *bufi = bufs + ego->ioffset;
Chris@42 86 plan_rdft2 *cldrest;
Chris@42 87
Chris@42 88 for (i = nbuf; i <= vl; i += nbuf) {
Chris@42 89 /* copy input into bufs: */
Chris@42 90 cldcpy->apply((plan *) cldcpy, cr, ci, bufr, bufi);
Chris@42 91 cr += ivs_by_nbuf; ci += ivs_by_nbuf;
Chris@42 92
Chris@42 93 /* transform to output */
Chris@42 94 cld->apply((plan *) cld, r0, r1, bufr, bufi);
Chris@42 95 r0 += ovs_by_nbuf; r1 += ovs_by_nbuf;
Chris@42 96 }
Chris@42 97
Chris@42 98 X(ifree)(bufs);
Chris@42 99
Chris@42 100 /* Do the remaining transforms, if any: */
Chris@42 101 cldrest = (plan_rdft2 *) ego->cldrest;
Chris@42 102 cldrest->apply((plan *) cldrest, r0, r1, cr, ci);
Chris@42 103 }
Chris@42 104
Chris@42 105
Chris@42 106 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@42 107 {
Chris@42 108 P *ego = (P *) ego_;
Chris@42 109
Chris@42 110 X(plan_awake)(ego->cld, wakefulness);
Chris@42 111 X(plan_awake)(ego->cldcpy, wakefulness);
Chris@42 112 X(plan_awake)(ego->cldrest, wakefulness);
Chris@42 113 }
Chris@42 114
Chris@42 115 static void destroy(plan *ego_)
Chris@42 116 {
Chris@42 117 P *ego = (P *) ego_;
Chris@42 118 X(plan_destroy_internal)(ego->cldrest);
Chris@42 119 X(plan_destroy_internal)(ego->cldcpy);
Chris@42 120 X(plan_destroy_internal)(ego->cld);
Chris@42 121 }
Chris@42 122
Chris@42 123 static void print(const plan *ego_, printer *p)
Chris@42 124 {
Chris@42 125 const P *ego = (const P *) ego_;
Chris@42 126 p->print(p, "(rdft2-buffered-%D%v/%D-%D%(%p%)%(%p%)%(%p%))",
Chris@42 127 ego->n, ego->nbuf,
Chris@42 128 ego->vl, ego->bufdist % ego->n,
Chris@42 129 ego->cld, ego->cldcpy, ego->cldrest);
Chris@42 130 }
Chris@42 131
Chris@42 132 static int applicable0(const S *ego, const problem *p_, const planner *plnr)
Chris@42 133 {
Chris@42 134 const problem_rdft2 *p = (const problem_rdft2 *) p_;
Chris@42 135 iodim *d = p->sz->dims;
Chris@42 136
Chris@42 137 if (1
Chris@42 138 && p->vecsz->rnk <= 1
Chris@42 139 && p->sz->rnk == 1
Chris@42 140
Chris@42 141 /* we assume even n throughout */
Chris@42 142 && (d[0].n % 2) == 0
Chris@42 143
Chris@42 144 /* and we only consider these two cases */
Chris@42 145 && (p->kind == R2HC || p->kind == HC2R)
Chris@42 146
Chris@42 147 ) {
Chris@42 148 INT vl, ivs, ovs;
Chris@42 149 X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs);
Chris@42 150
Chris@42 151 if (X(toobig)(d[0].n) && CONSERVE_MEMORYP(plnr))
Chris@42 152 return 0;
Chris@42 153
Chris@42 154 /* if this solver is redundant, in the sense that a solver
Chris@42 155 of lower index generates the same plan, then prune this
Chris@42 156 solver */
Chris@42 157 if (X(nbuf_redundant)(d[0].n, vl,
Chris@42 158 ego->maxnbuf_ndx,
Chris@42 159 maxnbufs, NELEM(maxnbufs)))
Chris@42 160 return 0;
Chris@42 161
Chris@42 162 if (p->r0 != p->cr) {
Chris@42 163 if (p->kind == HC2R) {
Chris@42 164 /* Allow HC2R problems only if the input is to be
Chris@42 165 preserved. This solver sets NO_DESTROY_INPUT,
Chris@42 166 which prevents infinite loops */
Chris@42 167 return (NO_DESTROY_INPUTP(plnr));
Chris@42 168 } else {
Chris@42 169 /*
Chris@42 170 In principle, the buffered transforms might be useful
Chris@42 171 when working out of place. However, in order to
Chris@42 172 prevent infinite loops in the planner, we require
Chris@42 173 that the output stride of the buffered transforms be
Chris@42 174 greater than 2.
Chris@42 175 */
Chris@42 176 return (d[0].os > 2);
Chris@42 177 }
Chris@42 178 }
Chris@42 179
Chris@42 180 /*
Chris@42 181 * If the problem is in place, the input/output strides must
Chris@42 182 * be the same or the whole thing must fit in the buffer.
Chris@42 183 */
Chris@42 184 if (X(rdft2_inplace_strides(p, RNK_MINFTY)))
Chris@42 185 return 1;
Chris@42 186
Chris@42 187 if (/* fits into buffer: */
Chris@42 188 ((p->vecsz->rnk == 0)
Chris@42 189 ||
Chris@42 190 (X(nbuf)(d[0].n, p->vecsz->dims[0].n,
Chris@42 191 maxnbufs[ego->maxnbuf_ndx])
Chris@42 192 == p->vecsz->dims[0].n)))
Chris@42 193 return 1;
Chris@42 194 }
Chris@42 195
Chris@42 196 return 0;
Chris@42 197 }
Chris@42 198
Chris@42 199 static int applicable(const S *ego, const problem *p_, const planner *plnr)
Chris@42 200 {
Chris@42 201 const problem_rdft2 *p;
Chris@42 202
Chris@42 203 if (NO_BUFFERINGP(plnr)) return 0;
Chris@42 204
Chris@42 205 if (!applicable0(ego, p_, plnr)) return 0;
Chris@42 206
Chris@42 207 p = (const problem_rdft2 *) p_;
Chris@42 208 if (p->kind == HC2R) {
Chris@42 209 if (NO_UGLYP(plnr)) {
Chris@42 210 /* UGLY if in-place and too big, since the problem
Chris@42 211 could be solved via transpositions */
Chris@42 212 if (p->r0 == p->cr && X(toobig)(p->sz->dims[0].n))
Chris@42 213 return 0;
Chris@42 214 }
Chris@42 215 } else {
Chris@42 216 if (NO_UGLYP(plnr)) {
Chris@42 217 if (p->r0 != p->cr || X(toobig)(p->sz->dims[0].n))
Chris@42 218 return 0;
Chris@42 219 }
Chris@42 220 }
Chris@42 221 return 1;
Chris@42 222 }
Chris@42 223
Chris@42 224 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@42 225 {
Chris@42 226 P *pln;
Chris@42 227 const S *ego = (const S *)ego_;
Chris@42 228 plan *cld = (plan *) 0;
Chris@42 229 plan *cldcpy = (plan *) 0;
Chris@42 230 plan *cldrest = (plan *) 0;
Chris@42 231 const problem_rdft2 *p = (const problem_rdft2 *) p_;
Chris@42 232 R *bufs = (R *) 0;
Chris@42 233 INT nbuf = 0, bufdist, n, vl;
Chris@42 234 INT ivs, ovs, ioffset, roffset, id, od;
Chris@42 235
Chris@42 236 static const plan_adt padt = {
Chris@42 237 X(rdft2_solve), awake, print, destroy
Chris@42 238 };
Chris@42 239
Chris@42 240 if (!applicable(ego, p_, plnr))
Chris@42 241 goto nada;
Chris@42 242
Chris@42 243 n = X(tensor_sz)(p->sz);
Chris@42 244 X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs);
Chris@42 245
Chris@42 246 nbuf = X(nbuf)(n, vl, maxnbufs[ego->maxnbuf_ndx]);
Chris@42 247 bufdist = X(bufdist)(n + 2, vl); /* complex-side rdft2 stores N+2
Chris@42 248 real numbers */
Chris@42 249 A(nbuf > 0);
Chris@42 250
Chris@42 251 /* attempt to keep real and imaginary part in the same order,
Chris@42 252 so as to allow optimizations in the the copy plan */
Chris@42 253 roffset = (p->cr - p->ci > 0) ? (INT)1 : (INT)0;
Chris@42 254 ioffset = 1 - roffset;
Chris@42 255
Chris@42 256 /* initial allocation for the purpose of planning */
Chris@42 257 bufs = (R *) MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS);
Chris@42 258
Chris@42 259 id = ivs * (nbuf * (vl / nbuf));
Chris@42 260 od = ovs * (nbuf * (vl / nbuf));
Chris@42 261
Chris@42 262 if (p->kind == R2HC) {
Chris@42 263 /* allow destruction of input if problem is in place */
Chris@42 264 cld = X(mkplan_f_d)(
Chris@42 265 plnr,
Chris@42 266 X(mkproblem_rdft2_d)(
Chris@42 267 X(mktensor_1d)(n, p->sz->dims[0].is, 2),
Chris@42 268 X(mktensor_1d)(nbuf, ivs, bufdist),
Chris@42 269 TAINT(p->r0, ivs * nbuf), TAINT(p->r1, ivs * nbuf),
Chris@42 270 bufs + roffset, bufs + ioffset, p->kind),
Chris@42 271 0, 0, (p->r0 == p->cr) ? NO_DESTROY_INPUT : 0);
Chris@42 272 if (!cld) goto nada;
Chris@42 273
Chris@42 274 /* copying back from the buffer is a rank-0 DFT: */
Chris@42 275 cldcpy = X(mkplan_d)(
Chris@42 276 plnr,
Chris@42 277 X(mkproblem_dft_d)(
Chris@42 278 X(mktensor_0d)(),
Chris@42 279 X(mktensor_2d)(nbuf, bufdist, ovs,
Chris@42 280 n/2+1, 2, p->sz->dims[0].os),
Chris@42 281 bufs + roffset, bufs + ioffset,
Chris@42 282 TAINT(p->cr, ovs * nbuf), TAINT(p->ci, ovs * nbuf) ));
Chris@42 283 if (!cldcpy) goto nada;
Chris@42 284
Chris@42 285 X(ifree)(bufs); bufs = 0;
Chris@42 286
Chris@42 287 cldrest = X(mkplan_d)(plnr,
Chris@42 288 X(mkproblem_rdft2_d)(
Chris@42 289 X(tensor_copy)(p->sz),
Chris@42 290 X(mktensor_1d)(vl % nbuf, ivs, ovs),
Chris@42 291 p->r0 + id, p->r1 + id,
Chris@42 292 p->cr + od, p->ci + od,
Chris@42 293 p->kind));
Chris@42 294 if (!cldrest) goto nada;
Chris@42 295 pln = MKPLAN_RDFT2(P, &padt, apply_r2hc);
Chris@42 296 } else {
Chris@42 297 /* allow destruction of buffer */
Chris@42 298 cld = X(mkplan_f_d)(
Chris@42 299 plnr,
Chris@42 300 X(mkproblem_rdft2_d)(
Chris@42 301 X(mktensor_1d)(n, 2, p->sz->dims[0].os),
Chris@42 302 X(mktensor_1d)(nbuf, bufdist, ovs),
Chris@42 303 TAINT(p->r0, ovs * nbuf), TAINT(p->r1, ovs * nbuf),
Chris@42 304 bufs + roffset, bufs + ioffset, p->kind),
Chris@42 305 0, 0, NO_DESTROY_INPUT);
Chris@42 306 if (!cld) goto nada;
Chris@42 307
Chris@42 308 /* copying input into buffer is a rank-0 DFT: */
Chris@42 309 cldcpy = X(mkplan_d)(
Chris@42 310 plnr,
Chris@42 311 X(mkproblem_dft_d)(
Chris@42 312 X(mktensor_0d)(),
Chris@42 313 X(mktensor_2d)(nbuf, ivs, bufdist,
Chris@42 314 n/2+1, p->sz->dims[0].is, 2),
Chris@42 315 TAINT(p->cr, ivs * nbuf), TAINT(p->ci, ivs * nbuf),
Chris@42 316 bufs + roffset, bufs + ioffset));
Chris@42 317 if (!cldcpy) goto nada;
Chris@42 318
Chris@42 319 X(ifree)(bufs); bufs = 0;
Chris@42 320
Chris@42 321 cldrest = X(mkplan_d)(plnr,
Chris@42 322 X(mkproblem_rdft2_d)(
Chris@42 323 X(tensor_copy)(p->sz),
Chris@42 324 X(mktensor_1d)(vl % nbuf, ivs, ovs),
Chris@42 325 p->r0 + od, p->r1 + od,
Chris@42 326 p->cr + id, p->ci + id,
Chris@42 327 p->kind));
Chris@42 328 if (!cldrest) goto nada;
Chris@42 329
Chris@42 330 pln = MKPLAN_RDFT2(P, &padt, apply_hc2r);
Chris@42 331 }
Chris@42 332
Chris@42 333 pln->cld = cld;
Chris@42 334 pln->cldcpy = cldcpy;
Chris@42 335 pln->cldrest = cldrest;
Chris@42 336 pln->n = n;
Chris@42 337 pln->vl = vl;
Chris@42 338 pln->ivs_by_nbuf = ivs * nbuf;
Chris@42 339 pln->ovs_by_nbuf = ovs * nbuf;
Chris@42 340 pln->roffset = roffset;
Chris@42 341 pln->ioffset = ioffset;
Chris@42 342
Chris@42 343 pln->nbuf = nbuf;
Chris@42 344 pln->bufdist = bufdist;
Chris@42 345
Chris@42 346 {
Chris@42 347 opcnt t;
Chris@42 348 X(ops_add)(&cld->ops, &cldcpy->ops, &t);
Chris@42 349 X(ops_madd)(vl / nbuf, &t, &cldrest->ops, &pln->super.super.ops);
Chris@42 350 }
Chris@42 351
Chris@42 352 return &(pln->super.super);
Chris@42 353
Chris@42 354 nada:
Chris@42 355 X(ifree0)(bufs);
Chris@42 356 X(plan_destroy_internal)(cldrest);
Chris@42 357 X(plan_destroy_internal)(cldcpy);
Chris@42 358 X(plan_destroy_internal)(cld);
Chris@42 359 return (plan *) 0;
Chris@42 360 }
Chris@42 361
Chris@42 362 static solver *mksolver(size_t maxnbuf_ndx)
Chris@42 363 {
Chris@42 364 static const solver_adt sadt = { PROBLEM_RDFT2, mkplan, 0 };
Chris@42 365 S *slv = MKSOLVER(S, &sadt);
Chris@42 366 slv->maxnbuf_ndx = maxnbuf_ndx;
Chris@42 367 return &(slv->super);
Chris@42 368 }
Chris@42 369
Chris@42 370 void X(rdft2_buffered_register)(planner *p)
Chris@42 371 {
Chris@42 372 size_t i;
Chris@42 373 for (i = 0; i < NELEM(maxnbufs); ++i)
Chris@42 374 REGISTER_SOLVER(p, mksolver(i));
Chris@42 375 }