Skip to content

Commit b65daa7

Browse files
Test new floating point bit casts
1 parent 23e08e2 commit b65daa7

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// -Zmir-opt-level=0
2+
// run-pass
3+
4+
#![feature(const_panic)]
5+
#![feature(const_float_bits_conv)]
6+
#![feature(const_float_classify)]
7+
8+
// Don't promote
9+
const fn nop<T>(x: T) -> T { x }
10+
11+
macro_rules! const_assert {
12+
($a:expr) => {
13+
{
14+
const _: () = assert!($a);
15+
assert!(nop($a));
16+
}
17+
};
18+
($a:expr, $b:expr) => {
19+
{
20+
const _: () = assert!($a == $b);
21+
assert_eq!(nop($a), nop($b));
22+
}
23+
};
24+
}
25+
26+
fn f32() {
27+
const_assert!((1f32).to_bits(), 0x3f800000);
28+
const_assert!(u32::from_be_bytes(1f32.to_be_bytes()), 0x3f800000);
29+
const_assert!((12.5f32).to_bits(), 0x41480000);
30+
const_assert!(u32::from_le_bytes(12.5f32.to_le_bytes()), 0x41480000);
31+
const_assert!((1337f32).to_bits(), 0x44a72000);
32+
const_assert!(u32::from_ne_bytes(1337f32.to_ne_bytes()), 0x44a72000);
33+
const_assert!((-14.25f32).to_bits(), 0xc1640000);
34+
const_assert!(f32::from_bits(0x3f800000), 1.0);
35+
const_assert!(f32::from_be_bytes(0x3f800000u32.to_be_bytes()), 1.0);
36+
const_assert!(f32::from_bits(0x41480000), 12.5);
37+
const_assert!(f32::from_le_bytes(0x41480000u32.to_le_bytes()), 12.5);
38+
const_assert!(f32::from_bits(0x44a72000), 1337.0);
39+
const_assert!(f32::from_ne_bytes(0x44a72000u32.to_ne_bytes()), 1337.0);
40+
const_assert!(f32::from_bits(0xc1640000), -14.25);
41+
42+
// Check that NaNs roundtrip their bits regardless of signalingness
43+
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
44+
const MASKED_NAN1: u32 = f32::NAN.to_bits() ^ 0x002A_AAAA;
45+
const MASKED_NAN2: u32 = f32::NAN.to_bits() ^ 0x0055_5555;
46+
47+
const_assert!(f32::from_bits(MASKED_NAN1).is_nan());
48+
const_assert!(f32::from_bits(MASKED_NAN1).is_nan());
49+
50+
// LLVM does not guarantee that loads and stores of NaNs preserve their exact bit pattern.
51+
// In practice, this seems to only cause a problem on x86, since the most widely used calling
52+
// convention mandates that floating point values are returned on the x87 FPU stack. See #73328.
53+
if !cfg!(target_arch = "x86") {
54+
const_assert!(f32::from_bits(MASKED_NAN1).to_bits(), MASKED_NAN1);
55+
const_assert!(f32::from_bits(MASKED_NAN2).to_bits(), MASKED_NAN2);
56+
}
57+
}
58+
59+
fn f64() {
60+
const_assert!((1f64).to_bits(), 0x3ff0000000000000);
61+
const_assert!(u64::from_be_bytes(1f64.to_be_bytes()), 0x3ff0000000000000);
62+
const_assert!((12.5f64).to_bits(), 0x4029000000000000);
63+
const_assert!(u64::from_le_bytes(12.5f64.to_le_bytes()), 0x4029000000000000);
64+
const_assert!((1337f64).to_bits(), 0x4094e40000000000);
65+
const_assert!(u64::from_ne_bytes(1337f64.to_ne_bytes()), 0x4094e40000000000);
66+
const_assert!((-14.25f64).to_bits(), 0xc02c800000000000);
67+
const_assert!(f64::from_bits(0x3ff0000000000000), 1.0);
68+
const_assert!(f64::from_be_bytes(0x3ff0000000000000u64.to_be_bytes()), 1.0);
69+
const_assert!(f64::from_bits(0x4029000000000000), 12.5);
70+
const_assert!(f64::from_le_bytes(0x4029000000000000u64.to_le_bytes()), 12.5);
71+
const_assert!(f64::from_bits(0x4094e40000000000), 1337.0);
72+
const_assert!(f64::from_ne_bytes(0x4094e40000000000u64.to_ne_bytes()), 1337.0);
73+
const_assert!(f64::from_bits(0xc02c800000000000), -14.25);
74+
75+
// Check that NaNs roundtrip their bits regardless of signalingness
76+
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
77+
const MASKED_NAN1: u64 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
78+
const MASKED_NAN2: u64 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
79+
80+
const_assert!(f64::from_bits(MASKED_NAN1).is_nan());
81+
const_assert!(f64::from_bits(MASKED_NAN1).is_nan());
82+
83+
// See comment above.
84+
if !cfg!(target_arch = "x86") {
85+
const_assert!(f64::from_bits(MASKED_NAN1).to_bits(), MASKED_NAN1);
86+
const_assert!(f64::from_bits(MASKED_NAN2).to_bits(), MASKED_NAN2);
87+
}
88+
}
89+
90+
fn main() {
91+
f32();
92+
f64();
93+
}
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// -Zmir-opt-level=0
2+
// run-pass
3+
4+
#![feature(const_panic)]
5+
#![feature(const_float_bits_conv)]
6+
#![feature(const_float_classify)]
7+
#![feature(const_trait_impl)]
8+
#![allow(incomplete_features)]
9+
10+
// Don't promote
11+
const fn nop<T>(x: T) -> T { x }
12+
13+
macro_rules! const_assert {
14+
($a:expr, $b:expr) => {
15+
{
16+
const _: () = assert!($a == $b);
17+
assert_eq!(nop($a), nop($b));
18+
}
19+
};
20+
}
21+
22+
macro_rules! suite {
23+
( $( $tt:tt )* ) => {
24+
fn f32() {
25+
suite_inner!(f32 $($tt)*);
26+
}
27+
28+
fn f64() {
29+
suite_inner!(f64 $($tt)*);
30+
}
31+
}
32+
33+
}
34+
35+
macro_rules! suite_inner {
36+
(
37+
$ty:ident [$( $fn:ident ),*]
38+
$val:expr => [$($out:ident),*]
39+
40+
$( $tail:tt )*
41+
) => {
42+
$( const_assert!($ty::$fn($val), $out); )*
43+
suite_inner!($ty [$($fn),*] $($tail)*)
44+
};
45+
46+
( $ty:ident [$( $fn:ident ),*]) => {};
47+
}
48+
49+
#[derive(Debug)]
50+
struct NonDet;
51+
52+
impl const PartialEq<NonDet> for bool {
53+
fn eq(&self, _: &NonDet) -> bool {
54+
true
55+
}
56+
}
57+
58+
// The result of the `is_sign` methods are not checked for correctness, since LLVM does not
59+
// guarantee anything about the signedness of NaNs. See
60+
// https://github.com/rust-lang/rust/issues/55131.
61+
62+
suite! {
63+
[is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]
64+
-0.0 / 0.0 => [ true, false, false, false, NonDet, NonDet]
65+
0.0 / 0.0 => [ true, false, false, false, NonDet, NonDet]
66+
1.0 => [ false, false, true, true, true, false]
67+
-1.0 => [ false, false, true, true, false, true]
68+
0.0 => [ false, false, true, false, true, false]
69+
-0.0 => [ false, false, true, false, false, true]
70+
1.0 / 0.0 => [ false, true, false, false, true, false]
71+
-1.0 / 0.0 => [ false, true, false, false, false, true]
72+
}
73+
74+
fn main() {
75+
f32();
76+
f64();
77+
}

0 commit comments

Comments
 (0)