Skip to content

Commit 128b8db

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 c764619 commit 128b8db

File tree

2 files changed

+208
-32
lines changed

2 files changed

+208
-32
lines changed

library/core/src/num/f128.rs

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

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

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

819901
/// Return the memory representation of this floating point number as a byte array in
@@ -836,8 +918,9 @@ impl f128 {
836918
/// ```
837919
#[inline]
838920
#[unstable(feature = "f128", issue = "116909")]
921+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
839922
#[must_use = "this returns the result of the operation, without modifying the original"]
840-
pub fn to_be_bytes(self) -> [u8; 16] {
923+
pub const fn to_be_bytes(self) -> [u8; 16] {
841924
self.to_bits().to_be_bytes()
842925
}
843926

@@ -861,8 +944,9 @@ impl f128 {
861944
/// ```
862945
#[inline]
863946
#[unstable(feature = "f128", issue = "116909")]
947+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
864948
#[must_use = "this returns the result of the operation, without modifying the original"]
865-
pub fn to_le_bytes(self) -> [u8; 16] {
949+
pub const fn to_le_bytes(self) -> [u8; 16] {
866950
self.to_bits().to_le_bytes()
867951
}
868952

@@ -897,8 +981,9 @@ impl f128 {
897981
/// ```
898982
#[inline]
899983
#[unstable(feature = "f128", issue = "116909")]
984+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
900985
#[must_use = "this returns the result of the operation, without modifying the original"]
901-
pub fn to_ne_bytes(self) -> [u8; 16] {
986+
pub const fn to_ne_bytes(self) -> [u8; 16] {
902987
self.to_bits().to_ne_bytes()
903988
}
904989

@@ -924,7 +1009,8 @@ impl f128 {
9241009
#[inline]
9251010
#[must_use]
9261011
#[unstable(feature = "f128", issue = "116909")]
927-
pub fn from_be_bytes(bytes: [u8; 16]) -> Self {
1012+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1013+
pub const fn from_be_bytes(bytes: [u8; 16]) -> Self {
9281014
Self::from_bits(u128::from_be_bytes(bytes))
9291015
}
9301016

@@ -950,7 +1036,8 @@ impl f128 {
9501036
#[inline]
9511037
#[must_use]
9521038
#[unstable(feature = "f128", issue = "116909")]
953-
pub fn from_le_bytes(bytes: [u8; 16]) -> Self {
1039+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1040+
pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
9541041
Self::from_bits(u128::from_le_bytes(bytes))
9551042
}
9561043

@@ -986,7 +1073,8 @@ impl f128 {
9861073
#[inline]
9871074
#[must_use]
9881075
#[unstable(feature = "f128", issue = "116909")]
989-
pub fn from_ne_bytes(bytes: [u8; 16]) -> Self {
1076+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1077+
pub const fn from_ne_bytes(bytes: [u8; 16]) -> Self {
9901078
Self::from_bits(u128::from_ne_bytes(bytes))
9911079
}
9921080

library/core/src/num/f16.rs

+104-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,52 @@ 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+
//
801+
// And, of course evaluating to a NaN value is fairly nondeterministic.
802+
// More precisely: when NaN should be returned is knowable, but which NaN?
803+
// So far that's defined by a combination of LLVM and the CPU, not Rust.
804+
// This function, however, allows observing the bitstring of a NaN,
805+
// thus introspection on CTFE.
806+
//
807+
// In order to preserve, at least for the moment, const-to-runtime equivalence,
808+
// we reject any of these possible situations from happening.
809+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
810+
const fn ct_f16_to_u16(ct: f16) -> u16 {
811+
// FIXME(f16_f128): we should use `.classify()` like `f32` and `f64`, but we don't
812+
// yet want to rely on that on all platforms (ABI issues). So just classify the
813+
// bits instead.
814+
815+
// SAFETY: this direction is a POD transmutation. We just can't return it unless
816+
// it is normal, infinite, or zero.
817+
let bits = unsafe { mem::transmute::<f16, u16>(ct) };
818+
match f16::classify_bits(bits) {
819+
FpCategory::Nan => {
820+
panic!("const-eval error: cannot use f16::to_bits on a NaN")
821+
}
822+
FpCategory::Subnormal => {
823+
panic!("const-eval error: cannot use f16::to_bits on a subnormal number")
824+
}
825+
FpCategory::Infinite | FpCategory::Normal | FpCategory::Zero => bits,
826+
}
827+
}
828+
829+
#[inline(always)] // See https://github.com/rust-lang/compiler-builtins/issues/491
830+
fn rt_f16_to_u16(x: f16) -> u16 {
831+
// SAFETY: `u16` is a plain old datatype so we can always... uh...
832+
// ...look, just pretend you forgot what you just read.
833+
// Stability concerns.
834+
unsafe { mem::transmute(x) }
835+
}
836+
intrinsics::const_eval_select((self,), ct_f16_to_u16, rt_f16_to_u16)
795837
}
796838

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

845927
/// Return the memory representation of this floating point number as a byte array in
@@ -858,8 +940,9 @@ impl f16 {
858940
/// ```
859941
#[inline]
860942
#[unstable(feature = "f16", issue = "116909")]
943+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
861944
#[must_use = "this returns the result of the operation, without modifying the original"]
862-
pub fn to_be_bytes(self) -> [u8; 2] {
945+
pub const fn to_be_bytes(self) -> [u8; 2] {
863946
self.to_bits().to_be_bytes()
864947
}
865948

@@ -879,8 +962,9 @@ impl f16 {
879962
/// ```
880963
#[inline]
881964
#[unstable(feature = "f16", issue = "116909")]
965+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
882966
#[must_use = "this returns the result of the operation, without modifying the original"]
883-
pub fn to_le_bytes(self) -> [u8; 2] {
967+
pub const fn to_le_bytes(self) -> [u8; 2] {
884968
self.to_bits().to_le_bytes()
885969
}
886970

@@ -913,8 +997,9 @@ impl f16 {
913997
/// ```
914998
#[inline]
915999
#[unstable(feature = "f16", issue = "116909")]
1000+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
9161001
#[must_use = "this returns the result of the operation, without modifying the original"]
917-
pub fn to_ne_bytes(self) -> [u8; 2] {
1002+
pub const fn to_ne_bytes(self) -> [u8; 2] {
9181003
self.to_bits().to_ne_bytes()
9191004
}
9201005

@@ -936,7 +1021,8 @@ impl f16 {
9361021
#[inline]
9371022
#[must_use]
9381023
#[unstable(feature = "f16", issue = "116909")]
939-
pub fn from_be_bytes(bytes: [u8; 2]) -> Self {
1024+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1025+
pub const fn from_be_bytes(bytes: [u8; 2]) -> Self {
9401026
Self::from_bits(u16::from_be_bytes(bytes))
9411027
}
9421028

@@ -958,7 +1044,8 @@ impl f16 {
9581044
#[inline]
9591045
#[must_use]
9601046
#[unstable(feature = "f16", issue = "116909")]
961-
pub fn from_le_bytes(bytes: [u8; 2]) -> Self {
1047+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1048+
pub const fn from_le_bytes(bytes: [u8; 2]) -> Self {
9621049
Self::from_bits(u16::from_le_bytes(bytes))
9631050
}
9641051

@@ -991,7 +1078,8 @@ impl f16 {
9911078
#[inline]
9921079
#[must_use]
9931080
#[unstable(feature = "f16", issue = "116909")]
994-
pub fn from_ne_bytes(bytes: [u8; 2]) -> Self {
1081+
#[rustc_const_unstable(feature = "const_float_bits_conv", issue = "72447")]
1082+
pub const fn from_ne_bytes(bytes: [u8; 2]) -> Self {
9951083
Self::from_bits(u16::from_ne_bytes(bytes))
9961084
}
9971085

0 commit comments

Comments
 (0)