-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathadjust_float_expressions.cpp
233 lines (199 loc) · 6.77 KB
/
adjust_float_expressions.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************\
Module: Symbolic Execution
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Symbolic Execution
#include "adjust_float_expressions.h"
#include <util/arith_tools.h>
#include <util/cprover_prefix.h>
#include <util/expr_util.h>
#include <util/floatbv_expr.h>
#include <util/ieee_float.h>
#include <util/std_expr.h>
#include <util/symbol.h>
#include "goto_model.h"
irep_idt rounding_mode_identifier()
{
return CPROVER_PREFIX "rounding_mode";
}
/// Iterate over an expression and check it or any of its subexpressions are
/// floating point operations that haven't been adjusted with a rounding mode
/// yet.
static bool have_to_adjust_float_expressions(const exprt &expr)
{
if(expr.id()==ID_floatbv_plus ||
expr.id()==ID_floatbv_minus ||
expr.id()==ID_floatbv_mult ||
expr.id()==ID_floatbv_div ||
expr.id()==ID_floatbv_div ||
expr.id()==ID_floatbv_rem ||
expr.id()==ID_floatbv_typecast)
return false;
const typet &type = expr.type();
if(
type.id() == ID_floatbv ||
(type.id() == ID_complex &&
to_complex_type(type).subtype().id() == ID_floatbv))
{
if(
expr.id() == ID_plus || expr.id() == ID_minus || expr.id() == ID_mult ||
expr.id() == ID_div)
return true;
}
if(expr.id()==ID_typecast)
{
const typecast_exprt &typecast_expr=to_typecast_expr(expr);
const typet &src_type=typecast_expr.op().type();
const typet &dest_type=typecast_expr.type();
if(dest_type.id()==ID_floatbv &&
src_type.id()==ID_floatbv)
return true;
else if(
dest_type.id() == ID_floatbv &&
(src_type.id() == ID_c_bit_field || src_type.id() == ID_signedbv ||
src_type.id() == ID_unsignedbv || src_type.id() == ID_c_enum_tag))
return true;
else if(
(dest_type.id() == ID_signedbv || dest_type.id() == ID_unsignedbv ||
dest_type.id() == ID_c_enum_tag || dest_type.id() == ID_c_bit_field) &&
src_type.id() == ID_floatbv)
return true;
}
for(const auto &op : expr.operands())
{
if(have_to_adjust_float_expressions(op))
return true;
}
return false;
}
void adjust_float_expressions(exprt &expr, const exprt &rounding_mode)
{
if(!have_to_adjust_float_expressions(expr))
return;
// recursive call
// Note that we do the recursion twice here; once in
// `have_to_adjust_float_expressions` and once here. Presumably this is to
// avoid breaking sharing (calling the non-const operands() function breaks
// sharing)
for(auto &op : expr.operands())
adjust_float_expressions(op, rounding_mode);
const typet &type = expr.type();
if(
type.id() == ID_floatbv ||
(type.id() == ID_complex &&
to_complex_type(type).subtype().id() == ID_floatbv))
{
if(
expr.id() == ID_plus || expr.id() == ID_minus || expr.id() == ID_mult ||
expr.id() == ID_div)
{
DATA_INVARIANT(
expr.operands().size() >= 2,
"arithmetic operations must have two or more operands");
// make sure we have binary expressions
if(expr.operands().size()>2)
{
expr=make_binary(expr);
CHECK_RETURN(expr.operands().size() == 2);
}
// now add rounding mode
expr.id(expr.id()==ID_plus?ID_floatbv_plus:
expr.id()==ID_minus?ID_floatbv_minus:
expr.id()==ID_mult?ID_floatbv_mult:
expr.id()==ID_div?ID_floatbv_div:
irep_idt());
expr.operands().resize(3);
to_ieee_float_op_expr(expr).rounding_mode() = rounding_mode;
}
}
if(expr.id()==ID_typecast)
{
const typecast_exprt &typecast_expr=to_typecast_expr(expr);
const typet &src_type=typecast_expr.op().type();
const typet &dest_type=typecast_expr.type();
if(dest_type.id()==ID_floatbv &&
src_type.id()==ID_floatbv)
{
// Casts from bigger to smaller float-type might round.
// For smaller to bigger it is strictly redundant but
// we leave this optimisation until later to simplify
// the representation.
expr.id(ID_floatbv_typecast);
expr.operands().resize(2);
to_floatbv_typecast_expr(expr).rounding_mode() = rounding_mode;
}
else if(
dest_type.id() == ID_floatbv &&
(src_type.id() == ID_signedbv || src_type.id() == ID_unsignedbv ||
src_type.id() == ID_c_enum_tag || src_type.id() == ID_c_bit_field))
{
// casts from integer to float-type might round
expr.id(ID_floatbv_typecast);
expr.operands().resize(2);
to_floatbv_typecast_expr(expr).rounding_mode() = rounding_mode;
}
else if(
dest_type.id() == ID_floatbv &&
(src_type.id() == ID_c_bool || src_type.id() == ID_bool))
{
// casts from bool or c_bool to float-type do not need rounding
}
else if(
(dest_type.id() == ID_signedbv || dest_type.id() == ID_unsignedbv ||
dest_type.id() == ID_c_enum_tag || dest_type.id() == ID_c_bit_field) &&
src_type.id() == ID_floatbv)
{
// In C, casts from float to integer always round to zero,
// irrespectively of the rounding mode that is currently set.
// We will have to consider other programming languages
// eventually.
/* ISO 9899:1999
* 6.3.1.4 Real floating and integer
* 1 When a finite value of real floating type is converted
* to an integer type other than _Bool, the fractional part
* is discarded (i.e., the value is truncated toward zero).
*/
expr.id(ID_floatbv_typecast);
expr.operands().resize(2);
to_floatbv_typecast_expr(expr).rounding_mode() =
from_integer(ieee_floatt::ROUND_TO_ZERO, rounding_mode.type());
}
}
}
void adjust_float_expressions(exprt &expr, const namespacet &ns)
{
if(!have_to_adjust_float_expressions(expr))
return;
symbol_exprt rounding_mode =
ns.lookup(rounding_mode_identifier()).symbol_expr();
rounding_mode.add_source_location() = expr.source_location();
adjust_float_expressions(expr, rounding_mode);
}
void adjust_float_expressions(
goto_functionst::goto_functiont &goto_function,
const namespacet &ns)
{
for(auto &i : goto_function.body.instructions)
i.transform([&ns](exprt expr) -> std::optional<exprt> {
if(have_to_adjust_float_expressions(expr))
{
adjust_float_expressions(expr, ns);
return expr;
}
else
return {};
});
}
void adjust_float_expressions(
goto_functionst &goto_functions,
const namespacet &ns)
{
for(auto &gf_entry : goto_functions.function_map)
adjust_float_expressions(gf_entry.second, ns);
}
void adjust_float_expressions(goto_modelt &goto_model)
{
namespacet ns(goto_model.symbol_table);
adjust_float_expressions(goto_model.goto_functions, ns);
}