Render object range on the CPU
[ipdf/code.git] / src / real_fast2sum.h
1 #ifndef _REAL_FAST2SUM_H
2 #define _REAL_FAST2SUM_H
3
4 #include <cmath>
5 // otherwise the abs() function won't work
6
7 namespace IPDF
8 {
9
10         template <class T = float>
11         struct RealF2S
12         {
13                 RealF2S(const T & value = 0.0L) : m_value(value) {} //{Debug("Construct from value %f", m_value);}
14                 RealF2S(const RealF2S & cpy) : m_value(cpy.m_value) {} //{Debug("Copy construct from value %f", m_value);}
15
16                 RealF2S & operator+=(const RealF2S & a) {this->operator=(RealF2S(*this) + a); return *this;}
17                 RealF2S & operator-=(const RealF2S & a) {this->operator=(RealF2S(*this) - a); return *this;}
18                 RealF2S & operator*=(const RealF2S & a) {this->operator=(RealF2S(*this) * a); return *this;}
19                 RealF2S & operator/=(const RealF2S & a) {this->operator=(RealF2S(*this) / a); return *this;}
20
21                 RealF2S & operator=(const RealF2S & equ) {this->m_value = equ.m_value; return *this;}
22                 RealF2S & operator=(const T & equ) {this->m_value = equ.m_value; return *this;}
23
24                 T m_value;
25         };
26
27         
28
29         template <class T> RealF2S<T> operator+(const RealF2S<T> & a, const RealF2S<T> & b)
30         {
31                 // Use fast2sum
32                 if (abs(T(a.m_value)) < abs(T(b.m_value)))
33                         return b+a;
34                 T z = a.m_value + b.m_value;
35                 T w = z - a.m_value;
36                 T zz = b.m_value - w;
37                 return RealF2S<T>(z + zz);
38         }
39         template <class T> RealF2S<T> operator-(const RealF2S<T> & a, const RealF2S<T> & b)
40         {
41                 // Use fast2sum
42                 return a + RealF2S<T>(-b.m_value);
43         }
44
45         template <class T> RealF2S<T> operator*(const RealF2S<T> & a, const RealF2S<T> & b)
46         {
47                 return RealF2S<T>(a.m_value * b.m_value);
48         }
49         template <class T> RealF2S<T> operator/(const RealF2S<T> & a, const RealF2S<T> & b)
50         {
51                 return RealF2S<T>(a.m_value / b.m_value);
52         }
53
54
55 }
56
57 #endif //_REAL_FAST2SUM_H

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