annotate src/fftw-3.3.3/kernel/trig.c @ 23:619f715526df sv_v2.1

Update Vamp plugin SDK to 2.5
author Chris Cannam
date Thu, 09 May 2013 10:52:46 +0100
parents 37bf6b4a2645
children
rev   line source
Chris@10 1 /*
Chris@10 2 * Copyright (c) 2003, 2007-11 Matteo Frigo
Chris@10 3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
Chris@10 4 *
Chris@10 5 * This program is free software; you can redistribute it and/or modify
Chris@10 6 * it under the terms of the GNU General Public License as published by
Chris@10 7 * the Free Software Foundation; either version 2 of the License, or
Chris@10 8 * (at your option) any later version.
Chris@10 9 *
Chris@10 10 * This program is distributed in the hope that it will be useful,
Chris@10 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@10 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@10 13 * GNU General Public License for more details.
Chris@10 14 *
Chris@10 15 * You should have received a copy of the GNU General Public License
Chris@10 16 * along with this program; if not, write to the Free Software
Chris@10 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@10 18 *
Chris@10 19 */
Chris@10 20
Chris@10 21
Chris@10 22 /* trigonometric functions */
Chris@10 23 #include "ifftw.h"
Chris@10 24 #include <math.h>
Chris@10 25
Chris@10 26 #if defined(TRIGREAL_IS_LONG_DOUBLE)
Chris@10 27 # define COS cosl
Chris@10 28 # define SIN sinl
Chris@10 29 # define KTRIG(x) (x##L)
Chris@10 30 # ifndef HAVE_DECL_SINL
Chris@10 31 extern long double sinl(long double x);
Chris@10 32 # endif
Chris@10 33 # ifndef HAVE_DECL_COSL
Chris@10 34 extern long double cosl(long double x);
Chris@10 35 # endif
Chris@10 36 #elif defined(TRIGREAL_IS_QUAD)
Chris@10 37 # define COS cosq
Chris@10 38 # define SIN sinq
Chris@10 39 # define KTRIG(x) (x##Q)
Chris@10 40 extern __float128 sinq(__float128 x);
Chris@10 41 extern __float128 cosq(__float128 x);
Chris@10 42 #else
Chris@10 43 # define COS cos
Chris@10 44 # define SIN sin
Chris@10 45 # define KTRIG(x) (x)
Chris@10 46 #endif
Chris@10 47
Chris@10 48 static const trigreal K2PI =
Chris@10 49 KTRIG(6.2831853071795864769252867665590057683943388);
Chris@10 50 #define by2pi(m, n) ((K2PI * (m)) / (n))
Chris@10 51
Chris@10 52 /*
Chris@10 53 * Improve accuracy by reducing x to range [0..1/8]
Chris@10 54 * before multiplication by 2 * PI.
Chris@10 55 */
Chris@10 56
Chris@10 57 static void real_cexp(INT m, INT n, trigreal *out)
Chris@10 58 {
Chris@10 59 trigreal theta, c, s, t;
Chris@10 60 unsigned octant = 0;
Chris@10 61 INT quarter_n = n;
Chris@10 62
Chris@10 63 n += n; n += n;
Chris@10 64 m += m; m += m;
Chris@10 65
Chris@10 66 if (m < 0) m += n;
Chris@10 67 if (m > n - m) { m = n - m; octant |= 4; }
Chris@10 68 if (m - quarter_n > 0) { m = m - quarter_n; octant |= 2; }
Chris@10 69 if (m > quarter_n - m) { m = quarter_n - m; octant |= 1; }
Chris@10 70
Chris@10 71 theta = by2pi(m, n);
Chris@10 72 c = COS(theta); s = SIN(theta);
Chris@10 73
Chris@10 74 if (octant & 1) { t = c; c = s; s = t; }
Chris@10 75 if (octant & 2) { t = c; c = -s; s = t; }
Chris@10 76 if (octant & 4) { s = -s; }
Chris@10 77
Chris@10 78 out[0] = c;
Chris@10 79 out[1] = s;
Chris@10 80 }
Chris@10 81
Chris@10 82 static INT choose_twshft(INT n)
Chris@10 83 {
Chris@10 84 INT log2r = 0;
Chris@10 85 while (n > 0) {
Chris@10 86 ++log2r;
Chris@10 87 n /= 4;
Chris@10 88 }
Chris@10 89 return log2r;
Chris@10 90 }
Chris@10 91
Chris@10 92 static void cexpl_sqrtn_table(triggen *p, INT m, trigreal *res)
Chris@10 93 {
Chris@10 94 m += p->n * (m < 0);
Chris@10 95
Chris@10 96 {
Chris@10 97 INT m0 = m & p->twmsk;
Chris@10 98 INT m1 = m >> p->twshft;
Chris@10 99 trigreal wr0 = p->W0[2 * m0];
Chris@10 100 trigreal wi0 = p->W0[2 * m0 + 1];
Chris@10 101 trigreal wr1 = p->W1[2 * m1];
Chris@10 102 trigreal wi1 = p->W1[2 * m1 + 1];
Chris@10 103
Chris@10 104 res[0] = wr1 * wr0 - wi1 * wi0;
Chris@10 105 res[1] = wi1 * wr0 + wr1 * wi0;
Chris@10 106 }
Chris@10 107 }
Chris@10 108
Chris@10 109 /* multiply (xr, xi) by exp(FFT_SIGN * 2*pi*i*m/n) */
Chris@10 110 static void rotate_sqrtn_table(triggen *p, INT m, R xr, R xi, R *res)
Chris@10 111 {
Chris@10 112 m += p->n * (m < 0);
Chris@10 113
Chris@10 114 {
Chris@10 115 INT m0 = m & p->twmsk;
Chris@10 116 INT m1 = m >> p->twshft;
Chris@10 117 trigreal wr0 = p->W0[2 * m0];
Chris@10 118 trigreal wi0 = p->W0[2 * m0 + 1];
Chris@10 119 trigreal wr1 = p->W1[2 * m1];
Chris@10 120 trigreal wi1 = p->W1[2 * m1 + 1];
Chris@10 121 trigreal wr = wr1 * wr0 - wi1 * wi0;
Chris@10 122 trigreal wi = wi1 * wr0 + wr1 * wi0;
Chris@10 123
Chris@10 124 #if FFT_SIGN == -1
Chris@10 125 res[0] = xr * wr + xi * wi;
Chris@10 126 res[1] = xi * wr - xr * wi;
Chris@10 127 #else
Chris@10 128 res[0] = xr * wr - xi * wi;
Chris@10 129 res[1] = xi * wr + xr * wi;
Chris@10 130 #endif
Chris@10 131 }
Chris@10 132 }
Chris@10 133
Chris@10 134 static void cexpl_sincos(triggen *p, INT m, trigreal *res)
Chris@10 135 {
Chris@10 136 real_cexp(m, p->n, res);
Chris@10 137 }
Chris@10 138
Chris@10 139 static void cexp_zero(triggen *p, INT m, R *res)
Chris@10 140 {
Chris@10 141 UNUSED(p); UNUSED(m);
Chris@10 142 res[0] = 0;
Chris@10 143 res[1] = 0;
Chris@10 144 }
Chris@10 145
Chris@10 146 static void cexpl_zero(triggen *p, INT m, trigreal *res)
Chris@10 147 {
Chris@10 148 UNUSED(p); UNUSED(m);
Chris@10 149 res[0] = 0;
Chris@10 150 res[1] = 0;
Chris@10 151 }
Chris@10 152
Chris@10 153 static void cexp_generic(triggen *p, INT m, R *res)
Chris@10 154 {
Chris@10 155 trigreal resl[2];
Chris@10 156 p->cexpl(p, m, resl);
Chris@10 157 res[0] = (R)resl[0];
Chris@10 158 res[1] = (R)resl[1];
Chris@10 159 }
Chris@10 160
Chris@10 161 static void rotate_generic(triggen *p, INT m, R xr, R xi, R *res)
Chris@10 162 {
Chris@10 163 trigreal w[2];
Chris@10 164 p->cexpl(p, m, w);
Chris@10 165 res[0] = xr * w[0] - xi * (FFT_SIGN * w[1]);
Chris@10 166 res[1] = xi * w[0] + xr * (FFT_SIGN * w[1]);
Chris@10 167 }
Chris@10 168
Chris@10 169 triggen *X(mktriggen)(enum wakefulness wakefulness, INT n)
Chris@10 170 {
Chris@10 171 INT i, n0, n1;
Chris@10 172 triggen *p = (triggen *)MALLOC(sizeof(*p), TWIDDLES);
Chris@10 173
Chris@10 174 p->n = n;
Chris@10 175 p->W0 = p->W1 = 0;
Chris@10 176 p->cexp = 0;
Chris@10 177 p->rotate = 0;
Chris@10 178
Chris@10 179 switch (wakefulness) {
Chris@10 180 case SLEEPY:
Chris@10 181 A(0 /* can't happen */);
Chris@10 182 break;
Chris@10 183
Chris@10 184 case AWAKE_SQRTN_TABLE: {
Chris@10 185 INT twshft = choose_twshft(n);
Chris@10 186
Chris@10 187 p->twshft = twshft;
Chris@10 188 p->twradix = ((INT)1) << twshft;
Chris@10 189 p->twmsk = p->twradix - 1;
Chris@10 190
Chris@10 191 n0 = p->twradix;
Chris@10 192 n1 = (n + n0 - 1) / n0;
Chris@10 193
Chris@10 194 p->W0 = (trigreal *)MALLOC(n0 * 2 * sizeof(trigreal), TWIDDLES);
Chris@10 195 p->W1 = (trigreal *)MALLOC(n1 * 2 * sizeof(trigreal), TWIDDLES);
Chris@10 196
Chris@10 197 for (i = 0; i < n0; ++i)
Chris@10 198 real_cexp(i, n, p->W0 + 2 * i);
Chris@10 199
Chris@10 200 for (i = 0; i < n1; ++i)
Chris@10 201 real_cexp(i * p->twradix, n, p->W1 + 2 * i);
Chris@10 202
Chris@10 203 p->cexpl = cexpl_sqrtn_table;
Chris@10 204 p->rotate = rotate_sqrtn_table;
Chris@10 205 break;
Chris@10 206 }
Chris@10 207
Chris@10 208 case AWAKE_SINCOS:
Chris@10 209 p->cexpl = cexpl_sincos;
Chris@10 210 break;
Chris@10 211
Chris@10 212 case AWAKE_ZERO:
Chris@10 213 p->cexp = cexp_zero;
Chris@10 214 p->cexpl = cexpl_zero;
Chris@10 215 break;
Chris@10 216 }
Chris@10 217
Chris@10 218 if (!p->cexp) {
Chris@10 219 if (sizeof(trigreal) == sizeof(R))
Chris@10 220 p->cexp = (void (*)(triggen *, INT, R *))p->cexpl;
Chris@10 221 else
Chris@10 222 p->cexp = cexp_generic;
Chris@10 223 }
Chris@10 224 if (!p->rotate)
Chris@10 225 p->rotate = rotate_generic;
Chris@10 226 return p;
Chris@10 227 }
Chris@10 228
Chris@10 229 void X(triggen_destroy)(triggen *p)
Chris@10 230 {
Chris@10 231 X(ifree0)(p->W0);
Chris@10 232 X(ifree0)(p->W1);
Chris@10 233 X(ifree)(p);
Chris@10 234 }