Make min/max and copysign intrinsics generic - #162414
Conversation
|
Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr
cc @bjorn3
cc @rust-lang/clippy
cc @rust-lang/miri
Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri Any special-casing of Miri in the standard library requires review. cc @rust-lang/miri
cc @rust-lang/wg-const-eval
cc @rust-lang/wg-const-eval Some changes occurred to the CTFE machinery |
|
|
I just realized that we can maybe just use |
|
doesn't that feel a bit hackier / risk prone that just doing it like this? though i don't mind, we are calling externs from |
| @@ -66,6 +71,18 @@ const unsafe impl FloatPrimitive for f16 { | |||
| fn from_bits(bits: Self::UInt) -> Self { | |||
| f16::from_bits(bits) | |||
| } | |||
| #[inline] | |||
| fn is_nan(self) -> bool { | |||
| f16::is_nan(self) | |||
| } | |||
| #[inline] | |||
| fn is_sign_positive(self) -> bool { | |||
| f16::is_sign_positive(self) | |||
| } | |||
| #[inline] | |||
| fn is_sign_negative(self) -> bool { | |||
| f16::is_sign_negative(self) | |||
| } | |||
| } | |||
|
|
|||
| #[rustc_const_unstable(feature = "core_intrinsics", issue = "none")] | |||
| @@ -80,6 +97,18 @@ const unsafe impl FloatPrimitive for f32 { | |||
| fn from_bits(bits: Self::UInt) -> Self { | |||
| f32::from_bits(bits) | |||
| } | |||
| #[inline] | |||
| fn is_nan(self) -> bool { | |||
| f32::is_nan(self) | |||
| } | |||
| #[inline] | |||
| fn is_sign_positive(self) -> bool { | |||
| f32::is_sign_positive(self) | |||
| } | |||
| #[inline] | |||
| fn is_sign_negative(self) -> bool { | |||
| f32::is_sign_negative(self) | |||
| } | |||
| } | |||
|
|
|||
| #[rustc_const_unstable(feature = "core_intrinsics", issue = "none")] | |||
| @@ -94,6 +123,18 @@ const unsafe impl FloatPrimitive for f64 { | |||
| fn from_bits(bits: Self::UInt) -> Self { | |||
| f64::from_bits(bits) | |||
| } | |||
| #[inline] | |||
| fn is_nan(self) -> bool { | |||
| f64::is_nan(self) | |||
| } | |||
| #[inline] | |||
| fn is_sign_positive(self) -> bool { | |||
| f64::is_sign_positive(self) | |||
| } | |||
| #[inline] | |||
| fn is_sign_negative(self) -> bool { | |||
| f64::is_sign_negative(self) | |||
| } | |||
| } | |||
|
|
|||
| #[rustc_const_unstable(feature = "core_intrinsics", issue = "none")] | |||
| @@ -108,6 +149,18 @@ const unsafe impl FloatPrimitive for f128 { | |||
| fn from_bits(bits: Self::UInt) -> Self { | |||
| f128::from_bits(bits) | |||
| } | |||
| #[inline] | |||
| fn is_nan(self) -> bool { | |||
| f128::is_nan(self) | |||
| } | |||
| #[inline] | |||
| fn is_sign_positive(self) -> bool { | |||
| f128::is_sign_positive(self) | |||
| } | |||
| #[inline] | |||
| fn is_sign_negative(self) -> bool { | |||
| f128::is_sign_negative(self) | |||
| } | |||
| } | |||
There was a problem hiding this comment.
Since this is repeating the same thing 4 times, I think it would be tidier as a macro. Something like:
| macro_rules! impl_float_primitive { | |
| (unsafe $($float:ident => $bits:ident),+) => { | |
| #[rustc_const_unstable(feature = "core_intrinsics", issue = "none")] | |
| const unsafe impl FloatPrimitive for $float { | |
| type UInt = $bits; | |
| const SIGN_MASK: Self::UInt = Self::SIGN_MASK; | |
| #[inline] | |
| fn to_bits(self) -> Self::UInt { | |
| Self::to_bits(self) | |
| } | |
| #[inline] | |
| fn from_bits(bits: Self::UInt) -> Self { | |
| Self::from_bits(bits) | |
| } | |
| #[inline] | |
| fn is_nan(self) -> bool { | |
| Self::is_nan(self) | |
| } | |
| #[inline] | |
| fn is_sign_positive(self) -> bool { | |
| Self::is_sign_positive(self) | |
| } | |
| #[inline] | |
| fn is_sign_negative(self) -> bool { | |
| Self::is_sign_negative(self) | |
| } | |
| } | |
| }; | |
| } | |
| impl_float_primitive!(unsafe f16 => u16, f32 => u32, f64 => u64, f128 => u128); |
There was a problem hiding this comment.
true! originally we had agreed to not use a macro but given it's growing a bit and there's a macro for ints right below this makes sense; done in 1514b9c
Split off from #162395
Make the following intrinsics generic over the float type:
copysign, with a shared fallback (bit manipulations). Note that this required adding#[rustc_const_unstable]to the whole intrinsic, whichcopysignf16andcopysignf128didn't have. This was already FCP'd, see rust-lang/rust#153834 (comment).minimum_number_nsz,maximum_number_nsz,minimum,maximum, with shared fallbacks.Because these are generic and require const trait impls to have a fallback, I had to add
#[rustc_allow_const_fn_unstable]to them:copysignneedsconst_ops,const_trait_implminimum_number_nszandmaximum_number_nszneedconst_cmp,const_trait_implminimumandmaximumneedconst_cmp,const_ops,const_trait_implI have also bundled in the addition of the attribute to
fabs, which used these features without having a formal approval (see #162409 -- this was introduced in #153834)See #162395 (comment)
r? RalfJung