_kiss_fft_guts.h

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2003-2004, Mark Borgerding
00003 
00004 All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * 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.
00010     * 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.
00011 
00012 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.
00013 */
00014 
00015 #define MIN(a,b) ((a)<(b) ? (a):(b))
00016 #define MAX(a,b) ((a)>(b) ? (a):(b))
00017 
00018 /* kiss_fft.h
00019    defines kiss_fft_scalar as either short or a float type
00020    and defines
00021    typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
00022 #include "kiss_fft.h"
00023 #include <limits.h>
00024 
00025 #define MAXFACTORS 32
00026 /* e.g. an fft of length 128 has 4 factors 
00027  as far as kissfft is concerned
00028  4*4*4*2
00029  */
00030 
00031 struct kiss_fft_state{
00032     int nfft;
00033     int inverse;
00034     int factors[2*MAXFACTORS];
00035     kiss_fft_cpx twiddles[1];
00036 };
00037 
00038 /*
00039   Explanation of macros dealing with complex math:
00040 
00041    C_MUL(m,a,b)         : m = a*b
00042    C_FIXDIV( c , div )  : if a fixed point impl., c /= div. noop otherwise
00043    C_SUB( res, a,b)     : res = a - b
00044    C_SUBFROM( res , a)  : res -= a
00045    C_ADDTO( res , a)    : res += a
00046  * */
00047 #ifdef FIXED_POINT
00048 # define FRACBITS 15
00049 # define SAMPPROD int32_t 
00050 #define SAMP_MAX 32767
00051 
00052 #define SAMP_MIN -SAMP_MAX
00053 
00054 #if defined(CHECK_OVERFLOW)
00055 #  define CHECK_OVERFLOW_OP(a,op,b)  \
00056         if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
00057                 fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) );  }
00058 #endif
00059 
00060 
00061 #   define smul(a,b) ( (SAMPPROD)(a)*(b) )
00062 #   define sround( x )  (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
00063 
00064 #   define S_MUL(a,b) sround( smul(a,b) )
00065 
00066 #   define C_MUL(m,a,b) \
00067       do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
00068           (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
00069 
00070 #   define DIVSCALAR(x,k) \
00071         (x) = sround( smul(  x, SAMP_MAX/k ) )
00072 
00073 #   define C_FIXDIV(c,div) \
00074         do {    DIVSCALAR( (c).r , div);  \
00075                 DIVSCALAR( (c).i  , div); }while (0)
00076 
00077 #   define C_MULBYSCALAR( c, s ) \
00078     do{ (c).r =  sround( smul( (c).r , s ) ) ;\
00079         (c).i =  sround( smul( (c).i , s ) ) ; }while(0)
00080 
00081 #else  /* not FIXED_POINT*/
00082 
00083 #   define S_MUL(a,b) ( (a)*(b) )
00084 #define C_MUL(m,a,b) \
00085     do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
00086         (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
00087 #   define C_FIXDIV(c,div) /* NOOP */
00088 #   define C_MULBYSCALAR( c, s ) \
00089     do{ (c).r *= (s);\
00090         (c).i *= (s); }while(0)
00091 #endif
00092 
00093 #ifndef CHECK_OVERFLOW_OP
00094 #  define CHECK_OVERFLOW_OP(a,op,b) /* noop */
00095 #endif
00096 
00097 #define  C_ADD( res, a,b)\
00098     do { \
00099             CHECK_OVERFLOW_OP((a).r,+,(b).r)\
00100             CHECK_OVERFLOW_OP((a).i,+,(b).i)\
00101             (res).r=(a).r+(b).r;  (res).i=(a).i+(b).i; \
00102     }while(0)
00103 #define  C_SUB( res, a,b)\
00104     do { \
00105             CHECK_OVERFLOW_OP((a).r,-,(b).r)\
00106             CHECK_OVERFLOW_OP((a).i,-,(b).i)\
00107             (res).r=(a).r-(b).r;  (res).i=(a).i-(b).i; \
00108     }while(0)
00109 #define C_ADDTO( res , a)\
00110     do { \
00111             CHECK_OVERFLOW_OP((res).r,+,(a).r)\
00112             CHECK_OVERFLOW_OP((res).i,+,(a).i)\
00113             (res).r += (a).r;  (res).i += (a).i;\
00114     }while(0)
00115 
00116 #define C_SUBFROM( res , a)\
00117     do {\
00118             CHECK_OVERFLOW_OP((res).r,-,(a).r)\
00119             CHECK_OVERFLOW_OP((res).i,-,(a).i)\
00120             (res).r -= (a).r;  (res).i -= (a).i; \
00121     }while(0)
00122 
00123 
00124 #ifdef FIXED_POINT
00125 #  define KISS_FFT_COS(phase)  floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
00126 #  define KISS_FFT_SIN(phase)  floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))
00127 #  define HALF_OF(x) ((x)>>1)
00128 #elif defined(USE_SIMD)
00129 #  define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
00130 #  define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
00131 #  define HALF_OF(x) ((x)*_mm_set1_ps(.5))
00132 #else
00133 #  define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
00134 #  define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
00135 #  define HALF_OF(x) ((x)*.5)
00136 #endif
00137 
00138 #define  kf_cexp(x,phase) \
00139         do{ \
00140                 (x)->r = KISS_FFT_COS(phase);\
00141                 (x)->i = KISS_FFT_SIN(phase);\
00142         }while(0)
00143 
00144 
00145 /* a debugging function */
00146 #define pcpx(c)\
00147     fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )

Generated on Mon Jan 22 16:50:41 2007 for speex by  doxygen 1.5.1