Skip to content

Commit 151d627

Browse files
committed
Introduce floatbv_round_to_integral_exprt
This adds a new expression, floatbv_round_to_integral, which rounds an IEEE 754 floating-point number given as bit-vector to the nearest integer, considering the explicitly given rounding mode.
1 parent b3e9976 commit 151d627

File tree

24 files changed

+622
-341
lines changed

24 files changed

+622
-341
lines changed

Diff for: regression/cbmc-library/__sort_of_CPROVER_round_to_integral-01/main.c

-9
This file was deleted.

Diff for: regression/cbmc-library/__sort_of_CPROVER_round_to_integral-01/test.desc

-8
This file was deleted.

Diff for: regression/cbmc-library/__sort_of_CPROVER_round_to_integralf-01/main.c

-9
This file was deleted.

Diff for: regression/cbmc-library/__sort_of_CPROVER_round_to_integralf-01/test.desc

-8
This file was deleted.

Diff for: regression/cbmc-library/__sort_of_CPROVER_round_to_integrall-01/main.c

-9
This file was deleted.

Diff for: regression/cbmc-library/__sort_of_CPROVER_round_to_integrall-01/test.desc

-8
This file was deleted.

Diff for: regression/smt2_solver/fp/roundToIntegral1.smt2

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
(set-logic FP)
2+
3+
(define-fun zero () (_ FloatingPoint 11 53) ((_ to_fp 11 53) RNE 0))
4+
(define-fun minus-zero () (_ FloatingPoint 11 53) (- zero))
5+
(define-fun one () (_ FloatingPoint 11 53) ((_ to_fp 11 53) RNE 1))
6+
(define-fun minus-one () (_ FloatingPoint 11 53) (- one))
7+
(define-fun zero-point-one () (_ FloatingPoint 11 53) ((_ to_fp 11 53) RNE 0.1))
8+
(define-fun minus-zero-point-one () (_ FloatingPoint 11 53) (- zero-point-one))
9+
(define-fun ten-point-one () (_ FloatingPoint 11 53) ((_ to_fp 11 53) RNE 10.1))
10+
(define-fun minus-ten-point-one () (_ FloatingPoint 11 53) (- ten-point-one))
11+
(define-fun ten () (_ FloatingPoint 11 53) ((_ to_fp 11 53) RNE 10))
12+
(define-fun minus-ten () (_ FloatingPoint 11 53) (- ten))
13+
(define-fun eleven () (_ FloatingPoint 11 53) ((_ to_fp 11 53) RNE 11))
14+
(define-fun minus-eleven () (_ FloatingPoint 11 53) (- eleven))
15+
16+
(assert (not (and
17+
18+
; round up
19+
(= (fp.roundToIntegral RTP (_ NaN 11 53)) (_ NaN 11 53))
20+
(= (fp.roundToIntegral RTP (_ +oo 11 53)) (_ +oo 11 53))
21+
(= (fp.roundToIntegral RTP (_ -oo 11 53)) (_ -oo 11 53))
22+
(= (fp.roundToIntegral RTP zero) zero)
23+
(= (fp.roundToIntegral RTP minus-zero) minus-zero)
24+
(= (fp.roundToIntegral RTP one) one)
25+
(= (fp.roundToIntegral RTP zero-point-one) one)
26+
(= (fp.roundToIntegral RTP minus-zero-point-one) minus-zero)
27+
(= (fp.roundToIntegral RTP ten-point-one) eleven)
28+
(= (fp.roundToIntegral RTP minus-ten-point-one) minus-ten)
29+
;(= (fp.roundToIntegral RTP ((_ to_fp 11 53) RTN 0x1.0p+52)) ((_ to_fp 11 53) RTN 0x1.0p+52))
30+
;(= (fp.roundToIntegral RTP dmax) dmax)
31+
32+
; round down
33+
(= (fp.roundToIntegral RTN (_ NaN 11 53)) (_ NaN 11 53))
34+
(= (fp.roundToIntegral RTN (_ +oo 11 53)) (_ +oo 11 53))
35+
(= (fp.roundToIntegral RTN (_ -oo 11 53)) (_ -oo 11 53))
36+
(= (fp.roundToIntegral RTN zero) zero)
37+
(= (fp.roundToIntegral RTN minus-zero) minus-zero)
38+
(= (fp.roundToIntegral RTN one) one)
39+
(= (fp.roundToIntegral RTN zero-point-one) zero)
40+
(= (fp.roundToIntegral RTN minus-zero-point-one) minus-one)
41+
(= (fp.roundToIntegral RTN ten-point-one) ten)
42+
(= (fp.roundToIntegral RTN minus-ten-point-one) minus-eleven)
43+
;(= (fp.roundToIntegral RTN ((_ to_fp 11 53) RTN 0x1.0p+52)) ((_ to_fp 11 53) RTN 0x1.0p+52))
44+
;(= (fp.roundToIntegral RTN dmax)) dmax)
45+
46+
; round to nearest ties to even
47+
(= (fp.roundToIntegral RNE (_ NaN 11 53)) (_ NaN 11 53))
48+
(= (fp.roundToIntegral RNE (_ +oo 11 53)) (_ +oo 11 53))
49+
(= (fp.roundToIntegral RNE (_ -oo 11 53)) (_ -oo 11 53))
50+
(= (fp.roundToIntegral RNE zero) zero)
51+
(= (fp.roundToIntegral RNE minus-zero) minus-zero)
52+
(= (fp.roundToIntegral RNE one) one)
53+
(= (fp.roundToIntegral RNE zero-point-one) zero)
54+
(= (fp.roundToIntegral RNE minus-zero-point-one) minus-zero)
55+
(= (fp.roundToIntegral RNE ten-point-one) ten)
56+
(= (fp.roundToIntegral RNE minus-ten-point-one) minus-ten)
57+
;(= (fp.roundToIntegral RNE ((_ to_fp 11 53) RTN 0x1.0p+52)) ((_ to_fp 11 53) RTN 0x1.0p+52))
58+
;(= (fp.roundToIntegral RNE dmax)) dmax)
59+
60+
; round to zero
61+
(= (fp.roundToIntegral RTZ (_ NaN 11 53)) (_ NaN 11 53))
62+
(= (fp.roundToIntegral RTZ (_ +oo 11 53)) (_ +oo 11 53))
63+
(= (fp.roundToIntegral RTZ (_ -oo 11 53)) (_ -oo 11 53))
64+
(= (fp.roundToIntegral RTZ zero) zero)
65+
(= (fp.roundToIntegral RTZ minus-zero) minus-zero)
66+
(= (fp.roundToIntegral RTZ one) one)
67+
(= (fp.roundToIntegral RTZ zero-point-one) zero)
68+
(= (fp.roundToIntegral RTZ minus-zero-point-one) minus-zero)
69+
(= (fp.roundToIntegral RTZ ten-point-one) ten)
70+
(= (fp.roundToIntegral RTZ minus-ten-point-one) minus-ten)
71+
;(= (fp.roundToIntegral RTZ ((_ to_fp 11 53) RTN 0x1.0p+52)) ((_ to_fp 11 53) RTN 0x1.0p+52))
72+
;(= (fp.roundToIntegral RTZ dmax)) dmax)
73+
)))
74+
75+
; should be unsat
76+
(check-sat)

