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