generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 129
Add a Kani function that checks if the range of a float is valid for conversion to int #3742
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
Merged
+166
−1
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e6067e9
Add a Kani function that checks if the range of a float is valid for …
zhassan-aws 1523ad1
Fix import
zhassan-aws 021f64a
Merge branch 'main' into f2i-bounds
zhassan-aws 788d5d8
Address PR comments
zhassan-aws be2eae3
Mark API as unstable
zhassan-aws 895a01f
Clippy fix
zhassan-aws 9cd21f3
Use core directly
zhassan-aws 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
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
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,33 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! This module contains functions useful for float-related checks | ||
| #[macro_export] | ||
| macro_rules! generate_float { | ||
| ($core:path) => { | ||
| use super::kani_intrinsic; | ||
| /// Returns whether the given float `value` satisfies the range | ||
| /// condition of the `to_int_unchecked` methods, namely that the `value` | ||
| /// after truncation is in range of the target `Int` | ||
| /// | ||
| /// # Example: | ||
| /// | ||
| /// ```no_run | ||
| /// let f: f32 = 145.7; | ||
| /// let fits_in_i8 = kani::float::float_to_int_in_range::<f32, i8>(f); | ||
| /// // doesn't fit in `i8` because the value after truncation (`145.0`) is larger than `i8::MAX` | ||
| /// assert!(!fits_in_i8); | ||
| /// | ||
| /// let f: f64 = 1e6; | ||
| /// let fits_in_u32 = kani::float::float_to_int_in_range::<f64, u32>(f); | ||
| /// // fits in `u32` because the value after truncation (`1e6`) is smaller than `u32::MAX` | ||
| /// assert!(fits_in_u32); | ||
| /// ``` | ||
| #[kanitool::fn_marker = "FloatToIntInRangeHook"] | ||
| #[inline(never)] | ||
| pub fn float_to_int_in_range<Float, Int>(value: Float) -> bool { | ||
zhassan-aws marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
zhassan-aws marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| kani_intrinsic() | ||
| } | ||
| }; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| error: Invalid type for first argument of `float_to_int_in_range` intrinsic. Expected a float type. Got `i32` |
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,11 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! This test checks that Kani emits an error when | ||
| //! `kani::float::float_to_int_in_range` is instantiated with a non-float type | ||
|
|
||
| #[kani::proof] | ||
| fn check_invalid_float() { | ||
| let i: i32 = 5; | ||
| let _c = kani::float::float_to_int_in_range::<i32, u8>(i); | ||
| } |
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 @@ | ||
| error: Invalid type for second argument of `float_to_int_in_range` intrinsic. Expected an integer type. Got `bool` |
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,11 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! This test checks that Kani emits an error when | ||
| //! `kani::float::float_to_int_in_range` is instantiated with a non-integer type | ||
|
|
||
| #[kani::proof] | ||
| fn check_invalid_integer() { | ||
| let f: f32 = kani::any(); | ||
| let _c = kani::float::float_to_int_in_range::<f32, bool>(f); | ||
| } |
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,25 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! This test checks that `kani::float::float_to_int_in_range` works as expected | ||
|
|
||
| #[kani::proof] | ||
| fn check_float_to_int_in_range() { | ||
| let f: f32 = 5.6; | ||
| let fits_in_u16 = kani::float::float_to_int_in_range::<f32, u16>(f); | ||
| assert!(fits_in_u16); | ||
| let i: u16 = unsafe { f.to_int_unchecked() }; | ||
| assert_eq!(i, 5); | ||
|
|
||
| let f: f32 = 145.7; | ||
| let fits_in_i8 = kani::float::float_to_int_in_range::<f32, i8>(f); | ||
| // doesn't fit in `i8` because the value after truncation (`145.0`) is larger than `i8::MAX` | ||
| assert!(!fits_in_i8); | ||
|
|
||
| let f: f64 = 1e6; | ||
| let fits_in_u32 = kani::float::float_to_int_in_range::<f64, u32>(f); | ||
| // fits in `u32` because the value after truncation (`1e6`) is smaller than `u32::MAX` | ||
| assert!(fits_in_u32); | ||
| let i: u32 = unsafe { f.to_int_unchecked() }; | ||
| assert_eq!(i, 1_000_000); | ||
| } |
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.