@@ -30,6 +30,10 @@ trait TestableFloat: Sized {
30
30
const EPS_ADD : Self ;
31
31
const EPS_MUL : Self ;
32
32
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 ;
33
37
}
34
38
35
39
impl TestableFloat for f16 {
@@ -50,6 +54,10 @@ impl TestableFloat for f16 {
50
54
const EPS_ADD : Self = if cfg ! ( miri) { 1e1 } else { 0.0 } ;
51
55
const EPS_MUL : Self = if cfg ! ( miri) { 1e3 } else { 0.0 } ;
52
56
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 ) ;
53
61
}
54
62
55
63
impl TestableFloat for f32 {
@@ -72,6 +80,10 @@ impl TestableFloat for f32 {
72
80
const EPS_ADD : Self = if cfg ! ( miri) { 1e-3 } else { 0.0 } ;
73
81
const EPS_MUL : Self = if cfg ! ( miri) { 1e-1 } else { 0.0 } ;
74
82
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 ) ;
75
87
}
76
88
77
89
impl TestableFloat for f64 {
@@ -90,6 +102,10 @@ impl TestableFloat for f64 {
90
102
const EPS_ADD : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
91
103
const EPS_MUL : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
92
104
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 ) ;
93
109
}
94
110
95
111
impl TestableFloat for f128 {
@@ -108,6 +124,10 @@ impl TestableFloat for f128 {
108
124
const EPS_ADD : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
109
125
const EPS_MUL : Self = if cfg ! ( miri) { 1e-6 } else { 0.0 } ;
110
126
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 ) ;
111
131
}
112
132
113
133
/// Determine the tolerance for values of the argument type.
@@ -250,27 +270,35 @@ macro_rules! float_test {
250
270
$( $( #[ $f16_meta] ) + ) ?
251
271
fn test_f16( ) {
252
272
type $fty = f16;
273
+ #[ allow( unused) ]
274
+ const fn flt ( x: $fty) -> $fty { x }
253
275
$test
254
276
}
255
277
256
278
#[ test]
257
279
$( $( #[ $f32_meta] ) + ) ?
258
280
fn test_f32( ) {
259
281
type $fty = f32 ;
282
+ #[ allow( unused) ]
283
+ const fn flt ( x: $fty) -> $fty { x }
260
284
$test
261
285
}
262
286
263
287
#[ test]
264
288
$( $( #[ $f64_meta] ) + ) ?
265
289
fn test_f64( ) {
266
290
type $fty = f64 ;
291
+ #[ allow( unused) ]
292
+ const fn flt ( x: $fty) -> $fty { x }
267
293
$test
268
294
}
269
295
270
296
#[ test]
271
297
$( $( #[ $f128_meta] ) + ) ?
272
298
fn test_f128( ) {
273
299
type $fty = f128;
300
+ #[ allow( unused) ]
301
+ const fn flt ( x: $fty) -> $fty { x }
274
302
$test
275
303
}
276
304
@@ -293,27 +321,35 @@ macro_rules! float_test {
293
321
$( $( #[ $f16_const_meta] ) + ) ?
294
322
fn test_f16( ) {
295
323
type $fty = f16;
324
+ #[ allow( unused) ]
325
+ const fn flt ( x: $fty) -> $fty { x }
296
326
const { $test }
297
327
}
298
328
299
329
#[ test]
300
330
$( $( #[ $f32_const_meta] ) + ) ?
301
331
fn test_f32( ) {
302
332
type $fty = f32 ;
333
+ #[ allow( unused) ]
334
+ const fn flt ( x: $fty) -> $fty { x }
303
335
const { $test }
304
336
}
305
337
306
338
#[ test]
307
339
$( $( #[ $f64_const_meta] ) + ) ?
308
340
fn test_f64( ) {
309
341
type $fty = f64 ;
342
+ #[ allow( unused) ]
343
+ const fn flt ( x: $fty) -> $fty { x }
310
344
const { $test }
311
345
}
312
346
313
347
#[ test]
314
348
$( $( #[ $f128_const_meta] ) + ) ?
315
349
fn test_f128( ) {
316
350
type $fty = f128;
351
+ #[ allow( unused) ]
352
+ const fn flt ( x: $fty) -> $fty { x }
317
353
const { $test }
318
354
}
319
355
}
@@ -1479,3 +1515,30 @@ float_test! {
1479
1515
assert_approx_eq!( a. algebraic_rem( b) , a % b, Float :: EPS_DIV ) ;
1480
1516
}
1481
1517
}
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