Skip to content

Commit 97bea15

Browse files
author
Daniel Kroening
committed
added means to perform bit-wise operations on the bit-vector representation
1 parent ec8b34d commit 97bea15

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

src/util/arith_tools.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,26 @@ 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 f: the functor
269+
irep_idt bitvector_bitwise_op(
270+
const irep_idt &a,
271+
const irep_idt &b,
272+
const std::function<bool(bool, bool)> f)
273+
{
274+
PRECONDITION(a.size() == b.size());
275+
276+
std::string result(' ', a.size());
277+
278+
for(std::size_t i = 0; i < result.size(); i++)
279+
{
280+
const bool bit_a = a[i] == '1';
281+
const bool bit_b = b[i] == '1';
282+
const bool bit_result = f(bit_a, bit_b);
283+
result[i] = bit_result ? '1' : '0';
284+
}
285+
286+
return result;
287+
}

src/util/arith_tools.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,11 @@ 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+
/// perform a binary bit-wise operation, given as a functor,
168+
/// on a bit-vector representation
169+
irep_idt bitvector_bitwise_op(
170+
const irep_idt &,
171+
const irep_idt &,
172+
std::function<bool(bool, bool)>);
173+
167174
#endif // CPROVER_UTIL_ARITH_TOOLS_H

src/util/simplify_expr_int.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -717,9 +717,6 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
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, f);
759747

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

0 commit comments

Comments
 (0)