Skip to content

Commit 9bf813d

Browse files
Daniel Kroeningchrisr-diffblue
Daniel Kroening
authored andcommitted
C front-end: constant folding for floating-point
1 parent 0aa898a commit 9bf813d

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

regression/cbmc/switch7/main.c

+23
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

+8
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

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Author: Daniel Kroening, [email protected]
2626
#include <util/simplify_expr.h>
2727
#include <util/string_constant.h>
2828

29+
#include <goto-programs/adjust_float_expressions.h>
30+
2931
#include "builtin_factory.h"
3032
#include "c_typecast.h"
3133
#include "c_qualifiers.h"
@@ -3451,6 +3453,13 @@ void c_typecheck_baset::typecheck_side_effect_assignment(
34513453

34523454
void c_typecheck_baset::make_constant(exprt &expr)
34533455
{
3456+
// Floating-point expressions may require a rounding mode.
3457+
// ISO 9899:1999 F.7.2 says that the default is "round to nearest".
3458+
// Some compilers have command-line options to override.
3459+
const auto rounding_mode =
3460+
from_integer(ieee_floatt::ROUND_TO_EVEN, signed_int_type());
3461+
adjust_float_expressions(expr, rounding_mode);
3462+
34543463
simplify(expr, *this);
34553464

34563465
if(!expr.is_constant() &&

0 commit comments

Comments
 (0)