OLD | NEW |
1 /* Copyright (c) 2002-2008 Jean-Marc Valin | 1 /* Copyright (c) 2002-2008 Jean-Marc Valin |
2 Copyright (c) 2007-2008 CSIRO | 2 Copyright (c) 2007-2008 CSIRO |
3 Copyright (c) 2007-2009 Xiph.Org Foundation | 3 Copyright (c) 2007-2009 Xiph.Org Foundation |
4 Written by Jean-Marc Valin */ | 4 Written by Jean-Marc Valin */ |
5 /** | 5 /** |
6 @file mathops.h | 6 @file mathops.h |
7 @brief Various math functions | 7 @brief Various math functions |
8 */ | 8 */ |
9 /* | 9 /* |
10 Redistribution and use in source and binary forms, with or without | 10 Redistribution and use in source and binary forms, with or without |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 #include "arch.h" | 37 #include "arch.h" |
38 #include "entcode.h" | 38 #include "entcode.h" |
39 #include "os_support.h" | 39 #include "os_support.h" |
40 | 40 |
41 /* Multiplies two 16-bit fractional values. Bit-exactness of this macro is impor
tant */ | 41 /* Multiplies two 16-bit fractional values. Bit-exactness of this macro is impor
tant */ |
42 #define FRAC_MUL16(a,b) ((16384+((opus_int32)(opus_int16)(a)*(opus_int16)(b)))>>
15) | 42 #define FRAC_MUL16(a,b) ((16384+((opus_int32)(opus_int16)(a)*(opus_int16)(b)))>>
15) |
43 | 43 |
44 unsigned isqrt32(opus_uint32 _val); | 44 unsigned isqrt32(opus_uint32 _val); |
45 | 45 |
| 46 #ifndef OVERRIDE_CELT_MAXABS16 |
| 47 static inline opus_val32 celt_maxabs16(const opus_val16 *x, int len) |
| 48 { |
| 49 int i; |
| 50 opus_val16 maxval = 0; |
| 51 opus_val16 minval = 0; |
| 52 for (i=0;i<len;i++) |
| 53 { |
| 54 maxval = MAX16(maxval, x[i]); |
| 55 minval = MIN16(minval, x[i]); |
| 56 } |
| 57 return MAX32(EXTEND32(maxval),-EXTEND32(minval)); |
| 58 } |
| 59 #endif |
| 60 |
| 61 #ifndef OVERRIDE_CELT_MAXABS32 |
| 62 #ifdef FIXED_POINT |
| 63 static inline opus_val32 celt_maxabs32(const opus_val32 *x, int len) |
| 64 { |
| 65 int i; |
| 66 opus_val32 maxval = 0; |
| 67 opus_val32 minval = 0; |
| 68 for (i=0;i<len;i++) |
| 69 { |
| 70 maxval = MAX32(maxval, x[i]); |
| 71 minval = MIN32(minval, x[i]); |
| 72 } |
| 73 return MAX32(maxval, -minval); |
| 74 } |
| 75 #else |
| 76 #define celt_maxabs32(x,len) celt_maxabs16(x,len) |
| 77 #endif |
| 78 #endif |
| 79 |
| 80 |
46 #ifndef FIXED_POINT | 81 #ifndef FIXED_POINT |
47 | 82 |
48 #define PI 3.141592653f | 83 #define PI 3.141592653f |
49 #define celt_sqrt(x) ((float)sqrt(x)) | 84 #define celt_sqrt(x) ((float)sqrt(x)) |
50 #define celt_rsqrt(x) (1.f/celt_sqrt(x)) | 85 #define celt_rsqrt(x) (1.f/celt_sqrt(x)) |
51 #define celt_rsqrt_norm(x) (celt_rsqrt(x)) | 86 #define celt_rsqrt_norm(x) (celt_rsqrt(x)) |
52 #define celt_cos_norm(x) ((float)cos((.5f*PI)*(x))) | 87 #define celt_cos_norm(x) ((float)cos((.5f*PI)*(x))) |
53 #define celt_rcp(x) (1.f/(x)) | 88 #define celt_rcp(x) (1.f/(x)) |
54 #define celt_div(a,b) ((a)/(b)) | 89 #define celt_div(a,b) ((a)/(b)) |
55 #define frac_div32(a,b) ((float)(a)/(b)) | 90 #define frac_div32(a,b) ((float)(a)/(b)) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 145 |
111 #ifndef OVERRIDE_CELT_ILOG2 | 146 #ifndef OVERRIDE_CELT_ILOG2 |
112 /** Integer log in base2. Undefined for zero and negative numbers */ | 147 /** Integer log in base2. Undefined for zero and negative numbers */ |
113 static inline opus_int16 celt_ilog2(opus_int32 x) | 148 static inline opus_int16 celt_ilog2(opus_int32 x) |
114 { | 149 { |
115 celt_assert2(x>0, "celt_ilog2() only defined for strictly positive numbers"); | 150 celt_assert2(x>0, "celt_ilog2() only defined for strictly positive numbers"); |
116 return EC_ILOG(x)-1; | 151 return EC_ILOG(x)-1; |
117 } | 152 } |
118 #endif | 153 #endif |
119 | 154 |
120 #ifndef OVERRIDE_CELT_MAXABS16 | |
121 static inline opus_val16 celt_maxabs16(opus_val16 *x, int len) | |
122 { | |
123 int i; | |
124 opus_val16 maxval = 0; | |
125 for (i=0;i<len;i++) | |
126 maxval = MAX16(maxval, ABS16(x[i])); | |
127 return maxval; | |
128 } | |
129 #endif | |
130 | |
131 #ifndef OVERRIDE_CELT_MAXABS32 | |
132 static inline opus_val32 celt_maxabs32(opus_val32 *x, int len) | |
133 { | |
134 int i; | |
135 opus_val32 maxval = 0; | |
136 for (i=0;i<len;i++) | |
137 maxval = MAX32(maxval, ABS32(x[i])); | |
138 return maxval; | |
139 } | |
140 #endif | |
141 | 155 |
142 /** Integer log in base2. Defined for zero, but not for negative numbers */ | 156 /** Integer log in base2. Defined for zero, but not for negative numbers */ |
143 static inline opus_int16 celt_zlog2(opus_val32 x) | 157 static inline opus_int16 celt_zlog2(opus_val32 x) |
144 { | 158 { |
145 return x <= 0 ? 0 : celt_ilog2(x); | 159 return x <= 0 ? 0 : celt_ilog2(x); |
146 } | 160 } |
147 | 161 |
148 opus_val16 celt_rsqrt_norm(opus_val32 x); | 162 opus_val16 celt_rsqrt_norm(opus_val32 x); |
149 | 163 |
150 opus_val32 celt_sqrt(opus_val32 x); | 164 opus_val32 celt_sqrt(opus_val32 x); |
151 | 165 |
152 opus_val16 celt_cos_norm(opus_val32 x); | 166 opus_val16 celt_cos_norm(opus_val32 x); |
153 | 167 |
| 168 /** Base-2 logarithm approximation (log2(x)). (Q14 input, Q10 output) */ |
154 static inline opus_val16 celt_log2(opus_val32 x) | 169 static inline opus_val16 celt_log2(opus_val32 x) |
155 { | 170 { |
156 int i; | 171 int i; |
157 opus_val16 n, frac; | 172 opus_val16 n, frac; |
158 /* -0.41509302963303146, 0.9609890551383969, -0.31836011537636605, | 173 /* -0.41509302963303146, 0.9609890551383969, -0.31836011537636605, |
159 0.15530808010959576, -0.08556153059057618 */ | 174 0.15530808010959576, -0.08556153059057618 */ |
160 static const opus_val16 C[5] = {-6801+(1<<(13-DB_SHIFT)), 15746, -5217, 2545,
-1401}; | 175 static const opus_val16 C[5] = {-6801+(1<<(13-DB_SHIFT)), 15746, -5217, 2545,
-1401}; |
161 if (x==0) | 176 if (x==0) |
162 return -32767; | 177 return -32767; |
163 i = celt_ilog2(x); | 178 i = celt_ilog2(x); |
164 n = VSHR32(x,i-15)-32768-16384; | 179 n = VSHR32(x,i-15)-32768-16384; |
165 frac = ADD16(C[0], MULT16_16_Q15(n, ADD16(C[1], MULT16_16_Q15(n, ADD16(C[2],
MULT16_16_Q15(n, ADD16(C[3], MULT16_16_Q15(n, C[4])))))))); | 180 frac = ADD16(C[0], MULT16_16_Q15(n, ADD16(C[1], MULT16_16_Q15(n, ADD16(C[2],
MULT16_16_Q15(n, ADD16(C[3], MULT16_16_Q15(n, C[4])))))))); |
166 return SHL16(i-13,DB_SHIFT)+SHR16(frac,14-DB_SHIFT); | 181 return SHL16(i-13,DB_SHIFT)+SHR16(frac,14-DB_SHIFT); |
167 } | 182 } |
168 | 183 |
169 /* | 184 /* |
170 K0 = 1 | 185 K0 = 1 |
171 K1 = log(2) | 186 K1 = log(2) |
172 K2 = 3-4*log(2) | 187 K2 = 3-4*log(2) |
173 K3 = 3*log(2) - 2 | 188 K3 = 3*log(2) - 2 |
174 */ | 189 */ |
175 #define D0 16383 | 190 #define D0 16383 |
176 #define D1 22804 | 191 #define D1 22804 |
177 #define D2 14819 | 192 #define D2 14819 |
178 #define D3 10204 | 193 #define D3 10204 |
| 194 |
| 195 static inline opus_val32 celt_exp2_frac(opus_val16 x) |
| 196 { |
| 197 opus_val16 frac; |
| 198 frac = SHL16(x, 4); |
| 199 return ADD16(D0, MULT16_16_Q15(frac, ADD16(D1, MULT16_16_Q15(frac, ADD16(D2 ,
MULT16_16_Q15(D3,frac)))))); |
| 200 } |
179 /** Base-2 exponential approximation (2^x). (Q10 input, Q16 output) */ | 201 /** Base-2 exponential approximation (2^x). (Q10 input, Q16 output) */ |
180 static inline opus_val32 celt_exp2(opus_val16 x) | 202 static inline opus_val32 celt_exp2(opus_val16 x) |
181 { | 203 { |
182 int integer; | 204 int integer; |
183 opus_val16 frac; | 205 opus_val16 frac; |
184 integer = SHR16(x,10); | 206 integer = SHR16(x,10); |
185 if (integer>14) | 207 if (integer>14) |
186 return 0x7f000000; | 208 return 0x7f000000; |
187 else if (integer < -15) | 209 else if (integer < -15) |
188 return 0; | 210 return 0; |
189 frac = SHL16(x-SHL16(integer,10),4); | 211 frac = celt_exp2_frac(x-SHL16(integer,10)); |
190 frac = ADD16(D0, MULT16_16_Q15(frac, ADD16(D1, MULT16_16_Q15(frac, ADD16(D2 ,
MULT16_16_Q15(D3,frac)))))); | |
191 return VSHR32(EXTEND32(frac), -integer-2); | 212 return VSHR32(EXTEND32(frac), -integer-2); |
192 } | 213 } |
193 | 214 |
194 opus_val32 celt_rcp(opus_val32 x); | 215 opus_val32 celt_rcp(opus_val32 x); |
195 | 216 |
196 #define celt_div(a,b) MULT32_32_Q31((opus_val32)(a),celt_rcp(b)) | 217 #define celt_div(a,b) MULT32_32_Q31((opus_val32)(a),celt_rcp(b)) |
197 | 218 |
198 opus_val32 frac_div32(opus_val32 a, opus_val32 b); | 219 opus_val32 frac_div32(opus_val32 a, opus_val32 b); |
199 | 220 |
200 #define M1 32767 | 221 #define M1 32767 |
(...skipping 27 matching lines...) Expand all Loading... |
228 opus_val32 arg; | 249 opus_val32 arg; |
229 arg = celt_div(SHL32(EXTEND32(x),15),y); | 250 arg = celt_div(SHL32(EXTEND32(x),15),y); |
230 if (arg >= 32767) | 251 if (arg >= 32767) |
231 arg = 32767; | 252 arg = 32767; |
232 return 25736-SHR16(celt_atan01(EXTRACT16(arg)),1); | 253 return 25736-SHR16(celt_atan01(EXTRACT16(arg)),1); |
233 } | 254 } |
234 } | 255 } |
235 | 256 |
236 #endif /* FIXED_POINT */ | 257 #endif /* FIXED_POINT */ |
237 #endif /* MATHOPS_H */ | 258 #endif /* MATHOPS_H */ |
OLD | NEW |