forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat_utils.cpp
133 lines (113 loc) · 3.17 KB
/
float_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <solvers/sat/satcheck.h>
#include <solvers/floatbv/float_utils.h>
float random_float()
{
unsigned u=random();
u=(u<<16)^random();
return *(float *)&u;
}
bool eq(const ieee_floatt &a, const ieee_floatt &b)
{
if(a.is_NaN() && b.is_NaN()) return true;
if(a.is_infinity() && b.is_infinity() &&
a.get_sign()==b.get_sign()) return true;
return a==b;
}
std::string to_str(const bvt &bv)
{
std::string result;
for(unsigned i=0; i<bv.size(); i++)
{
char ch;
if(bv[i]==const_literal(true))
ch='1';
else if(bv[i]==const_literal(false))
ch='0';
else
ch='?';
result=ch+result;
}
return result;
}
typedef enum { PLUS=0, MINUS=1, MULT=2, DIV=3 } binopt;
const char *binopsyms[]={ " + ", " - ", " * ", " / " };
int main()
{
ieee_floatt i1, i2, i3;
bvt b1, b2, b3, res;
float f1, f2, f3;
for(unsigned i=0; i<200; i++)
{
if(i%20==0)
std::cout << "*********** " << i << std::endl;
satcheckt satcheck;
float_utilst float_utils(satcheck);
#if 0
f1=random_float();
f2=random_float();
i1.from_float(f1);
i2.from_float(f2);
float_utils.spec=i1.spec;
b1=float_utils.build_constant(i1);
b2=float_utils.build_constant(i2);
f3=f1;
int op=(binopt)i%3;
// op=PLUS;
#else
f1=-0.25;
f2=-2.5;
i1.from_float(f1);
i2.from_float(f2);
float_utils.spec=i1.spec;
b1=float_utils.build_constant(i1);
b2=float_utils.build_constant(i2);
f3=f1;
int op=DIV;
#endif
switch(op)
{
case PLUS:
f3+=f2;
res=float_utils.add(b1, b2);
break;
case MINUS:
f3-=f2;
res=float_utils.sub(b1, b2);
break;
case MULT:
f3*=f2;
res=float_utils.mul(b1, b2);
break;
case DIV:
f3/=f2;
res=float_utils.div(b1, b2);
break;
default:assert(0);
}
i3.from_float(f3);
satcheckt::resultt result=satcheck.prop_solve();
assert(result==satcheckt::P_SATISFIABLE);
ieee_floatt fres;
fres=float_utils.get(res);
if(!eq(fres, i3))
{
const char *opsym=binopsyms[op];
std::cout << i1 << opsym << i2 << " != " << fres << std::endl;
std::cout << f1 << opsym << f2 << " == " << f3 << std::endl;
std::cout << integer2binary(i1.get_exponent(), i1.spec.e) << " " <<
integer2binary(i1.get_fraction(), i1.spec.f+1) << opsym <<
integer2binary(i2.get_exponent(), i1.spec.e) << " " <<
integer2binary(i2.get_fraction(), i1.spec.f+1) << " != " <<
integer2binary(fres.get_exponent(), i1.spec.e) << " " <<
integer2binary(fres.get_fraction(), i1.spec.f+1) << std::endl;
std::cout << integer2binary(i1.get_exponent(), i1.spec.e) << " " <<
integer2binary(i1.get_fraction(), i1.spec.f+1) << opsym <<
integer2binary(i2.get_exponent(), i1.spec.e) << " " <<
integer2binary(i2.get_fraction(), i1.spec.f+1) << " == " <<
integer2binary(i3.get_exponent(), i1.spec.e) << " " <<
integer2binary(i3.get_fraction(), i1.spec.f+1) << std::endl;
std::cout << std::endl;
}
}
}