ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
ieee_float.h
Go to the documentation of this file.
1#ifndef CPROVER_IEEE_FLOAT_H
2#define CPROVER_IEEE_FLOAT_H
3
5#include <irep2/irep2_type.h>
9
11{
12public:
13 unsigned f, e;
14
15 BigInt bias() const;
16
19
20 ieee_float_spect() : f(0), e(0)
21 {
22 }
23
24 ieee_float_spect(unsigned _f, unsigned _e) : f(_f), e(_e)
25 {
26 }
27
28 inline unsigned width() const
29 {
30 return f + e + 1;
31 }
32
33 /* that's a floatbv_type2t */
34 type2tc get_type() const;
35
36 BigInt max_exponent() const;
37 BigInt max_fraction() const;
38
39 floatbv_typet to_type() const;
41 {
42 // 32 bits in total
43 return ieee_float_spect(23, 8);
44 }
45
47 {
48 // 64 bits in total
49 return ieee_float_spect(52, 11);
50 }
51
53 {
54 // IEEE 754 binary128
55 return ieee_float_spect(112, 15);
56 }
57
58 inline friend bool
60 {
61 return a.f == b.f && a.e == b.e;
62 }
63
64 inline friend bool
66 {
67 return !(a == b);
68 }
69};
70
71bool operator==(const ieee_float_spect &a, const ieee_float_spect &b);
72bool operator!=(const ieee_float_spect &a, const ieee_float_spect &b);
73
75{
76public:
87
90
91 explicit ieee_floatt();
92 explicit ieee_floatt(const ieee_float_spect &s);
93 explicit ieee_floatt(const constant_exprt &expr);
94
95 void negate()
96 {
98 }
99
100 void set_sign(bool _sign)
101 {
102 sign_flag = _sign;
103 }
104
106 {
107 sign_flag = false;
108 exponent = 0;
109 fraction = 0;
110 NaN_flag = false;
111 infinity_flag = false;
112 }
113
114 void make_NaN();
115 void make_plus_infinity();
116 void make_minus_infinity();
117 void make_fltmax(); // maximum representable finite floating-point number
118 void make_fltmin(); // minimum normalized positive floating-point number
119
120 static ieee_floatt NaN(const ieee_float_spect &_spec)
121 {
122 ieee_floatt c(_spec);
123 c.make_NaN();
124 return c;
125 }
126
128 {
129 ieee_floatt c(_spec);
131 return c;
132 }
133
135 {
136 ieee_floatt c(_spec);
138 return c;
139 }
140
141 // maximum representable finite floating-point number
143 {
144 ieee_floatt c(_spec);
145 c.make_fltmax();
146 return c;
147 }
148
149 // minimum normalized positive floating-point number
151 {
152 ieee_floatt c(_spec);
153 c.make_fltmin();
154 return c;
155 }
156
157 // set to next representable number towards plus infinity
158 void increment(bool distinguish_zero = false)
159 {
160 if (is_zero() && get_sign() && distinguish_zero)
161 negate();
162 else
163 next_representable(true);
164 }
165
166 // set to previous representable number towards minus infinity
167 void decrement(bool distinguish_zero = false)
168 {
169 if (is_zero() && !get_sign() && distinguish_zero)
170 negate();
171 else
172 next_representable(false);
173 }
174
175 bool is_zero() const
176 {
177 return !NaN_flag && !infinity_flag && fraction == 0 && exponent == 0;
178 }
179
180 bool is_negative() const
181 {
182 return sign_flag;
183 }
184
185 bool get_sign() const
186 {
187 return sign_flag;
188 }
189
190 bool is_NaN() const
191 {
192 return NaN_flag;
193 }
194
195 bool is_infinity() const
196 {
197 return !NaN_flag && infinity_flag;
198 }
199
200 bool is_finite() const
201 {
202 return !infinity_flag && !NaN_flag;
203 }
204
205 bool is_normal() const;
206
207 const BigInt &get_exponent() const
208 {
209 return exponent;
210 }
211
212 const BigInt &get_fraction() const
213 {
214 return fraction;
215 }
216
217 // performs conversion to ieee floating point format
218 void from_integer(const BigInt &i);
219 void from_base10(const BigInt &exp, const BigInt &frac);
220 void build(const BigInt &exp, const BigInt &frac);
221
222 /* Given i non-negative, this method replaces the contents of this instance
223 * by interpreting it according to the IEEE-754 in-memory representation.
224 * In particular, the low-order spec.f bits are taken as the fraction, the
225 * following spec.e bits as the exponent and the sign_flag is determined
226 * based on the remaining bits being zero or not. Afterwards adjustments for
227 * bias, denormalized numbers and NaNs are performed. */
228 void unpack(const BigInt &i);
229
230 void from_double(const double d);
231 void from_float(const float f);
232 double to_double() const;
233 float to_float() const;
234 bool is_double() const;
235 bool is_float() const;
236 BigInt pack() const;
237 void extract_base2(BigInt &_fraction, BigInt &_exponent) const;
238 void extract_base10(BigInt &_fraction, BigInt &_exponent) const;
239 BigInt to_integer() const; // this always rounds to zero
240
241 // performs conversion from ieee floating point format
242 void change_spec(const ieee_float_spect &dest_spec);
243
244 void print(std::ostream &out) const;
245
246 std::string to_ansi_c_string() const
247 {
248 return format(format_spect());
249 }
250
251 std::string to_string_decimal(unsigned precision) const;
252 std::string to_string_scientific(unsigned precision) const;
253 std::string format(const format_spect &format_spec) const;
254
255 friend inline std::ostream &
256 operator<<(std::ostream &out, const ieee_floatt &f)
257 {
258 return out << f.to_ansi_c_string();
259 }
260 // expressions
261 constant_exprt to_expr() const;
262 void from_expr(const constant_exprt &expr);
263
268
269 friend ieee_floatt operator+(const ieee_floatt &lhs, const ieee_floatt &rhs);
270 friend ieee_floatt operator-(const ieee_floatt &lhs, const ieee_floatt &rhs);
271
273
274 friend bool operator<(const ieee_floatt &a, const ieee_floatt &b);
275 friend bool operator<=(const ieee_floatt &a, const ieee_floatt &b);
276 friend bool operator>(const ieee_floatt &a, const ieee_floatt &b);
277 friend bool operator>=(const ieee_floatt &a, const ieee_floatt &b);
278
279 // IEEE-value equality: NaN compares unequal to everything (including
280 // itself), and +0 == -0 despite differing bit patterns.
281 friend bool operator==(const ieee_floatt &a, const ieee_floatt &b);
282 friend bool operator!=(const ieee_floatt &a, const ieee_floatt &b);
283
284 friend bool operator==(const ieee_floatt &a, int i);
285 friend bool operator>(const ieee_floatt &a, int i);
286 friend bool operator<(const ieee_floatt &a, int i);
287 friend bool operator>=(const ieee_floatt &a, int i);
288 friend bool operator<=(const ieee_floatt &a, int i);
289
290protected:
291 void divide_and_round(BigInt &fraction, const BigInt &factor);
292 void align();
293 void next_representable(bool greater);
294
295 // we store the number unpacked
297 BigInt exponent; // this is unbiased
298 BigInt fraction; // this _does_ include the hidden bit
300
301 // number of digits of an integer >=1 in base 10
302 static BigInt base10_digits(const BigInt &src);
303};
304
305bool operator<(const ieee_floatt &a, const ieee_floatt &b);
306bool operator<=(const ieee_floatt &a, const ieee_floatt &b);
307bool operator>(const ieee_floatt &a, const ieee_floatt &b);
308bool operator>=(const ieee_floatt &a, const ieee_floatt &b);
309bool operator==(const ieee_floatt &a, const ieee_floatt &b);
310bool operator!=(const ieee_floatt &a, const ieee_floatt &b);
311std::ostream &operator<<(std::ostream &, const ieee_floatt &);
312
313#endif
Definition bigint.hh:58
Definition std_expr.h:1082
Floating-point bitvector type.
Definition irep2_type.h:621
Definition std_types.h:629
Definition format_spec.h:5
Definition ieee_float.h:11
BigInt max_fraction() const
Definition ieee_float.cpp:26
friend bool operator!=(const ieee_float_spect &a, const ieee_float_spect &b)
Definition ieee_float.h:65
unsigned width() const
Definition ieee_float.h:28
ieee_float_spect(const floatbv_typet &type)
Definition ieee_float.cpp:31
ieee_float_spect()
Definition ieee_float.h:20
BigInt bias() const
Definition ieee_float.cpp:8
unsigned f
Definition ieee_float.h:13
unsigned e
Definition ieee_float.h:13
type2tc get_type() const
Definition ieee_float.cpp:49
static ieee_float_spect single_precision()
Definition ieee_float.h:40
static ieee_float_spect quadruple_precision()
Definition ieee_float.h:52
BigInt max_exponent() const
Definition ieee_float.cpp:21
floatbv_typet to_type() const
Definition ieee_float.cpp:13
ieee_float_spect(unsigned _f, unsigned _e)
Definition ieee_float.h:24
friend bool operator==(const ieee_float_spect &a, const ieee_float_spect &b)
Definition ieee_float.h:59
static ieee_float_spect double_precision()
Definition ieee_float.h:46
Definition ieee_float.h:75
friend bool operator>=(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:1002
bool is_double() const
Definition ieee_float.cpp:1189
friend bool operator==(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:1007
ieee_floatt & operator*=(const ieee_floatt &other)
Definition ieee_float.cpp:784
friend bool operator!=(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:1071
void make_minus_infinity()
Definition ieee_float.cpp:1183
void make_fltmax()
Definition ieee_float.cpp:1163
bool is_negative() const
Definition ieee_float.h:180
float to_float() const
Definition ieee_float.cpp:1230
ieee_float_spect spec
Definition ieee_float.h:89
std::string to_ansi_c_string() const
Definition ieee_float.h:246
bool is_NaN() const
Definition ieee_float.h:190
void make_plus_infinity()
Definition ieee_float.cpp:1174
bool is_finite() const
Definition ieee_float.h:200
ieee_floatt & operator+=(const ieee_floatt &other)
Definition ieee_float.cpp:820
void extract_base10(BigInt &_fraction, BigInt &_exponent) const
Definition ieee_float.cpp:425
constant_exprt to_expr() const
Definition ieee_float.cpp:704
bool is_float() const
Definition ieee_float.cpp:1194
bool get_sign() const
Definition ieee_float.h:185
void make_zero()
Definition ieee_float.h:105
void set_sign(bool _sign)
Definition ieee_float.h:100
void from_float(const float f)
Definition ieee_float.cpp:1138
BigInt to_integer() const
Definition ieee_float.cpp:1100
bool NaN_flag
Definition ieee_float.h:299
static ieee_floatt plus_infinity(const ieee_float_spect &_spec)
Definition ieee_float.h:127
void from_expr(const constant_exprt &expr)
Definition ieee_float.cpp:1094
BigInt pack() const
Definition ieee_float.cpp:367
friend bool operator<(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:931
static ieee_floatt NaN(const ieee_float_spect &_spec)
Definition ieee_float.h:120
std::string to_string_decimal(unsigned precision) const
Definition ieee_float.cpp:143
void unpack(const BigInt &i)
Definition ieee_float.cpp:310
void negate()
Definition ieee_float.h:95
void make_NaN()
Definition ieee_float.cpp:1155
bool sign_flag
Definition ieee_float.h:296
const BigInt & get_exponent() const
Definition ieee_float.h:207
ieee_floatt & operator-=(const ieee_floatt &other)
Definition ieee_float.cpp:904
static ieee_floatt minus_infinity(const ieee_float_spect &_spec)
Definition ieee_float.h:134
void next_representable(bool greater)
Definition ieee_float.cpp:1266
const BigInt & get_fraction() const
Definition ieee_float.h:212
void increment(bool distinguish_zero=false)
Definition ieee_float.h:158
bool infinity_flag
Definition ieee_float.h:299
void from_integer(const BigInt &i)
Definition ieee_float.cpp:497
bool is_zero() const
Definition ieee_float.h:175
void build(const BigInt &exp, const BigInt &frac)
Definition ieee_float.cpp:459
BigInt fraction
Definition ieee_float.h:298
static BigInt base10_digits(const BigInt &src)
Definition ieee_float.cpp:130
double to_double() const
Definition ieee_float.cpp:1199
ieee_floatt & operator/=(const ieee_floatt &other)
Definition ieee_float.cpp:711
ieee_floatt()
Definition ieee_float.cpp:54
std::string format(const format_spect &format_spec) const
Definition ieee_float.cpp:86
friend ieee_floatt operator+(const ieee_floatt &lhs, const ieee_floatt &rhs)
Definition ieee_float.cpp:911
bool is_infinity() const
Definition ieee_float.h:195
friend std::ostream & operator<<(std::ostream &out, const ieee_floatt &f)
Definition ieee_float.h:256
void make_fltmin()
Definition ieee_float.cpp:1169
void from_base10(const BigInt &exp, const BigInt &frac)
Definition ieee_float.cpp:470
rounding_modet rounding_mode
Definition ieee_float.h:88
bool is_normal() const
Definition ieee_float.cpp:360
void extract_base2(BigInt &_fraction, BigInt &_exponent) const
Definition ieee_float.cpp:404
static ieee_floatt fltmin(const ieee_float_spect &_spec)
Definition ieee_float.h:150
ieee_floatt & operator-()
Definition ieee_float.cpp:925
std::string to_string_scientific(unsigned precision) const
Definition ieee_float.cpp:223
void decrement(bool distinguish_zero=false)
Definition ieee_float.h:167
void divide_and_round(BigInt &fraction, const BigInt &factor)
Definition ieee_float.cpp:641
rounding_modet
Definition ieee_float.h:78
@ NONDETERMINISTIC
Definition ieee_float.h:85
@ ROUND_TO_ZERO
Definition ieee_float.h:83
@ ROUND_TO_PLUS_INF
Definition ieee_float.h:81
@ UNKNOWN
Definition ieee_float.h:84
@ ROUND_TO_AWAY
Definition ieee_float.h:80
@ ROUND_TO_EVEN
Definition ieee_float.h:79
@ ROUND_TO_MINUS_INF
Definition ieee_float.h:82
void from_double(const double d)
Definition ieee_float.cpp:1121
void change_spec(const ieee_float_spect &dest_spec)
Definition ieee_float.cpp:1076
void align()
Definition ieee_float.cpp:505
friend bool operator<=(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:977
friend bool operator>(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:997
void print(std::ostream &out) const
Definition ieee_float.cpp:81
BigInt exponent
Definition ieee_float.h:297
static ieee_floatt fltmax(const ieee_float_spect &_spec)
Definition ieee_float.h:142
@ other
Definition converter_compare.cpp:887
double exp(double x)
Definition exp.c:84
bool operator>=(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:1002
bool operator!=(const ieee_float_spect &a, const ieee_float_spect &b)
Definition ieee_float.h:65
bool operator<(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:931
std::ostream & operator<<(std::ostream &, const ieee_floatt &)
Definition ieee_float.h:256
bool operator==(const ieee_float_spect &a, const ieee_float_spect &b)
Definition ieee_float.h:59
bool operator<=(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:977
bool operator>(const ieee_floatt &a, const ieee_floatt &b)
Definition ieee_float.cpp:997
irep_container< type2t > type2tc
Definition irep2.h:430