Skip to content

Commit 6dc6dd5

Browse files
authored
Merge pull request #8551 from diffblue/ieee_floatt-protect-rounding_mode
protect `ieee_floatt::rounding_mode`
2 parents f9a7807 + 1a1b6ac commit 6dc6dd5

File tree

3 files changed

+60
-40
lines changed

3 files changed

+60
-40
lines changed

src/solvers/refinement/refine_arithmetic.cpp

+7-10
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,6 @@ void bv_refinementt::check_SAT(approximationt &a)
171171
if(a.over_state==MAX_STATE)
172172
return;
173173

174-
ieee_float_spect spec(to_floatbv_type(type));
175-
ieee_floatt o0(spec), o1(spec);
176-
177-
o0.unpack(a.op0_value);
178-
o1.unpack(a.op1_value);
179-
180174
// get actual rounding mode
181175
constant_exprt rounding_mode_expr =
182176
to_constant_expr(get(float_op.rounding_mode()));
@@ -185,10 +179,13 @@ void bv_refinementt::check_SAT(approximationt &a)
185179
ieee_floatt::rounding_modet rounding_mode =
186180
(ieee_floatt::rounding_modet)rounding_mode_int;
187181

188-
ieee_floatt result=o0;
189-
o0.rounding_mode=rounding_mode;
190-
o1.rounding_mode=rounding_mode;
191-
result.rounding_mode=rounding_mode;
182+
ieee_float_spect spec(to_floatbv_type(type));
183+
ieee_floatt o0(spec, rounding_mode), o1(spec, rounding_mode);
184+
185+
o0.unpack(a.op0_value);
186+
o1.unpack(a.op1_value);
187+
188+
ieee_floatt result = o0;
192189

193190
if(a.expr.id()==ID_floatbv_plus)
194191
result+=o1;

src/util/ieee_float.h

+40-17
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,22 @@ class ieee_floatt
129129
// A helper to turn a rounding mode into a constant bitvector expression
130130
static constant_exprt rounding_mode_expr(rounding_modet);
131131

132-
rounding_modet rounding_mode;
133-
134132
ieee_float_spect spec;
135133

136-
explicit ieee_floatt(const ieee_float_spect &_spec):
137-
rounding_mode(ROUND_TO_EVEN),
138-
spec(_spec), sign_flag(false), exponent(0), fraction(0),
139-
NaN_flag(false), infinity_flag(false)
134+
explicit ieee_floatt(const ieee_float_spect &_spec)
135+
: spec(_spec),
136+
rounding_mode(ROUND_TO_EVEN),
137+
sign_flag(false),
138+
exponent(0),
139+
fraction(0),
140+
NaN_flag(false),
141+
infinity_flag(false)
140142
{
141143
}
142144

143-
explicit ieee_floatt(ieee_float_spect __spec, rounding_modet __rounding_mode)
144-
: rounding_mode(__rounding_mode),
145-
spec(std::move(__spec)),
145+
ieee_floatt(ieee_float_spect __spec, rounding_modet __rounding_mode)
146+
: spec(std::move(__spec)),
147+
rounding_mode(__rounding_mode),
146148
sign_flag(false),
147149
exponent(0),
148150
fraction(0),
@@ -151,14 +153,27 @@ class ieee_floatt
151153
{
152154
}
153155

154-
explicit ieee_floatt(const floatbv_typet &type):
155-
rounding_mode(ROUND_TO_EVEN),
156-
spec(ieee_float_spect(type)),
157-
sign_flag(false),
158-
exponent(0),
159-
fraction(0),
160-
NaN_flag(false),
161-
infinity_flag(false)
156+
explicit ieee_floatt(const floatbv_typet &type)
157+
: spec(ieee_float_spect(type)),
158+
rounding_mode(ROUND_TO_EVEN),
159+
sign_flag(false),
160+
exponent(0),
161+
fraction(0),
162+
NaN_flag(false),
163+
infinity_flag(false)
164+
{
165+
}
166+
167+
explicit ieee_floatt(
168+
const floatbv_typet &type,
169+
rounding_modet __rounding_mode)
170+
: spec(ieee_float_spect(type)),
171+
rounding_mode(__rounding_mode),
172+
sign_flag(false),
173+
exponent(0),
174+
fraction(0),
175+
NaN_flag(false),
176+
infinity_flag(false)
162177
{
163178
}
164179

