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_ovflw(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
62           (m).i = ADD32_ovflw(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_ovflw(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
66           (m).i = SUB32_ovflw(S_MUL((a).i,(b).r) , S_MUL((a).r,(b).i)); }while(0)
67 
68 #   define C_MULBYSCALAR( c, s ) \
69       do{ (c).r =  S_MUL( (c).r , s ) ;\
70           (c).i =  S_MUL( (c).i , s ) ; }while(0)
71 
72 #   define DIVSCALAR(x,k) \
73         (x) = S_MUL(  x, (TWID_MAX-((k)>>1))/(k)+1 )
74 
75 #   define C_FIXDIV(c,div) \
76         do {    DIVSCALAR( (c).r , div);  \
77                 DIVSCALAR( (c).i  , div); }while (0)
78 
79 #define  C_ADD( res, a,b)\
80     do {(res).r=ADD32_ovflw((a).r,(b).r);  (res).i=ADD32_ovflw((a).i,(b).i); \
81     }while(0)
82 #define  C_SUB( res, a,b)\
83     do {(res).r=SUB32_ovflw((a).r,(b).r);  (res).i=SUB32_ovflw((a).i,(b).i); \
84     }while(0)
85 #define C_ADDTO( res , a)\
86     do {(res).r = ADD32_ovflw((res).r, (a).r);  (res).i = ADD32_ovflw((res).i,(a).i);\
87     }while(0)
88 
89 #define C_SUBFROM( res , a)\
90     do {(res).r = ADD32_ovflw((res).r,(a).r);  (res).i = SUB32_ovflw((res).i,(a).i); \
91     }while(0)
92 
93 #if defined(OPUS_ARM_INLINE_ASM)
94 #include "arm/kiss_fft_armv4.h"
95 #endif
96 
97 #if defined(OPUS_ARM_INLINE_EDSP)
98 #include "arm/kiss_fft_armv5e.h"
99 #endif
100 #if defined(MIPSr1_ASM)
101 #include "mips/kiss_fft_mipsr1.h"
102 #endif
103 
104 #else  /* not FIXED_POINT*/
105 
106 #   define S_MUL(a,b) ( (a)*(b) )
107 #define C_MUL(m,a,b) \
108     do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
109         (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
110 #define C_MULC(m,a,b) \
111     do{ (m).r = (a).r*(b).r + (a).i*(b).i;\
112         (m).i = (a).i*(b).r - (a).r*(b).i; }while(0)
113 
114 #define C_MUL4(m,a,b) C_MUL(m,a,b)
115 
116 #   define C_FIXDIV(c,div) /* NOOP */
117 #   define C_MULBYSCALAR( c, s ) \
118     do{ (c).r *= (s);\
119         (c).i *= (s); }while(0)
120 #endif
121 
122 #ifndef CHECK_OVERFLOW_OP
123 #  define CHECK_OVERFLOW_OP(a,op,b) /* noop */
124 #endif
125 
126 #ifndef C_ADD
127 #define  C_ADD( res, a,b)\
128     do { \
129             CHECK_OVERFLOW_OP((a).r,+,(b).r)\
130             CHECK_OVERFLOW_OP((a).i,+,(b).i)\
131             (res).r=(a).r+(b).r;  (res).i=(a).i+(b).i; \
132     }while(0)
133 #define  C_SUB( res, a,b)\
134     do { \
135             CHECK_OVERFLOW_OP((a).r,-,(b).r)\
136             CHECK_OVERFLOW_OP((a).i,-,(b).i)\
137             (res).r=(a).r-(b).r;  (res).i=(a).i-(b).i; \
138     }while(0)
139 #define C_ADDTO( res , a)\
140     do { \
141             CHECK_OVERFLOW_OP((res).r,+,(a).r)\
142             CHECK_OVERFLOW_OP((res).i,+,(a).i)\
143             (res).r += (a).r;  (res).i += (a).i;\
144     }while(0)
145 
146 #define C_SUBFROM( res , a)\
147     do {\
148             CHECK_OVERFLOW_OP((res).r,-,(a).r)\
149             CHECK_OVERFLOW_OP((res).i,-,(a).i)\
150             (res).r -= (a).r;  (res).i -= (a).i; \
151     }while(0)
152 #endif /* C_ADD defined */
153 
154 #ifdef FIXED_POINT
155 /*#  define KISS_FFT_COS(phase)  TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
156 #  define KISS_FFT_SIN(phase)  TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))*/
157 #  define KISS_FFT_COS(phase)  floor(.5+TWID_MAX*cos (phase))
158 #  define KISS_FFT_SIN(phase)  floor(.5+TWID_MAX*sin (phase))
159 #  define HALF_OF(x) ((x)>>1)
160 #elif defined(USE_SIMD)
161 #  define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
162 #  define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
163 #  define HALF_OF(x) ((x)*_mm_set1_ps(.5f))
164 #else
165 #  define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
166 #  define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
167 #  define HALF_OF(x) ((x)*.5f)
168 #endif
169 
170 #define  kf_cexp(x,phase) \
171         do{ \
172                 (x)->r = KISS_FFT_COS(phase);\
173                 (x)->i = KISS_FFT_SIN(phase);\
174         }while(0)
175 
176 #define  kf_cexp2(x,phase) \
177    do{ \
178       (x)->r = TRIG_UPSCALE*celt_cos_norm((phase));\
179       (x)->i = TRIG_UPSCALE*celt_cos_norm((phase)-32768);\
180 }while(0)
181 
182 #endif /* KISS_FFT_GUTS_H */
183