From: Sam Moore Date: Tue, 29 Jul 2014 14:36:53 +0000 (+0800) Subject: Add VFPU::Float (but it is broken) X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=29f315953466e71fc76b1b05c79c1231e5c83074;p=ipdf%2Fcode.git Add VFPU::Float (but it is broken) Fails every single realops test. Also it's stuck at 32 bits still. --- diff --git a/src/real.h b/src/real.h index 798ea0b..4b2db1c 100644 --- a/src/real.h +++ b/src/real.h @@ -7,7 +7,7 @@ #define REAL_SINGLE 0 #define REAL_DOUBLE 1 #define REAL_LONG_DOUBLE 2 -#define REAL_SINGLE_FAST2SUM 3 //TODO: Remove, is FITH +#define REAL_VFPU 3 #define REAL_RATIONAL 4 #define REAL_RATIONAL_ARBINT 5 @@ -15,9 +15,9 @@ #error "REAL was not defined!" #endif -#if REAL >= REAL_SINGLE_FAST2SUM - #include "real_fast2sum.h" -#endif //REAL +#if REAL == REAL_VFPU + #include "vfpu.h" +#endif #if REAL == REAL_RATIONAL #include "rational.h" @@ -39,8 +39,8 @@ namespace IPDF typedef double Real; #elif REAL == REAL_LONG_DOUBLE typedef long double Real; -#elif REAL == REAL_SINGLE_FAST2SUM - typedef RealF2S Real; +#elif REAL == REAL_VFPU + typedef VFPU::Float Real; inline float Float(const Real & r) {return r.m_value;} inline double Double(const Real & r) {return r.m_value;} #elif REAL == REAL_RATIONAL diff --git a/src/vfpu.cpp b/src/vfpu.cpp index 18e1bde..b59fe3e 100644 --- a/src/vfpu.cpp +++ b/src/vfpu.cpp @@ -21,7 +21,7 @@ using namespace std; namespace VFPU { - + static const char g_fpu[] = "vfpu"; static bool g_running = false; @@ -114,10 +114,9 @@ float Exec(float opa, float opb, Opcode op, Rmode rmode) Register Exec(const Register & a, const Register & b, Opcode op, Rmode rmode) { assert(g_running); - Fatal("Unsupported"); stringstream s; - //TODO: Make it compile again - //s << hex << setw(8) << setfill('0') << a.to_ullong() << "\n" << b.to_ullong() << "\n" << setw(1) << op <<"\n" << setw(1) << rmode << "\n"; + //TODO: Make it compile on non C++11 + s << hex << setw(8) << setfill('0') << a.to_ullong() << "\n" << b.to_ullong() << "\n" << setw(1) << op <<"\n" << setw(1) << rmode << "\n"; string str(s.str()); //Debug("Writing: %s", str.c_str()); @@ -139,8 +138,8 @@ Register Exec(const Register & a, const Register & b, Opcode op, Rmode rmode) } stringstream s2; - //TODO: Make it comp[ile again - //s2 << hex << result.to_ullong(); + //TODO: Make it compile on non C++11 + s2 << hex << result.to_ullong(); //Debug("Result is: %s", s2.str().c_str()); return result; } diff --git a/src/vfpu.h b/src/vfpu.h index d77c1e3..04de136 100644 --- a/src/vfpu.h +++ b/src/vfpu.h @@ -16,6 +16,81 @@ namespace VFPU typedef std::bitset<32> Register; extern Register Exec(const Register & a, const Register & b, Opcode op, Rmode rmode = EVEN); // operate with registers extern float Exec(float a, float b, Opcode op, Rmode rmode = EVEN); //converts floats into registers and back + + class Float + { + public: + Float(float f = 0) : m_value(f) + { + static bool init = false; + if (!init) + { + init = true; + VFPU::Start(); + } + } + Float(const Float & cpy) : m_value(cpy.m_value) {} + virtual ~Float() + { + + } + + Float & operator+=(const Float & op) + { + m_value = Exec(m_value, op.m_value, ADD); + return *this; + } + Float & operator-=(const Float & op) + { + m_value = Exec(m_value, op.m_value, SUB); + return *this; + } + Float & operator*=(const Float & op) + { + m_value = Exec(m_value, op.m_value, MULT); + return *this; + } + Float & operator/=(const Float & op) + { + m_value = Exec(m_value, op.m_value, DIV); + return *this; + } + + Float operator+(const Float & op) const {Float f(*this); f+=op; return f;} + Float operator-(const Float & op) const {Float f(*this); f-=op; return f;} + Float operator*(const Float & op) const {Float f(*this); f*=op; return f;} + Float operator/(const Float & op) const {Float f(*this); f/=op; return f;} + + bool operator==(const Float & op) const + { + Float f(op); + f -= *this; + return (f.m_value == 0); + } + bool operator!=(const Float & op) const {return !this->operator==(op);} + bool operator<(const Float & op) const + { + Float f(op); + f -= *this; + return (f.m_value > 0); + } + bool operator<=(const Float & op) const + { + Float f(op); + f -= *this; + return (f.m_value >= 0); + } + bool operator>(const Float & op) const {return !this->operator<=(op);} + bool operator>=(const Float & op) const {return !this->operator<(op);} + + float m_value; + + }; + + inline Float pow(const Float & a, const Float & b) + { + return Float(pow(a.m_value, b.m_value)); + } } #endif //_VFPU_H