nmpp
tfixpoint.h
1#ifndef _tfixpoint
2#define _tfixpoint
3//#include <math.h>
4//#define STAT_MINMAX
5//#define STAT_FIXPOINT
6//#ifdef __NM__
7//#define _ASSERTE(a)
8//#else
9//#include <crtdbg.h>
10//#endif
11
12#define FIX_TYPE __int64
13#define ROUND2FIXED(a) ((a) >= 0 ? (((a)+0.5)) : (((a)-0.5)))
14
15#define FIXPOINT_ONE (T(1)<<point)
16
17#define DOUBLE2FIX64(a,point) (ROUND2FIXED((a)*((__int64(1))<<point)))
18#define DOUBLE2FIX32(a,point) (ROUND2FIXED((a)*(int(1)<<point))
19
20
21
22
23//#define FIX_POINT_ONE (1<<FIX_POINT)
24#define FLT2FIX(f) (DOUBLE2FIX64((f),(FIX_POINT)))
25
26#define MULI(a,i) ((a).m_value*(i))
27#define MULC(a,c) ((a.m_value*c)>>FIX_POINT)
28#define MULX(a,n) (((a).m_value*(n).m_value)>>FIX_POINT)
29
30#define ADDC(a,c) (a.m_value+c)
31#define ADDN(a,n) (a.m_value+n.m_value)
32#define SUBC(a,c) (a.m_value-c)
33#define SUBN(a,n) (a.m_value-n.m_value)
34
35//#define SHR(a,shift) (((a)+ ((1)<<((shift)-1)))>>shift)
36//#define FIX_RSH(a,shift) ((a)>>shift)
37#define SHR(a,shift) ((a)>>shift)
38
39
40//#ifndef _ASSERTE
41//#define _ASSERTE(a)
42//#endif
43
44#define DBL2FIX DOUBLE2FIX64
45
46#ifdef __NM__
47 #define INLINE
48#else
49 #define INLINE __INLINE__
50#endif
51
52template<class T, int point> class tfixpoint{
53public:
54 T m_value; // m_value
55 //---------- 0 operands constructors --------------------------------------------------
56 INLINE tfixpoint(void){}
57 //---------- 1 operands constructors --------------------------------------------------
58
59
60 INLINE tfixpoint(const int val)
61 {
62 m_value=T(val)<<point;
63 //_ASSERTE(m_value<(T(1)<<32));
64 //_ASSERTE(m_value>(-(T(1)<<32)));
65 }
66
67 INLINE tfixpoint(const float val)
68 {
69 m_value=ROUND2FIXED(val*FIXPOINT_ONE);
70 //_ASSERTE(m_value<(T(1)<<32));
71 //_ASSERTE(m_value>(-(T(1)<<32)));
72 }
73 INLINE tfixpoint(const double val)
74 {
75 m_value=ROUND2FIXED(val*FIXPOINT_ONE);
76 //_ASSERTE(m_value<(T(1)<<32));
77 //_ASSERTE(m_value>(-(T(1)<<32)));
78 }
79
80 //tfixpoint(const tfixpoint<T,point> val)
81 //{
82 // m_value=val.m_value;
83 //}
84
85 INLINE tfixpoint<T,point>& operator= (const int val)
86 {
87 m_value=T(val)<<point;
88 return (*this);
89 }
90
91 INLINE tfixpoint<T,point>& operator= (const float val)
92 {
93 m_value=ROUND2FIXED(val*FIXPOINT_ONE);
94 return (*this);
95 }
96
97 tfixpoint<T,point>& operator= (const double val)
98 {
99 m_value=ROUND2FIXED(val*FIXPOINT_ONE);
100 return (*this);
101 }
102
103 INLINE tfixpoint<T,point>& operator= (const tfixpoint<T,point>& val)
104 {
105 m_value=val.m_value;
106 return (*this);
107 }
108//#endif
109
110
111
112// tfixpoint<T,point>& operator= (const tfixpoint<T,point>& val)
113// {
114// m_value=val.m_value;
115// return (*this);
116// }
117
118// template <class T2> tfixpoint<T,point>& operator= (const T2& value)
119// {
120// m_value=value;
121// return (*this);
122// }
123
124
125 INLINE tfixpoint<T,point>& operator-= (const tfixpoint<T,point>& val)
126 {
127 #ifdef STAT_FIXPOINT
128 stat.subs++;
129 #endif
130 m_value-=val.m_value;
131 return (*this);
132 }
133
134
135 INLINE tfixpoint<T,point>& operator+= (const tfixpoint<T,point>& val)
136 {
137#ifdef STAT_FIXPOINT
138 stat.adds++;
139#endif
140 m_value+=val.m_value;
141 return (*this);
142 }
143
144 INLINE tfixpoint<T,point> operator- (const tfixpoint<T,point>& val) const
145 {
146 tfixpoint<T,point> Res(*this);
147 Res-=val;
148 return Res;
149 }
150
151 INLINE tfixpoint<T,point> operator+ (const tfixpoint<T,point>& val) const
152 {
153 tfixpoint<T,point> Res(*this);
154 Res+=val;
155 return Res;
156 }
157
158 INLINE tfixpoint<T,point>& operator++ (int)
159 {
160#ifdef STAT_FIXPOINT
161 stat.adds++;
162#endif
163 m_value+=FIXPOINT_ONE;
164 return (*this);
165 }
166
167 INLINE tfixpoint<T,point>& operator-- (int)
168 {
169#ifdef STAT_FIXPOINT
170 stat.subs++;
171#endif
172 m_value-=FIXPOINT_ONE;
173 return (*this);
174 }
175
176
177
178
179
180 template<class T2, int point2> INLINE tfixpoint<T,point>& operator*= (const tfixpoint<T2,point2> val)
181 {
182#ifdef STAT_FIXPOINT
183 stat.muls++;
184#endif
185 m_value*=val.m_value;
186 m_value+=(T(1)<<(point2-1));
187 m_value>>=point2;
188 return (*this);
189 }
190
191 INLINE tfixpoint<T,point>& operator*= (const int val)
192 {
193#ifdef STAT_FIXPOINT
194 stat.muls++;
195#endif
196 m_value*=val;
197 return (*this);
198 }
199
200 INLINE tfixpoint<T,point>& operator*= (const float val)
201 {
202#ifdef STAT_FIXPOINT
203 stat.muls++;
204#endif
205 m_value=ROUND2FIXED(m_value*val);
206 return (*this);
207 }
208
209 INLINE tfixpoint<T,point>& operator*= (const double val)
210 {
211#ifdef STAT_FIXPOINT
212 stat.muls++;
213#endif
214 //m_value*=val;
215 m_value=ROUND2FIXED(m_value*val);
216 return (*this);
217 }
218
219 template <int point2> INLINE tfixpoint<T,point> operator* (const tfixpoint<T,point2> val) const
220 {
221 tfixpoint<T,point> Res(*this);
222 Res*=val;
223 return Res;
224 }
225
226 INLINE tfixpoint<T,point> operator* (const int val) const
227 {
228 tfixpoint<T,point> Res(*this);
229 Res*=val;
230 return Res;
231 }
232
233/*
234 INLINE tfixpoint<T,point> operator* (const float val) const
235 {
236 tfixpoint<T,point> Res(*this);
237 Res*=val;
238 return Res;
239 }
240
241 INLINE tfixpoint<T,point> operator* (const double val) const
242 {
243 tfixpoint<T,point> Res(*this);
244 Res*=val;
245 return Res;
246 }
247*/
248 INLINE tfixpoint<T,point>& operator/= (const tfixpoint<T,point> val)
249 {
250#ifdef STAT_FIXPOINT
251 stat.divs++;
252#endif
253 long long val64=m_value;
254 val64<<=point;
255 val64/=val.m_value;
256 m_value=val64;
257 return (*this);
258 }
259
260 INLINE tfixpoint<T,point> operator/ (const tfixpoint<T,point> val) const
261 {
262 tfixpoint<T,point> Res(*this);
263 Res/=val;
264 return Res;
265 }
266
267 INLINE tfixpoint<T,point>& operator>>= (const int y)
268 {
269 m_value>>=y;
270 return (*this);
271 }
272
273 INLINE tfixpoint<T,point> operator>> (const int y) const
274 {
275 tfixpoint<T,point> z(*this);
276 z>>=y;
277 return z;
278 }
279
280 INLINE tfixpoint<T,point>& operator<<= (const int y)
281 {
282 m_value<<=y;
283 return (*this);
284 }
285
286 INLINE tfixpoint<T,point> operator<< (const int n) const
287 {
288 tfixpoint<T,point> Res(*this);
289 Res<<=n;
290 return Res;
291 }
292
293 INLINE tfixpoint<T,point>& operator^= (tfixpoint<T,point> &val)
294 {
295 m_value^=val.m_value;
296 return (*this);
297 }
298 INLINE tfixpoint<T,point> operator^ (tfixpoint<T,point> &val) const
299 {
300 tfixpoint<T,point> Res(*this);
301 Res^=val;
302 return Res;
303 }
304
305
306 INLINE tfixpoint<T,point> operator- () const
307 {
308#ifdef STAT_FIXPOINT
309 stat.subs++;
310#endif
312 Res.m_value=-m_value;
313 return Res;
314 }
315
316 bool operator> (const tfixpoint<T,point>& y) const
317 { return m_value>y.m_value;}
318 bool operator>= (const tfixpoint<T,point>& y) const
319 { return m_value>=y.m_value;}
320 bool operator< (const tfixpoint<T,point>& y) const
321 { return m_value<y.m_value;}
322 bool operator<= (const tfixpoint<T,point>& y) const
323 { return m_value<=y.m_value;}
324 bool operator== (const tfixpoint<T,point>& y) const
325 { return m_value==y.m_value;}
326 bool operator!= (const tfixpoint<T,point>& y) const
327 { return m_value!=y.m_value;}
328
329 float flt(){
330 float res=m_value;
331 res/=FIXPOINT_ONE;
332 return res;
333 }
334};
335
336//template class tfixpoint<float>;
337/*
338template <class T, int point> INLINE tfixpoint<T,point> pow (tfixpoint<T,point> a, tfixpoint<T,point> b) { return pow(a.flt(), b.flt());}
339template <class T, int point> INLINE tfixpoint<T,point> fabs(tfixpoint<T,point> a) { return fabs(a.flt());}
340template <class T, int point> INLINE tfixpoint<T,point> log (tfixpoint<T,point> a) { return log (a.flt());}
341template <class T, int point> INLINE tfixpoint<T,point> sqrt(tfixpoint<T,point> a) { return sqrt(a.flt());}
342template <class T, int point> INLINE tfixpoint<T,point> cos (tfixpoint<T,point> a) { return cos (a.flt());}
343template <class T, int point> INLINE tfixpoint<T,point> sin (tfixpoint<T,point> a) { return sin (a.flt());}
344*/
345
346//template class<int point> fixpoint32<point>: public ;
347
348#define fixpoint64(point) tfixpoint<__int64,point>
349#define fixpoint32(point) tfixpoint<int,point>
350
351//typedef template <int point> fixpoint32<int,point>;
352
353#define FIXPOINT64(val,point) (ROUND2FIXED((val)*((__int64(1))<<point)))
354
355double __INLINE__ cnv2double(double x){
356 return x;
357}
358
359float __INLINE__ cnv2float(float x){
360 return x;
361}
362
363template <class T, int point> double __INLINE__ cnv2double(tfixpoint<T,point>& x){
364 return x.flt();
365}
366template <class T, int point> float __INLINE__ cnv2float(tfixpoint<T,point>& x){
367 return x.flt();
368}
369
370#endif
Definition: tfixpoint.h:52