rust ffast-math (defunct, use lower)
add constants
bendn 2023-10-15
parent 762735e · commit 66bcdae
-rw-r--r--Cargo.toml2
-rw-r--r--src/generic_float.rs70
-rw-r--r--src/lib.rs7
3 files changed, 57 insertions, 22 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 890112c..6404ed8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "umath"
-version = "0.0.6"
+version = "0.0.7"
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 996e62d..6acb269 100644
--- a/src/generic_float.rs
+++ b/src/generic_float.rs
@@ -38,8 +38,33 @@ simp!["Trigonometry functions" trait Trig with sin, asin, sinh, asinh, cos, acos
simp!["Rounding functions" trait Rounding with floor, ceil, round];
simp!["Logarithm functions" trait Log with log(base), log2, log10, ln];
+/// Float constants.
+pub trait Constants {
+ /// returns π
+ #[doc = include_str!("refer.md")]
+ unsafe fn π() -> Self;
+ /// returns ε
+ #[doc = include_str!("refer.md")]
+ unsafe fn ε() -> Self;
+ /// returns E (euler's number)
+ #[doc = include_str!("refer.md")]
+ unsafe fn e() -> Self;
+}
+
macro_rules! ctor {
- ($for:ty) => {
+ ($for:ident) => {
+ impl Constants for $for {
+ unsafe fn π() -> $for {
+ std::$for::consts::PI
+ }
+ unsafe fn e() -> $for {
+ std::$for::consts::E
+ }
+ unsafe fn ε() -> $for {
+ $for::EPSILON
+ }
+ }
+
impl Constructors for $for {
/// Returns 0. This function is safe to call.
unsafe fn zero() -> $for {
@@ -133,6 +158,7 @@ pub trait FloatAlone:
+ PartialOrd
+ Copy
+ Constructors
+ + Constants
+ FloatMethods
+ Add<Self, Output = Self>
+ Sub<Self, Output = Self>
@@ -154,6 +180,7 @@ impl<
+ Copy
+ Constructors
+ FloatMethods
+ + Constants
+ Add<T, Output = T>
+ Sub<T, Output = T>
+ Mul<T, Output = T>
@@ -179,32 +206,17 @@ impl<
/// These functions are not marked `unsafe`, as the entire [`FFloat`] type is essentially unsafe.
/// Calling these functions on a [`f32`] is perfectly safe, even the `unsafe` marked functions (although theres not much point in doing so).
pub trait Float<F>:
- PartialEq
- + PartialOrd
- + PartialOrd<F>
- + Copy
- + Constructors
- + FloatMethods
- + Add<Self, Output = Self>
+ PartialOrd<F>
+ + FloatAlone
+ Add<F, Output = Self>
- + Sub<Self, Output = Self>
+ Sub<F, Output = Self>
- + Mul<Self, Output = Self>
+ Mul<F, Output = Self>
- + Rem<Self, Output = Self>
+ Rem<F, Output = Self>
- + Div<Self, Output = Self>
- + Neg<Output = Self>
+ Div<F, Output = Self>
- + AddAssign<Self>
+ AddAssign<F>
- + SubAssign<Self>
+ SubAssign<F>
- + MulAssign<Self>
+ MulAssign<F>
- + DivAssign<Self>
+ DivAssign<F>
- + RemAssign<Self>
+ RemAssign<F>
where
Self: Sized,
@@ -269,6 +281,24 @@ macro_rules! impf {
impf!(f32);
impf!(f64);
+impl<F: FastFloat + Constants> Constants for FFloat<F> {
+ /// Create a new [`FFloat`] representing the machine epsilon.
+ #[doc = include_str!("ffloat_safety_noconstr.md")]
+ unsafe fn ε() -> Self {
+ Self::new(F::ε())
+ }
+ /// Create a new [`FFloat`] representing π.
+ #[doc = include_str!("ffloat_safety_noconstr.md")]
+ unsafe fn π() -> Self {
+ Self::new(F::π())
+ }
+ /// Create a new [`FFloat`] representing eulers number.
+ #[doc = include_str!("ffloat_safety_noconstr.md")]
+ unsafe fn e() -> Self {
+ Self::new(F::e())
+ }
+}
+
impl<F: FastFloat + Constructors> Constructors for FFloat<F> {
/// Create a new [`FFloat`] representing `0.0`.
#[doc = include_str!("ffloat_safety_noconstr.md")]
@@ -280,12 +310,12 @@ impl<F: FastFloat + Constructors> Constructors for FFloat<F> {
unsafe fn one() -> Self {
Self::new(F::one())
}
- /// Create a new [`FFloat`] representing the minimum value for the inner float..
+ /// Create a new [`FFloat`] representing the minimum value for the inner float.
#[doc = include_str!("ffloat_safety_noconstr.md")]
unsafe fn min() -> Self {
Self::new(F::min())
}
- /// Create a new [`FFloat`] representing the maximum value for the inner float..
+ /// Create a new [`FFloat`] representing the maximum value for the inner float.
#[doc = include_str!("ffloat_safety_noconstr.md")]
unsafe fn max() -> Self {
Self::new(F::max())
diff --git a/src/lib.rs b/src/lib.rs
index 43f53a6..127a910 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -9,7 +9,12 @@
//! ```
#![feature(core_intrinsics)]
#![warn(clippy::pedantic, clippy::dbg_macro, missing_docs)]
-#![allow(clippy::return_self_not_must_use)]
+#![allow(
+ clippy::return_self_not_must_use,
+ mixed_script_confusables,
+ clippy::inline_always,
+ clippy::doc_markdown
+)]
use core::cmp::{Ordering, PartialEq, PartialOrd};
use core::ops::{
Add as add, AddAssign as add_assign, Deref, DerefMut, Div as div, DivAssign as div_assign,