Rollup merge of #66841 - SimonSapin:float_round_unchecked_to, r=rkruppe
Add `{f32,f64}::approx_unchecked_to<Int>` unsafe methods
As discussed in https://github.com/rust-lang/rust/issues/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.
<del>The “fit” wording is copied from https://llvm.org/docs/LangRef.html#fptoui-to-instruction, but I’m not certain what it means exactly. Presumably this is after rounding towards zero, and the doc-test with `i8::MIN` seems to confirm this.</del> Clang presumably uses those LLVM intrinsics to implement C and C++ casts, whose respective standard specify that the value *after truncating to keep its integral part* must be representable in the target type.
This commit is contained in:
commit
62528d86b0
8 changed files with 471 additions and 333 deletions
|
|
@ -40,6 +40,11 @@
|
|||
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
mod num;
|
||||
|
||||
#[unstable(feature = "convert_float_to_int", issue = "67057")]
|
||||
pub use num::FloatToInt;
|
||||
|
||||
/// The identity function.
|
||||
///
|
||||
/// Two things are important to note about this function:
|
||||
369
src/libcore/convert/num.rs
Normal file
369
src/libcore/convert/num.rs
Normal file
|
|
@ -0,0 +1,369 @@
|
|||
use super::{From, TryFrom};
|
||||
use crate::num::TryFromIntError;
|
||||
|
||||
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);
|
||||
|
||||
// Conversion traits for primitive integer and float types
|
||||
// Conversions T -> T are covered by a blanket impl and therefore excluded
|
||||
// Some conversions from and to usize/isize are not implemented due to portability concerns
|
||||
macro_rules! impl_from {
|
||||
($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
|
||||
#[$attr]
|
||||
#[doc = $doc]
|
||||
impl From<$Small> for $Large {
|
||||
#[inline]
|
||||
fn from(small: $Small) -> $Large {
|
||||
small as $Large
|
||||
}
|
||||
}
|
||||
};
|
||||
($Small: ty, $Large: ty, #[$attr:meta]) => {
|
||||
impl_from!($Small,
|
||||
$Large,
|
||||
#[$attr],
|
||||
concat!("Converts `",
|
||||
stringify!($Small),
|
||||
"` to `",
|
||||
stringify!($Large),
|
||||
"` losslessly."));
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_from_bool {
|
||||
($target: ty, #[$attr:meta]) => {
|
||||
impl_from!(bool, $target, #[$attr], concat!("Converts a `bool` to a `",
|
||||
stringify!($target), "`. The resulting value is `0` for `false` and `1` for `true`
|
||||
values.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
assert_eq!(", stringify!($target), "::from(true), 1);
|
||||
assert_eq!(", stringify!($target), "::from(false), 0);
|
||||
```"));
|
||||
};
|
||||
}
|
||||
|
||||
// Bool -> Any
|
||||
impl_from_bool! { u8, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { u16, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { u32, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { u64, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { u128, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { usize, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i8, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i16, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i32, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i64, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i128, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { isize, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
|
||||
// Unsigned -> Unsigned
|
||||
impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, u128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u8, usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, u128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u32, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u32, u128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u64, u128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
|
||||
// Signed -> Signed
|
||||
impl_from! { i8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i8, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { i8, isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i16, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { i32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i32, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { i64, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
|
||||
// Unsigned -> Signed
|
||||
impl_from! { u8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u32, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u64, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
|
||||
// The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
|
||||
// which imply that pointer-sized integers must be at least 16 bits:
|
||||
// https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
|
||||
impl_from! { u16, usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
|
||||
impl_from! { u8, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
|
||||
impl_from! { i16, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
|
||||
|
||||
// RISC-V defines the possibility of a 128-bit address space (RV128).
|
||||
|
||||
// CHERI proposes 256-bit “capabilities”. Unclear if this would be relevant to usize/isize.
|
||||
// https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
|
||||
// http://www.csl.sri.com/users/neumann/2012resolve-cheri.pdf
|
||||
|
||||
|
||||
// Note: integers can only be represented with full precision in a float if
|
||||
// they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
|
||||
// Lossy float conversions are not implemented at this time.
|
||||
|
||||
// Signed -> Float
|
||||
impl_from! { i8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { i8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { i16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { i16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { i32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
|
||||
// Unsigned -> Float
|
||||
impl_from! { u8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { u8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { u16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { u16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
|
||||
// Float -> Float
|
||||
impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
|
||||
// no possible bounds violation
|
||||
macro_rules! try_from_unbounded {
|
||||
($source:ty, $($target:ty),*) => {$(
|
||||
#[stable(feature = "try_from", since = "1.34.0")]
|
||||
impl TryFrom<$source> for $target {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
/// Try to create the target number type from a source
|
||||
/// number type. This returns an error if the source value
|
||||
/// is outside of the range of the target type.
|
||||
#[inline]
|
||||
fn try_from(value: $source) -> Result<Self, Self::Error> {
|
||||
Ok(value as $target)
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
// only negative bounds
|
||||
macro_rules! try_from_lower_bounded {
|
||||
($source:ty, $($target:ty),*) => {$(
|
||||
#[stable(feature = "try_from", since = "1.34.0")]
|
||||
impl TryFrom<$source> for $target {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
/// Try to create the target number type from a source
|
||||
/// number type. This returns an error if the source value
|
||||
/// is outside of the range of the target type.
|
||||
#[inline]
|
||||
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
|
||||
if u >= 0 {
|
||||
Ok(u as $target)
|
||||
} else {
|
||||
Err(TryFromIntError(()))
|
||||
}
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
// unsigned to signed (only positive bound)
|
||||
macro_rules! try_from_upper_bounded {
|
||||
($source:ty, $($target:ty),*) => {$(
|
||||
#[stable(feature = "try_from", since = "1.34.0")]
|
||||
impl TryFrom<$source> for $target {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
/// Try to create the target number type from a source
|
||||
/// number type. This returns an error if the source value
|
||||
/// is outside of the range of the target type.
|
||||
#[inline]
|
||||
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
|
||||
if u > (<$target>::max_value() as $source) {
|
||||
Err(TryFromIntError(()))
|
||||
} else {
|
||||
Ok(u as $target)
|
||||
}
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
// all other cases
|
||||
macro_rules! try_from_both_bounded {
|
||||
($source:ty, $($target:ty),*) => {$(
|
||||
#[stable(feature = "try_from", since = "1.34.0")]
|
||||
impl TryFrom<$source> for $target {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
/// Try to create the target number type from a source
|
||||
/// number type. This returns an error if the source value
|
||||
/// is outside of the range of the target type.
|
||||
#[inline]
|
||||
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
|
||||
let min = <$target>::min_value() as $source;
|
||||
let max = <$target>::max_value() as $source;
|
||||
if u < min || u > max {
|
||||
Err(TryFromIntError(()))
|
||||
} else {
|
||||
Ok(u as $target)
|
||||
}
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
macro_rules! rev {
|
||||
($mac:ident, $source:ty, $($target:ty),*) => {$(
|
||||
$mac!($target, $source);
|
||||
)*}
|
||||
}
|
||||
|
||||
// intra-sign conversions
|
||||
try_from_upper_bounded!(u16, u8);
|
||||
try_from_upper_bounded!(u32, u16, u8);
|
||||
try_from_upper_bounded!(u64, u32, u16, u8);
|
||||
try_from_upper_bounded!(u128, u64, u32, u16, u8);
|
||||
|
||||
try_from_both_bounded!(i16, i8);
|
||||
try_from_both_bounded!(i32, i16, i8);
|
||||
try_from_both_bounded!(i64, i32, i16, i8);
|
||||
try_from_both_bounded!(i128, i64, i32, i16, i8);
|
||||
|
||||
// unsigned-to-signed
|
||||
try_from_upper_bounded!(u8, i8);
|
||||
try_from_upper_bounded!(u16, i8, i16);
|
||||
try_from_upper_bounded!(u32, i8, i16, i32);
|
||||
try_from_upper_bounded!(u64, i8, i16, i32, i64);
|
||||
try_from_upper_bounded!(u128, i8, i16, i32, i64, i128);
|
||||
|
||||
// signed-to-unsigned
|
||||
try_from_lower_bounded!(i8, u8, u16, u32, u64, u128);
|
||||
try_from_lower_bounded!(i16, u16, u32, u64, u128);
|
||||
try_from_lower_bounded!(i32, u32, u64, u128);
|
||||
try_from_lower_bounded!(i64, u64, u128);
|
||||
try_from_lower_bounded!(i128, u128);
|
||||
try_from_both_bounded!(i16, u8);
|
||||
try_from_both_bounded!(i32, u16, u8);
|
||||
try_from_both_bounded!(i64, u32, u16, u8);
|
||||
try_from_both_bounded!(i128, u64, u32, u16, u8);
|
||||
|
||||
// usize/isize
|
||||
try_from_upper_bounded!(usize, isize);
|
||||
try_from_lower_bounded!(isize, usize);
|
||||
|
||||
#[cfg(target_pointer_width = "16")]
|
||||
mod ptr_try_from_impls {
|
||||
use super::TryFromIntError;
|
||||
use crate::convert::TryFrom;
|
||||
|
||||
try_from_upper_bounded!(usize, u8);
|
||||
try_from_unbounded!(usize, u16, u32, u64, u128);
|
||||
try_from_upper_bounded!(usize, i8, i16);
|
||||
try_from_unbounded!(usize, i32, i64, i128);
|
||||
|
||||
try_from_both_bounded!(isize, u8);
|
||||
try_from_lower_bounded!(isize, u16, u32, u64, u128);
|
||||
try_from_both_bounded!(isize, i8);
|
||||
try_from_unbounded!(isize, i16, i32, i64, i128);
|
||||
|
||||
rev!(try_from_upper_bounded, usize, u32, u64, u128);
|
||||
rev!(try_from_lower_bounded, usize, i8, i16);
|
||||
rev!(try_from_both_bounded, usize, i32, i64, i128);
|
||||
|
||||
rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
|
||||
rev!(try_from_both_bounded, isize, i32, i64, i128);
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
mod ptr_try_from_impls {
|
||||
use super::TryFromIntError;
|
||||
use crate::convert::TryFrom;
|
||||
|
||||
try_from_upper_bounded!(usize, u8, u16);
|
||||
try_from_unbounded!(usize, u32, u64, u128);
|
||||
try_from_upper_bounded!(usize, i8, i16, i32);
|
||||
try_from_unbounded!(usize, i64, i128);
|
||||
|
||||
try_from_both_bounded!(isize, u8, u16);
|
||||
try_from_lower_bounded!(isize, u32, u64, u128);
|
||||
try_from_both_bounded!(isize, i8, i16);
|
||||
try_from_unbounded!(isize, i32, i64, i128);
|
||||
|
||||
rev!(try_from_unbounded, usize, u32);
|
||||
rev!(try_from_upper_bounded, usize, u64, u128);
|
||||
rev!(try_from_lower_bounded, usize, i8, i16, i32);
|
||||
rev!(try_from_both_bounded, usize, i64, i128);
|
||||
|
||||
rev!(try_from_unbounded, isize, u16);
|
||||
rev!(try_from_upper_bounded, isize, u32, u64, u128);
|
||||
rev!(try_from_unbounded, isize, i32);
|
||||
rev!(try_from_both_bounded, isize, i64, i128);
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
mod ptr_try_from_impls {
|
||||
use super::TryFromIntError;
|
||||
use crate::convert::TryFrom;
|
||||
|
||||
try_from_upper_bounded!(usize, u8, u16, u32);
|
||||
try_from_unbounded!(usize, u64, u128);
|
||||
try_from_upper_bounded!(usize, i8, i16, i32, i64);
|
||||
try_from_unbounded!(usize, i128);
|
||||
|
||||
try_from_both_bounded!(isize, u8, u16, u32);
|
||||
try_from_lower_bounded!(isize, u64, u128);
|
||||
try_from_both_bounded!(isize, i8, i16, i32);
|
||||
try_from_unbounded!(isize, i64, i128);
|
||||
|
||||
rev!(try_from_unbounded, usize, u32, u64);
|
||||
rev!(try_from_upper_bounded, usize, u128);
|
||||
rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
|
||||
rev!(try_from_both_bounded, usize, i128);
|
||||
|
||||
rev!(try_from_unbounded, isize, u16, u32);
|
||||
rev!(try_from_upper_bounded, isize, u64, u128);
|
||||
rev!(try_from_unbounded, isize, i32, i64);
|
||||
rev!(try_from_both_bounded, isize, i128);
|
||||
}
|
||||
|
|
@ -1144,6 +1144,11 @@ extern "rust-intrinsic" {
|
|||
/// May assume inputs are finite.
|
||||
pub fn frem_fast<T>(a: T, b: T) -> T;
|
||||
|
||||
/// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
|
||||
/// https://github.com/rust-lang/rust/issues/10184
|
||||
#[cfg(not(bootstrap))]
|
||||
pub fn float_to_int_approx_unchecked<Float, Int>(value: Float) -> Int;
|
||||
|
||||
|
||||
/// Returns the number of bits set in an integer type `T`
|
||||
pub fn ctpop<T>(x: T) -> T;
|
||||
|
|
|
|||
|
|
@ -7,9 +7,10 @@
|
|||
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
#[cfg(not(bootstrap))]
|
||||
use crate::convert::FloatToInt;
|
||||
#[cfg(not(test))]
|
||||
use crate::intrinsics;
|
||||
|
||||
use crate::mem;
|
||||
use crate::num::FpCategory;
|
||||
|
||||
|
|
@ -400,6 +401,35 @@ impl f32 {
|
|||
intrinsics::minnumf32(self, other)
|
||||
}
|
||||
|
||||
/// Rounds toward zero and converts to any primitive integer type,
|
||||
/// assuming that the value is finite and fits in that type.
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(float_approx_unchecked_to)]
|
||||
///
|
||||
/// let value = 4.6_f32;
|
||||
/// let rounded = unsafe { value.approx_unchecked_to::<u16>() };
|
||||
/// assert_eq!(rounded, 4);
|
||||
///
|
||||
/// let value = -128.9_f32;
|
||||
/// let rounded = unsafe { value.approx_unchecked_to::<i8>() };
|
||||
/// assert_eq!(rounded, std::i8::MIN);
|
||||
/// ```
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The value must:
|
||||
///
|
||||
/// * Not be `NaN`
|
||||
/// * Not be infinite
|
||||
/// * Be representable in the return type `Int`, after truncating off its fractional part
|
||||
#[cfg(not(bootstrap))]
|
||||
#[unstable(feature = "float_approx_unchecked_to", issue = "67058")]
|
||||
#[inline]
|
||||
pub unsafe fn approx_unchecked_to<Int>(self) -> Int where Self: FloatToInt<Int> {
|
||||
FloatToInt::<Int>::approx_unchecked(self)
|
||||
}
|
||||
|
||||
/// Raw transmutation to `u32`.
|
||||
///
|
||||
/// This is currently identical to `transmute::<f32, u32>(self)` on all platforms.
|
||||
|
|
|
|||
|
|
@ -7,9 +7,10 @@
|
|||
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
#[cfg(not(bootstrap))]
|
||||
use crate::convert::FloatToInt;
|
||||
#[cfg(not(test))]
|
||||
use crate::intrinsics;
|
||||
|
||||
use crate::mem;
|
||||
use crate::num::FpCategory;
|
||||
|
||||
|
|
@ -413,6 +414,35 @@ impl f64 {
|
|||
intrinsics::minnumf64(self, other)
|
||||
}
|
||||
|
||||
/// Rounds toward zero and converts to any primitive integer type,
|
||||
/// assuming that the value is finite and fits in that type.
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(float_approx_unchecked_to)]
|
||||
///
|
||||
/// let value = 4.6_f32;
|
||||
/// let rounded = unsafe { value.approx_unchecked_to::<u16>() };
|
||||
/// assert_eq!(rounded, 4);
|
||||
///
|
||||
/// let value = -128.9_f32;
|
||||
/// let rounded = unsafe { value.approx_unchecked_to::<i8>() };
|
||||
/// assert_eq!(rounded, std::i8::MIN);
|
||||
/// ```
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The value must:
|
||||
///
|
||||
/// * Not be `NaN`
|
||||
/// * Not be infinite
|
||||
/// * Be representable in the return type `Int`, after truncating off its fractional part
|
||||
#[cfg(not(bootstrap))]
|
||||
#[unstable(feature = "float_approx_unchecked_to", issue = "67058")]
|
||||
#[inline]
|
||||
pub unsafe fn approx_unchecked_to<Int>(self) -> Int where Self: FloatToInt<Int> {
|
||||
FloatToInt::<Int>::approx_unchecked(self)
|
||||
}
|
||||
|
||||
/// Raw transmutation to `u64`.
|
||||
///
|
||||
/// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
use crate::convert::TryFrom;
|
||||
use crate::fmt;
|
||||
use crate::intrinsics;
|
||||
use crate::mem;
|
||||
|
|
@ -4701,7 +4700,7 @@ from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }
|
|||
/// The error type returned when a checked integral type conversion fails.
|
||||
#[stable(feature = "try_from", since = "1.34.0")]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub struct TryFromIntError(());
|
||||
pub struct TryFromIntError(pub(crate) ());
|
||||
|
||||
impl TryFromIntError {
|
||||
#[unstable(feature = "int_error_internals",
|
||||
|
|
@ -4728,206 +4727,6 @@ impl From<!> for TryFromIntError {
|
|||
}
|
||||
}
|
||||
|
||||
// no possible bounds violation
|
||||
macro_rules! try_from_unbounded {
|
||||
($source:ty, $($target:ty),*) => {$(
|
||||
#[stable(feature = "try_from", since = "1.34.0")]
|
||||
impl TryFrom<$source> for $target {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
/// Try to create the target number type from a source
|
||||
/// number type. This returns an error if the source value
|
||||
/// is outside of the range of the target type.
|
||||
#[inline]
|
||||
fn try_from(value: $source) -> Result<Self, Self::Error> {
|
||||
Ok(value as $target)
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
// only negative bounds
|
||||
macro_rules! try_from_lower_bounded {
|
||||
($source:ty, $($target:ty),*) => {$(
|
||||
#[stable(feature = "try_from", since = "1.34.0")]
|
||||
impl TryFrom<$source> for $target {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
/// Try to create the target number type from a source
|
||||
/// number type. This returns an error if the source value
|
||||
/// is outside of the range of the target type.
|
||||
#[inline]
|
||||
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
|
||||
if u >= 0 {
|
||||
Ok(u as $target)
|
||||
} else {
|
||||
Err(TryFromIntError(()))
|
||||
}
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
// unsigned to signed (only positive bound)
|
||||
macro_rules! try_from_upper_bounded {
|
||||
($source:ty, $($target:ty),*) => {$(
|
||||
#[stable(feature = "try_from", since = "1.34.0")]
|
||||
impl TryFrom<$source> for $target {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
/// Try to create the target number type from a source
|
||||
/// number type. This returns an error if the source value
|
||||
/// is outside of the range of the target type.
|
||||
#[inline]
|
||||
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
|
||||
if u > (<$target>::max_value() as $source) {
|
||||
Err(TryFromIntError(()))
|
||||
} else {
|
||||
Ok(u as $target)
|
||||
}
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
// all other cases
|
||||
macro_rules! try_from_both_bounded {
|
||||
($source:ty, $($target:ty),*) => {$(
|
||||
#[stable(feature = "try_from", since = "1.34.0")]
|
||||
impl TryFrom<$source> for $target {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
/// Try to create the target number type from a source
|
||||
/// number type. This returns an error if the source value
|
||||
/// is outside of the range of the target type.
|
||||
#[inline]
|
||||
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
|
||||
let min = <$target>::min_value() as $source;
|
||||
let max = <$target>::max_value() as $source;
|
||||
if u < min || u > max {
|
||||
Err(TryFromIntError(()))
|
||||
} else {
|
||||
Ok(u as $target)
|
||||
}
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
macro_rules! rev {
|
||||
($mac:ident, $source:ty, $($target:ty),*) => {$(
|
||||
$mac!($target, $source);
|
||||
)*}
|
||||
}
|
||||
|
||||
// intra-sign conversions
|
||||
try_from_upper_bounded!(u16, u8);
|
||||
try_from_upper_bounded!(u32, u16, u8);
|
||||
try_from_upper_bounded!(u64, u32, u16, u8);
|
||||
try_from_upper_bounded!(u128, u64, u32, u16, u8);
|
||||
|
||||
try_from_both_bounded!(i16, i8);
|
||||
try_from_both_bounded!(i32, i16, i8);
|
||||
try_from_both_bounded!(i64, i32, i16, i8);
|
||||
try_from_both_bounded!(i128, i64, i32, i16, i8);
|
||||
|
||||
// unsigned-to-signed
|
||||
try_from_upper_bounded!(u8, i8);
|
||||
try_from_upper_bounded!(u16, i8, i16);
|
||||
try_from_upper_bounded!(u32, i8, i16, i32);
|
||||
try_from_upper_bounded!(u64, i8, i16, i32, i64);
|
||||
try_from_upper_bounded!(u128, i8, i16, i32, i64, i128);
|
||||
|
||||
// signed-to-unsigned
|
||||
try_from_lower_bounded!(i8, u8, u16, u32, u64, u128);
|
||||
try_from_lower_bounded!(i16, u16, u32, u64, u128);
|
||||
try_from_lower_bounded!(i32, u32, u64, u128);
|
||||
try_from_lower_bounded!(i64, u64, u128);
|
||||
try_from_lower_bounded!(i128, u128);
|
||||
try_from_both_bounded!(i16, u8);
|
||||
try_from_both_bounded!(i32, u16, u8);
|
||||
try_from_both_bounded!(i64, u32, u16, u8);
|
||||
try_from_both_bounded!(i128, u64, u32, u16, u8);
|
||||
|
||||
// usize/isize
|
||||
try_from_upper_bounded!(usize, isize);
|
||||
try_from_lower_bounded!(isize, usize);
|
||||
|
||||
#[cfg(target_pointer_width = "16")]
|
||||
mod ptr_try_from_impls {
|
||||
use super::TryFromIntError;
|
||||
use crate::convert::TryFrom;
|
||||
|
||||
try_from_upper_bounded!(usize, u8);
|
||||
try_from_unbounded!(usize, u16, u32, u64, u128);
|
||||
try_from_upper_bounded!(usize, i8, i16);
|
||||
try_from_unbounded!(usize, i32, i64, i128);
|
||||
|
||||
try_from_both_bounded!(isize, u8);
|
||||
try_from_lower_bounded!(isize, u16, u32, u64, u128);
|
||||
try_from_both_bounded!(isize, i8);
|
||||
try_from_unbounded!(isize, i16, i32, i64, i128);
|
||||
|
||||
rev!(try_from_upper_bounded, usize, u32, u64, u128);
|
||||
rev!(try_from_lower_bounded, usize, i8, i16);
|
||||
rev!(try_from_both_bounded, usize, i32, i64, i128);
|
||||
|
||||
rev!(try_from_upper_bounded, isize, u16, u32, u64, u128);
|
||||
rev!(try_from_both_bounded, isize, i32, i64, i128);
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
mod ptr_try_from_impls {
|
||||
use super::TryFromIntError;
|
||||
use crate::convert::TryFrom;
|
||||
|
||||
try_from_upper_bounded!(usize, u8, u16);
|
||||
try_from_unbounded!(usize, u32, u64, u128);
|
||||
try_from_upper_bounded!(usize, i8, i16, i32);
|
||||
try_from_unbounded!(usize, i64, i128);
|
||||
|
||||
try_from_both_bounded!(isize, u8, u16);
|
||||
try_from_lower_bounded!(isize, u32, u64, u128);
|
||||
try_from_both_bounded!(isize, i8, i16);
|
||||
try_from_unbounded!(isize, i32, i64, i128);
|
||||
|
||||
rev!(try_from_unbounded, usize, u32);
|
||||
rev!(try_from_upper_bounded, usize, u64, u128);
|
||||
rev!(try_from_lower_bounded, usize, i8, i16, i32);
|
||||
rev!(try_from_both_bounded, usize, i64, i128);
|
||||
|
||||
rev!(try_from_unbounded, isize, u16);
|
||||
rev!(try_from_upper_bounded, isize, u32, u64, u128);
|
||||
rev!(try_from_unbounded, isize, i32);
|
||||
rev!(try_from_both_bounded, isize, i64, i128);
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
mod ptr_try_from_impls {
|
||||
use super::TryFromIntError;
|
||||
use crate::convert::TryFrom;
|
||||
|
||||
try_from_upper_bounded!(usize, u8, u16, u32);
|
||||
try_from_unbounded!(usize, u64, u128);
|
||||
try_from_upper_bounded!(usize, i8, i16, i32, i64);
|
||||
try_from_unbounded!(usize, i128);
|
||||
|
||||
try_from_both_bounded!(isize, u8, u16, u32);
|
||||
try_from_lower_bounded!(isize, u64, u128);
|
||||
try_from_both_bounded!(isize, i8, i16, i32);
|
||||
try_from_unbounded!(isize, i64, i128);
|
||||
|
||||
rev!(try_from_unbounded, usize, u32, u64);
|
||||
rev!(try_from_upper_bounded, usize, u128);
|
||||
rev!(try_from_lower_bounded, usize, i8, i16, i32, i64);
|
||||
rev!(try_from_both_bounded, usize, i128);
|
||||
|
||||
rev!(try_from_unbounded, isize, u16, u32);
|
||||
rev!(try_from_upper_bounded, isize, u64, u128);
|
||||
rev!(try_from_unbounded, isize, i32, i64);
|
||||
rev!(try_from_both_bounded, isize, i128);
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
trait FromStrRadixHelper: PartialOrd + Copy {
|
||||
fn min_value() -> Self;
|
||||
|
|
@ -5110,131 +4909,3 @@ impl fmt::Display for ParseIntError {
|
|||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub use crate::num::dec2flt::ParseFloatError;
|
||||
|
||||
// Conversion traits for primitive integer and float types
|
||||
// Conversions T -> T are covered by a blanket impl and therefore excluded
|
||||
// Some conversions from and to usize/isize are not implemented due to portability concerns
|
||||
macro_rules! impl_from {
|
||||
($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
|
||||
#[$attr]
|
||||
#[doc = $doc]
|
||||
impl From<$Small> for $Large {
|
||||
#[inline]
|
||||
fn from(small: $Small) -> $Large {
|
||||
small as $Large
|
||||
}
|
||||
}
|
||||
};
|
||||
($Small: ty, $Large: ty, #[$attr:meta]) => {
|
||||
impl_from!($Small,
|
||||
$Large,
|
||||
#[$attr],
|
||||
concat!("Converts `",
|
||||
stringify!($Small),
|
||||
"` to `",
|
||||
stringify!($Large),
|
||||
"` losslessly."));
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_from_bool {
|
||||
($target: ty, #[$attr:meta]) => {
|
||||
impl_from!(bool, $target, #[$attr], concat!("Converts a `bool` to a `",
|
||||
stringify!($target), "`. The resulting value is `0` for `false` and `1` for `true`
|
||||
values.
|
||||
|
||||
# Examples
|
||||
|
||||
```
|
||||
assert_eq!(", stringify!($target), "::from(true), 1);
|
||||
assert_eq!(", stringify!($target), "::from(false), 0);
|
||||
```"));
|
||||
};
|
||||
}
|
||||
|
||||
// Bool -> Any
|
||||
impl_from_bool! { u8, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { u16, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { u32, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { u64, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { u128, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { usize, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i8, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i16, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i32, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i64, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { i128, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
impl_from_bool! { isize, #[stable(feature = "from_bool", since = "1.28.0")] }
|
||||
|
||||
// Unsigned -> Unsigned
|
||||
impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, u128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u8, usize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, u128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u32, u64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u32, u128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u64, u128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
|
||||
// Signed -> Signed
|
||||
impl_from! { i8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i8, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { i8, isize, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i16, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { i32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { i32, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { i64, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
|
||||
// Unsigned -> Signed
|
||||
impl_from! { u8, i16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u8, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u16, i32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u16, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u32, i64, #[stable(feature = "lossless_int_conv", since = "1.5.0")] }
|
||||
impl_from! { u32, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
impl_from! { u64, i128, #[stable(feature = "i128", since = "1.26.0")] }
|
||||
|
||||
// The C99 standard defines bounds on INTPTR_MIN, INTPTR_MAX, and UINTPTR_MAX
|
||||
// which imply that pointer-sized integers must be at least 16 bits:
|
||||
// https://port70.net/~nsz/c/c99/n1256.html#7.18.2.4
|
||||
impl_from! { u16, usize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
|
||||
impl_from! { u8, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
|
||||
impl_from! { i16, isize, #[stable(feature = "lossless_iusize_conv", since = "1.26.0")] }
|
||||
|
||||
// RISC-V defines the possibility of a 128-bit address space (RV128).
|
||||
|
||||
// CHERI proposes 256-bit “capabilities”. Unclear if this would be relevant to usize/isize.
|
||||
// https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf
|
||||
// http://www.csl.sri.com/users/neumann/2012resolve-cheri.pdf
|
||||
|
||||
|
||||
// Note: integers can only be represented with full precision in a float if
|
||||
// they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
|
||||
// Lossy float conversions are not implemented at this time.
|
||||
|
||||
// Signed -> Float
|
||||
impl_from! { i8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { i8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { i16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { i16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { i32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
|
||||
// Unsigned -> Float
|
||||
impl_from! { u8, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { u8, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { u16, f32, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { u16, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
|
||||
// Float -> Float
|
||||
impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
|
||||
|
|
|
|||
|
|
@ -516,9 +516,36 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
|
|||
return;
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
"float_to_int_approx_unchecked" => {
|
||||
if float_type_width(arg_tys[0]).is_none() {
|
||||
span_invalid_monomorphization_error(
|
||||
tcx.sess, span,
|
||||
&format!("invalid monomorphization of `float_to_int_approx_unchecked` \
|
||||
intrinsic: expected basic float type, \
|
||||
found `{}`", arg_tys[0]));
|
||||
return;
|
||||
}
|
||||
match int_type_width_signed(ret_ty, self.cx) {
|
||||
Some((width, signed)) => {
|
||||
if signed {
|
||||
self.fptosi(args[0].immediate(), self.cx.type_ix(width))
|
||||
} else {
|
||||
self.fptoui(args[0].immediate(), self.cx.type_ix(width))
|
||||
}
|
||||
}
|
||||
None => {
|
||||
span_invalid_monomorphization_error(
|
||||
tcx.sess, span,
|
||||
&format!("invalid monomorphization of `float_to_int_approx_unchecked` \
|
||||
intrinsic: expected basic integer type, \
|
||||
found `{}`", ret_ty));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"discriminant_value" => {
|
||||
args[0].deref(self.cx()).codegen_get_discr(self, ret_ty)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -336,6 +336,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem) {
|
|||
(1, vec![param(0), param(0)], param(0)),
|
||||
"fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" =>
|
||||
(1, vec![param(0), param(0)], param(0)),
|
||||
"float_to_int_approx_unchecked" => (2, vec![ param(0) ], param(1)),
|
||||
|
||||
"assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
|
||||
"likely" => (0, vec![tcx.types.bool], tcx.types.bool),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue