|
| 1 | +// run-pass |
| 2 | + |
| 3 | +#![feature(const_panic)] |
| 4 | +#![feature(const_if_match)] |
| 5 | +#![feature(const_float_bits_conv)] |
| 6 | + |
| 7 | +macro_rules! const_assert { |
| 8 | + ($a:expr) => { |
| 9 | + { |
| 10 | + const _: () = assert!($a); |
| 11 | + assert!($a); |
| 12 | + } |
| 13 | + }; |
| 14 | + ($a:expr, $b:expr) => { |
| 15 | + { |
| 16 | + const _: () = assert!($a == $b); |
| 17 | + assert_eq!($a, $b); |
| 18 | + } |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +fn f32() { |
| 23 | + const_assert!((1f32).to_bits(), 0x3f800000); |
| 24 | + const_assert!(u32::from_be_bytes(1f32.to_be_bytes()), 0x3f800000); |
| 25 | + const_assert!((12.5f32).to_bits(), 0x41480000); |
| 26 | + const_assert!(u32::from_le_bytes(12.5f32.to_le_bytes()), 0x41480000); |
| 27 | + const_assert!((1337f32).to_bits(), 0x44a72000); |
| 28 | + const_assert!(u32::from_ne_bytes(1337f32.to_ne_bytes()), 0x44a72000); |
| 29 | + const_assert!((-14.25f32).to_bits(), 0xc1640000); |
| 30 | + const_assert!(f32::from_bits(0x3f800000), 1.0); |
| 31 | + const_assert!(f32::from_be_bytes(0x3f800000u32.to_be_bytes()), 1.0); |
| 32 | + const_assert!(f32::from_bits(0x41480000), 12.5); |
| 33 | + const_assert!(f32::from_le_bytes(0x41480000u32.to_le_bytes()), 12.5); |
| 34 | + const_assert!(f32::from_bits(0x44a72000), 1337.0); |
| 35 | + const_assert!(f32::from_ne_bytes(0x44a72000u32.to_ne_bytes()), 1337.0); |
| 36 | + const_assert!(f32::from_bits(0xc1640000), -14.25); |
| 37 | + |
| 38 | + // Check that NaNs roundtrip their bits regardless of signalingness |
| 39 | + // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits |
| 40 | + const MASKED_NAN1: u32 = f32::NAN.to_bits() ^ 0x002A_AAAA; |
| 41 | + const MASKED_NAN2: u32 = f32::NAN.to_bits() ^ 0x0055_5555; |
| 42 | + |
| 43 | + const_assert!(f32::from_bits(MASKED_NAN1).to_bits(), MASKED_NAN1); |
| 44 | + const_assert!(f32::from_bits(MASKED_NAN2).to_bits(), MASKED_NAN2); |
| 45 | +} |
| 46 | + |
| 47 | +fn f64() { |
| 48 | + const_assert!((1f64).to_bits(), 0x3ff0000000000000); |
| 49 | + const_assert!(u64::from_be_bytes(1f64.to_be_bytes()), 0x3ff0000000000000); |
| 50 | + const_assert!((12.5f64).to_bits(), 0x4029000000000000); |
| 51 | + const_assert!(u64::from_le_bytes(12.5f64.to_le_bytes()), 0x4029000000000000); |
| 52 | + const_assert!((1337f64).to_bits(), 0x4094e40000000000); |
| 53 | + const_assert!(u64::from_ne_bytes(1337f64.to_ne_bytes()), 0x4094e40000000000); |
| 54 | + const_assert!((-14.25f64).to_bits(), 0xc02c800000000000); |
| 55 | + const_assert!(f64::from_bits(0x3ff0000000000000), 1.0); |
| 56 | + const_assert!(f64::from_be_bytes(0x3ff0000000000000u64.to_be_bytes()), 1.0); |
| 57 | + const_assert!(f64::from_bits(0x4029000000000000), 12.5); |
| 58 | + const_assert!(f64::from_le_bytes(0x4029000000000000u64.to_le_bytes()), 12.5); |
| 59 | + const_assert!(f64::from_bits(0x4094e40000000000), 1337.0); |
| 60 | + const_assert!(f64::from_ne_bytes(0x4094e40000000000u64.to_ne_bytes()), 1337.0); |
| 61 | + const_assert!(f64::from_bits(0xc02c800000000000), -14.25); |
| 62 | + |
| 63 | + // Check that NaNs roundtrip their bits regardless of signalingness |
| 64 | + // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits |
| 65 | + const MASKED_NAN1: u64 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA; |
| 66 | + const MASKED_NAN2: u64 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555; |
| 67 | + |
| 68 | + const_assert!(f64::from_bits(MASKED_NAN1).to_bits(), MASKED_NAN1); |
| 69 | + const_assert!(f64::from_bits(MASKED_NAN2).to_bits(), MASKED_NAN2); |
| 70 | +} |
| 71 | + |
| 72 | +fn main() { |
| 73 | + f32(); |
| 74 | + f64(); |
| 75 | +} |
0 commit comments