Skip to content

Commit

Permalink
adjust_float_expressions can now take a rounding mode argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kroening committed Jul 17, 2018
1 parent 6ccce5b commit 8542137
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
50 changes: 28 additions & 22 deletions src/goto-programs/adjust_float_expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ Author: Daniel Kroening, [email protected]

#include "goto_model.h"

static bool have_to_adjust_float_expressions(
const exprt &expr,
const namespacet &ns)
static bool have_to_adjust_float_expressions(const exprt &expr)
{
if(expr.id()==ID_floatbv_plus ||
expr.id()==ID_floatbv_minus ||
Expand All @@ -33,11 +31,11 @@ static bool have_to_adjust_float_expressions(
expr.id()==ID_floatbv_typecast)
return false;

const typet &type=ns.follow(expr.type());
const typet &type=expr.type();

if(type.id()==ID_floatbv ||
(type.id()==ID_complex &&
ns.follow(type.subtype()).id()==ID_floatbv))
type.subtype().id()==ID_floatbv))
{
if(expr.id()==ID_plus || expr.id()==ID_minus ||
expr.id()==ID_mult || expr.id()==ID_div ||
Expand Down Expand Up @@ -69,7 +67,7 @@ static bool have_to_adjust_float_expressions(
}

forall_operands(it, expr)
if(have_to_adjust_float_expressions(*it, ns))
if(have_to_adjust_float_expressions(*it))
return true;

return false;
Expand All @@ -79,25 +77,21 @@ static bool have_to_adjust_float_expressions(
/// vectors and complex numbers.
void adjust_float_expressions(
exprt &expr,
const namespacet &ns)
const exprt &rounding_mode)
{
if(!have_to_adjust_float_expressions(expr, ns))
if(!have_to_adjust_float_expressions(expr))
return;

Forall_operands(it, expr)
adjust_float_expressions(*it, ns);
// recursive call
for(auto &op : expr.operands())
adjust_float_expressions(op, rounding_mode);

const typet &type=ns.follow(expr.type());
const typet &type=expr.type();

if(type.id()==ID_floatbv ||
(type.id()==ID_complex &&
ns.follow(type.subtype()).id()==ID_floatbv))
type.subtype().id()==ID_floatbv))
{
symbol_exprt rounding_mode=
ns.lookup(CPROVER_PREFIX "rounding_mode").symbol_expr();

rounding_mode.add_source_location()=expr.source_location();

if(expr.id()==ID_plus || expr.id()==ID_minus ||
expr.id()==ID_mult || expr.id()==ID_div ||
expr.id()==ID_rem)
Expand Down Expand Up @@ -128,11 +122,6 @@ void adjust_float_expressions(
const typet &src_type=typecast_expr.op().type();
const typet &dest_type=typecast_expr.type();

symbol_exprt rounding_mode=
ns.lookup(CPROVER_PREFIX "rounding_mode").symbol_expr();

rounding_mode.add_source_location()=expr.source_location();

if(dest_type.id()==ID_floatbv &&
src_type.id()==ID_floatbv)
{
Expand Down Expand Up @@ -179,6 +168,23 @@ void adjust_float_expressions(
}
}

/// This adds the rounding mode to floating-point operations, including those in
/// vectors and complex numbers.
void adjust_float_expressions(
exprt &expr,
const namespacet &ns)
{
if(!have_to_adjust_float_expressions(expr))
return;

symbol_exprt rounding_mode=
ns.lookup(CPROVER_PREFIX "rounding_mode").symbol_expr();

rounding_mode.add_source_location()=expr.source_location();

adjust_float_expressions(expr, rounding_mode);
}

void adjust_float_expressions(
goto_functionst::goto_functiont &goto_function,
const namespacet &ns)
Expand Down
4 changes: 4 additions & 0 deletions src/goto-programs/adjust_float_expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ void adjust_float_expressions(
exprt &expr,
const namespacet &ns);

void adjust_float_expressions(
exprt &expr,
const exprt &rounding_mode);

void adjust_float_expressions(
goto_functionst::goto_functiont &goto_function,
const namespacet &ns);
Expand Down

0 comments on commit 8542137

Please sign in to comment.