@@ -30,6 +30,10 @@ trait TestableFloat: Sized {
3030 const EPS_ADD : Self ;
3131 const EPS_MUL : Self ;
3232 const EPS_DIV : Self ;
33+ const RAW_1 : Self ;
34+ const RAW_12_DOT_5 : Self ;
35+ const RAW_1337 : Self ;
36+ const RAW_MINUS_14_DOT_25 : Self ;
3337}
3438
3539impl TestableFloat for f16 {
@@ -50,6 +54,10 @@ impl TestableFloat for f16 {
5054 const EPS_ADD : Self = if cfg ! ( miri) { 1e1 } else { 0.0 } ;
5155 const EPS_MUL : Self = if cfg ! ( miri) { 1e3 } else { 0.0 } ;
5256 const EPS_DIV : Self = if cfg ! ( miri) { 1e0 } else { 0.0 } ;
57+ const RAW_1 : Self = Self :: from_bits ( 0x3c00 ) ;
58+ const RAW_12_DOT_5 : Self = Self :: from_bits ( 0x4a40 ) ;
59+ const RAW_1337 : Self = Self :: from_bits ( 0x6539 ) ;
60+ const RAW_MINUS_14_DOT_25 : Self = Self :: from_bits ( 0xcb20 ) ;
5361}
5462
5563impl TestableFloat for f32 {
@@ -72,6 +80,10 @@ impl TestableFloat for f32 {
7280 const EPS_ADD : Self = if cfg ! ( miri) { 1e-3 } else { 0.0 } ;
7381 const EPS_MUL : Self = if cfg ! ( miri) { 1e-1 } else { 0.0 } ;
7482 const EPS_DIV : Self = if cfg ! ( miri) { 1e-4 } else { 0.0 } ;
83+ const RAW_1 : Self = Self :: from_bits ( 0x3f800000 ) ;
84+ const RAW_12_DOT_5 : Self = Self :: from_bits ( 0x41480000 ) ;
85+ const RAW_1337 : Self = Self :: from_bits ( 0x44a72000 ) ;
86+ const RAW_MINUS_14_DOT_25 : Self = Self :: from_bits ( 0xc1640000 ) ;
7587}
7688
7789impl TestableFloat for f64 {
@@ -90,6 +102,10 @@ impl TestableFloat for f64 {
90102 const EPS_ADD : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
91103 const EPS_MUL : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
92104 const EPS_DIV : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
105+ const RAW_1 : Self = Self :: from_bits ( 0x3ff0000000000000 ) ;
106+ const RAW_12_DOT_5 : Self = Self :: from_bits ( 0x4029000000000000 ) ;
107+ const RAW_1337 : Self = Self :: from_bits ( 0x4094e40000000000 ) ;
108+ const RAW_MINUS_14_DOT_25 : Self = Self :: from_bits ( 0xc02c800000000000 ) ;
93109}
94110
95111impl TestableFloat for f128 {
@@ -108,6 +124,10 @@ impl TestableFloat for f128 {
108124 const EPS_ADD : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
109125 const EPS_MUL : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
110126 const EPS_DIV : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
127+ const RAW_1 : Self = Self :: from_bits ( 0x3fff0000000000000000000000000000 ) ;
128+ const RAW_12_DOT_5 : Self = Self :: from_bits ( 0x40029000000000000000000000000000 ) ;
129+ const RAW_1337 : Self = Self :: from_bits ( 0x40094e40000000000000000000000000 ) ;
130+ const RAW_MINUS_14_DOT_25 : Self = Self :: from_bits ( 0xc002c800000000000000000000000000 ) ;
111131}
112132
113133/// Determine the tolerance for values of the argument type.
@@ -250,27 +270,35 @@ macro_rules! float_test {
250270 $( $( #[ $f16_meta] ) + ) ?
251271 fn test_f16( ) {
252272 type $fty = f16;
273+ #[ allow( unused) ]
274+ const fn flt ( x: $fty) -> $fty { x }
253275 $test
254276 }
255277
256278 #[ test]
257279 $( $( #[ $f32_meta] ) + ) ?
258280 fn test_f32( ) {
259281 type $fty = f32 ;
282+ #[ allow( unused) ]
283+ const fn flt ( x: $fty) -> $fty { x }
260284 $test
261285 }
262286
263287 #[ test]
264288 $( $( #[ $f64_meta] ) + ) ?
265289 fn test_f64( ) {
266290 type $fty = f64 ;
291+ #[ allow( unused) ]
292+ const fn flt ( x: $fty) -> $fty { x }
267293 $test
268294 }
269295
270296 #[ test]
271297 $( $( #[ $f128_meta] ) + ) ?
272298 fn test_f128( ) {
273299 type $fty = f128;
300+ #[ allow( unused) ]
301+ const fn flt ( x: $fty) -> $fty { x }
274302 $test
275303 }
276304
@@ -293,27 +321,35 @@ macro_rules! float_test {
293321 $( $( #[ $f16_const_meta] ) + ) ?
294322 fn test_f16( ) {
295323 type $fty = f16;
324+ #[ allow( unused) ]
325+ const fn flt ( x: $fty) -> $fty { x }
296326 const { $test }
297327 }
298328
299329 #[ test]
300330 $( $( #[ $f32_const_meta] ) + ) ?
301331 fn test_f32( ) {
302332 type $fty = f32 ;
333+ #[ allow( unused) ]
334+ const fn flt ( x: $fty) -> $fty { x }
303335 const { $test }
304336 }
305337
306338 #[ test]
307339 $( $( #[ $f64_const_meta] ) + ) ?
308340 fn test_f64( ) {
309341 type $fty = f64 ;
342+ #[ allow( unused) ]
343+ const fn flt ( x: $fty) -> $fty { x }
310344 const { $test }
311345 }
312346
313347 #[ test]
314348 $( $( #[ $f128_const_meta] ) + ) ?
315349 fn test_f128( ) {
316350 type $fty = f128;
351+ #[ allow( unused) ]
352+ const fn flt ( x: $fty) -> $fty { x }
317353 const { $test }
318354 }
319355 }
@@ -1479,3 +1515,30 @@ float_test! {
14791515 assert_approx_eq!( a. algebraic_rem( b) , a % b, Float :: EPS_DIV ) ;
14801516 }
14811517}
1518+
1519+ float_test ! {
1520+ name: to_bits_conv,
1521+ attrs: {
1522+ f16: #[ cfg( target_has_reliable_f16) ] ,
1523+ f128: #[ cfg( target_has_reliable_f128) ] ,
1524+ } ,
1525+ test<Float > {
1526+ assert_biteq!( flt( 1.0 ) , Float :: RAW_1 ) ;
1527+ assert_biteq!( flt( 12.5 ) , Float :: RAW_12_DOT_5 ) ;
1528+ assert_biteq!( flt( 1337.0 ) , Float :: RAW_1337 ) ;
1529+ assert_biteq!( flt( -14.25 ) , Float :: RAW_MINUS_14_DOT_25 ) ;
1530+ assert_biteq!( Float :: RAW_1 , 1.0 ) ;
1531+ assert_biteq!( Float :: RAW_12_DOT_5 , 12.5 ) ;
1532+ assert_biteq!( Float :: RAW_1337 , 1337.0 ) ;
1533+ assert_biteq!( Float :: RAW_MINUS_14_DOT_25 , -14.25 ) ;
1534+
1535+ // Check that NaNs roundtrip their bits regardless of signaling-ness
1536+ let masked_nan1 = Float :: NAN . to_bits( ) ^ Float :: NAN_MASK1 ;
1537+ let masked_nan2 = Float :: NAN . to_bits( ) ^ Float :: NAN_MASK2 ;
1538+ assert!( Float :: from_bits( masked_nan1) . is_nan( ) ) ;
1539+ assert!( Float :: from_bits( masked_nan2) . is_nan( ) ) ;
1540+
1541+ assert_biteq!( Float :: from_bits( masked_nan1) , Float :: from_bits( masked_nan1) ) ;
1542+ assert_biteq!( Float :: from_bits( masked_nan2) , Float :: from_bits( masked_nan2) ) ;
1543+ }
1544+ }
0 commit comments