artkpFloat_fixed.h
Go to the documentation of this file.
1 /* ========================================================================
2  * PROJECT: ARToolKitPlus
3  * ========================================================================
4  * This work is based on the original ARToolKit developed by
5  * Hirokazu Kato
6  * Mark Billinghurst
7  * HITLab, University of Washington, Seattle
8  * http://www.hitl.washington.edu/artoolkit/
9  *
10  * Copyright of the derived and new portions of this work
11  * (C) 2006 Graz University of Technology
12  *
13  * This framework is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This framework is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this framework; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * For further information please contact
28  * Dieter Schmalstieg
29  * <schmalstieg@icg.tu-graz.ac.at>
30  * Graz University of Technology,
31  * Institut for Computer Graphics and Vision,
32  * Inffeldgasse 16a, 8010 Graz, Austria.
33  * ========================================================================
34  ** @author Daniel Wagner
35  *
36  * $Id$
37  * @file
38  * ======================================================================== */
39 
40 
41 #ifndef __ARTKPFLOAT_FIXED_HEADERFILE__
42 #define __ARTKPFLOAT_FIXED_HEADERFILE__
43 
44 #include <math.h>
45 #include <assert.h>
46 
47 #pragma warning ( push )
48 #pragma warning ( disable: 4293 ) // fix warning about shifting in conversion constructor
49 
50 // fixed point implementation of artkpFloat
51 //
52 // MATHBASE provides concrete methods for
53 // internal usage. while artkpFloat_fixed is platform
54 // independent, MATHBASE is the correct place
55 // to implement hardware specific optimizations.
56 // (see artkpFixedBase_generic & artkpFixedBase_gpp)
57 //
58 
59 template<typename MATHBASE>
61 {
62 public:
63  enum {
64  PBITS = MATHBASE::PBITS,
65  CHECK = MATHBASE::CHECK
66  };
67 
69  artkpFloat_fixed(float nV) { setFloat(nV); }
70  artkpFloat_fixed(double nV) { setDouble(nV); }
71  artkpFloat_fixed(const artkpFloat_fixed& nOther) { v = nOther.v; }
72 
73  // conversion constructor. allows assigning values of different precision types
74  template <typename OTHER_MATHBASE>
76  {
77  if(OTHER_MATHBASE::PBITS<PBITS)
78  {
79  if(CHECK)
80  {
81  // check if the conversion resulted in an overflow
82  __int64 v64 = ((__int64)nOther.getFixed()) << (PBITS-OTHER_MATHBASE::PBITS);
83  if(v64 != int(v))
84  {
85  assert(false && "Fixed-Point to Fixed-Point conversion failed: the target's range was overflowed");
86  }
87  }
88  v = nOther.getFixed() << (PBITS-OTHER_MATHBASE::PBITS);
89  }
90  else
91  {
92  v = nOther.getFixed() >> (OTHER_MATHBASE::PBITS-PBITS);
93  }
94  }
95 
96  // custom interface
97  //
98  bool isFixed() { return true; }
99 
100  void setFixed(int nV) { v = nV; }
101  void setInt(int nV) { v = MATHBASE::fixedFromInt(nV); }
102  void setFloat(float nV) { v = MATHBASE::fixedFromFloat(nV); }
103  void setDouble(double nV) { v = MATHBASE::fixedFromDouble(nV); }
104  void setArtkpFloat(const artkpFloat_fixed& nOther) { v = nOther.v; }
105 
106  int getByte() const { return v>255<<(PBITS-8) ? 255 : v>>(PBITS-8); }
107  int getFixed() const { return v; }
108  int getInt() const { return v>>PBITS; }
109  float getFloat() const { return MATHBASE::floatFromFixed(v); }
110  double getDouble() const { return MATHBASE::doubleFromFixed(v); }
111 
112  void inverse(const artkpFloat_fixed& nOther) { v = MATHBASE::inverse(nOther.v); }
113  void inverse() { inverse(*this); }
114 
115  void inverseSqrt(const artkpFloat_fixed& nOther) { v = MATHBASE::inverseSqrt(nOther.v); }
116  void inverseSqrt() { inverseSqrt(*this); }
117 
118  void multiplyBy255() { int vOld = v; v<<=8; v-=vOld; }
119 
120 
121  // some standard math.h routines applied to this class
122  //
123  friend inline artkpFloat_fixed cos(const artkpFloat_fixed& nV)
124  { return MATHBASE::cos(nV.v); }
125 
126 
127  // overloaded operators
128  //
129  artkpFloat_fixed& operator=(int nV) { setInt(nV); return *this; }
130  artkpFloat_fixed& operator=(float nV) { setFloat(nV); return *this; }
131  artkpFloat_fixed& operator=(double nV) { setDouble(nV); return *this; }
132  artkpFloat_fixed& operator=(const artkpFloat_fixed& nOther) { v = nOther.v; return *this; }
133 
134  artkpFloat_fixed& operator+=(int nV) { v += MATHBASE::fixedFromInt(nV); return *this; }
135  artkpFloat_fixed& operator-=(int nV) { v -= MATHBASE::fixedFromInt(nV); return *this; }
136  artkpFloat_fixed& operator*=(int nV) { v = MATHBASE::multiply(v, MATHBASE::fixedFromInt(nV)); return *this; }
137  artkpFloat_fixed& operator/=(int nV) { v = MATHBASE::divide(v, MATHBASE::fixedFromInt(nV)); return *this; }
138 
139  artkpFloat_fixed& operator+=(float nV) { v += MATHBASE::fixedFromFloat(nV); return *this; }
140  artkpFloat_fixed& operator-=(float nV) { v -= MATHBASE::fixedFromFloat(nV); return *this; }
141  artkpFloat_fixed& operator*=(float nV) { v = MATHBASE::multiply(v, MATHBASE::fixedFromFloat(nV)); return *this; }
142  artkpFloat_fixed& operator/=(float nV) { v = MATHBASE::divide(v, MATHBASE::fixedFromFloat(nV)); return *this; }
143 
144  artkpFloat_fixed& operator+=(double nV) { v += MATHBASE::fixedFromDouble(nV); return *this; }
145  artkpFloat_fixed& operator-=(double nV) { v -= MATHBASE::fixedFromDouble(nV); return *this; }
146  artkpFloat_fixed& operator*=(double nV) { v = MATHBASE::multiply(v, fixedFromDouble(nV)); return *this; }
147  artkpFloat_fixed& operator/=(double nV) { v = MATHBASE::divide(v, fixedFromDouble(nV)); return *this; }
148 
149  artkpFloat_fixed& operator+=(const artkpFloat_fixed& nOther) { v+=nOther.v; return *this; }
150  artkpFloat_fixed& operator-=(const artkpFloat_fixed& nOther) { v-=nOther.v; return *this; }
151  artkpFloat_fixed& operator*=(const artkpFloat_fixed& nOther) { v = MATHBASE::multiply(v, nOther.v); return *this; }
152  artkpFloat_fixed& operator/=(const artkpFloat_fixed& nOther) { v = MATHBASE::divide(v, nOther.v); return *this; }
153 
154  artkpFloat_fixed& operator>>=(int nBits) { v>>=nBits; return *this; }
155  artkpFloat_fixed& operator<<=(int nBits) { v<<=nBits; return *this; }
156 
157  bool operator==(const artkpFloat_fixed& nOther) const { return v==nOther.v; }
158  bool operator!=(const artkpFloat_fixed& nOther) const { return v!=nOther.v; }
159  bool operator<=(const artkpFloat_fixed& nOther) const { return v<=nOther.v; }
160  bool operator>=(const artkpFloat_fixed& nOther) const { return v>=nOther.v; }
161  bool operator<(const artkpFloat_fixed& nOther) const { return v<nOther.v; }
162  bool operator>(const artkpFloat_fixed& nOther) const { return v>nOther.v; }
163 
164  bool operator==(int nOther) const { return v == MATHBASE::fixedFromInt(nOther); }
165  bool operator!=(int nOther) const { return v != MATHBASE::fixedFromInt(nOther); }
166  bool operator<=(int nOther) const { return v <= MATHBASE::fixedFromInt(nOther); }
167  bool operator>=(int nOther) const { return v >= MATHBASE::fixedFromInt(nOther); }
168  bool operator<(int nOther) const { return v < MATHBASE::fixedFromInt(nOther); }
169  bool operator>(int nOther) const { return v > MATHBASE::fixedFromInt(nOther); }
170 
171  bool operator==(float nOther) const { return v == MATHBASE::fixedFromFloat(nOther); }
172  bool operator!=(float nOther) const { return v != MATHBASE::fixedFromFloat(nOther); }
173  bool operator<=(float nOther) const { return v <= MATHBASE::fixedFromFloat(nOther); }
174  bool operator>=(float nOther) const { return v >= MATHBASE::fixedFromFloat(nOther); }
175  bool operator<(float nOther) const { return v < MATHBASE::fixedFromFloat(nOther); }
176  bool operator>(float nOther) const { return v > MATHBASE::fixedFromFloat(nOther); }
177 
179  { return artkpFloat_fixed(left.v+right.v); }
180  friend artkpFloat_fixed operator+ (const artkpFloat_fixed& left, float right)
181  { return artkpFloat_fixed(left.v+MATHBASE::fixedFromFloat(right)); }
182  friend artkpFloat_fixed operator+ (float left, const artkpFloat_fixed& right)
183  { return artkpFloat_fixed(MATHBASE::fixedFromFloat(left)+right.v); }
184 
186  { return artkpFloat_fixed(left.v-right.v); }
187  friend artkpFloat_fixed operator- (const artkpFloat_fixed& left, float right)
188  { return artkpFloat_fixed(left.v-MATHBASE::fixedFromFloat(right)); }
189  friend artkpFloat_fixed operator- (float left, const artkpFloat_fixed& right)
190  { return artkpFloat_fixed(MATHBASE::fixedFromFloat(left)-right.v); }
191 
193  { return artkpFloat_fixed(MATHBASE::multiply(left.v, right.v)); }
194  friend artkpFloat_fixed operator* (const artkpFloat_fixed& left, float right)
195  { return artkpFloat_fixed(MATHBASE::multiply(left.v, MATHBASE::fixedFromFloat(right))); }
196  friend artkpFloat_fixed operator* (float left, const artkpFloat_fixed& right)
197  { return artkpFloat_fixed(MATHBASE::multiply(MATHBASE::fixedFromFloat(left), right.v)); }
198 
200  { return artkpFloat_fixed(MATHBASE::divide(left.v, right.v)); }
201  friend artkpFloat_fixed operator/ (const artkpFloat_fixed& left, float right)
202  { return artkpFloat_fixed(MATHBASE::divide(left.v, MATHBASE::fixedFromFloat(right))); }
203  friend artkpFloat_fixed operator/ (float left, const artkpFloat_fixed& right)
204  { return artkpFloat_fixed(MATHBASE::divide(MATHBASE::fixedFromFloat(left), right.v)); }
205 
207  { artkpFloat_fixed w; w.v = -v; return w; }
208 
209 protected:
210  artkpFloat_fixed(int nV) { v = nV; } // constructor with fixed point parameter
211 
212  int v;
213 };
214 
215 
216 #pragma warning ( pop )
217 
218 
219 #endif //__ARTKPFLOAT_FIXED_HEADERFILE__
artkpFloat_fixed & operator*=(float nV)
void setFloat(float nV)
artkpFloat_fixed & operator<<=(int nBits)
bool operator>=(int nOther) const
double getDouble() const
artkpFloat_fixed(const artkpFloat_fixed &nOther)
float getFloat() const
bool operator<=(const artkpFloat_fixed &nOther) const
friend artkpFloat_fixed cos(const artkpFloat_fixed &nV)
artkpFloat_fixed & operator=(const artkpFloat_fixed &nOther)
artkpFloat_fixed & operator/=(int nV)
bool operator>(int nOther) const
artkpFloat_fixed & operator+=(float nV)
bool operator<(float nOther) const
artkpFloat_fixed & operator/=(const artkpFloat_fixed &nOther)
bool operator==(float nOther) const
bool operator>(float nOther) const
artkpFloat_fixed & operator=(float nV)
bool operator<=(float nOther) const
void inverseSqrt(const artkpFloat_fixed &nOther)
void setFixed(int nV)
bool operator<=(int nOther) const
void setInt(int nV)
artkpFloat_fixed & operator+=(double nV)
artkpFloat_fixed(float nV)
artkpFloat_fixed operator-() const
bool operator!=(float nOther) const
bool operator>(const artkpFloat_fixed &nOther) const
artkpFloat_fixed & operator*=(const artkpFloat_fixed &nOther)
artkpFloat_fixed & operator=(double nV)
bool operator==(int nOther) const
bool operator==(const artkpFloat_fixed &nOther) const
bool operator>=(const artkpFloat_fixed &nOther) const
artkpFloat_fixed & operator=(int nV)
int getInt() const
void setDouble(double nV)
friend artkpFloat_fixed operator/(const artkpFloat_fixed &left, const artkpFloat_fixed &right)
bool operator<(const artkpFloat_fixed &nOther) const
bool operator!=(int nOther) const
int getByte() const
friend artkpFloat_fixed operator+(const artkpFloat_fixed &left, const artkpFloat_fixed &right)
artkpFloat_fixed & operator*=(int nV)
artkpFloat_fixed & operator/=(double nV)
void inverse(const artkpFloat_fixed &nOther)
artkpFloat_fixed & operator>>=(int nBits)
TFSIMD_FORCE_INLINE const tfScalar & w() const
artkpFloat_fixed(const artkpFloat_fixed< OTHER_MATHBASE > &nOther)
friend artkpFloat_fixed operator*(const artkpFloat_fixed &left, const artkpFloat_fixed &right)
artkpFloat_fixed & operator-=(const artkpFloat_fixed &nOther)
artkpFloat_fixed & operator*=(double nV)
artkpFloat_fixed & operator/=(float nV)
bool operator<(int nOther) const
artkpFloat_fixed & operator-=(int nV)
bool operator!=(const artkpFloat_fixed &nOther) const
artkpFloat_fixed(double nV)
artkpFloat_fixed & operator-=(double nV)
artkpFloat_fixed & operator+=(int nV)
artkpFloat_fixed & operator-=(float nV)
void setArtkpFloat(const artkpFloat_fixed &nOther)
bool operator>=(float nOther) const
int getFixed() const
artkpFloat_fixed & operator+=(const artkpFloat_fixed &nOther)


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun Sep 4 2016 03:24:33