Diff for: src/ansi-c/c_typecheck_expr.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -3237,6 +3237,24 @@ exprt c_typecheck_baset::do_special_functions(
32373237

32383238
return std::move(infl_expr);
32393239
}
3240+
else if(
3241+
identifier == CPROVER_PREFIX "round_to_integralf" ||
3242+
identifier == CPROVER_PREFIX "round_to_integrald" ||
3243+
identifier == CPROVER_PREFIX "round_to_integralld")
3244+
{
3245+
if(expr.arguments().size() != 2)
3246+
{
3247+
error().source_location = f_op.source_location();
3248+
error() << identifier << " expects two arguments" << eom;
3249+
throw 0;
3250+
}
3251+
3252+
auto round_to_integral_expr =
3253+
floatbv_round_to_integral_exprt{expr.arguments()[0], expr.arguments()[1]};
3254+
round_to_integral_expr.add_source_location() = source_location;
3255+
3256+
return std::move(round_to_integral_expr);
3257+
}
32403258
else if(
32413259
identifier == CPROVER_PREFIX "abs" || identifier == CPROVER_PREFIX "labs" ||
32423260
identifier == CPROVER_PREFIX "llabs" ||

Diff for: src/ansi-c/cprover_builtin_headers.h

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ int __CPROVER_islessgreaterf(float f, float g);
9393
int __CPROVER_islessgreaterd(double f, double g);
9494
int __CPROVER_isunorderedf(float f, float g);
9595
int __CPROVER_isunorderedd(double f, double g);
96+
float __CPROVER_round_to_integralf(float, int);
97+
double __CPROVER_round_to_integrald(double, int);
98+
long double __CPROVER_round_to_integralld(long double, int);
9699

97100
// absolute value
98101
int __CPROVER_abs(int x);

0 commit comments

Comments
 (0)