-
Notifications
You must be signed in to change notification settings - Fork 291
Fix float-to-integer conversion for wide destination types #8986
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
Open
tautschnig
wants to merge
1
commit into
diffblue:develop
Choose a base branch
from
tautschnig:fix-float-to-integer
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−2
Open
Changes from all commits
Commits
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 hidden or 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,98 @@ | ||
| // Test float-to-integer (and double-to-integer) conversion for values | ||
| // that need more bits than the source's fraction width. | ||
| // | ||
| // Bug: float_bvt::to_integer did not extend the fraction to the | ||
| // destination width before shifting, so for any integer destination | ||
| // wider than the source's fraction the shift distance went negative | ||
| // for sufficiently large exponents and the conversion produced | ||
| // undefined results. float (24 fraction bits incl. hidden) was | ||
| // affected for any 32-bit-or-wider integer destination; double (53 | ||
| // fraction bits) was affected for any 64-bit-or-wider integer | ||
| // destination. | ||
|
|
||
| #include <assert.h> | ||
|
|
||
| extern int __CPROVER_rounding_mode; | ||
|
|
||
| int main() | ||
| { | ||
| // Test 1: large positive float -> int. | ||
| float a; | ||
| __CPROVER_assume(a == 1000000.0f); | ||
| __CPROVER_rounding_mode = 3; // ROUND_TO_ZERO (required for C semantics) | ||
| int r1 = (int)a; | ||
| assert(r1 == 1000000); | ||
|
|
||
| // Test 2: value at fraction-width boundary (2^24, exactly representable). | ||
| float b; | ||
| __CPROVER_assume(b == 16777216.0f); | ||
| int r2 = (int)b; | ||
| assert(r2 == 16777216); | ||
|
|
||
| // Test 3: value near INT_MAX, well past the fraction width. | ||
| float c; | ||
| __CPROVER_assume(c == 2.0e9f); | ||
| int r3 = (int)c; | ||
| assert(r3 == 2000000000); | ||
|
|
||
| // Test 4: negative large value. | ||
| float d; | ||
| __CPROVER_assume(d == -1000000.0f); | ||
| int r4 = (int)d; | ||
| assert(r4 == -1000000); | ||
|
|
||
| // Test 5: float -> unsigned int, exact value with exponent > fraction | ||
| // width. | ||
| float e1; | ||
| __CPROVER_assume(e1 == 16777216.0f); // 2^24, exactly representable | ||
| unsigned int r5 = (unsigned int)e1; | ||
| assert(r5 == 16777216u); | ||
|
|
||
| // Test 6: float -> unsigned int, value at 2^31 (exactly representable | ||
| // as float, above INT_MAX so the unsigned-vs-signed branch matters). | ||
| float e2; | ||
| __CPROVER_assume(e2 == (float)(1U << 31)); | ||
| unsigned int r6 = (unsigned int)e2; | ||
| assert(r6 == (1U << 31)); | ||
|
|
||
| // Test 7: float -> long long. dest_width = 64, fraction_width = 24, | ||
| // so the fix's extension path is exercised. | ||
| float ll1; | ||
| __CPROVER_assume(ll1 == (float)(1LL << 30)); | ||
| long long r7 = (long long)ll1; | ||
| assert(r7 == (1LL << 30)); | ||
|
|
||
| // Test 8: double -> long long. dest_width = 64, fraction_width = 53, | ||
| // exercises a different effective_width than tests 7. | ||
| double d1; | ||
| __CPROVER_assume(d1 == (double)(1LL << 40)); | ||
| long long r8 = (long long)d1; | ||
| assert(r8 == (1LL << 40)); | ||
|
|
||
| // Test 9: double -> long long, value above 2^53 where double can no | ||
| // longer represent every integer; check an exactly-representable one. | ||
| double d2; | ||
| __CPROVER_assume(d2 == (double)(1LL << 60)); | ||
| long long r9 = (long long)d2; | ||
| assert(r9 == (1LL << 60)); | ||
|
|
||
| #if defined(__SIZEOF_INT128__) | ||
| // Test 10: float -> __int128. dest_width = 128 sits at the documented | ||
| // boundary `(1 << (spec.e - 1)) = 128` for single-precision sources | ||
| // (PRECONDITION in float_bvt::to_integer / float_utilst::to_integer), | ||
| // so this exercises the widest currently-supported destination. | ||
| float fi128; | ||
| __CPROVER_assume(fi128 == (float)(1LL << 30)); | ||
| __int128 r10 = (__int128)fi128; | ||
| assert(r10 == (__int128)(1LL << 30)); | ||
|
|
||
| // Test 11: double -> __int128. dest_width = 128, well within the | ||
| // double precondition boundary `(1 << 10) = 1024`. | ||
| double di128; | ||
| __CPROVER_assume(di128 == (double)(1LL << 60)); | ||
| __int128 r11 = (__int128)di128; | ||
| assert(r11 == (__int128)(1LL << 60)); | ||
| #endif | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or 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,10 @@ | ||
| CORE no-new-smt | ||
| main.c | ||
| --floatbv | ||
| ^EXIT=0$ | ||
| ^SIGNAL=0$ | ||
| ^VERIFICATION SUCCESSFUL$ | ||
| -- | ||
| ^warning: ignoring | ||
| -- | ||
| Float-to-integer conversion for values needing more bits than the fraction. |
This file contains hidden or 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,10 @@ | ||
| CORE smt-backend no-new-smt | ||
| main.c | ||
| --z3 | ||
| ^EXIT=0$ | ||
| ^SIGNAL=0$ | ||
| ^VERIFICATION SUCCESSFUL$ | ||
| -- | ||
| ^warning: ignoring | ||
| -- | ||
| Float-to-integer conversion for values needing more bits than the fraction (SMT path). |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.