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