Skip to content

Commit

Permalink
Merge #280
Browse files Browse the repository at this point in the history
280: Update Float forwarding for MSRV 1.31 r=cuviper a=cuviper

Core floats got more methods since these were originally written.


Co-authored-by: Josh Stone <cuviper@gmail.com>
  • Loading branch information
bors[bot] and cuviper committed Jul 20, 2023
2 parents c263032 + 4f08176 commit 13cd223
Showing 1 changed file with 48 additions and 112 deletions.
160 changes: 48 additions & 112 deletions src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,66 +784,36 @@ impl FloatCore for f32 {
integer_decode_f32(self)
}

#[inline]
#[cfg(not(feature = "std"))]
fn classify(self) -> FpCategory {
const EXP_MASK: u32 = 0x7f800000;
const MAN_MASK: u32 = 0x007fffff;

let bits: u32 = self.to_bits();
match (bits & MAN_MASK, bits & EXP_MASK) {
(0, 0) => FpCategory::Zero,
(_, 0) => FpCategory::Subnormal,
(0, EXP_MASK) => FpCategory::Infinite,
(_, EXP_MASK) => FpCategory::Nan,
_ => FpCategory::Normal,
}
}

#[inline]
#[cfg(not(feature = "std"))]
fn is_sign_negative(self) -> bool {
const SIGN_MASK: u32 = 0x80000000;

self.to_bits() & SIGN_MASK != 0
}

#[inline]
#[cfg(not(feature = "std"))]
fn to_degrees(self) -> Self {
// Use a constant for better precision.
const PIS_IN_180: f32 = 57.2957795130823208767981548141051703_f32;
self * PIS_IN_180
}

#[inline]
#[cfg(not(feature = "std"))]
fn to_radians(self) -> Self {
self * (f32::consts::PI / 180.0)
}

#[cfg(feature = "std")]
forward! {
Self::is_nan(self) -> bool;
Self::is_infinite(self) -> bool;
Self::is_finite(self) -> bool;
Self::is_normal(self) -> bool;
Self::classify(self) -> FpCategory;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Self::min(self, other: Self) -> Self;
Self::max(self, other: Self) -> Self;
Self::recip(self) -> Self;
Self::to_degrees(self) -> Self;
Self::to_radians(self) -> Self;
}

#[cfg(has_is_subnormal)]
forward! {
Self::is_subnormal(self) -> bool;
}

