3
3
4
4
#![ feature( const_float_bits_conv) ]
5
5
#![ feature( const_float_classify) ]
6
+ #![ feature( f16) ]
7
+ #![ feature( f128) ]
6
8
#![ allow( unused_macro_rules) ]
7
-
8
9
// Don't promote
9
10
const fn nop < T > ( x : T ) -> T { x }
10
11
@@ -28,6 +29,36 @@ fn has_broken_floats() -> bool {
28
29
std:: env:: var ( "TARGET" ) . is_ok_and ( |v| v. contains ( "i586" ) )
29
30
}
30
31
32
+ fn f16 ( ) {
33
+ const_assert ! ( ( 1 f16) . to_bits( ) , 0x3c00 ) ;
34
+ const_assert ! ( u16 :: from_be_bytes( 1 f16. 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 ! ( ( 1337 f16) . to_bits( ) , 0x6539 ) ;
38
+ const_assert ! ( u16 :: from_ne_bytes( 1337 f16. 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 ) ;
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 ( ) ^ 0x0155 ;
52
+ const SIGNALING_NAN : u16 = f16:: NAN . to_bits ( ) ^ 0x02AA ;
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
+ }
60
+ }
61
+
31
62
fn f32 ( ) {
32
63
const_assert ! ( ( 1f32 ) . to_bits( ) , 0x3f800000 ) ;
33
64
const_assert ! ( u32 :: from_be_bytes( 1f32 . to_be_bytes( ) ) , 0x3f800000 ) ;
@@ -88,7 +119,39 @@ fn f64() {
88
119
}
89
120
}
90
121
122
+ fn f128 ( ) {
123
+ const_assert ! ( ( 1 f128) . to_bits( ) , 0x3fff0000000000000000000000000000 ) ;
124
+ const_assert ! ( u128 :: from_be_bytes( 1 f128. to_be_bytes( ) ) , 0x3fff0000000000000000000000000000 ) ;
125
+ const_assert ! ( ( 12.5f128 ) . to_bits( ) , 0x40029000000000000000000000000000 ) ;
126
+ const_assert ! ( u128 :: from_le_bytes( 12.5f128 . to_le_bytes( ) ) , 0x40029000000000000000000000000000 ) ;
127
+ const_assert ! ( ( 1337 f128) . to_bits( ) , 0x40094e40000000000000000000000000 ) ;
128
+ const_assert ! ( u128 :: from_ne_bytes( 1337 f128. to_ne_bytes( ) ) , 0x40094e40000000000000000000000000 ) ;
129
+ const_assert ! ( ( -14.25f128 ) . to_bits( ) , 0xc002c800000000000000000000000000 ) ;
130
+ const_assert ! ( f128:: from_bits( 0x3fff0000000000000000000000000000 ) , 1.0 ) ;
131
+ const_assert ! ( f128:: from_be_bytes( 0x3fff0000000000000000000000000000u128 . to_be_bytes( ) ) , 1.0 ) ;
132
+ const_assert ! ( f128:: from_bits( 0x40029000000000000000000000000000 ) , 12.5 ) ;
133
+ const_assert ! ( f128:: from_le_bytes( 0x40029000000000000000000000000000u128 . to_le_bytes( ) ) , 12.5 ) ;
134
+ const_assert ! ( f128:: from_bits( 0x40094e40000000000000000000000000 ) , 1337.0 ) ;
135
+ assert_eq ! ( f128:: from_ne_bytes( 0x40094e40000000000000000000000000u128 . to_ne_bytes( ) ) , 1337.0 ) ;
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 ( ) | 0x0000_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA ;
142
+ const SIGNALING_NAN : u128 = f128:: NAN . to_bits ( ) ^ 0x0000_5555_5555_5555_5555_5555_5555_5555 ;
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
+ }
150
+ }
151
+
91
152
fn main ( ) {
153
+ f16 ( ) ;
92
154
f32 ( ) ;
93
155
f64 ( ) ;
156
+ f128 ( ) ;
94
157
}
0 commit comments