Go to the documentation of this file.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 #ifndef __ARTKPFLOAT_FIXED_HEADERFILE__
00042 #define __ARTKPFLOAT_FIXED_HEADERFILE__
00043
00044 #include <math.h>
00045 #include <assert.h>
00046
00047 #pragma warning ( push )
00048 #pragma warning ( disable: 4293 ) // fix warning about shifting in conversion constructor
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 template<typename MATHBASE>
00060 class artkpFloat_fixed
00061 {
00062 public:
00063 enum {
00064 PBITS = MATHBASE::PBITS,
00065 CHECK = MATHBASE::CHECK
00066 };
00067
00068 artkpFloat_fixed() {}
00069 artkpFloat_fixed(float nV) { setFloat(nV); }
00070 artkpFloat_fixed(double nV) { setDouble(nV); }
00071 artkpFloat_fixed(const artkpFloat_fixed& nOther) { v = nOther.v; }
00072
00073
00074 template <typename OTHER_MATHBASE>
00075 artkpFloat_fixed(const artkpFloat_fixed<OTHER_MATHBASE>& nOther)
00076 {
00077 if(OTHER_MATHBASE::PBITS<PBITS)
00078 {
00079 if(CHECK)
00080 {
00081
00082 __int64 v64 = ((__int64)nOther.getFixed()) << (PBITS-OTHER_MATHBASE::PBITS);
00083 if(v64 != int(v))
00084 {
00085 assert(false && "Fixed-Point to Fixed-Point conversion failed: the target's range was overflowed");
00086 }
00087 }
00088 v = nOther.getFixed() << (PBITS-OTHER_MATHBASE::PBITS);
00089 }
00090 else
00091 {
00092 v = nOther.getFixed() >> (OTHER_MATHBASE::PBITS-PBITS);
00093 }
00094 }
00095
00096
00097
00098 bool isFixed() { return true; }
00099
00100 void setFixed(int nV) { v = nV; }
00101 void setInt(int nV) { v = MATHBASE::fixedFromInt(nV); }
00102 void setFloat(float nV) { v = MATHBASE::fixedFromFloat(nV); }
00103 void setDouble(double nV) { v = MATHBASE::fixedFromDouble(nV); }
00104 void setArtkpFloat(const artkpFloat_fixed& nOther) { v = nOther.v; }
00105
00106 int getByte() const { return v>255<<(PBITS-8) ? 255 : v>>(PBITS-8); }
00107 int getFixed() const { return v; }
00108 int getInt() const { return v>>PBITS; }
00109 float getFloat() const { return MATHBASE::floatFromFixed(v); }
00110 double getDouble() const { return MATHBASE::doubleFromFixed(v); }
00111
00112 void inverse(const artkpFloat_fixed& nOther) { v = MATHBASE::inverse(nOther.v); }
00113 void inverse() { inverse(*this); }
00114
00115 void inverseSqrt(const artkpFloat_fixed& nOther) { v = MATHBASE::inverseSqrt(nOther.v); }
00116 void inverseSqrt() { inverseSqrt(*this); }
00117
00118 void multiplyBy255() { int vOld = v; v<<=8; v-=vOld; }
00119
00120
00121
00122
00123 friend inline artkpFloat_fixed cos(const artkpFloat_fixed& nV)
00124 { return MATHBASE::cos(nV.v); }
00125
00126
00127
00128
00129 artkpFloat_fixed& operator=(int nV) { setInt(nV); return *this; }
00130 artkpFloat_fixed& operator=(float nV) { setFloat(nV); return *this; }
00131 artkpFloat_fixed& operator=(double nV) { setDouble(nV); return *this; }
00132 artkpFloat_fixed& operator=(const artkpFloat_fixed& nOther) { v = nOther.v; return *this; }
00133
00134 artkpFloat_fixed& operator+=(int nV) { v += MATHBASE::fixedFromInt(nV); return *this; }
00135 artkpFloat_fixed& operator-=(int nV) { v -= MATHBASE::fixedFromInt(nV); return *this; }
00136 artkpFloat_fixed& operator*=(int nV) { v = MATHBASE::multiply(v, MATHBASE::fixedFromInt(nV)); return *this; }
00137 artkpFloat_fixed& operator/=(int nV) { v = MATHBASE::divide(v, MATHBASE::fixedFromInt(nV)); return *this; }
00138
00139 artkpFloat_fixed& operator+=(float nV) { v += MATHBASE::fixedFromFloat(nV); return *this; }
00140 artkpFloat_fixed& operator-=(float nV) { v -= MATHBASE::fixedFromFloat(nV); return *this; }
00141 artkpFloat_fixed& operator*=(float nV) { v = MATHBASE::multiply(v, MATHBASE::fixedFromFloat(nV)); return *this; }
00142 artkpFloat_fixed& operator/=(float nV) { v = MATHBASE::divide(v, MATHBASE::fixedFromFloat(nV)); return *this; }
00143
00144 artkpFloat_fixed& operator+=(double nV) { v += MATHBASE::fixedFromDouble(nV); return *this; }
00145 artkpFloat_fixed& operator-=(double nV) { v -= MATHBASE::fixedFromDouble(nV); return *this; }
00146 artkpFloat_fixed& operator*=(double nV) { v = MATHBASE::multiply(v, fixedFromDouble(nV)); return *this; }
00147 artkpFloat_fixed& operator/=(double nV) { v = MATHBASE::divide(v, fixedFromDouble(nV)); return *this; }
00148
00149 artkpFloat_fixed& operator+=(const artkpFloat_fixed& nOther) { v+=nOther.v; return *this; }
00150 artkpFloat_fixed& operator-=(const artkpFloat_fixed& nOther) { v-=nOther.v; return *this; }
00151 artkpFloat_fixed& operator*=(const artkpFloat_fixed& nOther) { v = MATHBASE::multiply(v, nOther.v); return *this; }
00152 artkpFloat_fixed& operator/=(const artkpFloat_fixed& nOther) { v = MATHBASE::divide(v, nOther.v); return *this; }
00153
00154 artkpFloat_fixed& operator>>=(int nBits) { v>>=nBits; return *this; }
00155 artkpFloat_fixed& operator<<=(int nBits) { v<<=nBits; return *this; }
00156
00157 bool operator==(const artkpFloat_fixed& nOther) const { return v==nOther.v; }
00158 bool operator!=(const artkpFloat_fixed& nOther) const { return v!=nOther.v; }
00159 bool operator<=(const artkpFloat_fixed& nOther) const { return v<=nOther.v; }
00160 bool operator>=(const artkpFloat_fixed& nOther) const { return v>=nOther.v; }
00161 bool operator<(const artkpFloat_fixed& nOther) const { return v<nOther.v; }
00162 bool operator>(const artkpFloat_fixed& nOther) const { return v>nOther.v; }
00163
00164 bool operator==(int nOther) const { return v == MATHBASE::fixedFromInt(nOther); }
00165 bool operator!=(int nOther) const { return v != MATHBASE::fixedFromInt(nOther); }
00166 bool operator<=(int nOther) const { return v <= MATHBASE::fixedFromInt(nOther); }
00167 bool operator>=(int nOther) const { return v >= MATHBASE::fixedFromInt(nOther); }
00168 bool operator<(int nOther) const { return v < MATHBASE::fixedFromInt(nOther); }
00169 bool operator>(int nOther) const { return v > MATHBASE::fixedFromInt(nOther); }
00170
00171 bool operator==(float nOther) const { return v == MATHBASE::fixedFromFloat(nOther); }
00172 bool operator!=(float nOther) const { return v != MATHBASE::fixedFromFloat(nOther); }
00173 bool operator<=(float nOther) const { return v <= MATHBASE::fixedFromFloat(nOther); }
00174 bool operator>=(float nOther) const { return v >= MATHBASE::fixedFromFloat(nOther); }
00175 bool operator<(float nOther) const { return v < MATHBASE::fixedFromFloat(nOther); }
00176 bool operator>(float nOther) const { return v > MATHBASE::fixedFromFloat(nOther); }
00177
00178 friend artkpFloat_fixed operator+ (const artkpFloat_fixed& left, const artkpFloat_fixed& right)
00179 { return artkpFloat_fixed(left.v+right.v); }
00180 friend artkpFloat_fixed operator+ (const artkpFloat_fixed& left, float right)
00181 { return artkpFloat_fixed(left.v+MATHBASE::fixedFromFloat(right)); }
00182 friend artkpFloat_fixed operator+ (float left, const artkpFloat_fixed& right)
00183 { return artkpFloat_fixed(MATHBASE::fixedFromFloat(left)+right.v); }
00184
00185 friend artkpFloat_fixed operator- (const artkpFloat_fixed& left, const artkpFloat_fixed& right)
00186 { return artkpFloat_fixed(left.v-right.v); }
00187 friend artkpFloat_fixed operator- (const artkpFloat_fixed& left, float right)
00188 { return artkpFloat_fixed(left.v-MATHBASE::fixedFromFloat(right)); }
00189 friend artkpFloat_fixed operator- (float left, const artkpFloat_fixed& right)
00190 { return artkpFloat_fixed(MATHBASE::fixedFromFloat(left)-right.v); }
00191
00192 friend artkpFloat_fixed operator* (const artkpFloat_fixed& left, const artkpFloat_fixed& right)
00193 { return artkpFloat_fixed(MATHBASE::multiply(left.v, right.v)); }
00194 friend artkpFloat_fixed operator* (const artkpFloat_fixed& left, float right)
00195 { return artkpFloat_fixed(MATHBASE::multiply(left.v, MATHBASE::fixedFromFloat(right))); }
00196 friend artkpFloat_fixed operator* (float left, const artkpFloat_fixed& right)
00197 { return artkpFloat_fixed(MATHBASE::multiply(MATHBASE::fixedFromFloat(left), right.v)); }
00198
00199 friend artkpFloat_fixed operator/ (const artkpFloat_fixed& left, const artkpFloat_fixed& right)
00200 { return artkpFloat_fixed(MATHBASE::divide(left.v, right.v)); }
00201 friend artkpFloat_fixed operator/ (const artkpFloat_fixed& left, float right)
00202 { return artkpFloat_fixed(MATHBASE::divide(left.v, MATHBASE::fixedFromFloat(right))); }
00203 friend artkpFloat_fixed operator/ (float left, const artkpFloat_fixed& right)
00204 { return artkpFloat_fixed(MATHBASE::divide(MATHBASE::fixedFromFloat(left), right.v)); }
00205
00206 artkpFloat_fixed operator-() const
00207 { artkpFloat_fixed w; w.v = -v; return w; }
00208
00209 protected:
00210 artkpFloat_fixed(int nV) { v = nV; }
00211
00212 int v;
00213 };
00214
00215
00216 #pragma warning ( pop )
00217
00218
00219 #endif //__ARTKPFLOAT_FIXED_HEADERFILE__