Skip to content

Commit b657787

Browse files
authored
Rollup merge of rust-lang#129190 - rezwanahmedsami:master, r=tgross35
Added f16 and f128 to tests/ui/consts/const-float-bits-conv.rs Fixes rust-lang#129163
2 parents 178ccee + 803cbaf commit b657787

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

tests/ui/consts/const-float-bits-conv.rs

+64-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
#![feature(const_float_bits_conv)]
55
#![feature(const_float_classify)]
6+
#![feature(f16)]
7+
#![feature(f128)]
68
#![allow(unused_macro_rules)]
7-
89
// Don't promote
910
const fn nop<T>(x: T) -> T { x }
1011

@@ -28,6 +29,36 @@ fn has_broken_floats() -> bool {
2829
std::env::var("TARGET").is_ok_and(|v| v.contains("i586"))
2930
}
3031

32+
fn f16(){
33+
const_assert!((1f16).to_bits(), 0x3c00);
34+
const_assert!(u16::from_be_bytes(1f16.to_be_bytes()), 0x3c00);
35+
const_assert!((12.5f16).to_bits(), 0x4a40);
36+
const_assert!(u16::from_le_bytes(12.5f16.to_le_bytes()), 0x4a40);
37+
const_assert!((1337f16).to_bits(), 0x6539);
38+
const_assert!(u16::from_ne_bytes(1337f16.to_ne_bytes()), 0x6539);
39+
const_assert!((-14.25f16).to_bits(), 0xcb20);
40+
const_assert!(f16::from_bits(0x3c00), 1.0);
41+
const_assert!(f16::from_be_bytes(0x3c00u16.to_be_bytes()), 1.0);
42+
const_assert!(f16::from_bits(0x4a40), 12.5);
43+
const_assert!(f16::from_le_bytes(0x4a40u16.to_le_bytes()), 12.5);
44+
const_assert!(f16::from_bits(0x5be0), 252.0);
45+
const_assert!(f16::from_ne_bytes(0x5be0u16.to_ne_bytes()), 252.0);
46+
const_assert!(f16::from_bits(0xcb20), -14.25);
47+
48+
// Check that NaNs roundtrip their bits regardless of signalingness
49+
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
50+
// NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
51+
const QUIET_NAN: u16 = f16::NAN.to_bits() ^ 0x0155;
52+
const SIGNALING_NAN: u16 = f16::NAN.to_bits() ^ 0x02AA;
53+
54+
const_assert!(f16::from_bits(QUIET_NAN).is_nan());
55+
const_assert!(f16::from_bits(SIGNALING_NAN).is_nan());
56+
const_assert!(f16::from_bits(QUIET_NAN).to_bits(), QUIET_NAN);
57+
if !has_broken_floats() {
58+
const_assert!(f16::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN);
59+
}
60+
}
61+
3162
fn f32() {
3263
const_assert!((1f32).to_bits(), 0x3f800000);
3364
const_assert!(u32::from_be_bytes(1f32.to_be_bytes()), 0x3f800000);
@@ -88,7 +119,39 @@ fn f64() {
88119
}
89120
}
90121

122+
fn f128() {
123+
const_assert!((1f128).to_bits(), 0x3fff0000000000000000000000000000);
124+
const_assert!(u128::from_be_bytes(1f128.to_be_bytes()), 0x3fff0000000000000000000000000000);
125+
const_assert!((12.5f128).to_bits(), 0x40029000000000000000000000000000);
126+
const_assert!(u128::from_le_bytes(12.5f128.to_le_bytes()), 0x40029000000000000000000000000000);
127+
const_assert!((1337f128).to_bits(), 0x40094e40000000000000000000000000);
128+
const_assert!(u128::from_ne_bytes(1337f128.to_ne_bytes()), 0x40094e40000000000000000000000000);
129+
const_assert!((-14.25f128).to_bits(), 0xc002c800000000000000000000000000);
130+
const_assert!(f128::from_bits(0x3fff0000000000000000000000000000), 1.0);
131+
const_assert!(f128::from_be_bytes(0x3fff0000000000000000000000000000u128.to_be_bytes()), 1.0);
132+
const_assert!(f128::from_bits(0x40029000000000000000000000000000), 12.5);
133+
const_assert!(f128::from_le_bytes(0x40029000000000000000000000000000u128.to_le_bytes()), 12.5);
134+
const_assert!(f128::from_bits(0x40094e40000000000000000000000000), 1337.0);
135+
assert_eq!(f128::from_ne_bytes(0x40094e40000000000000000000000000u128.to_ne_bytes()), 1337.0);
136+
const_assert!(f128::from_bits(0xc002c800000000000000000000000000), -14.25);
137+
138+
// Check that NaNs roundtrip their bits regardless of signalingness
139+
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
140+
// NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
141+
const QUIET_NAN: u128 = f128::NAN.to_bits() | 0x0000_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA;
142+
const SIGNALING_NAN: u128 = f128::NAN.to_bits() ^ 0x0000_5555_5555_5555_5555_5555_5555_5555;
143+
144+
const_assert!(f128::from_bits(QUIET_NAN).is_nan());
145+
const_assert!(f128::from_bits(SIGNALING_NAN).is_nan());
146+
const_assert!(f128::from_bits(QUIET_NAN).to_bits(), QUIET_NAN);
147+
if !has_broken_floats() {
148+
const_assert!(f128::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN);
149+
}
150+
}
151+
91152
fn main() {
153+
f16();
92154
f32();
93155
f64();
156+
f128();
94157
}

0 commit comments

Comments
 (0)