cprover
popcount.cpp
Go to the documentation of this file.
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>
14 #include <util/std_expr.h>
15 
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 auto x_width = pointer_offset_bits(x.type(), ns);
30  CHECK_RETURN(x_width.has_value() && *x_width >= 1);
31  const std::size_t bits = address_bits(*x_width);
32  const std::size_t new_width = numeric_cast_v<std::size_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  const mp_integer value = binary2integer(bitstring, false);
51  const constant_exprt bitmask(integer2bvrep(value, new_width), x.type());
52  // build the expression
53  x = plus_exprt(bitand_exprt(x, bitmask), bitand_exprt(shifted_x, bitmask));
54  }
55 
56  // the result is restricted to the result type
57  x.make_typecast(expr.type());
58 
59  return x;
60 }
pointer_offset_size.h
arith_tools.h
mp_integer
BigInt mp_integer
Definition: mp_arith.h:22
plus_exprt
The plus expression Associativity is not specified.
Definition: std_expr.h:1042
exprt
Base class for all expressions.
Definition: expr.h:54
invariant.h
lshr_exprt
Logical right shift.
Definition: std_expr.h:2984
unsignedbv_typet
Fixed-width bit-vector with unsigned binary interpretation.
Definition: std_types.h:1223
expr_lowering.h
address_bits
std::size_t address_bits(const mp_integer &size)
ceil(log2(size))
Definition: arith_tools.cpp:200
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:93
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:68
integer2bvrep
irep_idt integer2bvrep(const mp_integer &src, std::size_t width)
convert an integer to bit-vector representation with given width This uses two's complement for negat...
Definition: arith_tools.cpp:403
pointer_offset_bits
optionalt< mp_integer > pointer_offset_bits(const typet &type, const namespacet &ns)
Definition: pointer_offset_size.cpp:101
irept::id
const irep_idt & id() const
Definition: irep.h:259
unary_exprt::op
const exprt & op() const
Definition: std_expr.h:371
bitand_exprt
Bit-wise AND.
Definition: std_expr.h:2811
lower_popcount
exprt lower_popcount(const popcount_exprt &expr, const namespacet &ns)
Lower a popcount_exprt to arithmetic and logic expressions.
Definition: popcount.cpp:16
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:123
binary2integer
const mp_integer binary2integer(const std::string &n, bool is_signed)
convert binary string representation to mp_integer
Definition: mp_arith.cpp:120
power
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.
Definition: arith_tools.cpp:218
popcount_exprt
The popcount (counting the number of bits set to 1) expression.
Definition: std_expr.h:4815
exprt::make_typecast
void make_typecast(const typet &_type)
Create a typecast_exprt to the given type.
Definition: expr.cpp:74
constant_exprt
A constant literal expression.
Definition: std_expr.h:4384
std_expr.h
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:470