Coverage Report

Created: 2026-12-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/external/faac/common/fft.c
Line
Count
Source
1
/*
2
 * FAAC - Freeware Advanced Audio Coder
3
 * Copyright (C) 2002 Krzysztof Nikiel
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library 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 GNU
13
 * Lesser General Public License for more details.
14
 */
15
16
#include "common_symbols.h"
17
18
#include <math.h>
19
20
#include "fft.h"
21
22
/* Double-precision pi, for one-time twiddle-table generation where the extra
23
 * precision is free -- this runs once, never in the per-sample hot path. */
24
#ifndef M_PI_DOUBLE
25
2.41k
#define M_PI_DOUBLE 3.14159265358979323846
26
#endif
27
28
/* Radix-4 twiddles laid out in the order the butterflies consume them: stage
29
 * by stage, six floats per butterfly (W^j, W^2j, W^3j as cos, -sin). One
30
 * unit-stride stream instead of three gathers keeps the kernel's inner loop
31
 * inside the register file. Built once per process and read-only afterwards,
32
 * so every handle shares them. */
33
34
/* Butterflies per transform: n/4 + n/16 + ... over the radix-4 stages. */
35
#ifndef FAAD_DISABLE_DAB
36
550k
#define TW_5     (8 + 2)
37
#else
38
#define TW_5     0
39
#endif
40
12.5k
#define TW_SHORT (16 + 4 + 1)
41
#define TW_LONG  (128 + 32 + 8 + 2)
42
43
#ifndef FAAD_DISABLE_DAB
44
763k
#define TW_OFFSET(logm) (6 * ((logm) == FFT_LOGM_5 ? 0 : ((logm) == FFT_LOGM_SHORT ? TW_5 : (TW_5 + TW_SHORT))))
45
#else
46
#define TW_OFFSET(logm) (6 * ((logm) == FFT_LOGM_SHORT ? 0 : TW_SHORT))
47
#endif
48
49
static fftfloat twiddles[6 * (TW_5 + TW_SHORT + TW_LONG)];
50
51
void fft_init(void)
52
4
{
53
4
#ifndef FAAD_DISABLE_DAB
54
4
    static const unsigned char logms[3] = { FFT_LOGM_5, FFT_LOGM_SHORT, FFT_LOGM_LONG };
55
4
    int num_logms = 3;
56
#else
57
    static const unsigned char logms[2] = { FFT_LOGM_SHORT, FFT_LOGM_LONG };
58
    int num_logms = 2;
59
#endif
60
4
    int t;
61
62
16
    for (t = 0; t < num_logms; t++)
63
12
    {
64
12
        int logm = logms[t];
65
12
        int size = 1 << logm;
66
12
        fftfloat *tw = twiddles + TW_OFFSET(logm);
67
12
        int n2, i;
68
69
48
        for (n2 = size >> 2; n2 >= 1; n2 >>= 2)
70
36
        {
71
            /* stage twiddle W_N^(j*size/(4*n2)) = exp(-2*pi*i*j/(4*n2)) */
72
840
            for (i = 0; i < n2; i++)
73
804
            {
74
804
                int m;
75
3.21k
                for (m = 1; m <= 3; m++)
76
2.41k
                {
77
2.41k
                    double theta = 2.0 * M_PI_DOUBLE * (double)(m * i) / (double)(4 * n2);
78
2.41k
                    *tw++ = (fftfloat)cos(theta);
79
2.41k
                    *tw++ = (fftfloat)-sin(theta);
80
2.41k
                }
81
804
            }
82
36
        }
83
12
    }
84
4
}
85
86
87
/* Radix-4 DIF, self-sorting (Stockham). Stage k holds B = 4^k sub-transforms
88
 * interleaved, element j of sub-transform c at j*B + c, so a butterfly reads
89
 * the four quarters of src and writes four interleaved runs of dst. The output
90
 * lands in natural order without a permutation pass. logm=9 (512) isn't a
91
 * power of 4, so it ends with one radix-2 stage.
92
 */
93
94
#define BUTTERFLY_SUMS \
95
35.6M
    float t1 = r1 + r3, t2 = i1 + i3; \
96
35.6M
    float t3 = r2 + r4, t4 = i2 + i4; \
97
35.6M
    float t5 = r1 - r3, t6 = i1 - i3; \
98
35.6M
    float t7 = r2 - r4, t8 = i2 - i4
99
100
/* Stage 0: a single sub-transform, so the twiddle changes every butterfly
101
 * and the four outputs of butterfly j are adjacent at 4j. j=0's identity
102
 * twiddle isn't special-cased: one butterfly in n/4 isn't worth a second
103
 * copy of the loop body. */
