cannam@154: /* Copyright (C) 2007-2009 Xiph.Org Foundation cannam@154: Copyright (C) 2003-2008 Jean-Marc Valin cannam@154: Copyright (C) 2007-2008 CSIRO */ cannam@154: /** cannam@154: @file fixed_generic.h cannam@154: @brief Generic fixed-point operations cannam@154: */ cannam@154: /* cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: cannam@154: - Redistributions of source code must retain the above copyright cannam@154: notice, this list of conditions and the following disclaimer. cannam@154: cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: */ cannam@154: cannam@154: #ifndef FIXED_GENERIC_H cannam@154: #define FIXED_GENERIC_H cannam@154: cannam@154: /** Multiply a 16-bit signed value by a 16-bit unsigned value. The result is a 32-bit signed value */ cannam@154: #define MULT16_16SU(a,b) ((opus_val32)(opus_val16)(a)*(opus_val32)(opus_uint16)(b)) cannam@154: cannam@154: /** 16x32 multiplication, followed by a 16-bit shift right. Results fits in 32 bits */ cannam@154: #if OPUS_FAST_INT64 cannam@154: #define MULT16_32_Q16(a,b) ((opus_val32)SHR((opus_int64)((opus_val16)(a))*(b),16)) cannam@154: #else cannam@154: #define MULT16_32_Q16(a,b) ADD32(MULT16_16((a),SHR((b),16)), SHR(MULT16_16SU((a),((b)&0x0000ffff)),16)) cannam@154: #endif cannam@154: cannam@154: /** 16x32 multiplication, followed by a 16-bit shift right (round-to-nearest). Results fits in 32 bits */ cannam@154: #if OPUS_FAST_INT64 cannam@154: #define MULT16_32_P16(a,b) ((opus_val32)PSHR((opus_int64)((opus_val16)(a))*(b),16)) cannam@154: #else cannam@154: #define MULT16_32_P16(a,b) ADD32(MULT16_16((a),SHR((b),16)), PSHR(MULT16_16SU((a),((b)&0x0000ffff)),16)) cannam@154: #endif cannam@154: cannam@154: /** 16x32 multiplication, followed by a 15-bit shift right. Results fits in 32 bits */ cannam@154: #if OPUS_FAST_INT64 cannam@154: #define MULT16_32_Q15(a,b) ((opus_val32)SHR((opus_int64)((opus_val16)(a))*(b),15)) cannam@154: #else cannam@154: #define MULT16_32_Q15(a,b) ADD32(SHL(MULT16_16((a),SHR((b),16)),1), SHR(MULT16_16SU((a),((b)&0x0000ffff)),15)) cannam@154: #endif cannam@154: cannam@154: /** 32x32 multiplication, followed by a 31-bit shift right. Results fits in 32 bits */ cannam@154: #if OPUS_FAST_INT64 cannam@154: #define MULT32_32_Q31(a,b) ((opus_val32)SHR((opus_int64)(a)*(opus_int64)(b),31)) cannam@154: #else cannam@154: #define MULT32_32_Q31(a,b) ADD32(ADD32(SHL(MULT16_16(SHR((a),16),SHR((b),16)),1), SHR(MULT16_16SU(SHR((a),16),((b)&0x0000ffff)),15)), SHR(MULT16_16SU(SHR((b),16),((a)&0x0000ffff)),15)) cannam@154: #endif cannam@154: cannam@154: /** Compile-time conversion of float constant to 16-bit value */ cannam@154: #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits)))) cannam@154: cannam@154: /** Compile-time conversion of float constant to 32-bit value */ cannam@154: #define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits)))) cannam@154: cannam@154: /** Negate a 16-bit value */ cannam@154: #define NEG16(x) (-(x)) cannam@154: /** Negate a 32-bit value */ cannam@154: #define NEG32(x) (-(x)) cannam@154: cannam@154: /** Change a 32-bit value into a 16-bit value. The value is assumed to fit in 16-bit, otherwise the result is undefined */ cannam@154: #define EXTRACT16(x) ((opus_val16)(x)) cannam@154: /** Change a 16-bit value into a 32-bit value */ cannam@154: #define EXTEND32(x) ((opus_val32)(x)) cannam@154: cannam@154: /** Arithmetic shift-right of a 16-bit value */ cannam@154: #define SHR16(a,shift) ((a) >> (shift)) cannam@154: /** Arithmetic shift-left of a 16-bit value */ cannam@154: #define SHL16(a,shift) ((opus_int16)((opus_uint16)(a)<<(shift))) cannam@154: /** Arithmetic shift-right of a 32-bit value */ cannam@154: #define SHR32(a,shift) ((a) >> (shift)) cannam@154: /** Arithmetic shift-left of a 32-bit value */ cannam@154: #define SHL32(a,shift) ((opus_int32)((opus_uint32)(a)<<(shift))) cannam@154: cannam@154: /** 32-bit arithmetic shift right with rounding-to-nearest instead of rounding down */ cannam@154: #define PSHR32(a,shift) (SHR32((a)+((EXTEND32(1)<<((shift))>>1)),shift)) cannam@154: /** 32-bit arithmetic shift right where the argument can be negative */ cannam@154: #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift))) cannam@154: cannam@154: /** "RAW" macros, should not be used outside of this header file */ cannam@154: #define SHR(a,shift) ((a) >> (shift)) cannam@154: #define SHL(a,shift) SHL32(a,shift) cannam@154: #define PSHR(a,shift) (SHR((a)+((EXTEND32(1)<<((shift))>>1)),shift)) cannam@154: #define SATURATE(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) cannam@154: cannam@154: #define SATURATE16(x) (EXTRACT16((x)>32767 ? 32767 : (x)<-32768 ? -32768 : (x))) cannam@154: cannam@154: /** Shift by a and round-to-neareast 32-bit value. Result is a 16-bit value */ cannam@154: #define ROUND16(x,a) (EXTRACT16(PSHR32((x),(a)))) cannam@154: /** Shift by a and round-to-neareast 32-bit value. Result is a saturated 16-bit value */ cannam@154: #define SROUND16(x,a) EXTRACT16(SATURATE(PSHR32(x,a), 32767)); cannam@154: cannam@154: /** Divide by two */ cannam@154: #define HALF16(x) (SHR16(x,1)) cannam@154: #define HALF32(x) (SHR32(x,1)) cannam@154: cannam@154: /** Add two 16-bit values */ cannam@154: #define ADD16(a,b) ((opus_val16)((opus_val16)(a)+(opus_val16)(b))) cannam@154: /** Subtract two 16-bit values */ cannam@154: #define SUB16(a,b) ((opus_val16)(a)-(opus_val16)(b)) cannam@154: /** Add two 32-bit values */ cannam@154: #define ADD32(a,b) ((opus_val32)(a)+(opus_val32)(b)) cannam@154: /** Subtract two 32-bit values */ cannam@154: #define SUB32(a,b) ((opus_val32)(a)-(opus_val32)(b)) cannam@154: cannam@154: /** Add two 32-bit values, ignore any overflows */ cannam@154: #define ADD32_ovflw(a,b) ((opus_val32)((opus_uint32)(a)+(opus_uint32)(b))) cannam@154: /** Subtract two 32-bit values, ignore any overflows */ cannam@154: #define SUB32_ovflw(a,b) ((opus_val32)((opus_uint32)(a)-(opus_uint32)(b))) cannam@154: /* Avoid MSVC warning C4146: unary minus operator applied to unsigned type */ cannam@154: /** Negate 32-bit value, ignore any overflows */ cannam@154: #define NEG32_ovflw(a) ((opus_val32)(0-(opus_uint32)(a))) cannam@154: cannam@154: /** 16x16 multiplication where the result fits in 16 bits */ cannam@154: #define MULT16_16_16(a,b) ((((opus_val16)(a))*((opus_val16)(b)))) cannam@154: cannam@154: /* (opus_val32)(opus_val16) gives TI compiler a hint that it's 16x16->32 multiply */ cannam@154: /** 16x16 multiplication where the result fits in 32 bits */ cannam@154: #define MULT16_16(a,b) (((opus_val32)(opus_val16)(a))*((opus_val32)(opus_val16)(b))) cannam@154: cannam@154: /** 16x16 multiply-add where the result fits in 32 bits */ cannam@154: #define MAC16_16(c,a,b) (ADD32((c),MULT16_16((a),(b)))) cannam@154: /** 16x32 multiply, followed by a 15-bit shift right and 32-bit add. cannam@154: b must fit in 31 bits. cannam@154: Result fits in 32 bits. */ cannam@154: #define MAC16_32_Q15(c,a,b) ADD32((c),ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15))) cannam@154: cannam@154: /** 16x32 multiplication, followed by a 16-bit shift right and 32-bit add. cannam@154: Results fits in 32 bits */ cannam@154: #define MAC16_32_Q16(c,a,b) ADD32((c),ADD32(MULT16_16((a),SHR((b),16)), SHR(MULT16_16SU((a),((b)&0x0000ffff)),16))) cannam@154: cannam@154: #define MULT16_16_Q11_32(a,b) (SHR(MULT16_16((a),(b)),11)) cannam@154: #define MULT16_16_Q11(a,b) (SHR(MULT16_16((a),(b)),11)) cannam@154: #define MULT16_16_Q13(a,b) (SHR(MULT16_16((a),(b)),13)) cannam@154: #define MULT16_16_Q14(a,b) (SHR(MULT16_16((a),(b)),14)) cannam@154: #define MULT16_16_Q15(a,b) (SHR(MULT16_16((a),(b)),15)) cannam@154: cannam@154: #define MULT16_16_P13(a,b) (SHR(ADD32(4096,MULT16_16((a),(b))),13)) cannam@154: #define MULT16_16_P14(a,b) (SHR(ADD32(8192,MULT16_16((a),(b))),14)) cannam@154: #define MULT16_16_P15(a,b) (SHR(ADD32(16384,MULT16_16((a),(b))),15)) cannam@154: cannam@154: /** Divide a 32-bit value by a 16-bit value. Result fits in 16 bits */ cannam@154: #define DIV32_16(a,b) ((opus_val16)(((opus_val32)(a))/((opus_val16)(b)))) cannam@154: cannam@154: /** Divide a 32-bit value by a 32-bit value. Result fits in 32 bits */ cannam@154: #define DIV32(a,b) (((opus_val32)(a))/((opus_val32)(b))) cannam@154: cannam@154: #if defined(MIPSr1_ASM) cannam@154: #include "mips/fixed_generic_mipsr1.h" cannam@154: #endif cannam@154: cannam@154: static OPUS_INLINE opus_val16 SIG2WORD16_generic(celt_sig x) cannam@154: { cannam@154: x = PSHR32(x, SIG_SHIFT); cannam@154: x = MAX32(x, -32768); cannam@154: x = MIN32(x, 32767); cannam@154: return EXTRACT16(x); cannam@154: } cannam@154: #define SIG2WORD16(x) (SIG2WORD16_generic(x)) cannam@154: cannam@154: #endif