Terrible hacky SolveCubic.
[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  *      Updated with even more terror! Whatever floats the boat!
5  */
6
7 #ifndef _VFPU_H
8 #define _VFPU_H
9
10 #include <bitset>
11 namespace VFPU
12 {
13         extern int Start(const char * vcd_output = NULL); // Starts the VFPU
14         extern int Halt(); // Halts the VFPU
15         typedef enum {ADD=0x0, SUB=0x1, MULT=0x2, DIV=0x3, SQRT=0x4} Opcode;
16         typedef enum {EVEN=0x0, ZERO=0x1, UP=0x2, DOWN=0x3} Rmode; // Rounding mode; to even, towards zero, always up, always down
17         typedef std::bitset<32> Register;
18         extern Register Exec(const Register & a, const Register & b, Opcode op, Rmode rmode = EVEN); // operate with registers
19         extern float Exec(float a, float b, Opcode op, Rmode rmode = EVEN); //converts floats into registers and back
20         
21         /**
22          * Wrapper class for floats where operations are done on the VFPU
23          */
24         class VFloat
25         {
26                 public:
27                         VFloat(float f = 0) : m_value(f) 
28                         {
29                                 static bool init = false;
30                                 if (!init)
31                                 {
32                                         init = true;
33                                         VFPU::Start("flops.vcd");
34                                 }
35                         }
36                         VFloat(const VFloat & cpy) : m_value(cpy.m_value) {}
37                         virtual ~VFloat() 
38                         {
39
40                         }
41                         
42                         VFloat & operator+=(const VFloat & op)
43                         {
44                                 m_value = Exec(m_value, op.m_value, ADD);
45                                 return *this;
46                         }
47                         VFloat & operator-=(const VFloat & op)
48                         {
49                                 m_value = Exec(m_value, op.m_value, SUB);
50                                 return *this;
51                         }
52                         VFloat & operator*=(const VFloat & op)
53                         {
54                                 m_value = Exec(m_value, op.m_value, MULT);
55                                 return *this;
56                         }
57                         VFloat & operator/=(const VFloat & op)
58                         {
59                                 m_value = Exec(m_value, op.m_value, DIV);
60                                 return *this;
61                         }
62                         
63                         VFloat operator+(const VFloat & op) const {VFloat f(*this); f+=op; return f;}
64                         VFloat operator-(const VFloat & op) const {VFloat f(*this); f-=op; return f;}
65                         VFloat operator*(const VFloat & op) const {VFloat f(*this); f*=op; return f;}
66                         VFloat operator/(const VFloat & op) const {VFloat f(*this); f/=op; return f;}
67                         
68                         bool operator==(const VFloat & op) const
69                         {
70                                 VFloat f(op);
71                                 f -= *this;
72                                 return (f.m_value == 0);                                
73                         }
74                         bool operator!=(const VFloat & op) const {return !this->operator==(op);}
75                         bool operator<(const VFloat & op) const
76                         {
77                                 VFloat f(op);
78                                 f -= *this;
79                                 return (f.m_value > 0);
80                         }
81                         bool operator<=(const VFloat & op) const
82                         {
83                                 VFloat f(op);
84                                 f -= *this;
85                                 return (f.m_value >= 0);                                
86                         }
87                         bool operator>(const VFloat & op) const {return !this->operator<=(op);}
88                         bool operator>=(const VFloat & op) const {return !this->operator<(op);}
89                         
90                         float m_value;
91                         
92         };
93 }
94
95 #endif //_VFPU_H
96
97

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