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,37 @@ fn has_broken_floats() -> bool {
28
29
std:: env:: var ( "TARGET" ) . is_ok_and ( |v| v. contains ( "i586" ) )
29
30
}
30
31
32
+ #[ cfg( target_arch = "x86_64" ) ]
33
+ fn f16 ( ) {
34
+ const_assert ! ( ( 1 f16) . to_bits( ) , 0x3c00 ) ;
35
+ const_assert ! ( u16 :: from_be_bytes( 1 f16. to_be_bytes( ) ) , 0x3c00 ) ;
36
+ const_assert ! ( ( 12.5f16 ) . to_bits( ) , 0x4a40 ) ;
37
+ const_assert ! ( u16 :: from_le_bytes( 12.5f16 . to_le_bytes( ) ) , 0x4a40 ) ;
38
+ const_assert ! ( ( 1337 f16) . to_bits( ) , 0x6539 ) ;
39
+ const_assert ! ( u16 :: from_ne_bytes( 1337 f16. to_ne_bytes( ) ) , 0x6539 ) ;
40
+ const_assert ! ( ( -14.25f16 ) . to_bits( ) , 0xcb20 ) ;
41
+ const_assert ! ( f16:: from_bits( 0x3c00 ) , 1.0 ) ;
42
+ const_assert ! ( f16:: from_be_bytes( 0x3c00u16 . to_be_bytes( ) ) , 1.0 ) ;
43
+ const_assert ! ( f16:: from_bits( 0x4a40 ) , 12.5 ) ;
44
+ const_assert ! ( f16:: from_le_bytes( 0x4a40u16 . to_le_bytes( ) ) , 12.5 ) ;
45
+ const_assert ! ( f16:: from_bits( 0x5be0 ) , 252.0 ) ;
46
+ const_assert ! ( f16:: from_ne_bytes( 0x5be0u16 . to_ne_bytes( ) ) , 252.0 ) ;
47
+ const_assert ! ( f16:: from_bits( 0xcb20 ) , -14.25 ) ;
48
+
49
+ // Check that NaNs roundtrip their bits regardless of signalingness
50
+ // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
51
+ // NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
52
+ const QUIET_NAN : u16 = f16:: NAN . to_bits ( ) ^ 0x0155 ;
53
+ const SIGNALING_NAN : u16 = f16:: NAN . to_bits ( ) ^ 0x02AA ;
54
+
55
+ const_assert ! ( f16:: from_bits( QUIET_NAN ) . is_nan( ) ) ;
56
+ const_assert ! ( f16:: from_bits( SIGNALING_NAN ) . is_nan( ) ) ;
57
+ const_assert ! ( f16:: from_bits( QUIET_NAN ) . to_bits( ) , QUIET_NAN ) ;
58
+ if !has_broken_floats ( ) {
59
+ const_assert ! ( f16:: from_bits( SIGNALING_NAN ) . to_bits( ) , SIGNALING_NAN ) ;
60
+ }
61
+ }
62
+
31
63
fn f32 ( ) {
32
64
const_assert ! ( ( 1f32 ) . to_bits( ) , 0x3f800000 ) ;
33
65
const_assert ! ( u32 :: from_be_bytes( 1f32 . to_be_bytes( ) ) , 0x3f800000 ) ;
@@ -88,7 +120,43 @@ fn f64() {
88
120
}
89
121
}
90
122
123
+ #[ cfg( target_arch = "x86_64" ) ]
124
+ fn f128 ( ) {
125
+ const_assert ! ( ( 1 f128) . to_bits( ) , 0x3fff0000000000000000000000000000 ) ;
126
+ const_assert ! ( u128 :: from_be_bytes( 1 f128. to_be_bytes( ) ) , 0x3fff0000000000000000000000000000 ) ;
127
+ const_assert ! ( ( 12.5f128 ) . to_bits( ) , 0x40029000000000000000000000000000 ) ;
128
+ const_assert ! ( u128 :: from_le_bytes( 12.5f128 . to_le_bytes( ) ) , 0x40029000000000000000000000000000 ) ;
129
+ const_assert ! ( ( 1337 f128) . to_bits( ) , 0x40094e40000000000000000000000000 ) ;
130
+ const_assert ! ( u128 :: from_ne_bytes( 1337 f128. to_ne_bytes( ) ) , 0x40094e40000000000000000000000000 ) ;
131
+ const_assert ! ( ( -14.25f128 ) . to_bits( ) , 0xc002c800000000000000000000000000 ) ;
132
+ const_assert ! ( f128:: from_bits( 0x3fff0000000000000000000000000000 ) , 1.0 ) ;
133
+ const_assert ! ( f128:: from_be_bytes( 0x3fff0000000000000000000000000000u128 . to_be_bytes( ) ) , 1.0 ) ;
134
+ const_assert ! ( f128:: from_bits( 0x40029000000000000000000000000000 ) , 12.5 ) ;
135
+ const_assert ! ( f128:: from_le_bytes( 0x40029000000000000000000000000000u128 . to_le_bytes( ) ) , 12.5 ) ;
136
+ const_assert ! ( f128:: from_bits( 0x40094e40000000000000000000000000 ) , 1337.0 ) ;
137
+ assert_eq ! ( f128:: from_ne_bytes( 0x40094e40000000000000000000000000u128 . to_ne_bytes( ) ) , 1337.0 ) ;
138
+ const_assert ! ( f128:: from_bits( 0xc002c800000000000000000000000000 ) , -14.25 ) ;
139
+
140
+ // Check that NaNs roundtrip their bits regardless of signalingness
141
+ // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
142
+ // NOTE: These names assume `f{BITS}::NAN` is a quiet NAN and IEEE754-2008's NaN rules apply!
143
+ const QUIET_NAN : u128 = f128:: NAN . to_bits ( ) | 0x0000_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA ;
144
+ const SIGNALING_NAN : u128 = f128:: NAN . to_bits ( ) ^ 0x0000_5555_5555_5555_5555_5555_5555_5555 ;
145
+
146
+ const_assert ! ( f128:: from_bits( QUIET_NAN ) . is_nan( ) ) ;
147
+ const_assert ! ( f128:: from_bits( SIGNALING_NAN ) . is_nan( ) ) ;
148
+ const_assert ! ( f128:: from_bits( QUIET_NAN ) . to_bits( ) , QUIET_NAN ) ;
149
+ if !has_broken_floats ( ) {
150
+ const_assert ! ( f128:: from_bits( SIGNALING_NAN ) . to_bits( ) , SIGNALING_NAN ) ;
151
+ }
152
+ }
153
+
91
154
fn main ( ) {
155
+ #[ cfg( target_arch = "x86_64" ) ]
156
+ {
157
+ f16 ( ) ;
158
+ f128 ( ) ;
159
+ }
92
160
f32 ( ) ;
93
161
f64 ( ) ;
94
162
}
0 commit comments