fixedmath.h

Go to the documentation of this file.
00001 
00021 /*
00022  * $Id: fixedmath.h 12034 2009-08-11 10:18:50Z chenlei $
00023  *
00024  *             MiniGUI for Linux/uClinux, eCos, uC/OS-II, VxWorks, 
00025  *                     pSOS, ThreadX, NuCleus, OSE, and Win32.
00026  *
00027  *             Copyright (C) 2002-2009 Feynman Software.  
00028  *             Copyright (C) 1998-2002 Wei Yongming.
00029  *
00030  *             Fixed-point math routins come from Allegro (a gift software)
00031  *             by Shawn Hargreaves and others.
00032  */
00033 
00034 #ifndef _MGUI_MGHAVE_FIXED_MATH_H
00035 #define _MGUI_MGHAVE_FIXED_MATH_H
00036 
00037 
00038 #include <errno.h>
00039 #include <math.h>
00040 #include <stdlib.h>
00041 
00042 /* Set up for C function definitions, even when using C++ */
00043 #ifdef __cplusplus
00044 extern "C" {
00045 #endif
00046 
00047 #ifdef _MGHAVE_FIXED_MATH
00048 
00089 MG_EXPORT fixed fixsqrt (fixed x);
00090 
00101 MG_EXPORT fixed fixhypot (fixed x, fixed y);
00102 
00115 MG_EXPORT fixed fixatan (fixed x);
00116 
00131 MG_EXPORT fixed fixatan2 (fixed y, fixed x);
00132 
00133 extern MG_EXPORT fixed _cos_tbl[];
00134 extern MG_EXPORT fixed _tan_tbl[];
00135 extern MG_EXPORT fixed _acos_tbl[];
00136 
00137 /************************** inline fixed point math functions *****************/
00138 /* ftofix and fixtof are used in generic C versions of fixmul and fixdiv */
00139 
00152 static inline fixed ftofix (double x)
00153 { 
00154    if (x > 32767.0) {
00155       errno = ERANGE;
00156       return 0x7FFFFFFF;
00157    }
00158 
00159    if (x < -32767.0) {
00160       errno = ERANGE;
00161       return -0x7FFFFFFF;
00162    }
00163 
00164    return (long)(x * 65536.0 + (x < 0 ? -0.5 : 0.5)); 
00165 }
00166 
00176 static inline double fixtof (fixed x)
00177 { 
00178    return (double)x / 65536.0; 
00179 }
00180 
00181 
00196 static inline fixed fixadd (fixed x, fixed y)
00197 {
00198    fixed result = x + y;
00199 
00200    if (result >= 0) {
00201       if ((x < 0) && (y < 0)) {
00202          errno = ERANGE;
00203          return -0x7FFFFFFF;
00204       }
00205       else
00206          return result;
00207    }
00208    else {
00209       if ((x > 0) && (y > 0)) {
00210          errno = ERANGE;
00211          return 0x7FFFFFFF;
00212       }
00213       else
00214          return result;
00215    }
00216 }
00217 
00233 static inline fixed fixsub (fixed x, fixed y)
00234 {
00235    fixed result = x - y;
00236 
00237    if (result >= 0) {
00238       if ((x < 0) && (y > 0)) {
00239          errno = ERANGE;
00240          return -0x7FFFFFFF;
00241       }
00242       else
00243          return result;
00244    }
00245    else {
00246       if ((x > 0) && (y < 0)) {
00247          errno = ERANGE;
00248          return 0x7FFFFFFF;
00249       }
00250       else
00251          return result;
00252    }
00253 }
00254 
00268 MG_EXPORT fixed fixmul (fixed x, fixed y);
00269 
00283 MG_EXPORT fixed fixdiv (fixed x, fixed y);
00284 
00295 static inline int fixceil (fixed x)
00296 {
00297    if (x > 0x7FFF0000) {
00298       errno = ERANGE;
00299       return 0x7FFF;
00300    }
00301 
00302    x += 0xFFFF;
00303    if (x > 0)
00304        return (x >> 16);
00305    else
00306        return ~((~x) >> 16);
00307 }
00308 
00317 static inline fixed itofix (int x)
00318 { 
00319    return x << 16;
00320 }
00321 
00330 static inline int fixtoi (fixed x)
00331 { 
00332    return (x >> 16) + ((x & 0x8000) >> 15);
00333 }
00334 
00344 static inline fixed fixcos (fixed x)
00345 {
00346    return _cos_tbl[((x + 0x4000) >> 15) & 0x1FF];
00347 }
00348 
00358 static inline fixed fixsin (fixed x)
00359 { 
00360    return _cos_tbl[((x - 0x400000 + 0x4000) >> 15) & 0x1FF];
00361 }
00362 
00372 static inline fixed fixtan (fixed x)
00373 { 
00374    return _tan_tbl[((x + 0x4000) >> 15) & 0xFF];
00375 }
00376 
00390 static inline fixed fixacos (fixed x)
00391 {
00392    if ((x < -65536) || (x > 65536)) {
00393       errno = EDOM;
00394       return 0;
00395    }
00396 
00397    return _acos_tbl[(x+65536+127)>>8];
00398 }
00399 
00413 static inline fixed fixasin (fixed x)
00414 { 
00415    if ((x < -65536) || (x > 65536)) {
00416       errno = EDOM;
00417       return 0;
00418    }
00419 
00420    return 0x00400000 - _acos_tbl[(x+65536+127)>>8];
00421 }
00422 
00430 #endif /* _MGHAVE_FIXED_MATH */
00431 
00432 /* Ends C function definitions when using C++ */
00433 #ifdef __cplusplus
00434 }
00435 #endif
00436 
00437 #endif /* _MGUI_MGHAVE_FIXED_MATH_H */
00438 
Generated on Thu Apr 7 15:55:29 2011 for MiniGUI V3.0.12 API Reference by  doxygen 1.6.3