Skip to content

Fix onehot0 flattening #8536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/solvers/flattening/boolbv_onehot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

bvt op=convert_bv(expr.op());

// onehot0 is the same as onehot with the input bits flipped

Check warning on line 18 in src/solvers/flattening/boolbv_onehot.cpp

View check run for this annotation

Codecov / codecov/patch

src/solvers/flattening/boolbv_onehot.cpp#L18

Added line #L18 was not covered by tests
if(expr.id() == ID_onehot0)
op = bv_utils.inverted(op);

Check warning on line 21 in src/solvers/flattening/boolbv_onehot.cpp

View check run for this annotation

Codecov / codecov/patch

src/solvers/flattening/boolbv_onehot.cpp#L21

Added line #L21 was not covered by tests
literalt one_seen=const_literal(false);
literalt more_than_one_seen=const_literal(false);

Expand All @@ -25,14 +29,5 @@
one_seen=prop.lor(*it, one_seen);
}

if(expr.id()==ID_onehot)
return prop.land(one_seen, !more_than_one_seen);
else
{
INVARIANT(
expr.id() == ID_onehot0,
"should be a onehot0 expression as other onehot expression kind has been "
"handled in other branch");
return !more_than_one_seen;
}
return prop.land(one_seen, !more_than_one_seen);
}
1 change: 1 addition & 0 deletions unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ SRC += analyses/ai/ai.cpp \
pointer-analysis/value_set.cpp \
solvers/bdd/miniBDD/miniBDD.cpp \
solvers/flattening/boolbv.cpp \
solvers/flattening/boolbv_onehot.cpp \
solvers/flattening/boolbv_update_bit.cpp \
solvers/floatbv/float_utils.cpp \
solvers/prop/bdd_expr.cpp \
Expand Down
92 changes: 92 additions & 0 deletions unit/solvers/flattening/boolbv_onehot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*******************************************************************\

Module: Unit tests for solvers/flattening/boolbv_onehot

Author: Daniel Kroening, [email protected]

\*******************************************************************/

/// \file

#include <util/arith_tools.h>
#include <util/bitvector_expr.h>
#include <util/bitvector_types.h>
#include <util/cout_message.h>
#include <util/namespace.h>
#include <util/std_expr.h>
#include <util/symbol_table.h>

#include <solvers/flattening/boolbv.h>
#include <solvers/sat/satcheck.h>
#include <testing-utils/use_catch.h>

TEST_CASE("onehot flattening", "[core][solvers][flattening][boolbvt][onehot]")
{
console_message_handlert message_handler;
message_handler.set_verbosity(0);
satcheckt satcheck{message_handler};
symbol_tablet symbol_table;
namespacet ns{symbol_table};
boolbvt boolbv{ns, satcheck, message_handler};
unsignedbv_typet u8{8};

GIVEN("A bit-vector that is one-hot")
{
boolbv << onehot_exprt{from_integer(64, u8)};

THEN("the lowering of onehot is true")
{
REQUIRE(boolbv() == decision_proceduret::resultt::D_SATISFIABLE);
}
}

GIVEN("A bit-vector that is not one-hot")
{
boolbv << onehot_exprt{from_integer(5, u8)};

THEN("the lowering of onehot is false")
{
REQUIRE(boolbv() == decision_proceduret::resultt::D_UNSATISFIABLE);
}
}

GIVEN("A bit-vector that is not one-hot")
{
boolbv << onehot_exprt{from_integer(0, u8)};

THEN("the lowering of onehot is false")
{
REQUIRE(boolbv() == decision_proceduret::resultt::D_UNSATISFIABLE);
}
}

GIVEN("A bit-vector that is one-hot 0")
{
boolbv << onehot0_exprt{from_integer(0xfe, u8)};

THEN("the lowering of onehot0 is true")
{
REQUIRE(boolbv() == decision_proceduret::resultt::D_SATISFIABLE);
}
}

GIVEN("A bit-vector that is not one-hot 0")
{
boolbv << onehot0_exprt{from_integer(0x7e, u8)};

THEN("the lowering of onehot0 is false")
{
REQUIRE(boolbv() == decision_proceduret::resultt::D_UNSATISFIABLE);
}
}

GIVEN("A bit-vector that is not one-hot 0")
{
boolbv << onehot0_exprt{from_integer(0xff, u8)};

THEN("the lowering of onehot0 is false")
{
REQUIRE(boolbv() == decision_proceduret::resultt::D_UNSATISFIABLE);
}
}
}
Loading