forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathieee_float.cpp
306 lines (258 loc) · 6.45 KB
/
ieee_float.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <cstdlib>
#include <ctime>
#include <limits>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <cassert>
#ifdef _WIN32
#define random() rand()
#define nextafterf(a, b) (throw "no nextafterf", 0);
#endif
#include <util/ieee_float.h>
#define PINF (std::numeric_limits<float>::infinity())
#define NINF (-std::numeric_limits<float>::infinity())
#ifndef NZERO
#define NZERO (-0.0f)
#endif
#define PZERO (0.0f)
#ifndef NAN
# define NAN (std::numeric_limits<float>::quiet_NaN())
#endif
std::string float2binary(float f)
{
union
{
float f;
int i;
} c;
c.f = f;
return integer2binary(c.i, 32);
}
float random_float()
{
union
{
float f;
unsigned i;
} u;
unsigned r = ((unsigned) random()) % 20;
switch(r)
{
case 0:
return PINF;
break;
case 1:
return NINF;
break;
case 2:
return NAN;
break;
case 3:
return PZERO;
break;
case 4:
return NZERO;
break;
default:
u.i=random();
u.i=(u.i<<16)^random();
return u.f;
}
}
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;
}
typedef enum { PLUS=0, MINUS=1, MULT=2, DIV=3 } binopt;
typedef enum { EQ=0, NEQ=1, LT=2, LE=3, GT=4, GE=5} binrel;
const char *binopsyms[]={ " + ", " - ", " * ", " / " };
const char *binrelsyms[]={ " == ", " != ", " < ", " <= ", " > ", " >= "};
void check_arithmetic(int i)
{
ieee_floatt i1, i2, i3, res;
float f1, f2, f3;
f1=random_float();
f2=random_float();
i1.from_float(f1);
i2.from_float(f2);
res=i1;
f3=f1;
int op=(binopt)i%4;
switch(op)
{
case PLUS:
f3+=f2;
res+=i2;
break;
case MINUS:
f3-=f2;
res-=i2;
break;
case MULT:
f3*=f2;
res*=i2;
break;
case DIV:
f3/=f2;
res/=i2;
break;
default:assert(0);
}
i3.from_float(f3);
if(!eq(res, i3))
{
const char *opsym=binopsyms[op];
std::cout << i1 << opsym << i2 << " != " << res << std::endl;
std::cout << f1 << opsym << f2 << " == " << f3 << std::endl;
std::cout << integer2binary(i1.get_fraction(), i1.spec.f+1) << opsym <<
integer2binary(i2.get_fraction(), i1.spec.f+1) << " != " <<
integer2binary(res.get_fraction(), i1.spec.f+1) <<
" (" << res.get_fraction() << ")" << std::endl;
std::cout << integer2binary(i1.get_fraction(), i1.spec.f+1) << opsym <<
integer2binary(i2.get_fraction(), i1.spec.f+1) << " == " <<
integer2binary(i3.get_fraction(), i1.spec.f+1) <<
" (" << i3.get_fraction() << ")" << std::endl;
std::cout << std::endl;
}
}
void check_comparison(int i)
{
ieee_floatt i1, i2;
float f1, f2;
bool ires, fres;
f1=random_float();
f2=random_float();
i1.from_float(f1);
i2.from_float(f2);
int op=(binrel)i%6;
switch(op)
{
case EQ:
ires = ieee_equal(i1, i2);
fres = (f1 == f2);
break;
case NEQ:
ires = ieee_not_equal(i1, i2);
fres = (f1 != f2);
break;
case LT:
ires = (i1 < i2);
fres = (f1 < f2);
break;
case LE:
ires = (i1 <= i2);
fres = (f1 <= f2);
break;
case GT:
ires = (i1 > i2);
fres = (f1 > f2);
break;
case GE:
ires = (i1 >= i2);
fres = (f1 >= f2);
break;
default:
assert(0);
}
if(ires != fres)
{
const char *opsym=binrelsyms[op];
std::cout << i1 << opsym << i2 << " != " << ires << std::endl;
std::cout << f1 << opsym << f2 << " == " << fres << std::endl;
std::cout << integer2binary(i1.get_fraction(), i1.spec.f+1) << opsym <<
integer2binary(i2.get_fraction(), i1.spec.f+1) << " != " << ires;
std::cout << integer2binary(i1.get_fraction(), i1.spec.f+1) << opsym <<
integer2binary(i2.get_fraction(), i1.spec.f+1) << " == " << fres;
std::cout << std::endl;
}
}
void check_conversion(int i)
{
union
{
float f;
unsigned i;
} a, b;
a.f = random_float();
ieee_floatt t;
t.from_float(a.f);
assert(t.is_float());
b.f = t.to_float();
if(a.i != b.i && !((a.f != a.f) && (b.f != b.f)))
{
std::cout << "conversion failure: " << a.f << " -> " << t << " -> "
<< b.f << std::endl;
}
}
void check_nextafter(int i)
{
float f1 = random_float();
float f2 = nextafterf(f1, PINF);
float f3 = nextafterf(f1, NINF);
ieee_floatt i1, i2, i3;
i1.from_float(f1);
i2 = i1;
i2.increment(false);
i3 = i1;
i3.decrement(false);
if((f1 != i1.to_float() && !(f1 != f1 && i1.is_NaN())) ||
(f2 != i2.to_float() && !(f2 != f2 && i2.is_NaN())) ||
(f3 != i3.to_float() && !(f3 != f3 && i3.is_NaN())))
{
std::cout << "Incorrect nextafter: " << std::endl
<< "mach float: " << f1 << " " << f2 << " " << f3 << std::endl
<< "ieee_float: " << i1.to_float() << " "
<< i2.to_float() << " " << i3.to_float() << std::endl;
std::cout << "Binary representation: " << std::endl
<< "mach float: " << float2binary(f1) << " "
<< float2binary(f2) << " " << float2binary(f3) << std::endl
<< "ieee_float: " << float2binary(i1.to_float()) << " "
<< float2binary(i2.to_float())
<< " " << float2binary(i3.to_float()) << std::endl;
}
}
void check_minmax()
{
float f = 0;
ieee_floatt t;
t.from_float(f);
t.make_fltmax();
if(t.to_float() != FLT_MAX)
std::cout << "make_fltmax is broken" << std::endl;
t.make_fltmin();
if(t.to_float() != FLT_MIN)
std::cout << "make_fltmin is broken:"
<< std::endl << " machine float: " << FLT_MIN
<< ", ieee_floatt: " << t.to_float() << "("
<< (t.to_float() == FLT_MIN) << ")" << std::endl;
}
void check_build_extract()
{
float f = random_float();
ieee_floatt t;
t.from_float(f);
mp_integer old_frac, old_exp;
t.extract_base2(old_frac, old_exp);
mp_integer frac_bak=old_frac, exp_bak=old_exp;
t.build(old_frac, old_exp);
t.extract_base2(old_frac, old_exp);
if(frac_bak != old_frac || exp_bak != old_exp)
std::cout << "extract - build - extract is broken for " << t << std::endl;
}
int main()
{
srand(time(0));
check_minmax();
for(unsigned i=0; i<100000; i++)
{
if(i%10000==0) std::cout << "*********** " << i << std::endl;
check_arithmetic(i);
check_comparison(i);
check_conversion(i);
check_nextafter(i);
check_build_extract();
}
}