Skip to content

Commit

Permalink
C front-end: constant folding for floating-point
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kroening committed Jul 17, 2018
1 parent 8542137 commit 171bf92
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
23 changes: 23 additions & 0 deletions regression/cbmc/switch7/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// These need to be constant-folded at compile time

#define C1 (int) ( 0. / 1. + 0.5)
#define C2 (int) ( 1. / 1. + 0.5)

int nondet_int();

int main(void)
{
int i=nondet_int();

switch(i)
{
case C1:
break;

case C2:
break;

default:
break;
}
}
8 changes: 8 additions & 0 deletions regression/cbmc/switch7/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c

^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
9 changes: 9 additions & 0 deletions src/ansi-c/c_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Author: Daniel Kroening, [email protected]
#include <util/simplify_expr.h>
#include <util/string_constant.h>

#include <goto-programs/adjust_float_expressions.h>

#include "builtin_factory.h"
#include "c_typecast.h"
#include "c_qualifiers.h"
Expand Down Expand Up @@ -3372,6 +3374,13 @@ void c_typecheck_baset::typecheck_side_effect_assignment(

void c_typecheck_baset::make_constant(exprt &expr)
{
// Floating-point expressions may require a rounding mode.
// The compile-time rounding mode may differ from the
// run-time rounding mode.
const auto rounding_mode =
from_integer(config.ansi_c.rounding_mode, signed_int_type());
adjust_float_expressions(expr, rounding_mode);

simplify(expr, *this);

if(!expr.is_constant() &&
Expand Down

0 comments on commit 171bf92

Please sign in to comment.