@@ -175,6 +190,12 @@ class ieee_floatt
175190
from_expr(expr);
176191
}
177192

193+
ieee_floatt(const constant_exprt &expr, rounding_modet __rounding_mode)
194+
: rounding_mode(__rounding_mode)
195+
{
196+
from_expr(expr);
197+
}
198+
178199
void negate()
179200
{
180201
sign_flag=!sign_flag;
@@ -316,6 +337,8 @@ class ieee_floatt
316337
void align();
317338
void next_representable(bool greater);
318339

340+
rounding_modet rounding_mode;
341+
319342
// we store the number unpacked
320343
bool sign_flag;
321344
mp_integer exponent; // this is unbiased

src/util/simplify_expr_floatbv.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ simplify_exprt::simplify_floatbv_typecast(const floatbv_typecast_exprt &expr)
190190
{
191191
if(dest_type.id()==ID_floatbv) // float to float
192192
{
193-
ieee_floatt result(to_constant_expr(casted_expr));
194-
result.rounding_mode =
193+
auto rounding_mode =
195194
(ieee_floatt::rounding_modet)numeric_cast_v<std::size_t>(
196195
*rounding_mode_index);
196+
ieee_floatt result(to_constant_expr(casted_expr), rounding_mode);
197197
result.change_spec(
198198
ieee_float_spect(to_floatbv_type(dest_type)));
199199
return result.to_expr();
@@ -203,10 +203,10 @@ simplify_exprt::simplify_floatbv_typecast(const floatbv_typecast_exprt &expr)
203203
{
204204
if(*rounding_mode_index == ieee_floatt::ROUND_TO_ZERO)
205205
{
206-
ieee_floatt result(to_constant_expr(casted_expr));
207-
result.rounding_mode =
206+
auto rounding_mode =
208207
(ieee_floatt::rounding_modet)numeric_cast_v<std::size_t>(
209208
*rounding_mode_index);
209+
ieee_floatt result(to_constant_expr(casted_expr), rounding_mode);
210210
mp_integer value=result.to_integer();
211211
return from_integer(value, dest_type);
212212
}
@@ -220,10 +220,10 @@ simplify_exprt::simplify_floatbv_typecast(const floatbv_typecast_exprt &expr)
220220
{
221221
if(dest_type.id()==ID_floatbv) // int to float
222222
{
223-
ieee_floatt result(to_floatbv_type(dest_type));
224-
result.rounding_mode =
223+
auto rounding_mode =
225224
(ieee_floatt::rounding_modet)numeric_cast_v<std::size_t>(
226225
*rounding_mode_index);
226+
ieee_floatt result(to_floatbv_type(dest_type), rounding_mode);
227227
result.from_integer(*value);
228228
return result.to_expr();
229229
}
@@ -296,16 +296,16 @@ simplify_exprt::simplify_floatbv_op(const ieee_float_op_exprt &expr)
296296

297297
if(op0.is_constant() && op1.is_constant() && op2.is_constant())
298298
{
299-
ieee_floatt v0(to_constant_expr(op0));
300-
ieee_floatt v1(to_constant_expr(op1));
299+
const auto rounding_mode_opt = numeric_cast<mp_integer>(op2);
301300

302-
const auto rounding_mode = numeric_cast<mp_integer>(op2);
303-
if(rounding_mode.has_value())
301+
if(rounding_mode_opt.has_value())
304302
{
305-
v0.rounding_mode =
303+
const auto rounding_mode =
306304
(ieee_floatt::rounding_modet)numeric_cast_v<std::size_t>(
307-
*rounding_mode);
308-
v1.rounding_mode=v0.rounding_mode;
305+
*rounding_mode_opt);
306+
307+
ieee_floatt v0(to_constant_expr(op0), rounding_mode);
308+
ieee_floatt v1(to_constant_expr(op1), rounding_mode);
309309

310310
ieee_floatt result=v0;
311311

0 commit comments

Comments
 (0)