Skip to content

Commit 6e8e9f2

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 86c69ed commit 6e8e9f2

File tree

2 files changed

+232
-32
lines changed

2 files changed

+232
-32
lines changed

library/core/src/num/f128.rs

+116-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

@@ -808,12 +810,59 @@ impl f128 {
808810
/// ```
809811
#[inline]
810812
#[unstable(feature = "f128", issue = "116909")]
813+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
811814
#[must_use = "this returns the result of the operation, without modifying the original"]
812-
pub fn to_bits(self) -> u128 {
813-
// SAFETY: `u128` is a plain old datatype so we can always... uh...
814-
// ...look, just pretend you forgot what you just read.
815-
// Stability concerns.
816-
unsafe { mem::transmute(self) }
815+
pub const fn to_bits(self) -> u128 {
816+
// SAFETY: `u128` is a plain old datatype so we can always transmute to it.
817+
// ...sorta.
818+
//
819+
// It turns out that at runtime, it is possible for a floating point number
820+
// to be subject to a floating point mode that alters nonzero subnormal numbers
821+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
822+
// This is not a problem per se, but at least one tier2 platform for Rust
823+
// actually exhibits this behavior by default.
824+
//
825+
// In addition, on x86 targets with SSE or SSE2 disabled and the x87 FPU enabled,
826+
// i.e. not soft-float, the way Rust does parameter passing can actually alter
827+
// a number that is "not infinity" to have the same exponent as infinity,
828+
// in a slightly unpredictable manner.
829+
//
830+
// And, of course evaluating to a NaN value is fairly nondeterministic.
831+
// More precisely: when NaN should be returned is knowable, but which NaN?
832+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
833+
// This function, however, allows observing the bitstring of a NaN,
834+
// thus introspection on CTFE.
835+
//
836+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
837+
// we reject any of these possible situations from happening.
838+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
839+
const fn ct_f128_to_u128(ct: f128) -> u128 {
840+
// FIXME(f16_f128): we should use `.classify()` like `f32` and `f64`, but that
841+
// is not avaialble on all platforms (needs `netf2` and `unordtf2`). So classify
842+
// the bits instead.
843+
844+
// SAFETY: this direction is a POD transmutation. We just can't return it unless
845+
// it is normal, infinite, or zero.
846+
let bits = unsafe { mem::transmute::<f128, u128>(ct) };
847+
match f128::classify_bits(bits) {
848+
FpCategory::Nan => {
849+
panic!("const-eval error: cannot use f128::to_bits on a NaN")
850+
}
851+
FpCategory::Subnormal => {
852+
panic!("const-eval error: cannot use f128::to_bits on a subnormal number")
853+
}
854+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => bits,
855+
}
856+
}
857+
858+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
859+
fn rt_f128_to_u128(x: f128) -> u128 {
860+
// SAFETY: `u128` is a plain old datatype so we can always... uh...
861+
// ...look, just pretend you forgot what you just read.
862+
// Stability concerns.
863+
unsafe { mem::transmute(x) }
864+
}
865+
intrinsics::const_eval_select((self,), ct_f128_to_u128, rt_f128_to_u128)
817866
}
818867

819868
/// Raw transmutation from `u128`.
@@ -858,11 +907,56 @@ impl f128 {
858907
#[inline]
859908
#[must_use]
860909
#[unstable(feature = "f128", issue = "116909")]
861-
pub fn from_bits(v: u128) -> Self {
862-
// SAFETY: `u128 is a plain old datatype so we can always... uh...
863-
// ...look, just pretend you forgot what you just read.
864-
// Stability concerns.
865-
unsafe { mem::transmute(v) }
910+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
911+
pub const fn from_bits(v: u128) -> Self {
912+
// It turns out the safety issues with sNaN were overblown! Hooray!
913+
// SAFETY: `u128` is a plain old datatype so we can always transmute from it
914+
// ...sorta.
915+
//
916+
// It turns out that at runtime, it is possible for a floating point number
917+
// to be subject to floating point modes that alter nonzero subnormal numbers
918+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
919+
// This is not a problem usually, but at least one tier2 platform for Rust
920+
// actually exhibits this behavior by default: thumbv7neon
921+
// aka "the Neon FPU in AArch32 state"
922+
//
923+
// In addition, on x86 targets with SSE or SSE2 disabled and the x87 FPU enabled,
924+
// i.e. not soft-float, the way Rust does parameter passing can actually alter
925+
// a number that is "not infinity" to have the same exponent as infinity,
926+
// in a slightly unpredictable manner.
927+
//
928+
// And, of course evaluating to a NaN value is fairly nondeterministic.
929+
// More precisely: when NaN should be returned is knowable, but which NaN?
930+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
931+
// This function, however, allows observing the bitstring of a NaN,
932+
// thus introspection on CTFE.
933+
//
934+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
935+
// reject any of these possible situations from happening.
936+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
937+
const fn ct_u128_to_f128(ct: u128) -> f128 {
938+
match f128::classify_bits(ct) {
939+
FpCategory::Subnormal => {
940+
panic!("const-eval error: cannot use f128::from_bits on a subnormal number")
941+
}
942+
FpCategory::Nan => {
943+
panic!("const-eval error: cannot use f128::from_bits on NaN")
944+
}
945+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => {
946+
// SAFETY: It's not a frumious number
947+
unsafe { mem::transmute::<u128, f128>(ct) }
948+
}
949+
}
950+
}
951+
952+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
953+
fn rt_u128_to_f128(x: u128) -> f128 {
954+
// SAFETY: `u128` is a plain old datatype so we can always... uh...
955+
// ...look, just pretend you forgot what you just read.
956+
// Stability concerns.
957+
unsafe { mem::transmute(x) }
958+
}
959+
intrinsics::const_eval_select((v,), ct_u128_to_f128, rt_u128_to_f128)
866960
}
867961

868962
/// Return the memory representation of this floating point number as a byte array in
@@ -885,8 +979,9 @@ impl f128 {
885979
/// ```
886980
#[inline]
887981
#[unstable(feature = "f128", issue = "116909")]
982+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
888983
#[must_use = "this returns the result of the operation, without modifying the original"]
889-
pub fn to_be_bytes(self) -> [u8; 16] {
984+
pub const fn to_be_bytes(self) -> [u8; 16] {
890985
self.to_bits().to_be_bytes()
891986
}
892987

@@ -910,8 +1005,9 @@ impl f128 {
9101005
/// ```
9111006
#[inline]
9121007
#[unstable(feature = "f128", issue = "116909")]
1008+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
9131009
#[must_use = "this returns the result of the operation, without modifying the original"]
914-
pub fn to_le_bytes(self) -> [u8; 16] {
1010+
pub const fn to_le_bytes(self) -> [u8; 16] {
9151011
self.to_bits().to_le_bytes()
9161012
}
9171013

@@ -946,8 +1042,9 @@ impl f128 {
9461042
/// ```
9471043
#[inline]
9481044
#[unstable(feature = "f128", issue = "116909")]
1045+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
9491046
#[must_use = "this returns the result of the operation, without modifying the original"]
950-
pub fn to_ne_bytes(self) -> [u8; 16] {
1047+
pub const fn to_ne_bytes(self) -> [u8; 16] {
9511048
self.to_bits().to_ne_bytes()
9521049
}
9531050

@@ -973,7 +1070,8 @@ impl f128 {
9731070
#[inline]
9741071
#[must_use]
9751072
#[unstable(feature = "f128", issue = "116909")]
976-
pub fn from_be_bytes(bytes: [u8; 16]) -> Self {
1073+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1074+
pub const fn from_be_bytes(bytes: [u8; 16]) -> Self {
9771075
Self::from_bits(u128::from_be_bytes(bytes))
9781076
}
9791077

@@ -999,7 +1097,8 @@ impl f128 {
9991097
#[inline]
10001098
#[must_use]
10011099
#[unstable(feature = "f128", issue = "116909")]
1002-
pub fn from_le_bytes(bytes: [u8; 16]) -> Self {
1100+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1101+
pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
10031102
Self::from_bits(u128::from_le_bytes(bytes))
10041103
}
10051104

@@ -1035,7 +1134,8 @@ impl f128 {
10351134
#[inline]
10361135
#[must_use]
10371136
#[unstable(feature = "f128", issue = "116909")]
1038-
pub fn from_ne_bytes(bytes: [u8; 16]) -> Self {
1137+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1138+
pub const fn from_ne_bytes(bytes: [u8; 16]) -> Self {
10391139
Self::from_bits(u128::from_ne_bytes(bytes))
10401140
}
10411141

library/core/src/num/f16.rs

+116-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

@@ -792,12 +794,59 @@ impl f16 {
792794
/// ```
793795
#[inline]
794796
#[unstable(feature = "f16", issue = "116909")]
797+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
795798
#[must_use = "this returns the result of the operation, without modifying the original"]
796-
pub fn to_bits(self) -> u16 {
797-
// SAFETY: `u16` is a plain old datatype so we can always... uh...
798-
// ...look, just pretend you forgot what you just read.
799-
// Stability concerns.
800-
unsafe { mem::transmute(self) }
799+
pub const fn to_bits(self) -> u16 {
800+
// SAFETY: `u16` is a plain old datatype so we can always transmute to it.
801+
// ...sorta.
802+
//
803+
// It turns out that at runtime, it is possible for a floating point number
804+
// to be subject to a floating point mode that alters nonzero subnormal numbers
805+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
806+
// This is not a problem per se, but at least one tier2 platform for Rust
807+
// actually exhibits this behavior by default.
808+
//
809+
// In addition, on x86 targets with SSE or SSE2 disabled and the x87 FPU enabled,
810+
// i.e. not soft-float, the way Rust does parameter passing can actually alter
811+
// a number that is "not infinity" to have the same exponent as infinity,
812+
// in a slightly unpredictable manner.
813+
//
814+
// And, of course evaluating to a NaN value is fairly nondeterministic.
815+
// More precisely: when NaN should be returned is knowable, but which NaN?
816+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
817+
// This function, however, allows observing the bitstring of a NaN,
818+
// thus introspection on CTFE.
819+
//
820+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
821+
// we reject any of these possible situations from happening.
822+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
823+
const fn ct_f16_to_u16(ct: f16) -> u16 {
824+
// FIXME(f16_f128): we should use `.classify()` like `f32` and `f64`, but we don't
825+
// yet want to rely on that on all platforms (ABI issues). So just classify the
826+
// bits instead.
827+
828+
// SAFETY: this direction is a POD transmutation. We just can't return it unless
829+
// it is normal, infinite, or zero.
830+
let bits = unsafe { mem::transmute::<f16, u16>(ct) };
831+
match f16::classify_bits(bits) {
832+
FpCategory::Nan => {
833+
panic!("const-eval error: cannot use f16::to_bits on a NaN")
834+
}
835+
FpCategory::Subnormal => {
836+
panic!("const-eval error: cannot use f16::to_bits on a subnormal number")
837+
}
838+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => bits,
839+
}
840+
}
841+
842+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
843+
fn rt_f16_to_u16(x: f16) -> u16 {
844+
// SAFETY: `u16` is a plain old datatype so we can always... uh...
845+
// ...look, just pretend you forgot what you just read.
846+
// Stability concerns.
847+
unsafe { mem::transmute(x) }
848+
}
849+
intrinsics::const_eval_select((self,), ct_f16_to_u16, rt_f16_to_u16)
801850
}
802851

803852
/// Raw transmutation from `u16`.
@@ -841,11 +890,56 @@ impl f16 {
841890
#[inline]
842891
#[must_use]
843892
#[unstable(feature = "f16", issue = "116909")]
844-
pub fn from_bits(v: u16) -> Self {
845-
// SAFETY: `u16` is a plain old datatype so we can always... uh...
846-
// ...look, just pretend you forgot what you just read.
847-
// Stability concerns.
848-
unsafe { mem::transmute(v) }
893+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
894+
pub const fn from_bits(v: u16) -> Self {
895+
// It turns out the safety issues with sNaN were overblown! Hooray!
896+
// SAFETY: `u16` is a plain old datatype so we can always transmute from it
897+
// ...sorta.
898+
//
899+
// It turns out that at runtime, it is possible for a floating point number
900+
// to be subject to floating point modes that alter nonzero subnormal numbers
901+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
902+
// This is not a problem usually, but at least one tier2 platform for Rust
903+
// actually exhibits this behavior by default: thumbv7neon
904+
// aka "the Neon FPU in AArch32 state"
905+
//
906+
// In addition, on x86 targets with SSE or SSE2 disabled and the x87 FPU enabled,
907+
// i.e. not soft-float, the way Rust does parameter passing can actually alter
908+
// a number that is "not infinity" to have the same exponent as infinity,
909+
// in a slightly unpredictable manner.
910+
//
911+
// And, of course evaluating to a NaN value is fairly nondeterministic.
912+
// More precisely: when NaN should be returned is knowable, but which NaN?
913+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
914+
// This function, however, allows observing the bitstring of a NaN,
915+
// thus introspection on CTFE.
916+
//
917+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
918+
// reject any of these possible situations from happening.
919+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
920+
const fn ct_u16_to_f16(ct: u16) -> f16 {
921+
match f16::classify_bits(ct) {
922+
FpCategory::Subnormal => {
923+
panic!("const-eval error: cannot use f16::from_bits on a subnormal number")
924+
}
925+
FpCategory::Nan => {
926+
panic!("const-eval error: cannot use f16::from_bits on NaN")
927+
}
928+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => {
929+
// SAFETY: It's not a frumious number
930+
unsafe { mem::transmute::<u16, f16>(ct) }
931+
}
932+
}
933+
}
934+
935+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
936+
fn rt_u16_to_f16(x: u16) -> f16 {
937+
// SAFETY: `u16` is a plain old datatype so we can always... uh...
938+
// ...look, just pretend you forgot what you just read.
939+
// Stability concerns.
940+
unsafe { mem::transmute(x) }
941+
}
942+
intrinsics::const_eval_select((v,), ct_u16_to_f16, rt_u16_to_f16)
849943
}
850944

851945
/// Return the memory representation of this floating point number as a byte array in
@@ -864,8 +958,9 @@ impl f16 {
864958
/// ```
865959
#[inline]
866960
#[unstable(feature = "f16", issue = "116909")]
961+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
867962
#[must_use = "this returns the result of the operation, without modifying the original"]
868-
pub fn to_be_bytes(self) -> [u8; 2] {
963+
pub const fn to_be_bytes(self) -> [u8; 2] {
869964
self.to_bits().to_be_bytes()
870965
}
871966

@@ -885,8 +980,9 @@ impl f16 {
885980
/// ```
886981
#[inline]
887982
#[unstable(feature = "f16", issue = "116909")]
983+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
888984
#[must_use = "this returns the result of the operation, without modifying the original"]
889-
pub fn to_le_bytes(self) -> [u8; 2] {
985+
pub const fn to_le_bytes(self) -> [u8; 2] {
890986
self.to_bits().to_le_bytes()
891987
}
892988

@@ -919,8 +1015,9 @@ impl f16 {
9191015
/// ```
9201016
#[inline]
9211017
#[unstable(feature = "f16", issue = "116909")]
1018+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
9221019
#[must_use = "this returns the result of the operation, without modifying the original"]
923-
pub fn to_ne_bytes(self) -> [u8; 2] {
1020+
pub const fn to_ne_bytes(self) -> [u8; 2] {
9241021
self.to_bits().to_ne_bytes()
9251022
}
9261023

@@ -942,7 +1039,8 @@ impl f16 {
9421039
#[inline]
9431040
#[must_use]
9441041
#[unstable(feature = "f16", issue = "116909")]
945-
pub fn from_be_bytes(bytes: [u8; 2]) -> Self {
1042+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1043+
pub const fn from_be_bytes(bytes: [u8; 2]) -> Self {
9461044
Self::from_bits(u16::from_be_bytes(bytes))
9471045
}
9481046

@@ -964,7 +1062,8 @@ impl f16 {
9641062
#[inline]
9651063
#[must_use]
9661064
#[unstable(feature = "f16", issue = "116909")]
967-
pub fn from_le_bytes(bytes: [u8; 2]) -> Self {
1065+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1066+
pub const fn from_le_bytes(bytes: [u8; 2]) -> Self {
9681067
Self::from_bits(u16::from_le_bytes(bytes))
9691068
}
9701069

@@ -997,7 +1096,8 @@ impl f16 {
9971096
#[inline]
9981097
#[must_use]
9991098
#[unstable(feature = "f16", issue = "116909")]
1000-
pub fn from_ne_bytes(bytes: [u8; 2]) -> Self {
1099+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1100+
pub const fn from_ne_bytes(bytes: [u8; 2]) -> Self {
10011101
Self::from_bits(u16::from_ne_bytes(bytes))
10021102
}
10031103

0 commit comments

Comments
 (0)