00001
00002
00003
00004
00005
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
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #ifndef GlFixed_MATH_INCLUDED
00070 #define GlFixed_MATH_INCLUDED
00071
00072 #include <limits.h>
00073 #include "gltypes.h"
00074 #include "gldebug.h"
00075
00076 #define GlFixed_0 ( 0 ) // Help the compliler - use 1<<16 instead of a const.
00077 #define GlFixed_1 ( 1<<16 ) // Help the compliler - use 1<<16 instead of a const.
00078
00079 inline S32 GlIntToFixed( int x ) { return x << 16; }
00080 inline S32 GlDoubleToFixed( double x ) { return S32( x * 65536.0 + 0.5); }
00081 inline double GlFixedToDouble( S32 f ) { return double( f ) / 65536.0; }
00082 inline float GlFixedToFloat( S32 f ) { return float( f ) / 65536.0f; }
00083 inline int GlFixedToInt( S32 f ) { return f >> 16; }
00084 inline int GlFixedToIntRound( S32 f ) { return ( f + 0x0800 ) >> 16; }
00085 inline int GlFixedToIntRoundUp( S32 f ) { return ( f + 0xffff ) >> 16; }
00086
00087
00088 inline S32 GlFixedMult( S32 _x, S32 _y )
00089 {
00090 #ifdef VALIDATE_DEBUG
00091 S64 x = _x;
00092 S64 y = _y;
00093 S64 ret = ( ( x * y ) >> 16 );
00094 GLASSERT( ret <= INT_MAX && ret >= INT_MIN );
00095 return S32( ret );
00096 #else
00097 return (S32) ( ( S64( _x ) * S64( _y ) ) >> 16 );
00098 #endif
00099 }
00100
00101
00102 inline S32 GlFixedDiv( S32 _x, S32 _y )
00103 {
00104 #ifdef VALIDATE_DEBUG
00105 S64 x = ( S64( _x ) << 16 );
00106 S64 y = _y;
00107
00108 S64 ret = x / y;
00109 GLASSERT( ret <= INT_MAX && ret >= INT_MIN );
00110 return S32( ret );
00111 #else
00112 return S32( ( S64( _x ) << 16 ) / S64( _y ) );
00113 #endif
00114 }
00115
00116
00119 class GlFixed
00120 {
00121 public:
00122 GlFixed() {}
00123
00124 GlFixed( const GlFixed &x ) { v = x.v; }
00125 GlFixed( int x ) { v = GlIntToFixed( x ); }
00126 GlFixed( const double x ) { v = GlDoubleToFixed( x ); }
00127
00128 #ifdef DEBUG
00129 void DebugDump() { GLOUTPUT( "fixed=%.2f\n", GlFixedToDouble( v ) ); }
00130 #endif
00131
00132
00133
00134
00135
00136
00137 int ToInt() const { return GlFixedToInt( v ); }
00138 int ToIntRoundUp() const { return GlFixedToIntRoundUp( v ); }
00139 int ToIntRound() const { return GlFixedToIntRound( v ); }
00140 double ToDouble() const { return GlFixedToDouble( v ); }
00141 float ToFloat() const { return GlFixedToFloat( v ); }
00142
00143 GlFixed Abs() { GlFixed t; ( v < 0 ) ? t.v = -v : t.v = v; return t; }
00144
00145 GlFixed& operator = (const GlFixed &x) { v = x.v; return *this; }
00146 GlFixed& operator = (const int x) { v = GlIntToFixed( x ); return *this; }
00147 GlFixed& operator = (const double x) { v = GlDoubleToFixed( x ); return *this; }
00148
00149 GlFixed& operator += (const GlFixed x) { v += x.v; return *this; }
00150 GlFixed& operator += (const int x) { v += GlIntToFixed(x); return *this; }
00151 GlFixed& operator += (const double x) { v += GlDoubleToFixed(x); return *this; }
00152
00153 GlFixed& operator -= (const GlFixed x) { v -= x.v; return *this; }
00154 GlFixed& operator -= (const int x) { v -= GlIntToFixed(x); return *this; }
00155 GlFixed& operator -= (const double x) { v -= GlDoubleToFixed(x); return *this; }
00156
00157 GlFixed& operator *= (const GlFixed x) { v = GlFixedMult(v, x.v); return *this; }
00158 GlFixed& operator *= (const int x) { v *= x; return *this; }
00159 GlFixed& operator *= (const double x) { v = GlDoubleToFixed(GlFixedToDouble(v) * x); return *this; }
00160
00161 GlFixed& operator /= (const GlFixed x) { v = GlFixedDiv(v, x.v); return *this; }
00162 GlFixed& operator /= (const int x) { v /= x; return *this; }
00163 GlFixed& operator /= (const double x) { v = GlFixedDiv( v, GlDoubleToFixed(x) ); return *this; }
00164
00165 GlFixed& operator <<= (const int x) { v <<= x; return *this; }
00166 GlFixed& operator >>= (const int x) { v >>= x; return *this; }
00167
00168 GlFixed& operator ++ () { v += GlFixed_1; return *this; }
00169 GlFixed& operator -- () { v -= GlFixed_1; return *this; }
00170
00171 GlFixed operator ++ (int) { GlFixed t; t.v = v; v += GlFixed_1; return t; }
00172 GlFixed operator -- (int) { GlFixed t; t.v = v; v -= GlFixed_1; return t; }
00173
00174
00175 GlFixed operator - () const { GlFixed t; t.v = -v; return t; }
00176
00177 inline friend GlFixed operator + (const GlFixed x, const GlFixed y) { GlFixed t; t.v = x.v + y.v; return t; }
00178 inline friend GlFixed operator + (const int x, const GlFixed y) { GlFixed t; t.v = GlIntToFixed(x) + y.v; return t; }
00179 inline friend GlFixed operator + (const GlFixed x, const int y ) { GlFixed t; t.v = x.v + GlIntToFixed(y); return t; }
00180
00181 inline friend GlFixed operator - (const GlFixed x, const GlFixed y) { GlFixed t; t.v = x.v - y.v; return t; }
00182 inline friend GlFixed operator - (const int x, const GlFixed y) { GlFixed t; t.v = GlIntToFixed(x) - y.v; return t; }
00183 inline friend GlFixed operator - (const GlFixed x, const int y) { GlFixed t; t.v = x.v - GlIntToFixed(y); return t; }
00184 inline friend GlFixed operator - (const double x, const GlFixed y) { GlFixed t; t.v = GlDoubleToFixed(x) - y.v; return t; }
00185 inline friend GlFixed operator - (const GlFixed x, const double y) { GlFixed t; t.v = x.v - GlDoubleToFixed(y); return t; }
00186
00187 inline friend GlFixed operator * (const GlFixed x, const GlFixed y) { GlFixed t; t.v = GlFixedMult(x.v, y.v); return t; }
00188 inline friend GlFixed operator * (const int x, const GlFixed y) { GlFixed t; t.v = GlFixedMult(GlIntToFixed(x), y.v); return t; }
00189 inline friend GlFixed operator * (const GlFixed x, const int y) { GlFixed t; t.v = GlFixedMult(x.v, GlIntToFixed(y)); return t; }
00190 inline friend GlFixed operator * (const double x, const GlFixed y) { GlFixed t; t.v = GlFixedMult(GlDoubleToFixed(x), y.v); return t; }
00191 inline friend GlFixed operator * (const GlFixed x, const double y) { GlFixed t; t.v = GlFixedMult(x.v, GlDoubleToFixed(y)); return t; }
00192
00193 inline friend GlFixed operator / (const GlFixed x, const GlFixed y) { GlFixed t; t.v = GlFixedDiv(x.v, y.v); return t; }
00194 inline friend GlFixed operator / (const int x, const GlFixed y) { GlFixed t; t.v = GlFixedDiv(GlIntToFixed(x), y.v); return t; }
00195 inline friend GlFixed operator / (const GlFixed x, const int y) { GlFixed t; t.v = GlFixedDiv(x.v, GlIntToFixed(y)); return t; }
00196
00197 inline friend GlFixed operator << (const GlFixed x, const int y) { GlFixed t; t.v = x.v << y; return t; }
00198 inline friend GlFixed operator >> (const GlFixed x, const int y) { GlFixed t; t.v = x.v >> y; return t; }
00199
00200 inline friend bool operator == (const GlFixed x, const GlFixed y) { return (x.v == y.v); }
00201 inline friend bool operator == (const int x, const GlFixed y) { return (GlIntToFixed(x) == y.v); }
00202 inline friend bool operator == (const GlFixed x, const int y) { return (x.v == GlIntToFixed(y)); }
00203
00204 inline friend bool operator != (const GlFixed x, const GlFixed y) { return (x.v != y.v); }
00205 inline friend bool operator != (const int x, const GlFixed y) { return (GlIntToFixed(x) != y.v); }
00206 inline friend bool operator != (const GlFixed x, const int y) { return (x.v !=GlIntToFixed(y)); }
00207
00208 inline friend bool operator < (const GlFixed x, const GlFixed y) { return (x.v < y.v); }
00209 inline friend bool operator < (const int x, const GlFixed y) { return (GlIntToFixed(x) < y.v); }
00210 inline friend bool operator < (const GlFixed x, const int y) { return (x.v < GlIntToFixed(y) ); }
00211
00212 inline friend bool operator > (const GlFixed x, const GlFixed y) { return (x.v > y.v); }
00213 inline friend bool operator > (const int x, const GlFixed y) { return (GlIntToFixed(x) > y.v); }
00214 inline friend bool operator > (const GlFixed x, const int y) { return (x.v > GlIntToFixed(y)); }
00215
00216 inline friend bool operator <= (const GlFixed x, const GlFixed y) { return (x.v <= y.v); }
00217 inline friend bool operator <= (const int x, const GlFixed y) { return (GlIntToFixed(x) <= y.v); }
00218 inline friend bool operator <= (const GlFixed x, const int y) { return (x.v <= GlIntToFixed(y)); }
00219
00220 inline friend bool operator >= (const GlFixed x, const GlFixed y) { return (x.v >= y.v); }
00221 inline friend bool operator >= (const int x, const GlFixed y) { return (GlIntToFixed(x) >= y.v); }
00222 inline friend bool operator >= (const GlFixed x, const int y) { return (x.v >= GlIntToFixed(y)); }
00223
00224 S32 v;
00225 };
00226
00227
00228 #endif