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