Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/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
1.20k
#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
4.63M
#define TW_5     (8 + 2)
37
#else
38
#define TW_5     0
39
#endif
40
126k
#define TW_SHORT (16 + 4 + 1)
41
#define TW_LONG  (128 + 32 + 8 + 2)
42
43
#ifndef FAAD_DISABLE_DAB
44
4.63M
#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
2
{
53
2
#ifndef FAAD_DISABLE_DAB
54
2
    static const unsigned char logms[3] = { FFT_LOGM_5, FFT_LOGM_SHORT, FFT_LOGM_LONG };
55
2
    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
2
    int t;
61
62
8
    for (t = 0; t < num_logms; t++)
63
6
    {
64
6
        int logm = logms[t];
65
6
        int size = 1 << logm;
66
6
        fftfloat *tw = twiddles + TW_OFFSET(logm);
67
6
        int n2, i;
68
69
24
        for (n2 = size >> 2; n2 >= 1; n2 >>= 2)
70
18
        {
71
            /* stage twiddle W_N^(j*size/(4*n2)) = exp(-2*pi*i*j/(4*n2)) */
72
420
            for (i = 0; i < n2; i++)
73
402
            {
74
402
                int m;
75
1.60k
                for (m = 1; m <= 3; m++)
76
1.20k
                {
77
1.20k
                    double theta = 2.0 * M_PI_DOUBLE * (double)(m * i) / (double)(4 * n2);
78
1.20k
                    *tw++ = (fftfloat)cos(theta);
79
1.20k
                    *tw++ = (fftfloat)-sin(theta);
80
1.20k
                }
81
402
            }
82
18
        }
83
6
    }
84
2
}
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
281M
    float t1 = r1 + r3, t2 = i1 + i3; \
96
281M
    float t3 = r2 + r4, t4 = i2 + i4; \
97
281M
    float t5 = r1 - r3, t6 = i1 - i3; \
98
281M
    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
