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