#[cfg(feature = "std")]
forward! {
Self::floor(self) -> Self;
Self::ceil(self) -> Self;
Self::round(self) -> Self;
Self::trunc(self) -> Self;
Self::fract(self) -> Self;
Self::abs(self) -> Self;
Self::signum(self) -> Self;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Self::min(self, other: Self) -> Self;
Self::max(self, other: Self) -> Self;
Self::recip(self) -> Self;
Self::powi(self, n: i32) -> Self;
Self::to_degrees(self) -> Self;
Self::to_radians(self) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
Expand All @@ -853,8 +823,6 @@ impl FloatCore for f32 {
libm::roundf as round(self) -> Self;
libm::truncf as trunc(self) -> Self;
libm::fabsf as abs(self) -> Self;
libm::fminf as min(self, other: Self) -> Self;
libm::fmaxf as max(self, other: Self) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
Expand All @@ -881,65 +849,17 @@ impl FloatCore for f64 {
integer_decode_f64(self)
}

#[inline]
#[cfg(not(feature = "std"))]
fn classify(self) -> FpCategory {
const EXP_MASK: u64 = 0x7ff0000000000000;
const MAN_MASK: u64 = 0x000fffffffffffff;

let bits: u64 = self.to_bits();
match (bits & MAN_MASK, bits & EXP_MASK) {
(0, 0) => FpCategory::Zero,
(_, 0) => FpCategory::Subnormal,
(0, EXP_MASK) => FpCategory::Infinite,
(_, EXP_MASK) => FpCategory::Nan,
_ => FpCategory::Normal,
}
}

#[inline]
#[cfg(not(feature = "std"))]
fn is_sign_negative(self) -> bool {
const SIGN_MASK: u64 = 0x8000000000000000;

self.to_bits() & SIGN_MASK != 0
}

#[inline]
#[cfg(not(feature = "std"))]
fn to_degrees(self) -> Self {
// The division here is correctly rounded with respect to the true
// value of 180/π. (This differs from f32, where a constant must be
// used to ensure a correctly rounded result.)
self * (180.0 / f64::consts::PI)
}

#[inline]
#[cfg(not(feature = "std"))]
fn to_radians(self) -> Self {
self * (f64::consts::PI / 180.0)
}

#[cfg(feature = "std")]
forward! {
Self::is_nan(self) -> bool;
Self::is_infinite(self) -> bool;
Self::is_finite(self) -> bool;
Self::is_normal(self) -> bool;
Self::classify(self) -> FpCategory;
Self::floor(self) -> Self;
Self::ceil(self) -> Self;
Self::round(self) -> Self;
Self::trunc(self) -> Self;
Self::fract(self) -> Self;
Self::abs(self) -> Self;
Self::signum(self) -> Self;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Self::min(self, other: Self) -> Self;
Self::max(self, other: Self) -> Self;
Self::recip(self) -> Self;
Self::powi(self, n: i32) -> Self;
Self::to_degrees(self) -> Self;
Self::to_radians(self) -> Self;
}
Expand All @@ -949,15 +869,25 @@ impl FloatCore for f64 {
Self::is_subnormal(self) -> bool;
}

#[cfg(feature = "std")]
forward! {
Self::floor(self) -> Self;
Self::ceil(self) -> Self;
Self::round(self) -> Self;
Self::trunc(self) -> Self;
Self::fract(self) -> Self;
Self::abs(self) -> Self;
Self::signum(self) -> Self;
Self::powi(self, n: i32) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
forward! {
libm::floor as floor(self) -> Self;
libm::ceil as ceil(self) -> Self;
libm::round as round(self) -> Self;
libm::trunc as trunc(self) -> Self;
libm::fabs as abs(self) -> Self;
libm::fmin as min(self, other: Self) -> Self;
libm::fmax as max(self, other: Self) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
Expand Down Expand Up @@ -2058,18 +1988,28 @@ macro_rules! float_impl_libm {
}

forward! {
FloatCore::is_nan(self) -> bool;
FloatCore::is_infinite(self) -> bool;
FloatCore::is_finite(self) -> bool;
FloatCore::is_normal(self) -> bool;
FloatCore::classify(self) -> FpCategory;
Self::is_nan(self) -> bool;
Self::is_infinite(self) -> bool;
Self::is_finite(self) -> bool;
Self::is_normal(self) -> bool;
Self::classify(self) -> FpCategory;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Self::min(self, other: Self) -> Self;
Self::max(self, other: Self) -> Self;
Self::recip(self) -> Self;
Self::to_degrees(self) -> Self;
Self::to_radians(self) -> Self;
}

#[cfg(has_is_subnormal)]
forward! {
Self::is_subnormal(self) -> bool;
}

forward! {
FloatCore::signum(self) -> Self;
FloatCore::is_sign_positive(self) -> bool;
FloatCore::is_sign_negative(self) -> bool;
FloatCore::recip(self) -> Self;
FloatCore::powi(self, n: i32) -> Self;
FloatCore::to_degrees(self) -> Self;
FloatCore::to_radians(self) -> Self;
}
};
}
Expand Down Expand Up @@ -2149,8 +2089,6 @@ impl Float for f32 {
libm::asinhf as asinh(self) -> Self;
libm::acoshf as acosh(self) -> Self;
libm::atanhf as atanh(self) -> Self;
libm::fmaxf as max(self, other: Self) -> Self;
libm::fminf as min(self, other: Self) -> Self;
libm::copysignf as copysign(self, other: Self) -> Self;
}
}
Expand Down Expand Up @@ -2197,8 +2135,6 @@ impl Float for f64 {
libm::asinh as asinh(self) -> Self;
libm::acosh as acosh(self) -> Self;
libm::atanh as atanh(self) -> Self;
libm::fmax as max(self, other: Self) -> Self;
libm::fmin as min(self, other: Self) -> Self;
libm::copysign as copysign(self, sign: Self) -> Self;
}
}
Expand Down

0 comments on commit 13cd223

Please sign in to comment.