Skip to content

Commit b6c1334

Browse files
bit-vector overflow/underflow operators exposed over C++ API
Signed-off-by: Nikolaj Bjorner <[email protected]>
1 parent 27765ee commit b6c1334

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/api/c++/z3++.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,17 @@ namespace z3 {
11181118
friend expr min(expr const& a, expr const& b);
11191119
friend expr max(expr const& a, expr const& b);
11201120

1121+
friend expr bv2int(expr const& a, bool is_signed);
1122+
friend expr int2bv(expr const& a, unsigned n);
1123+
friend expr bvadd_no_overflow(expr const& a, expr const& b);
1124+
friend expr bvadd_no_underflow(expr const& a, expr const& b);
1125+
friend expr bvsub_no_overflow(expr const& a, expr const& b);
1126+
friend expr bvsub_no_underflow(expr const& a, expr const& b);
1127+
friend expr bvsdiv_no_overflow(expr const& a, expr const& b);
1128+
friend expr bvneg_no_overflow(expr const& a);
1129+
friend expr bvmul_no_overflow(expr const& a, expr const& b, bool is_signed);
1130+
friend expr bvmul_no_underflow(expr const& a, expr const& b);
1131+
11211132
expr rotate_left(unsigned i) { Z3_ast r = Z3_mk_rotate_left(ctx(), i, *this); ctx().check_error(); return expr(ctx(), r); }
11221133
expr rotate_right(unsigned i) { Z3_ast r = Z3_mk_rotate_right(ctx(), i, *this); ctx().check_error(); return expr(ctx(), r); }
11231134
expr repeat(unsigned i) { Z3_ast r = Z3_mk_repeat(ctx(), i, *this); ctx().check_error(); return expr(ctx(), r); }
@@ -1625,6 +1636,7 @@ namespace z3 {
16251636
return expr(a.ctx(), r);
16261637
}
16271638

1639+
16281640
/**
16291641
\brief Create the if-then-else expression <tt>ite(c, t, e)</tt>
16301642
@@ -1740,6 +1752,41 @@ namespace z3 {
17401752
*/
17411753
inline expr zext(expr const & a, unsigned i) { return to_expr(a.ctx(), Z3_mk_zero_ext(a.ctx(), i, a)); }
17421754

1755+
/**
1756+
\brief bit-vector and integer conversions.
1757+
*/
1758+
inline expr bv2int(expr const& a, bool is_signed) { Z3_ast r = Z3_mk_bv2int(a.ctx(), a, is_signed); a.check_error(); return expr(a.ctx(), r); }
1759+
inline expr int2bv(expr const& a, unsigned n) { Z3_ast r = Z3_mk_intbv2(a.ctx(), a, n); a.check_error(); return expr(a.ctx(), r); }
1760+
1761+
/**
1762+
\brief bit-vector overflow/underflow checks
1763+
*/
1764+
inline expr bvadd_no_overflow(expr const& a, expr const& b) {
1765+
check_context(a, b); Z3_ast r = Z3_mk_bvadd_no_overflow(a.ctx(), a, b); a.check_error(); return expr(a.ctx(), r);
1766+
}
1767+
inline expr bvadd_no_underflow(expr const& a, expr const& b) {
1768+
check_context(a, b); Z3_ast r = Z3_mk_bvadd_no_underflow(a.ctx(), a, b); a.check_error(); return expr(a.ctx(), r);
1769+
}
1770+
inline expr bvsub_no_overflow(expr const& a, expr const& b) {
1771+
check_context(a, b); Z3_ast r = Z3_mk_bvsub_no_overflow(a.ctx(), a, b); a.check_error(); return expr(a.ctx(), r);
1772+
}
1773+
inline expr bvsub_no_underflow(expr const& a, expr const& b) {
1774+
check_context(a, b); Z3_ast r = Z3_mk_bvsub_no_underflow(a.ctx(), a, b); a.check_error(); return expr(a.ctx(), r);
1775+
}
1776+
inline expr bvsdiv_no_overflow(expr const& a, expr const& b) {
1777+
check_context(a, b); Z3_ast r = Z3_mk_bvsdiv_no_overflow(a.ctx(), a, b); a.check_error(); return expr(a.ctx(), r);
1778+
}
1779+
inline expr bvneg_no_overflow(expr const& a) {
1780+
Z3_ast r = Z3_mk_bvneg_no_overflow(a.ctx(), a, b); a.check_error(); return expr(a.ctx(), r);
1781+
}
1782+
inline expr bvmul_no_overflow(expr const& a, expr const& b, bool is_signed) {
1783+
check_context(a, b); Z3_ast r = Z3_mk_bvmul_no_overflow(a.ctx(), a, b, is_signed); a.check_error(); return expr(a.ctx(), r);
1784+
}
1785+
inline expr bvmul_no_underflow(expr const& a, expr const& b) {
1786+
check_context(a, b); Z3_ast r = Z3_mk_bvmul_no_underflow(a.ctx(), a, b); a.check_error(); return expr(a.ctx(), r);
1787+
}
1788+
1789+
17431790
/**
17441791
\brief Sign-extend of the given bit-vector to the (signed) equivalent bitvector of size m+i, where m is the size of the given bit-vector.
17451792
*/

src/api/z3_api.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,6 +2989,7 @@ extern "C" {
29892989
of \c t1 and \c t2 does not overflow.
29902990
29912991
The nodes \c t1 and \c t2 must have the same bit-vector sort.
2992+
The returned node is of sort Bool.
29922993
29932994
def_API('Z3_mk_bvadd_no_overflow', AST, (_in(CONTEXT), _in(AST), _in(AST), _in(BOOL)))
29942995
*/
@@ -2999,6 +3000,7 @@ extern "C" {
29993000
of \c t1 and \c t2 does not underflow.
30003001
30013002
The nodes \c t1 and \c t2 must have the same bit-vector sort.
3003+
The returned node is of sort Bool.
30023004
30033005
def_API('Z3_mk_bvadd_no_underflow', AST, (_in(CONTEXT), _in(AST), _in(AST)))
30043006
*/
@@ -3009,6 +3011,7 @@ extern "C" {
30093011
of \c t1 and \c t2 does not overflow.
30103012
30113013
The nodes \c t1 and \c t2 must have the same bit-vector sort.
3014+
The returned node is of sort Bool.
30123015
30133016
def_API('Z3_mk_bvsub_no_overflow', AST, (_in(CONTEXT), _in(AST), _in(AST)))
30143017
*/
@@ -3019,6 +3022,7 @@ extern "C" {
30193022
of \c t1 and \c t2 does not underflow.
30203023
30213024
The nodes \c t1 and \c t2 must have the same bit-vector sort.
3025+
The returned node is of sort Bool.
30223026
30233027
def_API('Z3_mk_bvsub_no_underflow', AST, (_in(CONTEXT), _in(AST), _in(AST), _in(BOOL)))
30243028
*/
@@ -3029,6 +3033,7 @@ extern "C" {
30293033
of \c t1 and \c t2 does not overflow.
30303034
30313035
The nodes \c t1 and \c t2 must have the same bit-vector sort.
3036+
The returned node is of sort Bool.
30323037
30333038
def_API('Z3_mk_bvsdiv_no_overflow', AST, (_in(CONTEXT), _in(AST), _in(AST)))
30343039
*/
@@ -3039,6 +3044,7 @@ extern "C" {
30393044
\c t1 is interpreted as a signed bit-vector.
30403045
30413046
The node \c t1 must have bit-vector sort.
3047+
The returned node is of sort Bool.
30423048
30433049
def_API('Z3_mk_bvneg_no_overflow', AST, (_in(CONTEXT), _in(AST)))
30443050
*/
@@ -3049,6 +3055,7 @@ extern "C" {
30493055
of \c t1 and \c t2 does not overflow.
30503056
30513057
The nodes \c t1 and \c t2 must have the same bit-vector sort.
3058+
The returned node is of sort Bool.
30523059
30533060
def_API('Z3_mk_bvmul_no_overflow', AST, (_in(CONTEXT), _in(AST), _in(AST), _in(BOOL)))
30543061
*/
@@ -3059,6 +3066,7 @@ extern "C" {
30593066
of \c t1 and \c t2 does not underflow.
30603067
30613068
The nodes \c t1 and \c t2 must have the same bit-vector sort.
3069+
The returned node is of sort Bool.
30623070
30633071
def_API('Z3_mk_bvmul_no_underflow', AST, (_in(CONTEXT), _in(AST), _in(AST)))
30643072
*/

0 commit comments

Comments
 (0)