rust ffast-math (defunct, use lower)
seperate float methods
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/generic_float.rs | 92 |
2 files changed, 51 insertions, 43 deletions
@@ -1,6 +1,6 @@ [package] name = "umath" -version = "0.0.3" +version = "0.0.4" description = "ffast-math in rust" license = "MIT" repository = "https://github.com/bend-n/umath" diff --git a/src/generic_float.rs b/src/generic_float.rs index 752441c..bd7218e 100644 --- a/src/generic_float.rs +++ b/src/generic_float.rs @@ -80,13 +80,55 @@ pub trait Constructors { unsafe fn max() -> Self; } -/// Generic float trait, implemented by {[`FFloat`], [`f32`], [`f64`]}. -/// The main purpose of this is to be taken (generically) by optionally fast functions. -/// +/// Methods on a float. /// If there is a method you would like to see on this trait, please open a issue. /// /// Do note that the implementations of these functions are provided by std. /// These functions are not likely to be faster than the std counterparts, unless the implementation is software provided and can benefit from fast math. +pub trait FloatMethods { + /// Refer to [`f32::trunc`] + fn trunc(self) -> Self; + + /// Refer to [`f32::fract`] + fn fract(self) -> Self; + + /// Refer to [`f32::abs`] + fn abs(self) -> Self; + + /// Refer to [`f32::powi`] + fn powi(self, n: i32) -> Self; + + /// Refer to [`f32::powf`] + fn powf(self, n: Self) -> Self; + + /// Refer to [`f32::sqrt`] + fn sqrt(self) -> Self; + + /// Refer to [`f32::cbrt`] + fn cbrt(self) -> Self; + + /// Refer to [`f32::hypot`] + fn hypot(self, other: Self) -> Self; + + /// Refer to [`f32::exp2`] + fn exp2(self) -> Self; + + /// Refer to [`f32::ln`] + fn ln(self) -> Self; + + /// Refer to [`f32::log`] + fn log(self, base: Self) -> Self; + + /// Refer to [`f32::min`] + fn min(self, other: Self) -> Self; + + /// Refer to [`f32::max`] + fn max(self, other: Self) -> Self; +} + +/// Generic float trait, implemented by {[`FFloat`], [`f32`], [`f64`]}. Takes a "base" argument, intended to be set to {[`f32`], [`f64`]}. +/// The main purpose of this is to be taken (generically) by optionally fast functions. +/// /// /// # Safety /// @@ -101,6 +143,7 @@ pub trait Float<F>: + Trig + Rounding + Constructors + + FloatMethods + Add<Self, Output = Self> + Add<F, Output = Self> + Sub<Self, Output = Self> @@ -131,45 +174,6 @@ where /// Returns this float fn take(self) -> F; - - /// Refer to [`f32::trunc`] - fn trunc(self) -> Self; - - /// Refer to [`f32::fract`] - fn fract(self) -> Self; - - /// Refer to [`f32::abs`] - fn abs(self) -> Self; - - /// Refer to [`f32::powi`] - fn powi(self, n: i32) -> Self; - - /// Refer to [`f32::powf`] - fn powf(self, n: Self) -> Self; - - /// Refer to [`f32::sqrt`] - fn sqrt(self) -> Self; - - /// Refer to [`f32::cbrt`] - fn cbrt(self) -> Self; - - /// Refer to [`f32::hypot`] - fn hypot(self, other: Self) -> Self; - - /// Refer to [`f32::exp2`] - fn exp2(self) -> Self; - - /// Refer to [`f32::ln`] - fn ln(self) -> Self; - - /// Refer to [`f32::log`] - fn log(self, base: Self) -> Self; - - /// Refer to [`f32::min`] - fn min(self, other: Self) -> Self; - - /// Refer to [`f32::max`] - fn max(self, other: Self) -> Self; } macro_rules! impf { @@ -182,6 +186,8 @@ macro_rules! impf { fn take(self) -> $for { self } + } + impl FloatMethods for $for { fn trunc(self) -> $for { self.trunc() } @@ -272,7 +278,9 @@ impl<F: FastFloat + Float<F>> Float<F> for FFloat<F> { fn take(self) -> F { self.0 } +} +impl<F: FloatMethods + FastFloat + Float<F>> FloatMethods for FFloat<F> { reuse!(fn trunc); reuse!(fn fract); reuse!(fn abs); |