1 // compile-flags: -Zmir-opt-level=0
2 // run-pass
3 
4 #![feature(const_float_bits_conv)]
5 #![feature(const_float_classify)]
6 
7 // Don't promote
nop<T>(x: T) -> T8 const fn nop<T>(x: T) -> T { x }
9 
10 macro_rules! const_assert {
11     ($a:expr) => {
12         {
13             const _: () = assert!($a);
14             assert!(nop($a));
15         }
16     };
17     ($a:expr, $b:expr) => {
18         {
19             const _: () = assert!($a == $b);
20             assert_eq!(nop($a), nop($b));
21         }
22     };
23 }
24 
f32()25 fn f32() {
26     const_assert!((1f32).to_bits(), 0x3f800000);
27     const_assert!(u32::from_be_bytes(1f32.to_be_bytes()), 0x3f800000);
28     const_assert!((12.5f32).to_bits(), 0x41480000);
29     const_assert!(u32::from_le_bytes(12.5f32.to_le_bytes()), 0x41480000);
30     const_assert!((1337f32).to_bits(), 0x44a72000);
31     const_assert!(u32::from_ne_bytes(1337f32.to_ne_bytes()), 0x44a72000);
32     const_assert!((-14.25f32).to_bits(), 0xc1640000);
33     const_assert!(f32::from_bits(0x3f800000), 1.0);
34     const_assert!(f32::from_be_bytes(0x3f800000u32.to_be_bytes()), 1.0);
35     const_assert!(f32::from_bits(0x41480000), 12.5);
36     const_assert!(f32::from_le_bytes(0x41480000u32.to_le_bytes()), 12.5);
37     const_assert!(f32::from_bits(0x44a72000), 1337.0);
38     const_assert!(f32::from_ne_bytes(0x44a72000u32.to_ne_bytes()), 1337.0);
39     const_assert!(f32::from_bits(0xc1640000), -14.25);
40 
41     // Check that NaNs roundtrip their bits regardless of signalingness
42     // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
43     const MASKED_NAN1: u32 = f32::NAN.to_bits() ^ 0x002A_AAAA;
44     const MASKED_NAN2: u32 = f32::NAN.to_bits() ^ 0x0055_5555;
45 
46     const_assert!(f32::from_bits(MASKED_NAN1).is_nan());
47     const_assert!(f32::from_bits(MASKED_NAN1).is_nan());
48 
49     // LLVM does not guarantee that loads and stores of NaNs preserve their exact bit pattern.
50     // In practice, this seems to only cause a problem on x86, since the most widely used calling
51     // convention mandates that floating point values are returned on the x87 FPU stack. See #73328.
52     if !cfg!(target_arch = "x86") {
53         const_assert!(f32::from_bits(MASKED_NAN1).to_bits(), MASKED_NAN1);
54         const_assert!(f32::from_bits(MASKED_NAN2).to_bits(), MASKED_NAN2);
55     }
56 }
57 
f64()58 fn f64() {
59     const_assert!((1f64).to_bits(), 0x3ff0000000000000);
60     const_assert!(u64::from_be_bytes(1f64.to_be_bytes()), 0x3ff0000000000000);
61     const_assert!((12.5f64).to_bits(), 0x4029000000000000);
62     const_assert!(u64::from_le_bytes(12.5f64.to_le_bytes()), 0x4029000000000000);
63     const_assert!((1337f64).to_bits(), 0x4094e40000000000);
64     const_assert!(u64::from_ne_bytes(1337f64.to_ne_bytes()), 0x4094e40000000000);
65     const_assert!((-14.25f64).to_bits(), 0xc02c800000000000);
66     const_assert!(f64::from_bits(0x3ff0000000000000), 1.0);
67     const_assert!(f64::from_be_bytes(0x3ff0000000000000u64.to_be_bytes()), 1.0);
68     const_assert!(f64::from_bits(0x4029000000000000), 12.5);
69     const_assert!(f64::from_le_bytes(0x4029000000000000u64.to_le_bytes()), 12.5);
70     const_assert!(f64::from_bits(0x4094e40000000000), 1337.0);
71     const_assert!(f64::from_ne_bytes(0x4094e40000000000u64.to_ne_bytes()), 1337.0);
72     const_assert!(f64::from_bits(0xc02c800000000000), -14.25);
73 
74     // Check that NaNs roundtrip their bits regardless of signalingness
75     // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
76     const MASKED_NAN1: u64 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
77     const MASKED_NAN2: u64 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
78 
79     const_assert!(f64::from_bits(MASKED_NAN1).is_nan());
80     const_assert!(f64::from_bits(MASKED_NAN1).is_nan());
81 
82     // See comment above.
83     if !cfg!(target_arch = "x86") {
84         const_assert!(f64::from_bits(MASKED_NAN1).to_bits(), MASKED_NAN1);
85         const_assert!(f64::from_bits(MASKED_NAN2).to_bits(), MASKED_NAN2);
86     }
87 }
88 
main()89 fn main() {
90     f32();
91     f64();
92 }
93