Skip to content

Commit eb7cb89

Browse files
committed
implement our own ulps check to test f16
the `float-cmp` and `num-traits` libraries don't (yet) support f16. Turns out we didn't really need much from them, just the ulps check. I've adapted the ulps check from miri instead
1 parent 81e7d7d commit eb7cb89

4 files changed

Lines changed: 11 additions & 15 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/std_float/tests/float.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(portable_simd)]
2+
#![feature(f16)]
23

34
macro_rules! unary_test {
45
{ $scalar:tt, $($func:tt),+ } => {
@@ -91,5 +92,6 @@ macro_rules! impl_tests {
9192
}
9293
}
9394

95+
impl_tests! { f16 }
9496
impl_tests! { f32 }
9597
impl_tests! { f64 }

crates/test_helpers/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ publish = false
66

77
[dependencies]
88
proptest = { workspace = true, features = ["alloc", "std"] }
9-
float-cmp = "0.10"

crates/test_helpers/src/approxeq.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Compare numeric types approximately.
22
3-
use float_cmp::Ulps;
4-
53
pub trait ApproxEq {
64
fn approxeq(&self, other: &Self, _ulps: i64) -> bool;
75
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result;
@@ -43,7 +41,14 @@ macro_rules! impl_float_approxeq {
4341
if self.is_nan() && other.is_nan() {
4442
true
4543
} else {
46-
(self.ulps(other) as i64).abs() <= ulps
44+
let allowed_ulp_diff = ulps;
45+
46+
// Approximate the ULP by taking half the distance between the number one place "up"
47+
// and the number one place "down".
48+
let ulp = (other.next_up() - other.next_down()) / 2.0;
49+
let ulp_diff = ((self - other) / ulp).abs().round() as i64;
50+
51+
ulp_diff <= allowed_ulp_diff
4752
}
4853
}
4954

@@ -55,7 +60,7 @@ macro_rules! impl_float_approxeq {
5560
};
5661
}
5762

58-
impl_float_approxeq! { f32, f64 }
63+
impl_float_approxeq! { f16, f32, f64 }
5964

6065
impl<T: ApproxEq, const N: usize> ApproxEq for [T; N] {
6166
fn approxeq(&self, other: &Self, ulps: i64) -> bool {

0 commit comments

Comments
 (0)