-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfixedpoint.cpp
151 lines (124 loc) · 3.21 KB
/
fixedpoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2012 Vadym Kliuchnikov sqct(dot)software(at)gmail(dot)com
//
// This file is part of SQCT.
//
// SQCT is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SQCT is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with SQCT. If not, see <http://www.gnu.org/licenses/>.
//
#include "fixedpoint.h"
#include <sstream>
using namespace std;
using namespace boost::multiprecision;
fixedpoint::fixedpoint(const hprr &val)
{
stringstream ss;
ss << to_mpz(pow2(frac_bits) * val);
m_value = bigint_t(ss.str());
}
fixedpoint::fixedpoint(const fixedpoint &val) : m_value(val.m_value)
{
}
fixedpoint fixedpoint::operator+(const fixedpoint &rhs) const
{
fixedpoint r(*this);
r.m_value+= rhs.m_value;
return r;
}
fixedpoint fixedpoint::operator-(const fixedpoint &rhs) const
{
fixedpoint r(*this);
r.m_value-= rhs.m_value;
return r;
}
fixedpoint fixedpoint::operator-(long rhs) const
{
fixedpoint r(*this);
bigint_t rh(rhs);
rh <<= frac_bits;
r.m_value-= rhs;
return r;
}
fixedpoint &fixedpoint::operator+=(const fixedpoint &rhs)
{
m_value+= rhs.m_value;
return *this;
}
double frac_to_double( const fixedpoint::bigint_t& frac )
{
fixedpoint::bigint_t fr(frac);
fr >>= 64;
long high = fr.convert_to<long>();
fr <<= 64;
long low = (frac-fr).convert_to<long>();
long double r = ldexp((long double)(low),-128);
if( high > 0 )
{
r += ldexp((long double)(high),-64);
}
return r;
}
std::pair<long, double> fixedpoint::round_ex() const
{
std::pair<long, double> res;
bigint_t int_part(abs(m_value));
bigint_t frac_part(int_part);
int_part >>= frac_bits;
res.first = int_part.convert_to<long>();
int_part <<= frac_bits;
frac_part -= int_part;
bigint_t t(frac_part);
t >>= (frac_bits - 1);
bool neg = false;
if( t > 0 )
{
res.first+=1;
frac_part-=one;
neg = true;
}
res.second = frac_to_double(abs(frac_part));
if( neg ) res.second = -res.second;
if( m_value < 0 )
{
res.first = -res.first;
res.second = -res.second;
}
return res;
}
double fixedpoint::to_double() const
{
auto res = round_ex();
return double(res.first) + res.second;
}
long ceil(const fixedpoint &val)
{
auto res = val.round_ex();
if( res.second <= 0. )
return res.first;
else
return res.first + 1;
}
long floor(const fixedpoint &val)
{
auto res = val.round_ex();
if( res.second < 0. )
return res.first - 1;
else
return res.first;
}
double to_double(const fixedpoint &val)
{
return val.to_double();
}
//fixedpoint::bigint_t fixedpoint::mask("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
//fixedpoint::bigint_t fixedpoint::half("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
fixedpoint::bigint_t fixedpoint::one("0x100000000000000000000000000000000");