4.63M
{
112
4.63M
    int j;
113
114
93.0M
    for (j = 0; j < n4; j++)
115
88.4M
    {
116
88.4M
        const float c1 = tw[6 * j],     s1 = tw[6 * j + 1];
117
88.4M
        const float c2 = tw[6 * j + 2], s2 = tw[6 * j + 3];
118
88.4M
        const float c3 = tw[6 * j + 4], s3 = tw[6 * j + 5];
119
88.4M
        float * restrict o = dr + 4 * j;
120
88.4M
        float * restrict p = di + 4 * j;
121
122
88.4M
        float r1 = sr[j],          i1 = si[j];
123
88.4M
        float r2 = sr[j + n4],     i2 = si[j + n4];
124
88.4M
        float r3 = sr[j + 2 * n4], i3 = si[j + 2 * n4];
125
88.4M
        float r4 = sr[j + 3 * n4], i4 = si[j + 3 * n4];
126
88.4M
        BUTTERFLY_SUMS;
127
128
88.4M
        o[0] = t1 + t3;
129
88.4M
        p[0] = t2 + t4;
130
131
88.4M
        r1 = t1 - t3; i1 = t2 - t4;
132
88.4M
        r2 = t5 + t8; i2 = t6 - t7;
133
88.4M
        r3 = t5 - t8; i3 = t6 + t7;
134
135
88.4M
        o[1] = r2 * c1 - i2 * s1;
136
88.4M
        p[1] = r2 * s1 + i2 * c1;
137
88.4M
        o[2] = r1 * c2 - i1 * s2;
138
88.4M
        p[2] = r1 * s2 + i1 * c2;
139
88.4M
        o[3] = r3 * c3 - i3 * s3;
140
88.4M
        p[3] = r3 * s3 + i3 * c3;
141
88.4M
    }
142
4.63M
}
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
27.8M
{
156
27.8M
    const float c1 = tw[0], s1 = tw[1];
157
27.8M
    const float c2 = tw[2], s2 = tw[3];
158
27.8M
    const float c3 = tw[4], s3 = tw[5];
159
27.8M
    int c;
160
161
220M
    for (c = 0; c < B; c++)
162
193M
    {
163
193M
        float r1 = sr[c],          i1 = si[c];
164
193M
        float r2 = sr[c + n4],     i2 = si[c + n4];
165
193M
        float r3 = sr[c + 2 * n4], i3 = si[c + 2 * n4];
166
193M
        float r4 = sr[c + 3 * n4], i4 = si[c + 3 * n4];
167
193M
        BUTTERFLY_SUMS;
168
169
193M
        o1r[c] = t1 + t3;
170
193M
        o1i[c] = t2 + t4;
171
172
193M
        r1 = t1 - t3; i1 = t2 - t4;
173
193M
        r2 = t5 + t8; i2 = t6 - t7;
174
193M
        r3 = t5 - t8; i3 = t6 + t7;
175
176
193M
        o2r[c] = r2 * c1 - i2 * s1;
177
193M
        o2i[c] = r2 * s1 + i2 * c1;
178
193M
        o3r[c] = r1 * c2 - i1 * s2;
179
193M
        o3i[c] = r1 * s2 + i1 * c2;
180
193M
        o4r[c] = r3 * c3 - i3 * s3;
181
193M
        o4i[c] = r3 * s3 + i3 * c3;
182
193M
    }
183
27.8M
}
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
9.40M
{
197
9.40M
    const int B = 4 * B4;
198
9.40M
    int n2 = n4 / B;
199
9.40M
    int j;
200
201
37.2M
    for (j = 0; j < n2; j++, tw += 6, sr += B, si += B, dr += 4 * B, di += 4 * B)
202
27.8M
        radix4_butterflies(sr, si, n4, dr, di, dr + B, di + B,
203
27.8M
                           dr + 2 * B, di + 2 * B, dr + 3 * B, di + 3 * B,
204
27.8M
                           B, tw);
205
9.40M
}
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
126k
{
215
126k
    int c;
216
217
32.6M
    for (c = 0; c < n2; c++)
218
32.4M
    {
219
32.4M
        float r1 = r1p[c], i1 = i1p[c];
220
32.4M
        float r2 = r2p[c], i2 = i2p[c];
221
32.4M
        o1r[c] = r1 + r2; o1i[c] = i1 + i2;
222
32.4M
        o2r[c] = r1 - r2; o2i[c] = i1 - i2;
223
32.4M
    }
224
126k
}
225
226
void fft(float * restrict x, float * restrict y, int logm)
227
4.63M
{
228
4.63M
    const fftfloat *tw = twiddles + TW_OFFSET(logm);
229
4.63M
    int n = 1 << logm;
230
4.63M
    int n4 = n >> 2;
231
4.63M
    float *src = y, *dst = x;
232
4.63M
    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
4.63M
    radix4_first_stage(x, x + n, y, y + n, n4, tw);
237
4.63M
    tw += 6 * n4;
238
14.0M
    for (B4 = 1; 4 * B4 <= n4; B4 *= 4)
239
9.40M
    {
240
9.40M
        float *t;
241
9.40M
        radix4_stage(src, src + n, dst, dst + n, n4, B4, tw);
242
9.40M
        tw += 6 * (n4 / (4 * B4));
243
9.40M
        t = src; src = dst; dst = t;
244
9.40M
    }
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
4.63M
    if (4 * B4 < n)
248
126k
        radix2_stage(x, x + n, x + n / 2, x + n + n / 2,
249
126k
                     y, y + n, y + n / 2, y + n + n / 2, 4 * B4);
250
4.63M
}
251
252
static inline void dft3(float r0, float i0, float r1, float i1, float r2, float i2,
253
                        float *or, float *oi)
