Skip to content

Fix a bug in the bitwise encoding of float to int casting. #359

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
Dec 19, 2016
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
18 changes: 18 additions & 0 deletions regression/cbmc/Float-to-int1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <assert.h>

double nondet_double();

int main(void)
{
double d = nondet_double();

__CPROVER_assume(d < 0x1.0p+63 && d > 0x1.0p+53);

long long int i = d;
double d1 = i;

assert(d1 != 0x0);

return 0;
}

8 changes: 8 additions & 0 deletions regression/cbmc/Float-to-int1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--floatbv
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
16 changes: 16 additions & 0 deletions regression/cbmc/Float-to-int2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <assert.h>

int nondet_int();
double nondet_double();

int main(void)
{
int i = nondet_int();
double di = (double)i;
int j = (int)di;

assert(i == j);

return 0;
}

8 changes: 8 additions & 0 deletions regression/cbmc/Float-to-int2/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--floatbv
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
20 changes: 20 additions & 0 deletions regression/cbmc/Float-to-int3/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <assert.h>
#include <stdint.h>

int64_t nondet_int64_t();
double nondet_double();

int main(void)
{
int64_t i = nondet_int64_t();

__CPROVER_assume((i & (int64_t)0x7FF) == (int64_t)0);

double di = (double)i;
int64_t j = (int64_t)di;

assert(i == j);

return 0;
}

8 changes: 8 additions & 0 deletions regression/cbmc/Float-to-int3/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--floatbv
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
20 changes: 13 additions & 7 deletions src/solvers/floatbv/float_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,21 @@ bvt float_utilst::to_integer(
// The following is the usual case in ANSI-C, and we optimize for that.
if(rounding_mode_bits.round_to_zero.is_true())
{
bvt fraction=unpacked.fraction;

if(dest_width>fraction.size())
{
bvt lsb_extension=bv_utils.build_constant(0U, dest_width-fraction.size());
fraction.insert(fraction.begin(),
lsb_extension.begin(),
lsb_extension.end());
}

// if the exponent is positive, shift right
bvt offset=bv_utils.build_constant(spec.f, unpacked.exponent.size());
bvt offset=bv_utils.build_constant(fraction.size()-1,
unpacked.exponent.size());
bvt distance=bv_utils.sub(offset, unpacked.exponent);
bvt shift_result=bv_utils.shift(unpacked.fraction, bv_utilst::LRIGHT, distance);
bvt shift_result=bv_utils.shift(fraction, bv_utilst::LRIGHT, distance);

// if the exponent is negative, we have zero anyways
bvt result=shift_result;
Expand All @@ -176,11 +187,6 @@ bvt float_utilst::to_integer(
{
result.resize(dest_width);
}
else if(result.size()<dest_width)
{
// zero extend
result=bv_utils.zero_extension(result, dest_width);
}

assert(result.size()==dest_width);

Expand Down