Rollup merge of #151109 - tyhdefu:float_bits_const, r=tgross35

fN::BITS constants for feature float_bits_const

Also enables the feature for compiler_builtins as otherwise this causes a warning and conflicts with the Float extension trait.

---
Implementation for rust-lang/rust#151073

Feature flag: `#![feature(float_bits_const)]`

Note that this is likely to conflict with some extension traits, as it has with compiler builtins. However, assuming correct values for the constants, they are either `u32`, the same type, which should not cause a problem (as shown by enabling the feature for compiler_builtins), or a different type (e.g. `usize`), which should cause a compiler error. Either way this should never change behaviour unless the extension trait implemented an incorrect value.

Also note that it doesn't seem to be possible to put multiple unstable attributes on an item, so `f128::BITS` and `f16::BITS` are gated behind the feature flags for those primitives, rather than `#![feature(float_bits_const)]`
This commit is contained in:
Stuart Cook 2026-02-03 21:58:39 +11:00 committed by GitHub
commit 37ece9cf9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 23 additions and 4 deletions

View file

@ -11,6 +11,7 @@
#![feature(repr_simd)]
#![feature(macro_metavar_expr_concat)]
#![feature(rustc_attrs)]
#![feature(float_bits_const)]
#![cfg_attr(f16_enabled, feature(f16))]
#![cfg_attr(f128_enabled, feature(f128))]
#![no_builtins]

View file

@ -154,6 +154,11 @@ impl f128 {
#[unstable(feature = "f128", issue = "116909")]
pub const RADIX: u32 = 2;
/// The size of this float type in bits.
// #[unstable(feature = "f128", issue = "116909")]
#[unstable(feature = "float_bits_const", issue = "151073")]
pub const BITS: u32 = 128;
/// Number of significant digits in base 2.
///
/// Note that the size of the mantissa in the bitwise representation is one

View file

@ -148,6 +148,11 @@ impl f16 {
#[unstable(feature = "f16", issue = "116909")]
pub const RADIX: u32 = 2;
/// The size of this float type in bits.
// #[unstable(feature = "f16", issue = "116909")]
#[unstable(feature = "float_bits_const", issue = "151073")]
pub const BITS: u32 = 16;
/// Number of significant digits in base 2.
///
/// Note that the size of the mantissa in the bitwise representation is one

View file

@ -398,6 +398,10 @@ impl f32 {
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
pub const RADIX: u32 = 2;
/// The size of this float type in bits.
#[unstable(feature = "float_bits_const", issue = "151073")]
pub const BITS: u32 = 32;
/// Number of significant digits in base 2.
///
/// Note that the size of the mantissa in the bitwise representation is one

View file

@ -398,6 +398,10 @@ impl f64 {
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
pub const RADIX: u32 = 2;
/// The size of this float type in bits.
#[unstable(feature = "float_bits_const", issue = "151073")]
pub const BITS: u32 = 64;
/// Number of significant digits in base 2.
///
/// Note that the size of the mantissa in the bitwise representation is one

View file

@ -353,7 +353,7 @@ macro_rules! test_ftoi_itof {
assert_itof(i, f, msg);
}
let fbits = <$fty>::BITS;
let fbits = <$fty as Float>::BITS;
let fsig_bits = <$fty>::SIGNIFICAND_BITS;
let ibits = <$ity>::BITS;
let imax: $ity = <$ity>::MAX;
@ -528,9 +528,9 @@ macro_rules! test_ftof {
assert!((<$f1>::NAN as $f2).is_nan(), "{} -> {} nan", stringify!($f1), stringify!($f2));
let min_sub_casted = <$f1>::from_bits(0x1) as $f2;
let min_neg_sub_casted = <$f1>::from_bits(0x1 | 1 << (<$f1>::BITS - 1)) as $f2;
let min_neg_sub_casted = <$f1>::from_bits(0x1 | 1 << (<$f1 as Float>::BITS - 1)) as $f2;
if <$f1>::BITS > <$f2>::BITS {
if <$f1 as Float>::BITS > <$f2 as Float>::BITS {
assert_feq(<$f1>::MAX as $f2, <$f2>::INFINITY, "max -> inf");
assert_feq(<$f1>::MIN as $f2, <$f2>::NEG_INFINITY, "max -> inf");
assert_biteq(min_sub_casted, f2zero, "min subnormal -> 0.0");

View file

@ -155,7 +155,7 @@ macro_rules! impl_float {
const BITS: u32 = <$ity>::BITS;
const MAN_BITS: u32 = Self::MANTISSA_DIGITS - 1;
const MAN_MASK: Self::Int = (Self::Int::ONE << Self::MAN_BITS) - Self::Int::ONE;
const SIGN_MASK: Self::Int = Self::Int::ONE << (Self::BITS-1);
const SIGN_MASK: Self::Int = Self::Int::ONE << (<Self as Float>::BITS-1);
fn from_bits(i: Self::Int) -> Self { Self::from_bits(i) }
fn to_bits(self) -> Self::Int { self.to_bits() }
fn constants() -> &'static Constants {