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