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