@@ -44,6 +44,19 @@ fn f16(){
44
44
const_assert ! ( f16:: from_bits( 0x5be0 ) , 252.0 ) ;
45
45
const_assert ! ( f16:: from_ne_bytes( 0x5be0u16 . to_ne_bytes( ) ) , 252.0 ) ;
46
46
const_assert ! ( f16:: from_bits( 0xcb20 ) , -14.25 ) ;
47
+
48
+ // Check that NaNs roundtrip their bits regardless of signalingness
49
+ // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
50
+ // NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
51
+ const QUIET_NAN : u16 = f16:: NAN . to_bits ( ) ^ 0x02AA ;
52
+ const SIGNALING_NAN : u16 = 0x7eaa ;
53
+
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 ) ;
57
+ if !has_broken_floats ( ) {
58
+ const_assert ! ( f16:: from_bits( SIGNALING_NAN ) . to_bits( ) , SIGNALING_NAN ) ;
59
+ }
47
60
}
48
61
49
62
fn f32 ( ) {
@@ -106,23 +119,6 @@ fn f64() {
106
119
}
107
120
}
108
121
109
- fn f128 ( ) {
110
- const_assert ! ( ( 1 f128) . to_bits( ) , 0x3fff0000000000000000000000000000 ) ;
111
- const_assert ! ( u128 :: from_be_bytes( 1 f128. to_be_bytes( ) ) , 0x3fff0000000000000000000000000000 ) ;
112
- const_assert ! ( ( 12.5f128 ) . to_bits( ) , 0x40029000000000000000000000000000 ) ;
113
- const_assert ! ( u128 :: from_le_bytes( 12.5f128 . to_le_bytes( ) ) , 0x40029000000000000000000000000000 ) ;
114
- const_assert ! ( ( 1337 f128) . to_bits( ) , 0x40094e40000000000000000000000000 ) ;
115
- const_assert ! ( u128 :: from_ne_bytes( 1337 f128. to_ne_bytes( ) ) , 0x40094e40000000000000000000000000 ) ;
116
- const_assert ! ( ( -14.25f128 ) . to_bits( ) , 0xc002c800000000000000000000000000 ) ;
117
- const_assert ! ( f128:: from_bits( 0x3fff0000000000000000000000000000 ) , 1.0 ) ;
118
- const_assert ! ( f128:: from_be_bytes( 0x3fff0000000000000000000000000000u128 . to_be_bytes( ) ) , 1.0 ) ;
119
- const_assert ! ( f128:: from_bits( 0x40029000000000000000000000000000 ) , 12.5 ) ;
120
- const_assert ! ( f128:: from_le_bytes( 0x40029000000000000000000000000000u128 . to_le_bytes( ) ) , 12.5 ) ;
121
- const_assert ! ( f128:: from_bits( 0x40094e40000000000000000000000000 ) , 1337.0 ) ;
122
- const_assert ! ( f128:: from_ne_bytes( 0x40094e40000000000000000000000000u128 . to_ne_bytes( ) ) , 1337.0 ) ;
123
- const_assert ! ( f128:: from_bits( 0xc002c800000000000000000000000000 ) , -14.25 ) ;
124
- }
125
-
126
122
fn f128 ( ) {
127
123
const_assert ! ( ( 1 f128) . to_bits( ) , 0x3fff0000000000000000000000000000 ) ;
128
124
const_assert ! ( u128 :: from_be_bytes( 1 f128. to_be_bytes( ) ) , 0x3fff0000000000000000000000000000 ) ;
@@ -138,6 +134,19 @@ fn f128() {
138
134
const_assert ! ( f128:: from_bits( 0x40094e40000000000000000000000000 ) , 1337.0 ) ;
139
135
assert_eq ! ( f128:: from_ne_bytes( 0x40094e40000000000000000000000000u128 . to_ne_bytes( ) ) , 1337.0 ) ;
140
136
const_assert ! ( f128:: from_bits( 0xc002c800000000000000000000000000 ) , -14.25 ) ;
137
+
138
+ // Check that NaNs roundtrip their bits regardless of signalingness
139
+ // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
140
+ // NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
141
+ const QUIET_NAN : u128 = f128:: NAN . to_bits ( ) | 0x0008_0000_0000_0000_0000_0000_0000_0000 ; // Set a non-zero mantissa bit
142
+ const SIGNALING_NAN : u128 = QUIET_NAN | 0x0000_0001_0000_0000_0000_0000_0000_0000 ; // Set the most significant mantissa bit
143
+
144
+ const_assert ! ( f128:: from_bits( QUIET_NAN ) . is_nan( ) ) ;
145
+ const_assert ! ( f128:: from_bits( SIGNALING_NAN ) . is_nan( ) ) ;
146
+ const_assert ! ( f128:: from_bits( QUIET_NAN ) . to_bits( ) , QUIET_NAN ) ;
147
+ if !has_broken_floats ( ) {
148
+ const_assert ! ( f128:: from_bits( SIGNALING_NAN ) . to_bits( ) , SIGNALING_NAN ) ;
149
+ }
141
150
}
142
151
143
152
fn main ( ) {
0 commit comments