104
static void radix4_first_stage(
105
    const float * restrict sr,
106
    const float * restrict si,
107
    float * restrict dr,
108
    float * restrict di,
109
    int n4,
110
    const fftfloat * restrict tw)
111
763k
{
112
763k
    int j;
113
114
12.6M
    for (j = 0; j < n4; j++)
115
11.9M
    {
116
11.9M
        const float c1 = tw[6 * j],     s1 = tw[6 * j + 1];
117
11.9M
        const float c2 = tw[6 * j + 2], s2 = tw[6 * j + 3];
118
11.9M
        const float c3 = tw[6 * j + 4], s3 = tw[6 * j + 5];
119
11.9M
        float * restrict o = dr + 4 * j;
120
11.9M
        float * restrict p = di + 4 * j;
121
122
11.9M
        float r1 = sr[j],          i1 = si[j];
123
11.9M
        float r2 = sr[j + n4],     i2 = si[j + n4];
124
11.9M
        float r3 = sr[j + 2 * n4], i3 = si[j + 2 * n4];
125
11.9M
        float r4 = sr[j + 3 * n4], i4 = si[j + 3 * n4];
126
11.9M
        BUTTERFLY_SUMS;
127
128
11.9M
        o[0] = t1 + t3;
129
11.9M
        p[0] = t2 + t4;
130
131
11.9M
        r1 = t1 - t3; i1 = t2 - t4;
132
11.9M
        r2 = t5 + t8; i2 = t6 - t7;
133
11.9M
        r3 = t5 - t8; i3 = t6 + t7;
134
135
11.9M
        o[1] = r2 * c1 - i2 * s1;
136
11.9M
        p[1] = r2 * s1 + i2 * c1;
137
11.9M
        o[2] = r1 * c2 - i1 * s2;
138
11.9M
        p[2] = r1 * s2 + i1 * c2;
139
11.9M
        o[3] = r3 * c3 - i3 * s3;
140
11.9M
        p[3] = r3 * s3 + i3 * c3;
141
11.9M
    }
142
763k
}
143
144
/* B butterflies sharing one twiddle: element c of each of the four input
145
 * quarters to element c of each of the four output runs. A restrict pointer
146
 * per output run tells the vectorizer the runs can't overlap. */
147
static inline void radix4_butterflies(
148
    const float * restrict sr, const float * restrict si, int n4,
149
    float * restrict o1r, float * restrict o1i,
150
    float * restrict o2r, float * restrict o2i,
151
    float * restrict o3r, float * restrict o3i,
152
    float * restrict o4r, float * restrict o4i,
153
    int B,
154
    const fftfloat * restrict tw)
155
3.64M
{
156
3.64M
    const float c1 = tw[0], s1 = tw[1];
157
3.64M
    const float c2 = tw[2], s2 = tw[3];
158
3.64M
    const float c3 = tw[4], s3 = tw[5];
159
3.64M
    int c;
160
161
27.3M
    for (c = 0; c < B; c++)
162
23.7M
    {
163
23.7M
        float r1 = sr[c],          i1 = si[c];
164
23.7M
        float r2 = sr[c + n4],     i2 = si[c + n4];
165
23.7M
        float r3 = sr[c + 2 * n4], i3 = si[c + 2 * n4];
166
23.7M
        float r4 = sr[c + 3 * n4], i4 = si[c + 3 * n4];
167
23.7M
        BUTTERFLY_SUMS;
168
169
23.7M
        o1r[c] = t1 + t3;
170
23.7M
        o1i[c] = t2 + t4;
171
172
23.7M
        r1 = t1 - t3; i1 = t2 - t4;
173
23.7M
        r2 = t5 + t8; i2 = t6 - t7;
174
23.7M
        r3 = t5 - t8; i3 = t6 + t7;
175
176
23.7M
        o2r[c] = r2 * c1 - i2 * s1;
177
23.7M
        o2i[c] = r2 * s1 + i2 * c1;
178
23.7M
        o3r[c] = r1 * c2 - i1 * s2;
179
23.7M
        o3i[c] = r1 * s2 + i1 * c2;
180
23.7M
        o4r[c] = r3 * c3 - i3 * s3;
181
23.7M
        o4i[c] = r3 * s3 + i3 * c3;
182
23.7M
    }
183
3.64M
}
184
185
/* Later stages: B = 4*B4 sub-transforms share each twiddle, so the inner
186
 * loop is unit-stride over c. Passing B4 rather than B lets the vectorizer
187
 * see the trip count is a multiple of four and skip the epilogue. */
