Merge branch 'master' of git.ucc.asn.au:ipdf/code
[ipdf/code.git] / src / real.h
1 #ifndef _REAL_H
2 #define _REAL_H
3
4 #include "common.h"
5
6 namespace IPDF
7 {
8
9 #define REAL_SINGLE
10 //#define REAL_DOUBLE
11 //#define REAL_HALF
12
13 #ifdef REAL_SINGLE
14         typedef float Real;
15         inline float Float(Real r) {return r;}
16 #elif defined REAL_DOUBLE
17         typedef double Real;
18         inline double Float(Real r) {return r;}
19 #elif defined REAL_HALF
20         struct Real
21         {
22                 Real() = default;
23                 Real(double r) : value(r) 
24                 {
25                         int & a = *((int*)(&value)); // um...
26                         // mask out extra bits in exponent
27                          //1000 1111 1000 0000 0000 0011 1111 1111
28                         // Endianness matters
29                         a &= 0xFFC001F1; //0x8F8003FF;
30
31                 }       
32         
33                 Real operator+(float f) const {return Real(value+f);}
34                 Real operator-(float f) const {return Real(value+f);}
35                 Real operator/(float f) const {return Real(value/f);}
36                 Real operator*(float f) const {return Real(value*f);}
37                 Real operator+(const Real & r) const {return Real(this->value + r.value);}
38                 Real operator-(const Real & r) const {return Real(this->value - r.value);}
39                 Real operator*(const Real & r) const {return Real(this->value * r.value);}
40                 Real operator/(const Real & r) const {return Real(this->value / r.value);}
41                 Real & operator+=(const Real & r) {this->value += r.value; return *this;}
42                 Real & operator-=(const Real & r) {this->value -= r.value; return *this;}
43                 Real & operator/=(const Real & r) {this->value /= r.value; return *this;}
44                 Real & operator*=(const Real & r) {this->value *= r.value; return *this;}
45
46                 float value;
47         };
48         inline float Float(Real r) {return r.value;}
49
50         inline std::ostream & operator<<(std::ostream & os, Real & r) {return os << r.value;} // yuk
51
52 #endif //REAL_HALF
53
54 }
55
56 #endif //_REAL_H

UCC git Repository :: git.ucc.asn.au