Skip to content

Commit 00f3417

Browse files
committed
Mark some f16 and f128 functions unstably const
These constifications were blocked on classification functions being added. Now that those methods are available, constify them. This brings things more in line with `f32` and `f64`.
1 parent 3a2c0ae commit 00f3417

File tree

2 files changed

+206
-32
lines changed

2 files changed

+206
-32
lines changed

library/core/src/num/f128.rs

+103-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#![unstable(feature = "f128", issue = "116909")]
1313

1414
use crate::convert::FloatToInt;
15+
#[cfg(not(test))]
16+
use crate::intrinsics;
1517
use crate::mem;
1618
use crate::num::FpCategory;
1719

@@ -758,12 +760,51 @@ impl f128 {
758760
/// ```
759761
#[inline]
760762
#[unstable(feature = "f128", issue = "116909")]
763+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
761764
#[must_use = "this returns the result of the operation, without modifying the original"]
762-
pub fn to_bits(self) -> u128 {
763-
// SAFETY: `u128` is a plain old datatype so we can always... uh...
764-
// ...look, just pretend you forgot what you just read.
765-
// Stability concerns.
766-
unsafe { mem::transmute(self) }
765+
pub const fn to_bits(self) -> u128 {
766+
// SAFETY: `u128` is a plain old datatype so we can always transmute to it.
767+
// ...sorta.
768+
//
769+
// It turns out that at runtime, it is possible for a floating point number
770+
// to be subject to a floating point mode that alters nonzero subnormal numbers
771+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
772+
//
773+
// And, of course evaluating to a NaN value is fairly nondeterministic.
774+
// More precisely: when NaN should be returned is knowable, but which NaN?
775+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
776+
// This function, however, allows observing the bitstring of a NaN,
777+
// thus introspection on CTFE.
778+
//
779+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
780+
// we reject any of these possible situations from happening.
781+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
782+
const fn ct_f128_to_u128(ct: f128) -> u128 {
783+
// FIXME(f16_f128): we should use `.classify()` like `f32` and `f64`, but that
784+
// is not available on all platforms (needs `netf2` and `unordtf2`). So classify
785+
// the bits instead.
786+
787+
// SAFETY: this is a POD transmutation
788+
let bits = unsafe { mem::transmute::<f128, u128>(ct) };
789+
match f128::classify_bits(bits) {
790+
FpCategory::Nan => {
791+
panic!("const-eval error: cannot use f128::to_bits on a NaN")
792+
}
793+
FpCategory::Subnormal => {
794+
panic!("const-eval error: cannot use f128::to_bits on a subnormal number")
795+
}
796+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => bits,
797+
}
798+
}
799+
800+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
801+
fn rt_f128_to_u128(x: f128) -> u128 {
802+
// SAFETY: `u128` is a plain old datatype so we can always... uh...
803+
// ...look, just pretend you forgot what you just read.
804+
// Stability concerns.
805+
unsafe { mem::transmute(x) }
806+
}
807+
intrinsics::const_eval_select((self,), ct_f128_to_u128, rt_f128_to_u128)
767808
}
768809

769810
/// Raw transmutation from `u128`.
@@ -808,11 +849,51 @@ impl f128 {
808849
#[inline]
809850
#[must_use]
810851
#[unstable(feature = "f128", issue = "116909")]
811-
pub fn from_bits(v: u128) -> Self {
812-
// SAFETY: `u128 is a plain old datatype so we can always... uh...
813-
// ...look, just pretend you forgot what you just read.
814-
// Stability concerns.
815-
unsafe { mem::transmute(v) }
852+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
853+
pub const fn from_bits(v: u128) -> Self {
854+
// It turns out the safety issues with sNaN were overblown! Hooray!
855+
// SAFETY: `u128` is a plain old datatype so we can always transmute from it
856+
// ...sorta.
857+
//
858+
// It turns out that at runtime, it is possible for a floating point number
859+
// to be subject to floating point modes that alter nonzero subnormal numbers
860+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
861+
// This is not a problem usually, but at least one tier2 platform for Rust
862+
// actually exhibits this behavior by default: thumbv7neon
863+
// aka "the Neon FPU in AArch32 state"
864+
//
865+
// And, of course evaluating to a NaN value is fairly nondeterministic.
866+
// More precisely: when NaN should be returned is knowable, but which NaN?
867+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
868+
// This function, however, allows observing the bitstring of a NaN,
869+
// thus introspection on CTFE.
870+
//
871+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
872+
// reject any of these possible situations from happening.
873+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
874+
const fn ct_u128_to_f128(ct: u128) -> f128 {
875+
match f128::classify_bits(ct) {
876+
FpCategory::Subnormal => {
877+
panic!("const-eval error: cannot use f128::from_bits on a subnormal number")
878+
}
879+
FpCategory::Nan => {
880+
panic!("const-eval error: cannot use f128::from_bits on NaN")
881+
}
882+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => {
883+
// SAFETY: It's not a frumious number
884+
unsafe { mem::transmute::<u128, f128>(ct) }
885+
}
886+
}
887+
}
888+
889+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
890+
fn rt_u128_to_f128(x: u128) -> f128 {
891+
// SAFETY: `u128` is a plain old datatype so we can always... uh...
892+
// ...look, just pretend you forgot what you just read.
893+
// Stability concerns.
894+
unsafe { mem::transmute(x) }
895+
}
896+
intrinsics::const_eval_select((v,), ct_u128_to_f128, rt_u128_to_f128)
816897
}
817898

818899
/// Return the memory representation of this floating point number as a byte array in
@@ -835,8 +916,9 @@ impl f128 {
835916
/// ```
836917
#[inline]
837918
#[unstable(feature = "f128", issue = "116909")]
919+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
838920
#[must_use = "this returns the result of the operation, without modifying the original"]
839-
pub fn to_be_bytes(self) -> [u8; 16] {
921+
pub const fn to_be_bytes(self) -> [u8; 16] {
840922
self.to_bits().to_be_bytes()
841923
}
842924

@@ -860,8 +942,9 @@ impl f128 {
860942
/// ```
861943
#[inline]
862944
#[unstable(feature = "f128", issue = "116909")]
945+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
863946
#[must_use = "this returns the result of the operation, without modifying the original"]
864-
pub fn to_le_bytes(self) -> [u8; 16] {
947+
pub const fn to_le_bytes(self) -> [u8; 16] {
865948
self.to_bits().to_le_bytes()
866949
}
867950

@@ -896,8 +979,9 @@ impl f128 {
896979
/// ```
897980
#[inline]
898981
#[unstable(feature = "f128", issue = "116909")]
982+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
899983
#[must_use = "this returns the result of the operation, without modifying the original"]
900-
pub fn to_ne_bytes(self) -> [u8; 16] {
984+
pub const fn to_ne_bytes(self) -> [u8; 16] {
901985
self.to_bits().to_ne_bytes()
902986
}
903987

@@ -923,7 +1007,8 @@ impl f128 {
9231007
#[inline]
9241008
#[must_use]
9251009
#[unstable(feature = "f128", issue = "116909")]
926-
pub fn from_be_bytes(bytes: [u8; 16]) -> Self {
1010+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1011+
pub const fn from_be_bytes(bytes: [u8; 16]) -> Self {
9271012
Self::from_bits(u128::from_be_bytes(bytes))
9281013
}
9291014

@@ -949,7 +1034,8 @@ impl f128 {
9491034
#[inline]
9501035
#[must_use]
9511036
#[unstable(feature = "f128", issue = "116909")]
952-
pub fn from_le_bytes(bytes: [u8; 16]) -> Self {
1037+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1038+
pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
9531039
Self::from_bits(u128::from_le_bytes(bytes))
9541040
}
9551041

@@ -985,7 +1071,8 @@ impl f128 {
9851071
#[inline]
9861072
#[must_use]
9871073
#[unstable(feature = "f128", issue = "116909")]
988-
pub fn from_ne_bytes(bytes: [u8; 16]) -> Self {
1074+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1075+
pub const fn from_ne_bytes(bytes: [u8; 16]) -> Self {
9891076
Self::from_bits(u128::from_ne_bytes(bytes))
9901077
}
9911078

library/core/src/num/f16.rs

+103-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#![unstable(feature = "f16", issue = "116909")]
1313

1414
use crate::convert::FloatToInt;
15+
#[cfg(not(test))]
16+
use crate::intrinsics;
1517
use crate::mem;
1618
use crate::num::FpCategory;
1719

@@ -788,12 +790,51 @@ impl f16 {
788790
/// ```
789791
#[inline]
790792
#[unstable(feature = "f16", issue = "116909")]
793+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
791794
#[must_use = "this returns the result of the operation, without modifying the original"]
792-
pub fn to_bits(self) -> u16 {
793-
// SAFETY: `u16` is a plain old datatype so we can always... uh...
794-
// ...look, just pretend you forgot what you just read.
795-
// Stability concerns.
796-
unsafe { mem::transmute(self) }
795+
pub const fn to_bits(self) -> u16 {
796+
// SAFETY: `u16` is a plain old datatype so we can always transmute to it.
797+
// ...sorta.
798+
//
799+
// It turns out that at runtime, it is possible for a floating point number
800+
// to be subject to a floating point mode that alters nonzero subnormal numbers
801+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
802+
//
803+
// And, of course evaluating to a NaN value is fairly nondeterministic.
804+
// More precisely: when NaN should be returned is knowable, but which NaN?
805+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
806+
// This function, however, allows observing the bitstring of a NaN,
807+
// thus introspection on CTFE.
808+
//
809+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
810+
// we reject any of these possible situations from happening.
811+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
812+
const fn ct_f16_to_u16(ct: f16) -> u16 {
813+
// FIXME(f16_f128): we should use `.classify()` like `f32` and `f64`, but we don't yet
814+
// want to rely on that on all platforms because it is nondeterministic (e.g. x86 has
815+
// convention discrepancies calling intrinsics). So just classify the bits instead.
816+
817+
// SAFETY: this is a POD transmutation
818+
let bits = unsafe { mem::transmute::<f16, u16>(ct) };
819+
match f16::classify_bits(bits) {
820+
FpCategory::Nan => {
821+
panic!("const-eval error: cannot use f16::to_bits on a NaN")
822+
}
823+
FpCategory::Subnormal => {
824+
panic!("const-eval error: cannot use f16::to_bits on a subnormal number")
825+
}
826+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => bits,
827+
}
828+
}
829+
830+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
831+
fn rt_f16_to_u16(x: f16) -> u16 {
832+
// SAFETY: `u16` is a plain old datatype so we can always... uh...
833+
// ...look, just pretend you forgot what you just read.
834+
// Stability concerns.
835+
unsafe { mem::transmute(x) }
836+
}
837+
intrinsics::const_eval_select((self,), ct_f16_to_u16, rt_f16_to_u16)
797838
}
798839

799840
/// Raw transmutation from `u16`.
@@ -837,11 +878,51 @@ impl f16 {
837878
#[inline]
838879
#[must_use]
839880
#[unstable(feature = "f16", issue = "116909")]
840-
pub fn from_bits(v: u16) -> Self {
841-
// SAFETY: `u16` is a plain old datatype so we can always... uh...
842-
// ...look, just pretend you forgot what you just read.
843-
// Stability concerns.
844-
unsafe { mem::transmute(v) }
881+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
882+
pub const fn from_bits(v: u16) -> Self {
883+
// It turns out the safety issues with sNaN were overblown! Hooray!
884+
// SAFETY: `u16` is a plain old datatype so we can always transmute from it
885+
// ...sorta.
886+
//
887+
// It turns out that at runtime, it is possible for a floating point number
888+
// to be subject to floating point modes that alter nonzero subnormal numbers
889+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
890+
// This is not a problem usually, but at least one tier2 platform for Rust
891+
// actually exhibits this behavior by default: thumbv7neon
892+
// aka "the Neon FPU in AArch32 state"
893+
//
894+
// And, of course evaluating to a NaN value is fairly nondeterministic.
895+
// More precisely: when NaN should be returned is knowable, but which NaN?
896+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
897+
// This function, however, allows observing the bitstring of a NaN,
898+
// thus introspection on CTFE.
899+
//
900+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
901+
// reject any of these possible situations from happening.
902+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
903+
const fn ct_u16_to_f16(ct: u16) -> f16 {
904+
match f16::classify_bits(ct) {
905+
FpCategory::Subnormal => {
906+
panic!("const-eval error: cannot use f16::from_bits on a subnormal number")
907+
}
908+
FpCategory::Nan => {
909+
panic!("const-eval error: cannot use f16::from_bits on NaN")
910+
}
911+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => {
912+
// SAFETY: It's not a frumious number
913+
unsafe { mem::transmute::<u16, f16>(ct) }
914+
}
915+
}
916+
}
917+
918+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
919+
fn rt_u16_to_f16(x: u16) -> f16 {
920+
// SAFETY: `u16` is a plain old datatype so we can always... uh...
921+
// ...look, just pretend you forgot what you just read.
922+
// Stability concerns.
923+
unsafe { mem::transmute(x) }
924+
}
925+
intrinsics::const_eval_select((v,), ct_u16_to_f16, rt_u16_to_f16)
845926
}
846927

847928
/// Return the memory representation of this floating point number as a byte array in
@@ -860,8 +941,9 @@ impl f16 {
860941
/// ```
861942
#[inline]
862943
#[unstable(feature = "f16", issue = "116909")]
944+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
863945
#[must_use = "this returns the result of the operation, without modifying the original"]
864-
pub fn to_be_bytes(self) -> [u8; 2] {
946+
pub const fn to_be_bytes(self) -> [u8; 2] {
865947
self.to_bits().to_be_bytes()
866948
}
867949

@@ -881,8 +963,9 @@ impl f16 {
881963
/// ```
882964
#[inline]
883965
#[unstable(feature = "f16", issue = "116909")]
966+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
884967
#[must_use = "this returns the result of the operation, without modifying the original"]
885-
pub fn to_le_bytes(self) -> [u8; 2] {
968+
pub const fn to_le_bytes(self) -> [u8; 2] {
886969
self.to_bits().to_le_bytes()
887970
}
888971

@@ -915,8 +998,9 @@ impl f16 {
915998
/// ```
916999
#[inline]
9171000
#[unstable(feature = "f16", issue = "116909")]
1001+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
9181002
#[must_use = "this returns the result of the operation, without modifying the original"]
919-
pub fn to_ne_bytes(self) -> [u8; 2] {
1003+
pub const fn to_ne_bytes(self) -> [u8; 2] {
9201004
self.to_bits().to_ne_bytes()
9211005
}
9221006

@@ -938,7 +1022,8 @@ impl f16 {
9381022
#[inline]
9391023
#[must_use]
9401024
#[unstable(feature = "f16", issue = "116909")]
941-
pub fn from_be_bytes(bytes: [u8; 2]) -> Self {
1025+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1026+
pub const fn from_be_bytes(bytes: [u8; 2]) -> Self {
9421027
Self::from_bits(u16::from_be_bytes(bytes))
9431028
}
9441029

@@ -960,7 +1045,8 @@ impl f16 {
9601045
#[inline]
9611046
#[must_use]
9621047
#[unstable(feature = "f16", issue = "116909")]
963-
pub fn from_le_bytes(bytes: [u8; 2]) -> Self {
1048+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1049+
pub const fn from_le_bytes(bytes: [u8; 2]) -> Self {
9641050
Self::from_bits(u16::from_le_bytes(bytes))
9651051
}
9661052

@@ -993,7 +1079,8 @@ impl f16 {
9931079
#[inline]
9941080
#[must_use]
9951081
#[unstable(feature = "f16", issue = "116909")]
996-
pub fn from_ne_bytes(bytes: [u8; 2]) -> Self {
1082+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1083+
pub const fn from_ne_bytes(bytes: [u8; 2]) -> Self {
9971084
Self::from_bits(u16::from_ne_bytes(bytes))
9981085
}
9991086

0 commit comments

Comments
 (0)