Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Copyright (C) 2003 Jean-Marc Valin */ |
michael@0 | 2 | /** |
michael@0 | 3 | @file fixed_generic.h |
michael@0 | 4 | @brief Generic fixed-point operations |
michael@0 | 5 | */ |
michael@0 | 6 | /* |
michael@0 | 7 | Redistribution and use in source and binary forms, with or without |
michael@0 | 8 | modification, are permitted provided that the following conditions |
michael@0 | 9 | are met: |
michael@0 | 10 | |
michael@0 | 11 | - Redistributions of source code must retain the above copyright |
michael@0 | 12 | notice, this list of conditions and the following disclaimer. |
michael@0 | 13 | |
michael@0 | 14 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 15 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 16 | documentation and/or other materials provided with the distribution. |
michael@0 | 17 | |
michael@0 | 18 | - Neither the name of the Xiph.org Foundation nor the names of its |
michael@0 | 19 | contributors may be used to endorse or promote products derived from |
michael@0 | 20 | this software without specific prior written permission. |
michael@0 | 21 | |
michael@0 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 23 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 25 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
michael@0 | 26 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 27 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 28 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 30 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 31 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 33 | */ |
michael@0 | 34 | |
michael@0 | 35 | #ifndef FIXED_GENERIC_H |
michael@0 | 36 | #define FIXED_GENERIC_H |
michael@0 | 37 | |
michael@0 | 38 | #define QCONST16(x,bits) ((spx_word16_t)(.5+(x)*(((spx_word32_t)1)<<(bits)))) |
michael@0 | 39 | #define QCONST32(x,bits) ((spx_word32_t)(.5+(x)*(((spx_word32_t)1)<<(bits)))) |
michael@0 | 40 | |
michael@0 | 41 | #define NEG16(x) (-(x)) |
michael@0 | 42 | #define NEG32(x) (-(x)) |
michael@0 | 43 | #define EXTRACT16(x) ((spx_word16_t)(x)) |
michael@0 | 44 | #define EXTEND32(x) ((spx_word32_t)(x)) |
michael@0 | 45 | #define SHR16(a,shift) ((a) >> (shift)) |
michael@0 | 46 | #define SHL16(a,shift) ((a) << (shift)) |
michael@0 | 47 | #define SHR32(a,shift) ((a) >> (shift)) |
michael@0 | 48 | #define SHL32(a,shift) ((a) << (shift)) |
michael@0 | 49 | #define PSHR16(a,shift) (SHR16((a)+((1<<((shift))>>1)),shift)) |
michael@0 | 50 | #define PSHR32(a,shift) (SHR32((a)+((EXTEND32(1)<<((shift))>>1)),shift)) |
michael@0 | 51 | #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift))) |
michael@0 | 52 | #define SATURATE16(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) |
michael@0 | 53 | #define SATURATE32(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) |
michael@0 | 54 | |
michael@0 | 55 | #define SHR(a,shift) ((a) >> (shift)) |
michael@0 | 56 | #define SHL(a,shift) ((spx_word32_t)(a) << (shift)) |
michael@0 | 57 | #define PSHR(a,shift) (SHR((a)+((EXTEND32(1)<<((shift))>>1)),shift)) |
michael@0 | 58 | #define SATURATE(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) |
michael@0 | 59 | |
michael@0 | 60 | |
michael@0 | 61 | #define ADD16(a,b) ((spx_word16_t)((spx_word16_t)(a)+(spx_word16_t)(b))) |
michael@0 | 62 | #define SUB16(a,b) ((spx_word16_t)(a)-(spx_word16_t)(b)) |
michael@0 | 63 | #define ADD32(a,b) ((spx_word32_t)(a)+(spx_word32_t)(b)) |
michael@0 | 64 | #define SUB32(a,b) ((spx_word32_t)(a)-(spx_word32_t)(b)) |
michael@0 | 65 | |
michael@0 | 66 | |
michael@0 | 67 | /* result fits in 16 bits */ |
michael@0 | 68 | #define MULT16_16_16(a,b) ((((spx_word16_t)(a))*((spx_word16_t)(b)))) |
michael@0 | 69 | |
michael@0 | 70 | /* (spx_word32_t)(spx_word16_t) gives TI compiler a hint that it's 16x16->32 multiply */ |
michael@0 | 71 | #define MULT16_16(a,b) (((spx_word32_t)(spx_word16_t)(a))*((spx_word32_t)(spx_word16_t)(b))) |
michael@0 | 72 | |
michael@0 | 73 | #define MAC16_16(c,a,b) (ADD32((c),MULT16_16((a),(b)))) |
michael@0 | 74 | #define MULT16_32_Q12(a,b) ADD32(MULT16_16((a),SHR((b),12)), SHR(MULT16_16((a),((b)&0x00000fff)),12)) |
michael@0 | 75 | #define MULT16_32_Q13(a,b) ADD32(MULT16_16((a),SHR((b),13)), SHR(MULT16_16((a),((b)&0x00001fff)),13)) |
michael@0 | 76 | #define MULT16_32_Q14(a,b) ADD32(MULT16_16((a),SHR((b),14)), SHR(MULT16_16((a),((b)&0x00003fff)),14)) |
michael@0 | 77 | |
michael@0 | 78 | #define MULT16_32_Q11(a,b) ADD32(MULT16_16((a),SHR((b),11)), SHR(MULT16_16((a),((b)&0x000007ff)),11)) |
michael@0 | 79 | #define MAC16_32_Q11(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),11)), SHR(MULT16_16((a),((b)&0x000007ff)),11))) |
michael@0 | 80 | |
michael@0 | 81 | #define MULT16_32_P15(a,b) ADD32(MULT16_16((a),SHR((b),15)), PSHR(MULT16_16((a),((b)&0x00007fff)),15)) |
michael@0 | 82 | #define MULT16_32_Q15(a,b) ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15)) |
michael@0 | 83 | #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 | 84 | |
michael@0 | 85 | |
michael@0 | 86 | #define MAC16_16_Q11(c,a,b) (ADD32((c),SHR(MULT16_16((a),(b)),11))) |
michael@0 | 87 | #define MAC16_16_Q13(c,a,b) (ADD32((c),SHR(MULT16_16((a),(b)),13))) |
michael@0 | 88 | #define MAC16_16_P13(c,a,b) (ADD32((c),SHR(ADD32(4096,MULT16_16((a),(b))),13))) |
michael@0 | 89 | |
michael@0 | 90 | #define MULT16_16_Q11_32(a,b) (SHR(MULT16_16((a),(b)),11)) |
michael@0 | 91 | #define MULT16_16_Q13(a,b) (SHR(MULT16_16((a),(b)),13)) |
michael@0 | 92 | #define MULT16_16_Q14(a,b) (SHR(MULT16_16((a),(b)),14)) |
michael@0 | 93 | #define MULT16_16_Q15(a,b) (SHR(MULT16_16((a),(b)),15)) |
michael@0 | 94 | |
michael@0 | 95 | #define MULT16_16_P13(a,b) (SHR(ADD32(4096,MULT16_16((a),(b))),13)) |
michael@0 | 96 | #define MULT16_16_P14(a,b) (SHR(ADD32(8192,MULT16_16((a),(b))),14)) |
michael@0 | 97 | #define MULT16_16_P15(a,b) (SHR(ADD32(16384,MULT16_16((a),(b))),15)) |
michael@0 | 98 | |
michael@0 | 99 | #define MUL_16_32_R15(a,bh,bl) ADD32(MULT16_16((a),(bh)), SHR(MULT16_16((a),(bl)),15)) |
michael@0 | 100 | |
michael@0 | 101 | #define DIV32_16(a,b) ((spx_word16_t)(((spx_word32_t)(a))/((spx_word16_t)(b)))) |
michael@0 | 102 | #define PDIV32_16(a,b) ((spx_word16_t)(((spx_word32_t)(a)+((spx_word16_t)(b)>>1))/((spx_word16_t)(b)))) |
michael@0 | 103 | #define DIV32(a,b) (((spx_word32_t)(a))/((spx_word32_t)(b))) |
michael@0 | 104 | #define PDIV32(a,b) (((spx_word32_t)(a)+((spx_word16_t)(b)>>1))/((spx_word32_t)(b))) |
michael@0 | 105 | |
michael@0 | 106 | #endif |