annotate src/fftw-3.3.5/rdft/direct-r2c.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 /* direct RDFT solver, using r2c codelets */
Chris@42 23
Chris@42 24 #include "rdft.h"
Chris@42 25
Chris@42 26 typedef struct {
Chris@42 27 solver super;
Chris@42 28 const kr2c_desc *desc;
Chris@42 29 kr2c k;
Chris@42 30 int bufferedp;
Chris@42 31 } S;
Chris@42 32
Chris@42 33 typedef struct {
Chris@42 34 plan_rdft super;
Chris@42 35
Chris@42 36 stride rs, csr, csi;
Chris@42 37 stride brs, bcsr, bcsi;
Chris@42 38 INT n, vl, rs0, ivs, ovs, ioffset, bioffset;
Chris@42 39 kr2c k;
Chris@42 40 const S *slv;
Chris@42 41 } P;
Chris@42 42
Chris@42 43 /*************************************************************
Chris@42 44 Nonbuffered code
Chris@42 45 *************************************************************/
Chris@42 46 static void apply_r2hc(const plan *ego_, R *I, R *O)
Chris@42 47 {
Chris@42 48 const P *ego = (const P *) ego_;
Chris@42 49 ASSERT_ALIGNED_DOUBLE;
Chris@42 50 ego->k(I, I + ego->rs0, O, O + ego->ioffset,
Chris@42 51 ego->rs, ego->csr, ego->csi,
Chris@42 52 ego->vl, ego->ivs, ego->ovs);
Chris@42 53 }
Chris@42 54
Chris@42 55 static void apply_hc2r(const plan *ego_, R *I, R *O)
Chris@42 56 {
Chris@42 57 const P *ego = (const P *) ego_;
Chris@42 58 ASSERT_ALIGNED_DOUBLE;
Chris@42 59 ego->k(O, O + ego->rs0, I, I + ego->ioffset,
Chris@42 60 ego->rs, ego->csr, ego->csi,
Chris@42 61 ego->vl, ego->ivs, ego->ovs);
Chris@42 62 }
Chris@42 63
Chris@42 64 /*************************************************************
Chris@42 65 Buffered code
Chris@42 66 *************************************************************/
Chris@42 67 /* should not be 2^k to avoid associativity conflicts */
Chris@42 68 static INT compute_batchsize(INT radix)
Chris@42 69 {
Chris@42 70 /* round up to multiple of 4 */
Chris@42 71 radix += 3;
Chris@42 72 radix &= -4;
Chris@42 73
Chris@42 74 return (radix + 2);
Chris@42 75 }
Chris@42 76
Chris@42 77 static void dobatch_r2hc(const P *ego, R *I, R *O, R *buf, INT batchsz)
Chris@42 78 {
Chris@42 79 X(cpy2d_ci)(I, buf,
Chris@42 80 ego->n, ego->rs0, WS(ego->bcsr /* hack */, 1),
Chris@42 81 batchsz, ego->ivs, 1, 1);
Chris@42 82
Chris@42 83 if (IABS(WS(ego->csr, 1)) < IABS(ego->ovs)) {
Chris@42 84 /* transform directly to output */
Chris@42 85 ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
Chris@42 86 O, O + ego->ioffset,
Chris@42 87 ego->brs, ego->csr, ego->csi,
Chris@42 88 batchsz, 1, ego->ovs);
Chris@42 89 } else {
Chris@42 90 /* transform to buffer and copy back */
Chris@42 91 ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
Chris@42 92 buf, buf + ego->bioffset,
Chris@42 93 ego->brs, ego->bcsr, ego->bcsi,
Chris@42 94 batchsz, 1, 1);
Chris@42 95 X(cpy2d_co)(buf, O,
Chris@42 96 ego->n, WS(ego->bcsr, 1), WS(ego->csr, 1),
Chris@42 97 batchsz, 1, ego->ovs, 1);
Chris@42 98 }
Chris@42 99 }
Chris@42 100
Chris@42 101 static void dobatch_hc2r(const P *ego, R *I, R *O, R *buf, INT batchsz)
Chris@42 102 {
Chris@42 103 if (IABS(WS(ego->csr, 1)) < IABS(ego->ivs)) {
Chris@42 104 /* transform directly from input */
Chris@42 105 ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
Chris@42 106 I, I + ego->ioffset,
Chris@42 107 ego->brs, ego->csr, ego->csi,
Chris@42 108 batchsz, ego->ivs, 1);
Chris@42 109 } else {
Chris@42 110 /* copy into buffer and transform in place */
Chris@42 111 X(cpy2d_ci)(I, buf,
Chris@42 112 ego->n, WS(ego->csr, 1), WS(ego->bcsr, 1),
Chris@42 113 batchsz, ego->ivs, 1, 1);
Chris@42 114 ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
Chris@42 115 buf, buf + ego->bioffset,
Chris@42 116 ego->brs, ego->bcsr, ego->bcsi,
Chris@42 117 batchsz, 1, 1);
Chris@42 118 }
Chris@42 119 X(cpy2d_co)(buf, O,
Chris@42 120 ego->n, WS(ego->bcsr /* hack */, 1), ego->rs0,
Chris@42 121 batchsz, 1, ego->ovs, 1);
Chris@42 122 }
Chris@42 123
Chris@42 124 static void iterate(const P *ego, R *I, R *O,
Chris@42 125 void (*dobatch)(const P *ego, R *I, R *O,
Chris@42 126 R *buf, INT batchsz))
Chris@42 127 {
Chris@42 128 R *buf;
Chris@42 129 INT vl = ego->vl;
Chris@42 130 INT n = ego->n;
Chris@42 131 INT i;
Chris@42 132 INT batchsz = compute_batchsize(n);
Chris@42 133 size_t bufsz = n * batchsz * sizeof(R);
Chris@42 134
Chris@42 135 BUF_ALLOC(R *, buf, bufsz);
Chris@42 136
Chris@42 137 for (i = 0; i < vl - batchsz; i += batchsz) {
Chris@42 138 dobatch(ego, I, O, buf, batchsz);
Chris@42 139 I += batchsz * ego->ivs;
Chris@42 140 O += batchsz * ego->ovs;
Chris@42 141 }
Chris@42 142 dobatch(ego, I, O, buf, vl - i);
Chris@42 143
Chris@42 144 BUF_FREE(buf, bufsz);
Chris@42 145 }
Chris@42 146
Chris@42 147 static void apply_buf_r2hc(const plan *ego_, R *I, R *O)
Chris@42 148 {
Chris@42 149 iterate((const P *) ego_, I, O, dobatch_r2hc);
Chris@42 150 }
Chris@42 151
Chris@42 152 static void apply_buf_hc2r(const plan *ego_, R *I, R *O)
Chris@42 153 {
Chris@42 154 iterate((const P *) ego_, I, O, dobatch_hc2r);
Chris@42 155 }
Chris@42 156
Chris@42 157 static void destroy(plan *ego_)
Chris@42 158 {
Chris@42 159 P *ego = (P *) ego_;
Chris@42 160 X(stride_destroy)(ego->rs);
Chris@42 161 X(stride_destroy)(ego->csr);
Chris@42 162 X(stride_destroy)(ego->csi);
Chris@42 163 X(stride_destroy)(ego->brs);
Chris@42 164 X(stride_destroy)(ego->bcsr);
Chris@42 165 X(stride_destroy)(ego->bcsi);
Chris@42 166 }
Chris@42 167
Chris@42 168 static void print(const plan *ego_, printer *p)
Chris@42 169 {
Chris@42 170 const P *ego = (const P *) ego_;
Chris@42 171 const S *s = ego->slv;
Chris@42 172
Chris@42 173 if (ego->slv->bufferedp)
Chris@42 174 p->print(p, "(rdft-%s-directbuf/%D-r2c-%D%v \"%s\")",
Chris@42 175 X(rdft_kind_str)(s->desc->genus->kind),
Chris@42 176 /* hack */ WS(ego->bcsr, 1), ego->n,
Chris@42 177 ego->vl, s->desc->nam);
Chris@42 178
Chris@42 179 else
Chris@42 180 p->print(p, "(rdft-%s-direct-r2c-%D%v \"%s\")",
Chris@42 181 X(rdft_kind_str)(s->desc->genus->kind), ego->n,
Chris@42 182 ego->vl, s->desc->nam);
Chris@42 183 }
Chris@42 184
Chris@42 185 static INT ioffset(rdft_kind kind, INT sz, INT s)
Chris@42 186 {
Chris@42 187 return(s * ((kind == R2HC || kind == HC2R) ? sz : (sz - 1)));
Chris@42 188 }
Chris@42 189
Chris@42 190 static int applicable(const solver *ego_, const problem *p_)
Chris@42 191 {
Chris@42 192 const S *ego = (const S *) ego_;
Chris@42 193 const kr2c_desc *desc = ego->desc;
Chris@42 194 const problem_rdft *p = (const problem_rdft *) p_;
Chris@42 195 INT vl, ivs, ovs;
Chris@42 196
Chris@42 197 return (
Chris@42 198 1
Chris@42 199 && p->sz->rnk == 1
Chris@42 200 && p->vecsz->rnk <= 1
Chris@42 201 && p->sz->dims[0].n == desc->n
Chris@42 202 && p->kind[0] == desc->genus->kind
Chris@42 203
Chris@42 204 /* check strides etc */
Chris@42 205 && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs)
Chris@42 206
Chris@42 207 && (0
Chris@42 208 /* can operate out-of-place */
Chris@42 209 || p->I != p->O
Chris@42 210
Chris@42 211 /* computing one transform */
Chris@42 212 || vl == 1
Chris@42 213
Chris@42 214 /* can operate in-place as long as strides are the same */
Chris@42 215 || X(tensor_inplace_strides2)(p->sz, p->vecsz)
Chris@42 216 )
Chris@42 217 );
Chris@42 218 }
Chris@42 219
Chris@42 220 static int applicable_buf(const solver *ego_, const problem *p_)
Chris@42 221 {
Chris@42 222 const S *ego = (const S *) ego_;
Chris@42 223 const kr2c_desc *desc = ego->desc;
Chris@42 224 const problem_rdft *p = (const problem_rdft *) p_;
Chris@42 225 INT vl, ivs, ovs, batchsz;
Chris@42 226
Chris@42 227 return (
Chris@42 228 1
Chris@42 229 && p->sz->rnk == 1
Chris@42 230 && p->vecsz->rnk <= 1
Chris@42 231 && p->sz->dims[0].n == desc->n
Chris@42 232 && p->kind[0] == desc->genus->kind
Chris@42 233
Chris@42 234 /* check strides etc */
Chris@42 235 && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs)
Chris@42 236
Chris@42 237 && (batchsz = compute_batchsize(desc->n), 1)
Chris@42 238
Chris@42 239 && (0
Chris@42 240 /* can operate out-of-place */
Chris@42 241 || p->I != p->O
Chris@42 242
Chris@42 243 /* can operate in-place as long as strides are the same */
Chris@42 244 || X(tensor_inplace_strides2)(p->sz, p->vecsz)
Chris@42 245
Chris@42 246 /* can do it if the problem fits in the buffer, no matter
Chris@42 247 what the strides are */
Chris@42 248 || vl <= batchsz
Chris@42 249 )
Chris@42 250 );
Chris@42 251 }
Chris@42 252
Chris@42 253 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@42 254 {
Chris@42 255 const S *ego = (const S *) ego_;
Chris@42 256 P *pln;
Chris@42 257 const problem_rdft *p;
Chris@42 258 iodim *d;
Chris@42 259 INT rs, cs, b, n;
Chris@42 260
Chris@42 261 static const plan_adt padt = {
Chris@42 262 X(rdft_solve), X(null_awake), print, destroy
Chris@42 263 };
Chris@42 264
Chris@42 265 UNUSED(plnr);
Chris@42 266
Chris@42 267 if (ego->bufferedp) {
Chris@42 268 if (!applicable_buf(ego_, p_))
Chris@42 269 return (plan *)0;
Chris@42 270 } else {
Chris@42 271 if (!applicable(ego_, p_))
Chris@42 272 return (plan *)0;
Chris@42 273 }
Chris@42 274
Chris@42 275 p = (const problem_rdft *) p_;
Chris@42 276
Chris@42 277 if (R2HC_KINDP(p->kind[0])) {
Chris@42 278 rs = p->sz->dims[0].is; cs = p->sz->dims[0].os;
Chris@42 279 pln = MKPLAN_RDFT(P, &padt,
Chris@42 280 ego->bufferedp ? apply_buf_r2hc : apply_r2hc);
Chris@42 281 } else {
Chris@42 282 rs = p->sz->dims[0].os; cs = p->sz->dims[0].is;
Chris@42 283 pln = MKPLAN_RDFT(P, &padt,
Chris@42 284 ego->bufferedp ? apply_buf_hc2r : apply_hc2r);
Chris@42 285 }
Chris@42 286
Chris@42 287 d = p->sz->dims;
Chris@42 288 n = d[0].n;
Chris@42 289
Chris@42 290 pln->k = ego->k;
Chris@42 291 pln->n = n;
Chris@42 292
Chris@42 293 pln->rs0 = rs;
Chris@42 294 pln->rs = X(mkstride)(n, 2 * rs);
Chris@42 295 pln->csr = X(mkstride)(n, cs);
Chris@42 296 pln->csi = X(mkstride)(n, -cs);
Chris@42 297 pln->ioffset = ioffset(p->kind[0], n, cs);
Chris@42 298
Chris@42 299 b = compute_batchsize(n);
Chris@42 300 pln->brs = X(mkstride)(n, 2 * b);
Chris@42 301 pln->bcsr = X(mkstride)(n, b);
Chris@42 302 pln->bcsi = X(mkstride)(n, -b);
Chris@42 303 pln->bioffset = ioffset(p->kind[0], n, b);
Chris@42 304
Chris@42 305 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
Chris@42 306
Chris@42 307 pln->slv = ego;
Chris@42 308 X(ops_zero)(&pln->super.super.ops);
Chris@42 309
Chris@42 310 X(ops_madd2)(pln->vl / ego->desc->genus->vl,
Chris@42 311 &ego->desc->ops,
Chris@42 312 &pln->super.super.ops);
Chris@42 313
Chris@42 314 if (ego->bufferedp)
Chris@42 315 pln->super.super.ops.other += 2 * n * pln->vl;
Chris@42 316
Chris@42 317 pln->super.super.could_prune_now_p = !ego->bufferedp;
Chris@42 318
Chris@42 319 return &(pln->super.super);
Chris@42 320 }
Chris@42 321
Chris@42 322 /* constructor */
Chris@42 323 static solver *mksolver(kr2c k, const kr2c_desc *desc, int bufferedp)
Chris@42 324 {
Chris@42 325 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
Chris@42 326 S *slv = MKSOLVER(S, &sadt);
Chris@42 327 slv->k = k;
Chris@42 328 slv->desc = desc;
Chris@42 329 slv->bufferedp = bufferedp;
Chris@42 330 return &(slv->super);
Chris@42 331 }
Chris@42 332
Chris@42 333 solver *X(mksolver_rdft_r2c_direct)(kr2c k, const kr2c_desc *desc)
Chris@42 334 {
Chris@42 335 return mksolver(k, desc, 0);
Chris@42 336 }
Chris@42 337
Chris@42 338 solver *X(mksolver_rdft_r2c_directbuf)(kr2c k, const kr2c_desc *desc)
Chris@42 339 {
Chris@42 340 return mksolver(k, desc, 1);
Chris@42 341 }