-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compiler-rt: refactor downcast possibility check to utility function
Bitwise polyfills API uses only u128 as input and output types. The actual bitwise operations happen on shorter types (e.g. i8 operations are in fact done on u8 etc). For this reason we make sure that the value passed in an u128 fits in the target type. This check happens so often, it is now moved to an utility submodule to reduce code duplication. Signed-off-by: Wojciech Zmuda <[email protected]>
- Loading branch information
Showing
5 changed files
with
30 additions
and
30 deletions.
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
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
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod alu; | ||
mod utils; |
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,20 @@ | ||
use core::num::traits::BitSize; | ||
|
||
// Make sure the value `v` can be safely casted down into type `T` without doing the actual cast, | ||
// panicking if this is not possible. | ||
// | ||
// This check is used multiple times throughout the project, so it is marked as #[inline] to hint | ||
// to the compiler that, if possible, it should not emit a call to this function but instead | ||
// insert its body directly into the caller. | ||
#[inline] | ||
pub fn assert_fits_in_type< | ||
T, impl TBitSize: BitSize<T>, impl TTryInto: TryInto<u128, T>, impl TDestruct: Destruct<T> | ||
>( | ||
v: u128 | ||
) { | ||
let bit_size = BitSize::<T>::bits(); | ||
let _: T = match v.try_into() { | ||
Option::Some(value) => value, | ||
Option::None => { panic!("value = {} does not fit in u{}", v, bit_size) }, | ||
}; | ||
} |