Chris@42: /* Chris@42: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@42: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@42: * Chris@42: * This program is free software; you can redistribute it and/or modify Chris@42: * it under the terms of the GNU General Public License as published by Chris@42: * the Free Software Foundation; either version 2 of the License, or Chris@42: * (at your option) any later version. Chris@42: * Chris@42: * This program is distributed in the hope that it will be useful, Chris@42: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@42: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@42: * GNU General Public License for more details. Chris@42: * Chris@42: * You should have received a copy of the GNU General Public License Chris@42: * along with this program; if not, write to the Free Software Chris@42: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@42: * Chris@42: */ Chris@42: Chris@42: Chris@42: /* trigonometric functions */ Chris@42: #include "ifftw.h" Chris@42: #include Chris@42: Chris@42: #if defined(TRIGREAL_IS_LONG_DOUBLE) Chris@42: # define COS cosl Chris@42: # define SIN sinl Chris@42: # define KTRIG(x) (x##L) Chris@42: # if defined(HAVE_DECL_SINL) && !HAVE_DECL_SINL Chris@42: extern long double sinl(long double x); Chris@42: # endif Chris@42: # if defined(HAVE_DECL_COSL) && !HAVE_DECL_COSL Chris@42: extern long double cosl(long double x); Chris@42: # endif Chris@42: #elif defined(TRIGREAL_IS_QUAD) Chris@42: # define COS cosq Chris@42: # define SIN sinq Chris@42: # define KTRIG(x) (x##Q) Chris@42: extern __float128 sinq(__float128 x); Chris@42: extern __float128 cosq(__float128 x); Chris@42: #else Chris@42: # define COS cos Chris@42: # define SIN sin Chris@42: # define KTRIG(x) (x) Chris@42: #endif Chris@42: Chris@42: static const trigreal K2PI = Chris@42: KTRIG(6.2831853071795864769252867665590057683943388); Chris@42: #define by2pi(m, n) ((K2PI * (m)) / (n)) Chris@42: Chris@42: /* Chris@42: * Improve accuracy by reducing x to range [0..1/8] Chris@42: * before multiplication by 2 * PI. Chris@42: */ Chris@42: Chris@42: static void real_cexp(INT m, INT n, trigreal *out) Chris@42: { Chris@42: trigreal theta, c, s, t; Chris@42: unsigned octant = 0; Chris@42: INT quarter_n = n; Chris@42: Chris@42: n += n; n += n; Chris@42: m += m; m += m; Chris@42: Chris@42: if (m < 0) m += n; Chris@42: if (m > n - m) { m = n - m; octant |= 4; } Chris@42: if (m - quarter_n > 0) { m = m - quarter_n; octant |= 2; } Chris@42: if (m > quarter_n - m) { m = quarter_n - m; octant |= 1; } Chris@42: Chris@42: theta = by2pi(m, n); Chris@42: c = COS(theta); s = SIN(theta); Chris@42: Chris@42: if (octant & 1) { t = c; c = s; s = t; } Chris@42: if (octant & 2) { t = c; c = -s; s = t; } Chris@42: if (octant & 4) { s = -s; } Chris@42: Chris@42: out[0] = c; Chris@42: out[1] = s; Chris@42: } Chris@42: Chris@42: static INT choose_twshft(INT n) Chris@42: { Chris@42: INT log2r = 0; Chris@42: while (n > 0) { Chris@42: ++log2r; Chris@42: n /= 4; Chris@42: } Chris@42: return log2r; Chris@42: } Chris@42: Chris@42: static void cexpl_sqrtn_table(triggen *p, INT m, trigreal *res) Chris@42: { Chris@42: m += p->n * (m < 0); Chris@42: Chris@42: { Chris@42: INT m0 = m & p->twmsk; Chris@42: INT m1 = m >> p->twshft; Chris@42: trigreal wr0 = p->W0[2 * m0]; Chris@42: trigreal wi0 = p->W0[2 * m0 + 1]; Chris@42: trigreal wr1 = p->W1[2 * m1]; Chris@42: trigreal wi1 = p->W1[2 * m1 + 1]; Chris@42: Chris@42: res[0] = wr1 * wr0 - wi1 * wi0; Chris@42: res[1] = wi1 * wr0 + wr1 * wi0; Chris@42: } Chris@42: } Chris@42: Chris@42: /* multiply (xr, xi) by exp(FFT_SIGN * 2*pi*i*m/n) */ Chris@42: static void rotate_sqrtn_table(triggen *p, INT m, R xr, R xi, R *res) Chris@42: { Chris@42: m += p->n * (m < 0); Chris@42: Chris@42: { Chris@42: INT m0 = m & p->twmsk; Chris@42: INT m1 = m >> p->twshft; Chris@42: trigreal wr0 = p->W0[2 * m0]; Chris@42: trigreal wi0 = p->W0[2 * m0 + 1]; Chris@42: trigreal wr1 = p->W1[2 * m1]; Chris@42: trigreal wi1 = p->W1[2 * m1 + 1]; Chris@42: trigreal wr = wr1 * wr0 - wi1 * wi0; Chris@42: trigreal wi = wi1 * wr0 + wr1 * wi0; Chris@42: Chris@42: #if FFT_SIGN == -1 Chris@42: res[0] = xr * wr + xi * wi; Chris@42: res[1] = xi * wr - xr * wi; Chris@42: #else Chris@42: res[0] = xr * wr - xi * wi; Chris@42: res[1] = xi * wr + xr * wi; Chris@42: #endif Chris@42: } Chris@42: } Chris@42: Chris@42: static void cexpl_sincos(triggen *p, INT m, trigreal *res) Chris@42: { Chris@42: real_cexp(m, p->n, res); Chris@42: } Chris@42: Chris@42: static void cexp_zero(triggen *p, INT m, R *res) Chris@42: { Chris@42: UNUSED(p); UNUSED(m); Chris@42: res[0] = 0; Chris@42: res[1] = 0; Chris@42: } Chris@42: Chris@42: static void cexpl_zero(triggen *p, INT m, trigreal *res) Chris@42: { Chris@42: UNUSED(p); UNUSED(m); Chris@42: res[0] = 0; Chris@42: res[1] = 0; Chris@42: } Chris@42: Chris@42: static void cexp_generic(triggen *p, INT m, R *res) Chris@42: { Chris@42: trigreal resl[2]; Chris@42: p->cexpl(p, m, resl); Chris@42: res[0] = (R)resl[0]; Chris@42: res[1] = (R)resl[1]; Chris@42: } Chris@42: Chris@42: static void rotate_generic(triggen *p, INT m, R xr, R xi, R *res) Chris@42: { Chris@42: trigreal w[2]; Chris@42: p->cexpl(p, m, w); Chris@42: res[0] = xr * w[0] - xi * (FFT_SIGN * w[1]); Chris@42: res[1] = xi * w[0] + xr * (FFT_SIGN * w[1]); Chris@42: } Chris@42: Chris@42: triggen *X(mktriggen)(enum wakefulness wakefulness, INT n) Chris@42: { Chris@42: INT i, n0, n1; Chris@42: triggen *p = (triggen *)MALLOC(sizeof(*p), TWIDDLES); Chris@42: Chris@42: p->n = n; Chris@42: p->W0 = p->W1 = 0; Chris@42: p->cexp = 0; Chris@42: p->rotate = 0; Chris@42: Chris@42: switch (wakefulness) { Chris@42: case SLEEPY: Chris@42: A(0 /* can't happen */); Chris@42: break; Chris@42: Chris@42: case AWAKE_SQRTN_TABLE: { Chris@42: INT twshft = choose_twshft(n); Chris@42: Chris@42: p->twshft = twshft; Chris@42: p->twradix = ((INT)1) << twshft; Chris@42: p->twmsk = p->twradix - 1; Chris@42: Chris@42: n0 = p->twradix; Chris@42: n1 = (n + n0 - 1) / n0; Chris@42: Chris@42: p->W0 = (trigreal *)MALLOC(n0 * 2 * sizeof(trigreal), TWIDDLES); Chris@42: p->W1 = (trigreal *)MALLOC(n1 * 2 * sizeof(trigreal), TWIDDLES); Chris@42: Chris@42: for (i = 0; i < n0; ++i) Chris@42: real_cexp(i, n, p->W0 + 2 * i); Chris@42: Chris@42: for (i = 0; i < n1; ++i) Chris@42: real_cexp(i * p->twradix, n, p->W1 + 2 * i); Chris@42: Chris@42: p->cexpl = cexpl_sqrtn_table; Chris@42: p->rotate = rotate_sqrtn_table; Chris@42: break; Chris@42: } Chris@42: Chris@42: case AWAKE_SINCOS: Chris@42: p->cexpl = cexpl_sincos; Chris@42: break; Chris@42: Chris@42: case AWAKE_ZERO: Chris@42: p->cexp = cexp_zero; Chris@42: p->cexpl = cexpl_zero; Chris@42: break; Chris@42: } Chris@42: Chris@42: if (!p->cexp) { Chris@42: if (sizeof(trigreal) == sizeof(R)) Chris@42: p->cexp = (void (*)(triggen *, INT, R *))p->cexpl; Chris@42: else Chris@42: p->cexp = cexp_generic; Chris@42: } Chris@42: if (!p->rotate) Chris@42: p->rotate = rotate_generic; Chris@42: return p; Chris@42: } Chris@42: Chris@42: void X(triggen_destroy)(triggen *p) Chris@42: { Chris@42: X(ifree0)(p->W0); Chris@42: X(ifree0)(p->W1); Chris@42: X(ifree)(p); Chris@42: }