FS#11968 by Peter Lecky - Slovak language update
[maemo-rb.git] / apps / codecs / libspeex / _kiss_fft_guts.h
blob12c055040fe29a62359ab8b62a141d3afc7abab8
1 /*
2 Copyright (c) 2003-2004, Mark Borgerding
4 All rights reserved.
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 #ifdef MIN
16 #undef MIN
17 #endif
18 #define MIN(a,b) ((a)<(b) ? (a):(b))
19 #ifdef MAX
20 #undef MAX
21 #endif
22 #define MAX(a,b) ((a)>(b) ? (a):(b))
24 /* kiss_fft.h
25 defines kiss_fft_scalar as either short or a float type
26 and defines
27 typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
28 #include "kiss_fft.h"
29 #include "math_approx.h"
31 #define MAXFACTORS 32
32 /* e.g. an fft of length 128 has 4 factors
33 as far as kissfft is concerned
34 4*4*4*2
37 struct kiss_fft_state{
38 int nfft;
39 int inverse;
40 int factors[2*MAXFACTORS];
41 kiss_fft_cpx twiddles[1];
45 Explanation of macros dealing with complex math:
47 C_MUL(m,a,b) : m = a*b
48 C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
49 C_SUB( res, a,b) : res = a - b
50 C_SUBFROM( res , a) : res -= a
51 C_ADDTO( res , a) : res += a
52 * */
53 #ifdef FIXED_POINT
54 #include "arch.h"
55 # define FRACBITS 15
56 # define SAMPPROD spx_int32_t
57 #define SAMP_MAX 32767
59 #define SAMP_MIN -SAMP_MAX
61 #if defined(CHECK_OVERFLOW)
62 # define CHECK_OVERFLOW_OP(a,op,b) \
63 if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
64 fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
65 #endif
68 # define smul(a,b) ( (SAMPPROD)(a)*(b) )
69 # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
71 # define S_MUL(a,b) sround( smul(a,b) )
73 # define C_MUL(m,a,b) \
74 do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
75 (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
77 # define C_MUL4(m,a,b) \
78 do{ (m).r = PSHR32( smul((a).r,(b).r) - smul((a).i,(b).i),17 ); \
79 (m).i = PSHR32( smul((a).r,(b).i) + smul((a).i,(b).r),17 ); }while(0)
81 # define DIVSCALAR(x,k) \
82 (x) = sround( smul( x, SAMP_MAX/k ) )
84 # define C_FIXDIV(c,div) \
85 do { DIVSCALAR( (c).r , div); \
86 DIVSCALAR( (c).i , div); }while (0)
88 # define C_MULBYSCALAR( c, s ) \
89 do{ (c).r = sround( smul( (c).r , s ) ) ;\
90 (c).i = sround( smul( (c).i , s ) ) ; }while(0)
92 #else /* not FIXED_POINT*/
94 # define S_MUL(a,b) ( (a)*(b) )
95 #define C_MUL(m,a,b) \
96 do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
97 (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
99 #define C_MUL4(m,a,b) C_MUL(m,a,b)
101 # define C_FIXDIV(c,div) /* NOOP */
102 # define C_MULBYSCALAR( c, s ) \
103 do{ (c).r *= (s);\
104 (c).i *= (s); }while(0)
105 #endif
107 #ifndef CHECK_OVERFLOW_OP
108 # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
109 #endif
111 #define C_ADD( res, a,b)\
112 do { \
113 CHECK_OVERFLOW_OP((a).r,+,(b).r)\
114 CHECK_OVERFLOW_OP((a).i,+,(b).i)\
115 (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
116 }while(0)
117 #define C_SUB( res, a,b)\
118 do { \
119 CHECK_OVERFLOW_OP((a).r,-,(b).r)\
120 CHECK_OVERFLOW_OP((a).i,-,(b).i)\
121 (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
122 }while(0)
123 #define C_ADDTO( res , a)\
124 do { \
125 CHECK_OVERFLOW_OP((res).r,+,(a).r)\
126 CHECK_OVERFLOW_OP((res).i,+,(a).i)\
127 (res).r += (a).r; (res).i += (a).i;\
128 }while(0)
130 #define C_SUBFROM( res , a)\
131 do {\
132 CHECK_OVERFLOW_OP((res).r,-,(a).r)\
133 CHECK_OVERFLOW_OP((res).i,-,(a).i)\
134 (res).r -= (a).r; (res).i -= (a).i; \
135 }while(0)
138 #ifdef FIXED_POINT
139 # define KISS_FFT_COS(phase) floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
140 # define KISS_FFT_SIN(phase) floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))
141 # define HALF_OF(x) ((x)>>1)
142 #elif defined(USE_SIMD)
143 # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
144 # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
145 # define HALF_OF(x) ((x)*_mm_set1_ps(.5))
146 #else
147 # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
148 # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
149 # define HALF_OF(x) ((x)*.5)
150 #endif
152 #define kf_cexp(x,phase) \
153 do{ \
154 (x)->r = KISS_FFT_COS(phase);\
155 (x)->i = KISS_FFT_SIN(phase);\
156 }while(0)
157 #define kf_cexp2(x,phase) \
158 do{ \
159 (x)->r = spx_cos_norm((phase));\
160 (x)->i = spx_cos_norm((phase)-32768);\
161 }while(0)
164 /* a debugging function */
165 #define pcpx(c)\
166 fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )