media/libopus/celt/fixed_generic.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Copyright (C) 2007-2009 Xiph.Org Foundation
michael@0 2 Copyright (C) 2003-2008 Jean-Marc Valin
michael@0 3 Copyright (C) 2007-2008 CSIRO */
michael@0 4 /**
michael@0 5 @file fixed_generic.h
michael@0 6 @brief Generic fixed-point operations
michael@0 7 */
michael@0 8 /*
michael@0 9 Redistribution and use in source and binary forms, with or without
michael@0 10 modification, are permitted provided that the following conditions
michael@0 11 are met:
michael@0 12
michael@0 13 - Redistributions of source code must retain the above copyright
michael@0 14 notice, this list of conditions and the following disclaimer.
michael@0 15
michael@0 16 - Redistributions in binary form must reproduce the above copyright
michael@0 17 notice, this list of conditions and the following disclaimer in the
michael@0 18 documentation and/or other materials provided with the distribution.
michael@0 19
michael@0 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
michael@0 24 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
michael@0 28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
michael@0 29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
michael@0 30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31 */
michael@0 32
michael@0 33 #ifndef FIXED_GENERIC_H
michael@0 34 #define FIXED_GENERIC_H
michael@0 35
michael@0 36 /** Multiply a 16-bit signed value by a 16-bit unsigned value. The result is a 32-bit signed value */
michael@0 37 #define MULT16_16SU(a,b) ((opus_val32)(opus_val16)(a)*(opus_val32)(opus_uint16)(b))
michael@0 38
michael@0 39 /** 16x32 multiplication, followed by a 16-bit shift right. Results fits in 32 bits */
michael@0 40 #define MULT16_32_Q16(a,b) ADD32(MULT16_16((a),SHR((b),16)), SHR(MULT16_16SU((a),((b)&0x0000ffff)),16))
michael@0 41
michael@0 42 /** 16x32 multiplication, followed by a 16-bit shift right (round-to-nearest). Results fits in 32 bits */
michael@0 43 #define MULT16_32_P16(a,b) ADD32(MULT16_16((a),SHR((b),16)), PSHR(MULT16_16SU((a),((b)&0x0000ffff)),16))
michael@0 44
michael@0 45 /** 16x32 multiplication, followed by a 15-bit shift right. Results fits in 32 bits */
michael@0 46 #define MULT16_32_Q15(a,b) ADD32(SHL(MULT16_16((a),SHR((b),16)),1), SHR(MULT16_16SU((a),((b)&0x0000ffff)),15))
michael@0 47
michael@0 48 /** 32x32 multiplication, followed by a 31-bit shift right. Results fits in 32 bits */
michael@0 49 #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))
michael@0 50
michael@0 51 /** Compile-time conversion of float constant to 16-bit value */
michael@0 52 #define QCONST16(x,bits) ((opus_val16)(.5+(x)*(((opus_val32)1)<<(bits))))
michael@0 53
michael@0 54 /** Compile-time conversion of float constant to 32-bit value */
michael@0 55 #define QCONST32(x,bits) ((opus_val32)(.5+(x)*(((opus_val32)1)<<(bits))))
michael@0 56
michael@0 57 /** Negate a 16-bit value */
michael@0 58 #define NEG16(x) (-(x))
michael@0 59 /** Negate a 32-bit value */
michael@0 60 #define NEG32(x) (-(x))
michael@0 61
michael@0 62 /** Change a 32-bit value into a 16-bit value. The value is assumed to fit in 16-bit, otherwise the result is undefined */
michael@0 63 #define EXTRACT16(x) ((opus_val16)(x))
michael@0 64 /** Change a 16-bit value into a 32-bit value */
michael@0 65 #define EXTEND32(x) ((opus_val32)(x))
michael@0 66
michael@0 67 /** Arithmetic shift-right of a 16-bit value */
michael@0 68 #define SHR16(a,shift) ((a) >> (shift))
michael@0 69 /** Arithmetic shift-left of a 16-bit value */
michael@0 70 #define SHL16(a,shift) ((opus_int16)((opus_uint16)(a)<<(shift)))
michael@0 71 /** Arithmetic shift-right of a 32-bit value */
michael@0 72 #define SHR32(a,shift) ((a) >> (shift))
michael@0 73 /** Arithmetic shift-left of a 32-bit value */
michael@0 74 #define SHL32(a,shift) ((opus_int32)((opus_uint32)(a)<<(shift)))
michael@0 75
michael@0 76 /** 32-bit arithmetic shift right with rounding-to-nearest instead of rounding down */
michael@0 77 #define PSHR32(a,shift) (SHR32((a)+((EXTEND32(1)<<((shift))>>1)),shift))
michael@0 78 /** 32-bit arithmetic shift right where the argument can be negative */
michael@0 79 #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))
michael@0 80
michael@0 81 /** "RAW" macros, should not be used outside of this header file */
michael@0 82 #define SHR(a,shift) ((a) >> (shift))
michael@0 83 #define SHL(a,shift) SHL32(a,shift)
michael@0 84 #define PSHR(a,shift) (SHR((a)+((EXTEND32(1)<<((shift))>>1)),shift))
michael@0 85 #define SATURATE(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
michael@0 86
michael@0 87 #define SATURATE16(x) (EXTRACT16((x)>32767 ? 32767 : (x)<-32768 ? -32768 : (x)))
michael@0 88
michael@0 89 /** Shift by a and round-to-neareast 32-bit value. Result is a 16-bit value */
michael@0 90 #define ROUND16(x,a) (EXTRACT16(PSHR32((x),(a))))
michael@0 91 /** Divide by two */
michael@0 92 #define HALF16(x) (SHR16(x,1))
michael@0 93 #define HALF32(x) (SHR32(x,1))
michael@0 94
michael@0 95 /** Add two 16-bit values */
michael@0 96 #define ADD16(a,b) ((opus_val16)((opus_val16)(a)+(opus_val16)(b)))
michael@0 97 /** Subtract two 16-bit values */
michael@0 98 #define SUB16(a,b) ((opus_val16)(a)-(opus_val16)(b))
michael@0 99 /** Add two 32-bit values */
michael@0 100 #define ADD32(a,b) ((opus_val32)(a)+(opus_val32)(b))
michael@0 101 /** Subtract two 32-bit values */
michael@0 102 #define SUB32(a,b) ((opus_val32)(a)-(opus_val32)(b))
michael@0 103
michael@0 104 /** 16x16 multiplication where the result fits in 16 bits */
michael@0 105 #define MULT16_16_16(a,b) ((((opus_val16)(a))*((opus_val16)(b))))
michael@0 106
michael@0 107 /* (opus_val32)(opus_val16) gives TI compiler a hint that it's 16x16->32 multiply */
michael@0 108 /** 16x16 multiplication where the result fits in 32 bits */
michael@0 109 #define MULT16_16(a,b) (((opus_val32)(opus_val16)(a))*((opus_val32)(opus_val16)(b)))
michael@0 110
michael@0 111 /** 16x16 multiply-add where the result fits in 32 bits */
michael@0 112 #define MAC16_16(c,a,b) (ADD32((c),MULT16_16((a),(b))))
michael@0 113 /** 16x32 multiply, followed by a 15-bit shift right and 32-bit add.
michael@0 114 b must fit in 31 bits.
michael@0 115 Result fits in 32 bits. */
michael@0 116 #define MAC16_32_Q15(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15)))
michael@0 117
michael@0 118 #define MULT16_16_Q11_32(a,b) (SHR(MULT16_16((a),(b)),11))
michael@0 119 #define MULT16_16_Q11(a,b) (SHR(MULT16_16((a),(b)),11))
michael@0 120 #define MULT16_16_Q13(a,b) (SHR(MULT16_16((a),(b)),13))
michael@0 121 #define MULT16_16_Q14(a,b) (SHR(MULT16_16((a),(b)),14))
michael@0 122 #define MULT16_16_Q15(a,b) (SHR(MULT16_16((a),(b)),15))
michael@0 123
michael@0 124 #define MULT16_16_P13(a,b) (SHR(ADD32(4096,MULT16_16((a),(b))),13))
michael@0 125 #define MULT16_16_P14(a,b) (SHR(ADD32(8192,MULT16_16((a),(b))),14))
michael@0 126 #define MULT16_16_P15(a,b) (SHR(ADD32(16384,MULT16_16((a),(b))),15))
michael@0 127
michael@0 128 /** Divide a 32-bit value by a 16-bit value. Result fits in 16 bits */
michael@0 129 #define DIV32_16(a,b) ((opus_val16)(((opus_val32)(a))/((opus_val16)(b))))
michael@0 130
michael@0 131 /** Divide a 32-bit value by a 32-bit value. Result fits in 32 bits */
michael@0 132 #define DIV32(a,b) (((opus_val32)(a))/((opus_val32)(b)))
michael@0 133
michael@0 134 #endif

mercurial