Skip to content

Commit 171bf92

Browse files
author
Daniel Kroening
committed
C front-end: constant folding for floating-point
1 parent 8542137 commit 171bf92

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
@@ -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+
// The compile-time rounding mode may differ from the
3379+
// run-time rounding mode.
3380+
const auto rounding_mode =
3381+
from_integer(config.ansi_c.rounding_mode, 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)