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