|
1 | 1 | //@ compile-flags: -Zmir-opt-level=0
|
2 | 2 | //@ run-pass
|
3 | 3 |
|
| 4 | +// This tests the float classification functions, for regular runtime code and for const evaluation. |
| 5 | + |
| 6 | +#![feature(const_float_bits_conv)] |
4 | 7 | #![feature(const_float_classify)]
|
5 |
| -#![feature(f16, f16_const)] |
6 |
| -#![feature(f128, f128_const)] |
| 8 | +#![feature(f16)] |
| 9 | +#![feature(f128)] |
7 | 10 | #![allow(unused_macro_rules)]
|
8 |
| -// Don't promote |
9 |
| -const fn nop<T>(x: T) -> T { x } |
10 | 11 |
|
11 |
| -macro_rules! const_assert { |
| 12 | +use std::hint::black_box; |
| 13 | + |
| 14 | +macro_rules! both_assert { |
12 | 15 | ($a:expr) => {
|
13 | 16 | {
|
14 | 17 | const _: () = assert!($a);
|
15 |
| - assert!(nop($a)); |
| 18 | + // `black_box` prevents promotion, and MIR opts are disabled above, so this is truly |
| 19 | + // going through LLVM. |
| 20 | + assert!(black_box($a)); |
16 | 21 | }
|
17 | 22 | };
|
18 | 23 | ($a:expr, $b:expr) => {
|
19 | 24 | {
|
20 | 25 | const _: () = assert!($a == $b);
|
21 |
| - assert_eq!(nop($a), nop($b)); |
| 26 | + assert_eq!(black_box($a), black_box($b)); |
22 | 27 | }
|
23 | 28 | };
|
24 | 29 | }
|
25 | 30 |
|
26 | 31 | fn has_broken_floats() -> bool {
|
27 | 32 | // i586 targets are broken due to <https://github.com/rust-lang/rust/issues/114479>.
|
28 |
| - std::env::var("TARGET").is_ok_and(|v| v.contains("i586")) |
| 33 | + cfg!(all(target_arch = "x86", not(target_feature = "sse2"))) |
29 | 34 | }
|
30 | 35 |
|
31 | 36 | #[cfg(target_arch = "x86_64")]
|
32 | 37 | 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); |
| 38 | + both_assert!((1f16).to_bits(), 0x3c00); |
| 39 | + both_assert!(u16::from_be_bytes(1f16.to_be_bytes()), 0x3c00); |
| 40 | + both_assert!((12.5f16).to_bits(), 0x4a40); |
| 41 | + both_assert!(u16::from_le_bytes(12.5f16.to_le_bytes()), 0x4a40); |
| 42 | + both_assert!((1337f16).to_bits(), 0x6539); |
| 43 | + both_assert!(u16::from_ne_bytes(1337f16.to_ne_bytes()), 0x6539); |
| 44 | + both_assert!((-14.25f16).to_bits(), 0xcb20); |
| 45 | + both_assert!(f16::from_bits(0x3c00), 1.0); |
| 46 | + both_assert!(f16::from_be_bytes(0x3c00u16.to_be_bytes()), 1.0); |
| 47 | + both_assert!(f16::from_bits(0x4a40), 12.5); |
| 48 | + both_assert!(f16::from_le_bytes(0x4a40u16.to_le_bytes()), 12.5); |
| 49 | + both_assert!(f16::from_bits(0x5be0), 252.0); |
| 50 | + both_assert!(f16::from_ne_bytes(0x5be0u16.to_ne_bytes()), 252.0); |
| 51 | + both_assert!(f16::from_bits(0xcb20), -14.25); |
47 | 52 |
|
48 | 53 | // Check that NaNs roundtrip their bits regardless of signalingness
|
49 | 54 | // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
|
50 | 55 | // NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
|
51 | 56 | const QUIET_NAN: u16 = f16::NAN.to_bits() ^ 0x0155;
|
52 | 57 | const SIGNALING_NAN: u16 = f16::NAN.to_bits() ^ 0x02AA;
|
53 | 58 |
|
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); |
| 59 | + both_assert!(f16::from_bits(QUIET_NAN).is_nan()); |
| 60 | + both_assert!(f16::from_bits(SIGNALING_NAN).is_nan()); |
| 61 | + both_assert!(f16::from_bits(QUIET_NAN).to_bits(), QUIET_NAN); |
57 | 62 | if !has_broken_floats() {
|
58 |
| - const_assert!(f16::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN); |
| 63 | + both_assert!(f16::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN); |
59 | 64 | }
|
60 | 65 | }
|
61 | 66 |
|
62 | 67 | fn f32() {
|
63 |
| - const_assert!((1f32).to_bits(), 0x3f800000); |
64 |
| - const_assert!(u32::from_be_bytes(1f32.to_be_bytes()), 0x3f800000); |
65 |
| - const_assert!((12.5f32).to_bits(), 0x41480000); |
66 |
| - const_assert!(u32::from_le_bytes(12.5f32.to_le_bytes()), 0x41480000); |
67 |
| - const_assert!((1337f32).to_bits(), 0x44a72000); |
68 |
| - const_assert!(u32::from_ne_bytes(1337f32.to_ne_bytes()), 0x44a72000); |
69 |
| - const_assert!((-14.25f32).to_bits(), 0xc1640000); |
70 |
| - const_assert!(f32::from_bits(0x3f800000), 1.0); |
71 |
| - const_assert!(f32::from_be_bytes(0x3f800000u32.to_be_bytes()), 1.0); |
72 |
| - const_assert!(f32::from_bits(0x41480000), 12.5); |
73 |
| - const_assert!(f32::from_le_bytes(0x41480000u32.to_le_bytes()), 12.5); |
74 |
| - const_assert!(f32::from_bits(0x44a72000), 1337.0); |
75 |
| - const_assert!(f32::from_ne_bytes(0x44a72000u32.to_ne_bytes()), 1337.0); |
76 |
| - const_assert!(f32::from_bits(0xc1640000), -14.25); |
| 68 | + both_assert!((1f32).to_bits(), 0x3f800000); |
| 69 | + both_assert!(u32::from_be_bytes(1f32.to_be_bytes()), 0x3f800000); |
| 70 | + both_assert!((12.5f32).to_bits(), 0x41480000); |
| 71 | + both_assert!(u32::from_le_bytes(12.5f32.to_le_bytes()), 0x41480000); |
| 72 | + both_assert!((1337f32).to_bits(), 0x44a72000); |
| 73 | + both_assert!(u32::from_ne_bytes(1337f32.to_ne_bytes()), 0x44a72000); |
| 74 | + both_assert!((-14.25f32).to_bits(), 0xc1640000); |
| 75 | + both_assert!(f32::from_bits(0x3f800000), 1.0); |
| 76 | + both_assert!(f32::from_be_bytes(0x3f800000u32.to_be_bytes()), 1.0); |
| 77 | + both_assert!(f32::from_bits(0x41480000), 12.5); |
| 78 | + both_assert!(f32::from_le_bytes(0x41480000u32.to_le_bytes()), 12.5); |
| 79 | + both_assert!(f32::from_bits(0x44a72000), 1337.0); |
| 80 | + both_assert!(f32::from_ne_bytes(0x44a72000u32.to_ne_bytes()), 1337.0); |
| 81 | + both_assert!(f32::from_bits(0xc1640000), -14.25); |
77 | 82 |
|
78 | 83 | // Check that NaNs roundtrip their bits regardless of signalingness
|
79 | 84 | // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
|
80 | 85 | // NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
|
81 | 86 | const QUIET_NAN: u32 = f32::NAN.to_bits() ^ 0x002A_AAAA;
|
82 | 87 | const SIGNALING_NAN: u32 = f32::NAN.to_bits() ^ 0x0055_5555;
|
83 | 88 |
|
84 |
| - const_assert!(f32::from_bits(QUIET_NAN).is_nan()); |
85 |
| - const_assert!(f32::from_bits(SIGNALING_NAN).is_nan()); |
86 |
| - const_assert!(f32::from_bits(QUIET_NAN).to_bits(), QUIET_NAN); |
| 89 | + both_assert!(f32::from_bits(QUIET_NAN).is_nan()); |
| 90 | + both_assert!(f32::from_bits(SIGNALING_NAN).is_nan()); |
| 91 | + both_assert!(f32::from_bits(QUIET_NAN).to_bits(), QUIET_NAN); |
87 | 92 | if !has_broken_floats() {
|
88 |
| - const_assert!(f32::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN); |
| 93 | + both_assert!(f32::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN); |
89 | 94 | }
|
90 | 95 | }
|
91 | 96 |
|
92 | 97 | fn f64() {
|
93 |
| - const_assert!((1f64).to_bits(), 0x3ff0000000000000); |
94 |
| - const_assert!(u64::from_be_bytes(1f64.to_be_bytes()), 0x3ff0000000000000); |
95 |
| - const_assert!((12.5f64).to_bits(), 0x4029000000000000); |
96 |
| - const_assert!(u64::from_le_bytes(12.5f64.to_le_bytes()), 0x4029000000000000); |
97 |
| - const_assert!((1337f64).to_bits(), 0x4094e40000000000); |
98 |
| - const_assert!(u64::from_ne_bytes(1337f64.to_ne_bytes()), 0x4094e40000000000); |
99 |
| - const_assert!((-14.25f64).to_bits(), 0xc02c800000000000); |
100 |
| - const_assert!(f64::from_bits(0x3ff0000000000000), 1.0); |
101 |
| - const_assert!(f64::from_be_bytes(0x3ff0000000000000u64.to_be_bytes()), 1.0); |
102 |
| - const_assert!(f64::from_bits(0x4029000000000000), 12.5); |
103 |
| - const_assert!(f64::from_le_bytes(0x4029000000000000u64.to_le_bytes()), 12.5); |
104 |
| - const_assert!(f64::from_bits(0x4094e40000000000), 1337.0); |
105 |
| - const_assert!(f64::from_ne_bytes(0x4094e40000000000u64.to_ne_bytes()), 1337.0); |
106 |
| - const_assert!(f64::from_bits(0xc02c800000000000), -14.25); |
| 98 | + both_assert!((1f64).to_bits(), 0x3ff0000000000000); |
| 99 | + both_assert!(u64::from_be_bytes(1f64.to_be_bytes()), 0x3ff0000000000000); |
| 100 | + both_assert!((12.5f64).to_bits(), 0x4029000000000000); |
| 101 | + both_assert!(u64::from_le_bytes(12.5f64.to_le_bytes()), 0x4029000000000000); |
| 102 | + both_assert!((1337f64).to_bits(), 0x4094e40000000000); |
| 103 | + both_assert!(u64::from_ne_bytes(1337f64.to_ne_bytes()), 0x4094e40000000000); |
| 104 | + both_assert!((-14.25f64).to_bits(), 0xc02c800000000000); |
| 105 | + both_assert!(f64::from_bits(0x3ff0000000000000), 1.0); |
| 106 | + both_assert!(f64::from_be_bytes(0x3ff0000000000000u64.to_be_bytes()), 1.0); |
| 107 | + both_assert!(f64::from_bits(0x4029000000000000), 12.5); |
| 108 | + both_assert!(f64::from_le_bytes(0x4029000000000000u64.to_le_bytes()), 12.5); |
| 109 | + both_assert!(f64::from_bits(0x4094e40000000000), 1337.0); |
| 110 | + both_assert!(f64::from_ne_bytes(0x4094e40000000000u64.to_ne_bytes()), 1337.0); |
| 111 | + both_assert!(f64::from_bits(0xc02c800000000000), -14.25); |
107 | 112 |
|
108 | 113 | // Check that NaNs roundtrip their bits regardless of signalingness
|
109 | 114 | // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
|
110 | 115 | // NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
|
111 | 116 | const QUIET_NAN: u64 = f64::NAN.to_bits() ^ 0x0005_5555_5555_5555;
|
112 | 117 | const SIGNALING_NAN: u64 = f64::NAN.to_bits() ^ 0x000A_AAAA_AAAA_AAAA;
|
113 | 118 |
|
114 |
| - const_assert!(f64::from_bits(QUIET_NAN).is_nan()); |
115 |
| - const_assert!(f64::from_bits(SIGNALING_NAN).is_nan()); |
116 |
| - const_assert!(f64::from_bits(QUIET_NAN).to_bits(), QUIET_NAN); |
| 119 | + both_assert!(f64::from_bits(QUIET_NAN).is_nan()); |
| 120 | + both_assert!(f64::from_bits(SIGNALING_NAN).is_nan()); |
| 121 | + both_assert!(f64::from_bits(QUIET_NAN).to_bits(), QUIET_NAN); |
117 | 122 | if !has_broken_floats() {
|
118 |
| - const_assert!(f64::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN); |
| 123 | + both_assert!(f64::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN); |
119 | 124 | }
|
120 | 125 | }
|
121 | 126 |
|
122 | 127 | #[cfg(target_arch = "x86_64")]
|
123 | 128 | fn f128() {
|
124 |
| - const_assert!((1f128).to_bits(), 0x3fff0000000000000000000000000000); |
125 |
| - const_assert!(u128::from_be_bytes(1f128.to_be_bytes()), 0x3fff0000000000000000000000000000); |
126 |
| - const_assert!((12.5f128).to_bits(), 0x40029000000000000000000000000000); |
127 |
| - const_assert!(u128::from_le_bytes(12.5f128.to_le_bytes()), 0x40029000000000000000000000000000); |
128 |
| - const_assert!((1337f128).to_bits(), 0x40094e40000000000000000000000000); |
129 |
| - const_assert!(u128::from_ne_bytes(1337f128.to_ne_bytes()), 0x40094e40000000000000000000000000); |
130 |
| - const_assert!((-14.25f128).to_bits(), 0xc002c800000000000000000000000000); |
131 |
| - const_assert!(f128::from_bits(0x3fff0000000000000000000000000000), 1.0); |
132 |
| - const_assert!(f128::from_be_bytes(0x3fff0000000000000000000000000000u128.to_be_bytes()), 1.0); |
133 |
| - const_assert!(f128::from_bits(0x40029000000000000000000000000000), 12.5); |
134 |
| - const_assert!(f128::from_le_bytes(0x40029000000000000000000000000000u128.to_le_bytes()), 12.5); |
135 |
| - const_assert!(f128::from_bits(0x40094e40000000000000000000000000), 1337.0); |
| 129 | + both_assert!((1f128).to_bits(), 0x3fff0000000000000000000000000000); |
| 130 | + both_assert!(u128::from_be_bytes(1f128.to_be_bytes()), 0x3fff0000000000000000000000000000); |
| 131 | + both_assert!((12.5f128).to_bits(), 0x40029000000000000000000000000000); |
| 132 | + both_assert!(u128::from_le_bytes(12.5f128.to_le_bytes()), 0x40029000000000000000000000000000); |
| 133 | + both_assert!((1337f128).to_bits(), 0x40094e40000000000000000000000000); |
| 134 | + both_assert!(u128::from_ne_bytes(1337f128.to_ne_bytes()), 0x40094e40000000000000000000000000); |
| 135 | + both_assert!((-14.25f128).to_bits(), 0xc002c800000000000000000000000000); |
| 136 | + both_assert!(f128::from_bits(0x3fff0000000000000000000000000000), 1.0); |
| 137 | + both_assert!(f128::from_be_bytes(0x3fff0000000000000000000000000000u128.to_be_bytes()), 1.0); |
| 138 | + both_assert!(f128::from_bits(0x40029000000000000000000000000000), 12.5); |
| 139 | + both_assert!(f128::from_le_bytes(0x40029000000000000000000000000000u128.to_le_bytes()), 12.5); |
| 140 | + both_assert!(f128::from_bits(0x40094e40000000000000000000000000), 1337.0); |
136 | 141 | assert_eq!(f128::from_ne_bytes(0x40094e40000000000000000000000000u128.to_ne_bytes()), 1337.0);
|
137 |
| - const_assert!(f128::from_bits(0xc002c800000000000000000000000000), -14.25); |
| 142 | + both_assert!(f128::from_bits(0xc002c800000000000000000000000000), -14.25); |
138 | 143 |
|
139 | 144 | // Check that NaNs roundtrip their bits regardless of signalingness
|
140 | 145 | // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
|
141 | 146 | // NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
|
142 | 147 | const QUIET_NAN: u128 = f128::NAN.to_bits() | 0x0000_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA;
|
143 | 148 | const SIGNALING_NAN: u128 = f128::NAN.to_bits() ^ 0x0000_5555_5555_5555_5555_5555_5555_5555;
|
144 | 149 |
|
145 |
| - const_assert!(f128::from_bits(QUIET_NAN).is_nan()); |
146 |
| - const_assert!(f128::from_bits(SIGNALING_NAN).is_nan()); |
147 |
| - const_assert!(f128::from_bits(QUIET_NAN).to_bits(), QUIET_NAN); |
| 150 | + both_assert!(f128::from_bits(QUIET_NAN).is_nan()); |
| 151 | + both_assert!(f128::from_bits(SIGNALING_NAN).is_nan()); |
| 152 | + both_assert!(f128::from_bits(QUIET_NAN).to_bits(), QUIET_NAN); |
148 | 153 | if !has_broken_floats() {
|
149 |
| - const_assert!(f128::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN); |
| 154 | + both_assert!(f128::from_bits(SIGNALING_NAN).to_bits(), SIGNALING_NAN); |
150 | 155 | }
|
151 | 156 | }
|
152 | 157 |
|
153 | 158 | fn main() {
|
| 159 | + f32(); |
| 160 | + f64(); |
| 161 | + |
154 | 162 | #[cfg(target_arch = "x86_64")]
|
155 | 163 | {
|
156 | 164 | f16();
|
157 | 165 | f128();
|
158 | 166 | }
|
159 |
| - f32(); |
160 |
| - f64(); |
161 | 167 | }
|
0 commit comments