Coverage Report

Created: 2025-06-22 06:45

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