Skip to content

Commit 9908b6e

Browse files
author
Daniel Kroening
committed
arithmetic promotion for fix-point arithmetic; issue #435
1 parent a28017e commit 9908b6e

File tree

10 files changed

+54
-25
lines changed

10 files changed

+54
-25
lines changed

regression/cbmc/Fixedbv1/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
int main() {
2-
double x;
2+
__CPROVER_fixedbv[64][32] x;
33
int y;
44

55
x=2;

regression/cbmc/Fixedbv1/test.desc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CORE
1+
KNOWNBUG
22
main.c
3-
--fixedbv
3+
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

regression/cbmc/Fixedbv2/main.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
main() {
2-
float a;
3-
double b;
2+
__CPROVER_fixedbv[32][16] a;
3+
__CPROVER_fixedbv[64][32] b;
44

5-
a=1.25L;
6-
assert(a==1.25);
5+
a=1.25L;
6+
assert(a==1.25);
77

8-
b=1.250;
9-
assert(b==1.25);
8+
b=1.250;
9+
assert(b==1.25);
1010
}

regression/cbmc/Fixedbv2/test.desc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CORE
1+
KNOWNBUG
22
main.c
3-
--fixedbv
3+
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

regression/cbmc/Fixedbv3/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
int nondet_int();
22

3-
double d = 0.0;
3+
__CPROVER_fixedbv[64][32] d = 0.0;
44

55
void f1()
66
{

regression/cbmc/Fixedbv3/test.desc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CORE
1+
KNOWNBUG
22
main.c
3-
--fixedbv
3+
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

regression/cbmc/Fixedbv5/main.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
int main()
22
{
3-
float a, b;
3+
typedef __CPROVER_fixedbv[32][16] ft;
4+
ft a, b;
45

5-
__CPROVER_assume(a==1 || a==0.5 || a==2 || a==3 || a==0.1);
6+
__CPROVER_assume(a==1 || a==(ft)0.5 || a==2 || a==3 || a==(ft)0.1);
67
b=a;
78
a/=2;
89
a*=2;
9-
__CPROVER_assert(a==b, "float div works");
10+
__CPROVER_assert(a==b, "fixedbv div works");
1011
}

regression/cbmc/Fixedbv5/test.desc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CORE
1+
KNOWNBUG
22
main.c
3-
--fixedbv
3+
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

src/ansi-c/c_typecast.cpp

+31-4
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,7 @@ c_typecastt::c_typet c_typecastt::get_c_type(
383383
return BOOL;
384384
else if(type.id()==ID_c_bool)
385385
return BOOL;
386-
else if(type.id()==ID_floatbv ||
387-
type.id()==ID_fixedbv)
386+
else if(type.id()==ID_floatbv)
388387
{
389388
if(width<=config.ansi_c.single_width)
390389
return SINGLE;
@@ -395,6 +394,10 @@ c_typecastt::c_typet c_typecastt::get_c_type(
395394
else if(width<=128)
396395
return FLOAT128;
397396
}
397+
else if(type.id()==ID_fixedbv)
398+
{
399+
return FIXEDBV;
400+
}
398401
else if(type.id()==ID_pointer)
399402
{
400403
if(type.subtype().id()==ID_empty)
@@ -728,8 +731,8 @@ void c_typecastt::implicit_typecast_arithmetic(
728731
if(max_type==LARGE_SIGNED_INT || max_type==LARGE_UNSIGNED_INT)
729732
{
730733
// get the biggest width of both
731-
unsigned width1=type1.get_int(ID_width);
732-
unsigned width2=type2.get_int(ID_width);
734+
std::size_t width1=type1.get_size_t(ID_width);
735+
std::size_t width2=type2.get_size_t(ID_width);
733736

734737
// produce type
735738
typet result_type;
@@ -751,6 +754,30 @@ void c_typecastt::implicit_typecast_arithmetic(
751754

752755
return;
753756
}
757+
else if(max_type==FIXEDBV)
758+
{
759+
typet result_type;
760+
761+
if(c_type1==FIXEDBV && c_type2==FIXEDBV)
762+
{
763+
// get bigger of both
764+
std::size_t width1=to_fixedbv_type(type1).get_width();
765+
std::size_t width2=to_fixedbv_type(type2).get_width();
766+
if(width1>=width2)
767+
result_type=type1;
768+
else
769+
result_type=type2;
770+
}
771+
else if(c_type1==FIXEDBV)
772+
result_type=type1;
773+
else
774+
result_type=type2;
775+
776+
do_typecast(expr1, result_type);
777+
do_typecast(expr2, result_type);
778+
779+
return;
780+
}
754781
else if(max_type==COMPLEX)
755782
{
756783
if(c_type1==COMPLEX && c_type2==COMPLEX)

src/ansi-c/c_typecast.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ class c_typecastt
7272
LONG, ULONG,
7373
LONGLONG, ULONGLONG,
7474
LARGE_SIGNED_INT, LARGE_UNSIGNED_INT,
75-
INTEGER, // these are unbounded integers
75+
INTEGER, // these are unbounded integers, non-standard
76+
FIXEDBV, // fixed-point, non-standard
7677
SINGLE, DOUBLE, LONGDOUBLE, FLOAT128, // float
77-
RATIONAL, REAL, // these are infinite precision
78+
RATIONAL, REAL, // infinite precision, non-standard
7879
COMPLEX,
7980
VOIDPTR, PTR, OTHER };
8081

0 commit comments

Comments
 (0)