Skip to content

Commit af4e2e4

Browse files
authored
Merge pull request #8550 from diffblue/ieee_float_valuet
Split `ieee_floatt` into `ieee_float_valuet` and `ieee_floatt`
2 parents f2489e3 + d73f9ab commit af4e2e4

33 files changed

+536
-481
lines changed

jbmc/src/java_bytecode/assignments_from_json.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,13 @@ assign_primitive_from_json(const exprt &expr, const jsont &json)
358358
}
359359
else if(expr.type() == java_double_type())
360360
{
361-
ieee_floatt ieee_float(to_floatbv_type(expr.type()));
361+
ieee_float_valuet ieee_float(to_floatbv_type(expr.type()));
362362
ieee_float.from_double(std::stod(json.value));
363363
result.add(code_frontend_assignt{expr, ieee_float.to_expr()});
364364
}
365365
else if(expr.type() == java_float_type())
366366
{
367-
ieee_floatt ieee_float(to_floatbv_type(expr.type()));
367+
ieee_float_valuet ieee_float(to_floatbv_type(expr.type()));
368368
ieee_float.from_float(std::stof(json.value));
369369
result.add(code_frontend_assignt{expr, ieee_float.to_expr()});
370370
}

jbmc/src/java_bytecode/expr2java.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ std::string expr2javat::convert_constant(
230230
(src.type()==java_double_type()))
231231
{
232232
// This converts NaNs to the canonical NaN
233-
const ieee_floatt ieee_repr(to_constant_expr(src));
233+
const ieee_float_valuet ieee_repr(to_constant_expr(src));
234234
if(ieee_repr.is_double())
235235
return floating_point_to_java_string(ieee_repr.to_double());
236236
return floating_point_to_java_string(ieee_repr.to_float());

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,16 +2181,19 @@ exprt::operandst &java_bytecode_convert_methodt::convert_const(
21812181
is_float ? ieee_float_spect::single_precision()
21822182
: ieee_float_spect::double_precision());
21832183

2184-
ieee_floatt value(spec);
21852184
if(arg0.type().id() != ID_floatbv)
21862185
{
2186+
// conversion from integer to float may need rounding
2187+
auto rm = ieee_floatt::rounding_modet::ROUND_TO_EVEN;
21872188
const mp_integer number = numeric_cast_v<mp_integer>(arg0);
2189+
ieee_floatt value(spec, rm);
21882190
value.from_integer(number);
2191+
results[0] = value.to_expr();
21892192
}
21902193
else
2191-
value.from_expr(arg0);
2192-
2193-
results[0] = value.to_expr();
2194+
{
2195+
results[0] = ieee_float_valuet{arg0}.to_expr();
2196+
}
21942197
}
21952198
else
21962199
{

jbmc/src/java_bytecode/java_bytecode_parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ void java_bytecode_parsert::rconstant_pool()
782782

783783
case CONSTANT_Float:
784784
{
785-
ieee_floatt value(ieee_float_spect::single_precision());
785+
ieee_float_valuet value(ieee_float_spect::single_precision());
786786
value.unpack(entry.number);
787787
entry.expr = value.to_expr();
788788
}
@@ -794,7 +794,7 @@ void java_bytecode_parsert::rconstant_pool()
794794

795795
case CONSTANT_Double:
796796
{
797-
ieee_floatt value(ieee_float_spect::double_precision());
797+
ieee_float_valuet value(ieee_float_spect::double_precision());
798798
value.unpack(entry.number);
799799
entry.expr = value.to_expr();
800800
}

jbmc/src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ code_blockt java_string_library_preprocesst::make_float_to_string_code(
938938

939939
// Expression representing 0.0
940940
const ieee_float_spect float_spec{to_floatbv_type(params[0].type())};
941-
ieee_floatt zero_float(float_spec);
941+
ieee_float_valuet zero_float(float_spec);
942942
zero_float.from_float(0.0);
943943
const constant_exprt zero = zero_float.to_expr();
944944

@@ -996,16 +996,17 @@ code_blockt java_string_library_preprocesst::make_float_to_string_code(
996996
string_expr_list.push_back(zero_string);
997997

998998
// Case of -0.0
999-
ieee_floatt minus_zero_float(float_spec);
999+
ieee_float_valuet minus_zero_float(float_spec);
10001000
minus_zero_float.from_float(-0.0f);
10011001
condition_list.push_back(equal_exprt(arg, minus_zero_float.to_expr()));
10021002
const refined_string_exprt minus_zero_string =
10031003
string_literal_to_string_expr("-0.0", loc, symbol_table, code);
10041004
string_expr_list.push_back(minus_zero_string);
10051005

10061006
// Case of simple notation
1007-
ieee_floatt bound_inf_float(float_spec);
1008-
ieee_floatt bound_sup_float(float_spec);
1007+
auto rm = ieee_floatt::rounding_modet::ROUND_TO_EVEN;
1008+
ieee_floatt bound_inf_float(float_spec, rm);
1009+
ieee_floatt bound_sup_float(float_spec, rm);
10091010
bound_inf_float.from_float(1e-3f);
10101011
bound_sup_float.from_float(1e7f);
10111012
bound_inf_float.change_spec(float_spec);

src/analyses/interval_domain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void interval_domaint::assume_rec(
288288
}
289289
else if(is_float(lhs.type()) && is_float(rhs.type()))
290290
{
291-
ieee_floatt tmp(to_constant_expr(rhs));
291+
ieee_float_valuet tmp(to_constant_expr(rhs));
292292
if(id==ID_lt)
293293
tmp.decrement();
294294
ieee_float_intervalt &fi=float_map[lhs_identifier];
@@ -313,7 +313,7 @@ void interval_domaint::assume_rec(
313313
}
314314
else if(is_float(lhs.type()) && is_float(rhs.type()))
315315
{
316-
ieee_floatt tmp(to_constant_expr(lhs));
316+
ieee_float_valuet tmp(to_constant_expr(lhs));
317317
if(id==ID_lt)
318318
tmp.increment();
319319
ieee_float_intervalt &fi=float_map[rhs_identifier];

src/analyses/interval_domain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Author: Daniel Kroening, [email protected]
1818

1919
#include "ai_domain.h"
2020

21-
typedef interval_templatet<ieee_floatt> ieee_float_intervalt;
21+
typedef interval_templatet<ieee_float_valuet> ieee_float_intervalt;
2222

2323
class interval_domaint:public ai_domain_baset
2424
{

src/ansi-c/expr2c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,7 @@ std::string expr2ct::convert_constant(
19121912
}
19131913
else if(type.id()==ID_floatbv)
19141914
{
1915-
dest=ieee_floatt(to_constant_expr(src)).to_ansi_c_string();
1915+
dest = ieee_float_valuet(to_constant_expr(src)).to_ansi_c_string();
19161916

19171917
if(!dest.empty() && isdigit(dest[dest.size() - 1]))
19181918
{

src/ansi-c/goto-conversion/goto_check_c.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -761,12 +761,13 @@ void goto_check_ct::conversion_check(const exprt &expr, const guardt &guard)
761761
else if(old_type.id() == ID_floatbv) // float -> signed
762762
{
763763
// Note that the fractional part is truncated!
764-
ieee_floatt upper(to_floatbv_type(old_type));
764+
auto rm = ieee_floatt::rounding_modet::ROUND_TO_EVEN;
765+
ieee_floatt upper(to_floatbv_type(old_type), rm);
765766
upper.from_integer(power(2, new_width - 1));
766767
const binary_relation_exprt no_overflow_upper(
767768
op, ID_lt, upper.to_expr());
768769

769-
ieee_floatt lower(to_floatbv_type(old_type));
770+
ieee_floatt lower(to_floatbv_type(old_type), rm);
770771
lower.from_integer(-power(2, new_width - 1) - 1);
771772
const binary_relation_exprt no_overflow_lower(
772773
op, ID_gt, lower.to_expr());
@@ -844,12 +845,13 @@ void goto_check_ct::conversion_check(const exprt &expr, const guardt &guard)
844845
else if(old_type.id() == ID_floatbv) // float -> unsigned
845846
{
846847
// Note that the fractional part is truncated!
847-
ieee_floatt upper(to_floatbv_type(old_type));
848+
auto rm = ieee_floatt::rounding_modet::ROUND_TO_EVEN;
849+
ieee_floatt upper(to_floatbv_type(old_type), rm);
848850
upper.from_integer(power(2, new_width));
849851
const binary_relation_exprt no_overflow_upper(
850852
op, ID_lt, upper.to_expr());
851853

852-
ieee_floatt lower(to_floatbv_type(old_type));
854+
ieee_floatt lower(to_floatbv_type(old_type), rm);
853855
lower.from_integer(-1);
854856
const binary_relation_exprt no_overflow_lower(
855857
op, ID_gt, lower.to_expr());
@@ -1295,8 +1297,8 @@ void goto_check_ct::nan_check(const exprt &expr, const guardt &guard)
12951297
// -inf + +inf = NaN and +inf + -inf = NaN,
12961298
// i.e., signs differ
12971299
ieee_float_spect spec = ieee_float_spect(to_floatbv_type(plus_expr.type()));
1298-
exprt plus_inf = ieee_floatt::plus_infinity(spec).to_expr();
1299-
exprt minus_inf = ieee_floatt::minus_infinity(spec).to_expr();
1300+
exprt plus_inf = ieee_float_valuet::plus_infinity(spec).to_expr();
1301+
exprt minus_inf = ieee_float_valuet::minus_infinity(spec).to_expr();
13001302

13011303
isnan = or_exprt(
13021304
and_exprt(
@@ -1315,8 +1317,8 @@ void goto_check_ct::nan_check(const exprt &expr, const guardt &guard)
13151317

13161318
ieee_float_spect spec =
13171319
ieee_float_spect(to_floatbv_type(minus_expr.type()));
1318-
exprt plus_inf = ieee_floatt::plus_infinity(spec).to_expr();
1319-
exprt minus_inf = ieee_floatt::minus_infinity(spec).to_expr();
1320+
exprt plus_inf = ieee_float_valuet::plus_infinity(spec).to_expr();
1321+
exprt minus_inf = ieee_float_valuet::minus_infinity(spec).to_expr();
13201322

13211323
isnan = or_exprt(
13221324
and_exprt(

src/ansi-c/literals/convert_float_literal.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ exprt convert_float_literal(const std::string &src)
7171
// but these aren't handled anywhere
7272
}
7373

74-
ieee_floatt a(type);
74+
// This may require rounding.
75+
auto rm = ieee_floatt::rounding_modet::ROUND_TO_EVEN;
76+
ieee_floatt a(type, rm);
7577

7678
if(parsed_float.exponent_base==10)
7779
a.from_base10(parsed_float.significand, parsed_float.exponent);

0 commit comments

Comments
 (0)