Skip to content

Commit

Permalink
ieee_floatt:one(...)
Browse files Browse the repository at this point in the history
This adds ieee_floatt::one(...), as a convenience helper for generating an
IEEE 754 number with the value one.
  • Loading branch information
kroening committed Jan 1, 2025
1 parent f9a7807 commit 2d64aa8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
4 changes: 1 addition & 3 deletions src/goto-programs/interpreter_evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,7 @@ interpretert::mp_vectort interpretert::evaluate(const exprt &expr)
}
else if(expr.type().id()==ID_floatbv)
{
ieee_floatt f(to_floatbv_type(expr.type()));
f.from_integer(1);
result=f.pack();
result = ieee_floatt::one(to_floatbv_type(expr.type())).pack();
}
else
result=1;
Expand Down
6 changes: 2 additions & 4 deletions src/solvers/flattening/boolbv_typecast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,8 @@ bool boolbvt::type_conversion(
// bool to float

// build a one
ieee_floatt f(to_floatbv_type(dest_type));
f.from_integer(1);

dest = convert_bv(f.to_expr());
auto one = ieee_floatt::one(to_floatbv_type(dest_type));
dest = convert_bv(one.to_expr());

INVARIANT(
src_width == 1, "bitvector of type boolean shall have width one");
Expand Down
21 changes: 2 additions & 19 deletions src/solvers/smt2/smt2_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3066,29 +3066,12 @@ void smt2_convt::convert_typecast(const typecast_exprt &expr)
{
constant_exprt val(irep_idt(), dest_type);

ieee_floatt a(dest_floatbv_type);

mp_integer significand;
mp_integer exponent;

out << "(ite ";
convert_expr(src);
out << " ";

significand = 1;
exponent = 0;
a.build(significand, exponent);
val.set_value(integer2bvrep(a.pack(), a.spec.width()));

convert_constant(val);
convert_constant(ieee_floatt::one(dest_floatbv_type).to_expr());
out << " ";

significand = 0;
exponent = 0;
a.build(significand, exponent);
val.set_value(integer2bvrep(a.pack(), a.spec.width()));

convert_constant(val);
convert_constant(ieee_floatt::zero(dest_floatbv_type).to_expr());
out << ")";
}
else if(src_type.id()==ID_c_bool)
Expand Down
13 changes: 13 additions & 0 deletions src/util/ieee_float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,19 @@ void ieee_floatt::extract_base10(
}
}

ieee_floatt ieee_floatt::one(const ieee_float_spect &spec)
{
ieee_floatt result{spec};
result.exponent = 0;
result.fraction = power(2, result.spec.f);
return result;
}

ieee_floatt ieee_floatt::one(const floatbv_typet &type)
{
return one(ieee_float_spect{type});
}

void ieee_floatt::build(
const mp_integer &_fraction,
const mp_integer &_exponent)
Expand Down
10 changes: 10 additions & 0 deletions src/util/ieee_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ class ieee_floatt
return result;
}

static ieee_floatt zero(const ieee_float_spect &spec)
{
ieee_floatt result(spec);
result.make_zero();
return result;
}

static ieee_floatt one(const floatbv_typet &);
static ieee_floatt one(const ieee_float_spect &);

void make_NaN();
void make_plus_infinity();
void make_minus_infinity();
Expand Down
1 change: 1 addition & 0 deletions unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ SRC += analyses/ai/ai.cpp \
util/get_base_name.cpp \
util/graph.cpp \
util/help_formatter.cpp \
util/ieee_float.cpp \
util/interval/add.cpp \
util/interval/bitwise.cpp \
util/interval/comparisons.cpp \
Expand Down

0 comments on commit 2d64aa8

Please sign in to comment.