Skip to content

Commit 8edc5ee

Browse files
author
Daniel Kroening
committed
C front-end: constant folding for floating-point
1 parent f9c5faf commit 8edc5ee

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

regression/cbmc/switch7/main.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// These need to be constant-folded at compile time
2+
3+
#define C1 (int)(0. / 1. + 0.5)
4+
#define C2 (int)(1. / 1. + 0.5)
5+
6+
int nondet_int();
7+
8+
int main(void)
9+
{
10+
int i = nondet_int();
11+
12+
switch(i)
13+
{
14+
case C1:
15+
break;
16+
17+
case C2:
18+
break;
19+
20+
default:
21+
break;
22+
}
23+
}

regression/cbmc/switch7/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Author: Daniel Kroening, [email protected]
2424
#include <util/simplify_expr.h>
2525
#include <util/string_constant.h>
2626

27+
#include <goto-programs/adjust_float_expressions.h>
28+
2729
#include "builtin_factory.h"
2830
#include "c_typecast.h"
2931
#include "c_qualifiers.h"
@@ -3372,6 +3374,13 @@ void c_typecheck_baset::typecheck_side_effect_assignment(
33723374

33733375
void c_typecheck_baset::make_constant(exprt &expr)
33743376
{
3377+
// Floating-point expressions may require a rounding mode.
3378+
// ISO 9899:1999 F.7.2 says that the default is "round to nearest".
3379+
// Some compilers have command-line options to override.
3380+
const auto rounding_mode =
3381+
from_integer(ieee_floatt::ROUND_TO_EVEN, signed_int_type());
3382+
adjust_float_expressions(expr, rounding_mode);
3383+
33753384
simplify(expr, *this);
33763385

33773386
if(!expr.is_constant() &&

0 commit comments

Comments
 (0)