1 /*
2 Copyright (c) 2003-2004, 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 #define MIN(a,b) ((a)<(b) ? (a):(b))
16 #define MAX(a,b) ((a)>(b) ? (a):(b))
17 
18 /* kiss_fft.h
19    defines kiss_fft_scalar as either short or a float type
20    and defines
21    typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
22 #include "kiss_fft.h"
23 #include <limits.h>
24 
25 #define MAXFACTORS 32
26 /* e.g. an fft of length 128 has 4 factors
27  as far as kissfft is concerned
28  4*4*4*2
29  */
30 
31 struct kiss_fft_state{
32     int nfft;
33     int inverse;
34     int factors[2*MAXFACTORS];
35     kiss_fft_cpx twiddles[1];
36 };
37 
38 /*
39   Explanation of macros dealing with complex math:
40 
41    C_MUL(m,a,b)         : m = a*b
42    C_FIXDIV( c , div )  : if a fixed point impl., c /= div. noop otherwise
43    C_SUB( res, a,b)     : res = a - b
44    C_SUBFROM( res , a)  : res -= a
45    C_ADDTO( res , a)    : res += a
46  * */
47 #ifdef FIXED_POINT
48 # define FRACBITS 15
49 # define SAMPPROD int32_t
50 #define SAMP_MAX 32767
51 
52 #define SAMP_MIN -SAMP_MAX
53 
54 #if defined(CHECK_OVERFLOW)
55 #  define CHECK_OVERFLOW_OP(a,op,b)  \
56 	if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
57 		fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) );  }
58 #endif
59 
60 
61 #   define smul(a,b) ( (SAMPPROD)(a)*(b) )
62 #   define sround( x )  (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
63 
64 #   define S_MUL(a,b) sround( smul(a,b) )
65 
66 #   define C_MUL(m,a,b) \
67       do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
68           (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
69 
70 #   define DIVSCALAR(x,k) \
71 	(x) = sround( smul(  x, SAMP_MAX/k ) )
72 
73 #   define C_FIXDIV(c,div) \
74 	do {    DIVSCALAR( (c).r , div);  \
75 		DIVSCALAR( (c).i  , div); }while (0)
76 
77 #   define C_MULBYSCALAR( c, s ) \
78     do{ (c).r =  sround( smul( (c).r , s ) ) ;\
79         (c).i =  sround( smul( (c).i , s ) ) ; }while(0)
80 
81 #else  /* not FIXED_POINT*/
82 
83 #   define S_MUL(a,b) ( (a)*(b) )
84 #define C_MUL(m,a,b) \
85     do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
86         (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
87 #   define C_FIXDIV(c,div) /* NOOP */
88 #   define C_MULBYSCALAR( c, s ) \
89     do{ (c).r *= (s);\
90         (c).i *= (s); }while(0)
91 #endif
92 
93 #ifndef CHECK_OVERFLOW_OP
94 #  define CHECK_OVERFLOW_OP(a,op,b) /* noop */
95 #endif
96 
97 #define  C_ADD( res, a,b)\
98     do { \
99 	    CHECK_OVERFLOW_OP((a).r,+,(b).r)\
100 	    CHECK_OVERFLOW_OP((a).i,+,(b).i)\
101 	    (res).r=(a).r+(b).r;  (res).i=(a).i+(b).i; \
102     }while(0)
103 #define  C_SUB( res, a,b)\
104     do { \
105 	    CHECK_OVERFLOW_OP((a).r,-,(b).r)\
106 	    CHECK_OVERFLOW_OP((a).i,-,(b).i)\
107 	    (res).r=(a).r-(b).r;  (res).i=(a).i-(b).i; \
108     }while(0)
109 #define C_ADDTO( res , a)\
110     do { \
111 	    CHECK_OVERFLOW_OP((res).r,+,(a).r)\
112 	    CHECK_OVERFLOW_OP((res).i,+,(a).i)\
113 	    (res).r += (a).r;  (res).i += (a).i;\
114     }while(0)
115 
116 #define C_SUBFROM( res , a)\
117     do {\
118 	    CHECK_OVERFLOW_OP((res).r,-,(a).r)\
119 	    CHECK_OVERFLOW_OP((res).i,-,(a).i)\
120 	    (res).r -= (a).r;  (res).i -= (a).i; \
121     }while(0)
122 
123 
124 #ifdef FIXED_POINT
125 #  define KISS_FFT_COS(phase)  floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
126 #  define KISS_FFT_SIN(phase)  floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))
127 #  define HALF_OF(x) ((x)>>1)
128 #elif defined(USE_SIMD)
129 #  define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
130 #  define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
131 #  define HALF_OF(x) ((x)*_mm_set1_ps(.5))
132 #else
133 #  define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
134 #  define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
135 #  define HALF_OF(x) ((x)*.5)
136 #endif
137 
138 #define  kf_cexp(x,phase) \
139 	do{ \
140 		(x)->r = KISS_FFT_COS(phase);\
141 		(x)->i = KISS_FFT_SIN(phase);\
142 	}while(0)
143 
144 
145 /* a debugging function */
146 #define pcpx(c)\
147     fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )
148