28 #ifndef __ETL__FIXED_H
29 #define __ETL__FIXED_H
41 #ifndef ETL_FIXED_TYPE
42 # define ETL_FIXED_TYPE int
45 #ifndef ETL_FIXED_BITS
46 #define ETL_FIXED_BITS 12
49 #ifndef ETL_FIXED_EPSILON
50 #define ETL_FIXED_EPSILON _EPSILON()
54 #define ETL_ATTRIB_CONST __attribute__ ((const))
55 #define ETL_ATTRIB_PURE __attribute__ ((pure))
56 #define ETL_ATTRIB_INLINE __attribute__ ((always_inline))
58 #define ETL_ATTRIB_CONST
59 #define ETL_ATTRIB_PURE
60 #define ETL_ATTRIB_INLINE
68 template<
typename T,
unsigned int FIXED_BITS>
class fixed_base;
74 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
abs(
const _ETL::fixed_base<T,FIXED_BITS>&);
75 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
cos(
const _ETL::fixed_base<T,FIXED_BITS>&);
76 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
cosh(
const _ETL::fixed_base<T,FIXED_BITS>&);
77 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
exp(
const _ETL::fixed_base<T,FIXED_BITS>&);
78 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
log(
const _ETL::fixed_base<T,FIXED_BITS>&);
79 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
log10(
const _ETL::fixed_base<T,FIXED_BITS>&);
80 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
pow(
const _ETL::fixed_base<T,FIXED_BITS>&,
int);
81 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
pow(
const _ETL::fixed_base<T,FIXED_BITS>&,
const T&);
82 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
pow(
const _ETL::fixed_base<T,FIXED_BITS>&,
83 const _ETL::fixed_base<T,FIXED_BITS>&);
84 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
pow(
const _ETL::fixed_base<T,FIXED_BITS>&,
const _ETL::fixed_base<T,FIXED_BITS>&);
85 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
sin(
const _ETL::fixed_base<T,FIXED_BITS>&);
86 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
sinh(
const _ETL::fixed_base<T,FIXED_BITS>&);
87 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
sqrt(
const _ETL::fixed_base<T,FIXED_BITS>&);
88 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
tan(
const _ETL::fixed_base<T,FIXED_BITS>&);
89 template<
typename T,
unsigned int FIXED_BITS> _ETL::fixed_base<T,FIXED_BITS>
tanh(
const _ETL::fixed_base<T,FIXED_BITS>&);
99 template <
class T,
unsigned int FIXED_BITS>
121 fixed_base(const
long double &f)ETL_ATTRIB_INLINE;
123 fixed_base(const
int &n,const
int &d)ETL_ATTRIB_INLINE;
130 const
_fixed& operator+=(const
_fixed &rhs) ETL_ATTRIB_INLINE;
131 const
_fixed& operator-=(const
_fixed &rhs) ETL_ATTRIB_INLINE;
132 template<typename U> const
_fixed& operator*=(const U &rhs) ETL_ATTRIB_INLINE;
133 template<typename U> const
_fixed& operator/=(const U &rhs) ETL_ATTRIB_INLINE;
134 const
_fixed& operator*=(const
_fixed &rhs) ETL_ATTRIB_INLINE;
135 const
_fixed& operator/=(const
_fixed &rhs) ETL_ATTRIB_INLINE;
136 const
_fixed& operator*=(const
int &rhs) ETL_ATTRIB_INLINE;
137 const
_fixed& operator/=(const
int &rhs) ETL_ATTRIB_INLINE;
140 template<typename U>
_fixed operator+(const U &rhs)const ETL_ATTRIB_INLINE;
141 template<typename U>
_fixed operator-(const U &rhs)const ETL_ATTRIB_INLINE;
142 template<typename U>
_fixed operator*(const U &rhs)const ETL_ATTRIB_INLINE;
143 template<typename U>
_fixed operator/(const U &rhs)const ETL_ATTRIB_INLINE;
144 _fixed operator+(const
_fixed &rhs)const ETL_ATTRIB_INLINE;
145 _fixed operator-(const
_fixed &rhs)const ETL_ATTRIB_INLINE;
146 _fixed operator*(const
_fixed &rhs)const ETL_ATTRIB_INLINE;
147 _fixed operator/(const
_fixed &rhs)const ETL_ATTRIB_INLINE;
148 _fixed operator*(const
int &rhs)const ETL_ATTRIB_INLINE;
149 _fixed operator/(const
int &rhs)const ETL_ATTRIB_INLINE;
150 _fixed operator*(const
float &rhs)const ETL_ATTRIB_INLINE;
151 _fixed operator*(const
double &rhs)const ETL_ATTRIB_INLINE;
154 _fixed operator-()const ETL_ATTRIB_INLINE;
157 inline operator
float()const ETL_ATTRIB_INLINE;
158 inline operator
double()const ETL_ATTRIB_INLINE;
159 inline operator
long double()const ETL_ATTRIB_INLINE;
160 inline operator
int()const ETL_ATTRIB_INLINE;
161 inline operator
bool()const ETL_ATTRIB_INLINE;
167 bool operator==(const
_fixed &rhs)
const {
return data()==rhs.data(); }
176 template <
class T,
unsigned int FIXED_BITS>
180 template <
class T,
unsigned int FIXED_BITS>
184 template <
class T,
unsigned int FIXED_BITS>
186 #ifdef ROUND_TO_NEAREST_INTEGER
191 template <
class T,
unsigned int FIXED_BITS>
193 #ifdef ROUND_TO_NEAREST_INTEGER
198 template <
class T,
unsigned int FIXED_BITS>
200 #ifdef ROUND_TO_NEAREST_INTEGER
205 template <
class T,
unsigned int FIXED_BITS>
209 template <
class T,
unsigned int FIXED_BITS>
212 template <
class T,
unsigned int FIXED_BITS>
217 template <
class T,
unsigned int FIXED_BITS>
inline bool
220 return sizeof(T)<
sizeof(
int);
223 template <
class T,
unsigned int FIXED_BITS>
inline bool
226 return sizeof(T)*8==FIXED_BITS;
229 template <
class T,
unsigned int FIXED_BITS>
inline T
232 return static_cast<T
>((_USING_ALL_BITS()?~T(0):1<<FIXED_BITS));
235 template <
class T,
unsigned int FIXED_BITS>
inline T
238 return static_cast<T
>(_USING_ALL_BITS()?~T(0):_ONE()-1);
241 template <
class T,
unsigned int FIXED_BITS>
inline float
244 return 1.0f/((float)_ONE()*2);
248 template <
class T,
unsigned int FIXED_BITS>T &
254 template <
class T,
unsigned int FIXED_BITS>
const T &
280 if(_TYPE_SMALLER_THAN_INT())
281 _data=
static_cast<T
>((int)_data*(
int)rhs.
_data>>FIXED_BITS);
295 if(_TYPE_SMALLER_THAN_INT())
296 _data=
static_cast<T
>((int)_data/(
int)rhs.
_data<<FIXED_BITS);
321 _data*=rhs;
return *
this;
328 _data/=rhs;
return *
this;
417 return (*
this)*
_fixed(rhs);
424 return (*
this)*
_fixed(rhs);
465 template <
class T,
unsigned int FIXED_BITS>
468 return static_cast<float>(_data)/static_cast<float>(_ONE());
471 template <
class T,
unsigned int FIXED_BITS>
474 return static_cast<double>(_data)/static_cast<double>(_ONE());
477 template <
class T,
unsigned int FIXED_BITS>
480 return static_cast<long double>(_data)/static_cast<long double>(_ONE());
483 template <
class T,
unsigned int FIXED_BITS>
486 return static_cast<int>(_data>>FIXED_BITS);
489 template <
class T,
unsigned int FIXED_BITS>
492 return static_cast<bool>(_data);
500 ret.
_data&=~_F_MASK();
508 if(ret.
_data&_F_MASK())
511 ret.
_data&=~_F_MASK();
519 ret.
_data+=_ONE()>>1;
520 ret.
_data&=~_F_MASK();
555 template <
class T,
unsigned int FIXED_BITS>
556 inline _ETL::fixed_base<T,FIXED_BITS>
557 ceil(
const _ETL::fixed_base<T,FIXED_BITS> &rhs)
558 {
return rhs.ceil(); }
560 template <
class T,
unsigned int FIXED_BITS>
561 _ETL::fixed_base<T,FIXED_BITS>
562 floor(
const _ETL::fixed_base<T,FIXED_BITS> &rhs)
563 {
return rhs.floor(); }
565 template <
class T,
unsigned int FIXED_BITS>
566 _ETL::fixed_base<T,FIXED_BITS>
567 round(
const _ETL::fixed_base<T,FIXED_BITS> &rhs)
568 {
return rhs.round(); }
570 template <
class T,
unsigned int FIXED_BITS>
571 _ETL::fixed_base<T,FIXED_BITS>
572 abs(
const _ETL::fixed_base<T,FIXED_BITS> &rhs)
573 {
return rhs<_ETL::fixed_base<T,FIXED_BITS>(0)?-rhs:rhs; }
604 #if defined(__GNUC__) && __GNUC__ == 3
605 template <
class T,
unsigned int FIXED_BITS,
typename U> U
606 operator*(
const U &a,
const _ETL::fixed_base<T,FIXED_BITS> &b)
607 {
return a*
static_cast<double>(b); }
609 template <
class T,
unsigned int FIXED_BITS,
typename U> U
610 operator/(
const U &a,
const _ETL::fixed_base<T,FIXED_BITS> &b)
611 {
return a/
static_cast<double>(b); }
613 template <
class T,
unsigned int FIXED_BITS,
typename U> U
614 operator+(
const U &a,
const _ETL::fixed_base<T,FIXED_BITS> &b)
615 {
return a+
static_cast<double>(b); }
617 template <
class T,
unsigned int FIXED_BITS,
typename U> U
618 operator-(
const U &a,
const _ETL::fixed_base<T,FIXED_BITS> &b)
619 {
return a-
static_cast<double>(b); }