Skip to content

Commit

Permalink
modernisation of sum_expr and mul_expr
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kroening committed Mar 4, 2018
1 parent 4d87ba1 commit f6890b3
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions src/util/simplify_expr_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,47 +54,47 @@ bool simplify_exprt::simplify_bswap(bswap_exprt &expr)
return true;
}

static bool sum_expr(exprt &dest, const exprt &expr)
//! produce a sum of two constant expressions of the same type
//! \return 'false' iff this was successful
static bool sum_expr(
constant_exprt &dest,
const constant_exprt &expr)
{
if(!dest.is_constant() || !expr.is_constant())
return true;

if(dest.type()!=expr.type())
return true;

const irep_idt &type_id=dest.type().id();

if(type_id==ID_integer || type_id==ID_natural)
{
dest.set(ID_value, integer2string(
string2integer(dest.get_string(ID_value))+
string2integer(expr.get_string(ID_value))));
dest.set_value(integer2string(
string2integer(id2string(dest.get_value()))+
string2integer(id2string(expr.get_value()))));
return false;
}
else if(type_id==ID_rational)
{
rationalt a, b;
if(!to_rational(dest, a) && !to_rational(expr, b))
{
exprt a_plus_b=from_rational(a+b);
dest.set(ID_value, a_plus_b.get_string(ID_value));
dest=from_rational(a+b);
return false;
}
}
else if(type_id==ID_unsignedbv || type_id==ID_signedbv)
{
dest.set(ID_value, integer2binary(
binary2integer(dest.get_string(ID_value), false)+
binary2integer(expr.get_string(ID_value), false),
unsafe_string2unsigned(dest.type().get_string(ID_width))));
dest.set_value(integer2binary(
binary2integer(id2string(dest.get_value()), false)+
binary2integer(id2string(expr.get_value()), false),
to_bitvector_type(dest.type()).get_width()));
return false;
}
else if(type_id==ID_fixedbv)
{
dest.set(ID_value, integer2binary(
binary2integer(dest.get_string(ID_value), false)+
binary2integer(expr.get_string(ID_value), false),
unsafe_string2unsigned(dest.type().get_string(ID_width))));
dest.set_value(integer2binary(
binary2integer(id2string(dest.get_value()), false)+
binary2integer(id2string(expr.get_value()), false),
to_bitvector_type(dest.type()).get_width()));
return false;
}
else if(type_id==ID_floatbv)
Expand All @@ -108,40 +108,40 @@ static bool sum_expr(exprt &dest, const exprt &expr)
return true;
}

static bool mul_expr(exprt &dest, const exprt &expr)
//! produce a product of two expressions of the same type
//! \return 'false' iff this was successful
static bool mul_expr(
constant_exprt &dest,
const constant_exprt &expr)
{
if(!dest.is_constant() || !expr.is_constant())
return true;

if(dest.type()!=expr.type())
return true;

const irep_idt &type_id=dest.type().id();

if(type_id==ID_integer || type_id==ID_natural)
{
dest.set(ID_value, integer2string(
string2integer(dest.get_string(ID_value))*
string2integer(expr.get_string(ID_value))));
dest.set_value(integer2string(
string2integer(id2string(dest.get_value()))*
string2integer(id2string(expr.get_value()))));
return false;
}
else if(type_id==ID_rational)
{
rationalt a, b;
if(!to_rational(dest, a) && !to_rational(expr, b))
{
exprt a_mul_b=from_rational(a*b);
dest.set(ID_value, a_mul_b.get_string(ID_value));
dest=from_rational(a*b);
return false;
}
}
else if(type_id==ID_unsignedbv || type_id==ID_signedbv)
{
// the following works for signed and unsigned integers
dest.set(ID_value, integer2binary(
binary2integer(dest.get_string(ID_value), false)*
binary2integer(expr.get_string(ID_value), false),
unsafe_string2unsigned(dest.type().get_string(ID_width))));
dest.set_value(integer2binary(
binary2integer(id2string(dest.get_value()), false)*
binary2integer(id2string(expr.get_value()), false),
to_bitvector_type(dest.type()).get_width()));
return false;
}
else if(type_id==ID_fixedbv)
Expand Down Expand Up @@ -213,7 +213,7 @@ bool simplify_exprt::simplify_mult(exprt &expr)
if(found)
{
// update the constant factor
if(!mul_expr(*constant, *it))
if(!mul_expr(to_constant_expr(*constant), to_constant_expr(*it)))
do_erase=true;
}
else
Expand Down Expand Up @@ -465,7 +465,7 @@ bool simplify_exprt::simplify_plus(exprt &expr)
it->is_constant() &&
next->is_constant())
{
sum_expr(*it, *next);
sum_expr(to_constant_expr(*it), to_constant_expr(*next));
operands.erase(next);
}
}
Expand Down Expand Up @@ -517,7 +517,8 @@ bool simplify_exprt::simplify_plus(exprt &expr)
}
else
{
if(!sum_expr(*const_sum, *it))
if(!sum_expr(to_constant_expr(*const_sum),
to_constant_expr(*it)))
{
*it=from_integer(0, it->type());
assert(it->is_not_nil());
Expand Down

0 comments on commit f6890b3

Please sign in to comment.