Skip to content

Commit db5cc29

Browse files
authored
Rollup merge of #146152 - rperier:unify_and_dedup_algebraic_float_tests, r=tgross35
Unify and deduplicate algebraic float tests cc #141726 This is a proposal to unify and deduplicate the algebraic tests for f16, f32, f64 and f128
2 parents 3b8d411 + 4e87237 commit db5cc29

File tree

5 files changed

+44
-83
lines changed

5 files changed

+44
-83
lines changed

library/coretests/tests/floats/f128.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// FIXME(f16_f128): only tested on platforms that have symbols and aren't buggy
22
#![cfg(target_has_reliable_f128)]
33

4-
use super::{assert_approx_eq, assert_biteq};
4+
#[cfg(any(miri, target_has_reliable_f128_math))]
5+
use super::assert_approx_eq;
6+
use super::assert_biteq;
57

68
// Note these tolerances make sense around zero, but not for more extreme exponents.
79

@@ -74,25 +76,6 @@ fn test_float_bits_conv() {
7476
assert_eq!(f128::from_bits(masked_nan2).to_bits(), masked_nan2);
7577
}
7678

77-
#[test]
78-
fn test_algebraic() {
79-
let a: f128 = 123.0;
80-
let b: f128 = 456.0;
81-
82-
// Check that individual operations match their primitive counterparts.
83-
//
84-
// This is a check of current implementations and does NOT imply any form of
85-
// guarantee about future behavior. The compiler reserves the right to make
86-
// these operations inexact matches in the future.
87-
let eps = if cfg!(miri) { 1e-6 } else { 0.0 };
88-
89-
assert_approx_eq!(a.algebraic_add(b), a + b, eps);
90-
assert_approx_eq!(a.algebraic_sub(b), a - b, eps);
91-
assert_approx_eq!(a.algebraic_mul(b), a * b, eps);
92-
assert_approx_eq!(a.algebraic_div(b), a / b, eps);
93-
assert_approx_eq!(a.algebraic_rem(b), a % b, eps);
94-
}
95-
9679
#[test]
9780
fn test_from() {
9881
assert_biteq!(f128::from(false), 0.0);

library/coretests/tests/floats/f16.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,6 @@ fn test_float_bits_conv() {
7373
assert_eq!(f16::from_bits(masked_nan2).to_bits(), masked_nan2);
7474
}
7575

76-
#[test]
77-
fn test_algebraic() {
78-
let a: f16 = 123.0;
79-
let b: f16 = 456.0;
80-
81-
// Check that individual operations match their primitive counterparts.
82-
//
83-
// This is a check of current implementations and does NOT imply any form of
84-
// guarantee about future behavior. The compiler reserves the right to make
85-
// these operations inexact matches in the future.
86-
let eps_add = if cfg!(miri) { 1e1 } else { 0.0 };
87-
let eps_mul = if cfg!(miri) { 1e3 } else { 0.0 };
88-
let eps_div = if cfg!(miri) { 1e0 } else { 0.0 };
89-
90-
assert_approx_eq!(a.algebraic_add(b), a + b, eps_add);
91-
assert_approx_eq!(a.algebraic_sub(b), a - b, eps_add);
92-
assert_approx_eq!(a.algebraic_mul(b), a * b, eps_mul);
93-
assert_approx_eq!(a.algebraic_div(b), a / b, eps_div);
94-
assert_approx_eq!(a.algebraic_rem(b), a % b, eps_div);
95-
}
96-
9776
#[test]
9877
fn test_from() {
9978
assert_biteq!(f16::from(false), 0.0);

library/coretests/tests/floats/f32.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::f32;
22

3-
use super::{assert_approx_eq, assert_biteq};
3+
use super::assert_biteq;
44

55
/// First pattern over the mantissa
66
const NAN_MASK1: u32 = 0x002a_aaaa;
@@ -47,24 +47,3 @@ fn test_float_bits_conv() {
4747
assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
4848
assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
4949
}
50-
51-
#[test]
52-
fn test_algebraic() {
53-
let a: f32 = 123.0;
54-
let b: f32 = 456.0;
55-
56-
// Check that individual operations match their primitive counterparts.
57-
//
58-
// This is a check of current implementations and does NOT imply any form of
59-
// guarantee about future behavior. The compiler reserves the right to make
60-
// these operations inexact matches in the future.
61-
let eps_add = if cfg!(miri) { 1e-3 } else { 0.0 };
62-
let eps_mul = if cfg!(miri) { 1e-1 } else { 0.0 };
63-
let eps_div = if cfg!(miri) { 1e-4 } else { 0.0 };
64-
65-
assert_approx_eq!(a.algebraic_add(b), a + b, eps_add);
66-
assert_approx_eq!(a.algebraic_sub(b), a - b, eps_add);
67-
assert_approx_eq!(a.algebraic_mul(b), a * b, eps_mul);
68-
assert_approx_eq!(a.algebraic_div(b), a / b, eps_div);
69-
assert_approx_eq!(a.algebraic_rem(b), a % b, eps_div);
70-
}

library/coretests/tests/floats/f64.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::f64;
22

3-
use super::{assert_approx_eq, assert_biteq};
3+
use super::assert_biteq;
44

55
/// First pattern over the mantissa
66
const NAN_MASK1: u64 = 0x000a_aaaa_aaaa_aaaa;
@@ -46,22 +46,3 @@ fn test_float_bits_conv() {
4646
assert_eq!(f64::from_bits(masked_nan1).to_bits(), masked_nan1);
4747
assert_eq!(f64::from_bits(masked_nan2).to_bits(), masked_nan2);
4848
}
49-
50-
#[test]
51-
fn test_algebraic() {
52-
let a: f64 = 123.0;
53-
let b: f64 = 456.0;
54-
55-
// Check that individual operations match their primitive counterparts.
56-
//
57-
// This is a check of current implementations and does NOT imply any form of
58-
// guarantee about future behavior. The compiler reserves the right to make
59-
// these operations inexact matches in the future.
60-
let eps = if cfg!(miri) { 1e-6 } else { 0.0 };
61-
62-
assert_approx_eq!(a.algebraic_add(b), a + b, eps);
63-
assert_approx_eq!(a.algebraic_sub(b), a - b, eps);
64-
assert_approx_eq!(a.algebraic_mul(b), a * b, eps);
65-
assert_approx_eq!(a.algebraic_div(b), a / b, eps);
66-
assert_approx_eq!(a.algebraic_rem(b), a % b, eps);
67-
}

library/coretests/tests/floats/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ trait TestableFloat: Sized {
2727
const NAN_MASK1: Self::Int;
2828
/// Second pattern over the mantissa
2929
const NAN_MASK2: Self::Int;
30+
const EPS_ADD: Self;
31+
const EPS_MUL: Self;
32+
const EPS_DIV: Self;
3033
}
3134

3235
impl TestableFloat for f16 {
@@ -44,6 +47,9 @@ impl TestableFloat for f16 {
4447
const MAX_DOWN: Self = Self::from_bits(0x7bfe);
4548
const NAN_MASK1: Self::Int = 0x02aa;
4649
const NAN_MASK2: Self::Int = 0x0155;
50+
const EPS_ADD: Self = if cfg!(miri) { 1e1 } else { 0.0 };
51+
const EPS_MUL: Self = if cfg!(miri) { 1e3 } else { 0.0 };
52+
const EPS_DIV: Self = if cfg!(miri) { 1e0 } else { 0.0 };
4753
}
4854

4955
impl TestableFloat for f32 {
@@ -63,6 +69,9 @@ impl TestableFloat for f32 {
6369
const MAX_DOWN: Self = Self::from_bits(0x7f7f_fffe);
6470
const NAN_MASK1: Self::Int = 0x002a_aaaa;
6571
const NAN_MASK2: Self::Int = 0x0055_5555;
72+
const EPS_ADD: Self = if cfg!(miri) { 1e-3 } else { 0.0 };
73+
const EPS_MUL: Self = if cfg!(miri) { 1e-1 } else { 0.0 };
74+
const EPS_DIV: Self = if cfg!(miri) { 1e-4 } else { 0.0 };
6675
}
6776

6877
impl TestableFloat for f64 {
@@ -78,6 +87,9 @@ impl TestableFloat for f64 {
7887
const MAX_DOWN: Self = Self::from_bits(0x7fef_ffff_ffff_fffe);
7988
const NAN_MASK1: Self::Int = 0x000a_aaaa_aaaa_aaaa;
8089
const NAN_MASK2: Self::Int = 0x0005_5555_5555_5555;
90+
const EPS_ADD: Self = if cfg!(miri) { 1e-6 } else { 0.0 };
91+
const EPS_MUL: Self = if cfg!(miri) { 1e-6 } else { 0.0 };
92+
const EPS_DIV: Self = if cfg!(miri) { 1e-6 } else { 0.0 };
8193
}
8294

8395
impl TestableFloat for f128 {
@@ -93,6 +105,9 @@ impl TestableFloat for f128 {
93105
const MAX_DOWN: Self = Self::from_bits(0x7ffefffffffffffffffffffffffffffe);
94106
const NAN_MASK1: Self::Int = 0x0000aaaaaaaaaaaaaaaaaaaaaaaaaaaa;
95107
const NAN_MASK2: Self::Int = 0x00005555555555555555555555555555;
108+
const EPS_ADD: Self = if cfg!(miri) { 1e-6 } else { 0.0 };
109+
const EPS_MUL: Self = if cfg!(miri) { 1e-6 } else { 0.0 };
110+
const EPS_DIV: Self = if cfg!(miri) { 1e-6 } else { 0.0 };
96111
}
97112

98113
/// Determine the tolerance for values of the argument type.
@@ -1440,3 +1455,27 @@ float_test! {
14401455
assert_biteq!(neg_inf.to_radians(), neg_inf);
14411456
}
14421457
}
1458+
1459+
float_test! {
1460+
name: to_algebraic,
1461+
attrs: {
1462+
f16: #[cfg(target_has_reliable_f16)],
1463+
f128: #[cfg(target_has_reliable_f128)],
1464+
},
1465+
test<Float> {
1466+
let a: Float = 123.0;
1467+
let b: Float = 456.0;
1468+
1469+
// Check that individual operations match their primitive counterparts.
1470+
//
1471+
// This is a check of current implementations and does NOT imply any form of
1472+
// guarantee about future behavior. The compiler reserves the right to make
1473+
// these operations inexact matches in the future.
1474+
1475+
assert_approx_eq!(a.algebraic_add(b), a + b, Float::EPS_ADD);
1476+
assert_approx_eq!(a.algebraic_sub(b), a - b, Float::EPS_ADD);
1477+
assert_approx_eq!(a.algebraic_mul(b), a * b, Float::EPS_MUL);
1478+
assert_approx_eq!(a.algebraic_div(b), a / b, Float::EPS_DIV);
1479+
assert_approx_eq!(a.algebraic_rem(b), a % b, Float::EPS_DIV);
1480+
}
1481+
}

0 commit comments

Comments
 (0)