Chris@434: #ifndef KISS_FFT__GUTS_H Chris@434: #define KISS_FFT__GUTS_H Chris@434: /* Chris@434: Copyright (c) 2003-2010, Mark Borgerding Chris@434: Chris@434: All rights reserved. Chris@434: Chris@434: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Chris@434: Chris@434: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Chris@434: * 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. Chris@434: * 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. Chris@434: Chris@434: 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. Chris@434: */ Chris@434: Chris@434: /* kiss_fft.h Chris@434: defines kiss_fft_scalar as either short or a float type Chris@434: and defines Chris@434: typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ Chris@434: #include "kiss_fft.h" Chris@434: #include Chris@434: Chris@434: #define MAXFACTORS 32 Chris@434: /* e.g. an fft of length 128 has 4 factors Chris@434: as far as kissfft is concerned Chris@434: 4*4*4*2 Chris@434: */ Chris@434: Chris@434: struct kiss_fft_state{ Chris@434: int nfft; Chris@434: int inverse; Chris@434: int factors[2*MAXFACTORS]; Chris@434: kiss_fft_cpx twiddles[1]; Chris@434: }; Chris@434: Chris@434: /* Chris@434: Explanation of macros dealing with complex math: Chris@434: Chris@434: C_MUL(m,a,b) : m = a*b Chris@434: C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise Chris@434: C_SUB( res, a,b) : res = a - b Chris@434: C_SUBFROM( res , a) : res -= a Chris@434: C_ADDTO( res , a) : res += a Chris@434: * */ Chris@434: #ifdef FIXED_POINT Chris@434: #if (FIXED_POINT==32) Chris@434: # define FRACBITS 31 Chris@434: # define SAMPPROD int64_t Chris@434: #define SAMP_MAX 2147483647 Chris@434: #else Chris@434: # define FRACBITS 15 Chris@434: # define SAMPPROD int32_t Chris@434: #define SAMP_MAX 32767 Chris@434: #endif Chris@434: Chris@434: #define SAMP_MIN -SAMP_MAX Chris@434: Chris@434: #if defined(CHECK_OVERFLOW) Chris@434: # define CHECK_OVERFLOW_OP(a,op,b) \ Chris@434: if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ Chris@434: fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } Chris@434: #endif Chris@434: Chris@434: Chris@434: # define smul(a,b) ( (SAMPPROD)(a)*(b) ) Chris@434: # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) Chris@434: Chris@434: # define S_MUL(a,b) sround( smul(a,b) ) Chris@434: Chris@434: # define C_MUL(m,a,b) \ Chris@434: do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ Chris@434: (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) Chris@434: Chris@434: # define DIVSCALAR(x,k) \ Chris@434: (x) = sround( smul( x, SAMP_MAX/k ) ) Chris@434: Chris@434: # define C_FIXDIV(c,div) \ Chris@434: do { DIVSCALAR( (c).r , div); \ Chris@434: DIVSCALAR( (c).i , div); }while (0) Chris@434: Chris@434: # define C_MULBYSCALAR( c, s ) \ Chris@434: do{ (c).r = sround( smul( (c).r , s ) ) ;\ Chris@434: (c).i = sround( smul( (c).i , s ) ) ; }while(0) Chris@434: Chris@434: #else /* not FIXED_POINT*/ Chris@434: Chris@434: # define S_MUL(a,b) ( (a)*(b) ) Chris@434: #define C_MUL(m,a,b) \ Chris@434: do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ Chris@434: (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) Chris@434: # define C_FIXDIV(c,div) /* NOOP */ Chris@434: # define C_MULBYSCALAR( c, s ) \ Chris@434: do{ (c).r *= (s);\ Chris@434: (c).i *= (s); }while(0) Chris@434: #endif Chris@434: Chris@434: #ifndef CHECK_OVERFLOW_OP Chris@434: # define CHECK_OVERFLOW_OP(a,op,b) /* noop */ Chris@434: #endif Chris@434: Chris@434: #define C_ADD( res, a,b)\ Chris@434: do { \ Chris@434: CHECK_OVERFLOW_OP((a).r,+,(b).r)\ Chris@434: CHECK_OVERFLOW_OP((a).i,+,(b).i)\ Chris@434: (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ Chris@434: }while(0) Chris@434: #define C_SUB( res, a,b)\ Chris@434: do { \ Chris@434: CHECK_OVERFLOW_OP((a).r,-,(b).r)\ Chris@434: CHECK_OVERFLOW_OP((a).i,-,(b).i)\ Chris@434: (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ Chris@434: }while(0) Chris@434: #define C_ADDTO( res , a)\ Chris@434: do { \ Chris@434: CHECK_OVERFLOW_OP((res).r,+,(a).r)\ Chris@434: CHECK_OVERFLOW_OP((res).i,+,(a).i)\ Chris@434: (res).r += (a).r; (res).i += (a).i;\ Chris@434: }while(0) Chris@434: Chris@434: #define C_SUBFROM( res , a)\ Chris@434: do {\ Chris@434: CHECK_OVERFLOW_OP((res).r,-,(a).r)\ Chris@434: CHECK_OVERFLOW_OP((res).i,-,(a).i)\ Chris@434: (res).r -= (a).r; (res).i -= (a).i; \ Chris@434: }while(0) Chris@434: Chris@434: Chris@434: #ifdef FIXED_POINT Chris@434: # define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) Chris@434: # define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) Chris@434: # define HALF_OF(x) ((x)>>1) Chris@434: #elif defined(USE_SIMD) Chris@434: # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) Chris@434: # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) Chris@434: # define HALF_OF(x) ((x)*_mm_set1_ps(.5)) Chris@434: #else Chris@434: # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) Chris@434: # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) Chris@434: # define HALF_OF(x) ((x)*.5) Chris@434: #endif Chris@434: Chris@434: #define kf_cexp(x,phase) \ Chris@434: do{ \ Chris@434: (x)->r = KISS_FFT_COS(phase);\ Chris@434: (x)->i = KISS_FFT_SIN(phase);\ Chris@434: }while(0) Chris@434: Chris@434: Chris@434: /* a debugging function */ Chris@434: #define pcpx(c)\ Chris@434: fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) Chris@434: Chris@434: Chris@434: #ifdef KISS_FFT_USE_ALLOCA Chris@434: // define this to allow use of alloca instead of malloc for temporary buffers Chris@434: // Temporary buffers are used in two case: Chris@434: // 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 Chris@434: // 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. Chris@434: #include Chris@434: #define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) Chris@434: #define KISS_FFT_TMP_FREE(ptr) Chris@434: #else Chris@434: #define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) Chris@434: #define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) Chris@434: #endif Chris@434: Chris@434: #endif