Skip to content

Commit

Permalink
fix #283: add possibility to include num-traits/libm for sin and cos …
Browse files Browse the repository at this point in the history
…in no_std builds
  • Loading branch information
wucke13 committed Sep 3, 2022
1 parent 5fd23fa commit 92834dc
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-full-test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --no-default-features --features "autoconvert f32 si use_serde"
args: --verbose --no-default-features --features "autoconvert f32 si use_serde libm"

- name: Test si with underlying storage types
uses: actions-rs/cargo@v1
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ use_serde = ["serde"]
rational-support = ["num-rational"]
bigint-support = ["num-bigint", "num-rational/num-bigint-std"]
complex-support = ["num-complex"]
# enable sin/cos for no_std builds, via libm
libm = [ "num-traits/libm" ]

[[example]]
name = "base"
Expand Down
12 changes: 6 additions & 6 deletions src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ macro_rules! si {
($($tt:tt)*) => {};
}

/// Expands the given block of code when `uom` is compiled with the `std` feature.
/// Expands the given block of code when `uom` is compiled with either the `std` or the `libm` feature.
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "std")]
macro_rules! std {
#[cfg(any(feature = "std", feature = "libm"))]
macro_rules! std_or_libm {
($($tt:tt)*) => { $($tt)* };
}

/// Does not expand the given block of code when `uom` is compiled without the `std` feature.
/// Does not expand the given block of code when `uom` is compiled without both the `std` and the `libm` feature.
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "std"))]
macro_rules! std {
#[cfg(not(any(feature = "std", feature = "libm")))]
macro_rules! std_or_libm {
($($tt:tt)*) => {};
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
//! `uom` with `no_std`. Enabled by default.
//! * `use_serde` -- Feature to enable support for serialization and deserialization of quantities
//! with the [Serde][serde] crate. Disabled by default.
//! * `libm` -- Enable advanced floatingpoint functions (sin, cos, ...) on no_std targets
//!
//! [Serde][serde] support for the `big*` and `rational*` underlying storage types requires
//! manually enabling the `serde` feature for the `num-rational` and `num-bigint` crates. To do
Expand Down Expand Up @@ -277,9 +278,9 @@ pub mod lib {
// Conditionally import num sub-crate types based on feature selection.
#[doc(hidden)]
pub mod num {
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
pub use num_traits::float::Float;
#[cfg(not(feature = "std"))]
#[cfg(not(any(feature = "std", feature = "libm")))]
pub use num_traits::float::FloatCore as Float;

pub use num_traits::{pow, FromPrimitive, Num, One, Saturating, Signed, ToPrimitive, Zero};
Expand Down
8 changes: 4 additions & 4 deletions src/si/angle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Angle (dimensionless quantity).

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
use super::ratio::Ratio;

quantity! {
Expand Down Expand Up @@ -64,7 +64,7 @@ impl Angle<crate::si::SI<f64>, f64> {
}

/// Implementation of various stdlib trigonometric functions
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<U, V> Angle<U, V>
where
U: crate::si::Units<V> + ?Sized,
Expand Down Expand Up @@ -121,7 +121,7 @@ where
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<D, U, V> crate::si::Quantity<D, U, V>
where
D: crate::si::Dimension + ?Sized,
Expand Down Expand Up @@ -161,7 +161,7 @@ mod tests {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
mod trig {
storage_types! {
types: Float;
Expand Down
6 changes: 3 additions & 3 deletions src/si/ratio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Ratio (dimensionless quantity).

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
use super::angle::{Angle, radian};

quantity! {
Expand Down Expand Up @@ -33,7 +33,7 @@ quantity! {
}

/// Implementation of various stdlib functions.
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<U, V> Ratio<U, V>
where
U: crate::si::Units<V> + ?Sized,
Expand Down Expand Up @@ -220,7 +220,7 @@ mod tests {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
mod float {
storage_types! {
types: Float;
Expand Down
4 changes: 2 additions & 2 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ macro_rules! system {
self.value.classify()
}

std! {
std_or_libm! {
autoconvert! {
/// Calculates the length of the hypotenuse of a right-angle triangle given the legs.
#[must_use = "method returns a new number and does not mutate the original value"]
Expand Down Expand Up @@ -807,7 +807,7 @@ macro_rules! system {
self.value.is_normal()
}

std! {
std_or_libm! {
/// Takes the cubic root of a number.
///
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
Expand Down
4 changes: 2 additions & 2 deletions src/tests/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ mod float {
Test::assert_eq(&3.3.fract(), &m1.fract::<kilogram>().get::<kilogram>());
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
quickcheck! {
#[allow(trivial_casts)]
fn hypot_same(l: V, r: V) -> bool {
Expand All @@ -477,7 +477,7 @@ mod float {
}
}

#[cfg(all(feature = "std", feature = "autoconvert"))]
#[cfg(all(any(feature = "std", feature = "libm"), feature = "autoconvert"))]
quickcheck! {
#[allow(trivial_casts)]
fn hypot_mixed(l: V, r: V) -> bool {
Expand Down
14 changes: 7 additions & 7 deletions src/tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ mod float {
v.classify() == Length::new::<meter>(*v).classify()
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
#[allow(trivial_casts)]
fn cbrt(v: A<V>) -> bool {
let l: Quantity<Q<P1, Z0, Z0>, U<V>, V> = Quantity::<Q<P3, Z0, Z0>, U<V>, V> {
Expand All @@ -298,7 +298,7 @@ mod float {
Test::eq(&v.cbrt(), &l.value)
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
#[allow(trivial_casts)]
fn hypot(l: A<V>, r: A<V>) -> bool {
Test::eq(&Length::new::<meter>(l.hypot(*r)),
Expand All @@ -315,7 +315,7 @@ mod float {
v.is_sign_negative() == Length::new::<meter>(*v).is_sign_negative()
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
#[allow(trivial_casts)]
fn mul_add(s: A<V>, a: A<V>, b: A<V>) -> bool {
let r: Quantity<Q<P2, Z0, Z0>, U<V>, V> = Length::new::<meter>(*s).mul_add(
Expand All @@ -340,13 +340,13 @@ mod float {
Test::eq(&v.recip(), &a.value)
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
#[allow(trivial_casts)]
fn powi(v: A<V>) -> bool {
Test::eq(&v.powi(3), &Length::new::<meter>(*v).powi(P3::new()).value)
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
#[allow(trivial_casts)]
fn sqrt(v: A<V>) -> TestResult {
if *v < V::zero() {
Expand Down Expand Up @@ -647,7 +647,7 @@ mod complex {
v.is_normal() == Length::new::<meter>(*v).is_normal()
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
#[allow(trivial_casts)]
fn cbrt(v: A<V>) -> bool {
let l: Quantity<Q<P1, Z0, Z0>, U<V>, V> = Quantity::<Q<P3, Z0, Z0>, U<V>, V> {
Expand All @@ -659,7 +659,7 @@ mod complex {
Test::eq(&v.cbrt(), &l.value)
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
#[allow(trivial_casts)]
fn mul_add(s: A<V>, a: A<V>, b: A<V>) -> bool {
#[allow(unused_imports)]
Expand Down

0 comments on commit 92834dc

Please sign in to comment.