Skip to content

Commit 855b35e

Browse files
author
Daniel Kroening
committed
added means to perform bit-wise operations on the bit-vector representation
1 parent 95fe44b commit 855b35e

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

src/util/arith_tools.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,46 @@ void mp_max(mp_integer &a, const mp_integer &b)
262262
if(b>a)
263263
a=b;
264264
}
265+
266+
/// perform a binary bit-wise operation, given as a functor,
267+
/// on a bit-vector representation
268+
/// \param a: the representation of the first bit vector
269+
/// \param b: the representation of the second bit vector
270+
/// \param width: the width of the bit-vector
271+
/// \param f: the functor
272+
/// \returns new bitvector representation
273+
irep_idt bitvector_bitwise_op(
274+
const irep_idt &a,
275+
const irep_idt &b,
276+
const std::size_t width,
277+
const std::function<bool(bool, bool)> f)
278+
{
279+
PRECONDITION(a.size() == width);
280+
PRECONDITION(b.size() == width);
281+
282+
std::string result(width, ' ');
283+
284+
for(std::size_t i = 0; i < width; i++)
285+
{
286+
const bool bit_a = a[i] == '1';
287+
const bool bit_b = b[i] == '1';
288+
const bool bit_result = f(bit_a, bit_b);
289+
result[i] = bit_result ? '1' : '0';
290+
}
291+
292+
return result;
293+
}
294+
295+
/// perform a unary bit-wise operation, given as a functor,
296+
/// on a bit-vector representation
297+
/// \param a: the bit-vector representation
298+
/// \param width: the width of the bit-vector
299+
/// \param f: the functor
300+
/// \returns new bitvector representation
301+
irep_idt bitvector_bitwise_op(
302+
const irep_idt &a,
303+
const std::size_t width,
304+
const std::function<bool(bool)> f)
305+
{
306+
return bitvector_bitwise_op(a, a, width, [f](bool a, bool) { return f(a); });
307+
}

src/util/arith_tools.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,15 @@ 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+
irep_idt bitvector_bitwise_op(
168+
const irep_idt &,
169+
const irep_idt &,
170+
const std::size_t width,
171+
std::function<bool(bool, bool)>);
172+
173+
irep_idt bitvector_bitwise_op(
174+
const irep_idt &,
175+
const std::size_t width,
176+
std::function<bool(bool)>);
177+
167178
#endif // CPROVER_UTIL_ARITH_TOOLS_H

src/util/simplify_expr_int.cpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -713,13 +713,10 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
713713

714714
// try to merge constants
715715

716-
std::size_t width=to_bitvector_type(expr.type()).get_width();
716+
const std::size_t width = to_bitvector_type(expr.type()).get_width();
717717

718718
while(expr.operands().size()>=2)
719719
{
720-
const irep_idt &a_str=expr.op0().get(ID_value);
721-
const irep_idt &b_str=expr.op1().get(ID_value);
722-
723720
if(!expr.op0().is_constant())
724721
break;
725722

@@ -732,30 +729,21 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
732729
if(expr.op1().type()!=expr.type())
733730
break;
734731

735-
INVARIANT(
736-
a_str.size() == b_str.size(),
737-
"bitvectors of the same type have the same size");
732+
const auto &a_val = to_constant_expr(expr.op0()).get_value();
733+
const auto &b_val = to_constant_expr(expr.op1()).get_value();
738734

739-
std::string new_value;
740-
new_value.resize(width);
735+
std::function<bool(bool, bool)> f;
741736

742737
if(expr.id()==ID_bitand)
743-
{
744-
for(std::size_t i=0; i<width; i++)
745-
new_value[i]=(a_str[i]=='1' && b_str[i]=='1')?'1':'0';
746-
}
738+
f = [](bool a, bool b) { return a & b; };
747739
else if(expr.id()==ID_bitor)
748-
{
749-
for(std::size_t i=0; i<width; i++)
750-
new_value[i]=(a_str[i]=='1' || b_str[i]=='1')?'1':'0';
751-
}
740+
f = [](bool a, bool b) { return a | b; };
752741
else if(expr.id()==ID_bitxor)
753-
{
754-
for(std::size_t i=0; i<width; i++)
755-
new_value[i]=((a_str[i]=='1')!=(b_str[i]=='1'))?'1':'0';
756-
}
742+
f = [](bool a, bool b) { return a ^ b; };
757743
else
758-
break;
744+
UNREACHABLE;
745+
746+
const irep_idt new_value = bitvector_bitwise_op(a_val, b_val, width, f);
759747

760748
constant_exprt new_op(new_value, expr.type());
761749

@@ -1280,16 +1268,16 @@ bool simplify_exprt::simplify_bitnot(exprt &expr)
12801268
expr.type().id()==ID_unsignedbv ||
12811269
expr.type().id()==ID_signedbv)
12821270
{
1271+
const auto width = to_bitvector_type(expr.type()).get_width();
1272+
12831273
if(op.type()==expr.type())
12841274
{
12851275
if(op.id()==ID_constant)
12861276
{
1287-
std::string value=op.get_string(ID_value);
1288-
1289-
for(auto &ch : value)
1290-
ch=(ch=='0')?'1':'0';
1291-
1292-
expr = constant_exprt(value, op.type());
1277+
const auto &value = to_constant_expr(op).get_value();
1278+
const auto new_value =
1279+
bitvector_bitwise_op(value, width, [](bool a) { return !a; });
1280+
expr = constant_exprt(new_value, op.type());
12931281
return false;
12941282
}
12951283
}

0 commit comments

Comments
 (0)