Change to things to make performance testing easier
[ipdf/code.git] / src / real.h
1 #ifndef _REAL_H
2 #define _REAL_H
3
4 #include "common.h"
5 #include <cmath>
6
7
8 #define REAL_SINGLE 0
9 #define REAL_DOUBLE 1
10 #define REAL_LONG_DOUBLE 2
11 #define REAL_VFPU 3
12 #define REAL_RATIONAL 4
13 #define REAL_RATIONAL_ARBINT 5
14 #define REAL_MPFRCPP 6
15 #define REAL_IRRAM 7
16
17 #ifndef REALTYPE
18         #error "REALTYPE was not defined!"
19 #endif
20
21 #if REALTYPE == REAL_VFPU
22         #include "vfpu.h"
23 #endif
24
25 #if REALTYPE == REAL_RATIONAL
26         #include "rational.h"
27 #endif //REALTYPE
28
29 #if REALTYPE == REAL_RATIONAL_ARBINT
30         #include "rational.h"
31         #include "arbint.h"
32         #include "gmpint.h"
33 #endif //REALTYPE
34
35 #if REALTYPE == REAL_MPFRCPP
36         #include <mpreal.h>
37 #endif //REALTYPE
38
39 #if REALTYPE == REAL_IRRAM
40         #include "../contrib/iRRAM/include/iRRAM/lib.h"
41 #endif
42
43 namespace IPDF
44 {       
45         extern const char * g_real_name[];
46
47 #if REALTYPE == REAL_SINGLE
48         typedef float Real;
49         inline Real RealFromStr(const char * str) {return strtof(str, NULL);}
50 #elif REALTYPE == REAL_DOUBLE
51         typedef double Real;
52         inline Real RealFromStr(const char * str) {return strtod(str, NULL);}
53 #elif REALTYPE == REAL_LONG_DOUBLE
54         typedef long double Real;
55         inline Real RealFromStr(const char * str) {return strtold(str, NULL);}
56 #elif REALTYPE == REAL_VFPU
57         typedef VFPU::VFloat Real;
58         inline float Float(const Real & r) {return r.m_value;}
59         inline double Double(const Real & r) {return r.m_value;}
60         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
61 #elif REALTYPE == REAL_RATIONAL
62         typedef Rational<int64_t> Real;
63         inline float Float(const Real & r) {return (float)r.ToDouble();}
64         inline double Double(const Real & r) {return r.ToDouble();}
65         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
66 #elif REALTYPE == REAL_RATIONAL_ARBINT
67         #define ARBINT Gmpint // Set to Gmpint or Arbint here
68         
69         typedef Rational<ARBINT> Real;
70         inline float Float(const Real & r) {return (float)r.ToDouble();}
71         inline double Double(const Real & r) {return r.ToDouble();}
72         inline int64_t Int64(const Real & r) {return r.ToInt64();}
73         inline Rational<ARBINT> Sqrt(const Rational<ARBINT> & r) {return r.Sqrt();}
74 #elif REALTYPE == REAL_MPFRCPP
75         typedef mpfr::mpreal Real;
76         inline double Double(const Real & r) {return r.toDouble();}
77         inline float Float(const Real & r) {return r.toDouble();}
78         inline int64_t Int64(const Real & r) {return r.toLong();}
79         inline Real Sqrt(const Real & r) {return mpfr::sqrt(r, mpfr::mpreal::get_default_rnd());}
80         inline Real Abs(const Real & r) {return mpfr::abs(r, mpfr::mpreal::get_default_rnd());}
81         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
82 #elif REALTYPE == REAL_IRRAM
83         typedef iRRAM::REAL Real;
84         inline double Double(const Real & r) {return r.as_double(53);}
85         inline float Float(const Real & r) {return r.as_double(53);}
86         inline int64_t Int64(const Real & r) {return (int64_t)r.as_double(53);}
87         inline Real Sqrt(const Real & r) {return iRRAM::sqrt(r);}
88         inline Real RealFromStr(const char * str) {return Real(strtod(str, NULL));}
89 #else
90         #error "Type of Real unspecified."
91 #endif //REALTYPE
92
93         // Allow us to call Float on the primative types
94         // Useful so I can template some things that could be either (a more complicated) Real or a primitive type
95         // Mostly in the testers.
96         inline float Float(double f) {return (float)f;}
97         inline float Float(long double f) {return (float)(f);}
98         inline double Double(float f) {return (double)f;}
99         inline double Double(double f) {return (double)f;}
100         inline double Double(long double f) {return (double)(f);}
101         inline double Sqrt(double f) {return sqrt(f);}
102         inline double Abs(double a) {return fabs(a);}
103         inline int64_t Int64(double a){return (int64_t)a;}
104         
105         inline Real Power(const Real & a, int n)
106         {
107                 if (n < 0)
108                 {
109                         return Power(Real(1)/a, -n);
110                 }
111                 Real r(1);
112                 for (int i = 0; i < n; ++i)
113                         r *= a;
114                 return r;
115         }
116         struct Vec2
117         {
118                 Real x;
119                 Real y;
120                 Vec2() : x(0), y(0) {}
121                 Vec2(Real _x, Real _y) : x(_x), y(_y) {}
122                 Vec2(const std::pair<Real, Real> & p) : x(p.first), y(p.second) {}
123                 #if REALTYPE != REAL_IRRAM
124                 Vec2(const std::pair<int64_t, int64_t> & p) : x(p.first), y(p.second) {}
125                 #else
126                 Vec2(const std::pair<int64_t, int64_t> & p) : x((int)p.first), y((int)p.second) {}
127                 // Apparently iRRAM didn't implement -= for a constant argument
128                 #endif
129                 bool operator==(const Vec2& other) const { return (x == other.x) && (y == other.y); }
130                 bool operator!=(const Vec2& other) const { return !(*this == other); }
131                 
132                 Vec2& operator=(const Vec2& other) { x = other.x; y = other.y; return *this; }
133                 Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; return *this; }
134                 Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; return *this; }
135                 Vec2& operator*=(const Real& lambda) { x *= lambda; y *= lambda; return *this; }
136                 Vec2& operator/=(const Real& lambda) { x /= lambda; y /= lambda; return *this; }
137
138                 Vec2 operator+(const Vec2& other) const { return Vec2(x + other.x, y + other.y); }
139                 Vec2 operator-(const Vec2& other) const { return Vec2(x - other.x, y - other.y); }
140                 Vec2 operator*(const Real& lambda) const { return Vec2(x * lambda, y * lambda); }
141                 Vec2 operator/(const Real& lambda) const { return Vec2(x / lambda, y / lambda); }
142
143                 const Real SquareLength() const { return (x*x + y*y); }
144         
145         };
146
147         inline Real RealFromStr(const std::string & str) {return RealFromStr(str.c_str());}
148
149
150
151 }
152
153 #endif //_REAL_H

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