File: NE10_fft_generic_int32.cpp

package info (click to toggle)
ne10 1.2.1-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,816 kB
  • sloc: ansic: 26,841; asm: 8,546; cpp: 2,428; python: 342; javascript: 136; sh: 135; objc: 105; xml: 63; java: 35; makefile: 12
file content (395 lines) | stat: -rw-r--r-- 14,827 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 *  Copyright 2015 ARM Limited and Contributors.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    * Neither the name of ARM Limited nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY ARM LIMITED AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL ARM LIMITED BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* license of Kiss FFT */
/*
Copyright (c) 2003-2010, Mark Borgerding

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
 * NE10 Library : dsp/NE10_fft_generic_int32.cpp
 */

#include "NE10_types.h"
#include "NE10_macros.h"
#include "NE10_fft.h"
#include "NE10_fft_generic_int32.h"

/**
 * @brief Generic butterfly function for 32-bit fixed point.
 * @tparam RADIX            Radix of this stage. One among {2, 3, 4, 5 }
 * @tparam is_first_stage   Whether this stags is the first. If it is, multiplication
 * with twiddles is optimizied because all twiddles are \f$ 1 \f$.
 * @tparam is_inverse       Whether it is the inverse (backward) FFT.
 * @tparam is_scaled        Whether output of this stage is scaled. For fixed-point,
 * output is scaled by RADIX during each stage.
 * @param[out] Fout         Pointing to output buffer.
 * @param[in]  Fin          Pointing to input buffer.
 * @param[in]  twiddles     Pointint to twiddles buffer. If is_first_stage is set true,
 * twiddles will be ignored.
 * @param[in]  fstride      Number of butterfly groups.
 * @param[in]  out_step     Step between output data.
 * @param[in]  nfft         Size of the input array.
 */
template<int RADIX, bool is_first_stage, bool is_inverse, bool is_scaled>
inline void ne10_radix_butterfly_int32_c (ne10_fft_cpx_int32_t *Fout,
        const ne10_fft_cpx_int32_t *Fin,
        const ne10_fft_cpx_int32_t *twiddles,
        const ne10_int32_t fstride,
        const ne10_int32_t out_step,
        const ne10_int32_t nfft)
{
    const ne10_int32_t in_step = nfft / RADIX;
    ne10_int32_t f_count;
    ne10_int32_t m_count;

    for (f_count = fstride; f_count > 0; f_count--)
    {
        for (m_count = out_step; m_count > 0; m_count--)
        {
            ne10_fft_cpx_int32_t scratch_in[RADIX];
            ne10_fft_cpx_int32_t scratch_out[RADIX];

            // Load from input buffer.
            NE10_LOAD_BY_STEP <RADIX> (scratch_in, Fin, in_step);

            if (is_inverse)
            {
                // Conjugate all elements in scratch_in.
                NE10_CONJ<RADIX> (scratch_in);
            }

            if (is_scaled)
            {
                // All elements in scratch_in are divided by radix of this stage.
                NE10_SCALED<RADIX> (scratch_in, RADIX);
            }

            if (!is_first_stage)
            {
                // Multiply twiddles for all stages but the first one.
                ne10_fft_cpx_int32_t scratch_tw[RADIX - 1];
                ne10_fft_cpx_int32_t scratch[RADIX];

                // Load twiddles from twiddles buffer.
                NE10_LOAD_BY_STEP<RADIX - 1> (scratch_tw, twiddles, out_step);

                FFT_MUL_TW<RADIX> (scratch, scratch_in, scratch_tw);

                // Copy from temp buff scratch to scratch_in.
                NE10_LOAD_BY_STEP<RADIX> (scratch_in, scratch, 1);
            }

            // Radix -2, -3, -4 or -5 butterfly
            // From scratch_in to scratch_out.
            FFT_FCU<RADIX> (scratch_out, scratch_in);

            if (is_inverse)
            {
                // Conjugate all elements in scratch_out.
                NE10_CONJ<RADIX> (scratch_out);
            }

            // Store to output buffer.
            NE10_STORE_BY_STEP<RADIX> (Fout, scratch_out, out_step);

            // Update input, output and twiddles pointers.
            Fin++;
            if (!is_first_stage)
            {
                Fout++;
                twiddles++;
            }
            else
            {
                Fout += RADIX;
            }
        }
        if (!is_first_stage)
        {
            // Roll back twiddles.
            twiddles -= out_step;
            // Next output groups.
            Fout += (RADIX - 1) * out_step;
        }
    }
}

