Skip to content

Commit a1939c5

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 7f69ce2 commit a1939c5

File tree

2 files changed

+222
-32
lines changed

2 files changed

+222
-32
lines changed

Diff for: library/core/src/num/f128.rs

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

@@ -780,12 +782,54 @@ impl f128 {
780782
/// ```
781783
#[inline]
782784
#[unstable(feature = "f128", issue = "116909")]
785+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
783786
#[must_use = "this returns the result of the operation, without modifying the original"]
784-
pub fn to_bits(self) -> u128 {
785-
// SAFETY: `u128` is a plain old datatype so we can always... uh...
786-
// ...look, just pretend you forgot what you just read.
787-
// Stability concerns.
788-
unsafe { mem::transmute(self) }
787+
pub const fn to_bits(self) -> u128 {
788+
// SAFETY: `u128` is a plain old datatype so we can always transmute to it.
789+
// ...sorta.
790+
//
791+
// It turns out that at runtime, it is possible for a floating point number
792+
// to be subject to a floating point mode that alters nonzero subnormal numbers
793+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
794+
// This is not a problem per se, but at least one tier2 platform for Rust
795+
// actually exhibits this behavior by default.
796+
//
797+
// And, of course evaluating to a NaN value is fairly nondeterministic.
798+
// More precisely: when NaN should be returned is knowable, but which NaN?
799+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
800+
// This function, however, allows observing the bitstring of a NaN,
801+
// thus introspection on CTFE.
802+
//
803+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
804+
// we reject any of these possible situations from happening.
805+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
806+
const fn ct_f128_to_u128(ct: f128) -> u128 {
807+
// FIXME(f16_f128): we should use `.classify()` like `f32` and `f64`, but that
808+
// is not avaialble on all platforms (needs `netf2` and `unordtf2`). So classify
809+
// the bits instead.
810+
811+
// SAFETY: this direction is a POD transmutation. We just can't return it unless
812+
// it is normal, infinite, or zero.
813+
let bits = unsafe { mem::transmute::<f128, u128>(ct) };
814+
match f128::classify_bits(bits) {
815+
FpCategory::Nan => {
816+
panic!("const-eval error: cannot use f128::to_bits on a NaN")
817+
}
818+
FpCategory::Subnormal => {
819+
panic!("const-eval error: cannot use f128::to_bits on a subnormal number")
820+
}
821+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => bits,
822+
}
823+
}
824+
825+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
826+
fn rt_f128_to_u128(x: f128) -> u128 {
827+
// SAFETY: `u128` is a plain old datatype so we can always... uh...
828+
// ...look, just pretend you forgot what you just read.
829+
// Stability concerns.
830+
unsafe { mem::transmute(x) }
831+
}
832+
intrinsics::const_eval_select((self,), ct_f128_to_u128, rt_f128_to_u128)
789833
}
790834

791835
/// Raw transmutation from `u128`.
@@ -830,11 +874,56 @@ impl f128 {
830874
#[inline]
831875
#[must_use]
832876
#[unstable(feature = "f128", issue = "116909")]
833-
pub fn from_bits(v: u128) -> Self {
834-
// SAFETY: `u128 is a plain old datatype so we can always... uh...
835-
// ...look, just pretend you forgot what you just read.
836-
// Stability concerns.
837-
unsafe { mem::transmute(v) }
877+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
878+
pub const fn from_bits(v: u128) -> Self {
879+
// It turns out the safety issues with sNaN were overblown! Hooray!
880+
// SAFETY: `u128` is a plain old datatype so we can always transmute from it
881+
// ...sorta.
882+
//
883+
// It turns out that at runtime, it is possible for a floating point number
884+
// to be subject to floating point modes that alter nonzero subnormal numbers
885+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
886+
// This is not a problem usually, but at least one tier2 platform for Rust
887+
// actually exhibits this behavior by default: thumbv7neon
888+
// aka "the Neon FPU in AArch32 state"
889+
//
890+
// In addition, on x86 targets with SSE or SSE2 disabled and the x87 FPU enabled,
891+
// i.e. not soft-float, the way Rust does parameter passing can actually alter
892+
// a number that is "not infinity" to have the same exponent as infinity,
893+
// in a slightly unpredictable manner.
894+
//
895+
// And, of course evaluating to a NaN value is fairly nondeterministic.
896+
// More precisely: when NaN should be returned is knowable, but which NaN?
897+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
898+
// This function, however, allows observing the bitstring of a NaN,
899+
// thus introspection on CTFE.
900+
//
901+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
902+
// reject any of these possible situations from happening.
903+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
904+
const fn ct_u128_to_f128(ct: u128) -> f128 {
905+
match f128::classify_bits(ct) {
906+
FpCategory::Subnormal => {
907+
panic!("const-eval error: cannot use f128::from_bits on a subnormal number")
908+
}
909+
FpCategory::Nan => {
910+
panic!("const-eval error: cannot use f128::from_bits on NaN")
911+
}
912+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => {
913+
// SAFETY: It's not a frumious number
914+
unsafe { mem::transmute::<u128, f128>(ct) }
915+
}
916+
}
917+
}
918+
919+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
920+
fn rt_u128_to_f128(x: u128) -> f128 {
921+
// SAFETY: `u128` is a plain old datatype so we can always... uh...
922+
// ...look, just pretend you forgot what you just read.
923+
// Stability concerns.
924+
unsafe { mem::transmute(x) }
925+
}
926+
intrinsics::const_eval_select((v,), ct_u128_to_f128, rt_u128_to_f128)
838927
}
839928

840929
/// Return the memory representation of this floating point number as a byte array in
@@ -857,8 +946,9 @@ impl f128 {
857946
/// ```
858947
#[inline]
859948
#[unstable(feature = "f128", issue = "116909")]
949+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
860950
#[must_use = "this returns the result of the operation, without modifying the original"]
861-
pub fn to_be_bytes(self) -> [u8; 16] {
951+
pub const fn to_be_bytes(self) -> [u8; 16] {
862952
self.to_bits().to_be_bytes()
863953
}
864954

@@ -882,8 +972,9 @@ impl f128 {
882972
/// ```
883973
#[inline]
884974
#[unstable(feature = "f128", issue = "116909")]
975+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
885976
#[must_use = "this returns the result of the operation, without modifying the original"]
886-
pub fn to_le_bytes(self) -> [u8; 16] {
977+
pub const fn to_le_bytes(self) -> [u8; 16] {
887978
self.to_bits().to_le_bytes()
888979
}
889980

@@ -918,8 +1009,9 @@ impl f128 {
9181009
/// ```
9191010
#[inline]
9201011
#[unstable(feature = "f128", issue = "116909")]
1012+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
9211013
#[must_use = "this returns the result of the operation, without modifying the original"]
922-
pub fn to_ne_bytes(self) -> [u8; 16] {
1014+
pub const fn to_ne_bytes(self) -> [u8; 16] {
9231015
self.to_bits().to_ne_bytes()
9241016
}
9251017

@@ -945,7 +1037,8 @@ impl f128 {
9451037
#[inline]
9461038
#[must_use]
9471039
#[unstable(feature = "f128", issue = "116909")]
948-
pub fn from_be_bytes(bytes: [u8; 16]) -> Self {
1040+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1041+
pub const fn from_be_bytes(bytes: [u8; 16]) -> Self {
9491042
Self::from_bits(u128::from_be_bytes(bytes))
9501043
}
9511044

@@ -971,7 +1064,8 @@ impl f128 {
9711064
#[inline]
9721065
#[must_use]
9731066
#[unstable(feature = "f128", issue = "116909")]
974-
pub fn from_le_bytes(bytes: [u8; 16]) -> Self {
1067+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1068+
pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
9751069
Self::from_bits(u128::from_le_bytes(bytes))
9761070
}
9771071

@@ -1007,7 +1101,8 @@ impl f128 {
10071101
#[inline]
10081102
#[must_use]
10091103
#[unstable(feature = "f128", issue = "116909")]
1010-
pub fn from_ne_bytes(bytes: [u8; 16]) -> Self {
1104+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1105+
pub const fn from_ne_bytes(bytes: [u8; 16]) -> Self {
10111106
Self::from_bits(u128::from_ne_bytes(bytes))
10121107
}
10131108

Diff for: library/core/src/num/f16.rs

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

@@ -786,12 +788,54 @@ impl f16 {
786788
/// ```
787789
#[inline]
788790
#[unstable(feature = "f16", issue = "116909")]
791+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
789792
#[must_use = "this returns the result of the operation, without modifying the original"]
790-
pub fn to_bits(self) -> u16 {
791-
// SAFETY: `u16` is a plain old datatype so we can always... uh...
792-
// ...look, just pretend you forgot what you just read.
793-
// Stability concerns.
794-
unsafe { mem::transmute(self) }
793+
pub const fn to_bits(self) -> u16 {
794+
// SAFETY: `u16` is a plain old datatype so we can always transmute to it.
795+
// ...sorta.
796+
//
797+
// It turns out that at runtime, it is possible for a floating point number
798+
// to be subject to a floating point mode that alters nonzero subnormal numbers
799+
// to zero on reads and writes, aka "denormals are zero" and "flush to zero".
800+
// This is not a problem per se, but at least one tier2 platform for Rust
801+
// actually exhibits this behavior by default.
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
814+
// yet want to rely on that on all platforms (ABI issues). So just classify the
815+
// bits instead.
816+
817+
// SAFETY: this direction is a POD transmutation. We just can't return it unless
818+
// it is normal, infinite, or zero.
819+
let bits = unsafe { mem::transmute::<f16, u16>(ct) };
820+
match f16::classify_bits(bits) {
821+
FpCategory::Nan => {
822+
panic!("const-eval error: cannot use f16::to_bits on a NaN")
823+
}
824+
FpCategory::Subnormal => {
825+
panic!("const-eval error: cannot use f16::to_bits on a subnormal number")
826+
}
827+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => bits,
828+
}
829+
}
830+
831+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
832+
fn rt_f16_to_u16(x: f16) -> u16 {
833+
// SAFETY: `u16` is a plain old datatype so we can always... uh...
834+
// ...look, just pretend you forgot what you just read.
835+
// Stability concerns.
836+
unsafe { mem::transmute(x) }
837+
}
838+
intrinsics::const_eval_select((self,), ct_f16_to_u16, rt_f16_to_u16)
795839
}
796840

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

845934
/// Return the memory representation of this floating point number as a byte array in
@@ -858,8 +947,9 @@ impl f16 {
858947
/// ```
859948
#[inline]
860949
#[unstable(feature = "f16", issue = "116909")]
950+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
861951
#[must_use = "this returns the result of the operation, without modifying the original"]
862-
pub fn to_be_bytes(self) -> [u8; 2] {
952+
pub const fn to_be_bytes(self) -> [u8; 2] {
863953
self.to_bits().to_be_bytes()
864954
}
865955

@@ -879,8 +969,9 @@ impl f16 {
879969
/// ```
880970
#[inline]
881971
#[unstable(feature = "f16", issue = "116909")]
972+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
882973
#[must_use = "this returns the result of the operation, without modifying the original"]
883-
pub fn to_le_bytes(self) -> [u8; 2] {
974+
pub const fn to_le_bytes(self) -> [u8; 2] {
884975
self.to_bits().to_le_bytes()
885976
}
886977

@@ -913,8 +1004,9 @@ impl f16 {
9131004
/// ```
9141005
#[inline]
9151006
#[unstable(feature = "f16", issue = "116909")]
1007+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
9161008
#[must_use = "this returns the result of the operation, without modifying the original"]
917-
pub fn to_ne_bytes(self) -> [u8; 2] {
1009+
pub const fn to_ne_bytes(self) -> [u8; 2] {
9181010
self.to_bits().to_ne_bytes()
9191011
}
9201012

@@ -936,7 +1028,8 @@ impl f16 {
9361028
#[inline]
9371029
#[must_use]
9381030
#[unstable(feature = "f16", issue = "116909")]
939-
pub fn from_be_bytes(bytes: [u8; 2]) -> Self {
1031+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1032+
pub const fn from_be_bytes(bytes: [u8; 2]) -> Self {
9401033
Self::from_bits(u16::from_be_bytes(bytes))
9411034
}
9421035

@@ -958,7 +1051,8 @@ impl f16 {
9581051
#[inline]
9591052
#[must_use]
9601053
#[unstable(feature = "f16", issue = "116909")]
961-
pub fn from_le_bytes(bytes: [u8; 2]) -> Self {
1054+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1055+
pub const fn from_le_bytes(bytes: [u8; 2]) -> Self {
9621056
Self::from_bits(u16::from_le_bytes(bytes))
9631057
}
9641058

@@ -991,7 +1085,8 @@ impl f16 {
9911085
#[inline]
9921086
#[must_use]
9931087
#[unstable(feature = "f16", issue = "116909")]
994-
pub fn from_ne_bytes(bytes: [u8; 2]) -> Self {
1088+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1089+
pub const fn from_ne_bytes(bytes: [u8; 2]) -> Self {
9951090
Self::from_bits(u16::from_ne_bytes(bytes))
9961091
}
9971092

0 commit comments

Comments
 (0)