188
static void radix4_stage(
189
    const float * restrict sr,
190
    const float * restrict si,
191
    float * restrict dr,
192
    float * restrict di,
193
    int n4,
194
    int B4,
195
    const fftfloat * restrict tw)
196
1.32M
{
197
1.32M
    const int B = 4 * B4;
198
1.32M
    int n2 = n4 / B;
199
1.32M
    int j;
200
201
4.97M
    for (j = 0; j < n2; j++, tw += 6, sr += B, si += B, dr += 4 * B, di += 4 * B)
202
3.64M
        radix4_butterflies(sr, si, n4, dr, di, dr + B, di + B,
203
3.64M
                           dr + 2 * B, di + 2 * B, dr + 3 * B, di + 3 * B,
204
3.64M
                           B, tw);
205
1.32M
}
206
207
/* odd logm: 4^k can't fill it, one radix-2 stage mops up the remainder */
208
static void radix2_stage(
209
    const float * restrict r1p, const float * restrict i1p,
210
    const float * restrict r2p, const float * restrict i2p,
211
    float * restrict o1r, float * restrict o1i,
212
    float * restrict o2r, float * restrict o2i,
213
    int n2)
214
226k
{
215
226k
    int c;
216
217
6.86M
    for (c = 0; c < n2; c++)
218
6.63M
    {
219
6.63M
        float r1 = r1p[c], i1 = i1p[c];
220
6.63M
        float r2 = r2p[c], i2 = i2p[c];
221
6.63M
        o1r[c] = r1 + r2; o1i[c] = i1 + i2;
222
6.63M
        o2r[c] = r1 - r2; o2i[c] = i1 - i2;
223
6.63M
    }
224
226k
}
225
226
void fft(float * restrict x, float * restrict y, int logm)
227
763k
{
228
763k
    const fftfloat *tw = twiddles + TW_OFFSET(logm);
229
763k
    int n = 1 << logm;
230
763k
    int n4 = n >> 2;
231
763k
    float *src = y, *dst = x;
232
763k
    int B4;
233
234
    /* Stages ping-pong x -> y -> x ...; both sizes take an odd number of
235
     * them, so the result lands in y. */
236
763k
    radix4_first_stage(x, x + n, y, y + n, n4, tw);
237
763k
    tw += 6 * n4;
238
2.09M
    for (B4 = 1; 4 * B4 <= n4; B4 *= 4)
239
1.32M
    {
240
1.32M
        float *t;
241
1.32M
        radix4_stage(src, src + n, dst, dst + n, n4, B4, tw);
242
1.32M
        tw += 6 * (n4 / (4 * B4));
243
1.32M
        t = src; src = dst; dst = t;
244
1.32M
    }
245
    /* Odd logm stops at 4*B4 = n/2 sub-transforms of length 2, whose two
246
     * elements sit n/2 apart: one radix-2 stage pairs them. */
247
763k
    if (4 * B4 < n)
248
226k
        radix2_stage(x, x + n, x + n / 2, x + n + n / 2,
249
226k
                     y, y + n, y + n / 2, y + n + n / 2, 4 * B4);
250
763k
}
251
252
static inline void dft3(float r0, float i0, float r1, float i1, float r2, float i2,
253
                        float *or, float *oi)