254
0
{
255
0
    float a_r = r1 + r2, a_i = i1 + i2;
256
0
    float b_r = (i1 - i2) * 0.8660254037844386f, b_i = (r2 - r1) * 0.8660254037844386f;
257
0
    or[0] = r0 + a_r;
258
0
    oi[0] = i0 + a_i;
259
0
    float m_r = r0 - 0.5f * a_r, m_i = i0 - 0.5f * a_i;
260
0
    or[1] = m_r + b_r;
261
0
    oi[1] = m_i + b_i;
262
0
    or[2] = m_r - b_r;
263
0
    oi[2] = m_i - b_i;
264
0
}
265
266
static inline void dft5(const float *ir, const float *ii, float *or, float *oi)
267
0
{
268
0
    static const float c1 = 0.3090169943749474f,  c2 = -0.8090169943749475f;
269
0
    static const float s1 = 0.9510565162951535f,  s2 =  0.5877852522924731f;
270
271
0
    float a1_r = ir[1] + ir[4], a1_i = ii[1] + ii[4];
272
0
    float a2_r = ir[2] + ir[3], a2_i = ii[2] + ii[3];
273
0
    float b1_r = ii[4] - ii[1], b1_i = ir[1] - ir[4];
274
0
    float b2_r = ii[3] - ii[2], b2_i = ir[2] - ir[3];
275
276
0
    float r0 = ir[0], i0 = ii[0];
277
0
    or[0] = r0 + a1_r + a2_r;
278
0
    oi[0] = i0 + a1_i + a2_i;
279
280
0
    or[1] = r0 + c1 * a1_r + c2 * a2_r - s1 * b1_r - s2 * b2_r;
281
0
    oi[1] = i0 + c1 * a1_i + c2 * a2_i - s1 * b1_i - s2 * b2_i;
282
283
0
    or[2] = r0 + c2 * a1_r + c1 * a2_r - s2 * b1_r + s1 * b2_r;
284
0
    oi[2] = i0 + c2 * a1_i + c1 * a2_i - s2 * b1_i + s1 * b2_i;
285
286
0
    or[3] = r0 + c2 * a1_r + c1 * a2_r + s2 * b1_r - s1 * b2_r;
287
0
    oi[3] = i0 + c2 * a1_i + c1 * a2_i + s2 * b1_i - s1 * b2_i;
288
289
0
    or[4] = r0 + c1 * a1_r + c2 * a2_r + s1 * b1_r + s2 * b2_r;
290
0
    oi[4] = i0 + c1 * a1_i + c2 * a2_i + s1 * b1_i + s2 * b2_i;
291
0
}
292
293
static inline void dft15(const float *ir, const float *ii, float *or, float *oi)
294
0
{
295
0
    float t1_r[3][5], t1_i[3][5];
296
0
    static const unsigned char pfa15_in[3][5] = {
297
0
        { 0,  3,  6,  9, 12},
298
0
        { 5,  8, 11, 14,  2},
299
0
        {10, 13,  1,  4,  7}
300
0
    };
301
0
    for (int j = 0; j < 5; j++) {
302
0
        int idx0 = pfa15_in[0][j], idx1 = pfa15_in[1][j], idx2 = pfa15_in[2][j];
303
0
        float col_r[3], col_i[3];
304
0
        dft3(ir[idx0], ii[idx0], ir[idx1], ii[idx1], ir[idx2], ii[idx2], col_r, col_i);
305
0
        t1_r[0][j] = col_r[0]; t1_i[0][j] = col_i[0];
306
0
        t1_r[1][j] = col_r[1]; t1_i[1][j] = col_i[1];
307
0
        t1_r[2][j] = col_r[2]; t1_i[2][j] = col_i[2];
308
0
    }
309
0
    float t2_r[3][5], t2_i[3][5];
310
0
    for (int i = 0; i < 3; i++) {
311
0
        dft5(t1_r[i], t1_i[i], t2_r[i], t2_i[i]);
312
0
    }
313
0
    static const unsigned char pfa15_out[3][5] = {
314
0
        { 0,  6, 12,  3,  9},
315
0
        {10,  1,  7, 13,  4},
316
0
        { 5, 11,  2,  8, 14}
317
0
    };
318
0
    for (int ka = 0; ka < 3; ka++) {
319
0
        for (int kb = 0; kb < 5; kb++) {
320
0
            int out_idx = pfa15_out[ka][kb];
321
0
            or[out_idx] = t2_r[ka][kb];
322
0
            oi[out_idx] = t2_i[ka][kb];
323
0
        }
324
0
    }
325
0
}
326
327
static inline void dft4(const float *ir, const float *ii, float *or, float *oi)
328
0
{
329
0
    float r0 = ir[0], i0 = ii[0];
330
0
    float r1 = ir[1], i1 = ii[1];
331
0
    float r2 = ir[2], i2 = ii[2];
332
0
    float r3 = ir[3], i3 = ii[3];
333
334
0
    float a_r = r0 + r2, a_i = i0 + i2;
335
0
    float b_r = r1 + r3, b_i = i1 + i3;
336
0
    float c_r = r0 - r2, c_i = i0 - i2;
337
0
    float d_r = i1 - i3, d_i = r3 - r1;
338
339
0
    or[0] = a_r + b_r; oi[0] = a_i + b_i;
340
0
    or[2] = a_r - b_r; oi[2] = a_i - b_i;
341
0
    or[1] = c_r + d_r; oi[1] = c_i + d_i;
342
0
    or[3] = c_r - d_r; oi[3] = c_i - d_i;
343
0
}
344
345
#ifndef FAAD_DISABLE_DAB
346
void fft60(float * restrict x, float * restrict y)
347
0
{
348
0
    const float *xr = x, *xi = x + 60;
349
0
    float t1_r[15][4], t1_i[15][4];
350
351
0
    for (int n1 = 0; n1 < 15; n1++) {
352
0
        float in_r[4], in_i[4], out_r[4], out_i[4];
353
0
        for (int n2 = 0; n2 < 4; n2++) {
354
0
            int idx = (4 * n1 + 15 * n2) % 60;
355
0
            in_r[n2] = xr[idx];
356
0
            in_i[n2] = xi[idx];
357
0
        }
358
0
        dft4(in_r, in_i, out_r, out_i);
359
0
        for (int n2 = 0; n2 < 4; n2++) {
360
0
            t1_r[n1][n2] = out_r[n2];
361
0
            t1_i[n1][n2] = out_i[n2];
362
0
        }
363
0
    }
364
365
    /* Each column is consumed before its results replace it. */
366
0
    for (int n2 = 0; n2 < 4; n2++) {
367
0
        float in_r[15], in_i[15], out_r[15], out_i[15];
368
0
        for (int n1 = 0; n1 < 15; n1++) {
369
0
            in_r[n1] = t1_r[n1][n2];
370
0
            in_i[n1] = t1_i[n1][n2];
371
0
        }
372
0
        dft15(in_r, in_i, out_r, out_i);
373
0
        for (int n1 = 0; n1 < 15; n1++) {
374
0
            t1_r[n1][n2] = out_r[n1];
375
0
            t1_i[n1][n2] = out_i[n1];
376
0
        }
377
0
    }
378
379
0
    float *yr = y, *yi = y + 60;
380
0
    for (int k1 = 0; k1 < 15; k1++) {
381
0
        for (int k2 = 0; k2 < 4; k2++) {
382
0
            int out_idx = (16 * k1 + 45 * k2) % 60;
383
0
            yr[out_idx] = t1_r[k1][k2];
384
0
            yi[out_idx] = t1_i[k1][k2];
385
0
        }
386
0
    }
387
0
}
388
389
void fft480(float * restrict x, float * restrict y)
390
0
{
391
0
    const float *xr = x, *xi = x + 480;
392
0
    float t1_r[15][32], t1_i[15][32];
393
394
0
    for (int n1 = 0; n1 < 15; n1++) {
395
0
        float col_buf[64], col_work[64];
396
0
        for (int n2 = 0; n2 < 32; n2++) {
397
0
            int idx = (32 * n1 + 15 * n2) % 480;
398
0
            col_buf[n2] = xr[idx];
399
0
            col_buf[32 + n2] = xi[idx];
400
0
        }
401
0
        fft(col_buf, col_work, 5);
402
0
        for (int n2 = 0; n2 < 32; n2++) {
403
0
            t1_r[n1][n2] = col_work[n2];
404
0
            t1_i[n1][n2] = col_work[32 + n2];
405
0
        }
406
0
    }
407
408
    /* Each column is consumed before its results replace it. */
409
0
    for (int n2 = 0; n2 < 32; n2++) {
410
0
        float in_r[15], in_i[15], out_r[15], out_i[15];
411
0
        for (int n1 = 0; n1 < 15; n1++) {
412
0
            in_r[n1] = t1_r[n1][n2];
413
0
            in_i[n1] = t1_i[n1][n2];
414
0
        }
415
0
        dft15(in_r, in_i, out_r, out_i);
416
0
        for (int n1 = 0; n1 < 15; n1++) {
417
0
            t1_r[n1][n2] = out_r[n1];
418
0
            t1_i[n1][n2] = out_i[n1];
419
0
        }
420
0
    }
421
422
0
    float *yr = y, *yi = y + 480;
423
0
    for (int k1 = 0; k1 < 15; k1++) {
424
0
        for (int k2 = 0; k2 < 32; k2++) {
425
0
            int out_idx = (256 * k1 + 225 * k2) % 480;
426
0
            yr[out_idx] = t1_r[k1][k2];
427
0
            yi[out_idx] = t1_i[k1][k2];
428
0
        }
429
0
    }
430
0
}
431
#endif