Skip to content

Commit 565a92b

Browse files
authored
Merge pull request #3196 from diffblue/get_bitvector_bit_width
get_bitvector_bit is now told the width
2 parents 87dfaeb + 9f7c072 commit 565a92b

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

src/solvers/flattening/boolbv_constant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ bvt boolbvt::convert_constant(const constant_exprt &expr)
9191

9292
for(std::size_t i=0; i<width; i++)
9393
{
94-
const bool bit = get_bitvector_bit(value, i);
94+
const bool bit = get_bitvector_bit(value, width, i);
9595
bv[i]=const_literal(bit);
9696
}
9797

src/util/arith_tools.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,17 @@ void mp_max(mp_integer &a, const mp_integer &b)
265265

266266
/// Get a bit with given index from bit-vector representation.
267267
/// \param src: the bitvector representation
268+
/// \param width: the number of bits in the bitvector
268269
/// \param bit_index: index (0 is the least significant)
269-
bool get_bitvector_bit(const irep_idt &src, std::size_t bit_index)
270+
bool get_bitvector_bit(
271+
const irep_idt &src,
272+
std::size_t width,
273+
std::size_t bit_index)
270274
{
271275
// The representation is binary, using '0'/'1',
272276
// most significant bit first.
273-
PRECONDITION(bit_index < src.size());
277+
PRECONDITION(bit_index < width);
278+
PRECONDITION(src.size() == width);
274279
return src[src.size() - 1 - bit_index] == '1';
275280
}
276281

@@ -302,8 +307,8 @@ irep_idt bitvector_bitwise_op(
302307
const std::size_t width,
303308
const std::function<bool(bool, bool)> f)
304309
{
305-
return make_bvrep(width, [&a, &b, f](std::size_t i) {
306-
return f(get_bitvector_bit(a, i), get_bitvector_bit(b, i));
310+
return make_bvrep(width, [&a, &b, &width, f](std::size_t i) {
311+
return f(get_bitvector_bit(a, width, i), get_bitvector_bit(b, width, i));
307312
});
308313
}
309314

@@ -318,6 +323,7 @@ irep_idt bitvector_bitwise_op(
318323
const std::size_t width,
319324
const std::function<bool(bool)> f)
320325
{
321-
return make_bvrep(
322-
width, [&a, f](std::size_t i) { return f(get_bitvector_bit(a, i)); });
326+
return make_bvrep(width, [&a, &width, f](std::size_t i) {
327+
return f(get_bitvector_bit(a, width, i));
328+
});
323329
}

src/util/arith_tools.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ 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);
167+
bool get_bitvector_bit(
168+
const irep_idt &src,
169+
std::size_t width,
170+
std::size_t bit_index);
168171

169172
irep_idt
170173
make_bvrep(const std::size_t width, const std::function<bool(std::size_t)> f);

src/util/simplify_expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ bool simplify_exprt::simplify_popcount(popcount_exprt &expr)
144144
std::size_t result = 0;
145145

146146
for(std::size_t i = 0; i < width; i++)
147-
if(get_bitvector_bit(value, i))
147+
if(get_bitvector_bit(value, width, i))
148148
result++;
149149

150150
auto result_expr = from_integer(result, expr.type());

src/util/simplify_expr_int.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,10 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
744744
UNREACHABLE;
745745

746746
const irep_idt new_value =
747-
make_bvrep(width, [&a_val, &b_val, &f](std::size_t i) {
748-
return f(get_bitvector_bit(a_val, i), get_bitvector_bit(b_val, i));
747+
make_bvrep(width, [&a_val, &b_val, &width, &f](std::size_t i) {
748+
return f(
749+
get_bitvector_bit(a_val, width, i),
750+
get_bitvector_bit(b_val, width, i));
749751
});
750752

751753
constant_exprt new_op(new_value, expr.type());
@@ -1278,9 +1280,10 @@ bool simplify_exprt::simplify_bitnot(exprt &expr)
12781280
if(op.id()==ID_constant)
12791281
{
12801282
const auto &value = to_constant_expr(op).get_value();
1281-
const auto new_value = make_bvrep(width, [&value](std::size_t i) {
1282-
return !get_bitvector_bit(value, i);
1283-
});
1283+
const auto new_value =
1284+
make_bvrep(width, [&value, &width](std::size_t i) {
1285+
return !get_bitvector_bit(value, width, i);
1286+
});
12841287
expr = constant_exprt(new_value, op.type());
12851288
return false;
12861289
}

0 commit comments

Comments
 (0)