Skip to content

Commit c69a792

Browse files
authored
Rollup merge of #146380 - rperier:unify_and_dedup_bits_conv_float_tests, r=tgross35
Unify and deduplicate bits conv float tests cc #141726 This is a proposal to unify and deduplicate the bits conv tests for f16, f32, f64 and f128
2 parents c79c990 + b1c4e19 commit c69a792

File tree

5 files changed

+63
-110
lines changed

5 files changed

+63
-110
lines changed

library/coretests/tests/floats/f128.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ const TOL: f128 = 1e-12;
1717
#[allow(unused)]
1818
const TOL_PRECISE: f128 = 1e-28;
1919

20-
/// First pattern over the mantissa
21-
const NAN_MASK1: u128 = 0x0000aaaaaaaaaaaaaaaaaaaaaaaaaaaa;
22-
23-
/// Second pattern over the mantissa
24-
const NAN_MASK2: u128 = 0x00005555555555555555555555555555;
25-
2620
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
2721
// the intrinsics.
2822

@@ -54,28 +48,6 @@ fn test_max_recip() {
5448
);
5549
}
5650

57-
#[test]
58-
fn test_float_bits_conv() {
59-
assert_eq!((1f128).to_bits(), 0x3fff0000000000000000000000000000);
60-
assert_eq!((12.5f128).to_bits(), 0x40029000000000000000000000000000);
61-
assert_eq!((1337f128).to_bits(), 0x40094e40000000000000000000000000);
62-
assert_eq!((-14.25f128).to_bits(), 0xc002c800000000000000000000000000);
63-
assert_biteq!(f128::from_bits(0x3fff0000000000000000000000000000), 1.0);
64-
assert_biteq!(f128::from_bits(0x40029000000000000000000000000000), 12.5);
65-
assert_biteq!(f128::from_bits(0x40094e40000000000000000000000000), 1337.0);
66-
assert_biteq!(f128::from_bits(0xc002c800000000000000000000000000), -14.25);
67-
68-
// Check that NaNs roundtrip their bits regardless of signaling-ness
69-
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
70-
let masked_nan1 = f128::NAN.to_bits() ^ NAN_MASK1;
71-
let masked_nan2 = f128::NAN.to_bits() ^ NAN_MASK2;
72-
assert!(f128::from_bits(masked_nan1).is_nan());
73-
assert!(f128::from_bits(masked_nan2).is_nan());
74-
75-
assert_eq!(f128::from_bits(masked_nan1).to_bits(), masked_nan1);
76-
assert_eq!(f128::from_bits(masked_nan2).to_bits(), masked_nan2);
77-
}
78-
7951
#[test]
8052
fn test_from() {
8153
assert_biteq!(f128::from(false), 0.0);

library/coretests/tests/floats/f16.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ const TOL_P2: f16 = 0.5;
1919
#[allow(unused)]
2020
const TOL_P4: f16 = 10.0;
2121

22-
/// First pattern over the mantissa
23-
const NAN_MASK1: u16 = 0x02aa;
24-
25-
/// Second pattern over the mantissa
26-
const NAN_MASK2: u16 = 0x0155;
27-
2822
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
2923
// the intrinsics.
3024

@@ -52,27 +46,6 @@ fn test_max_recip() {
5246
assert_approx_eq!(f16::MAX.recip(), 1.526624e-5f16, 1e-4);
5347
}
5448

55-
#[test]
56-
fn test_float_bits_conv() {
57-
assert_eq!((1f16).to_bits(), 0x3c00);
58-
assert_eq!((12.5f16).to_bits(), 0x4a40);
59-
assert_eq!((1337f16).to_bits(), 0x6539);
60-
assert_eq!((-14.25f16).to_bits(), 0xcb20);
61-
assert_biteq!(f16::from_bits(0x3c00), 1.0);
62-
assert_biteq!(f16::from_bits(0x4a40), 12.5);
63-
assert_biteq!(f16::from_bits(0x6539), 1337.0);
64-
assert_biteq!(f16::from_bits(0xcb20), -14.25);
65-
66-
// Check that NaNs roundtrip their bits regardless of signaling-ness
67-
let masked_nan1 = f16::NAN.to_bits() ^ NAN_MASK1;
68-
let masked_nan2 = f16::NAN.to_bits() ^ NAN_MASK2;
69-
assert!(f16::from_bits(masked_nan1).is_nan());
70-
assert!(f16::from_bits(masked_nan2).is_nan());
71-
72-
assert_eq!(f16::from_bits(masked_nan1).to_bits(), masked_nan1);
73-
assert_eq!(f16::from_bits(masked_nan2).to_bits(), masked_nan2);
74-
}
75-
7649
#[test]
7750
fn test_from() {
7851
assert_biteq!(f16::from(false), 0.0);

library/coretests/tests/floats/f32.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ use core::f32;
22

33
use super::assert_biteq;
44

5-
/// First pattern over the mantissa
6-
const NAN_MASK1: u32 = 0x002a_aaaa;
7-
8-
/// Second pattern over the mantissa
9-
const NAN_MASK2: u32 = 0x0055_5555;
10-
115
// FIXME(#140515): mingw has an incorrect fma https://sourceforge.net/p/mingw-w64/bugs/848/
126
#[cfg_attr(all(target_os = "windows", target_env = "gnu", not(target_abi = "llvm")), ignore)]
137
#[test]
@@ -25,25 +19,3 @@ fn test_mul_add() {
2519
assert_biteq!(f32::math::mul_add(8.9f32, inf, 3.2), inf);
2620
assert_biteq!(f32::math::mul_add(-3.2f32, 2.4, neg_inf), neg_inf);
2721
}
28-
29-
#[test]
30-
fn test_float_bits_conv() {
31-
assert_eq!((1f32).to_bits(), 0x3f800000);
32-
assert_eq!((12.5f32).to_bits(), 0x41480000);
33-
assert_eq!((1337f32).to_bits(), 0x44a72000);
34-
assert_eq!((-14.25f32).to_bits(), 0xc1640000);
35-
assert_biteq!(f32::from_bits(0x3f800000), 1.0);
36-
assert_biteq!(f32::from_bits(0x41480000), 12.5);
37-
assert_biteq!(f32::from_bits(0x44a72000), 1337.0);
38-
assert_biteq!(f32::from_bits(0xc1640000), -14.25);
39-
40-
// Check that NaNs roundtrip their bits regardless of signaling-ness
41-
// 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
42-
let masked_nan1 = f32::NAN.to_bits() ^ NAN_MASK1;
43-
let masked_nan2 = f32::NAN.to_bits() ^ NAN_MASK2;
44-
assert!(f32::from_bits(masked_nan1).is_nan());
45-
assert!(f32::from_bits(masked_nan2).is_nan());
46-
47-
assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
48-
assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
49-
}

library/coretests/tests/floats/f64.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ use core::f64;
22

33
use super::assert_biteq;
44

5-
/// First pattern over the mantissa
6-
const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa;
7-
8-
/// Second pattern over the mantissa
9-
const NAN_MASK2: u64 = 0x0005_5555_5555_5555;
10-
115
// FIXME(#140515): mingw has an incorrect fma https://sourceforge.net/p/mingw-w64/bugs/848/
126
#[cfg_attr(all(target_os = "windows", target_env = "gnu", not(target_abi = "llvm")), ignore)]
137
#[test]
@@ -25,24 +19,3 @@ fn test_mul_add() {
2519
assert_biteq!(8.9f64.mul_add(inf, 3.2), inf);
2620
assert_biteq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
2721
}
28-
29-
#[test]
30-
fn test_float_bits_conv() {
31-
assert_eq!((1f64).to_bits(), 0x3ff0000000000000);
32-
assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
33-
assert_eq!((1337f64).to_bits(), 0x4094e40000000000);
34-
assert_eq!((-14.25f64).to_bits(), 0xc02c800000000000);
35-
assert_biteq!(f64::from_bits(0x3ff0000000000000), 1.0);
36-
assert_biteq!(f64::from_bits(0x4029000000000000), 12.5);
37-
assert_biteq!(f64::from_bits(0x4094e40000000000), 1337.0);
38-
assert_biteq!(f64::from_bits(0xc02c800000000000), -14.25);
39-
40-
// Check that NaNs roundtrip their bits regardless of signaling-ness
41-
let masked_nan1 = f64::NAN.to_bits() ^ NAN_MASK1;
42-
let masked_nan2 = f64::NAN.to_bits() ^ NAN_MASK2;
43-
assert!(f64::from_bits(masked_nan1).is_nan());
44-
assert!(f64::from_bits(masked_nan2).is_nan());
45-
46-
assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
47-
assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
48-
}

library/coretests/tests/floats/mod.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3539
impl 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

5563
impl 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

7789
impl 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

95111
impl 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

Comments
 (0)