From a7cd13b6a32874dd6bc18cdb7e7d7e6bda105adf Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Wed, 22 Jan 2025 07:13:45 +0000 Subject: [PATCH] Make `Float::exp` return an unsigned integer `exp` does not perform any form of unbiasing, so there isn't any reason it should be signed. Change this. Additionally, add `EPSILON` to the `Float` trait. --- .../libm/crates/libm-test/src/f8_impl.rs | 2 ++ library/compiler-builtins/libm/src/math/generic/sqrt.rs | 2 +- .../libm/src/math/support/float_traits.rs | 8 +++++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/library/compiler-builtins/libm/crates/libm-test/src/f8_impl.rs b/library/compiler-builtins/libm/crates/libm-test/src/f8_impl.rs index 299553d202ad..96b78392453f 100644 --- a/library/compiler-builtins/libm/crates/libm-test/src/f8_impl.rs +++ b/library/compiler-builtins/libm/crates/libm-test/src/f8_impl.rs @@ -30,6 +30,8 @@ impl Float for f8 { const INFINITY: Self = Self(0b0_1111_000); const NEG_INFINITY: Self = Self(0b1_1111_000); const NAN: Self = Self(0b0_1111_100); + // FIXME: incorrect values + const EPSILON: Self = Self::ZERO; const PI: Self = Self::ZERO; const NEG_PI: Self = Self::ZERO; const FRAC_PI_2: Self = Self::ZERO; diff --git a/library/compiler-builtins/libm/src/math/generic/sqrt.rs b/library/compiler-builtins/libm/src/math/generic/sqrt.rs index c892f9997241..22ee93f57a3c 100644 --- a/library/compiler-builtins/libm/src/math/generic/sqrt.rs +++ b/library/compiler-builtins/libm/src/math/generic/sqrt.rs @@ -96,7 +96,7 @@ where ix = scaled.to_bits(); match top { Exp::Shifted(ref mut v) => { - *v = scaled.exp().unsigned(); + *v = scaled.exp(); *v = (*v).wrapping_sub(F::SIG_BITS); } Exp::NoShift(()) => { diff --git a/library/compiler-builtins/libm/src/math/support/float_traits.rs b/library/compiler-builtins/libm/src/math/support/float_traits.rs index 2e467111f32f..1abb7c4ded84 100644 --- a/library/compiler-builtins/libm/src/math/support/float_traits.rs +++ b/library/compiler-builtins/libm/src/math/support/float_traits.rs @@ -34,6 +34,7 @@ pub trait Float: const NAN: Self; const MAX: Self; const MIN: Self; + const EPSILON: Self; const PI: Self; const NEG_PI: Self; const FRAC_PI_2: Self; @@ -107,13 +108,13 @@ pub trait Float: } /// Returns the exponent, not adjusting for bias, not accounting for subnormals or zero. - fn exp(self) -> i32 { - (u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_MAX).signed() + fn exp(self) -> u32 { + u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_MAX } /// Extract the exponent and adjust it for bias, not accounting for subnormals or zero. fn exp_unbiased(self) -> i32 { - self.exp() - (Self::EXP_BIAS as i32) + self.exp().signed() - (Self::EXP_BIAS as i32) } /// Returns the significand with no implicit bit (or the "fractional" part) @@ -180,6 +181,7 @@ macro_rules! float_impl { const MAX: Self = -Self::MIN; // Sign bit set, saturated mantissa, saturated exponent with last bit zeroed const MIN: Self = $from_bits(Self::Int::MAX & !(1 << Self::SIG_BITS)); + const EPSILON: Self = <$ty>::EPSILON; const PI: Self = core::$ty::consts::PI; const NEG_PI: Self = -Self::PI;