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; */
29 Explanation of macros dealing with complex math:
31 C_MUL(m,a,b) : m = a*b
32 C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
33 C_SUB( res, a,b) : res = a - b
34 C_SUBFROM( res , a) : res -= a
35 C_ADDTO( res , a) : res += a
40 #define DOUBLE_PRECISION
42 #ifdef DOUBLE_PRECISION
45 # define SAMPPROD long long
46 #define SAMP_MAX 2147483647
47 #define TWID_MAX 32767
48 #define TRIG_UPSCALE 1
51 #else /* DOUBLE_PRECISION */
54 # define SAMPPROD celt_int32
55 #define SAMP_MAX 32767
56 #define TRIG_UPSCALE 1
57 #define EXT32(a) EXTEND32(a)
59 #endif /* !DOUBLE_PRECISION */
61 #define SAMP_MIN -SAMP_MAX
63 #if defined(CHECK_OVERFLOW)
64 # define CHECK_OVERFLOW_OP(a,op,b) \
65 if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
66 fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
69 # define smul(a,b) ( (SAMPPROD)(a)*(b) )
70 # define sround( x ) (kiss_fft_scalar)( ( (x) + ((SAMPPROD)1<<(FRACBITS-1)) ) >> FRACBITS )
73 # define S_MUL(a,b) MULT16_32_Q15(b, a)
75 # define C_MUL(m,a,b) \
76 do{ (m).r = SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
77 (m).i = ADD32(S_MUL((a).r,(b).i) , S_MUL((a).i,(b).r)); }while(0)
79 # define C_MULC(m,a,b) \
80 do{ (m).r = ADD32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
81 (m).i = SUB32(S_MUL((a).i,(b).r) , S_MUL((a).r,(b).i)); }while(0)
83 # define C_MUL4(m,a,b) \
84 do{ (m).r = SHR(SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)),2); \
85 (m).i = SHR(ADD32(S_MUL((a).r,(b).i) , S_MUL((a).i,(b).r)),2); }while(0)
87 # define C_MULBYSCALAR( c, s ) \
88 do{ (c).r = S_MUL( (c).r , s ) ;\
89 (c).i = S_MUL( (c).i , s ) ; }while(0)
91 # define DIVSCALAR(x,k) \
92 (x) = S_MUL( x, (TWID_MAX-((k)>>1))/(k)+1 )
94 # define C_FIXDIV(c,div) \
95 do { DIVSCALAR( (c).r , div); \
96 DIVSCALAR( (c).i , div); }while (0)
98 #define C_ADD( res, a,b)\
99 do {(res).r=ADD32((a).r,(b).r); (res).i=ADD32((a).i,(b).i); \
101 #define C_SUB( res, a,b)\
102 do {(res).r=SUB32((a).r,(b).r); (res).i=SUB32((a).i,(b).i); \
104 #define C_ADDTO( res , a)\
105 do {(res).r = ADD32((res).r, (a).r); (res).i = ADD32((res).i,(a).i);\
108 #define C_SUBFROM( res , a)\
109 do {(res).r = ADD32((res).r,(a).r); (res).i = SUB32((res).i,(a).i); \
115 #else /* not FIXED_POINT*/
119 # define S_MUL(a,b) ( (a)*(b) )
120 #define C_MUL(m,a,b) \
121 do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
122 (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
123 #define C_MULC(m,a,b) \
124 do{ (m).r = (a).r*(b).r + (a).i*(b).i;\
125 (m).i = (a).i*(b).r - (a).r*(b).i; }while(0)
127 #define C_MUL4(m,a,b) C_MUL(m,a,b)
129 # define C_FIXDIV(c,div) /* NOOP */
130 # define C_MULBYSCALAR( c, s ) \
132 (c).i *= (s); }while(0)
137 #ifndef CHECK_OVERFLOW_OP
138 # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
142 #define C_ADD( res, a,b)\
144 CHECK_OVERFLOW_OP((a).r,+,(b).r)\
145 CHECK_OVERFLOW_OP((a).i,+,(b).i)\
146 (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
148 #define C_SUB( res, a,b)\
150 CHECK_OVERFLOW_OP((a).r,-,(b).r)\
151 CHECK_OVERFLOW_OP((a).i,-,(b).i)\
152 (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
154 #define C_ADDTO( res , a)\
156 CHECK_OVERFLOW_OP((res).r,+,(a).r)\
157 CHECK_OVERFLOW_OP((res).i,+,(a).i)\
158 (res).r += (a).r; (res).i += (a).i;\
161 #define C_SUBFROM( res , a)\
163 CHECK_OVERFLOW_OP((res).r,-,(a).r)\
164 CHECK_OVERFLOW_OP((res).i,-,(a).i)\
165 (res).r -= (a).r; (res).i -= (a).i; \
167 #endif /* C_ADD defined */
170 /*# define KISS_FFT_COS(phase) TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
171 # define KISS_FFT_SIN(phase) TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))*/
172 # define KISS_FFT_COS(phase) floor(.5+TWID_MAX*cos (phase))
173 # define KISS_FFT_SIN(phase) floor(.5+TWID_MAX*sin (phase))
174 # define HALF_OF(x) ((x)>>1)
175 #elif defined(USE_SIMD)
176 # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
177 # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
178 # define HALF_OF(x) ((x)*_mm_set1_ps(.5f))
180 # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
181 # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
182 # define HALF_OF(x) ((x)*.5f)
185 #define kf_cexp(x,phase) \
187 (x)->r = KISS_FFT_COS(phase);\
188 (x)->i = KISS_FFT_SIN(phase);\
191 #define kf_cexp2(x,phase) \
193 (x)->r = TRIG_UPSCALE*celt_cos_norm((phase));\
194 (x)->i = TRIG_UPSCALE*celt_cos_norm((phase)-32768);\
198 #endif /* KISS_FFT_GUTS_H */