X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=ETL%2Ftrunk%2FETL%2F_fixed.h;h=92f781e036b19fb8b739e5127f3feb97f5e7a759;hb=e0447b01caa3a223b6ea2ec83abdbc42fb86e4ac;hp=cf69fc12f0327a40fc5414d47ee0fc6468581a32;hpb=b3016b249333ac0ab0008d8c6c4d9029b2ff30c9;p=synfig.git diff --git a/ETL/trunk/ETL/_fixed.h b/ETL/trunk/ETL/_fixed.h index cf69fc1..92f781e 100644 --- a/ETL/trunk/ETL/_fixed.h +++ b/ETL/trunk/ETL/_fixed.h @@ -1,7 +1,7 @@ /*! ======================================================================== ** Extended Template and Library ** Fixed-Point Math Class Implementation -** $Id: _fixed.h,v 1.1.1.1 2005/01/04 01:31:47 darco Exp $ +** $Id$ ** ** Copyright (c) 2002 Robert B. Quattlebaum Jr. ** @@ -33,6 +33,10 @@ /* === M A C R O S ========================================================= */ +// the "+0.5" code was commented out - maybe to make thing run faster? +// it can be re-enabled by uncommenting this next line: +// #define ROUND_TO_NEAREST_INTEGER + #ifndef ETL_FIXED_TYPE # define ETL_FIXED_TYPE int #endif @@ -74,7 +78,7 @@ template _ETL::fixed_base log template _ETL::fixed_base log10(const _ETL::fixed_base&); template _ETL::fixed_base pow(const _ETL::fixed_base&, int); template _ETL::fixed_base pow(const _ETL::fixed_base&, const T&); -template _ETL::fixed_base pow(const _ETL::fixed_base&, +template _ETL::fixed_base pow(const _ETL::fixed_base&, const _ETL::fixed_base&); template _ETL::fixed_base pow(const _ETL::fixed_base&, const _ETL::fixed_base&); template _ETL::fixed_base sin(const _ETL::fixed_base&); @@ -98,7 +102,7 @@ public: typedef T value_type; private: T _data; - + typedef fixed_base _fixed; typedef fixed_base self_type; @@ -111,15 +115,15 @@ private: class raw { }; public: fixed_base()ETL_ATTRIB_INLINE; - fixed_base(const _fixed &x)ETL_ATTRIB_INLINE; fixed_base(const float &f)ETL_ATTRIB_INLINE; fixed_base(const double &f)ETL_ATTRIB_INLINE; fixed_base(const long double &f)ETL_ATTRIB_INLINE; fixed_base(const int &i)ETL_ATTRIB_INLINE; fixed_base(const int &n,const int &d)ETL_ATTRIB_INLINE; //!< Fraction constructor + fixed_base(const _fixed &x)ETL_ATTRIB_INLINE; fixed_base(value_type x,raw)ETL_ATTRIB_INLINE; - T &data() ETL_ATTRIB_PURE ETL_ATTRIB_INLINE; + T &data() ETL_ATTRIB_PURE ETL_ATTRIB_INLINE; const T &data()const ETL_ATTRIB_PURE ETL_ATTRIB_INLINE; const _fixed& operator+=(const _fixed &rhs) ETL_ATTRIB_INLINE; @@ -142,7 +146,9 @@ public: _fixed operator/(const _fixed &rhs)const ETL_ATTRIB_INLINE; _fixed operator*(const int &rhs)const ETL_ATTRIB_INLINE; _fixed operator/(const int &rhs)const ETL_ATTRIB_INLINE; - + _fixed operator*(const float &rhs)const ETL_ATTRIB_INLINE; + _fixed operator*(const double &rhs)const ETL_ATTRIB_INLINE; + // Negation Operator _fixed operator-()const ETL_ATTRIB_INLINE; @@ -156,7 +162,7 @@ public: _fixed floor()const; _fixed ceil()const; _fixed round()const; - + bool operator==(const _fixed &rhs)const { return data()==rhs.data(); } bool operator!=(const _fixed &rhs)const { return data()!=rhs.data(); } bool operator<(const _fixed &rhs)const { return data() fixed_base::fixed_base() -{} +{} template fixed_base::fixed_base(const _fixed &x):_data(x._data) {} template -fixed_base::fixed_base(const float &f):_data(static_cast(f*_ONE()+0.5f)) -{} +fixed_base::fixed_base(const float &f):_data(static_cast(f*_ONE() +#ifdef ROUND_TO_NEAREST_INTEGER + +0.5f +#endif + )) {} template -fixed_base::fixed_base(const double &f):_data(static_cast(f*_ONE()+0.5)) -{} +fixed_base::fixed_base(const double &f):_data(static_cast(f*_ONE() +#ifdef ROUND_TO_NEAREST_INTEGER + +0.5 +#endif + )) {} template -fixed_base::fixed_base(const long double &f):_data(static_cast(f*_ONE()+0.5)) -{} +fixed_base::fixed_base(const long double &f):_data(static_cast(f*_ONE() +#ifdef ROUND_TO_NEAREST_INTEGER + +0.5 +#endif + )) {} template fixed_base::fixed_base(const int &i):_data(i<::fixed_base(value_type x,raw):_data(x) { } template fixed_base::fixed_base(const int &n,const int &d):_data((n< inline bool -fixed_base::_TYPE_SMALLER_THAN_INT() +fixed_base::_TYPE_SMALLER_THAN_INT() { return sizeof(T) inline bool -fixed_base::_USING_ALL_BITS() +fixed_base::_USING_ALL_BITS() { return sizeof(T)*8==FIXED_BITS; } template inline T -fixed_base::_ONE() +fixed_base::_ONE() { return static_cast((_USING_ALL_BITS()?~T(0):1< inline T -fixed_base::_F_MASK() +fixed_base::_F_MASK() { return static_cast(_USING_ALL_BITS()?~T(0):_ONE()-1); } template inline float -fixed_base::_EPSILON() +fixed_base::_EPSILON() { return 1.0f/((float)_ONE()*2); } template T & -fixed_base::data() +fixed_base::data() { return _data; } template const T & -fixed_base::data()const +fixed_base::data()const { return _data; } //! fixed+=fixed template const fixed_base & -fixed_base::operator+=(const fixed_base &rhs) +fixed_base::operator+=(const _fixed &rhs) { _data+=rhs._data; return *this; @@ -251,7 +266,7 @@ fixed_base::operator+=(const fixed_base &rhs) //! fixed-=fixed template const fixed_base & -fixed_base::operator-=(const fixed_base &rhs) +fixed_base::operator-=(const _fixed &rhs) { _data-=rhs._data; return *this; @@ -259,7 +274,7 @@ fixed_base::operator-=(const fixed_base &rhs) //! fixed*=fixed template const fixed_base & -fixed_base::operator*=(const fixed_base &rhs) +fixed_base::operator*=(const _fixed &rhs) { if(_TYPE_SMALLER_THAN_INT()) _data=static_cast((int)_data*(int)rhs._data>>FIXED_BITS); @@ -268,13 +283,13 @@ fixed_base::operator*=(const fixed_base &rhs) _data*=rhs._data; _data>>=FIXED_BITS; } - + return *this; } //! fixed/=fixed template const fixed_base & -fixed_base::operator/=(const fixed_base &rhs) +fixed_base::operator/=(const _fixed &rhs) { if(_TYPE_SMALLER_THAN_INT()) _data=static_cast((int)_data/(int)rhs._data<::operator/=(const fixed_base &rhs) } template template const fixed_base & -fixed_base::operator*=(const U &rhs) +fixed_base::operator*=(const U &rhs) { return operator*=(fixed_base(rhs)); } template template const fixed_base & -fixed_base::operator/=(const U &rhs) +fixed_base::operator/=(const U &rhs) { return operator/=(fixed_base(rhs)); } //! fixed*=int template const fixed_base & -fixed_base::operator*=(const int &rhs) +fixed_base::operator*=(const int &rhs) { _data*=rhs; return *this; } //! fixed/=int template const fixed_base & -fixed_base::operator/=(const int &rhs) +fixed_base::operator/=(const int &rhs) { _data/=rhs; return *this; } @@ -320,7 +335,7 @@ fixed_base::operator/=(const int &rhs) //! fixed + fixed template fixed_base -fixed_base::operator+(const fixed_base &rhs)const +fixed_base::operator+(const _fixed &rhs)const { _fixed ret; ret._data=_data+rhs._data; @@ -329,7 +344,7 @@ fixed_base::operator+(const fixed_base &rhs)const //! fixed - fixed template fixed_base -fixed_base::operator-(const fixed_base &rhs)const +fixed_base::operator-(const _fixed &rhs)const { _fixed ret; ret._data=_data-rhs._data; @@ -338,7 +353,7 @@ fixed_base::operator-(const fixed_base &rhs)const //! fixed * fixed template fixed_base -fixed_base::operator*(const fixed_base &rhs)const +fixed_base::operator*(const _fixed &rhs)const { _fixed ret; ret._data=((_data*rhs._data)>>FIXED_BITS); @@ -348,7 +363,7 @@ fixed_base::operator*(const fixed_base &rhs)const //! fixed / fixed template fixed_base -fixed_base::operator/(const fixed_base &rhs)const +fixed_base::operator/(const _fixed &rhs)const { _fixed ret; ret._data=((_data/rhs._data)<::operator/(const U &rhs) const //! fixed * int template fixed_base -fixed_base::operator*(const int &rhs)const +fixed_base::operator*(const int &rhs)const { _fixed ret; ret._data=_data*rhs; @@ -394,9 +409,24 @@ fixed_base::operator*(const int &rhs)const //return reinterpret_cast<_fixed>(_data*rhs); } +//! fixed * float +template fixed_base +fixed_base::operator*(const float &rhs)const +{ + return (*this)*_fixed(rhs); +} + +//! fixed * double +template fixed_base +fixed_base::operator*(const double &rhs)const +{ + return (*this)*_fixed(rhs); +} + + //! fixed / int template fixed_base -fixed_base::operator/(const int &rhs)const +fixed_base::operator/(const int &rhs)const { _fixed ret; ret._data=_data/rhs; @@ -404,7 +434,19 @@ fixed_base::operator/(const int &rhs)const //return reinterpret_cast<_fixed>(_data/rhs); } +//! float * fixed +template fixed_base +operator*(const float& lhs, const fixed_base &rhs) +{ + return rhs*lhs; +} +//! double * fixed +template fixed_base +operator*(const double& lhs, const fixed_base &rhs) +{ + return rhs*lhs; +} @@ -413,38 +455,38 @@ fixed_base::operator/(const int &rhs)const // Negation Operator template fixed_base -fixed_base::operator-()const +fixed_base::operator-()const { _fixed ret; ret._data=-_data; return ret; } // Casting Operators template -fixed_base::operator float()const +fixed_base::operator float()const { return static_cast(_data)/static_cast(_ONE()); -} +} template -fixed_base::operator double()const +fixed_base::operator double()const { return static_cast(_data)/static_cast(_ONE()); -} +} template -fixed_base::operator long double()const +fixed_base::operator long double()const { return static_cast(_data)/static_cast(_ONE()); -} +} template -fixed_base::operator int()const +fixed_base::operator int()const { return static_cast(_data>>FIXED_BITS); } template -fixed_base::operator bool()const +fixed_base::operator bool()const { return static_cast(_data); }