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 __ARTKPFIXEDBASE_GENERIC_HEADERFILE__
00042 #define __ARTKPFIXEDBASE_GENERIC_HEADERFILE__
00043 
00044 #include <assert.h>
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 template <int PBITS_, int CHECK_>
00053 class artkpFixedBase_generic
00054 {
00055 public:
00056         enum {
00057                 PBITS = PBITS_,
00058                 CHECK = CHECK_
00059         };
00060 
00061 
00062         static void checkInt(int nInteger)
00063         {
00064                 __int64 v64 = ((__int64)nInteger) << PBITS;
00065                 if(v64 != int(v64))
00066                 {
00067                         assert(false && "Integer to Fixed-Point conversion failed: the target's range was overflowed");
00068                 };
00069         }
00070 
00071         static void checkFloat(float nFloat)
00072         {
00073                 __int64 v64 = (__int64)(nFloat *  ((float)(1 << PBITS) + 0.5f));
00074                 if(v64 != int(v64))
00075                 {
00076                         assert(false && "Float to Fixed-Point conversion failed: the target's range was overflowed");
00077                 };
00078         }
00079 
00080         static void checkDouble(double nDouble)
00081         {
00082                 __int64 v64 = (__int64)(nDouble *  ((double)(1 << PBITS) + 0.5f));
00083                 if(v64 != int(v64))
00084                 {
00085                         assert(false && "Double to Fixed-Point conversion failed: the target's range was overflowed");
00086                 };
00087         }
00088 
00089         static float floatFromFixed(int nFixed)
00090         {
00091                 return nFixed/(float)(1 << PBITS);
00092         }       
00093 
00094         static double doubleFromFixed(int nFixed)
00095         {
00096                 return nFixed/(double)(1 << PBITS);
00097         }
00098 
00099         static int fixedFromInt(int nV)
00100         {
00101                 if(CHECK)
00102                         checkInt(nV);
00103                 return nV<<PBITS;
00104         }
00105 
00106         static int fixedFromFloat(float nV)
00107         {
00108                 if(CHECK)
00109                         checkFloat(nV);
00110                 return (int)(nV *  (float)(1 << PBITS) + 0.5f);
00111         }
00112 
00113         static int fixedFromDouble(double nV)
00114         {
00115                 if(CHECK)
00116                         checkDouble(nV);
00117                 return (int)(nV * (double)(1 << PBITS) + 0.5f);
00118         }
00119 
00120         static int inverse(int nFixed)
00121         {
00122                 return (__int32)(((__int64)1<<(2*PBITS))/nFixed);
00123         }
00124 
00125         static int multiply(int nLeftFixed, int nRightFixed)
00126         {
00127                 return (__int32)(((__int64)nLeftFixed * (__int64)nRightFixed) >> PBITS);
00128         }
00129 
00130         static int divide(int nLeftFixed, int nRightFixed)
00131         {
00132                 return (__int32)(((__int64)nLeftFixed << PBITS) / nRightFixed);
00133         }
00134 
00135         static int cos(int nFixed)
00136         {
00137                 return fixedFromDouble(::cos(floatFromFixed(nFixed)));
00138         }
00139 
00140         static int sin(int nFixed)
00141         {
00142                 return fixedFromDouble(::sin(floatFromFixed(nFixed)));
00143         }
00144 
00145         static int fabs(int nFixed)
00146         {
00147                 return nFixed<0 ? -nFixed : nFixed;
00148         }
00149 
00150         static int sqrt(int nFixed)
00151         {
00152                 return fixedFromDouble(::sqrt(floatFromFixed(nFixed)));
00153         }
00154 
00155         static int inverseSqrt(int nFixed)
00156         {
00157                 return inverse(sqrt(nFixed));
00158         }
00159 
00160         static int ceil(int nFixed)
00161         {
00162                 int ret = (nFixed>>PBITS)<<PBITS;
00163 
00164                 if(nFixed>=0 && ret<nFixed)
00165                         ret += fixedFromInt(1);
00166 
00167                 return ret;
00168         }
00169 
00170 };
00171 
00172 
00173 #endif //__ARTKPFIXEDBASE_GENERIC_HEADERFILE__