Skip to content

Commit

Permalink
rust-lang#128598 rebase done
Browse files Browse the repository at this point in the history
  • Loading branch information
rezwanahmedsami committed Aug 17, 2024
1 parent 9e5bdb7 commit 0aa6075
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions tests/ui/consts/const-float-bits-conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ fn f16(){
const_assert!(f16::from_bits(0x5be0), 252.0);
const_assert!(f16::from_ne_bytes(0x5be0u16.to_ne_bytes()), 252.0);
const_assert!(f16::from_bits(0xcb20), -14.25);

// Check that NaNs roundtrip their bits regardless of signalingness
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
// NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
const QUIET_NAN: u16 = f16::NAN.to_bits() ^ 0x02AA;
const SIGNALING_NAN: u16 = 0x7eaa;

const_assert!(f16::from_bits(QUIET_NAN).is_nan());
const_assert!(f16::from_bits(SIGNALING_NAN).is_nan());
const_assert!(f16::from_bits(QUIET_NAN).to_bits(), QUIET_NAN);
if !has_broken_floats() {
const_assert!(f16::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN);
}
}

fn f32() {
Expand Down Expand Up @@ -106,23 +119,6 @@ fn f64() {
}
}

fn f128() {
const_assert!((1f128).to_bits(), 0x3fff0000000000000000000000000000);
const_assert!(u128::from_be_bytes(1f128.to_be_bytes()), 0x3fff0000000000000000000000000000);
const_assert!((12.5f128).to_bits(), 0x40029000000000000000000000000000);
const_assert!(u128::from_le_bytes(12.5f128.to_le_bytes()), 0x40029000000000000000000000000000);
const_assert!((1337f128).to_bits(), 0x40094e40000000000000000000000000);
const_assert!(u128::from_ne_bytes(1337f128.to_ne_bytes()), 0x40094e40000000000000000000000000);
const_assert!((-14.25f128).to_bits(), 0xc002c800000000000000000000000000);
const_assert!(f128::from_bits(0x3fff0000000000000000000000000000), 1.0);
const_assert!(f128::from_be_bytes(0x3fff0000000000000000000000000000u128.to_be_bytes()), 1.0);
const_assert!(f128::from_bits(0x40029000000000000000000000000000), 12.5);
const_assert!(f128::from_le_bytes(0x40029000000000000000000000000000u128.to_le_bytes()), 12.5);
const_assert!(f128::from_bits(0x40094e40000000000000000000000000), 1337.0);
const_assert!(f128::from_ne_bytes(0x40094e40000000000000000000000000u128.to_ne_bytes()), 1337.0);
const_assert!(f128::from_bits(0xc002c800000000000000000000000000), -14.25);
}

fn f128() {
const_assert!((1f128).to_bits(), 0x3fff0000000000000000000000000000);
const_assert!(u128::from_be_bytes(1f128.to_be_bytes()), 0x3fff0000000000000000000000000000);
Expand All @@ -138,6 +134,19 @@ fn f128() {
const_assert!(f128::from_bits(0x40094e40000000000000000000000000), 1337.0);
assert_eq!(f128::from_ne_bytes(0x40094e40000000000000000000000000u128.to_ne_bytes()), 1337.0);
const_assert!(f128::from_bits(0xc002c800000000000000000000000000), -14.25);

// Check that NaNs roundtrip their bits regardless of signalingness
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
// NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
const QUIET_NAN: u128 = f128::NAN.to_bits() | 0x0008_0000_0000_0000_0000_0000_0000_0000; // Set a non-zero mantissa bit
const SIGNALING_NAN: u128 = QUIET_NAN | 0x0000_0001_0000_0000_0000_0000_0000_0000; // Set the most significant mantissa bit

const_assert!(f128::from_bits(QUIET_NAN).is_nan());
const_assert!(f128::from_bits(SIGNALING_NAN).is_nan());
const_assert!(f128::from_bits(QUIET_NAN).to_bits(), QUIET_NAN);
if !has_broken_floats() {
const_assert!(f128::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN);
}
}

fn main() {
Expand Down

0 comments on commit 0aa6075

Please sign in to comment.