/**
 * @brief Generic butterfly function for 32-bit fixed point.
 * @tparam is_inverse       Whether it is the inverse (backward) FFT.
 * @tparam is_scaled        Whether output of this stage is scaled. For fixed-point,
 * output is scaled by RADIX during each stage.
 * @param[out] Fout         Pointing to output buffer.
 * @param[in]  Fin          Pointing to input buffer.
 * @param[in]  twiddles     Pointint to twiddles buffer.
 * @param[in]  radix        Radix of this stage.
 * @param[in]  in_step      Step between input data.
 * @param[in]  out_step     Step between output data.
 */
template<bool is_inverse, bool is_scaled>
static inline void ne10_radix_generic_butterfly_int32_c (ne10_fft_cpx_int32_t *Fout,
        const ne10_fft_cpx_int32_t *Fin,
        const ne10_fft_cpx_int32_t *twiddles,
        const ne10_int32_t radix,
        const ne10_int32_t in_step,
        const ne10_int32_t out_step)
{
    ne10_int32_t q, q1;
    ne10_int32_t f_count = in_step;

    ne10_fft_cpx_int32_t tmp;
    ne10_fft_cpx_int32_t *scratch;
    scratch = (ne10_fft_cpx_int32_t *) NE10_MALLOC (radix *
            sizeof (ne10_fft_cpx_int32_t));

    for (; f_count > 0; f_count--)
    {
        // load
        for (q1 = 0; q1 < radix; q1++)
        {
            scratch[q1] = Fin[in_step * q1];
            if (is_inverse)
            {
                scratch[q1].i = -scratch[q1].i;
            }
            if (is_scaled)
            {
                NE10_F2I32_FIXDIV (scratch[q1], radix);
            }
        } // q1

        // compute Fout[q1 * out_step] from definition
        for (q1 = 0; q1 < radix; q1++)
        {
            ne10_int32_t twidx = 0;
            Fout[q1 * out_step] = scratch[0];
            for (q = 1; q < radix; q++)
            {
                twidx += 1 * q1;
                if (twidx >= radix)
                {
                    twidx -= radix;
                }
                NE10_CPX_MUL_F32 (tmp, scratch[q], twiddles[twidx]);
                NE10_CPX_ADDTO (Fout[q1 * out_step], tmp);
            } // q
            if (is_inverse)
            {
                Fout[q1 * out_step].i = -Fout[q1 * out_step].i;
            }
        } // q1

        Fout += radix;
        Fin++;
    }

    NE10_FREE (scratch);
}

/**
 * @brief Generic FFT function for 32-bit fixed point.
 * @tparam is_inverse       Whether it is the inverse (backward) FFT.
 * @tparam is_scaled        Whether output is scaled by size of FFT.
 * @param[out] Fout         Pointing to output buffer.
 * @param[in]  Fin          Pointing to input buffer.
 * @param[in]  factors      Pointing to factors buffer.
 * @param[in]  twiddles     Pointing to twiddles buffer.
 * @param[in]  buffer       Pointing to temp buffer so that Fin is not polluted.
 */
