Skip to content

Commit 716103e

Browse files
committed
Implement popcount in SAT back-end
Previously only constants were supported via expression simplification. The implementation uses lowering to supported expressions and could thus equally be used in other back-ends.
1 parent ddde9dc commit 716103e

File tree

8 files changed

+189
-0
lines changed

8 files changed

+189
-0
lines changed

regression/cbmc/gcc_popcount1/main.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <assert.h>
2+
3+
#ifndef __GNUC__
4+
int __builtin_popcount(unsigned int);
5+
int __builtin_popcountl(unsigned long);
6+
int __builtin_popcountll(unsigned long long);
7+
#endif
8+
9+
unsigned int __VERIFIER_nondet_unsigned();
10+
unsigned long __VERIFIER_nondet_unsigned_long();
11+
unsigned long long __VERIFIER_nondet_unsigned_long_long();
12+
13+
// Hacker's Delight
14+
// http://www.hackersdelight.org/permissions.htm
15+
int pop4(unsigned long long x)
16+
{
17+
int n = 0;
18+
19+
// variant with additional bounding to make sure symbolic execution always
20+
// terminates without having to specify an unwinding bound
21+
for(int i = 0; x != 0 && i < sizeof(x) * 8; ++i)
22+
{
23+
++n;
24+
x = x & (x - 1);
25+
}
26+
27+
return n;
28+
}
29+
30+
int main()
31+
{
32+
assert(pop4(42) == 3);
33+
assert(__builtin_popcount(42) == 3);
34+
assert(__builtin_popcountl(42) == 3);
35+
assert(__builtin_popcountll(42) == 3);
36+
37+
unsigned int u = __VERIFIER_nondet_unsigned();
38+
assert(pop4(u) == __builtin_popcount(u));
39+
40+
return 0;
41+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

regression/cbmc/gcc_popcount2/main.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <assert.h>
2+
3+
#ifndef __GNUC__
4+
int __builtin_popcount(unsigned int);
5+
int __builtin_popcountl(unsigned long);
6+
int __builtin_popcountll(unsigned long long);
7+
#endif
8+
9+
unsigned int __VERIFIER_nondet_unsigned();
10+
unsigned long __VERIFIER_nondet_unsigned_long();
11+
unsigned long long __VERIFIER_nondet_unsigned_long_long();
12+
13+
// Hacker's Delight
14+
// http://www.hackersdelight.org/permissions.htm
15+
int pop4(unsigned long long x)
16+
{
17+
int n = 0;
18+
19+
// variant with additional bounding to make sure symbolic execution always
20+
// terminates without having to specify an unwinding bound
21+
for(int i = 0; x != 0 && i < sizeof(x) * 8; ++i)
22+
{
23+
++n;
24+
x = x & (x - 1);
25+
}
26+
27+
return n;
28+
}
29+
30+
int main()
31+
{
32+
unsigned long ul = __VERIFIER_nondet_unsigned_long();
33+
assert(pop4(ul) == __builtin_popcountl(ul));
34+
35+
unsigned long long ull = __VERIFIER_nondet_unsigned_long_long();
36+
assert(pop4(ull) == __builtin_popcountll(ull));
37+
38+
// expected to fail as bits may have been cut off
39+
assert(
40+
sizeof(ull) != sizeof(unsigned int) &&
41+
pop4(ull) == __builtin_popcount(ull));
42+
43+
return 0;
44+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
THOROUGH
2+
main.c
3+
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
\[main\.assertion\.1\] assertion sizeof\(ull\) != sizeof\(unsigned int\) && pop4\(ull\) == __builtin_popcount\(ull\): FAILURE$
8+
^\*\* 1 of 3 failed
9+
--
10+
^warning: ignoring

src/solvers/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ SRC = $(BOOLEFORCE_SRC) \
157157
floatbv/float_bv.cpp \
158158
floatbv/float_utils.cpp \
159159
floatbv/float_approximation.cpp \
160+
lowering/popcount.cpp \
160161
miniBDD/miniBDD.cpp \
161162
prop/aig.cpp \
162163
prop/aig_prop.cpp \

src/solvers/flattening/boolbv.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Author: Daniel Kroening, [email protected]
2727
#include "boolbv_type.h"
2828

2929
#include "../floatbv/float_utils.h"
30+
#include "../lowering/expr_lowering.h"
3031

3132
bool boolbvt::literal(
3233
const exprt &expr,
@@ -305,6 +306,8 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
305306
float_utils.debug2(bv0, bv1);
306307
return bv;
307308
}
309+
else if(expr.id() == ID_popcount)
310+
return convert_bv(lower_popcount(to_popcount_expr(expr), ns));
308311

309312
return conversion_failed(expr);
310313
}

src/solvers/lowering/expr_lowering.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*******************************************************************\
2+
3+
Module: Lower expressions to arithmetic and logic expressions
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_SOLVERS_LOWERING_EXPR_LOWERING_H
10+
#define CPROVER_SOLVERS_LOWERING_EXPR_LOWERING_H
11+
12+
#include <util/expr.h>
13+
14+
class namespacet;
15+
class popcount_exprt;
16+
17+
/// Lower a popcount_exprt to arithmetic and logic expressions
18+
/// \param expr Input expression to be translated
19+
/// \param ns Namespace for type lookups
20+
/// \return Semantically equivalent expression
21+
exprt lower_popcount(const popcount_exprt &expr, const namespacet &ns);
22+
23+
#endif /* CPROVER_SOLVERS_LOWERING_EXPR_LOWERING_H */

src/solvers/lowering/popcount.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*******************************************************************\
2+
3+
Module: Lowering of popcount
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
#include "expr_lowering.h"
10+
11+
#include <util/arith_tools.h>
12+
#include <util/invariant.h>
13+
#include <util/pointer_offset_size.h>
14+
#include <util/std_expr.h>
15+
16+
exprt lower_popcount(const popcount_exprt &expr, const namespacet &ns)
17+
{
18+
// Hacker's Delight, variant pop0:
19+
// x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
20+
// x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
21+
// x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
22+
// x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
23+
// etc.
24+
// return x;
25+
// http://www.hackersdelight.org/permissions.htm
26+
27+
// make sure the operand width is a power of two
28+
exprt x = expr.op();
29+
const mp_integer x_width = pointer_offset_bits(x.type(), ns);
30+
CHECK_RETURN(x_width > 0);
31+
const std::size_t bits = address_bits(x_width);
32+
const std::size_t new_width = integer2size_t(power(2, bits));
33+
const bool need_typecast =
34+
new_width > x_width || x.type().id() != ID_unsignedbv;
35+
if(need_typecast)
36+
x.make_typecast(unsignedbv_typet(new_width));
37+
38+
// repeatedly compute x = (x & bitmask) + ((x >> shift) & bitmask)
39+
for(std::size_t shift = 1; shift < new_width; shift <<= 1)
40+
{
41+
// x >> shift
42+
lshr_exprt shifted_x(
43+
x, from_integer(shift, unsignedbv_typet(address_bits(shift) + 1)));
44+
// bitmask is a string of alternating shift-many bits starting from lsb set
45+
// to 1
46+
std::string bitstring;
47+
bitstring.reserve(new_width);
48+
for(std::size_t i = 0; i < new_width / (2 * shift); ++i)
49+
bitstring += std::string(shift, '0') + std::string(shift, '1');
50+
constant_exprt bitmask(bitstring, x.type());
51+
// build the expression
52+
x = plus_exprt(bitand_exprt(x, bitmask), bitand_exprt(shifted_x, bitmask));
53+
}
54+
55+
// the result is restricted to the result type
56+
x.make_typecast(expr.type());
57+
58+
return x;
59+
}

0 commit comments

Comments
 (0)