Tester to compare custom Real to IEEE float
[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_FLOAT
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(float 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) {return Real(value+f);}
34                 Real operator-(float f) {return Real(value+f);}
35                 Real operator/(float f) {return Real(value/f);}
36                 Real operator*(float f) {return Real(value*f);}
37                 Real operator+(const Real & r) {return this->operator+(r.value);}
38                 Real operator-(const Real & r) {return this->operator-(r.value);}
39                 Real operator*(const Real & r) {return this->operator*(r.value);}
40                 Real operator/(const Real & r) {return this->operator/(r.value);}
41                 float value;
42         };
43         inline float Float(Real r) {return r.value;}
44
45         inline std::ostream & operator<<(std::ostream & os, Real & r) {return os << r.value;} // yuk
46
47 #endif //REAL_HALF
48
49 }
50
51 #endif //_REAL_H

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