template<bool is_inverse, bool is_scaled>
inline void ne10_mixed_radix_generic_butterfly_int32_impl_c (ne10_fft_cpx_int32_t *Fout,
        const ne10_fft_cpx_int32_t *Fin,
        const ne10_int32_t *factors,
        const ne10_fft_cpx_int32_t *twiddles,
        ne10_fft_cpx_int32_t *buffer)
{
    ne10_int32_t fstride, mstride, radix;
    ne10_int32_t stage_count;
    ne10_int32_t nfft;

    // init fstride, mstride, radix, nfft
    stage_count = factors[0];
    fstride = factors[1];
    mstride = 1;
    radix = factors[stage_count << 1]; // radix of first stage
    nfft = fstride * radix;

    if (stage_count % 2 == 0)
    {
        ne10_swap_ptr (buffer, Fout);
    }

    // first stage
    switch (radix)
    {
    case 2:
        ne10_radix_butterfly_int32_c<2, true, is_inverse, is_scaled> (Fout, Fin,
                NULL, // Twiddles are not used for first stage.
                fstride, 1, nfft);
        break;
    case 4:
        ne10_radix_butterfly_int32_c<4, true, is_inverse, is_scaled> (Fout, Fin,
                NULL, // Same as above.
                fstride, 1, nfft);
        break;
    case 3:
        ne10_radix_butterfly_int32_c<3, true, is_inverse, is_scaled> (Fout, Fin,
                NULL, // Same as above.
                fstride, 1, nfft);
        break;
    case 5:
        ne10_radix_butterfly_int32_c<5, true, is_inverse, is_scaled> (Fout, Fin,
                NULL, // Same as above.
                fstride, 1, nfft);
        break;
    default:
        ne10_radix_generic_butterfly_int32_c<is_inverse, is_scaled> (Fout, Fin,
                twiddles, // Twiddles for butterfly.
                radix, fstride, 1);
        break;
    }

    stage_count--;
    if (!stage_count) // finish
    {
        return;
    }

    if (radix % 2)
    {
        twiddles += radix;
    }

    // other stages
    while (stage_count > 0)
    {
        ne10_swap_ptr (buffer, Fout);
        mstride *= radix;

        // update radix
        radix = factors[stage_count << 1];
        assert ((radix > 1) && (radix < 6));

        fstride /= radix;
        switch (radix)
        {
        case 2:
            ne10_radix_butterfly_int32_c<2, false, is_inverse, is_scaled> (Fout,
                    buffer, twiddles, fstride, mstride, nfft);
            break;
        case 3:
            ne10_radix_butterfly_int32_c<3, false, is_inverse, is_scaled> (Fout,
                    buffer, twiddles, fstride, mstride, nfft);
            break;
        case 4:
            ne10_radix_butterfly_int32_c<4, false, is_inverse, is_scaled> (Fout,
                    buffer, twiddles, fstride, mstride, nfft);
            break;
        case 5:
            ne10_radix_butterfly_int32_c<5, false, is_inverse, is_scaled> (Fout,
                    buffer, twiddles, fstride, mstride, nfft);
            break;
        } // switch (radix)

        twiddles += mstride * (radix - 1);
        stage_count--;
    } // while (stage_count)
}

/**
 * @brief Generic (forward) FFT function for 32-bit fixed point.
 * @param[out] Fout         Pointing to output buffer.
 * @param[in]  Fin          Pointing to input buffer.
 * @param[in]  factors      Pointing to factors buffer.
 * @param[in]  twiddles     Pointing to twiddles buffer.
 * @param[in]  buffer       Pointing to temp buffer so that Fin is not polluted.
 * @param[in]  is_scaled    Whether output is scaled by size of FFT.
 */
void ne10_mixed_radix_generic_butterfly_int32_c (ne10_fft_cpx_int32_t *Fout,
        const ne10_fft_cpx_int32_t *Fin,
        const ne10_int32_t *factors,
        const ne10_fft_cpx_int32_t *twiddles,
        ne10_fft_cpx_int32_t *buffer,
        const ne10_int32_t is_scaled)
{
    const bool is_inverse = false;
    if (is_scaled)
    {
        const bool is_scaled_flag = true;
        ne10_mixed_radix_generic_butterfly_int32_impl_c<is_inverse,
            is_scaled_flag> (Fout, Fin, factors, twiddles, buffer);
    }
    else
    {
        const bool is_scaled_flag = false;
        ne10_mixed_radix_generic_butterfly_int32_impl_c<is_inverse,
            is_scaled_flag> (Fout, Fin, factors, twiddles, buffer);
    }
}

/**
 * @brief Generic IFFT function for 32-bit fixed point.
 * @param[out] Fout         Pointing to output buffer.
 * @param[in]  Fin          Pointing to input buffer.
 * @param[in]  factors      Pointing to factors buffer.
 * @param[in]  twiddles     Pointing to twiddles buffer.
 * @param[in]  buffer       Pointing to temp buffer so that Fin is not polluted.
 * @param[in]  is_scaled    Whether output is scaled by size of FFT.
 */
void ne10_mixed_radix_generic_butterfly_inverse_int32_c (
        ne10_fft_cpx_int32_t *Fout,
        const ne10_fft_cpx_int32_t *Fin,
        const ne10_int32_t *factors,
        const ne10_fft_cpx_int32_t *twiddles,
        ne10_fft_cpx_int32_t *buffer,
        const ne10_int32_t is_scaled)
{
    const bool is_inverse = true;
    if (is_scaled)
    {
        const bool is_scaled_flag = true;
        ne10_mixed_radix_generic_butterfly_int32_impl_c<is_inverse,
            is_scaled_flag> (Fout, Fin, factors, twiddles, buffer);
    }
    else
    {
        const bool is_scaled_flag = false;
        ne10_mixed_radix_generic_butterfly_int32_impl_c<is_inverse,
            is_scaled_flag> (Fout, Fin, factors, twiddles, buffer);
    }
}