Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
{f32,f64}::approx_unchecked_to<Int>
unsafe methods
As discussed in #10184 Currently, casting a floating point number to an integer with `as` is Undefined Behavior if the value is out of range. `-Z saturating-float-casts` fixes this soundness hole by making `as` “saturate” to the maximum or minimum value of the integer type (or zero for `NaN`), but has measurable negative performance impact in some benchmarks. There is some consensus in that thread for enabling saturation by default anyway, but provide an `unsafe fn` alternative for users who know through some other mean that their values are in range.
- Loading branch information
1 parent
f442797
commit cba479f
Showing
7 changed files
with
139 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
mod private { | ||
/// This trait being unreachable from outside the crate | ||
/// prevents other implementations of the `FloatToInt` trait, | ||
/// which allows potentially adding more trait methods after the trait is `#[stable]`. | ||
#[unstable(feature = "convert_float_to_int", issue = "67057")] | ||
pub trait Sealed {} | ||
} | ||
|
||
/// Supporting trait for inherent methods of `f32` and `f64` such as `round_unchecked_to`. | ||
/// Typically doesn’t need to be used directly. | ||
#[unstable(feature = "convert_float_to_int", issue = "67057")] | ||
pub trait FloatToInt<Int>: private::Sealed + Sized { | ||
#[cfg(not(bootstrap))] | ||
#[unstable(feature = "float_approx_unchecked_to", issue = "67058")] | ||
#[doc(hidden)] | ||
unsafe fn approx_unchecked(self) -> Int; | ||
} | ||
|
||
macro_rules! impl_float_to_int { | ||
( $Float: ident => $( $Int: ident )+ ) => { | ||
#[unstable(feature = "convert_float_to_int", issue = "67057")] | ||
impl private::Sealed for $Float {} | ||
$( | ||
#[unstable(feature = "convert_float_to_int", issue = "67057")] | ||
impl FloatToInt<$Int> for $Float { | ||
#[cfg(not(bootstrap))] | ||
#[doc(hidden)] | ||
#[inline] | ||
unsafe fn approx_unchecked(self) -> $Int { | ||
crate::intrinsics::float_to_int_approx_unchecked(self) | ||
} | ||
} | ||
)+ | ||
} | ||
} | ||
|
||
impl_float_to_int!(f32 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize); | ||
impl_float_to_int!(f64 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize); |
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
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