|
| 1 | +//@ run-pass |
| 2 | +//@ compile-flags: -Copt-level=0 |
| 3 | + |
| 4 | +// Test that floats (in particular signalling NaNs) are losslessly returned from functions. |
| 5 | + |
| 6 | +fn main() { |
| 7 | + let bits_f32 = std::hint::black_box([ |
| 8 | + 4.2_f32.to_bits(), |
| 9 | + f32::INFINITY.to_bits(), |
| 10 | + f32::NEG_INFINITY.to_bits(), |
| 11 | + f32::NAN.to_bits(), |
| 12 | + // These two masks cover all the mantissa bits. One of them is a signalling NaN, the other |
| 13 | + // is quiet. |
| 14 | + // Similar to the masks in `test_float_bits_conv` in library/std/src/f32/tests.rs |
| 15 | + f32::NAN.to_bits() ^ 0x002A_AAAA, |
| 16 | + f32::NAN.to_bits() ^ 0x0055_5555, |
| 17 | + // Same as above but with the sign bit flipped. |
| 18 | + f32::NAN.to_bits() ^ 0x802A_AAAA, |
| 19 | + f32::NAN.to_bits() ^ 0x8055_5555, |
| 20 | + ]); |
| 21 | + for bits in bits_f32 { |
| 22 | + assert_eq!(identity(f32::from_bits(bits)).to_bits(), bits); |
| 23 | + // Test types that are returned as scalar pairs. |
| 24 | + assert_eq!(identity((f32::from_bits(bits), 42)).0.to_bits(), bits); |
| 25 | + assert_eq!(identity((42, f32::from_bits(bits))).1.to_bits(), bits); |
| 26 | + let (a, b) = identity((f32::from_bits(bits), f32::from_bits(bits))); |
| 27 | + assert_eq!((a.to_bits(), b.to_bits()), (bits, bits)); |
| 28 | + } |
| 29 | + |
| 30 | + let bits_f64 = std::hint::black_box([ |
| 31 | + 4.2_f64.to_bits(), |
| 32 | + f64::INFINITY.to_bits(), |
| 33 | + f64::NEG_INFINITY.to_bits(), |
| 34 | + f64::NAN.to_bits(), |
| 35 | + // These two masks cover all the mantissa bits. One of them is a signalling NaN, the other |
| 36 | + // is quiet. |
| 37 | + // Similar to the masks in `test_float_bits_conv` in library/std/src/f64/tests.rs |
| 38 | + f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA, |
| 39 | + f64::NAN.to_bits() ^ 0x0005_5555_5555_5555, |
| 40 | + // Same as above but with the sign bit flipped. |
| 41 | + f64::NAN.to_bits() ^ 0x800A_AAAA_AAAA_AAAA, |
| 42 | + f64::NAN.to_bits() ^ 0x8005_5555_5555_5555, |
| 43 | + ]); |
| 44 | + for bits in bits_f64 { |
| 45 | + assert_eq!(identity(f64::from_bits(bits)).to_bits(), bits); |
| 46 | + // Test types that are returned as scalar pairs. |
| 47 | + assert_eq!(identity((f64::from_bits(bits), 42)).0.to_bits(), bits); |
| 48 | + assert_eq!(identity((42, f64::from_bits(bits))).1.to_bits(), bits); |
| 49 | + let (a, b) = identity((f64::from_bits(bits), f64::from_bits(bits))); |
| 50 | + assert_eq!((a.to_bits(), b.to_bits()), (bits, bits)); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[inline(never)] |
| 55 | +fn identity<T>(x: T) -> T { |
| 56 | + x |
| 57 | +} |
0 commit comments