annotate fft/fftw/fftw-3.3.4/kernel/trig.c @ 40:223f770b5341 kissfft-double tip

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