annotate src/fftw-3.3.8/kernel/trig.c @ 84:08ae793730bd

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