36 #include "opus_defines.h"
39 OPUS_EXPORT opus_int64 celt_mips=0;
41 extern opus_int64 celt_mips;
44 #define MULT16_16SU(a,b) ((opus_val32)(opus_val16)(a)*(opus_val32)(opus_uint16)(b))
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))
48 #define MULT16_32_Q16(a,b) ADD32(MULT16_16((a),SHR32((b),16)), SHR32(MULT16_16SU((a),((b)&0x0000ffff)),16))
50 #define MULT16_32_P16(a,b) MULT16_32_PX(a,b,16)
52 #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits))))
53 #define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits))))
55 #define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768)
56 #define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL)
57 #define VERIFY_UINT(x) ((x)<=(2147483647LLU<<1))
59 #define SHR(a,b) SHR32(a,b)
60 #define PSHR(a,b) PSHR32(a,b)
63 #define ADD32_ovflw(a,b) (celt_mips+=2,(opus_val32)((opus_uint32)(a)+(opus_uint32)(b)))
65 #define SUB32_ovflw(a,b) (celt_mips+=2,(opus_val32)((opus_uint32)(a)-(opus_uint32)(b)))
68 #define NEG32_ovflw(a) (celt_mips+=2,(opus_val32)(0-(opus_uint32)(a)))
70 static OPUS_INLINE
short NEG16(
int x)
75 fprintf (stderr,
"NEG16: input is not short: %d\n", (
int)x);
76 #ifdef FIXED_DEBUG_ASSERT
81 if (!VERIFY_SHORT(res))
83 fprintf (stderr,
"NEG16: output is not short: %d\n", (
int)res);
84 #ifdef FIXED_DEBUG_ASSERT
91 static OPUS_INLINE
int NEG32(opus_int64 x)
96 fprintf (stderr,
"NEG16: input is not int: %d\n", (
int)x);
97 #ifdef FIXED_DEBUG_ASSERT
102 if (!VERIFY_INT(res))
104 fprintf (stderr,
"NEG16: output is not int: %d\n", (
int)res);
105 #ifdef FIXED_DEBUG_ASSERT
113 #define EXTRACT16(x) EXTRACT16_(x, __FILE__, __LINE__)
114 static OPUS_INLINE
short EXTRACT16_(
int x,
char *file,
int line)
117 if (!VERIFY_SHORT(x))
119 fprintf (stderr,
"EXTRACT16: input is not short: %d in %s: line %d\n", x, file, line);
120 #ifdef FIXED_DEBUG_ASSERT
129 #define EXTEND32(x) EXTEND32_(x, __FILE__, __LINE__)
130 static OPUS_INLINE
int EXTEND32_(
int x,
char *file,
int line)
133 if (!VERIFY_SHORT(x))
135 fprintf (stderr,
"EXTEND32: input is not short: %d in %s: line %d\n", x, file, line);
136 #ifdef FIXED_DEBUG_ASSERT
145 #define SHR16(a, shift) SHR16_(a, shift, __FILE__, __LINE__)
146 static OPUS_INLINE
short SHR16_(
int a,
int shift,
char *file,
int line)
149 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
151 fprintf (stderr,
"SHR16: inputs are not short: %d >> %d in %s: line %d\n", a, shift, file, line);
152 #ifdef FIXED_DEBUG_ASSERT
157 if (!VERIFY_SHORT(res))
159 fprintf (stderr,
"SHR16: output is not short: %d in %s: line %d\n", res, file, line);
160 #ifdef FIXED_DEBUG_ASSERT
167 #define SHL16(a, shift) SHL16_(a, shift, __FILE__, __LINE__)
168 static OPUS_INLINE
short SHL16_(
int a,
int shift,
char *file,
int line)
171 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
173 fprintf (stderr,
"SHL16: inputs are not short: %d %d in %s: line %d\n", a, shift, file, line);
174 #ifdef FIXED_DEBUG_ASSERT
179 if (!VERIFY_SHORT(res))
181 fprintf (stderr,
"SHL16: output is not short: %d in %s: line %d\n", res, file, line);
182 #ifdef FIXED_DEBUG_ASSERT
190 static OPUS_INLINE
int SHR32(opus_int64 a,
int shift)
193 if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
195 fprintf (stderr,
"SHR32: inputs are not int: %d %d\n", (
int)a, shift);
196 #ifdef FIXED_DEBUG_ASSERT
201 if (!VERIFY_INT(res))
203 fprintf (stderr,
"SHR32: output is not int: %d\n", (
int)res);
204 #ifdef FIXED_DEBUG_ASSERT
211 #define SHL32(a, shift) SHL32_(a, shift, __FILE__, __LINE__)
212 static OPUS_INLINE
int SHL32_(opus_int64 a,
int shift,
char *file,
int line)
215 if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
217 fprintf (stderr,
"SHL32: inputs are not int: %lld %d in %s: line %d\n", a, shift, file, line);
218 #ifdef FIXED_DEBUG_ASSERT
223 if (!VERIFY_INT(res))
225 fprintf (stderr,
"SHL32: output is not int: %lld<<%d = %lld in %s: line %d\n", a, shift, res, file, line);
226 #ifdef FIXED_DEBUG_ASSERT
234 #define PSHR32(a,shift) (celt_mips--,SHR32(ADD32((a),(((opus_val32)(1)<<((shift))>>1))),shift))
235 #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))
237 #define ROUND16(x,a) (celt_mips--,EXTRACT16(PSHR32((x),(a))))
238 #define SROUND16(x,a) (celt_mips--,EXTRACT16(SATURATE(PSHR32(x,a), 32767)));
240 #define HALF16(x) (SHR16(x,1))
241 #define HALF32(x) (SHR32(x,1))
243 #define ADD16(a, b) ADD16_(a, b, __FILE__, __LINE__)
244 static OPUS_INLINE
short ADD16_(
int a,
int b,
char *file,
int line)
247 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
249 fprintf (stderr,
"ADD16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
250 #ifdef FIXED_DEBUG_ASSERT
255 if (!VERIFY_SHORT(res))
257 fprintf (stderr,
"ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line);
258 #ifdef FIXED_DEBUG_ASSERT
266 #define SUB16(a, b) SUB16_(a, b, __FILE__, __LINE__)
267 static OPUS_INLINE
short SUB16_(
int a,
int b,
char *file,
int line)
270 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
272 fprintf (stderr,
"SUB16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
273 #ifdef FIXED_DEBUG_ASSERT
278 if (!VERIFY_SHORT(res))
280 fprintf (stderr,
"SUB16: output is not short: %d in %s: line %d\n", res, file, line);
281 #ifdef FIXED_DEBUG_ASSERT
289 #define ADD32(a, b) ADD32_(a, b, __FILE__, __LINE__)
290 static OPUS_INLINE
int ADD32_(opus_int64 a, opus_int64 b,
char *file,
int line)
293 if (!VERIFY_INT(a) || !VERIFY_INT(b))
295 fprintf (stderr,
"ADD32: inputs are not int: %d %d in %s: line %d\n", (
int)a, (
int)b, file, line);
296 #ifdef FIXED_DEBUG_ASSERT
301 if (!VERIFY_INT(res))
303 fprintf (stderr,
"ADD32: output is not int: %d in %s: line %d\n", (
int)res, file, line);
304 #ifdef FIXED_DEBUG_ASSERT
312 #define SUB32(a, b) SUB32_(a, b, __FILE__, __LINE__)
313 static OPUS_INLINE
int SUB32_(opus_int64 a, opus_int64 b,
char *file,
int line)
316 if (!VERIFY_INT(a) || !VERIFY_INT(b))
318 fprintf (stderr,
"SUB32: inputs are not int: %d %d in %s: line %d\n", (
int)a, (
int)b, file, line);
319 #ifdef FIXED_DEBUG_ASSERT
324 if (!VERIFY_INT(res))
326 fprintf (stderr,
"SUB32: output is not int: %d in %s: line %d\n", (
int)res, file, line);
327 #ifdef FIXED_DEBUG_ASSERT
336 #define UADD32(a, b) UADD32_(a, b, __FILE__, __LINE__)
337 static OPUS_INLINE
unsigned int UADD32_(opus_uint64 a, opus_uint64 b,
char *file,
int line)
340 if (!VERIFY_UINT(a) || !VERIFY_UINT(b))
342 fprintf (stderr,
"UADD32: inputs are not uint32: %llu %llu in %s: line %d\n", a, b, file, line);
343 #ifdef FIXED_DEBUG_ASSERT
348 if (!VERIFY_UINT(res))
350 fprintf (stderr,
"UADD32: output is not uint32: %llu in %s: line %d\n", res, file, line);
351 #ifdef FIXED_DEBUG_ASSERT
360 #define USUB32(a, b) USUB32_(a, b, __FILE__, __LINE__)
361 static OPUS_INLINE
unsigned int USUB32_(opus_uint64 a, opus_uint64 b,
char *file,
int line)
364 if (!VERIFY_UINT(a) || !VERIFY_UINT(b))
366 fprintf (stderr,
"USUB32: inputs are not uint32: %llu %llu in %s: line %d\n", a, b, file, line);
367 #ifdef FIXED_DEBUG_ASSERT
373 fprintf (stderr,
"USUB32: inputs underflow: %llu < %llu in %s: line %d\n", a, b, file, line);
374 #ifdef FIXED_DEBUG_ASSERT
379 if (!VERIFY_UINT(res))
381 fprintf (stderr,
"USUB32: output is not uint32: %llu - %llu = %llu in %s: line %d\n", a, b, res, file, line);
382 #ifdef FIXED_DEBUG_ASSERT
391 static OPUS_INLINE
short MULT16_16_16(
int a,
int b)
394 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
396 fprintf (stderr,
"MULT16_16_16: inputs are not short: %d %d\n", a, b);
397 #ifdef FIXED_DEBUG_ASSERT
402 if (!VERIFY_SHORT(res))
404 fprintf (stderr,
"MULT16_16_16: output is not short: %d\n", res);
405 #ifdef FIXED_DEBUG_ASSERT
413 #define MULT16_16(a, b) MULT16_16_(a, b, __FILE__, __LINE__)
414 static OPUS_INLINE
int MULT16_16_(
int a,
int b,
char *file,
int line)
417 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
419 fprintf (stderr,
"MULT16_16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
420 #ifdef FIXED_DEBUG_ASSERT
424 res = ((opus_int64)a)*b;
425 if (!VERIFY_INT(res))
427 fprintf (stderr,
"MULT16_16: output is not int: %d in %s: line %d\n", (
int)res, file, line);
428 #ifdef FIXED_DEBUG_ASSERT
436 #define MAC16_16(c,a,b) (celt_mips-=2,ADD32((c),MULT16_16((a),(b))))
438 #define MULT16_32_QX(a, b, Q) MULT16_32_QX_(a, b, Q, __FILE__, __LINE__)
439 static OPUS_INLINE
int MULT16_32_QX_(
int a, opus_int64 b,
int Q,
char *file,
int line)
442 if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
444 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);
445 #ifdef FIXED_DEBUG_ASSERT
449 if (ABS32(b)>=((opus_val32)(1)<<(15+Q)))
451 fprintf (stderr,
"MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n", Q, (
int)a, (
int)b, file, line);
452 #ifdef FIXED_DEBUG_ASSERT
456 res = (((opus_int64)a)*(opus_int64)b) >> Q;
457 if (!VERIFY_INT(res))
459 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);
460 #ifdef FIXED_DEBUG_ASSERT
471 #define MULT16_32_PX(a, b, Q) MULT16_32_PX_(a, b, Q, __FILE__, __LINE__)
472 static OPUS_INLINE
int MULT16_32_PX_(
int a, opus_int64 b,
int Q,
char *file,
int line)
475 if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
477 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);
478 #ifdef FIXED_DEBUG_ASSERT
482 if (ABS32(b)>=((opus_int64)(1)<<(15+Q)))
484 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);
485 #ifdef FIXED_DEBUG_ASSERT
489 res = ((((opus_int64)a)*(opus_int64)b) + (((opus_val32)(1)<<Q)>>1))>> Q;
490 if (!VERIFY_INT(res))
492 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);
493 #ifdef FIXED_DEBUG_ASSERT
504 #define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15)
505 #define MAC16_32_Q15(c,a,b) (celt_mips-=2,ADD32((c),MULT16_32_Q15((a),(b))))
506 #define MAC16_32_Q16(c,a,b) (celt_mips-=2,ADD32((c),MULT16_32_Q16((a),(b))))
508 static OPUS_INLINE
int SATURATE(
int a,
int b)
518 static OPUS_INLINE opus_int16 SATURATE16(opus_int32 a)
528 static OPUS_INLINE
int MULT16_16_Q11_32(
int a,
int b)
531 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
533 fprintf (stderr,
"MULT16_16_Q11: inputs are not short: %d %d\n", a, b);
534 #ifdef FIXED_DEBUG_ASSERT
538 res = ((opus_int64)a)*b;
540 if (!VERIFY_INT(res))
542 fprintf (stderr,
"MULT16_16_Q11: output is not short: %d*%d=%d\n", (
int)a, (
int)b, (
int)res);
543 #ifdef FIXED_DEBUG_ASSERT
550 static OPUS_INLINE
short MULT16_16_Q13(
int a,
int b)
553 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
555 fprintf (stderr,
"MULT16_16_Q13: inputs are not short: %d %d\n", a, b);
556 #ifdef FIXED_DEBUG_ASSERT
560 res = ((opus_int64)a)*b;
562 if (!VERIFY_SHORT(res))
564 fprintf (stderr,
"MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (
int)res);
565 #ifdef FIXED_DEBUG_ASSERT
572 static OPUS_INLINE
short MULT16_16_Q14(
int a,
int b)
575 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
577 fprintf (stderr,
"MULT16_16_Q14: inputs are not short: %d %d\n", a, b);
578 #ifdef FIXED_DEBUG_ASSERT
582 res = ((opus_int64)a)*b;
584 if (!VERIFY_SHORT(res))
586 fprintf (stderr,
"MULT16_16_Q14: output is not short: %d\n", (
int)res);
587 #ifdef FIXED_DEBUG_ASSERT
595 #define MULT16_16_Q15(a, b) MULT16_16_Q15_(a, b, __FILE__, __LINE__)
596 static OPUS_INLINE
short MULT16_16_Q15_(
int a,
int b,
char *file,
int line)
599 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
601 fprintf (stderr,
"MULT16_16_Q15: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
602 #ifdef FIXED_DEBUG_ASSERT
606 res = ((opus_int64)a)*b;
608 if (!VERIFY_SHORT(res))
610 fprintf (stderr,
"MULT16_16_Q15: output is not short: %d in %s: line %d\n", (
int)res, file, line);
611 #ifdef FIXED_DEBUG_ASSERT
619 static OPUS_INLINE
short MULT16_16_P13(
int a,
int b)
622 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
624 fprintf (stderr,
"MULT16_16_P13: inputs are not short: %d %d\n", a, b);
625 #ifdef FIXED_DEBUG_ASSERT
629 res = ((opus_int64)a)*b;
631 if (!VERIFY_INT(res))
633 fprintf (stderr,
"MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (
int)res);
634 #ifdef FIXED_DEBUG_ASSERT
639 if (!VERIFY_SHORT(res))
641 fprintf (stderr,
"MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (
int)res);
642 #ifdef FIXED_DEBUG_ASSERT
649 static OPUS_INLINE
short MULT16_16_P14(
int a,
int b)
652 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
654 fprintf (stderr,
"MULT16_16_P14: inputs are not short: %d %d\n", a, b);
655 #ifdef FIXED_DEBUG_ASSERT
659 res = ((opus_int64)a)*b;
661 if (!VERIFY_INT(res))
663 fprintf (stderr,
"MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (
int)res);
664 #ifdef FIXED_DEBUG_ASSERT
669 if (!VERIFY_SHORT(res))
671 fprintf (stderr,
"MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (
int)res);
672 #ifdef FIXED_DEBUG_ASSERT
679 static OPUS_INLINE
short MULT16_16_P15(
int a,
int b)
682 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
684 fprintf (stderr,
"MULT16_16_P15: inputs are not short: %d %d\n", a, b);
685 #ifdef FIXED_DEBUG_ASSERT
689 res = ((opus_int64)a)*b;
691 if (!VERIFY_INT(res))
693 fprintf (stderr,
"MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (
int)res);
694 #ifdef FIXED_DEBUG_ASSERT
699 if (!VERIFY_SHORT(res))
701 fprintf (stderr,
"MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (
int)res);
702 #ifdef FIXED_DEBUG_ASSERT
710 #define DIV32_16(a, b) DIV32_16_(a, b, __FILE__, __LINE__)
712 static OPUS_INLINE
int DIV32_16_(opus_int64 a, opus_int64 b,
char *file,
int line)
717 fprintf(stderr,
"DIV32_16: divide by zero: %d/%d in %s: line %d\n", (
int)a, (
int)b, file, line);
718 #ifdef FIXED_DEBUG_ASSERT
723 if (!VERIFY_INT(a) || !VERIFY_SHORT(b))
725 fprintf (stderr,
"DIV32_16: inputs are not int/short: %d %d in %s: line %d\n", (
int)a, (
int)b, file, line);
726 #ifdef FIXED_DEBUG_ASSERT
731 if (!VERIFY_SHORT(res))
733 fprintf (stderr,
"DIV32_16: output is not short: %d / %d = %d in %s: line %d\n", (
int)a,(
int)b,(
int)res, file, line);
738 #ifdef FIXED_DEBUG_ASSERT
746 #define DIV32(a, b) DIV32_(a, b, __FILE__, __LINE__)
747 static OPUS_INLINE
int DIV32_(opus_int64 a, opus_int64 b,
char *file,
int line)
752 fprintf(stderr,
"DIV32: divide by zero: %d/%d in %s: line %d\n", (
int)a, (
int)b, file, line);
753 #ifdef FIXED_DEBUG_ASSERT
759 if (!VERIFY_INT(a) || !VERIFY_INT(b))
761 fprintf (stderr,
"DIV32: inputs are not int/short: %d %d in %s: line %d\n", (
int)a, (
int)b, file, line);
762 #ifdef FIXED_DEBUG_ASSERT
767 if (!VERIFY_INT(res))
769 fprintf (stderr,
"DIV32: output is not int: %d in %s: line %d\n", (
int)res, file, line);
770 #ifdef FIXED_DEBUG_ASSERT
778 static OPUS_INLINE opus_val16 SIG2WORD16_generic(celt_sig x)
780 x = PSHR32(x, SIG_SHIFT);
781 x = MAX32(x, -32768);
785 #define SIG2WORD16(x) (SIG2WORD16_generic(x))
789 #define PRINT_MIPS(file) do {fprintf (file, "total complexity = %llu MIPS\n", celt_mips);} while (0);