8444afd27a31feb4761d20a4a9f2d8df4b7f10ac
[ipdf/code.git] / src / vfpu.h
1 /**
2  * @file vfpu.h
3  * @brief Implements a terrible and hacky interface to use a virtual FPU to do floating point operations
4  */
5
6 #ifndef _VFPU_H
7 #define _VFPU_H
8
9 #include <bitset>
10 namespace VFPU
11 {
12         extern int Start(const char * vcd_output = NULL); // Starts the VFPU
13         extern int Halt(); // Halts the VFPU
14         typedef enum {ADD=0x0, SUB=0x1, MULT=0x2, DIV=0x3, SQRT=0x4} Opcode;
15         typedef enum {EVEN=0x0, ZERO=0x1, UP=0x2, DOWN=0x3} Rmode; // Rounding mode; to even, towards zero, always up, always down
16         typedef std::bitset<32> Register;
17         extern Register Exec(const Register & a, const Register & b, Opcode op, Rmode rmode = EVEN); // operate with registers
18         extern float Exec(float a, float b, Opcode op, Rmode rmode = EVEN); //converts floats into registers and back
19         
20         class Float
21         {
22                 public:
23                         Float(float f = 0) : m_value(f) 
24                         {
25                                 static bool init = false;
26                                 if (!init)
27                                 {
28                                         init = true;
29                                         VFPU::Start();
30                                 }
31                         }
32                         Float(const Float & cpy) : m_value(cpy.m_value) {}
33                         virtual ~Float() 
34                         {
35
36                         }
37                         
38                         Float & operator+=(const Float & op)
39                         {
40                                 m_value = Exec(m_value, op.m_value, ADD);
41                                 return *this;
42                         }
43                         Float & operator-=(const Float & op)
44                         {
45                                 m_value = Exec(m_value, op.m_value, SUB);
46                                 return *this;
47                         }
48                         Float & operator*=(const Float & op)
49                         {
50                                 m_value = Exec(m_value, op.m_value, MULT);
51                                 return *this;
52                         }
53                         Float & operator/=(const Float & op)
54                         {
55                                 m_value = Exec(m_value, op.m_value, DIV);
56                                 return *this;
57                         }
58                         
59                         Float operator+(const Float & op) const {Float f(*this); f+=op; return f;}
60                         Float operator-(const Float & op) const {Float f(*this); f-=op; return f;}
61                         Float operator*(const Float & op) const {Float f(*this); f*=op; return f;}
62                         Float operator/(const Float & op) const {Float f(*this); f/=op; return f;}
63                         
64                         bool operator==(const Float & op) const
65                         {
66                                 Float f(op);
67                                 f -= *this;
68                                 return (f.m_value == 0);                                
69                         }
70                         bool operator!=(const Float & op) const {return !this->operator==(op);}
71                         bool operator<(const Float & op) const
72                         {
73                                 Float f(op);
74                                 f -= *this;
75                                 return (f.m_value > 0);
76                         }
77                         bool operator<=(const Float & op) const
78                         {
79                                 Float f(op);
80                                 f -= *this;
81                                 return (f.m_value >= 0);                                
82                         }
83                         bool operator>(const Float & op) const {return !this->operator<=(op);}
84                         bool operator>=(const Float & op) const {return !this->operator<(op);}
85                         
86                         float m_value;
87                         
88         };
89 }
90
91 #endif //_VFPU_H
92
93

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