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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/* Definitions for simple data type for real numbers.
Copyright (C) 2002-2014 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC 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 General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef GCC_SREAL_H
#define GCC_SREAL_H
/* SREAL_PART_BITS has to be an even number. */
#define SREAL_PART_BITS 32
#define UINT64_BITS 64
#define SREAL_MIN_SIG ((uint64_t) 1 << (SREAL_PART_BITS - 1))
#define SREAL_MAX_SIG (((uint64_t) 1 << SREAL_PART_BITS) - 1)
#define SREAL_MAX_EXP (INT_MAX / 4)
#define SREAL_BITS SREAL_PART_BITS
/* Structure for holding a simple real number. */
class sreal
{
public:
/* Construct an uninitialized sreal. */
sreal () : m_sig (-1), m_exp (-1), m_negative (0) {}
/* Construct a sreal. */
sreal (int64_t sig, int exp = 0) : m_exp (exp)
{
m_negative = sig < 0;
if (sig < 0)
sig = -sig;
m_sig = (uint64_t) sig;
normalize ();
}
void dump (FILE *) const;
int64_t to_int () const;
sreal operator+ (const sreal &other) const;
sreal operator- (const sreal &other) const;
sreal operator* (const sreal &other) const;
sreal operator/ (const sreal &other) const;
bool operator< (const sreal &other) const
{
if (m_negative != other.m_negative)
return m_negative > other.m_negative;
bool r = m_exp < other.m_exp
|| (m_exp == other.m_exp && m_sig < other.m_sig);
return m_negative ? !r : r;
}
bool operator== (const sreal &other) const
{
return m_exp == other.m_exp && m_sig == other.m_sig
&& m_negative == other.m_negative;
}
sreal operator- () const
{
if (m_sig == 0)
return *this;
sreal tmp = *this;
tmp.m_negative = !tmp.m_negative;
return tmp;
}
sreal shift (int sig) const
{
sreal tmp = *this;
tmp.m_sig += sig;
return tmp;
}
/* Global minimum sreal can hold. */
inline static sreal min ()
{
static sreal min = sreal (-SREAL_MAX_SIG, SREAL_MAX_EXP);
return min;
}
/* Global minimum sreal can hold. */
inline static sreal max ()
{
static sreal max = sreal (SREAL_MAX_SIG, SREAL_MAX_EXP);
return max;
}
private:
void normalize ();
void shift_right (int amount);
static sreal signedless_plus (const sreal &a, const sreal &b, bool negative);
static sreal signedless_minus (const sreal &a, const sreal &b, bool negative);
uint64_t m_sig; /* Significant. */
signed int m_exp; /* Exponent. */
bool m_negative; /* Negative sign. */
};
extern void debug (sreal &ref);
extern void debug (sreal *ptr);
inline sreal &operator+= (sreal &a, const sreal &b)
{
return a = a + b;
}
inline sreal &operator-= (sreal &a, const sreal &b)
{
return a = a - b;
}
inline sreal &operator/= (sreal &a, const sreal &b)
{
return a = a / b;
}
inline sreal &operator*= (sreal &a, const sreal &b)
{
return a = a * b;
}
inline bool operator!= (const sreal &a, const sreal &b)
{
return !(a == b);
}
inline bool operator> (const sreal &a, const sreal &b)
{
return !(a == b || a < b);
}
inline bool operator<= (const sreal &a, const sreal &b)
{
return a < b || a == b;
}
inline bool operator>= (const sreal &a, const sreal &b)
{
return a == b || a > b;
}
inline sreal operator<< (const sreal &a, int exp)
{
return a.shift (exp);
}
inline sreal operator>> (const sreal &a, int exp)
{
return a.shift (-exp);
}
#endif
|