ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
TMBOY
Date:
Tue Jul 18 16:54:45 2017 +0800
Revision:
47:9e361da97763
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMBOY 47:9e361da97763 1 /* Copyright (C) 2003 Jean-Marc Valin */
TMBOY 47:9e361da97763 2 /**
TMBOY 47:9e361da97763 3 @file fixed_debug.h
TMBOY 47:9e361da97763 4 @brief Fixed-point operations with debugging
TMBOY 47:9e361da97763 5 */
TMBOY 47:9e361da97763 6 /*
TMBOY 47:9e361da97763 7 Redistribution and use in source and binary forms, with or without
TMBOY 47:9e361da97763 8 modification, are permitted provided that the following conditions
TMBOY 47:9e361da97763 9 are met:
TMBOY 47:9e361da97763 10
TMBOY 47:9e361da97763 11 - Redistributions of source code must retain the above copyright
TMBOY 47:9e361da97763 12 notice, this list of conditions and the following disclaimer.
TMBOY 47:9e361da97763 13
TMBOY 47:9e361da97763 14 - Redistributions in binary form must reproduce the above copyright
TMBOY 47:9e361da97763 15 notice, this list of conditions and the following disclaimer in the
TMBOY 47:9e361da97763 16 documentation and/or other materials provided with the distribution.
TMBOY 47:9e361da97763 17
TMBOY 47:9e361da97763 18 - Neither the name of the Xiph.org Foundation nor the names of its
TMBOY 47:9e361da97763 19 contributors may be used to endorse or promote products derived from
TMBOY 47:9e361da97763 20 this software without specific prior written permission.
TMBOY 47:9e361da97763 21
TMBOY 47:9e361da97763 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
TMBOY 47:9e361da97763 23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
TMBOY 47:9e361da97763 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
TMBOY 47:9e361da97763 25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
TMBOY 47:9e361da97763 26 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
TMBOY 47:9e361da97763 27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
TMBOY 47:9e361da97763 28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
TMBOY 47:9e361da97763 29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
TMBOY 47:9e361da97763 30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
TMBOY 47:9e361da97763 31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
TMBOY 47:9e361da97763 32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
TMBOY 47:9e361da97763 33 */
TMBOY 47:9e361da97763 34
TMBOY 47:9e361da97763 35 #ifndef FIXED_DEBUG_H
TMBOY 47:9e361da97763 36 #define FIXED_DEBUG_H
TMBOY 47:9e361da97763 37
TMBOY 47:9e361da97763 38 #include <stdio.h>
TMBOY 47:9e361da97763 39
TMBOY 47:9e361da97763 40 extern long long spx_mips;
TMBOY 47:9e361da97763 41 #define MIPS_INC spx_mips++,
TMBOY 47:9e361da97763 42
TMBOY 47:9e361da97763 43 #define QCONST16(x,bits) ((spx_word16_t)(.5+(x)*(((spx_word32_t)1)<<(bits))))
TMBOY 47:9e361da97763 44 #define QCONST32(x,bits) ((spx_word32_t)(.5+(x)*(((spx_word32_t)1)<<(bits))))
TMBOY 47:9e361da97763 45
TMBOY 47:9e361da97763 46
TMBOY 47:9e361da97763 47 #define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768)
TMBOY 47:9e361da97763 48 #define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL)
TMBOY 47:9e361da97763 49
TMBOY 47:9e361da97763 50 static inline short NEG16(int x)
TMBOY 47:9e361da97763 51 {
TMBOY 47:9e361da97763 52 int res;
TMBOY 47:9e361da97763 53 if (!VERIFY_SHORT(x))
TMBOY 47:9e361da97763 54 {
TMBOY 47:9e361da97763 55 fprintf (stderr, "NEG16: input is not short: %d\n", (int)x);
TMBOY 47:9e361da97763 56 }
TMBOY 47:9e361da97763 57 res = -x;
TMBOY 47:9e361da97763 58 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 59 fprintf (stderr, "NEG16: output is not short: %d\n", (int)res);
TMBOY 47:9e361da97763 60 spx_mips++;
TMBOY 47:9e361da97763 61 return res;
TMBOY 47:9e361da97763 62 }
TMBOY 47:9e361da97763 63 static inline int NEG32(long long x)
TMBOY 47:9e361da97763 64 {
TMBOY 47:9e361da97763 65 long long res;
TMBOY 47:9e361da97763 66 if (!VERIFY_INT(x))
TMBOY 47:9e361da97763 67 {
TMBOY 47:9e361da97763 68 fprintf (stderr, "NEG16: input is not int: %d\n", (int)x);
TMBOY 47:9e361da97763 69 }
TMBOY 47:9e361da97763 70 res = -x;
TMBOY 47:9e361da97763 71 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 72 fprintf (stderr, "NEG16: output is not int: %d\n", (int)res);
TMBOY 47:9e361da97763 73 spx_mips++;
TMBOY 47:9e361da97763 74 return res;
TMBOY 47:9e361da97763 75 }
TMBOY 47:9e361da97763 76
TMBOY 47:9e361da97763 77 #define EXTRACT16(x) _EXTRACT16(x, __FILE__, __LINE__)
TMBOY 47:9e361da97763 78 static inline short _EXTRACT16(int x, char *file, int line)
TMBOY 47:9e361da97763 79 {
TMBOY 47:9e361da97763 80 int res;
TMBOY 47:9e361da97763 81 if (!VERIFY_SHORT(x))
TMBOY 47:9e361da97763 82 {
TMBOY 47:9e361da97763 83 fprintf (stderr, "EXTRACT16: input is not short: %d in %s: line %d\n", x, file, line);
TMBOY 47:9e361da97763 84 }
TMBOY 47:9e361da97763 85 res = x;
TMBOY 47:9e361da97763 86 spx_mips++;
TMBOY 47:9e361da97763 87 return res;
TMBOY 47:9e361da97763 88 }
TMBOY 47:9e361da97763 89
TMBOY 47:9e361da97763 90 #define EXTEND32(x) _EXTEND32(x, __FILE__, __LINE__)
TMBOY 47:9e361da97763 91 static inline int _EXTEND32(int x, char *file, int line)
TMBOY 47:9e361da97763 92 {
TMBOY 47:9e361da97763 93 int res;
TMBOY 47:9e361da97763 94 if (!VERIFY_SHORT(x))
TMBOY 47:9e361da97763 95 {
TMBOY 47:9e361da97763 96 fprintf (stderr, "EXTEND32: input is not short: %d in %s: line %d\n", x, file, line);
TMBOY 47:9e361da97763 97 }
TMBOY 47:9e361da97763 98 res = x;
TMBOY 47:9e361da97763 99 spx_mips++;
TMBOY 47:9e361da97763 100 return res;
TMBOY 47:9e361da97763 101 }
TMBOY 47:9e361da97763 102
TMBOY 47:9e361da97763 103 #define SHR16(a, shift) _SHR16(a, shift, __FILE__, __LINE__)
TMBOY 47:9e361da97763 104 static inline short _SHR16(int a, int shift, char *file, int line)
TMBOY 47:9e361da97763 105 {
TMBOY 47:9e361da97763 106 int res;
TMBOY 47:9e361da97763 107 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
TMBOY 47:9e361da97763 108 {
TMBOY 47:9e361da97763 109 fprintf (stderr, "SHR16: inputs are not short: %d >> %d in %s: line %d\n", a, shift, file, line);
TMBOY 47:9e361da97763 110 }
TMBOY 47:9e361da97763 111 res = a>>shift;
TMBOY 47:9e361da97763 112 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 113 fprintf (stderr, "SHR16: output is not short: %d in %s: line %d\n", res, file, line);
TMBOY 47:9e361da97763 114 spx_mips++;
TMBOY 47:9e361da97763 115 return res;
TMBOY 47:9e361da97763 116 }
TMBOY 47:9e361da97763 117 #define SHL16(a, shift) _SHL16(a, shift, __FILE__, __LINE__)
TMBOY 47:9e361da97763 118 static inline short _SHL16(int a, int shift, char *file, int line)
TMBOY 47:9e361da97763 119 {
TMBOY 47:9e361da97763 120 int res;
TMBOY 47:9e361da97763 121 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
TMBOY 47:9e361da97763 122 {
TMBOY 47:9e361da97763 123 fprintf (stderr, "SHL16: inputs are not short: %d %d in %s: line %d\n", a, shift, file, line);
TMBOY 47:9e361da97763 124 }
TMBOY 47:9e361da97763 125 res = a<<shift;
TMBOY 47:9e361da97763 126 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 127 fprintf (stderr, "SHL16: output is not short: %d in %s: line %d\n", res, file, line);
TMBOY 47:9e361da97763 128 spx_mips++;
TMBOY 47:9e361da97763 129 return res;
TMBOY 47:9e361da97763 130 }
TMBOY 47:9e361da97763 131
TMBOY 47:9e361da97763 132 static inline int SHR32(long long a, int shift)
TMBOY 47:9e361da97763 133 {
TMBOY 47:9e361da97763 134 long long res;
TMBOY 47:9e361da97763 135 if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
TMBOY 47:9e361da97763 136 {
TMBOY 47:9e361da97763 137 fprintf (stderr, "SHR32: inputs are not int: %d %d\n", (int)a, shift);
TMBOY 47:9e361da97763 138 }
TMBOY 47:9e361da97763 139 res = a>>shift;
TMBOY 47:9e361da97763 140 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 141 {
TMBOY 47:9e361da97763 142 fprintf (stderr, "SHR32: output is not int: %d\n", (int)res);
TMBOY 47:9e361da97763 143 }
TMBOY 47:9e361da97763 144 spx_mips++;
TMBOY 47:9e361da97763 145 return res;
TMBOY 47:9e361da97763 146 }
TMBOY 47:9e361da97763 147 static inline int SHL32(long long a, int shift)
TMBOY 47:9e361da97763 148 {
TMBOY 47:9e361da97763 149 long long res;
TMBOY 47:9e361da97763 150 if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
TMBOY 47:9e361da97763 151 {
TMBOY 47:9e361da97763 152 fprintf (stderr, "SHL32: inputs are not int: %d %d\n", (int)a, shift);
TMBOY 47:9e361da97763 153 }
TMBOY 47:9e361da97763 154 res = a<<shift;
TMBOY 47:9e361da97763 155 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 156 {
TMBOY 47:9e361da97763 157 fprintf (stderr, "SHL32: output is not int: %d\n", (int)res);
TMBOY 47:9e361da97763 158 }
TMBOY 47:9e361da97763 159 spx_mips++;
TMBOY 47:9e361da97763 160 return res;
TMBOY 47:9e361da97763 161 }
TMBOY 47:9e361da97763 162
TMBOY 47:9e361da97763 163 #define PSHR16(a,shift) (SHR16(ADD16((a),((1<<((shift))>>1))),shift))
TMBOY 47:9e361da97763 164 #define PSHR32(a,shift) (SHR32(ADD32((a),((1<<((shift))>>1))),shift))
TMBOY 47:9e361da97763 165 #define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))
TMBOY 47:9e361da97763 166
TMBOY 47:9e361da97763 167 #define SATURATE16(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
TMBOY 47:9e361da97763 168 #define SATURATE32(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
TMBOY 47:9e361da97763 169
TMBOY 47:9e361da97763 170 //#define SHR(a,shift) ((a) >> (shift))
TMBOY 47:9e361da97763 171 //#define SHL(a,shift) ((a) << (shift))
TMBOY 47:9e361da97763 172
TMBOY 47:9e361da97763 173 #define ADD16(a, b) _ADD16(a, b, __FILE__, __LINE__)
TMBOY 47:9e361da97763 174 static inline short _ADD16(int a, int b, char *file, int line)
TMBOY 47:9e361da97763 175 {
TMBOY 47:9e361da97763 176 int res;
TMBOY 47:9e361da97763 177 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 178 {
TMBOY 47:9e361da97763 179 fprintf (stderr, "ADD16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
TMBOY 47:9e361da97763 180 }
TMBOY 47:9e361da97763 181 res = a+b;
TMBOY 47:9e361da97763 182 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 183 {
TMBOY 47:9e361da97763 184 fprintf (stderr, "ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line);
TMBOY 47:9e361da97763 185 }
TMBOY 47:9e361da97763 186 spx_mips++;
TMBOY 47:9e361da97763 187 return res;
TMBOY 47:9e361da97763 188 }
TMBOY 47:9e361da97763 189
TMBOY 47:9e361da97763 190 #define SUB16(a, b) _SUB16(a, b, __FILE__, __LINE__)
TMBOY 47:9e361da97763 191 static inline short _SUB16(int a, int b, char *file, int line)
TMBOY 47:9e361da97763 192 {
TMBOY 47:9e361da97763 193 int res;
TMBOY 47:9e361da97763 194 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 195 {
TMBOY 47:9e361da97763 196 fprintf (stderr, "SUB16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
TMBOY 47:9e361da97763 197 }
TMBOY 47:9e361da97763 198 res = a-b;
TMBOY 47:9e361da97763 199 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 200 fprintf (stderr, "SUB16: output is not short: %d in %s: line %d\n", res, file, line);
TMBOY 47:9e361da97763 201 spx_mips++;
TMBOY 47:9e361da97763 202 return res;
TMBOY 47:9e361da97763 203 }
TMBOY 47:9e361da97763 204
TMBOY 47:9e361da97763 205 #define ADD32(a, b) _ADD32(a, b, __FILE__, __LINE__)
TMBOY 47:9e361da97763 206 static inline int _ADD32(long long a, long long b, char *file, int line)
TMBOY 47:9e361da97763 207 {
TMBOY 47:9e361da97763 208 long long res;
TMBOY 47:9e361da97763 209 if (!VERIFY_INT(a) || !VERIFY_INT(b))
TMBOY 47:9e361da97763 210 {
TMBOY 47:9e361da97763 211 fprintf (stderr, "ADD32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
TMBOY 47:9e361da97763 212 }
TMBOY 47:9e361da97763 213 res = a+b;
TMBOY 47:9e361da97763 214 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 215 {
TMBOY 47:9e361da97763 216 fprintf (stderr, "ADD32: output is not int: %d in %s: line %d\n", (int)res, file, line);
TMBOY 47:9e361da97763 217 }
TMBOY 47:9e361da97763 218 spx_mips++;
TMBOY 47:9e361da97763 219 return res;
TMBOY 47:9e361da97763 220 }
TMBOY 47:9e361da97763 221
TMBOY 47:9e361da97763 222 static inline int SUB32(long long a, long long b)
TMBOY 47:9e361da97763 223 {
TMBOY 47:9e361da97763 224 long long res;
TMBOY 47:9e361da97763 225 if (!VERIFY_INT(a) || !VERIFY_INT(b))
TMBOY 47:9e361da97763 226 {
TMBOY 47:9e361da97763 227 fprintf (stderr, "SUB32: inputs are not int: %d %d\n", (int)a, (int)b);
TMBOY 47:9e361da97763 228 }
TMBOY 47:9e361da97763 229 res = a-b;
TMBOY 47:9e361da97763 230 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 231 fprintf (stderr, "SUB32: output is not int: %d\n", (int)res);
TMBOY 47:9e361da97763 232 spx_mips++;
TMBOY 47:9e361da97763 233 return res;
TMBOY 47:9e361da97763 234 }
TMBOY 47:9e361da97763 235
TMBOY 47:9e361da97763 236 #define ADD64(a,b) (MIPS_INC(a)+(b))
TMBOY 47:9e361da97763 237
TMBOY 47:9e361da97763 238 /* result fits in 16 bits */
TMBOY 47:9e361da97763 239 static inline short MULT16_16_16(int a, int b)
TMBOY 47:9e361da97763 240 {
TMBOY 47:9e361da97763 241 int res;
TMBOY 47:9e361da97763 242 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 243 {
TMBOY 47:9e361da97763 244 fprintf (stderr, "MULT16_16_16: inputs are not short: %d %d\n", a, b);
TMBOY 47:9e361da97763 245 }
TMBOY 47:9e361da97763 246 res = a*b;
TMBOY 47:9e361da97763 247 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 248 fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res);
TMBOY 47:9e361da97763 249 spx_mips++;
TMBOY 47:9e361da97763 250 return res;
TMBOY 47:9e361da97763 251 }
TMBOY 47:9e361da97763 252
TMBOY 47:9e361da97763 253 #define MULT16_16(a, b) _MULT16_16(a, b, __FILE__, __LINE__)
TMBOY 47:9e361da97763 254 static inline int _MULT16_16(int a, int b, char *file, int line)
TMBOY 47:9e361da97763 255 {
TMBOY 47:9e361da97763 256 long long res;
TMBOY 47:9e361da97763 257 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 258 {
TMBOY 47:9e361da97763 259 fprintf (stderr, "MULT16_16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
TMBOY 47:9e361da97763 260 }
TMBOY 47:9e361da97763 261 res = ((long long)a)*b;
TMBOY 47:9e361da97763 262 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 263 fprintf (stderr, "MULT16_16: output is not int: %d in %s: line %d\n", (int)res, file, line);
TMBOY 47:9e361da97763 264 spx_mips++;
TMBOY 47:9e361da97763 265 return res;
TMBOY 47:9e361da97763 266 }
TMBOY 47:9e361da97763 267
TMBOY 47:9e361da97763 268 #define MAC16_16(c,a,b) (spx_mips--,ADD32((c),MULT16_16((a),(b))))
TMBOY 47:9e361da97763 269 #define MAC16_16_Q11(c,a,b) (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),11)))))
TMBOY 47:9e361da97763 270 #define MAC16_16_Q13(c,a,b) (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),13)))))
TMBOY 47:9e361da97763 271 #define MAC16_16_P13(c,a,b) (EXTRACT16(ADD32((c),SHR32(ADD32(4096,MULT16_16((a),(b))),13))))
TMBOY 47:9e361da97763 272
TMBOY 47:9e361da97763 273
TMBOY 47:9e361da97763 274 #define MULT16_32_QX(a, b, Q) _MULT16_32_QX(a, b, Q, __FILE__, __LINE__)
TMBOY 47:9e361da97763 275 static inline int _MULT16_32_QX(int a, long long b, int Q, char *file, int line)
TMBOY 47:9e361da97763 276 {
TMBOY 47:9e361da97763 277 long long res;
TMBOY 47:9e361da97763 278 if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
TMBOY 47:9e361da97763 279 {
TMBOY 47:9e361da97763 280 fprintf (stderr, "MULT16_32_Q%d: inputs are not short+int: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line);
TMBOY 47:9e361da97763 281 }
TMBOY 47:9e361da97763 282 if (ABS32(b)>=(1<<(15+Q)))
TMBOY 47:9e361da97763 283 fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line);
TMBOY 47:9e361da97763 284 res = (((long long)a)*(long long)b) >> Q;
TMBOY 47:9e361da97763 285 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 286 fprintf (stderr, "MULT16_32_Q%d: output is not int: %d*%d=%d in %s: line %d\n", Q, (int)a, (int)b,(int)res, file, line);
TMBOY 47:9e361da97763 287 spx_mips+=5;
TMBOY 47:9e361da97763 288 return res;
TMBOY 47:9e361da97763 289 }
TMBOY 47:9e361da97763 290
TMBOY 47:9e361da97763 291 static inline int MULT16_32_PX(int a, long long b, int Q)
TMBOY 47:9e361da97763 292 {
TMBOY 47:9e361da97763 293 long long res;
TMBOY 47:9e361da97763 294 if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
TMBOY 47:9e361da97763 295 {
TMBOY 47:9e361da97763 296 fprintf (stderr, "MULT16_32_P%d: inputs are not short+int: %d %d\n", Q, (int)a, (int)b);
TMBOY 47:9e361da97763 297 }
TMBOY 47:9e361da97763 298 if (ABS32(b)>=(1<<(15+Q)))
TMBOY 47:9e361da97763 299 fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d\n", Q, (int)a, (int)b);
TMBOY 47:9e361da97763 300 res = ((((long long)a)*(long long)b) + ((1<<Q)>>1))>> Q;
TMBOY 47:9e361da97763 301 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 302 fprintf (stderr, "MULT16_32_P%d: output is not int: %d*%d=%d\n", Q, (int)a, (int)b,(int)res);
TMBOY 47:9e361da97763 303 spx_mips+=5;
TMBOY 47:9e361da97763 304 return res;
TMBOY 47:9e361da97763 305 }
TMBOY 47:9e361da97763 306
TMBOY 47:9e361da97763 307
TMBOY 47:9e361da97763 308 #define MULT16_32_Q11(a,b) MULT16_32_QX(a,b,11)
TMBOY 47:9e361da97763 309 #define MAC16_32_Q11(c,a,b) ADD32((c),MULT16_32_Q11((a),(b)))
TMBOY 47:9e361da97763 310 #define MULT16_32_Q12(a,b) MULT16_32_QX(a,b,12)
TMBOY 47:9e361da97763 311 #define MULT16_32_Q13(a,b) MULT16_32_QX(a,b,13)
TMBOY 47:9e361da97763 312 #define MULT16_32_Q14(a,b) MULT16_32_QX(a,b,14)
TMBOY 47:9e361da97763 313 #define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15)
TMBOY 47:9e361da97763 314 #define MULT16_32_P15(a,b) MULT16_32_PX(a,b,15)
TMBOY 47:9e361da97763 315 #define MAC16_32_Q15(c,a,b) ADD32((c),MULT16_32_Q15((a),(b)))
TMBOY 47:9e361da97763 316
TMBOY 47:9e361da97763 317 static inline int SATURATE(int a, int b)
TMBOY 47:9e361da97763 318 {
TMBOY 47:9e361da97763 319 if (a>b)
TMBOY 47:9e361da97763 320 a=b;
TMBOY 47:9e361da97763 321 if (a<-b)
TMBOY 47:9e361da97763 322 a = -b;
TMBOY 47:9e361da97763 323 return a;
TMBOY 47:9e361da97763 324 }
TMBOY 47:9e361da97763 325
TMBOY 47:9e361da97763 326 static inline int MULT16_16_Q11_32(int a, int b)
TMBOY 47:9e361da97763 327 {
TMBOY 47:9e361da97763 328 long long res;
TMBOY 47:9e361da97763 329 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 330 {
TMBOY 47:9e361da97763 331 fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b);
TMBOY 47:9e361da97763 332 }
TMBOY 47:9e361da97763 333 res = ((long long)a)*b;
TMBOY 47:9e361da97763 334 res >>= 11;
TMBOY 47:9e361da97763 335 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 336 fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res);
TMBOY 47:9e361da97763 337 spx_mips+=3;
TMBOY 47:9e361da97763 338 return res;
TMBOY 47:9e361da97763 339 }
TMBOY 47:9e361da97763 340 static inline short MULT16_16_Q13(int a, int b)
TMBOY 47:9e361da97763 341 {
TMBOY 47:9e361da97763 342 long long res;
TMBOY 47:9e361da97763 343 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 344 {
TMBOY 47:9e361da97763 345 fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b);
TMBOY 47:9e361da97763 346 }
TMBOY 47:9e361da97763 347 res = ((long long)a)*b;
TMBOY 47:9e361da97763 348 res >>= 13;
TMBOY 47:9e361da97763 349 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 350 fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res);
TMBOY 47:9e361da97763 351 spx_mips+=3;
TMBOY 47:9e361da97763 352 return res;
TMBOY 47:9e361da97763 353 }
TMBOY 47:9e361da97763 354 static inline short MULT16_16_Q14(int a, int b)
TMBOY 47:9e361da97763 355 {
TMBOY 47:9e361da97763 356 long long res;
TMBOY 47:9e361da97763 357 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 358 {
TMBOY 47:9e361da97763 359 fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b);
TMBOY 47:9e361da97763 360 }
TMBOY 47:9e361da97763 361 res = ((long long)a)*b;
TMBOY 47:9e361da97763 362 res >>= 14;
TMBOY 47:9e361da97763 363 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 364 fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res);
TMBOY 47:9e361da97763 365 spx_mips+=3;
TMBOY 47:9e361da97763 366 return res;
TMBOY 47:9e361da97763 367 }
TMBOY 47:9e361da97763 368 static inline short MULT16_16_Q15(int a, int b)
TMBOY 47:9e361da97763 369 {
TMBOY 47:9e361da97763 370 long long res;
TMBOY 47:9e361da97763 371 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 372 {
TMBOY 47:9e361da97763 373 fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d\n", a, b);
TMBOY 47:9e361da97763 374 }
TMBOY 47:9e361da97763 375 res = ((long long)a)*b;
TMBOY 47:9e361da97763 376 res >>= 15;
TMBOY 47:9e361da97763 377 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 378 {
TMBOY 47:9e361da97763 379 fprintf (stderr, "MULT16_16_Q15: output is not short: %d\n", (int)res);
TMBOY 47:9e361da97763 380 }
TMBOY 47:9e361da97763 381 spx_mips+=3;
TMBOY 47:9e361da97763 382 return res;
TMBOY 47:9e361da97763 383 }
TMBOY 47:9e361da97763 384
TMBOY 47:9e361da97763 385 static inline short MULT16_16_P13(int a, int b)
TMBOY 47:9e361da97763 386 {
TMBOY 47:9e361da97763 387 long long res;
TMBOY 47:9e361da97763 388 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 389 {
TMBOY 47:9e361da97763 390 fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b);
TMBOY 47:9e361da97763 391 }
TMBOY 47:9e361da97763 392 res = ((long long)a)*b;
TMBOY 47:9e361da97763 393 res += 4096;
TMBOY 47:9e361da97763 394 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 395 fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res);
TMBOY 47:9e361da97763 396 res >>= 13;
TMBOY 47:9e361da97763 397 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 398 fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res);
TMBOY 47:9e361da97763 399 spx_mips+=4;
TMBOY 47:9e361da97763 400 return res;
TMBOY 47:9e361da97763 401 }
TMBOY 47:9e361da97763 402 static inline short MULT16_16_P14(int a, int b)
TMBOY 47:9e361da97763 403 {
TMBOY 47:9e361da97763 404 long long res;
TMBOY 47:9e361da97763 405 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 406 {
TMBOY 47:9e361da97763 407 fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b);
TMBOY 47:9e361da97763 408 }
TMBOY 47:9e361da97763 409 res = ((long long)a)*b;
TMBOY 47:9e361da97763 410 res += 8192;
TMBOY 47:9e361da97763 411 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 412 fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res);
TMBOY 47:9e361da97763 413 res >>= 14;
TMBOY 47:9e361da97763 414 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 415 fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
TMBOY 47:9e361da97763 416 spx_mips+=4;
TMBOY 47:9e361da97763 417 return res;
TMBOY 47:9e361da97763 418 }
TMBOY 47:9e361da97763 419 static inline short MULT16_16_P15(int a, int b)
TMBOY 47:9e361da97763 420 {
TMBOY 47:9e361da97763 421 long long res;
TMBOY 47:9e361da97763 422 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 423 {
TMBOY 47:9e361da97763 424 fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b);
TMBOY 47:9e361da97763 425 }
TMBOY 47:9e361da97763 426 res = ((long long)a)*b;
TMBOY 47:9e361da97763 427 res += 16384;
TMBOY 47:9e361da97763 428 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 429 fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res);
TMBOY 47:9e361da97763 430 res >>= 15;
TMBOY 47:9e361da97763 431 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 432 fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res);
TMBOY 47:9e361da97763 433 spx_mips+=4;
TMBOY 47:9e361da97763 434 return res;
TMBOY 47:9e361da97763 435 }
TMBOY 47:9e361da97763 436
TMBOY 47:9e361da97763 437 #define DIV32_16(a, b) _DIV32_16(a, b, __FILE__, __LINE__)
TMBOY 47:9e361da97763 438
TMBOY 47:9e361da97763 439 static inline int _DIV32_16(long long a, long long b, char *file, int line)
TMBOY 47:9e361da97763 440 {
TMBOY 47:9e361da97763 441 long long res;
TMBOY 47:9e361da97763 442 if (b==0)
TMBOY 47:9e361da97763 443 {
TMBOY 47:9e361da97763 444 fprintf(stderr, "DIV32_16: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line);
TMBOY 47:9e361da97763 445 return 0;
TMBOY 47:9e361da97763 446 }
TMBOY 47:9e361da97763 447 if (!VERIFY_INT(a) || !VERIFY_SHORT(b))
TMBOY 47:9e361da97763 448 {
TMBOY 47:9e361da97763 449 fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
TMBOY 47:9e361da97763 450 }
TMBOY 47:9e361da97763 451 res = a/b;
TMBOY 47:9e361da97763 452 if (!VERIFY_SHORT(res))
TMBOY 47:9e361da97763 453 {
TMBOY 47:9e361da97763 454 fprintf (stderr, "DIV32_16: output is not short: %d / %d = %d in %s: line %d\n", (int)a,(int)b,(int)res, file, line);
TMBOY 47:9e361da97763 455 if (res>32767)
TMBOY 47:9e361da97763 456 res = 32767;
TMBOY 47:9e361da97763 457 if (res<-32768)
TMBOY 47:9e361da97763 458 res = -32768;
TMBOY 47:9e361da97763 459 }
TMBOY 47:9e361da97763 460 spx_mips+=20;
TMBOY 47:9e361da97763 461 return res;
TMBOY 47:9e361da97763 462 }
TMBOY 47:9e361da97763 463
TMBOY 47:9e361da97763 464 #define DIV32(a, b) _DIV32(a, b, __FILE__, __LINE__)
TMBOY 47:9e361da97763 465 static inline int _DIV32(long long a, long long b, char *file, int line)
TMBOY 47:9e361da97763 466 {
TMBOY 47:9e361da97763 467 long long res;
TMBOY 47:9e361da97763 468 if (b==0)
TMBOY 47:9e361da97763 469 {
TMBOY 47:9e361da97763 470 fprintf(stderr, "DIV32: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line);
TMBOY 47:9e361da97763 471 return 0;
TMBOY 47:9e361da97763 472 }
TMBOY 47:9e361da97763 473
TMBOY 47:9e361da97763 474 if (!VERIFY_INT(a) || !VERIFY_INT(b))
TMBOY 47:9e361da97763 475 {
TMBOY 47:9e361da97763 476 fprintf (stderr, "DIV32: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
TMBOY 47:9e361da97763 477 }
TMBOY 47:9e361da97763 478 res = a/b;
TMBOY 47:9e361da97763 479 if (!VERIFY_INT(res))
TMBOY 47:9e361da97763 480 fprintf (stderr, "DIV32: output is not int: %d in %s: line %d\n", (int)res, file, line);
TMBOY 47:9e361da97763 481 spx_mips+=36;
TMBOY 47:9e361da97763 482 return res;
TMBOY 47:9e361da97763 483 }
TMBOY 47:9e361da97763 484 #define PDIV32(a,b) DIV32(ADD32((a),(b)>>1),b)
TMBOY 47:9e361da97763 485 #define PDIV32_16(a,b) DIV32_16(ADD32((a),(b)>>1),b)
TMBOY 47:9e361da97763 486
TMBOY 47:9e361da97763 487 #endif