Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* Copyright (C) 2003-2008 Jean-Marc Valin |
michael@0 | 2 | Copyright (C) 2007-2012 Xiph.Org Foundation */ |
michael@0 | 3 | /** |
michael@0 | 4 | @file fixed_debug.h |
michael@0 | 5 | @brief Fixed-point operations with debugging |
michael@0 | 6 | */ |
michael@0 | 7 | /* |
michael@0 | 8 | Redistribution and use in source and binary forms, with or without |
michael@0 | 9 | modification, are permitted provided that the following conditions |
michael@0 | 10 | are met: |
michael@0 | 11 | |
michael@0 | 12 | - Redistributions of source code must retain the above copyright |
michael@0 | 13 | notice, this list of conditions and the following disclaimer. |
michael@0 | 14 | |
michael@0 | 15 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 16 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 17 | documentation and/or other materials provided with the distribution. |
michael@0 | 18 | |
michael@0 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 20 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
michael@0 | 23 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 24 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 25 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 27 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 28 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 30 | */ |
michael@0 | 31 | |
michael@0 | 32 | #ifndef FIXED_DEBUG_H |
michael@0 | 33 | #define FIXED_DEBUG_H |
michael@0 | 34 | |
michael@0 | 35 | #include <stdio.h> |
michael@0 | 36 | #include "opus_defines.h" |
michael@0 | 37 | |
michael@0 | 38 | #ifdef CELT_C |
michael@0 | 39 | OPUS_EXPORT opus_int64 celt_mips=0; |
michael@0 | 40 | #else |
michael@0 | 41 | extern opus_int64 celt_mips; |
michael@0 | 42 | #endif |
michael@0 | 43 | |
michael@0 | 44 | #define MULT16_16SU(a,b) ((opus_val32)(opus_val16)(a)*(opus_val32)(opus_uint16)(b)) |
michael@0 | 45 | #define MULT32_32_Q31(a,b) ADD32(ADD32(SHL32(MULT16_16(SHR32((a),16),SHR((b),16)),1), SHR32(MULT16_16SU(SHR32((a),16),((b)&0x0000ffff)),15)), SHR32(MULT16_16SU(SHR32((b),16),((a)&0x0000ffff)),15)) |
michael@0 | 46 | |
michael@0 | 47 | /** 16x32 multiplication, followed by a 16-bit shift right. Results fits in 32 bits */ |
michael@0 | 48 | #define MULT16_32_Q16(a,b) ADD32(MULT16_16((a),SHR32((b),16)), SHR32(MULT16_16SU((a),((b)&0x0000ffff)),16)) |
michael@0 | 49 | |
michael@0 | 50 | #define MULT16_32_P16(a,b) MULT16_32_PX(a,b,16) |
michael@0 | 51 | |
michael@0 | 52 | #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits)))) |
michael@0 | 53 | #define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits)))) |
michael@0 | 54 | |
michael@0 | 55 | #define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768) |
michael@0 | 56 | #define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL) |
michael@0 | 57 | #define VERIFY_UINT(x) ((x)<=(2147483647LLU<<1)) |
michael@0 | 58 | |
michael@0 | 59 | #define SHR(a,b) SHR32(a,b) |
michael@0 | 60 | #define PSHR(a,b) PSHR32(a,b) |
michael@0 | 61 | |
michael@0 | 62 | static OPUS_INLINE short NEG16(int x) |
michael@0 | 63 | { |
michael@0 | 64 | int res; |
michael@0 | 65 | if (!VERIFY_SHORT(x)) |
michael@0 | 66 | { |
michael@0 | 67 | fprintf (stderr, "NEG16: input is not short: %d\n", (int)x); |
michael@0 | 68 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 69 | celt_assert(0); |
michael@0 | 70 | #endif |
michael@0 | 71 | } |
michael@0 | 72 | res = -x; |
michael@0 | 73 | if (!VERIFY_SHORT(res)) |
michael@0 | 74 | { |
michael@0 | 75 | fprintf (stderr, "NEG16: output is not short: %d\n", (int)res); |
michael@0 | 76 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 77 | celt_assert(0); |
michael@0 | 78 | #endif |
michael@0 | 79 | } |
michael@0 | 80 | celt_mips++; |
michael@0 | 81 | return res; |
michael@0 | 82 | } |
michael@0 | 83 | static OPUS_INLINE int NEG32(opus_int64 x) |
michael@0 | 84 | { |
michael@0 | 85 | opus_int64 res; |
michael@0 | 86 | if (!VERIFY_INT(x)) |
michael@0 | 87 | { |
michael@0 | 88 | fprintf (stderr, "NEG16: input is not int: %d\n", (int)x); |
michael@0 | 89 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 90 | celt_assert(0); |
michael@0 | 91 | #endif |
michael@0 | 92 | } |
michael@0 | 93 | res = -x; |
michael@0 | 94 | if (!VERIFY_INT(res)) |
michael@0 | 95 | { |
michael@0 | 96 | fprintf (stderr, "NEG16: output is not int: %d\n", (int)res); |
michael@0 | 97 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 98 | celt_assert(0); |
michael@0 | 99 | #endif |
michael@0 | 100 | } |
michael@0 | 101 | celt_mips+=2; |
michael@0 | 102 | return res; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | #define EXTRACT16(x) EXTRACT16_(x, __FILE__, __LINE__) |
michael@0 | 106 | static OPUS_INLINE short EXTRACT16_(int x, char *file, int line) |
michael@0 | 107 | { |
michael@0 | 108 | int res; |
michael@0 | 109 | if (!VERIFY_SHORT(x)) |
michael@0 | 110 | { |
michael@0 | 111 | fprintf (stderr, "EXTRACT16: input is not short: %d in %s: line %d\n", x, file, line); |
michael@0 | 112 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 113 | celt_assert(0); |
michael@0 | 114 | #endif |
michael@0 | 115 | } |
michael@0 | 116 | res = x; |
michael@0 | 117 | celt_mips++; |
michael@0 | 118 | return res; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | #define EXTEND32(x) EXTEND32_(x, __FILE__, __LINE__) |
michael@0 | 122 | static OPUS_INLINE int EXTEND32_(int x, char *file, int line) |
michael@0 | 123 | { |
michael@0 | 124 | int res; |
michael@0 | 125 | if (!VERIFY_SHORT(x)) |
michael@0 | 126 | { |
michael@0 | 127 | fprintf (stderr, "EXTEND32: input is not short: %d in %s: line %d\n", x, file, line); |
michael@0 | 128 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 129 | celt_assert(0); |
michael@0 | 130 | #endif |
michael@0 | 131 | } |
michael@0 | 132 | res = x; |
michael@0 | 133 | celt_mips++; |
michael@0 | 134 | return res; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | #define SHR16(a, shift) SHR16_(a, shift, __FILE__, __LINE__) |
michael@0 | 138 | static OPUS_INLINE short SHR16_(int a, int shift, char *file, int line) |
michael@0 | 139 | { |
michael@0 | 140 | int res; |
michael@0 | 141 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift)) |
michael@0 | 142 | { |
michael@0 | 143 | fprintf (stderr, "SHR16: inputs are not short: %d >> %d in %s: line %d\n", a, shift, file, line); |
michael@0 | 144 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 145 | celt_assert(0); |
michael@0 | 146 | #endif |
michael@0 | 147 | } |
michael@0 | 148 | res = a>>shift; |
michael@0 | 149 | if (!VERIFY_SHORT(res)) |
michael@0 | 150 | { |
michael@0 | 151 | fprintf (stderr, "SHR16: output is not short: %d in %s: line %d\n", res, file, line); |
michael@0 | 152 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 153 | celt_assert(0); |
michael@0 | 154 | #endif |
michael@0 | 155 | } |
michael@0 | 156 | celt_mips++; |
michael@0 | 157 | return res; |
michael@0 | 158 | } |
michael@0 | 159 | #define SHL16(a, shift) SHL16_(a, shift, __FILE__, __LINE__) |
michael@0 | 160 | static OPUS_INLINE short SHL16_(int a, int shift, char *file, int line) |
michael@0 | 161 | { |
michael@0 | 162 | int res; |
michael@0 | 163 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift)) |
michael@0 | 164 | { |
michael@0 | 165 | fprintf (stderr, "SHL16: inputs are not short: %d %d in %s: line %d\n", a, shift, file, line); |
michael@0 | 166 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 167 | celt_assert(0); |
michael@0 | 168 | #endif |
michael@0 | 169 | } |
michael@0 | 170 | res = a<<shift; |
michael@0 | 171 | if (!VERIFY_SHORT(res)) |
michael@0 | 172 | { |
michael@0 | 173 | fprintf (stderr, "SHL16: output is not short: %d in %s: line %d\n", res, file, line); |
michael@0 | 174 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 175 | celt_assert(0); |
michael@0 | 176 | #endif |
michael@0 | 177 | } |
michael@0 | 178 | celt_mips++; |
michael@0 | 179 | return res; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | static OPUS_INLINE int SHR32(opus_int64 a, int shift) |
michael@0 | 183 | { |
michael@0 | 184 | opus_int64 res; |
michael@0 | 185 | if (!VERIFY_INT(a) || !VERIFY_SHORT(shift)) |
michael@0 | 186 | { |
michael@0 | 187 | fprintf (stderr, "SHR32: inputs are not int: %d %d\n", (int)a, shift); |
michael@0 | 188 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 189 | celt_assert(0); |
michael@0 | 190 | #endif |
michael@0 | 191 | } |
michael@0 | 192 | res = a>>shift; |
michael@0 | 193 | if (!VERIFY_INT(res)) |
michael@0 | 194 | { |
michael@0 | 195 | fprintf (stderr, "SHR32: output is not int: %d\n", (int)res); |
michael@0 | 196 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 197 | celt_assert(0); |
michael@0 | 198 | #endif |
michael@0 | 199 | } |
michael@0 | 200 | celt_mips+=2; |
michael@0 | 201 | return res; |
michael@0 | 202 | } |
michael@0 | 203 | #define SHL32(a, shift) SHL32_(a, shift, __FILE__, __LINE__) |
michael@0 | 204 | static OPUS_INLINE int SHL32_(opus_int64 a, int shift, char *file, int line) |
michael@0 | 205 | { |
michael@0 | 206 | opus_int64 res; |
michael@0 | 207 | if (!VERIFY_INT(a) || !VERIFY_SHORT(shift)) |
michael@0 | 208 | { |
michael@0 | 209 | fprintf (stderr, "SHL32: inputs are not int: %lld %d in %s: line %d\n", a, shift, file, line); |
michael@0 | 210 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 211 | celt_assert(0); |
michael@0 | 212 | #endif |
michael@0 | 213 | } |
michael@0 | 214 | res = a<<shift; |
michael@0 | 215 | if (!VERIFY_INT(res)) |
michael@0 | 216 | { |
michael@0 | 217 | fprintf (stderr, "SHL32: output is not int: %lld<<%d = %lld in %s: line %d\n", a, shift, res, file, line); |
michael@0 | 218 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 219 | celt_assert(0); |
michael@0 | 220 | #endif |
michael@0 | 221 | } |
michael@0 | 222 | celt_mips+=2; |
michael@0 | 223 | return res; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | #define PSHR32(a,shift) (celt_mips--,SHR32(ADD32((a),(((opus_val32)(1)<<((shift))>>1))),shift)) |
michael@0 | 227 | #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift))) |
michael@0 | 228 | |
michael@0 | 229 | #define ROUND16(x,a) (celt_mips--,EXTRACT16(PSHR32((x),(a)))) |
michael@0 | 230 | #define HALF16(x) (SHR16(x,1)) |
michael@0 | 231 | #define HALF32(x) (SHR32(x,1)) |
michael@0 | 232 | |
michael@0 | 233 | //#define SHR(a,shift) ((a) >> (shift)) |
michael@0 | 234 | //#define SHL(a,shift) ((a) << (shift)) |
michael@0 | 235 | |
michael@0 | 236 | #define ADD16(a, b) ADD16_(a, b, __FILE__, __LINE__) |
michael@0 | 237 | static OPUS_INLINE short ADD16_(int a, int b, char *file, int line) |
michael@0 | 238 | { |
michael@0 | 239 | int res; |
michael@0 | 240 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 241 | { |
michael@0 | 242 | fprintf (stderr, "ADD16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); |
michael@0 | 243 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 244 | celt_assert(0); |
michael@0 | 245 | #endif |
michael@0 | 246 | } |
michael@0 | 247 | res = a+b; |
michael@0 | 248 | if (!VERIFY_SHORT(res)) |
michael@0 | 249 | { |
michael@0 | 250 | fprintf (stderr, "ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line); |
michael@0 | 251 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 252 | celt_assert(0); |
michael@0 | 253 | #endif |
michael@0 | 254 | } |
michael@0 | 255 | celt_mips++; |
michael@0 | 256 | return res; |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | #define SUB16(a, b) SUB16_(a, b, __FILE__, __LINE__) |
michael@0 | 260 | static OPUS_INLINE short SUB16_(int a, int b, char *file, int line) |
michael@0 | 261 | { |
michael@0 | 262 | int res; |
michael@0 | 263 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 264 | { |
michael@0 | 265 | fprintf (stderr, "SUB16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); |
michael@0 | 266 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 267 | celt_assert(0); |
michael@0 | 268 | #endif |
michael@0 | 269 | } |
michael@0 | 270 | res = a-b; |
michael@0 | 271 | if (!VERIFY_SHORT(res)) |
michael@0 | 272 | { |
michael@0 | 273 | fprintf (stderr, "SUB16: output is not short: %d in %s: line %d\n", res, file, line); |
michael@0 | 274 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 275 | celt_assert(0); |
michael@0 | 276 | #endif |
michael@0 | 277 | } |
michael@0 | 278 | celt_mips++; |
michael@0 | 279 | return res; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | #define ADD32(a, b) ADD32_(a, b, __FILE__, __LINE__) |
michael@0 | 283 | static OPUS_INLINE int ADD32_(opus_int64 a, opus_int64 b, char *file, int line) |
michael@0 | 284 | { |
michael@0 | 285 | opus_int64 res; |
michael@0 | 286 | if (!VERIFY_INT(a) || !VERIFY_INT(b)) |
michael@0 | 287 | { |
michael@0 | 288 | fprintf (stderr, "ADD32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line); |
michael@0 | 289 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 290 | celt_assert(0); |
michael@0 | 291 | #endif |
michael@0 | 292 | } |
michael@0 | 293 | res = a+b; |
michael@0 | 294 | if (!VERIFY_INT(res)) |
michael@0 | 295 | { |
michael@0 | 296 | fprintf (stderr, "ADD32: output is not int: %d in %s: line %d\n", (int)res, file, line); |
michael@0 | 297 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 298 | celt_assert(0); |
michael@0 | 299 | #endif |
michael@0 | 300 | } |
michael@0 | 301 | celt_mips+=2; |
michael@0 | 302 | return res; |
michael@0 | 303 | } |
michael@0 | 304 | |
michael@0 | 305 | #define SUB32(a, b) SUB32_(a, b, __FILE__, __LINE__) |
michael@0 | 306 | static OPUS_INLINE int SUB32_(opus_int64 a, opus_int64 b, char *file, int line) |
michael@0 | 307 | { |
michael@0 | 308 | opus_int64 res; |
michael@0 | 309 | if (!VERIFY_INT(a) || !VERIFY_INT(b)) |
michael@0 | 310 | { |
michael@0 | 311 | fprintf (stderr, "SUB32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line); |
michael@0 | 312 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 313 | celt_assert(0); |
michael@0 | 314 | #endif |
michael@0 | 315 | } |
michael@0 | 316 | res = a-b; |
michael@0 | 317 | if (!VERIFY_INT(res)) |
michael@0 | 318 | { |
michael@0 | 319 | fprintf (stderr, "SUB32: output is not int: %d in %s: line %d\n", (int)res, file, line); |
michael@0 | 320 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 321 | celt_assert(0); |
michael@0 | 322 | #endif |
michael@0 | 323 | } |
michael@0 | 324 | celt_mips+=2; |
michael@0 | 325 | return res; |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | #undef UADD32 |
michael@0 | 329 | #define UADD32(a, b) UADD32_(a, b, __FILE__, __LINE__) |
michael@0 | 330 | static OPUS_INLINE unsigned int UADD32_(opus_uint64 a, opus_uint64 b, char *file, int line) |
michael@0 | 331 | { |
michael@0 | 332 | opus_uint64 res; |
michael@0 | 333 | if (!VERIFY_UINT(a) || !VERIFY_UINT(b)) |
michael@0 | 334 | { |
michael@0 | 335 | fprintf (stderr, "UADD32: inputs are not uint32: %llu %llu in %s: line %d\n", a, b, file, line); |
michael@0 | 336 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 337 | celt_assert(0); |
michael@0 | 338 | #endif |
michael@0 | 339 | } |
michael@0 | 340 | res = a+b; |
michael@0 | 341 | if (!VERIFY_UINT(res)) |
michael@0 | 342 | { |
michael@0 | 343 | fprintf (stderr, "UADD32: output is not uint32: %llu in %s: line %d\n", res, file, line); |
michael@0 | 344 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 345 | celt_assert(0); |
michael@0 | 346 | #endif |
michael@0 | 347 | } |
michael@0 | 348 | celt_mips+=2; |
michael@0 | 349 | return res; |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | #undef USUB32 |
michael@0 | 353 | #define USUB32(a, b) USUB32_(a, b, __FILE__, __LINE__) |
michael@0 | 354 | static OPUS_INLINE unsigned int USUB32_(opus_uint64 a, opus_uint64 b, char *file, int line) |
michael@0 | 355 | { |
michael@0 | 356 | opus_uint64 res; |
michael@0 | 357 | if (!VERIFY_UINT(a) || !VERIFY_UINT(b)) |
michael@0 | 358 | { |
michael@0 | 359 | fprintf (stderr, "USUB32: inputs are not uint32: %llu %llu in %s: line %d\n", a, b, file, line); |
michael@0 | 360 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 361 | celt_assert(0); |
michael@0 | 362 | #endif |
michael@0 | 363 | } |
michael@0 | 364 | if (a<b) |
michael@0 | 365 | { |
michael@0 | 366 | fprintf (stderr, "USUB32: inputs underflow: %llu < %llu in %s: line %d\n", a, b, file, line); |
michael@0 | 367 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 368 | celt_assert(0); |
michael@0 | 369 | #endif |
michael@0 | 370 | } |
michael@0 | 371 | res = a-b; |
michael@0 | 372 | if (!VERIFY_UINT(res)) |
michael@0 | 373 | { |
michael@0 | 374 | fprintf (stderr, "USUB32: output is not uint32: %llu - %llu = %llu in %s: line %d\n", a, b, res, file, line); |
michael@0 | 375 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 376 | celt_assert(0); |
michael@0 | 377 | #endif |
michael@0 | 378 | } |
michael@0 | 379 | celt_mips+=2; |
michael@0 | 380 | return res; |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | /* result fits in 16 bits */ |
michael@0 | 384 | static OPUS_INLINE short MULT16_16_16(int a, int b) |
michael@0 | 385 | { |
michael@0 | 386 | int res; |
michael@0 | 387 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 388 | { |
michael@0 | 389 | fprintf (stderr, "MULT16_16_16: inputs are not short: %d %d\n", a, b); |
michael@0 | 390 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 391 | celt_assert(0); |
michael@0 | 392 | #endif |
michael@0 | 393 | } |
michael@0 | 394 | res = a*b; |
michael@0 | 395 | if (!VERIFY_SHORT(res)) |
michael@0 | 396 | { |
michael@0 | 397 | fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res); |
michael@0 | 398 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 399 | celt_assert(0); |
michael@0 | 400 | #endif |
michael@0 | 401 | } |
michael@0 | 402 | celt_mips++; |
michael@0 | 403 | return res; |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | #define MULT16_16(a, b) MULT16_16_(a, b, __FILE__, __LINE__) |
michael@0 | 407 | static OPUS_INLINE int MULT16_16_(int a, int b, char *file, int line) |
michael@0 | 408 | { |
michael@0 | 409 | opus_int64 res; |
michael@0 | 410 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 411 | { |
michael@0 | 412 | fprintf (stderr, "MULT16_16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); |
michael@0 | 413 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 414 | celt_assert(0); |
michael@0 | 415 | #endif |
michael@0 | 416 | } |
michael@0 | 417 | res = ((opus_int64)a)*b; |
michael@0 | 418 | if (!VERIFY_INT(res)) |
michael@0 | 419 | { |
michael@0 | 420 | fprintf (stderr, "MULT16_16: output is not int: %d in %s: line %d\n", (int)res, file, line); |
michael@0 | 421 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 422 | celt_assert(0); |
michael@0 | 423 | #endif |
michael@0 | 424 | } |
michael@0 | 425 | celt_mips++; |
michael@0 | 426 | return res; |
michael@0 | 427 | } |
michael@0 | 428 | |
michael@0 | 429 | #define MAC16_16(c,a,b) (celt_mips-=2,ADD32((c),MULT16_16((a),(b)))) |
michael@0 | 430 | |
michael@0 | 431 | #define MULT16_32_QX(a, b, Q) MULT16_32_QX_(a, b, Q, __FILE__, __LINE__) |
michael@0 | 432 | static OPUS_INLINE int MULT16_32_QX_(int a, opus_int64 b, int Q, char *file, int line) |
michael@0 | 433 | { |
michael@0 | 434 | opus_int64 res; |
michael@0 | 435 | if (!VERIFY_SHORT(a) || !VERIFY_INT(b)) |
michael@0 | 436 | { |
michael@0 | 437 | fprintf (stderr, "MULT16_32_Q%d: inputs are not short+int: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line); |
michael@0 | 438 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 439 | celt_assert(0); |
michael@0 | 440 | #endif |
michael@0 | 441 | } |
michael@0 | 442 | if (ABS32(b)>=((opus_val32)(1)<<(15+Q))) |
michael@0 | 443 | { |
michael@0 | 444 | fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line); |
michael@0 | 445 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 446 | celt_assert(0); |
michael@0 | 447 | #endif |
michael@0 | 448 | } |
michael@0 | 449 | res = (((opus_int64)a)*(opus_int64)b) >> Q; |
michael@0 | 450 | if (!VERIFY_INT(res)) |
michael@0 | 451 | { |
michael@0 | 452 | fprintf (stderr, "MULT16_32_Q%d: output is not int: %d*%d=%d in %s: line %d\n", Q, (int)a, (int)b,(int)res, file, line); |
michael@0 | 453 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 454 | celt_assert(0); |
michael@0 | 455 | #endif |
michael@0 | 456 | } |
michael@0 | 457 | if (Q==15) |
michael@0 | 458 | celt_mips+=3; |
michael@0 | 459 | else |
michael@0 | 460 | celt_mips+=4; |
michael@0 | 461 | return res; |
michael@0 | 462 | } |
michael@0 | 463 | |
michael@0 | 464 | #define MULT16_32_PX(a, b, Q) MULT16_32_PX_(a, b, Q, __FILE__, __LINE__) |
michael@0 | 465 | static OPUS_INLINE int MULT16_32_PX_(int a, opus_int64 b, int Q, char *file, int line) |
michael@0 | 466 | { |
michael@0 | 467 | opus_int64 res; |
michael@0 | 468 | if (!VERIFY_SHORT(a) || !VERIFY_INT(b)) |
michael@0 | 469 | { |
michael@0 | 470 | fprintf (stderr, "MULT16_32_P%d: inputs are not short+int: %d %d in %s: line %d\n\n", Q, (int)a, (int)b, file, line); |
michael@0 | 471 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 472 | celt_assert(0); |
michael@0 | 473 | #endif |
michael@0 | 474 | } |
michael@0 | 475 | if (ABS32(b)>=((opus_int64)(1)<<(15+Q))) |
michael@0 | 476 | { |
michael@0 | 477 | fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n\n", Q, (int)a, (int)b,file, line); |
michael@0 | 478 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 479 | celt_assert(0); |
michael@0 | 480 | #endif |
michael@0 | 481 | } |
michael@0 | 482 | res = ((((opus_int64)a)*(opus_int64)b) + (((opus_val32)(1)<<Q)>>1))>> Q; |
michael@0 | 483 | if (!VERIFY_INT(res)) |
michael@0 | 484 | { |
michael@0 | 485 | fprintf (stderr, "MULT16_32_P%d: output is not int: %d*%d=%d in %s: line %d\n\n", Q, (int)a, (int)b,(int)res, file, line); |
michael@0 | 486 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 487 | celt_assert(0); |
michael@0 | 488 | #endif |
michael@0 | 489 | } |
michael@0 | 490 | if (Q==15) |
michael@0 | 491 | celt_mips+=4; |
michael@0 | 492 | else |
michael@0 | 493 | celt_mips+=5; |
michael@0 | 494 | return res; |
michael@0 | 495 | } |
michael@0 | 496 | |
michael@0 | 497 | #define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15) |
michael@0 | 498 | #define MAC16_32_Q15(c,a,b) (celt_mips-=2,ADD32((c),MULT16_32_Q15((a),(b)))) |
michael@0 | 499 | |
michael@0 | 500 | static OPUS_INLINE int SATURATE(int a, int b) |
michael@0 | 501 | { |
michael@0 | 502 | if (a>b) |
michael@0 | 503 | a=b; |
michael@0 | 504 | if (a<-b) |
michael@0 | 505 | a = -b; |
michael@0 | 506 | celt_mips+=3; |
michael@0 | 507 | return a; |
michael@0 | 508 | } |
michael@0 | 509 | |
michael@0 | 510 | static OPUS_INLINE opus_int16 SATURATE16(opus_int32 a) |
michael@0 | 511 | { |
michael@0 | 512 | celt_mips+=3; |
michael@0 | 513 | if (a>32767) |
michael@0 | 514 | return 32767; |
michael@0 | 515 | else if (a<-32768) |
michael@0 | 516 | return -32768; |
michael@0 | 517 | else return a; |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | static OPUS_INLINE int MULT16_16_Q11_32(int a, int b) |
michael@0 | 521 | { |
michael@0 | 522 | opus_int64 res; |
michael@0 | 523 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 524 | { |
michael@0 | 525 | fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b); |
michael@0 | 526 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 527 | celt_assert(0); |
michael@0 | 528 | #endif |
michael@0 | 529 | } |
michael@0 | 530 | res = ((opus_int64)a)*b; |
michael@0 | 531 | res >>= 11; |
michael@0 | 532 | if (!VERIFY_INT(res)) |
michael@0 | 533 | { |
michael@0 | 534 | fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res); |
michael@0 | 535 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 536 | celt_assert(0); |
michael@0 | 537 | #endif |
michael@0 | 538 | } |
michael@0 | 539 | celt_mips+=3; |
michael@0 | 540 | return res; |
michael@0 | 541 | } |
michael@0 | 542 | static OPUS_INLINE short MULT16_16_Q13(int a, int b) |
michael@0 | 543 | { |
michael@0 | 544 | opus_int64 res; |
michael@0 | 545 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 546 | { |
michael@0 | 547 | fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b); |
michael@0 | 548 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 549 | celt_assert(0); |
michael@0 | 550 | #endif |
michael@0 | 551 | } |
michael@0 | 552 | res = ((opus_int64)a)*b; |
michael@0 | 553 | res >>= 13; |
michael@0 | 554 | if (!VERIFY_SHORT(res)) |
michael@0 | 555 | { |
michael@0 | 556 | fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res); |
michael@0 | 557 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 558 | celt_assert(0); |
michael@0 | 559 | #endif |
michael@0 | 560 | } |
michael@0 | 561 | celt_mips+=3; |
michael@0 | 562 | return res; |
michael@0 | 563 | } |
michael@0 | 564 | static OPUS_INLINE short MULT16_16_Q14(int a, int b) |
michael@0 | 565 | { |
michael@0 | 566 | opus_int64 res; |
michael@0 | 567 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 568 | { |
michael@0 | 569 | fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b); |
michael@0 | 570 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 571 | celt_assert(0); |
michael@0 | 572 | #endif |
michael@0 | 573 | } |
michael@0 | 574 | res = ((opus_int64)a)*b; |
michael@0 | 575 | res >>= 14; |
michael@0 | 576 | if (!VERIFY_SHORT(res)) |
michael@0 | 577 | { |
michael@0 | 578 | fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res); |
michael@0 | 579 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 580 | celt_assert(0); |
michael@0 | 581 | #endif |
michael@0 | 582 | } |
michael@0 | 583 | celt_mips+=3; |
michael@0 | 584 | return res; |
michael@0 | 585 | } |
michael@0 | 586 | |
michael@0 | 587 | #define MULT16_16_Q15(a, b) MULT16_16_Q15_(a, b, __FILE__, __LINE__) |
michael@0 | 588 | static OPUS_INLINE short MULT16_16_Q15_(int a, int b, char *file, int line) |
michael@0 | 589 | { |
michael@0 | 590 | opus_int64 res; |
michael@0 | 591 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 592 | { |
michael@0 | 593 | fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); |
michael@0 | 594 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 595 | celt_assert(0); |
michael@0 | 596 | #endif |
michael@0 | 597 | } |
michael@0 | 598 | res = ((opus_int64)a)*b; |
michael@0 | 599 | res >>= 15; |
michael@0 | 600 | if (!VERIFY_SHORT(res)) |
michael@0 | 601 | { |
michael@0 | 602 | fprintf (stderr, "MULT16_16_Q15: output is not short: %d in %s: line %d\n", (int)res, file, line); |
michael@0 | 603 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 604 | celt_assert(0); |
michael@0 | 605 | #endif |
michael@0 | 606 | } |
michael@0 | 607 | celt_mips+=1; |
michael@0 | 608 | return res; |
michael@0 | 609 | } |
michael@0 | 610 | |
michael@0 | 611 | static OPUS_INLINE short MULT16_16_P13(int a, int b) |
michael@0 | 612 | { |
michael@0 | 613 | opus_int64 res; |
michael@0 | 614 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 615 | { |
michael@0 | 616 | fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b); |
michael@0 | 617 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 618 | celt_assert(0); |
michael@0 | 619 | #endif |
michael@0 | 620 | } |
michael@0 | 621 | res = ((opus_int64)a)*b; |
michael@0 | 622 | res += 4096; |
michael@0 | 623 | if (!VERIFY_INT(res)) |
michael@0 | 624 | { |
michael@0 | 625 | fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res); |
michael@0 | 626 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 627 | celt_assert(0); |
michael@0 | 628 | #endif |
michael@0 | 629 | } |
michael@0 | 630 | res >>= 13; |
michael@0 | 631 | if (!VERIFY_SHORT(res)) |
michael@0 | 632 | { |
michael@0 | 633 | fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res); |
michael@0 | 634 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 635 | celt_assert(0); |
michael@0 | 636 | #endif |
michael@0 | 637 | } |
michael@0 | 638 | celt_mips+=4; |
michael@0 | 639 | return res; |
michael@0 | 640 | } |
michael@0 | 641 | static OPUS_INLINE short MULT16_16_P14(int a, int b) |
michael@0 | 642 | { |
michael@0 | 643 | opus_int64 res; |
michael@0 | 644 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 645 | { |
michael@0 | 646 | fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b); |
michael@0 | 647 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 648 | celt_assert(0); |
michael@0 | 649 | #endif |
michael@0 | 650 | } |
michael@0 | 651 | res = ((opus_int64)a)*b; |
michael@0 | 652 | res += 8192; |
michael@0 | 653 | if (!VERIFY_INT(res)) |
michael@0 | 654 | { |
michael@0 | 655 | fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res); |
michael@0 | 656 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 657 | celt_assert(0); |
michael@0 | 658 | #endif |
michael@0 | 659 | } |
michael@0 | 660 | res >>= 14; |
michael@0 | 661 | if (!VERIFY_SHORT(res)) |
michael@0 | 662 | { |
michael@0 | 663 | fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res); |
michael@0 | 664 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 665 | celt_assert(0); |
michael@0 | 666 | #endif |
michael@0 | 667 | } |
michael@0 | 668 | celt_mips+=4; |
michael@0 | 669 | return res; |
michael@0 | 670 | } |
michael@0 | 671 | static OPUS_INLINE short MULT16_16_P15(int a, int b) |
michael@0 | 672 | { |
michael@0 | 673 | opus_int64 res; |
michael@0 | 674 | if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) |
michael@0 | 675 | { |
michael@0 | 676 | fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b); |
michael@0 | 677 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 678 | celt_assert(0); |
michael@0 | 679 | #endif |
michael@0 | 680 | } |
michael@0 | 681 | res = ((opus_int64)a)*b; |
michael@0 | 682 | res += 16384; |
michael@0 | 683 | if (!VERIFY_INT(res)) |
michael@0 | 684 | { |
michael@0 | 685 | fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res); |
michael@0 | 686 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 687 | celt_assert(0); |
michael@0 | 688 | #endif |
michael@0 | 689 | } |
michael@0 | 690 | res >>= 15; |
michael@0 | 691 | if (!VERIFY_SHORT(res)) |
michael@0 | 692 | { |
michael@0 | 693 | fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res); |
michael@0 | 694 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 695 | celt_assert(0); |
michael@0 | 696 | #endif |
michael@0 | 697 | } |
michael@0 | 698 | celt_mips+=2; |
michael@0 | 699 | return res; |
michael@0 | 700 | } |
michael@0 | 701 | |
michael@0 | 702 | #define DIV32_16(a, b) DIV32_16_(a, b, __FILE__, __LINE__) |
michael@0 | 703 | |
michael@0 | 704 | static OPUS_INLINE int DIV32_16_(opus_int64 a, opus_int64 b, char *file, int line) |
michael@0 | 705 | { |
michael@0 | 706 | opus_int64 res; |
michael@0 | 707 | if (b==0) |
michael@0 | 708 | { |
michael@0 | 709 | fprintf(stderr, "DIV32_16: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line); |
michael@0 | 710 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 711 | celt_assert(0); |
michael@0 | 712 | #endif |
michael@0 | 713 | return 0; |
michael@0 | 714 | } |
michael@0 | 715 | if (!VERIFY_INT(a) || !VERIFY_SHORT(b)) |
michael@0 | 716 | { |
michael@0 | 717 | fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line); |
michael@0 | 718 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 719 | celt_assert(0); |
michael@0 | 720 | #endif |
michael@0 | 721 | } |
michael@0 | 722 | res = a/b; |
michael@0 | 723 | if (!VERIFY_SHORT(res)) |
michael@0 | 724 | { |
michael@0 | 725 | fprintf (stderr, "DIV32_16: output is not short: %d / %d = %d in %s: line %d\n", (int)a,(int)b,(int)res, file, line); |
michael@0 | 726 | if (res>32767) |
michael@0 | 727 | res = 32767; |
michael@0 | 728 | if (res<-32768) |
michael@0 | 729 | res = -32768; |
michael@0 | 730 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 731 | celt_assert(0); |
michael@0 | 732 | #endif |
michael@0 | 733 | } |
michael@0 | 734 | celt_mips+=35; |
michael@0 | 735 | return res; |
michael@0 | 736 | } |
michael@0 | 737 | |
michael@0 | 738 | #define DIV32(a, b) DIV32_(a, b, __FILE__, __LINE__) |
michael@0 | 739 | static OPUS_INLINE int DIV32_(opus_int64 a, opus_int64 b, char *file, int line) |
michael@0 | 740 | { |
michael@0 | 741 | opus_int64 res; |
michael@0 | 742 | if (b==0) |
michael@0 | 743 | { |
michael@0 | 744 | fprintf(stderr, "DIV32: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line); |
michael@0 | 745 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 746 | celt_assert(0); |
michael@0 | 747 | #endif |
michael@0 | 748 | return 0; |
michael@0 | 749 | } |
michael@0 | 750 | |
michael@0 | 751 | if (!VERIFY_INT(a) || !VERIFY_INT(b)) |
michael@0 | 752 | { |
michael@0 | 753 | fprintf (stderr, "DIV32: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line); |
michael@0 | 754 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 755 | celt_assert(0); |
michael@0 | 756 | #endif |
michael@0 | 757 | } |
michael@0 | 758 | res = a/b; |
michael@0 | 759 | if (!VERIFY_INT(res)) |
michael@0 | 760 | { |
michael@0 | 761 | fprintf (stderr, "DIV32: output is not int: %d in %s: line %d\n", (int)res, file, line); |
michael@0 | 762 | #ifdef FIXED_DEBUG_ASSERT |
michael@0 | 763 | celt_assert(0); |
michael@0 | 764 | #endif |
michael@0 | 765 | } |
michael@0 | 766 | celt_mips+=70; |
michael@0 | 767 | return res; |
michael@0 | 768 | } |
michael@0 | 769 | |
michael@0 | 770 | #undef PRINT_MIPS |
michael@0 | 771 | #define PRINT_MIPS(file) do {fprintf (file, "total complexity = %llu MIPS\n", celt_mips);} while (0); |
michael@0 | 772 | |
michael@0 | 773 | #endif |