00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef FIXED_DEBUG_H
00036 #define FIXED_DEBUG_H
00037
00038 #include <stdio.h>
00039
00040 extern long long spx_mips;
00041 #define MIPS_INC spx_mips++,
00042
00043 #define QCONST16(x,bits) ((spx_word16_t)((x)*(1<<(bits))+(1<<((bits)-1))))
00044 #define QCONST32(x,bits) ((spx_word32_t)((x)*(1<<(bits))+(1<<((bits)-1))))
00045
00046 #define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768)
00047 #define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL)
00048
00049 static inline short NEG16(int x)
00050 {
00051 int res;
00052 if (!VERIFY_SHORT(x))
00053 {
00054 fprintf (stderr, "NEG16: input is not short: %d\n", (int)x);
00055 }
00056 res = -x;
00057 if (!VERIFY_SHORT(res))
00058 fprintf (stderr, "NEG16: output is not short: %d\n", (int)res);
00059 spx_mips++;
00060 return res;
00061 }
00062 static inline int NEG32(long long x)
00063 {
00064 long long res;
00065 if (!VERIFY_INT(x))
00066 {
00067 fprintf (stderr, "NEG16: input is not int: %d\n", (int)x);
00068 }
00069 res = -x;
00070 if (!VERIFY_INT(res))
00071 fprintf (stderr, "NEG16: output is not int: %d\n", (int)res);
00072 spx_mips++;
00073 return res;
00074 }
00075
00076 static inline short EXTRACT16(int x)
00077 {
00078 int res;
00079 if (!VERIFY_SHORT(x))
00080 {
00081 fprintf (stderr, "EXTRACT16: input is not short: %d\n", x);
00082 }
00083 res = x;
00084 spx_mips++;
00085 return res;
00086 }
00087
00088 static inline int EXTEND32(int x)
00089 {
00090 int res;
00091 if (!VERIFY_SHORT(x))
00092 {
00093 fprintf (stderr, "EXTRACT16: input is not short: %d\n", x);
00094 }
00095 res = x;
00096 spx_mips++;
00097 return res;
00098 }
00099
00100 static inline short SHR16(int a, int shift)
00101 {
00102 int res;
00103 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
00104 {
00105 fprintf (stderr, "SHR16: inputs are not short: %d %d\n", a, shift);
00106 }
00107 res = a>>shift;
00108 if (!VERIFY_SHORT(res))
00109 fprintf (stderr, "SHR16: output is not short: %d\n", res);
00110 spx_mips++;
00111 return res;
00112 }
00113 static inline short SHL16(int a, int shift)
00114 {
00115 int res;
00116 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
00117 {
00118 fprintf (stderr, "SHR16: inputs are not short: %d %d\n", a, shift);
00119 }
00120 res = a<<shift;
00121 if (!VERIFY_SHORT(res))
00122 fprintf (stderr, "SHR16: output is not short: %d\n", res);
00123 spx_mips++;
00124 return res;
00125 }
00126
00127 static inline int SHR32(long long a, int shift)
00128 {
00129 long long res;
00130 if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
00131 {
00132 fprintf (stderr, "SHR32: inputs are not int: %d %d\n", (int)a, shift);
00133 }
00134 res = a>>shift;
00135 if (!VERIFY_INT(res))
00136 fprintf (stderr, "SHR32: output is not int: %d\n", (int)res);
00137 spx_mips++;
00138 return res;
00139 }
00140 static inline int SHL32(long long a, int shift)
00141 {
00142 long long res;
00143 if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
00144 {
00145 fprintf (stderr, "SHR32: inputs are not int: %d %d\n", (int)a, shift);
00146 }
00147 res = a<<shift;
00148 if (!VERIFY_INT(res))
00149 fprintf (stderr, "SHR32: output is not int: %d\n", (int)res);
00150 spx_mips++;
00151 return res;
00152 }
00153
00154
00155 #define PSHR16(a,shift) (SHR16(ADD16(a,(1<<((shift)-1))),shift))
00156 #define PSHR32(a,shift) (SHR32(ADD32(a,(1<<((shift)-1))),shift))
00157 #define SATURATE16(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
00158 #define SATURATE32(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
00159
00160 #define SHR(a,shift) ((a) >> (shift))
00161 #define SHL(a,shift) ((a) << (shift))
00162
00163 static inline short ADD16(int a, int b)
00164 {
00165 int res;
00166 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00167 {
00168 fprintf (stderr, "ADD16: inputs are not short: %d %d\n", a, b);
00169 }
00170 res = a+b;
00171 if (!VERIFY_SHORT(res))
00172 fprintf (stderr, "ADD16: output is not short: %d\n", res);
00173 spx_mips++;
00174 return res;
00175 }
00176 static inline short SUB16(int a, int b)
00177 {
00178 int res;
00179 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00180 {
00181 fprintf (stderr, "SUB16: inputs are not short: %d %d\n", a, b);
00182 }
00183 res = a-b;
00184 if (!VERIFY_SHORT(res))
00185 fprintf (stderr, "SUB16: output is not short: %d\n", res);
00186 spx_mips++;
00187 return res;
00188 }
00189
00190 static inline int ADD32(long long a, long long b)
00191 {
00192 long long res;
00193 if (!VERIFY_INT(a) || !VERIFY_INT(b))
00194 {
00195 fprintf (stderr, "ADD32: inputs are not int: %d %d\n", (int)a, (int)b);
00196 }
00197 res = a+b;
00198 if (!VERIFY_INT(res))
00199 fprintf (stderr, "ADD32: output is not int: %d\n", (int)res);
00200 spx_mips++;
00201 return res;
00202 }
00203
00204 static inline int SUB32(long long a, long long b)
00205 {
00206 long long res;
00207 if (!VERIFY_INT(a) || !VERIFY_INT(b))
00208 {
00209 fprintf (stderr, "SUB32: inputs are not int: %d %d\n", (int)a, (int)b);
00210 }
00211 res = a-b;
00212 if (!VERIFY_INT(res))
00213 fprintf (stderr, "SUB32: output is not int: %d\n", (int)res);
00214 spx_mips++;
00215 return res;
00216 }
00217
00218 #define ADD64(a,b) (MIPS_INC(a)+(b))
00219
00220 #define PSHR(a,shift) (SHR((a)+(1<<((shift)-1)),shift))
00221
00222
00223 static inline short MULT16_16_16(int a, int b)
00224 {
00225 int res;
00226 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00227 {
00228 fprintf (stderr, "MULT16_16_16: inputs are not short: %d %d\n", a, b);
00229 }
00230 res = a*b;
00231 if (!VERIFY_SHORT(res))
00232 fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res);
00233 spx_mips++;
00234 return res;
00235 }
00236
00237 static inline int MULT16_16(int a, int b)
00238 {
00239 long long res;
00240 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00241 {
00242 fprintf (stderr, "MULT16_16: inputs are not short: %d %d\n", a, b);
00243 }
00244 res = ((long long)a)*b;
00245 if (!VERIFY_INT(res))
00246 fprintf (stderr, "MULT16_16: output is not int: %d\n", (int)res);
00247 spx_mips++;
00248 return res;
00249 }
00250
00251 #define MAC16_16(c,a,b) (spx_mips--,ADD32((c),MULT16_16((a),(b))))
00252 #define MAC16_16_Q11(c,a,b) (ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),11))))
00253 #define MAC16_16_Q13(c,a,b) (ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),13))))
00254
00255 static inline int MULT16_32_QX(int a, long long b, int Q)
00256 {
00257 long long res;
00258 if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
00259 {
00260 fprintf (stderr, "MULT16_32_Q%d: inputs are not short+int: %d %d\n", Q, (int)a, (int)b);
00261 }
00262 res = (((long long)a)*(long long)b) >> Q;
00263 if (!VERIFY_INT(res))
00264 fprintf (stderr, "MULT16_32_Q%d: output is not int: %d*%d=%d\n", Q, (int)a, (int)b,(int)res);
00265 spx_mips+=5;
00266 return res;
00267 }
00268
00269
00270 #define MULT16_32_Q11(a,b) MULT16_32_QX(a,b,11)
00271 #define MAC16_32_Q11(c,a,b) ADD32((c),MULT16_32_Q11((a),(b)))
00272 #define MULT16_32_Q12(a,b) MULT16_32_QX(a,b,12)
00273 #define MULT16_32_Q13(a,b) MULT16_32_QX(a,b,13)
00274 #define MULT16_32_Q14(a,b) MULT16_32_QX(a,b,14)
00275 #define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15)
00276 #define MAC16_32_Q15(c,a,b) ADD32((c),MULT16_32_Q15((a),(b)))
00277
00278 static inline int SATURATE(int a, int b)
00279 {
00280 if (a>b)
00281 a=b;
00282 if (a<-b)
00283 a = -b;
00284 return a;
00285 }
00286
00287 static inline int MULT16_16_Q11_32(int a, int b)
00288 {
00289 long long res;
00290 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00291 {
00292 fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b);
00293 }
00294 res = ((long long)a)*b;
00295 res >>= 11;
00296 if (!VERIFY_INT(res))
00297 fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res);
00298 spx_mips+=3;
00299 return res;
00300 }
00301 static inline short MULT16_16_Q13(int a, int b)
00302 {
00303 long long res;
00304 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00305 {
00306 fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b);
00307 }
00308 res = ((long long)a)*b;
00309 res >>= 13;
00310 if (!VERIFY_SHORT(res))
00311 fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res);
00312 spx_mips+=3;
00313 return res;
00314 }
00315 static inline short MULT16_16_Q14(int a, int b)
00316 {
00317 long long res;
00318 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00319 {
00320 fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b);
00321 }
00322 res = ((long long)a)*b;
00323 res >>= 14;
00324 if (!VERIFY_SHORT(res))
00325 fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res);
00326 spx_mips+=3;
00327 return res;
00328 }
00329 static inline short MULT16_16_Q15(int a, int b)
00330 {
00331 long long res;
00332 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00333 {
00334 fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d\n", a, b);
00335 }
00336 res = ((long long)a)*b;
00337 res >>= 15;
00338 if (!VERIFY_SHORT(res))
00339 fprintf (stderr, "MULT16_16_Q15: output is not short: %d\n", (int)res);
00340 spx_mips+=3;
00341 return res;
00342 }
00343
00344 static inline short MULT16_16_P13(int a, int b)
00345 {
00346 long long res;
00347 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00348 {
00349 fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b);
00350 }
00351 res = ((long long)a)*b;
00352 res += 4096;
00353 if (!VERIFY_INT(res))
00354 fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res);
00355 res >>= 13;
00356 if (!VERIFY_SHORT(res))
00357 fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res);
00358 spx_mips+=4;
00359 return res;
00360 }
00361 static inline short MULT16_16_P14(int a, int b)
00362 {
00363 long long res;
00364 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00365 {
00366 fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b);
00367 }
00368 res = ((long long)a)*b;
00369 res += 8192;
00370 if (!VERIFY_INT(res))
00371 fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res);
00372 res >>= 14;
00373 if (!VERIFY_SHORT(res))
00374 fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
00375 spx_mips+=4;
00376 return res;
00377 }
00378 static inline short MULT16_16_P15(int a, int b)
00379 {
00380 long long res;
00381 if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
00382 {
00383 fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b);
00384 }
00385 res = ((long long)a)*b;
00386 res += 16384;
00387 if (!VERIFY_INT(res))
00388 fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res);
00389 res >>= 15;
00390 if (!VERIFY_SHORT(res))
00391 fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res);
00392 spx_mips+=4;
00393 return res;
00394 }
00395
00396
00397 static inline int DIV32_16(long long a, long long b)
00398 {
00399 long long res;
00400 if (b==0)
00401 {
00402 fprintf(stderr, "DIV32_16: divide by zero: %d/%d\n", (int)a, (int)b);
00403 return 0;
00404 }
00405 if (!VERIFY_INT(a) || !VERIFY_SHORT(b))
00406 {
00407 fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d\n", (int)a, (int)b);
00408 }
00409 res = a/b;
00410 if (!VERIFY_SHORT(res))
00411 {
00412 fprintf (stderr, "DIV32_16: output is not short: %d / %d = %d\n", (int)a,(int)b,(int)res);
00413 if (res>32767)
00414 res = 32767;
00415 if (res<-32768)
00416 res = -32768;
00417 }
00418 spx_mips+=20;
00419 return res;
00420 }
00421 static inline int DIV32(long long a, long long b)
00422 {
00423 long long res;
00424 if (b==0)
00425 {
00426 fprintf(stderr, "DIV32: divide by zero: %d/%d\n", (int)a, (int)b);
00427 return 0;
00428 }
00429
00430 if (!VERIFY_INT(a) || !VERIFY_INT(b))
00431 {
00432 fprintf (stderr, "DIV32: inputs are not int/short: %d %d\n", (int)a, (int)b);
00433 }
00434 res = a/b;
00435 if (!VERIFY_INT(res))
00436 fprintf (stderr, "DIV32: output is not int: %d\n", (int)res);
00437 spx_mips+=36;
00438 return res;
00439 }
00440
00441
00442
00443 #endif