Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use libm in Float::{min,max} and in FloatCore #196

Merged
merged 1 commit into from Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
354 changes: 105 additions & 249 deletions src/float.rs
Expand Up @@ -818,6 +818,23 @@ impl FloatCore for f32 {
Self::to_degrees(self) -> Self;
Self::to_radians(self) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
forward! {
libm::floorf as floor(self) -> Self;
libm::ceilf as ceil(self) -> Self;
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"))]
#[inline]
fn fract(self) -> Self {
self - libm::truncf(self)
}
}

impl FloatCore for f64 {
Expand Down Expand Up @@ -893,6 +910,23 @@ impl FloatCore for f64 {
Self::to_degrees(self) -> Self;
Self::to_radians(self) -> 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"))]
#[inline]
fn fract(self) -> Self {
self - libm::trunc(self)
}
}

// FIXME: these doctests aren't actually helpful, because they're using and
Expand Down Expand Up @@ -1908,7 +1942,7 @@ macro_rules! float_impl_libm {

#[inline]
fn fract(self) -> Self {
self - FloatCore::trunc(self)
self - Float::trunc(self)
}

#[inline]
Expand All @@ -1929,8 +1963,6 @@ macro_rules! float_impl_libm {
FloatCore::powi(self, n: i32) -> Self;
FloatCore::to_degrees(self) -> Self;
FloatCore::to_radians(self) -> Self;
FloatCore::max(self, other: Self) -> Self;
FloatCore::min(self, other: Self) -> Self;
}
};
}
Expand Down Expand Up @@ -1981,129 +2013,41 @@ impl Float for f32 {
fn abs_sub(self, other: Self) -> Self {
libm::fdimf(self, other)
}
#[inline]
fn floor(self) -> Self {
libm::floorf(self)
}
#[inline]
fn ceil(self) -> Self {
libm::ceilf(self)
}
#[inline]
fn round(self) -> Self {
libm::roundf(self)
}
#[inline]
fn trunc(self) -> Self {
libm::truncf(self)
}
#[inline]
fn abs(self) -> Self {
libm::fabsf(self)
}
#[inline]
fn mul_add(self, a: Self, b: Self) -> Self {
libm::fmaf(self, a, b)
}
#[inline]
fn powf(self, n: Self) -> Self {
libm::powf(self, n)
}
#[inline]
fn sqrt(self) -> Self {
libm::sqrtf(self)
}
#[inline]
fn exp(self) -> Self {
libm::expf(self)
}
#[inline]
fn exp2(self) -> Self {
libm::exp2f(self)
}
#[inline]
fn ln(self) -> Self {
libm::logf(self)
}
#[inline]
fn log2(self) -> Self {
libm::log2f(self)
}
#[inline]
fn log10(self) -> Self {
libm::log10f(self)
}
#[inline]
fn cbrt(self) -> Self {
libm::cbrtf(self)
}
#[inline]
fn hypot(self, other: Self) -> Self {
libm::hypotf(self, other)
}
#[inline]
fn sin(self) -> Self {
libm::sinf(self)
}
#[inline]
fn cos(self) -> Self {
libm::cosf(self)
}
#[inline]
fn tan(self) -> Self {
libm::tanf(self)
}
#[inline]
fn asin(self) -> Self {
libm::asinf(self)
}
#[inline]
fn acos(self) -> Self {
libm::acosf(self)
}
#[inline]
fn atan(self) -> Self {
libm::atanf(self)
}
#[inline]
fn atan2(self, other: Self) -> Self {
libm::atan2f(self, other)
}
#[inline]
fn sin_cos(self) -> (Self, Self) {
libm::sincosf(self)
}
#[inline]
fn exp_m1(self) -> Self {
libm::expm1f(self)
}
#[inline]
fn ln_1p(self) -> Self {
libm::log1pf(self)
}
#[inline]
fn sinh(self) -> Self {
libm::sinhf(self)
}
#[inline]
fn cosh(self) -> Self {
libm::coshf(self)
}
#[inline]
fn tanh(self) -> Self {
libm::tanhf(self)
}
#[inline]
fn asinh(self) -> Self {
libm::asinhf(self)
}
#[inline]
fn acosh(self) -> Self {
libm::acoshf(self)
}
#[inline]
fn atanh(self) -> Self {
libm::atanhf(self)

forward! {
libm::floorf as floor(self) -> Self;
libm::ceilf as ceil(self) -> Self;
libm::roundf as round(self) -> Self;
libm::truncf as trunc(self) -> Self;
libm::fabsf as abs(self) -> Self;
libm::fmaf as mul_add(self, a: Self, b: Self) -> Self;
libm::powf as powf(self, n: Self) -> Self;
libm::sqrtf as sqrt(self) -> Self;
libm::expf as exp(self) -> Self;
libm::exp2f as exp2(self) -> Self;
libm::logf as ln(self) -> Self;
libm::log2f as log2(self) -> Self;
libm::log10f as log10(self) -> Self;
libm::cbrtf as cbrt(self) -> Self;
libm::hypotf as hypot(self, other: Self) -> Self;
libm::sinf as sin(self) -> Self;
libm::cosf as cos(self) -> Self;
libm::tanf as tan(self) -> Self;
libm::asinf as asin(self) -> Self;
libm::acosf as acos(self) -> Self;
libm::atanf as atan(self) -> Self;
libm::atan2f as atan2(self, other: Self) -> Self;
libm::sincosf as sin_cos(self) -> (Self, Self);
libm::expm1f as exp_m1(self) -> Self;
libm::log1pf as ln_1p(self) -> Self;
libm::sinhf as sinh(self) -> Self;
libm::coshf as cosh(self) -> Self;
libm::tanhf as tanh(self) -> Self;
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;
}
}

Expand All @@ -2116,129 +2060,41 @@ impl Float for f64 {
fn abs_sub(self, other: Self) -> Self {
libm::fdim(self, other)
}
#[inline]
fn floor(self) -> Self {
libm::floor(self)
}
#[inline]
fn ceil(self) -> Self {
libm::ceil(self)
}
#[inline]
fn round(self) -> Self {
libm::round(self)
}
#[inline]
fn trunc(self) -> Self {
libm::trunc(self)
}
#[inline]
fn abs(self) -> Self {
libm::fabs(self)
}
#[inline]
fn mul_add(self, a: Self, b: Self) -> Self {
libm::fma(self, a, b)
}
#[inline]
fn powf(self, n: Self) -> Self {
libm::pow(self, n)
}
#[inline]
fn sqrt(self) -> Self {
libm::sqrt(self)
}
#[inline]
fn exp(self) -> Self {
libm::exp(self)
}
#[inline]
fn exp2(self) -> Self {
libm::exp2(self)
}
#[inline]
fn ln(self) -> Self {
libm::log(self)
}
#[inline]
fn log2(self) -> Self {
libm::log2(self)
}
#[inline]
fn log10(self) -> Self {
libm::log10(self)
}
#[inline]
fn cbrt(self) -> Self {
libm::cbrt(self)
}
#[inline]
fn hypot(self, other: Self) -> Self {
libm::hypot(self, other)
}
#[inline]
fn sin(self) -> Self {
libm::sin(self)
}
#[inline]
fn cos(self) -> Self {
libm::cos(self)
}
#[inline]
fn tan(self) -> Self {
libm::tan(self)
}
#[inline]
fn asin(self) -> Self {
libm::asin(self)
}
#[inline]
fn acos(self) -> Self {
libm::acos(self)
}
#[inline]
fn atan(self) -> Self {
libm::atan(self)
}
#[inline]
fn atan2(self, other: Self) -> Self {
libm::atan2(self, other)
}
#[inline]
fn sin_cos(self) -> (Self, Self) {
libm::sincos(self)
}
#[inline]
fn exp_m1(self) -> Self {
libm::expm1(self)
}
#[inline]
fn ln_1p(self) -> Self {
libm::log1p(self)
}
#[inline]
fn sinh(self) -> Self {
libm::sinh(self)
}
#[inline]
fn cosh(self) -> Self {
libm::cosh(self)
}
#[inline]
fn tanh(self) -> Self {
libm::tanh(self)
}
#[inline]
fn asinh(self) -> Self {
libm::asinh(self)
}
#[inline]
fn acosh(self) -> Self {
libm::acosh(self)
}
#[inline]
fn atanh(self) -> Self {
libm::atanh(self)

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::fma as mul_add(self, a: Self, b: Self) -> Self;
libm::pow as powf(self, n: Self) -> Self;
libm::sqrt as sqrt(self) -> Self;
libm::exp as exp(self) -> Self;
libm::exp2 as exp2(self) -> Self;
libm::log as ln(self) -> Self;
libm::log2 as log2(self) -> Self;
libm::log10 as log10(self) -> Self;
libm::cbrt as cbrt(self) -> Self;
libm::hypot as hypot(self, other: Self) -> Self;
libm::sin as sin(self) -> Self;
libm::cos as cos(self) -> Self;
libm::tan as tan(self) -> Self;
libm::asin as asin(self) -> Self;
libm::acos as acos(self) -> Self;
libm::atan as atan(self) -> Self;
libm::atan2 as atan2(self, other: Self) -> Self;
libm::sincos as sin_cos(self) -> (Self, Self);
libm::expm1 as exp_m1(self) -> Self;
libm::log1p as ln_1p(self) -> Self;
libm::sinh as sinh(self) -> Self;
libm::cosh as cosh(self) -> Self;
libm::tanh as tanh(self) -> Self;
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;
}
}

Expand Down