Skip to content

Commit f0ec760

Browse files
author
Daniel Kroening
authored
Merge pull request #3106 from diffblue/hex-bitvectors7
boolbvt::convert_constant is now independent of bitvector representation
2 parents 2dc9574 + 2313911 commit f0ec760

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

src/solvers/flattening/boolbv_constant.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

9+
#include <util/arith_tools.h>
910

1011
#include "boolbv.h"
1112

@@ -78,9 +79,9 @@ bvt boolbvt::convert_constant(const constant_exprt &expr)
7879
expr_type.id()==ID_c_bit_field ||
7980
expr_type.id()==ID_incomplete_c_enum)
8081
{
81-
const std::string &binary=id2string(expr.get_value());
82+
const auto &value = expr.get_value();
8283

83-
if(binary.size()!=width)
84+
if(value.size() != width)
8485
{
8586
error().source_location=expr.find_source_location();
8687
error() << "wrong value length in constant: "
@@ -90,7 +91,7 @@ bvt boolbvt::convert_constant(const constant_exprt &expr)
9091

9192
for(std::size_t i=0; i<width; i++)
9293
{
93-
bool bit=(binary[binary.size()-i-1]=='1');
94+
const bool bit = get_bitvector_bit(value, i);
9495
bv[i]=const_literal(bit);
9596
}
9697

src/util/arith_tools.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,14 @@ void mp_max(mp_integer &a, const mp_integer &b)
262262
if(b>a)
263263
a=b;
264264
}
265+
266+
/// Get a bit with given index from bit-vector representation.
267+
/// \param src: the bitvector representation
268+
/// \param bit_index: index (0 is the least significant)
269+
bool get_bitvector_bit(const irep_idt &src, std::size_t bit_index)
270+
{
271+
// The representation is binary, using '0'/'1',
272+
// most significant bit first.
273+
PRECONDITION(bit_index < src.size());
274+
return src[src.size() - 1 - bit_index] == '1';
275+
}

src/util/arith_tools.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,6 @@ mp_integer power(const mp_integer &base, const mp_integer &exponent);
164164
void mp_min(mp_integer &a, const mp_integer &b);
165165
void mp_max(mp_integer &a, const mp_integer &b);
166166

167+
bool get_bitvector_bit(const irep_idt &src, std::size_t bit_index);
168+
167169
#endif // CPROVER_UTIL_ARITH_TOOLS_H

src/util/simplify_expr.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,22 @@ bool simplify_exprt::simplify_popcount(popcount_exprt &expr)
135135

136136
if(op.is_constant())
137137
{
138-
const typet &type=ns.follow(op.type());
138+
const typet &op_type = op.type();
139139

140-
if(type.id()==ID_signedbv ||
141-
type.id()==ID_unsignedbv)
140+
if(op_type.id() == ID_signedbv || op_type.id() == ID_unsignedbv)
142141
{
143-
mp_integer value;
144-
if(!to_integer(op, value))
145-
{
146-
std::size_t result;
142+
const auto width = to_bitvector_type(op_type).get_width();
143+
const auto &value = to_constant_expr(op).get_value();
144+
std::size_t result = 0;
147145

148-
for(result=0; value!=0; value=value>>1)
149-
if(value.is_odd())
150-
result++;
146+
for(std::size_t i = 0; i < width; i++)
147+
if(get_bitvector_bit(value, i))
148+
result++;
151149

152-
exprt simp_result = from_integer(result, expr.type());
153-
expr.swap(simp_result);
150+
auto result_expr = from_integer(result, expr.type());
151+
expr.swap(result_expr);
154152

155-
return false;
156-
}
153+
return false;
157154
}
158155
}
159156

0 commit comments

Comments
 (0)