Skip to content

Commit 37ece9c

Browse files
authored
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 #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)]`
2 parents c4a254f + 3387a58 commit 37ece9c

File tree

7 files changed

+23
-4
lines changed

7 files changed

+23
-4
lines changed

library/compiler-builtins/compiler-builtins/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(repr_simd)]
1212
#![feature(macro_metavar_expr_concat)]
1313
#![feature(rustc_attrs)]
14+
#![feature(float_bits_const)]
1415
#![cfg_attr(f16_enabled, feature(f16))]
1516
#![cfg_attr(f128_enabled, feature(f128))]
1617
#![no_builtins]

library/core/src/num/f128.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ impl f128 {
154154
#[unstable(feature = "f128", issue = "116909")]
155155
pub const RADIX: u32 = 2;
156156

157+
/// The size of this float type in bits.
158+
// #[unstable(feature = "f128", issue = "116909")]
159+
#[unstable(feature = "float_bits_const", issue = "151073")]
160+
pub const BITS: u32 = 128;
161+
157162
/// Number of significant digits in base 2.
158163
///
159164
/// Note that the size of the mantissa in the bitwise representation is one

library/core/src/num/f16.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ impl f16 {
148148
#[unstable(feature = "f16", issue = "116909")]
149149
pub const RADIX: u32 = 2;
150150

151+
/// The size of this float type in bits.
152+
// #[unstable(feature = "f16", issue = "116909")]
153+
#[unstable(feature = "float_bits_const", issue = "151073")]
154+
pub const BITS: u32 = 16;
155+
151156
/// Number of significant digits in base 2.
152157
///
153158
/// Note that the size of the mantissa in the bitwise representation is one

library/core/src/num/f32.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ impl f32 {
398398
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
399399
pub const RADIX: u32 = 2;
400400

401+
/// The size of this float type in bits.
402+
#[unstable(feature = "float_bits_const", issue = "151073")]
403+
pub const BITS: u32 = 32;
404+
401405
/// Number of significant digits in base 2.
402406
///
403407
/// Note that the size of the mantissa in the bitwise representation is one

library/core/src/num/f64.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ impl f64 {
398398
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
399399
pub const RADIX: u32 = 2;
400400

401+
/// The size of this float type in bits.
402+
#[unstable(feature = "float_bits_const", issue = "151073")]
403+
pub const BITS: u32 = 64;
404+
401405
/// Number of significant digits in base 2.
402406
///
403407
/// Note that the size of the mantissa in the bitwise representation is one

src/tools/miri/tests/pass/float.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ macro_rules! test_ftoi_itof {
353353
assert_itof(i, f, msg);
354354
}
355355

356-
let fbits = <$fty>::BITS;
356+
let fbits = <$fty as Float>::BITS;
357357
let fsig_bits = <$fty>::SIGNIFICAND_BITS;
358358
let ibits = <$ity>::BITS;
359359
let imax: $ity = <$ity>::MAX;
@@ -528,9 +528,9 @@ macro_rules! test_ftof {
528528
assert!((<$f1>::NAN as $f2).is_nan(), "{} -> {} nan", stringify!($f1), stringify!($f2));
529529

530530
let min_sub_casted = <$f1>::from_bits(0x1) as $f2;
531-
let min_neg_sub_casted = <$f1>::from_bits(0x1 | 1 << (<$f1>::BITS - 1)) as $f2;
531+
let min_neg_sub_casted = <$f1>::from_bits(0x1 | 1 << (<$f1 as Float>::BITS - 1)) as $f2;
532532

533-
if <$f1>::BITS > <$f2>::BITS {
533+
if <$f1 as Float>::BITS > <$f2 as Float>::BITS {
534534
assert_feq(<$f1>::MAX as $f2, <$f2>::INFINITY, "max -> inf");
535535
assert_feq(<$f1>::MIN as $f2, <$f2>::NEG_INFINITY, "max -> inf");
536536
assert_biteq(min_sub_casted, f2zero, "min subnormal -> 0.0");

src/tools/test-float-parse/src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ macro_rules! impl_float {
155155
const BITS: u32 = <$ity>::BITS;
156156
const MAN_BITS: u32 = Self::MANTISSA_DIGITS - 1;
157157
const MAN_MASK: Self::Int = (Self::Int::ONE << Self::MAN_BITS) - Self::Int::ONE;
158-
const SIGN_MASK: Self::Int = Self::Int::ONE << (Self::BITS-1);
158+
const SIGN_MASK: Self::Int = Self::Int::ONE << (<Self as Float>::BITS-1);
159159
fn from_bits(i: Self::Int) -> Self { Self::from_bits(i) }
160160
fn to_bits(self) -> Self::Int { self.to_bits() }
161161
fn constants() -> &'static Constants {

0 commit comments

Comments
 (0)