Skip to content

Commit

Permalink
Use libm for no_std support
Browse files Browse the repository at this point in the history
  • Loading branch information
staticintlucas committed May 1, 2024
1 parent e4acd0c commit c7aa13a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ jobs:
combo: [
{ rust: stable, flags: --all-features },
{ rust: 1.70.0, flags: --all-features },
{ rust: stable, flags: --no-default-features },
{ rust: 1.46.0, flags: --no-default-features },
{ rust: stable, flags: --features=default },
{ rust: 1.60.0, flags: --features=default },
{ rust: stable, flags: --no-default-features --features=libm },
{ rust: 1.60.0, flags: --no-default-features --features=libm },
]
steps:
- name: Checkout
Expand Down
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ keywords = ["float", "equal", "isclose", "is_close", "approximate"]
categories = ["command-line-utilities", "graphics", "multimedia::images", "rendering"]

[features]
default = ["std"]
std = ["euclid?/std"]
libm = ["dep:libm", "euclid?/libm"]
half = ["dep:half"]
euclid = ["dep:euclid"]

[dependencies]
half = { version = "2", default-features = false, optional = true }
euclid = { version = "0.22", default-features = false, features = ["std"], optional = true }
euclid = { version = "0.22", default-features = false, optional = true }
half = { version = "2", default-features = false, features = ["num-traits"], optional= true }
libm = { version = "0.2", optional = true }

[package.metadata.docs.rs]
all-features = true
21 changes: 21 additions & 0 deletions src/euclid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -32,6 +33,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -52,6 +54,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -72,6 +75,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -94,6 +98,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -113,6 +118,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -133,6 +139,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -154,6 +161,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -174,6 +182,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -197,6 +206,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -216,6 +226,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -238,6 +249,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -257,6 +269,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -279,6 +292,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -299,6 +313,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -320,6 +335,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -344,6 +360,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand Down Expand Up @@ -378,6 +395,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -398,6 +416,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -419,6 +438,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand All @@ -439,6 +459,7 @@ where
const ABS_TOL: T = T::ABS_TOL;
const REL_TOL: T = T::REL_TOL;

#[inline]
fn is_close_tol(
&self,
other: impl core::borrow::Borrow<Self>,
Expand Down
48 changes: 26 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,35 @@ trait Abs {
fn abs(self) -> Self;
}

impl Abs for f32 {
#[cfg(feature = "std")]
mod abs {
impl crate::Abs for f32 {
fn abs(self) -> Self {
#[cfg(feature = "std")]
{
Self::abs(self)
}
#[cfg(not(feature = "std"))]
{
Self::from_bits(self.to_bits() & 0x7fff_ffff)
}
Self::abs(self)
}
}

impl Abs for f64 {
impl crate::Abs for f64 {
fn abs(self) -> Self {
#[cfg(feature = "std")]
{
Self::abs(self)
}
#[cfg(not(feature = "std"))]
{
Self::from_bits(self.to_bits() & 0x7fff_ffff_ffff_ffff)
}
Self::abs(self)
}
}
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
mod abs {
impl crate::Abs for f32 {
fn abs(self) -> Self {
libm::fabsf(self)
}
}

impl crate::Abs for f64 {
fn abs(self) -> Self {
libm::fabs(self)
}
}
}

/// Trait used to return a generic zero value for the tolerance
pub trait Zero {
Expand Down Expand Up @@ -148,8 +152,8 @@ impl IsClose for f32 {
abs_tol: impl Borrow<Self>,
) -> bool {
let (other, rel_tol, abs_tol) = (other.borrow(), rel_tol.borrow(), abs_tol.borrow());
let tol = Self::max(Abs::abs(*self), Abs::abs(*self)) * rel_tol + abs_tol;
Abs::abs(*self - *other) <= tol
let tol = Self::max(self.abs(), other.abs()) * rel_tol + abs_tol;
(*self - *other).abs() <= tol
}
}

Expand All @@ -169,8 +173,8 @@ impl IsClose for f64 {
abs_tol: impl Borrow<Self>,
) -> bool {
let (other, rel_tol, abs_tol) = (other.borrow(), rel_tol.borrow(), abs_tol.borrow());
let tol = Self::max(Abs::abs(*self), Abs::abs(*self)) * rel_tol + abs_tol;
Abs::abs(*self - *other) <= tol
let tol = Self::max(self.abs(), other.abs()) * rel_tol + abs_tol;
(*self - *other).abs() <= tol
}
}

Expand Down

0 comments on commit c7aa13a

Please sign in to comment.