-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathfloatbv_expr.h
576 lines (504 loc) · 14.1 KB
/
floatbv_expr.h
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*******************************************************************\
Module: API to expression classes for floating-point arithmetic
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#ifndef CPROVER_UTIL_FLOATBV_EXPR_H
#define CPROVER_UTIL_FLOATBV_EXPR_H
/// \file util/floatbv_expr.h
/// API to expression classes for floating-point arithmetic
#include "std_expr.h"
/// \brief Semantic type conversion from/to floating-point formats
class floatbv_typecast_exprt : public binary_exprt
{
public:
floatbv_typecast_exprt(exprt op, exprt rounding, typet _type)
: binary_exprt(
std::move(op),
ID_floatbv_typecast,
std::move(rounding),
std::move(_type))
{
}
exprt &op()
{
return op0();
}
const exprt &op() const
{
return op0();
}
exprt &rounding_mode()
{
return op1();
}
const exprt &rounding_mode() const
{
return op1();
}
};
template <>
inline bool can_cast_expr<floatbv_typecast_exprt>(const exprt &base)
{
return base.id() == ID_floatbv_typecast;
}
inline void validate_expr(const floatbv_typecast_exprt &value)
{
validate_operands(value, 2, "Float typecast must have two operands");
}
/// \brief Cast an exprt to a \ref floatbv_typecast_exprt
///
/// \a expr must be known to be \ref floatbv_typecast_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref floatbv_typecast_exprt
inline const floatbv_typecast_exprt &to_floatbv_typecast_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_floatbv_typecast);
const floatbv_typecast_exprt &ret =
static_cast<const floatbv_typecast_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \copydoc to_floatbv_typecast_expr(const exprt &)
inline floatbv_typecast_exprt &to_floatbv_typecast_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_floatbv_typecast);
floatbv_typecast_exprt &ret = static_cast<floatbv_typecast_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \brief Round a floating-point number to an integral value
/// considering the given rounding mode
class floatbv_round_to_integral_exprt : public binary_exprt
{
public:
floatbv_round_to_integral_exprt(exprt op, exprt rounding)
: binary_exprt(
op,
ID_floatbv_round_to_integral,
std::move(rounding),
op.type())
{
}
exprt &op()
{
return op0();
}
const exprt &op() const
{
return op0();
}
exprt &rounding_mode()
{
return op1();
}
const exprt &rounding_mode() const
{
return op1();
}
};
template <>
inline bool can_cast_expr<floatbv_round_to_integral_exprt>(const exprt &base)
{
return base.id() == ID_floatbv_round_to_integral;
}
/// \brief Cast an exprt to a \ref floatbv_round_to_integral_exprt
///
/// \a expr must be known to be \ref floatbv_round_to_integral_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref floatbv_round_to_integral_exprt
inline const floatbv_round_to_integral_exprt &
to_floatbv_round_to_integral_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_floatbv_round_to_integral);
floatbv_round_to_integral_exprt::check(expr);
return static_cast<const floatbv_round_to_integral_exprt &>(expr);
}
/// \copydoc to_floatbv_round_to_integral_expr(const exprt &)
inline floatbv_round_to_integral_exprt &
to_floatbv_round_to_integral_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_floatbv_round_to_integral);
floatbv_round_to_integral_exprt::check(expr);
return static_cast<floatbv_round_to_integral_exprt &>(expr);
}
/// \brief Evaluates to true if the operand is NaN
class isnan_exprt : public unary_predicate_exprt
{
public:
explicit isnan_exprt(exprt op)
: unary_predicate_exprt(ID_isnan, std::move(op))
{
}
};
template <>
inline bool can_cast_expr<isnan_exprt>(const exprt &base)
{
return base.id() == ID_isnan;
}
inline void validate_expr(const isnan_exprt &value)
{
validate_operands(value, 1, "Is NaN must have one operand");
}
/// \brief Cast an exprt to a \ref isnan_exprt
///
/// \a expr must be known to be \ref isnan_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref isnan_exprt
inline const isnan_exprt &to_isnan_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_isnan);
const isnan_exprt &ret = static_cast<const isnan_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \copydoc to_isnan_expr(const exprt &)
inline isnan_exprt &to_isnan_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_isnan);
isnan_exprt &ret = static_cast<isnan_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \brief Evaluates to true if the operand is infinite
class isinf_exprt : public unary_predicate_exprt
{
public:
explicit isinf_exprt(exprt op)
: unary_predicate_exprt(ID_isinf, std::move(op))
{
}
};
template <>
inline bool can_cast_expr<isinf_exprt>(const exprt &base)
{
return base.id() == ID_isinf;
}
inline void validate_expr(const isinf_exprt &value)
{
validate_operands(value, 1, "Is infinite must have one operand");
}
/// \brief Cast an exprt to a \ref isinf_exprt
///
/// \a expr must be known to be \ref isinf_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref isinf_exprt
inline const isinf_exprt &to_isinf_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_isinf);
const isinf_exprt &ret = static_cast<const isinf_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \copydoc to_isinf_expr(const exprt &)
inline isinf_exprt &to_isinf_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_isinf);
isinf_exprt &ret = static_cast<isinf_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \brief Evaluates to true if the operand is finite
class isfinite_exprt : public unary_predicate_exprt
{
public:
explicit isfinite_exprt(exprt op)
: unary_predicate_exprt(ID_isfinite, std::move(op))
{
}
};
template <>
inline bool can_cast_expr<isfinite_exprt>(const exprt &base)
{
return base.id() == ID_isfinite;
}
inline void validate_expr(const isfinite_exprt &value)
{
validate_operands(value, 1, "Is finite must have one operand");
}
/// \brief Cast an exprt to a \ref isfinite_exprt
///
/// \a expr must be known to be \ref isfinite_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref isfinite_exprt
inline const isfinite_exprt &to_isfinite_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_isfinite);
const isfinite_exprt &ret = static_cast<const isfinite_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \copydoc to_isfinite_expr(const exprt &)
inline isfinite_exprt &to_isfinite_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_isfinite);
isfinite_exprt &ret = static_cast<isfinite_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \brief Evaluates to true if the operand is a normal number
class isnormal_exprt : public unary_predicate_exprt
{
public:
explicit isnormal_exprt(exprt op)
: unary_predicate_exprt(ID_isnormal, std::move(op))
{
}
};
template <>
inline bool can_cast_expr<isnormal_exprt>(const exprt &base)
{
return base.id() == ID_isnormal;
}
inline void validate_expr(const isnormal_exprt &value)
{
validate_operands(value, 1, "Is normal must have one operand");
}
/// \brief Cast an exprt to a \ref isnormal_exprt
///
/// \a expr must be known to be \ref isnormal_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref isnormal_exprt
inline const isnormal_exprt &to_isnormal_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_isnormal);
const isnormal_exprt &ret = static_cast<const isnormal_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \copydoc to_isnormal_expr(const exprt &)
inline isnormal_exprt &to_isnormal_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_isnormal);
isnormal_exprt &ret = static_cast<isnormal_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \brief IEEE-floating-point equality
class ieee_float_equal_exprt : public binary_relation_exprt
{
public:
ieee_float_equal_exprt(exprt _lhs, exprt _rhs)
: binary_relation_exprt(
std::move(_lhs),
ID_ieee_float_equal,
std::move(_rhs))
{
}
};
template <>
inline bool can_cast_expr<ieee_float_equal_exprt>(const exprt &base)
{
return base.id() == ID_ieee_float_equal;
}
inline void validate_expr(const ieee_float_equal_exprt &value)
{
validate_operands(value, 2, "IEEE equality must have two operands");
}
/// \brief Cast an exprt to an \ref ieee_float_equal_exprt
///
/// \a expr must be known to be \ref ieee_float_equal_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref ieee_float_equal_exprt
inline const ieee_float_equal_exprt &to_ieee_float_equal_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_ieee_float_equal);
const ieee_float_equal_exprt &ret =
static_cast<const ieee_float_equal_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \copydoc to_ieee_float_equal_expr(const exprt &)
inline ieee_float_equal_exprt &to_ieee_float_equal_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_ieee_float_equal);
ieee_float_equal_exprt &ret = static_cast<ieee_float_equal_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \brief IEEE floating-point disequality
class ieee_float_notequal_exprt : public binary_relation_exprt
{
public:
ieee_float_notequal_exprt(exprt _lhs, exprt _rhs)
: binary_relation_exprt(
std::move(_lhs),
ID_ieee_float_notequal,
std::move(_rhs))
{
}
};
template <>
inline bool can_cast_expr<ieee_float_notequal_exprt>(const exprt &base)
{
return base.id() == ID_ieee_float_notequal;
}
inline void validate_expr(const ieee_float_notequal_exprt &value)
{
validate_operands(value, 2, "IEEE inequality must have two operands");
}
/// \brief Cast an exprt to an \ref ieee_float_notequal_exprt
///
/// \a expr must be known to be \ref ieee_float_notequal_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref ieee_float_notequal_exprt
inline const ieee_float_notequal_exprt &
to_ieee_float_notequal_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_ieee_float_notequal);
const ieee_float_notequal_exprt &ret =
static_cast<const ieee_float_notequal_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \copydoc to_ieee_float_notequal_expr(const exprt &)
inline ieee_float_notequal_exprt &to_ieee_float_notequal_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_ieee_float_notequal);
ieee_float_notequal_exprt &ret =
static_cast<ieee_float_notequal_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \brief IEEE floating-point operations
/// These have two data operands (op0 and op1) and one rounding mode (op2).
/// The type of the result is that of the data operands.
class ieee_float_op_exprt : public ternary_exprt
{
public:
ieee_float_op_exprt(
const exprt &_lhs,
const irep_idt &_id,
exprt _rhs,
exprt _rm)
: ternary_exprt(_id, _lhs, std::move(_rhs), std::move(_rm), _lhs.type())
{
}
exprt &lhs()
{
return op0();
}
const exprt &lhs() const
{
return op0();
}
exprt &rhs()
{
return op1();
}
const exprt &rhs() const
{
return op1();
}
exprt &rounding_mode()
{
return op2();
}
const exprt &rounding_mode() const
{
return op2();
}
};
template <>
inline bool can_cast_expr<ieee_float_op_exprt>(const exprt &base)
{
return base.id() == ID_floatbv_plus || base.id() == ID_floatbv_minus ||
base.id() == ID_floatbv_div || base.id() == ID_floatbv_mult;
}
inline void validate_expr(const ieee_float_op_exprt &value)
{
validate_operands(
value, 3, "IEEE float operations must have three arguments");
}
/// \brief Cast an exprt to an \ref ieee_float_op_exprt
///
/// \a expr must be known to be \ref ieee_float_op_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref ieee_float_op_exprt
inline const ieee_float_op_exprt &to_ieee_float_op_expr(const exprt &expr)
{
const ieee_float_op_exprt &ret =
static_cast<const ieee_float_op_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \copydoc to_ieee_float_op_expr(const exprt &)
inline ieee_float_op_exprt &to_ieee_float_op_expr(exprt &expr)
{
ieee_float_op_exprt &ret = static_cast<ieee_float_op_exprt &>(expr);
validate_expr(ret);
return ret;
}
/// \brief IEEE floating-point mod
///
/// Note that this expression does not have a rounding mode.
class floatbv_mod_exprt : public binary_exprt
{
public:
floatbv_mod_exprt(exprt _lhs, exprt _rhs)
: binary_exprt(_lhs, ID_floatbv_mod, _rhs, _lhs.type())
{
}
};
/// \brief Cast an exprt to a \ref floatbv_mod_exprt
///
/// \a expr must be known to be \ref floatbv_mod_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref floatbv_mod_exprt
inline const floatbv_mod_exprt &to_floatbv_mod_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_floatbv_mod);
floatbv_mod_exprt::check(expr);
return static_cast<const floatbv_mod_exprt &>(expr);
}
/// \copydoc to_floatbv_mod_expr(const exprt &)
inline floatbv_mod_exprt &to_floatbv_mod_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_floatbv_mod);
floatbv_mod_exprt::check(expr);
return static_cast<floatbv_mod_exprt &>(expr);
}
/// \brief IEEE floating-point rem
///
/// Note that this expression does not have a rounding mode.
class floatbv_rem_exprt : public binary_exprt
{
public:
floatbv_rem_exprt(exprt _lhs, exprt _rhs)
: binary_exprt(_lhs, ID_floatbv_rem, _rhs, _lhs.type())
{
}
};
/// \brief Cast an exprt to a \ref floatbv_rem_exprt
///
/// \a expr must be known to be \ref floatbv_rem_exprt.
///
/// \param expr: Source expression
/// \return Object of type \ref floatbv_rem_exprt
inline const floatbv_rem_exprt &to_floatbv_rem_expr(const exprt &expr)
{
PRECONDITION(expr.id() == ID_floatbv_rem);
floatbv_rem_exprt::check(expr);
return static_cast<const floatbv_rem_exprt &>(expr);
}
/// \copydoc to_floatbv_rem_expr(const exprt &)
inline floatbv_rem_exprt &to_floatbv_rem_expr(exprt &expr)
{
PRECONDITION(expr.id() == ID_floatbv_rem);
floatbv_rem_exprt::check(expr);
return static_cast<floatbv_rem_exprt &>(expr);
}
/// \brief returns the a rounding mode expression for a given
/// IEEE rounding mode, encoded using the recommendation in
/// C11 5.2.4.2.2
constant_exprt floatbv_rounding_mode(unsigned);
#endif // CPROVER_UTIL_FLOATBV_EXPR_H