1 /*Copyright (c) 2003-2004, Mark Borgerding
2 
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions are met:
7 
8     * Redistributions of source code must retain the above copyright notice,
9        this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright notice,
11        this list of conditions and the following disclaimer in the
12        documentation and/or other materials provided with the distribution.
13 
14   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   POSSIBILITY OF SUCH DAMAGE.*/
25 
26 #ifndef KISS_FFT_GUTS_H
27 #define KISS_FFT_GUTS_H
28 
29 #define MIN(a,b) ((a)<(b) ? (a):(b))
30 #define MAX(a,b) ((a)>(b) ? (a):(b))
31 
32 /* kiss_fft.h
33    defines kiss_fft_scalar as either short or a float type
34    and defines
35    typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
36 #include "kiss_fft.h"
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 #include "arch.h"
49 
50 
51 #define SAMP_MAX 2147483647
52 #define TWID_MAX 32767
53 #define TRIG_UPSCALE 1
54 
55 #define SAMP_MIN -SAMP_MAX
56 
57 
58 #   define S_MUL(a,b) MULT16_32_Q15(b, a)
59 
60 #   define C_MUL(m,a,b) \
61       do{ (m).r = SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
62           (m).i = ADD32(S_MUL((a).r,(b).i) , S_MUL((a).i,(b).r)); }while(0)
63 
64 #   define C_MULC(m,a,b) \
65       do{ (m).r = ADD32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
66           (m).i = SUB32(S_MUL((a).i,(b).r) , S_MUL((a).r,(b).i)); }while(0)
67 
68 #   define C_MUL4(m,a,b) \
69       do{ (m).r = SHR32(SUB32(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)),2); \
70           (m).i = SHR32(ADD32(S_MUL((a).r,(b).i) , S_MUL((a).i,(b).r)),2); }while(0)
71 
72 #   define C_MULBYSCALAR( c, s ) \
73       do{ (c).r =  S_MUL( (c).r , s ) ;\
74           (c).i =  S_MUL( (c).i , s ) ; }while(0)
75 
76 #   define DIVSCALAR(x,k) \
77         (x) = S_MUL(  x, (TWID_MAX-((k)>>1))/(k)+1 )
78 
79 #   define C_FIXDIV(c,div) \
80         do {    DIVSCALAR( (c).r , div);  \
81                 DIVSCALAR( (c).i  , div); }while (0)
82 
83 #define  C_ADD( res, a,b)\
84     do {(res).r=ADD32((a).r,(b).r);  (res).i=ADD32((a).i,(b).i); \
85     }while(0)
86 #define  C_SUB( res, a,b)\
87     do {(res).r=SUB32((a).r,(b).r);  (res).i=SUB32((a).i,(b).i); \
88     }while(0)
89 #define C_ADDTO( res , a)\
90     do {(res).r = ADD32((res).r, (a).r);  (res).i = ADD32((res).i,(a).i);\
91     }while(0)
92 
93 #define C_SUBFROM( res , a)\
94     do {(res).r = ADD32((res).r,(a).r);  (res).i = SUB32((res).i,(a).i); \
95     }while(0)
96 
97 #if defined(OPUS_ARM_INLINE_ASM)
98 #include "arm/kiss_fft_armv4.h"
99 #endif
100 
101 #if defined(OPUS_ARM_INLINE_EDSP)
102 #include "arm/kiss_fft_armv5e.h"
103 #endif
104 
105 #else  /* not FIXED_POINT*/
106 
107 #   define S_MUL(a,b) ( (a)*(b) )
108 #define C_MUL(m,a,b) \
109     do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
110         (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
111 #define C_MULC(m,a,b) \
112     do{ (m).r = (a).r*(b).r + (a).i*(b).i;\
113         (m).i = (a).i*(b).r - (a).r*(b).i; }while(0)
114 
115 #define C_MUL4(m,a,b) C_MUL(m,a,b)
116 
117 #   define C_FIXDIV(c,div) /* NOOP */
118 #   define C_MULBYSCALAR( c, s ) \
119     do{ (c).r *= (s);\
120         (c).i *= (s); }while(0)
121 #endif
122 
123 #ifndef CHECK_OVERFLOW_OP
124 #  define CHECK_OVERFLOW_OP(a,op,b) /* noop */
125 #endif
126 
127 #ifndef C_ADD
128 #define  C_ADD( res, a,b)\
129     do { \
130             CHECK_OVERFLOW_OP((a).r,+,(b).r)\
131             CHECK_OVERFLOW_OP((a).i,+,(b).i)\
132             (res).r=(a).r+(b).r;  (res).i=(a).i+(b).i; \
133     }while(0)
134 #define  C_SUB( res, a,b)\
135     do { \
136             CHECK_OVERFLOW_OP((a).r,-,(b).r)\
137             CHECK_OVERFLOW_OP((a).i,-,(b).i)\
138             (res).r=(a).r-(b).r;  (res).i=(a).i-(b).i; \
139     }while(0)
140 #define C_ADDTO( res , a)\
141     do { \
142             CHECK_OVERFLOW_OP((res).r,+,(a).r)\
143             CHECK_OVERFLOW_OP((res).i,+,(a).i)\
144             (res).r += (a).r;  (res).i += (a).i;\
145     }while(0)
146 
147 #define C_SUBFROM( res , a)\
148     do {\
149             CHECK_OVERFLOW_OP((res).r,-,(a).r)\
150             CHECK_OVERFLOW_OP((res).i,-,(a).i)\
151             (res).r -= (a).r;  (res).i -= (a).i; \
152     }while(0)
153 #endif /* C_ADD defined */
154 
155 #ifdef FIXED_POINT
156 /*#  define KISS_FFT_COS(phase)  TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
157 #  define KISS_FFT_SIN(phase)  TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))*/
158 #  define KISS_FFT_COS(phase)  floor(.5+TWID_MAX*cos (phase))
159 #  define KISS_FFT_SIN(phase)  floor(.5+TWID_MAX*sin (phase))
160 #  define HALF_OF(x) ((x)>>1)
161 #elif defined(USE_SIMD)
162 #  define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
163 #  define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
164 #  define HALF_OF(x) ((x)*_mm_set1_ps(.5f))
165 #else
166 #  define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
167 #  define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
168 #  define HALF_OF(x) ((x)*.5f)
169 #endif
170 
171 #define  kf_cexp(x,phase) \
172         do{ \
173                 (x)->r = KISS_FFT_COS(phase);\
174                 (x)->i = KISS_FFT_SIN(phase);\
175         }while(0)
176 
177 #define  kf_cexp2(x,phase) \
178    do{ \
179       (x)->r = TRIG_UPSCALE*celt_cos_norm((phase));\
180       (x)->i = TRIG_UPSCALE*celt_cos_norm((phase)-32768);\
181 }while(0)
182 
183 #endif /* KISS_FFT_GUTS_H */
184