Skip to content

Commit b5cdbd4

Browse files
committed
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.
1 parent 0ce5308 commit b5cdbd4

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

crates/libm-test/src/f8_impl.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ impl Float for f8 {
3030
const INFINITY: Self = Self(0b0_1111_000);
3131
const NEG_INFINITY: Self = Self(0b1_1111_000);
3232
const NAN: Self = Self(0b0_1111_100);
33+
// FIXME: incorrect values
34+
const EPSILON: Self = Self::ZERO;
3335
const PI: Self = Self::ZERO;
3436
const NEG_PI: Self = Self::ZERO;
3537
const FRAC_PI_2: Self = Self::ZERO;

src/math/generic/sqrt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ where
9696
ix = scaled.to_bits();
9797
match top {
9898
Exp::Shifted(ref mut v) => {
99-
*v = scaled.exp().unsigned();
99+
*v = scaled.exp();
100100
*v = (*v).wrapping_sub(F::SIG_BITS);
101101
}
102102
Exp::NoShift(()) => {

src/math/support/float_traits.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub trait Float:
3434
const NAN: Self;
3535
const MAX: Self;
3636
const MIN: Self;
37+
const EPSILON: Self;
3738
const PI: Self;
3839
const NEG_PI: Self;
3940
const FRAC_PI_2: Self;
@@ -107,13 +108,13 @@ pub trait Float:
107108
}
108109

109110
/// Returns the exponent, not adjusting for bias, not accounting for subnormals or zero.
110-
fn exp(self) -> i32 {
111-
(u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_MAX).signed()
111+
fn exp(self) -> u32 {
112+
u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_MAX
112113
}
113114

114115
/// Extract the exponent and adjust it for bias, not accounting for subnormals or zero.
115116
fn exp_unbiased(self) -> i32 {
116-
self.exp() - (Self::EXP_BIAS as i32)
117+
self.exp().signed() - (Self::EXP_BIAS as i32)
117118
}
118119

119120
/// Returns the significand with no implicit bit (or the "fractional" part)
@@ -180,6 +181,7 @@ macro_rules! float_impl {
180181
const MAX: Self = -Self::MIN;
181182
// Sign bit set, saturated mantissa, saturated exponent with last bit zeroed
182183
const MIN: Self = $from_bits(Self::Int::MAX & !(1 << Self::SIG_BITS));
184+
const EPSILON: Self = <$ty>::EPSILON;
183185

184186
const PI: Self = core::$ty::consts::PI;
185187
const NEG_PI: Self = -Self::PI;

0 commit comments

Comments
 (0)