Skip to content

ieee_floatt::one(...) #8552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
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();

Check warning on line 712 in src/goto-programs/interpreter_evaluate.cpp

View check run for this annotation

Codecov / codecov/patch

src/goto-programs/interpreter_evaluate.cpp#L712

Added line #L712 was not covered by tests
}
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
29 changes: 5 additions & 24 deletions src/solvers/smt2/smt2_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3064,32 +3064,13 @@ void smt2_convt::convert_typecast(const typecast_exprt &expr)

if(src_type.id()==ID_bool)
{
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);
out << " ";

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

convert_constant(val);
out << ")";
out << ' ';
convert_constant(ieee_floatt::one(dest_floatbv_type).to_expr());
out << ' ';
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 @@ -220,6 +220,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
17 changes: 17 additions & 0 deletions unit/util/ieee_float.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*******************************************************************\

Module: Unit tests for ieee_floatt

Author: Daniel Kroening, [email protected]

Check warning on line 5 in unit/util/ieee_float.cpp

View check run for this annotation

Codecov / codecov/patch

unit/util/ieee_float.cpp#L5

Added line #L5 was not covered by tests

\*******************************************************************/

#include <util/ieee_float.h>

Check warning on line 9 in unit/util/ieee_float.cpp

View check run for this annotation

Codecov / codecov/patch

unit/util/ieee_float.cpp#L7-L9

Added lines #L7 - L9 were not covered by tests

#include <testing-utils/use_catch.h>

TEST_CASE("Make an IEEE 754 one", "[core][util][ieee_float]")
{
auto spec = ieee_float_spect::single_precision();
REQUIRE(ieee_floatt::one(spec) == 1);
}
Loading