|
| 1 | +/* |
| 2 | + * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. |
| 3 | + * This file is part of KISS FFT - https://github.com/mborgerding/kissfft |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: BSD-3-Clause |
| 6 | + * See COPYING file for more information. |
| 7 | + */ |
| 8 | + |
| 9 | +/* kiss_fft.h |
| 10 | + defines kiss_fft_scalar as either short or a float type |
| 11 | + and defines |
| 12 | + typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ |
| 13 | +#include "kiss_fft.h" |
| 14 | +#include <limits.h> |
| 15 | + |
| 16 | +#define MAXFACTORS 32 |
| 17 | +/* e.g. an fft of length 128 has 4 factors |
| 18 | + as far as kissfft is concerned |
| 19 | + 4*4*4*2 |
| 20 | + */ |
| 21 | + |
| 22 | +struct kiss_fft_state{ |
| 23 | + int nfft; |
| 24 | + int inverse; |
| 25 | + int factors[2*MAXFACTORS]; |
| 26 | + kiss_fft_cpx twiddles[1]; |
| 27 | +}; |
| 28 | + |
| 29 | +/* |
| 30 | + Explanation of macros dealing with complex math: |
| 31 | +
|
| 32 | + C_MUL(m,a,b) : m = a*b |
| 33 | + C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise |
| 34 | + C_SUB( res, a,b) : res = a - b |
| 35 | + C_SUBFROM( res , a) : res -= a |
| 36 | + C_ADDTO( res , a) : res += a |
| 37 | + * */ |
| 38 | +#ifdef FIXED_POINT |
| 39 | +#if (FIXED_POINT==32) |
| 40 | +# define FRACBITS 31 |
| 41 | +# define SAMPPROD int64_t |
| 42 | +#define SAMP_MAX 2147483647 |
| 43 | +#else |
| 44 | +# define FRACBITS 15 |
| 45 | +# define SAMPPROD int32_t |
| 46 | +#define SAMP_MAX 32767 |
| 47 | +#endif |
| 48 | + |
| 49 | +#define SAMP_MIN -SAMP_MAX |
| 50 | + |
| 51 | +#if defined(CHECK_OVERFLOW) |
| 52 | +# define CHECK_OVERFLOW_OP(a,op,b) \ |
| 53 | + if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ |
| 54 | + fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } |
| 55 | +#endif |
| 56 | + |
| 57 | + |
| 58 | +# define smul(a,b) ( (SAMPPROD)(a)*(b) ) |
| 59 | +# define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) |
| 60 | + |
| 61 | +# define S_MUL(a,b) sround( smul(a,b) ) |
| 62 | + |
| 63 | +# define C_MUL(m,a,b) \ |
| 64 | + do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ |
| 65 | + (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) |
| 66 | + |
| 67 | +# define DIVSCALAR(x,k) \ |
| 68 | + (x) = sround( smul( x, SAMP_MAX/k ) ) |
| 69 | + |
| 70 | +# define C_FIXDIV(c,div) \ |
| 71 | + do { DIVSCALAR( (c).r , div); \ |
| 72 | + DIVSCALAR( (c).i , div); }while (0) |
| 73 | + |
| 74 | +# define C_MULBYSCALAR( c, s ) \ |
| 75 | + do{ (c).r = sround( smul( (c).r , s ) ) ;\ |
| 76 | + (c).i = sround( smul( (c).i , s ) ) ; }while(0) |
| 77 | + |
| 78 | +#else /* not FIXED_POINT*/ |
| 79 | + |
| 80 | +# define S_MUL(a,b) ( (a)*(b) ) |
| 81 | +#define C_MUL(m,a,b) \ |
| 82 | + do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ |
| 83 | + (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) |
| 84 | +# define C_FIXDIV(c,div) /* NOOP */ |
| 85 | +# define C_MULBYSCALAR( c, s ) \ |
| 86 | + do{ (c).r *= (s);\ |
| 87 | + (c).i *= (s); }while(0) |
| 88 | +#endif |
| 89 | + |
| 90 | +#ifndef CHECK_OVERFLOW_OP |
| 91 | +# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ |
| 92 | +#endif |
| 93 | + |
| 94 | +#define C_ADD( res, a,b)\ |
| 95 | + do { \ |
| 96 | + CHECK_OVERFLOW_OP((a).r,+,(b).r)\ |
| 97 | + CHECK_OVERFLOW_OP((a).i,+,(b).i)\ |
| 98 | + (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ |
| 99 | + }while(0) |
| 100 | +#define C_SUB( res, a,b)\ |
| 101 | + do { \ |
| 102 | + CHECK_OVERFLOW_OP((a).r,-,(b).r)\ |
| 103 | + CHECK_OVERFLOW_OP((a).i,-,(b).i)\ |
| 104 | + (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ |
| 105 | + }while(0) |
| 106 | +#define C_ADDTO( res , a)\ |
| 107 | + do { \ |
| 108 | + CHECK_OVERFLOW_OP((res).r,+,(a).r)\ |
| 109 | + CHECK_OVERFLOW_OP((res).i,+,(a).i)\ |
| 110 | + (res).r += (a).r; (res).i += (a).i;\ |
| 111 | + }while(0) |
| 112 | + |
| 113 | +#define C_SUBFROM( res , a)\ |
| 114 | + do {\ |
| 115 | + CHECK_OVERFLOW_OP((res).r,-,(a).r)\ |
| 116 | + CHECK_OVERFLOW_OP((res).i,-,(a).i)\ |
| 117 | + (res).r -= (a).r; (res).i -= (a).i; \ |
| 118 | + }while(0) |
| 119 | + |
| 120 | + |
| 121 | +#ifdef FIXED_POINT |
| 122 | +# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) |
| 123 | +# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) |
| 124 | +# define HALF_OF(x) ((x)>>1) |
| 125 | +#elif defined(USE_SIMD) |
| 126 | +# define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) |
| 127 | +# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) |
| 128 | +# define HALF_OF(x) ((x)*_mm_set1_ps(.5)) |
| 129 | +#else |
| 130 | +# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) |
| 131 | +# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) |
| 132 | +# define HALF_OF(x) ((x)*.5) |
| 133 | +#endif |
| 134 | + |
| 135 | +#define kf_cexp(x,phase) \ |
| 136 | + do{ \ |
| 137 | + (x)->r = KISS_FFT_COS(phase);\ |
| 138 | + (x)->i = KISS_FFT_SIN(phase);\ |
| 139 | + }while(0) |
| 140 | + |
| 141 | + |
| 142 | +/* a debugging function */ |
| 143 | +#define pcpx(c)\ |
| 144 | + fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) |
| 145 | + |
| 146 | + |
| 147 | +#ifdef KISS_FFT_USE_ALLOCA |
| 148 | +// define this to allow use of alloca instead of malloc for temporary buffers |
| 149 | +// Temporary buffers are used in two case: |
| 150 | +// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 |
| 151 | +// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. |
| 152 | +#include <alloca.h> |
| 153 | +#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) |
| 154 | +#define KISS_FFT_TMP_FREE(ptr) |
| 155 | +#else |
| 156 | +#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) |
| 157 | +#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) |
| 158 | +#endif |
0 commit comments