| 1 | /* |
| 2 | Copyright (c) 2003-2010, Mark Borgerding |
| 3 | |
| 4 | All rights reserved. |
| 5 | |
| 6 | Redistribution and use in source and binary forms, with or without modification, |
| 7 | are permitted |
| 8 | provided that the following conditions are met: |
| 9 | |
| 10 | * Redistributions of source code must retain the above copyright notice, |
| 11 | this list of conditions |
| 12 | and the following disclaimer. |
| 13 | * Redistributions in binary form must reproduce the above copyright notice, |
| 14 | this list of |
| 15 | conditions and the following disclaimer in the documentation and/or other |
| 16 | materials provided with |
| 17 | the distribution. |
| 18 | * Neither the author nor the names of any contributors may be used to |
| 19 | endorse or promote |
| 20 | products derived from this software without specific prior written permission. |
| 21 | |
| 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 23 | ANY EXPRESS OR |
| 24 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 | MERCHANTABILITY AND |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 27 | OWNER OR |
| 28 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 29 | OR CONSEQUENTIAL |
| 30 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 31 | SERVICES; LOSS OF USE, |
| 32 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 33 | LIABILITY, WHETHER |
| 34 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 35 | ARISING IN ANY WAY OUT OF |
| 36 | THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #include "FreeSurround/_KissFFTGuts.h" |
| 40 | /* The guts header contains all the multiplication and addition macros that are |
| 41 | defined for |
| 42 | fixed or floating point complex numbers. It also delares the kf_ internal |
| 43 | functions. |
| 44 | */ |
| 45 | |
| 46 | static void kf_bfly2(kiss_fft_cpx *Fout, const size_t fstride, |
| 47 | const kiss_fft_cfg st, int m) { |
| 48 | kiss_fft_cpx *Fout2; |
| 49 | kiss_fft_cpx *tw1 = st->twiddles; |
| 50 | kiss_fft_cpx t; |
| 51 | Fout2 = Fout + m; |
| 52 | do { |
| 53 | C_FIXDIV(*Fout, 2); |
| 54 | C_FIXDIV(*Fout2, 2); |
| 55 | |
| 56 | C_MUL(t, *Fout2, *tw1); |
| 57 | tw1 += fstride; |
| 58 | C_SUB(*Fout2, *Fout, t); |
| 59 | C_ADDTO(*Fout, t); |
| 60 | ++Fout2; |
| 61 | ++Fout; |
| 62 | } while (--m); |
| 63 | } |
| 64 | |
| 65 | static void kf_bfly4(kiss_fft_cpx *Fout, const size_t fstride, |
| 66 | const kiss_fft_cfg st, const size_t m) { |
| 67 | kiss_fft_cpx *tw1, *tw2, *tw3; |
| 68 | kiss_fft_cpx scratch[6]; |
| 69 | size_t k = m; |
| 70 | const size_t m2 = 2 * m; |
| 71 | const size_t m3 = 3 * m; |
| 72 | |
| 73 | tw3 = tw2 = tw1 = st->twiddles; |
| 74 | |
| 75 | do { |
| 76 | C_FIXDIV(*Fout, 4); |
| 77 | C_FIXDIV(Fout[m], 4); |
| 78 | C_FIXDIV(Fout[m2], 4); |
| 79 | C_FIXDIV(Fout[m3], 4); |
| 80 | |
| 81 | C_MUL(scratch[0], Fout[m], *tw1); |
| 82 | C_MUL(scratch[1], Fout[m2], *tw2); |
| 83 | C_MUL(scratch[2], Fout[m3], *tw3); |
| 84 | |
| 85 | C_SUB(scratch[5], *Fout, scratch[1]); |
| 86 | C_ADDTO(*Fout, scratch[1]); |
| 87 | C_ADD(scratch[3], scratch[0], scratch[2]); |
| 88 | C_SUB(scratch[4], scratch[0], scratch[2]); |
| 89 | C_SUB(Fout[m2], *Fout, scratch[3]); |
| 90 | tw1 += fstride; |
| 91 | tw2 += fstride * 2; |
| 92 | tw3 += fstride * 3; |
| 93 | C_ADDTO(*Fout, scratch[3]); |
| 94 | |
| 95 | if (st->inverse) { |
| 96 | Fout[m].r = scratch[5].r - scratch[4].i; |
| 97 | Fout[m].i = scratch[5].i + scratch[4].r; |
| 98 | Fout[m3].r = scratch[5].r + scratch[4].i; |
| 99 | Fout[m3].i = scratch[5].i - scratch[4].r; |
| 100 | } else { |
| 101 | Fout[m].r = scratch[5].r + scratch[4].i; |
| 102 | Fout[m].i = scratch[5].i - scratch[4].r; |
| 103 | Fout[m3].r = scratch[5].r - scratch[4].i; |
| 104 | Fout[m3].i = scratch[5].i + scratch[4].r; |
| 105 | } |
| 106 | ++Fout; |
| 107 | } while (--k); |
| 108 | } |
| 109 | |
| 110 | static void kf_bfly3(kiss_fft_cpx *Fout, const size_t fstride, |
| 111 | const kiss_fft_cfg st, size_t m) { |
| 112 | size_t k = m; |
| 113 | const size_t m2 = 2 * m; |
| 114 | kiss_fft_cpx *tw1, *tw2; |
| 115 | kiss_fft_cpx scratch[5]; |
| 116 | kiss_fft_cpx epi3; |
| 117 | epi3 = st->twiddles[fstride * m]; |
| 118 | |
| 119 | tw1 = tw2 = st->twiddles; |
| 120 | |
| 121 | do { |
| 122 | C_FIXDIV(*Fout, 3); |
| 123 | C_FIXDIV(Fout[m], 3); |
| 124 | C_FIXDIV(Fout[m2], 3); |
| 125 | |
| 126 | C_MUL(scratch[1], Fout[m], *tw1); |
| 127 | C_MUL(scratch[2], Fout[m2], *tw2); |
| 128 | |
| 129 | C_ADD(scratch[3], scratch[1], scratch[2]); |
| 130 | C_SUB(scratch[0], scratch[1], scratch[2]); |
| 131 | tw1 += fstride; |
| 132 | tw2 += fstride * 2; |
| 133 | |
| 134 | Fout[m].r = Fout->r - HALF_OF(scratch[3].r); |
| 135 | Fout[m].i = Fout->i - HALF_OF(scratch[3].i); |
| 136 | |
| 137 | C_MULBYSCALAR(scratch[0], epi3.i); |
| 138 | |
| 139 | C_ADDTO(*Fout, scratch[3]); |
| 140 | |
| 141 | Fout[m2].r = Fout[m].r + scratch[0].i; |
| 142 | Fout[m2].i = Fout[m].i - scratch[0].r; |
| 143 | |
| 144 | Fout[m].r -= scratch[0].i; |
| 145 | Fout[m].i += scratch[0].r; |
| 146 | |
| 147 | ++Fout; |
| 148 | } while (--k); |
| 149 | } |
| 150 | |
| 151 | static void kf_bfly5(kiss_fft_cpx *Fout, const size_t fstride, |
| 152 | const kiss_fft_cfg st, int m) { |
| 153 | kiss_fft_cpx *Fout0, *Fout1, *Fout2, *Fout3, *Fout4; |
| 154 | int u; |
| 155 | kiss_fft_cpx scratch[13]; |
| 156 | kiss_fft_cpx *twiddles = st->twiddles; |
| 157 | kiss_fft_cpx *tw; |
| 158 | kiss_fft_cpx ya, yb; |
| 159 | ya = twiddles[fstride * m]; |
| 160 | yb = twiddles[fstride * 2 * m]; |
| 161 | |
| 162 | Fout0 = Fout; |
| 163 | Fout1 = Fout0 + m; |
| 164 | Fout2 = Fout0 + 2 * m; |
| 165 | Fout3 = Fout0 + 3 * m; |
| 166 | Fout4 = Fout0 + 4 * m; |
| 167 | |
| 168 | tw = st->twiddles; |
| 169 | for (u = 0; u < m; ++u) { |
| 170 | C_FIXDIV(*Fout0, 5); |
| 171 | C_FIXDIV(*Fout1, 5); |
| 172 | C_FIXDIV(*Fout2, 5); |
| 173 | C_FIXDIV(*Fout3, 5); |
| 174 | C_FIXDIV(*Fout4, 5); |
| 175 | scratch[0] = *Fout0; |
| 176 | |
| 177 | C_MUL(scratch[1], *Fout1, tw[u * fstride]); |
| 178 | C_MUL(scratch[2], *Fout2, tw[2 * u * fstride]); |
| 179 | C_MUL(scratch[3], *Fout3, tw[3 * u * fstride]); |
| 180 | C_MUL(scratch[4], *Fout4, tw[4 * u * fstride]); |
| 181 | |
| 182 | C_ADD(scratch[7], scratch[1], scratch[4]); |
| 183 | C_SUB(scratch[10], scratch[1], scratch[4]); |
| 184 | C_ADD(scratch[8], scratch[2], scratch[3]); |
| 185 | C_SUB(scratch[9], scratch[2], scratch[3]); |
| 186 | |
| 187 | Fout0->r += scratch[7].r + scratch[8].r; |
| 188 | Fout0->i += scratch[7].i + scratch[8].i; |
| 189 | |
| 190 | scratch[5].r = |
| 191 | scratch[0].r + S_MUL(scratch[7].r, ya.r) + S_MUL(scratch[8].r, yb.r); |
| 192 | scratch[5].i = |
| 193 | scratch[0].i + S_MUL(scratch[7].i, ya.r) + S_MUL(scratch[8].i, yb.r); |
| 194 | |
| 195 | scratch[6].r = S_MUL(scratch[10].i, ya.i) + S_MUL(scratch[9].i, yb.i); |
| 196 | scratch[6].i = -S_MUL(scratch[10].r, ya.i) - S_MUL(scratch[9].r, yb.i); |
| 197 | |
| 198 | C_SUB(*Fout1, scratch[5], scratch[6]); |
| 199 | C_ADD(*Fout4, scratch[5], scratch[6]); |
| 200 | |
| 201 | scratch[11].r = |
| 202 | scratch[0].r + S_MUL(scratch[7].r, yb.r) + S_MUL(scratch[8].r, ya.r); |
| 203 | scratch[11].i = |
| 204 | scratch[0].i + S_MUL(scratch[7].i, yb.r) + S_MUL(scratch[8].i, ya.r); |
| 205 | scratch[12].r = -S_MUL(scratch[10].i, yb.i) + S_MUL(scratch[9].i, ya.i); |
| 206 | scratch[12].i = S_MUL(scratch[10].r, yb.i) - S_MUL(scratch[9].r, ya.i); |
| 207 | |
| 208 | C_ADD(*Fout2, scratch[11], scratch[12]); |
| 209 | C_SUB(*Fout3, scratch[11], scratch[12]); |
| 210 | |
| 211 | ++Fout0; |
| 212 | ++Fout1; |
| 213 | ++Fout2; |
| 214 | ++Fout3; |
| 215 | ++Fout4; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* perform the butterfly for one stage of a mixed radix FFT */ |
| 220 | static void kf_bfly_generic(kiss_fft_cpx *Fout, const size_t fstride, |
| 221 | const kiss_fft_cfg st, int m, int p) { |
| 222 | int u, k, q1, q; |
| 223 | kiss_fft_cpx *twiddles = st->twiddles; |
| 224 | kiss_fft_cpx t; |
| 225 | int Norig = st->nfft; |
| 226 | |
| 227 | kiss_fft_cpx *scratch = |
| 228 | (kiss_fft_cpx *)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx) * p); |
| 229 | |
| 230 | for (u = 0; u < m; ++u) { |
| 231 | k = u; |
| 232 | for (q1 = 0; q1 < p; ++q1) { |
| 233 | scratch[q1] = Fout[k]; |
| 234 | C_FIXDIV(scratch[q1], p); |
| 235 | k += m; |
| 236 | } |
| 237 | |
| 238 | k = u; |
| 239 | for (q1 = 0; q1 < p; ++q1) { |
| 240 | int twidx = 0; |
| 241 | Fout[k] = scratch[0]; |
| 242 | for (q = 1; q < p; ++q) { |
| 243 | twidx += static_cast<int>(fstride) * k; |
| 244 | if (twidx >= Norig) |
| 245 | twidx -= Norig; |
| 246 | C_MUL(t, scratch[q], twiddles[twidx]); |
| 247 | C_ADDTO(Fout[k], t); |
| 248 | } |
| 249 | k += m; |
| 250 | } |
| 251 | } |
| 252 | KISS_FFT_TMP_FREE(scratch); |
| 253 | } |
| 254 | |
| 255 | static void kf_work(kiss_fft_cpx *Fout, const kiss_fft_cpx *f, |
| 256 | const size_t fstride, int in_stride, int *factors, |
| 257 | const kiss_fft_cfg st) { |
| 258 | kiss_fft_cpx *Fout_beg = Fout; |
| 259 | const int p = *factors++; /* the radix */ |
| 260 | const int m = *factors++; /* stage's fft length/p */ |
| 261 | const kiss_fft_cpx *Fout_end = Fout + p * m; |
| 262 | |
| 263 | #ifdef _OPENMP |
| 264 | // use openmp extensions at the |
| 265 | // top-level (not recursive) |
| 266 | if (fstride == 1 && p <= 5) { |
| 267 | int k; |
| 268 | |
| 269 | // execute the p different work units in different threads |
| 270 | #pragma omp parallel for |
| 271 | for (k = 0; k < p; ++k) |
| 272 | kf_work(Fout + k * m, f + fstride * in_stride * k, fstride * p, in_stride, |
| 273 | factors, st); |
| 274 | // all threads have joined by this point |
| 275 | |
| 276 | switch (p) { |
| 277 | case 2: |
| 278 | kf_bfly2(Fout, fstride, st, m); |
| 279 | break; |
| 280 | case 3: |
| 281 | kf_bfly3(Fout, fstride, st, m); |
| 282 | break; |
| 283 | case 4: |
| 284 | kf_bfly4(Fout, fstride, st, m); |
| 285 | break; |
| 286 | case 5: |
| 287 | kf_bfly5(Fout, fstride, st, m); |
| 288 | break; |
| 289 | default: |
| 290 | kf_bfly_generic(Fout, fstride, st, m, p); |
| 291 | break; |
| 292 | } |
| 293 | return; |
| 294 | } |
| 295 | #endif |
| 296 | |
| 297 | if (m == 1) { |
| 298 | do { |
| 299 | *Fout = *f; |
| 300 | f += fstride * in_stride; |
| 301 | } while (++Fout != Fout_end); |
| 302 | } else { |
| 303 | do { |
| 304 | // recursive call: |
| 305 | // DFT of size m*p performed by doing |
| 306 | // p instances of smaller DFTs of size m, |
| 307 | // each one takes a decimated version of the input |
| 308 | kf_work(Fout, f, fstride * p, in_stride, factors, st); |
| 309 | f += fstride * in_stride; |
| 310 | } while ((Fout += m) != Fout_end); |
| 311 | } |
| 312 | |
| 313 | Fout = Fout_beg; |
| 314 | |
| 315 | // recombine the p smaller DFTs |
| 316 | switch (p) { |
| 317 | case 2: |
| 318 | kf_bfly2(Fout, fstride, st, m); |
| 319 | break; |
| 320 | case 3: |
| 321 | kf_bfly3(Fout, fstride, st, m); |
| 322 | break; |
| 323 | case 4: |
| 324 | kf_bfly4(Fout, fstride, st, m); |
| 325 | break; |
| 326 | case 5: |
| 327 | kf_bfly5(Fout, fstride, st, m); |
| 328 | break; |
| 329 | default: |
| 330 | kf_bfly_generic(Fout, fstride, st, m, p); |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /* facbuf is populated by p1,m1,p2,m2, ... |
| 336 | where |
| 337 | p[i] * m[i] = m[i-1] |
| 338 | m0 = n */ |
| 339 | static void kf_factor(int n, int *facbuf) { |
| 340 | int p = 4; |
| 341 | double floor_sqrt; |
| 342 | floor_sqrt = floor(sqrt((double)n)); |
| 343 | |
| 344 | /*factor out powers of 4, powers of 2, then any remaining primes */ |
| 345 | do { |
| 346 | while (n % p) { |
| 347 | switch (p) { |
| 348 | case 4: |
| 349 | p = 2; |
| 350 | break; |
| 351 | case 2: |
| 352 | p = 3; |
| 353 | break; |
| 354 | default: |
| 355 | p += 2; |
| 356 | break; |
| 357 | } |
| 358 | if (p > floor_sqrt) |
| 359 | p = n; /* no more factors, skip to end */ |
| 360 | } |
| 361 | n /= p; |
| 362 | *facbuf++ = p; |
| 363 | *facbuf++ = n; |
| 364 | } while (n > 1); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * |
| 369 | * User-callable function to allocate all necessary storage space for the fft. |
| 370 | * |
| 371 | * The return value is a contiguous block of memory, allocated with malloc. As |
| 372 | * such, |
| 373 | * It can be freed with free(), rather than a kiss_fft-specific function. |
| 374 | * */ |
| 375 | kiss_fft_cfg kiss_fft_alloc(int nfft, int inverse_fft, void *mem, |
| 376 | size_t *lenmem) { |
| 377 | kiss_fft_cfg st = NULL; |
| 378 | size_t memneeded = sizeof(struct kiss_fft_state) + |
| 379 | sizeof(kiss_fft_cpx) * (nfft - 1); /* twiddle factors*/ |
| 380 | |
| 381 | if (lenmem == NULL) { |
| 382 | st = (kiss_fft_cfg) new char[memneeded]; |
| 383 | } else { |
| 384 | if (mem != NULL && *lenmem >= memneeded) |
| 385 | st = (kiss_fft_cfg)mem; |
| 386 | *lenmem = memneeded; |
| 387 | } |
| 388 | if (st) { |
| 389 | int i; |
| 390 | st->nfft = nfft; |
| 391 | st->inverse = inverse_fft; |
| 392 | |
| 393 | for (i = 0; i < nfft; ++i) { |
| 394 | const double pi = |
| 395 | 3.141592653589793238462643383279502884197169399375105820974944; |
| 396 | double phase = -2 * pi * i / nfft; |
| 397 | if (st->inverse) |
| 398 | phase *= -1; |
| 399 | kf_cexp(st->twiddles + i, phase); |
| 400 | } |
| 401 | |
| 402 | kf_factor(nfft, st->factors); |
| 403 | } |
| 404 | return st; |
| 405 | } |
| 406 | |
| 407 | void kiss_fft_stride(kiss_fft_cfg st, const kiss_fft_cpx *fin, |
| 408 | kiss_fft_cpx *fout, int in_stride) { |
| 409 | if (fin == fout) { |
| 410 | // NOTE: this is not really an in-place FFT algorithm. |
| 411 | // It just performs an out-of-place FFT into a temp buffer |
| 412 | kiss_fft_cpx *tmpbuf = |
| 413 | (kiss_fft_cpx *)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx) * st->nfft); |
| 414 | kf_work(tmpbuf, fin, 1, in_stride, st->factors, st); |
| 415 | memcpy(fout, tmpbuf, sizeof(kiss_fft_cpx) * st->nfft); |
| 416 | KISS_FFT_TMP_FREE(tmpbuf); |
| 417 | } else { |
| 418 | kf_work(fout, fin, 1, in_stride, st->factors, st); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void kiss_fft(kiss_fft_cfg cfg, const kiss_fft_cpx *fin, kiss_fft_cpx *fout) { |
| 423 | kiss_fft_stride(cfg, fin, fout, 1); |
| 424 | } |
| 425 | |
| 426 | void kiss_fft_cleanup(void) { |
| 427 | // nothing needed any more |
| 428 | } |
| 429 | |
| 430 | int kiss_fft_next_fast_size(int n) { |
| 431 | while (1) { |
| 432 | int m = n; |
| 433 | while ((m % 2) == 0) |
| 434 | m /= 2; |
| 435 | while ((m % 3) == 0) |
| 436 | m /= 3; |
| 437 | while ((m % 5) == 0) |
| 438 | m /= 5; |
| 439 | if (m <= 1) |
| 440 | break; /* n is completely factorable by twos, threes, and fives */ |
| 441 | n++; |
| 442 | } |
| 443 | return n; |
| 444 | } |