Skip to content

Commit 393c56d

Browse files
authored
Merge pull request #8998 from diffblue/feature/1bit-signed-bv-compare
Support 1-bit signed bitvectors in `bv_utilst::lt_or_le`
2 parents b875a22 + 648e9ae commit 393c56d

3 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎src/solvers/flattening/bv_utils.cpp‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,7 @@ literalt bv_utilst::lt_or_le(
14091409
representationt rep)
14101410
{
14111411
PRECONDITION(bv0.size() == bv1.size());
1412+
PRECONDITION(!bv0.empty());
14121413

14131414
#ifdef COMPACT_LT_OR_LE
14141415
if(prop.has_set_to() && prop.cnf_handled_well())
@@ -1422,8 +1423,18 @@ literalt bv_utilst::lt_or_le(
14221423
else if(top0.is_true() && top1.is_false())
14231424
return const_literal(true);
14241425

1426+
// 1-bit signed: the sign bit is the only bit
1427+
if(bv0.size() == 1)
1428+
{
1429+
if(or_equal)
1430+
return prop.lor(top0, !top1);
1431+
else
1432+
return prop.land(top0, !top1);
1433+
}
1434+
14251435
INVARIANT(
1426-
bv0.size() >= 2, "signed bitvectors should have at least two bits");
1436+
bv0.size() >= 2,
1437+
"the compact signed comparison encoding requires at least two bits");
14271438
// 1 if a compare is needed below this bit
14281439
bvt compareBelow = prop.new_variables(bv0.size() - 1);
14291440
size_t start = compareBelow.size() - 1;

‎unit/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ SRC += analyses/ai/ai.cpp \
104104
solvers/flattening/boolbv_enumeration.cpp \
105105
solvers/flattening/boolbv_onehot.cpp \
106106
solvers/flattening/boolbv_update_bit.cpp \
107+
solvers/flattening/bv_utils.cpp \
107108
solvers/floatbv/float_utils.cpp \
108109
solvers/prop/bdd_expr.cpp \
109110
solvers/sat/external_sat.cpp \
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for bv_utilst
4+
5+
Author: Daniel Kroening
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Unit tests for bv_utilst
11+
12+
#include <util/arith_tools.h>
13+
#include <util/bitvector_types.h>
14+
#include <util/cout_message.h>
15+
#include <util/namespace.h>
16+
#include <util/std_expr.h>
17+
#include <util/symbol_table.h>
18+
19+
#include <solvers/flattening/boolbv.h>
20+
#include <solvers/sat/satcheck.h>
21+
#include <testing-utils/use_catch.h>
22+
23+
SCENARIO("1-bit signed less-than", "[core][solvers][flattening][bv_utils]")
24+
{
25+
console_message_handlert message_handler;
26+
message_handler.set_verbosity(0);
27+
28+
GIVEN("Two 1-bit signed bitvector symbols")
29+
{
30+
satcheckt satcheck(message_handler);
31+
symbol_tablet symbol_table;
32+
namespacet ns(symbol_table);
33+
boolbvt boolbv(ns, satcheck, message_handler);
34+
35+
signedbv_typet s1(1);
36+
auto x = symbol_exprt("x", s1);
37+
auto y = symbol_exprt("y", s1);
38+
39+
THEN("-1 < 0 is satisfiable")
40+
{
41+
// x = -1, y = 0, x < y
42+
boolbv << equal_exprt(x, from_integer(-1, s1));
43+
boolbv << equal_exprt(y, from_integer(0, s1));
44+
boolbv << less_than_exprt(x, y);
45+
REQUIRE(boolbv() == decision_proceduret::resultt::D_SATISFIABLE);
46+
}
47+
48+
THEN("0 < -1 is unsatisfiable")
49+
{
50+
boolbv << equal_exprt(x, from_integer(0, s1));
51+
boolbv << equal_exprt(y, from_integer(-1, s1));
52+
boolbv << less_than_exprt(x, y);
53+
REQUIRE(boolbv() == decision_proceduret::resultt::D_UNSATISFIABLE);
54+
}
55+
56+
THEN("-1 <= -1 is satisfiable")
57+
{
58+
boolbv << equal_exprt(x, from_integer(-1, s1));
59+
boolbv << equal_exprt(y, from_integer(-1, s1));
60+
boolbv << less_than_or_equal_exprt(x, y);
61+
REQUIRE(boolbv() == decision_proceduret::resultt::D_SATISFIABLE);
62+
}
63+
64+
THEN("-1 < -1 is unsatisfiable")
65+
{
66+
boolbv << equal_exprt(x, from_integer(-1, s1));
67+
boolbv << equal_exprt(y, from_integer(-1, s1));
68+
boolbv << less_than_exprt(x, y);
69+
REQUIRE(boolbv() == decision_proceduret::resultt::D_UNSATISFIABLE);
70+
}
71+
72+
THEN("x < y is satisfiable for symbolic 1-bit signed values")
73+
{
74+
boolbv << less_than_exprt(x, y);
75+
REQUIRE(boolbv() == decision_proceduret::resultt::D_SATISFIABLE);
76+
}
77+
78+
THEN("x <= y is satisfiable for symbolic 1-bit signed values")
79+
{
80+
boolbv << less_than_or_equal_exprt(x, y);
81+
REQUIRE(boolbv() == decision_proceduret::resultt::D_SATISFIABLE);
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)