Skip to content

Commit 2a5599a

Browse files
author
Daniel Kroening
committed
added means to create bit-vector representation from functor
1 parent f0ec760 commit 2a5599a

File tree

3 files changed

+66
-28
lines changed

3 files changed

+66
-28
lines changed

src/util/arith_tools.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,49 @@ bool get_bitvector_bit(const irep_idt &src, std::size_t bit_index)
273273
PRECONDITION(bit_index < src.size());
274274
return src[src.size() - 1 - bit_index] == '1';
275275
}
276+
277+
/// construct a bit-vector representation from a functor
278+
/// \param width: the width of the bit-vector
279+
/// \param f: the functor -- the parameter is the bit index
280+
/// \returns new bitvector representation
281+
irep_idt make_bvrep(
282+
const std::size_t width,
283+
const std::function<bool(std::size_t)> f)
284+
{
285+
std::string result(width, ' ');
286+
287+
for(std::size_t i = 0; i < width; i++)
288+
result[width - 1 - i] = f(i) ? '1' : '0';
289+
290+
return result;
291+
}
292+
293+
/// perform a binary bit-wise operation, given as a functor,
294+
/// on a bit-vector representation
295+
/// \param a: the representation of the first bit vector
296+
/// \param b: the representation of the second bit vector
297+
/// \param width: the width of the bit-vector
298+
/// \param f: the functor
299+
/// \returns new bitvector representation
300+
irep_idt bitvector_bitwise_op(
301+
const irep_idt &a,
302+
const irep_idt &b,
303+
const std::size_t width,
304+
const std::function<bool(bool, bool)> f)
305+
{
306+
return make_bvrep(width, [&a, &b, f](std::size_t i){ return f(get_bitvector_bit(a, i), get_bitvector_bit(b, i)); });
307+
}
308+
309+
/// perform a unary bit-wise operation, given as a functor,
310+
/// on a bit-vector representation
311+
/// \param a: the bit-vector representation
312+
/// \param width: the width of the bit-vector
313+
/// \param f: the functor
314+
/// \returns new bitvector representation
315+
irep_idt bitvector_bitwise_op(
316+
const irep_idt &a,
317+
const std::size_t width,
318+
const std::function<bool(bool)> f)
319+
{
320+
return make_bvrep(width, [&a, f](std::size_t i){ return f(get_bitvector_bit(a, i)); });
321+
}

src/util/arith_tools.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,8 @@ void mp_max(mp_integer &a, const mp_integer &b);
166166

167167
bool get_bitvector_bit(const irep_idt &src, std::size_t bit_index);
168168

169+
irep_idt make_bvrep(
170+
const std::size_t width,
171+
const std::function<bool(std::size_t)> f);
172+
169173
#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 = make_bvrep(width, [&a_val, &b_val, &f](std::size_t i) { return f(get_bitvector_bit(a_val, i), get_bitvector_bit(b_val, i)); });
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+
make_bvrep(width, [&value](std::size_t i){ return !get_bitvector_bit(value, i);});
1280+
expr = constant_exprt(new_value, op.type());
12931281
return false;
12941282
}
12951283
}

0 commit comments

Comments
 (0)