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