michael@0: /* Copyright (C) 2003-2008 Jean-Marc Valin michael@0: Copyright (C) 2007-2012 Xiph.Org Foundation */ michael@0: /** michael@0: @file fixed_debug.h michael@0: @brief Fixed-point operations with debugging michael@0: */ michael@0: /* michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: michael@0: - Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER michael@0: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifndef FIXED_DEBUG_H michael@0: #define FIXED_DEBUG_H michael@0: michael@0: #include michael@0: #include "opus_defines.h" michael@0: michael@0: #ifdef CELT_C michael@0: OPUS_EXPORT opus_int64 celt_mips=0; michael@0: #else michael@0: extern opus_int64 celt_mips; michael@0: #endif michael@0: michael@0: #define MULT16_16SU(a,b) ((opus_val32)(opus_val16)(a)*(opus_val32)(opus_uint16)(b)) michael@0: #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: michael@0: /** 16x32 multiplication, followed by a 16-bit shift right. Results fits in 32 bits */ michael@0: #define MULT16_32_Q16(a,b) ADD32(MULT16_16((a),SHR32((b),16)), SHR32(MULT16_16SU((a),((b)&0x0000ffff)),16)) michael@0: michael@0: #define MULT16_32_P16(a,b) MULT16_32_PX(a,b,16) michael@0: michael@0: #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits)))) michael@0: #define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits)))) michael@0: michael@0: #define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768) michael@0: #define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL) michael@0: #define VERIFY_UINT(x) ((x)<=(2147483647LLU<<1)) michael@0: michael@0: #define SHR(a,b) SHR32(a,b) michael@0: #define PSHR(a,b) PSHR32(a,b) michael@0: michael@0: static OPUS_INLINE short NEG16(int x) michael@0: { michael@0: int res; michael@0: if (!VERIFY_SHORT(x)) michael@0: { michael@0: fprintf (stderr, "NEG16: input is not short: %d\n", (int)x); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = -x; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "NEG16: output is not short: %d\n", (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips++; michael@0: return res; michael@0: } michael@0: static OPUS_INLINE int NEG32(opus_int64 x) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_INT(x)) michael@0: { michael@0: fprintf (stderr, "NEG16: input is not int: %d\n", (int)x); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = -x; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: fprintf (stderr, "NEG16: output is not int: %d\n", (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=2; michael@0: return res; michael@0: } michael@0: michael@0: #define EXTRACT16(x) EXTRACT16_(x, __FILE__, __LINE__) michael@0: static OPUS_INLINE short EXTRACT16_(int x, char *file, int line) michael@0: { michael@0: int res; michael@0: if (!VERIFY_SHORT(x)) michael@0: { michael@0: fprintf (stderr, "EXTRACT16: input is not short: %d in %s: line %d\n", x, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = x; michael@0: celt_mips++; michael@0: return res; michael@0: } michael@0: michael@0: #define EXTEND32(x) EXTEND32_(x, __FILE__, __LINE__) michael@0: static OPUS_INLINE int EXTEND32_(int x, char *file, int line) michael@0: { michael@0: int res; michael@0: if (!VERIFY_SHORT(x)) michael@0: { michael@0: fprintf (stderr, "EXTEND32: input is not short: %d in %s: line %d\n", x, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = x; michael@0: celt_mips++; michael@0: return res; michael@0: } michael@0: michael@0: #define SHR16(a, shift) SHR16_(a, shift, __FILE__, __LINE__) michael@0: static OPUS_INLINE short SHR16_(int a, int shift, char *file, int line) michael@0: { michael@0: int res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift)) michael@0: { michael@0: fprintf (stderr, "SHR16: inputs are not short: %d >> %d in %s: line %d\n", a, shift, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a>>shift; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "SHR16: output is not short: %d in %s: line %d\n", res, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips++; michael@0: return res; michael@0: } michael@0: #define SHL16(a, shift) SHL16_(a, shift, __FILE__, __LINE__) michael@0: static OPUS_INLINE short SHL16_(int a, int shift, char *file, int line) michael@0: { michael@0: int res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift)) michael@0: { michael@0: fprintf (stderr, "SHL16: inputs are not short: %d %d in %s: line %d\n", a, shift, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a<>shift; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: fprintf (stderr, "SHR32: output is not int: %d\n", (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=2; michael@0: return res; michael@0: } michael@0: #define SHL32(a, shift) SHL32_(a, shift, __FILE__, __LINE__) michael@0: static OPUS_INLINE int SHL32_(opus_int64 a, int shift, char *file, int line) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_INT(a) || !VERIFY_SHORT(shift)) michael@0: { michael@0: fprintf (stderr, "SHL32: inputs are not int: %lld %d in %s: line %d\n", a, shift, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a<>1))),shift)) michael@0: #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift))) michael@0: michael@0: #define ROUND16(x,a) (celt_mips--,EXTRACT16(PSHR32((x),(a)))) michael@0: #define HALF16(x) (SHR16(x,1)) michael@0: #define HALF32(x) (SHR32(x,1)) michael@0: michael@0: //#define SHR(a,shift) ((a) >> (shift)) michael@0: //#define SHL(a,shift) ((a) << (shift)) michael@0: michael@0: #define ADD16(a, b) ADD16_(a, b, __FILE__, __LINE__) michael@0: static OPUS_INLINE short ADD16_(int a, int b, char *file, int line) michael@0: { michael@0: int res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "ADD16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a+b; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips++; michael@0: return res; michael@0: } michael@0: michael@0: #define SUB16(a, b) SUB16_(a, b, __FILE__, __LINE__) michael@0: static OPUS_INLINE short SUB16_(int a, int b, char *file, int line) michael@0: { michael@0: int res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "SUB16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a-b; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "SUB16: output is not short: %d in %s: line %d\n", res, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips++; michael@0: return res; michael@0: } michael@0: michael@0: #define ADD32(a, b) ADD32_(a, b, __FILE__, __LINE__) michael@0: static OPUS_INLINE int ADD32_(opus_int64 a, opus_int64 b, char *file, int line) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_INT(a) || !VERIFY_INT(b)) michael@0: { michael@0: fprintf (stderr, "ADD32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a+b; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: fprintf (stderr, "ADD32: output is not int: %d in %s: line %d\n", (int)res, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=2; michael@0: return res; michael@0: } michael@0: michael@0: #define SUB32(a, b) SUB32_(a, b, __FILE__, __LINE__) michael@0: static OPUS_INLINE int SUB32_(opus_int64 a, opus_int64 b, char *file, int line) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_INT(a) || !VERIFY_INT(b)) michael@0: { michael@0: fprintf (stderr, "SUB32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a-b; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: fprintf (stderr, "SUB32: output is not int: %d in %s: line %d\n", (int)res, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=2; michael@0: return res; michael@0: } michael@0: michael@0: #undef UADD32 michael@0: #define UADD32(a, b) UADD32_(a, b, __FILE__, __LINE__) michael@0: static OPUS_INLINE unsigned int UADD32_(opus_uint64 a, opus_uint64 b, char *file, int line) michael@0: { michael@0: opus_uint64 res; michael@0: if (!VERIFY_UINT(a) || !VERIFY_UINT(b)) michael@0: { michael@0: fprintf (stderr, "UADD32: inputs are not uint32: %llu %llu in %s: line %d\n", a, b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a+b; michael@0: if (!VERIFY_UINT(res)) michael@0: { michael@0: fprintf (stderr, "UADD32: output is not uint32: %llu in %s: line %d\n", res, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=2; michael@0: return res; michael@0: } michael@0: michael@0: #undef USUB32 michael@0: #define USUB32(a, b) USUB32_(a, b, __FILE__, __LINE__) michael@0: static OPUS_INLINE unsigned int USUB32_(opus_uint64 a, opus_uint64 b, char *file, int line) michael@0: { michael@0: opus_uint64 res; michael@0: if (!VERIFY_UINT(a) || !VERIFY_UINT(b)) michael@0: { michael@0: fprintf (stderr, "USUB32: inputs are not uint32: %llu %llu in %s: line %d\n", a, b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: if (a=((opus_val32)(1)<<(15+Q))) michael@0: { michael@0: 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: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = (((opus_int64)a)*(opus_int64)b) >> Q; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: 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: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: if (Q==15) michael@0: celt_mips+=3; michael@0: else michael@0: celt_mips+=4; michael@0: return res; michael@0: } michael@0: michael@0: #define MULT16_32_PX(a, b, Q) MULT16_32_PX_(a, b, Q, __FILE__, __LINE__) michael@0: static OPUS_INLINE int MULT16_32_PX_(int a, opus_int64 b, int Q, char *file, int line) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_INT(b)) michael@0: { michael@0: 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: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: if (ABS32(b)>=((opus_int64)(1)<<(15+Q))) michael@0: { michael@0: 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: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = ((((opus_int64)a)*(opus_int64)b) + (((opus_val32)(1)<>1))>> Q; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: 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: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: if (Q==15) michael@0: celt_mips+=4; michael@0: else michael@0: celt_mips+=5; michael@0: return res; michael@0: } michael@0: michael@0: #define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15) michael@0: #define MAC16_32_Q15(c,a,b) (celt_mips-=2,ADD32((c),MULT16_32_Q15((a),(b)))) michael@0: michael@0: static OPUS_INLINE int SATURATE(int a, int b) michael@0: { michael@0: if (a>b) michael@0: a=b; michael@0: if (a<-b) michael@0: a = -b; michael@0: celt_mips+=3; michael@0: return a; michael@0: } michael@0: michael@0: static OPUS_INLINE opus_int16 SATURATE16(opus_int32 a) michael@0: { michael@0: celt_mips+=3; michael@0: if (a>32767) michael@0: return 32767; michael@0: else if (a<-32768) michael@0: return -32768; michael@0: else return a; michael@0: } michael@0: michael@0: static OPUS_INLINE int MULT16_16_Q11_32(int a, int b) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = ((opus_int64)a)*b; michael@0: res >>= 11; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=3; michael@0: return res; michael@0: } michael@0: static OPUS_INLINE short MULT16_16_Q13(int a, int b) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = ((opus_int64)a)*b; michael@0: res >>= 13; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=3; michael@0: return res; michael@0: } michael@0: static OPUS_INLINE short MULT16_16_Q14(int a, int b) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = ((opus_int64)a)*b; michael@0: res >>= 14; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=3; michael@0: return res; michael@0: } michael@0: michael@0: #define MULT16_16_Q15(a, b) MULT16_16_Q15_(a, b, __FILE__, __LINE__) michael@0: static OPUS_INLINE short MULT16_16_Q15_(int a, int b, char *file, int line) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = ((opus_int64)a)*b; michael@0: res >>= 15; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_Q15: output is not short: %d in %s: line %d\n", (int)res, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=1; michael@0: return res; michael@0: } michael@0: michael@0: static OPUS_INLINE short MULT16_16_P13(int a, int b) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = ((opus_int64)a)*b; michael@0: res += 4096; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res >>= 13; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=4; michael@0: return res; michael@0: } michael@0: static OPUS_INLINE short MULT16_16_P14(int a, int b) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = ((opus_int64)a)*b; michael@0: res += 8192; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res >>= 14; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=4; michael@0: return res; michael@0: } michael@0: static OPUS_INLINE short MULT16_16_P15(int a, int b) michael@0: { michael@0: opus_int64 res; michael@0: if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = ((opus_int64)a)*b; michael@0: res += 16384; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res >>= 15; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=2; michael@0: return res; michael@0: } michael@0: michael@0: #define DIV32_16(a, b) DIV32_16_(a, b, __FILE__, __LINE__) michael@0: michael@0: static OPUS_INLINE int DIV32_16_(opus_int64 a, opus_int64 b, char *file, int line) michael@0: { michael@0: opus_int64 res; michael@0: if (b==0) michael@0: { michael@0: fprintf(stderr, "DIV32_16: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: return 0; michael@0: } michael@0: if (!VERIFY_INT(a) || !VERIFY_SHORT(b)) michael@0: { michael@0: fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a/b; michael@0: if (!VERIFY_SHORT(res)) michael@0: { michael@0: 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: if (res>32767) michael@0: res = 32767; michael@0: if (res<-32768) michael@0: res = -32768; michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=35; michael@0: return res; michael@0: } michael@0: michael@0: #define DIV32(a, b) DIV32_(a, b, __FILE__, __LINE__) michael@0: static OPUS_INLINE int DIV32_(opus_int64 a, opus_int64 b, char *file, int line) michael@0: { michael@0: opus_int64 res; michael@0: if (b==0) michael@0: { michael@0: fprintf(stderr, "DIV32: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: return 0; michael@0: } michael@0: michael@0: if (!VERIFY_INT(a) || !VERIFY_INT(b)) michael@0: { michael@0: fprintf (stderr, "DIV32: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: res = a/b; michael@0: if (!VERIFY_INT(res)) michael@0: { michael@0: fprintf (stderr, "DIV32: output is not int: %d in %s: line %d\n", (int)res, file, line); michael@0: #ifdef FIXED_DEBUG_ASSERT michael@0: celt_assert(0); michael@0: #endif michael@0: } michael@0: celt_mips+=70; michael@0: return res; michael@0: } michael@0: michael@0: #undef PRINT_MIPS michael@0: #define PRINT_MIPS(file) do {fprintf (file, "total complexity = %llu MIPS\n", celt_mips);} while (0); michael@0: michael@0: #endif