Skip to content

Commit 57b4a33

Browse files
author
Daniel Kroening
committed
fix adjust_float_expressions
adjust_float_expressions adds a rounding mode to floating-point operations when needed. The fix does the following: 1) casts from/to c_bit_field DO need a rounding mode 2) casts from c_bool and bool DO NOT need a rounding mode
1 parent 1b9a1d3 commit 57b4a33

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

regression/cbmc/Float-no-simp1/main.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1+
#include <assert.h>
2+
3+
struct S
4+
{
5+
unsigned int some_bit : 7;
6+
};
7+
18
int main()
29
{
310
unsigned int i, j;
411
double d;
512

13+
// double to integer and back
614
i=100.0;
715
d=i;
816
j=d;
917
assert(j==100);
18+
19+
// double to bool and back
20+
_Bool b;
21+
b=100.0;
22+
d=b;
23+
assert(d);
24+
25+
// double to bit-field and back
26+
struct S s;
27+
s.some_bit = 100.0;
28+
d=s.some_bit;
29+
s.some_bit=d;
30+
assert(s.some_bit==100);
1031
}

src/goto-programs/adjust_float_expressions.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ static bool have_to_adjust_float_expressions(const exprt &expr)
5454
src_type.id()==ID_floatbv)
5555
return true;
5656
else if(dest_type.id()==ID_floatbv &&
57-
(src_type.id()==ID_c_bool ||
57+
(src_type.id()==ID_c_bit_field ||
5858
src_type.id()==ID_signedbv ||
5959
src_type.id()==ID_unsignedbv ||
6060
src_type.id()==ID_c_enum_tag))
6161
return true;
6262
else if((dest_type.id()==ID_signedbv ||
6363
dest_type.id()==ID_unsignedbv ||
64-
dest_type.id()==ID_c_enum_tag) &&
64+
dest_type.id()==ID_c_enum_tag ||
65+
dest_type.id()==ID_c_bit_field) &&
6566
src_type.id()==ID_floatbv)
6667
return true;
6768
}
@@ -137,19 +138,26 @@ void adjust_float_expressions(exprt &expr, const exprt &rounding_mode)
137138
expr.op1()=rounding_mode;
138139
}
139140
else if(dest_type.id()==ID_floatbv &&
140-
(src_type.id()==ID_c_bool ||
141-
src_type.id()==ID_signedbv ||
141+
(src_type.id()==ID_signedbv ||
142142
src_type.id()==ID_unsignedbv ||
143-
src_type.id()==ID_c_enum_tag))
143+
src_type.id()==ID_c_enum_tag ||
144+
src_type.id()==ID_c_bit_field))
144145
{
145146
// casts from integer to float-type might round
146147
expr.id(ID_floatbv_typecast);
147148
expr.operands().resize(2);
148149
expr.op1()=rounding_mode;
149150
}
151+
else if(dest_type.id()==ID_floatbv &&
152+
(src_type.id()==ID_c_bool ||
153+
src_type.id()==ID_bool))
154+
{
155+
// casts from bool or c_bool to float-type do not need rounding
156+
}
150157
else if((dest_type.id()==ID_signedbv ||
151158
dest_type.id()==ID_unsignedbv ||
152-
dest_type.id()==ID_c_enum_tag) &&
159+
dest_type.id()==ID_c_enum_tag ||
160+
dest_type.id()==ID_c_bit_field) &&
153161
src_type.id()==ID_floatbv)
154162
{
155163
// In C, casts from float to integer always round to zero,

0 commit comments

Comments
 (0)