254
2.44M
{
255
2.44M
    float a_r = r1 + r2, a_i = i1 + i2;
256
2.44M
    float b_r = (i1 - i2) * 0.8660254037844386f, b_i = (r2 - r1) * 0.8660254037844386f;
257
2.44M
    or[0] = r0 + a_r;
258
2.44M
    oi[0] = i0 + a_i;
259
2.44M
    float m_r = r0 - 0.5f * a_r, m_i = i0 - 0.5f * a_i;
260
2.44M
    or[1] = m_r + b_r;
261
2.44M
    oi[1] = m_i + b_i;
262
2.44M
    or[2] = m_r - b_r;
263
2.44M
    oi[2] = m_i - b_i;
264
2.44M
}
265
266
static inline void dft5(const float *ir, const float *ii, float *or, float *oi)
267
1.46M
{
268
1.46M
    static const float c1 = 0.3090169943749474f,  c2 = -0.8090169943749475f;
269
1.46M
    static const float s1 = 0.9510565162951535f,  s2 =  0.5877852522924731f;
270
271
1.46M
    float a1_r = ir[1] + ir[4], a1_i = ii[1] + ii[4];
272
1.46M
    float a2_r = ir[2] + ir[3], a2_i = ii[2] + ii[3];
273
1.46M
    float b1_r = ii[4] - ii[1], b1_i = ir[1] - ir[4];
274
1.46M
    float b2_r = ii[3] - ii[2], b2_i = ir[2] - ir[3];
275
276
1.46M
    float r0 = ir[0], i0 = ii[0];
277
1.46M
    or[0] = r0 + a1_r + a2_r;
278
1.46M
    oi[0] = i0 + a1_i + a2_i;
279
280
1.46M
    or[1] = r0 + c1 * a1_r + c2 * a2_r - s1 * b1_r - s2 * b2_r;
281
1.46M
    oi[1] = i0 + c1 * a1_i + c2 * a2_i - s1 * b1_i - s2 * b2_i;
282
283
1.46M
    or[2] = r0 + c2 * a1_r + c1 * a2_r - s2 * b1_r + s1 * b2_r;
284
1.46M
    oi[2] = i0 + c2 * a1_i + c1 * a2_i - s2 * b1_i + s1 * b2_i;
285
286
1.46M
    or[3] = r0 + c2 * a1_r + c1 * a2_r + s2 * b1_r - s1 * b2_r;
287
1.46M
    oi[3] = i0 + c2 * a1_i + c1 * a2_i + s2 * b1_i - s1 * b2_i;
288
289
1.46M
    or[4] = r0 + c1 * a1_r + c2 * a2_r + s1 * b1_r + s2 * b2_r;
290
1.46M
    oi[4] = i0 + c1 * a1_i + c2 * a2_i + s1 * b1_i + s2 * b2_i;
291
1.46M
}
292
293
static inline void dft15(const float *ir, const float *ii, float *or, float *oi)
294
489k
{
295
489k
    float t1_r[3][5], t1_i[3][5];
296
489k
    static const unsigned char pfa15_in[3][5] = {
297
489k
        { 0,  3,  6,  9, 12},
298
489k
        { 5,  8, 11, 14,  2},
299
489k
        {10, 13,  1,  4,  7}
300
489k
    };
301
2.93M
    for (int j = 0; j < 5; j++) {
302
2.44M
        int idx0 = pfa15_in[0][j], idx1 = pfa15_in[1][j], idx2 = pfa15_in[2][j];
303
2.44M
        float col_r[3], col_i[3];
304
2.44M
        dft3(ir[idx0], ii[idx0], ir[idx1], ii[idx1], ir[idx2], ii[idx2], col_r, col_i);
305
2.44M
        t1_r[0][j] = col_r[0]; t1_i[0][j] = col_i[0];
306
2.44M
        t1_r[1][j] = col_r[1]; t1_i[1][j] = col_i[1];
307
2.44M
        t1_r[2][j] = col_r[2]; t1_i[2][j] = col_i[2];
308
2.44M
    }
309
489k
    float t2_r[3][5], t2_i[3][5];
310
1.95M
    for (int i = 0; i < 3; i++) {
311
1.46M
        dft5(t1_r[i], t1_i[i], t2_r[i], t2_i[i]);
312
1.46M
    }
313
489k
    static const unsigned char pfa15_out[3][5] = {
314
489k
        { 0,  6, 12,  3,  9},
315
489k
        {10,  1,  7, 13,  4},
316
489k
        { 5, 11,  2,  8, 14}
317
489k
    };
318
1.95M
    for (int ka = 0; ka < 3; ka++) {
319
8.81M
        for (int kb = 0; kb < 5; kb++) {
320
7.34M
            int out_idx = pfa15_out[ka][kb];
321
7.34M
            or[out_idx] = t2_r[ka][kb];
322
7.34M
            oi[out_idx] = t2_i[ka][kb];
323
7.34M
        }
324
1.46M
    }
325
489k
}
326
327
static inline void dft4(const float *ir, const float *ii, float *or, float *oi)
328
128k
{
329
128k
    float r0 = ir[0], i0 = ii[0];
330
128k
    float r1 = ir[1], i1 = ii[1];
331
128k
    float r2 = ir[2], i2 = ii[2];
332
128k
    float r3 = ir[3], i3 = ii[3];
333
334
128k
    float a_r = r0 + r2, a_i = i0 + i2;
335
128k
    float b_r = r1 + r3, b_i = i1 + i3;
336
128k
    float c_r = r0 - r2, c_i = i0 - i2;
337
128k
    float d_r = i1 - i3, d_i = r3 - r1;
338
339
128k
    or[0] = a_r + b_r; oi[0] = a_i + b_i;
340
128k
    or[2] = a_r - b_r; oi[2] = a_i - b_i;
341
128k
    or[1] = c_r + d_r; oi[1] = c_i + d_i;
342
128k
    or[3] = c_r - d_r; oi[3] = c_i - d_i;
343
128k
}
344
345
#ifndef FAAD_DISABLE_DAB
346
void fft60(float * restrict x, float * restrict y)
347
8.54k
{
348
8.54k
    const float *xr = x, *xi = x + 60;
349
8.54k
    float t1_r[15][4], t1_i[15][4];
350
351
136k
    for (int n1 = 0; n1 < 15; n1++) {
352
128k
        float in_r[4], in_i[4], out_r[4], out_i[4];
353
640k
        for (int n2 = 0; n2 < 4; n2++) {
354
512k
            int idx = (4 * n1 + 15 * n2) % 60;
355
512k
            in_r[n2] = xr[idx];
356
512k
            in_i[n2] = xi[idx];
357
512k
        }
358
128k
        dft4(in_r, in_i, out_r, out_i);
359
640k
        for (int n2 = 0; n2 < 4; n2++) {
360
512k
            t1_r[n1][n2] = out_r[n2];
361
512k
            t1_i[n1][n2] = out_i[n2];
362
512k
        }
363
128k
    }
364
365
    /* Each column is consumed before its results replace it. */
366
42.7k
    for (int n2 = 0; n2 < 4; n2++) {
367
34.1k
        float in_r[15], in_i[15], out_r[15], out_i[15];
368
546k
        for (int n1 = 0; n1 < 15; n1++) {
369
512k
            in_r[n1] = t1_r[n1][n2];
370
512k
            in_i[n1] = t1_i[n1][n2];
371
512k
        }
372
34.1k
        dft15(in_r, in_i, out_r, out_i);
373
546k
        for (int n1 = 0; n1 < 15; n1++) {
374
512k
            t1_r[n1][n2] = out_r[n1];
375
512k
            t1_i[n1][n2] = out_i[n1];
376
512k
        }
377
34.1k
    }
378
379
8.54k
    float *yr = y, *yi = y + 60;
380
136k
    for (int k1 = 0; k1 < 15; k1++) {
381
640k
        for (int k2 = 0; k2 < 4; k2++) {
382
512k
            int out_idx = (16 * k1 + 45 * k2) % 60;
383
512k
            yr[out_idx] = t1_r[k1][k2];
384
512k
            yi[out_idx] = t1_i[k1][k2];
385
512k
        }
386
128k
    }
387
8.54k
}
388
389
void fft480(float * restrict x, float * restrict y)
390
14.2k
{
391
14.2k
    const float *xr = x, *xi = x + 480;
392
14.2k
    float t1_r[15][32], t1_i[15][32];
393
394
227k
    for (int n1 = 0; n1 < 15; n1++) {
395
213k
        float col_buf[64], col_work[64];
396
7.04M
        for (int n2 = 0; n2 < 32; n2++) {
397
6.83M
            int idx = (32 * n1 + 15 * n2) % 480;
398
6.83M
            col_buf[n2] = xr[idx];
399
6.83M
            col_buf[32 + n2] = xi[idx];
400
6.83M
        }
401
213k
        fft(col_buf, col_work, 5);
402
7.04M
        for (int n2 = 0; n2 < 32; n2++) {
403
6.83M
            t1_r[n1][n2] = col_work[n2];
404
6.83M
            t1_i[n1][n2] = col_work[32 + n2];
405
6.83M
        }
406
213k
    }
407
408
    /* Each column is consumed before its results replace it. */
409
469k
    for (int n2 = 0; n2 < 32; n2++) {
410
455k
        float in_r[15], in_i[15], out_r[15], out_i[15];
411
7.29M
        for (int n1 = 0; n1 < 15; n1++) {
412
6.83M
            in_r[n1] = t1_r[n1][n2];
413
6.83M
            in_i[n1] = t1_i[n1][n2];
414
6.83M
        }
415
455k
        dft15(in_r, in_i, out_r, out_i);
416
7.29M
        for (int n1 = 0; n1 < 15; n1++) {
417
6.83M
            t1_r[n1][n2] = out_r[n1];
418
6.83M
            t1_i[n1][n2] = out_i[n1];
419
6.83M
        }
420
455k
    }
421
422
14.2k
    float *yr = y, *yi = y + 480;
423
227k
    for (int k1 = 0; k1 < 15; k1++) {
424
7.04M
        for (int k2 = 0; k2 < 32; k2++) {
425
6.83M
            int out_idx = (256 * k1 + 225 * k2) % 480;
426
6.83M
            yr[out_idx] = t1_r[k1][k2];
427
6.83M
            yi[out_idx] = t1_i[k1][k2];
428
6.83M
        }
429
213k
    }
430
14.2k
}
431
#endif