-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ANSI-C: Floating-point constant folding #2584
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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. | ||
// ISO 9899:1999 F.7.2 says that the default is "round to nearest". | ||
// Some compilers have command-line options to override. | ||
const auto rounding_mode = | ||
from_integer(ieee_floatt::ROUND_TO_EVEN, signed_int_type()); | ||
adjust_float_expressions(expr, rounding_mode); | ||
|
||
simplify(expr, *this); | ||
|
||
if(!expr.is_constant() && | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 || | ||
|
@@ -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)) | ||
if( | ||
type.id() == ID_floatbv || | ||
(type.id() == ID_complex && type.subtype().id() == ID_floatbv)) | ||
{ | ||
if(expr.id()==ID_plus || expr.id()==ID_minus || | ||
expr.id()==ID_mult || expr.id()==ID_div || | ||
|
@@ -69,35 +67,29 @@ 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; | ||
} | ||
|
||
/// 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) | ||
void adjust_float_expressions(exprt &expr, 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)) | ||
if( | ||
type.id() == ID_floatbv || | ||
(type.id() == ID_complex && 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) | ||
|
@@ -128,11 +120,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) | ||
{ | ||
|
@@ -179,6 +166,21 @@ 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this follow not necessary (anymore)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hasn't been necessary for many years (ever since typedefs get expanded immediately).