2 Copyright (c) 2003-2004, Mark Borgerding
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
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.
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.
15 #ifndef KISS_FFT_GUTS_H
16 #define KISS_FFT_GUTS_H
18 #define MIN(a,b) ((a)<(b) ? (a):(b))
19 #define MAX(a,b) ((a)>(b) ? (a):(b))
22 defines kiss_fft_scalar as either short or a float type
24 typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
28 /* e.g. an fft of length 128 has 4 factors
29 as far as kissfft is concerned
33 struct kiss_fft_state{
36 kiss_fft_scalar scale;
38 int factors[2*MAXFACTORS];
40 kiss_twiddle_cpx twiddles[1];
44 Explanation of macros dealing with complex math:
46 C_MUL(m,a,b) : m = a*b
47 C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
48 C_SUB( res, a,b) : res = a - b
49 C_SUBFROM( res , a) : res -= a
50 C_ADDTO( res , a) : res += a
55 #ifdef DOUBLE_PRECISION
58 # define SAMPPROD celt_int64_t
59 #define SAMP_MAX 2147483647
60 #ifdef MIXED_PRECISION
61 #define TWID_MAX 32767
62 #define TRIG_UPSCALE 1
64 #define TRIG_UPSCALE 65536
65 #define TWID_MAX 2147483647
69 #else /* DOUBLE_PRECISION */
72 # define SAMPPROD celt_int32_t
73 #define SAMP_MAX 32767
74 #define TRIG_UPSCALE 1
75 #define EXT32(a) EXTEND32(a)
77 #endif /* !DOUBLE_PRECISION */
79 #define SAMP_MIN -SAMP_MAX
81 #if defined(CHECK_OVERFLOW)
82 # define CHECK_OVERFLOW_OP(a,op,b) \
83 if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
84 fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
87 # define smul(a,b) ( (SAMPPROD)(a)*(b) )
88 # define sround( x ) (kiss_fft_scalar)( ( (x) + ((SAMPPROD)1<<(FRACBITS-1)) ) >> FRACBITS )
90 #ifdef MIXED_PRECISION
92 # define S_MUL(a,b) MULT16_32_Q15(b, a)
94 # define C_MUL(m,a,b) \
95 do{ (m).r = S_MUL((a).r,(b).r) - S_MUL((a).i,(b).i); \
96 (m).i = S_MUL((a).r,(b).i) + S_MUL((a).i,(b).r); }while(0)
98 # define C_MULC(m,a,b) \
99 do{ (m).r = S_MUL((a).r,(b).r) + S_MUL((a).i,(b).i); \
100 (m).i = S_MUL((a).i,(b).r) - S_MUL((a).r,(b).i); }while(0)
102 # define C_MUL4(m,a,b) \
103 do{ (m).r = SHR(S_MUL((a).r,(b).r) - S_MUL((a).i,(b).i),2); \
104 (m).i = SHR(S_MUL((a).r,(b).i) + S_MUL((a).i,(b).r),2); }while(0)
106 # define C_MULBYSCALAR( c, s ) \
107 do{ (c).r = S_MUL( (c).r , s ) ;\
108 (c).i = S_MUL( (c).i , s ) ; }while(0)
110 # define DIVSCALAR(x,k) \
111 (x) = S_MUL( x, (TWID_MAX-((k)>>1))/(k)+1 )
113 # define C_FIXDIV(c,div) \
114 do { DIVSCALAR( (c).r , div); \
115 DIVSCALAR( (c).i , div); }while (0)
117 #else /* MIXED_PRECISION */
118 # define sround4( x ) (kiss_fft_scalar)( ( (x) + ((SAMPPROD)1<<(FRACBITS-1)) ) >> (FRACBITS+2) )
120 # define S_MUL(a,b) sround( smul(a,b) )
122 # define C_MUL(m,a,b) \
123 do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
124 (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
125 # define C_MULC(m,a,b) \
126 do{ (m).r = sround( smul((a).r,(b).r) + smul((a).i,(b).i) ); \
127 (m).i = sround( smul((a).i,(b).r) - smul((a).r,(b).i) ); }while(0)
129 # define C_MUL4(m,a,b) \
130 do{ (m).r = sround4( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
131 (m).i = sround4( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
133 # define C_MULBYSCALAR( c, s ) \
134 do{ (c).r = sround( smul( (c).r , s ) ) ;\
135 (c).i = sround( smul( (c).i , s ) ) ; }while(0)
137 # define DIVSCALAR(x,k) \
138 (x) = sround( smul( x, SAMP_MAX/k ) )
140 # define C_FIXDIV(c,div) \
141 do { DIVSCALAR( (c).r , div); \
142 DIVSCALAR( (c).i , div); }while (0)
144 #endif /* !MIXED_PRECISION */
148 #else /* not FIXED_POINT*/
152 # define S_MUL(a,b) ( (a)*(b) )
153 #define C_MUL(m,a,b) \
154 do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
155 (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
156 #define C_MULC(m,a,b) \
157 do{ (m).r = (a).r*(b).r + (a).i*(b).i;\
158 (m).i = (a).i*(b).r - (a).r*(b).i; }while(0)
160 #define C_MUL4(m,a,b) C_MUL(m,a,b)
162 # define C_FIXDIV(c,div) /* NOOP */
163 # define C_MULBYSCALAR( c, s ) \
165 (c).i *= (s); }while(0)
168 #ifndef CHECK_OVERFLOW_OP
169 # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
172 #define C_ADD( res, a,b)\
174 CHECK_OVERFLOW_OP((a).r,+,(b).r)\
175 CHECK_OVERFLOW_OP((a).i,+,(b).i)\
176 (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
178 #define C_SUB( res, a,b)\
180 CHECK_OVERFLOW_OP((a).r,-,(b).r)\
181 CHECK_OVERFLOW_OP((a).i,-,(b).i)\
182 (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
184 #define C_ADDTO( res , a)\
186 CHECK_OVERFLOW_OP((res).r,+,(a).r)\
187 CHECK_OVERFLOW_OP((res).i,+,(a).i)\
188 (res).r += (a).r; (res).i += (a).i;\
191 #define C_SUBFROM( res , a)\
193 CHECK_OVERFLOW_OP((res).r,-,(a).r)\
194 CHECK_OVERFLOW_OP((res).i,-,(a).i)\
195 (res).r -= (a).r; (res).i -= (a).i; \
200 /*# define KISS_FFT_COS(phase) TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
201 # define KISS_FFT_SIN(phase) TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))*/
202 # define KISS_FFT_COS(phase) floor(.5+TWID_MAX*cos (phase))
203 # define KISS_FFT_SIN(phase) floor(.5+TWID_MAX*sin (phase))
204 # define HALF_OF(x) ((x)>>1)
205 #elif defined(USE_SIMD)
206 # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
207 # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
208 # define HALF_OF(x) ((x)*_mm_set1_ps(.5))
210 # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
211 # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
212 # define HALF_OF(x) ((x)*.5)
215 #define kf_cexp(x,phase) \
217 (x)->r = KISS_FFT_COS(phase);\
218 (x)->i = KISS_FFT_SIN(phase);\
221 #define kf_cexp2(x,phase) \
223 (x)->r = TRIG_UPSCALE*celt_cos_norm((phase));\
224 (x)->i = TRIG_UPSCALE*celt_cos_norm((phase)-32768);\
228 #endif /* KISS_FFT_GUTS_H */