Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ libm = { version = "0.2.0", optional = true }
default = ["std"]
libm = ["dep:libm"]
std = []
force-libm = ["libm"]

# vestigial features, now always in effect
i128 = []
Expand Down
22 changes: 21 additions & 1 deletion ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if ! check_version $MSRV ; then
exit 1
fi

FEATURES=(libm)
FEATURES=(libm force-libm)
echo "Testing supported features: ${FEATURES[*]}"

cargo generate-lockfile
Expand Down Expand Up @@ -58,3 +58,23 @@ cargo test --features="std ${FEATURES[*]}"

cargo build --features="${FEATURES[*]}"
cargo test --features="${FEATURES[*]}"

# Verify that `force-libm` feature actually switches the math implementation
# by checking for libm symbols in the linked test binary.
get_test_binary() {
cargo test --no-run --no-default-features --features="$1" --message-format=json 2>/dev/null \
| grep '"name":"num_traits"' \
| sed -n 's/.*"executable":"\([^"]*\)".*/\1/p'
}

echo "Checking that test binary built with only the 'std' feature DOES NOT contain libm math symbols..."
if nm -C "$(get_test_binary "std")" | grep -q 'libm::math'; then
echo "force-libm symbol check failed: std-only binary should not contain libm math symbols"
exit 1
fi

echo "Checking that test binary built with 'std' and 'force-libm' features DOES contain libm math symbols..."
if ! nm -C "$(get_test_binary "std,force-libm")" | grep -q 'libm::math'; then
echo "force-libm symbol check failed: force-libm binary should contain libm math symbols"
exit 1
fi
24 changes: 12 additions & 12 deletions src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ impl FloatCore for f32 {
Self::to_radians(self) -> Self;
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(feature = "force-libm")))]
forward! {
Self::floor(self) -> Self;
Self::ceil(self) -> Self;
Expand All @@ -844,7 +844,7 @@ impl FloatCore for f32 {
Self::powi(self, n: i32) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
#[cfg(any(feature = "force-libm", all(not(feature = "std"), feature = "libm")))]
forward! {
libm::floorf as floor(self) -> Self;
libm::ceilf as ceil(self) -> Self;
Expand All @@ -853,7 +853,7 @@ impl FloatCore for f32 {
libm::fabsf as abs(self) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
#[cfg(any(feature = "force-libm", all(not(feature = "std"), feature = "libm")))]
#[inline]
fn fract(self) -> Self {
self - libm::truncf(self)
Expand Down Expand Up @@ -894,7 +894,7 @@ impl FloatCore for f64 {
Self::to_radians(self) -> Self;
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(feature = "force-libm")))]
forward! {
Self::floor(self) -> Self;
Self::ceil(self) -> Self;
Expand All @@ -906,7 +906,7 @@ impl FloatCore for f64 {
Self::powi(self, n: i32) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
#[cfg(any(feature = "force-libm", all(not(feature = "std"), feature = "libm")))]
forward! {
libm::floor as floor(self) -> Self;
libm::ceil as ceil(self) -> Self;
Expand All @@ -915,7 +915,7 @@ impl FloatCore for f64 {
libm::fabs as abs(self) -> Self;
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
#[cfg(any(feature = "force-libm", all(not(feature = "std"), feature = "libm")))]
#[inline]
fn fract(self) -> Self {
self - libm::trunc(self)
Expand Down Expand Up @@ -1911,7 +1911,7 @@ pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
}
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(feature = "force-libm")))]
macro_rules! float_impl_std {
($T:ident $decode:ident) => {
impl Float for $T {
Expand Down Expand Up @@ -1993,7 +1993,7 @@ macro_rules! float_impl_std {
};
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
#[cfg(any(feature = "force-libm", all(not(feature = "std"), feature = "libm")))]
macro_rules! float_impl_libm {
($T:ident $decode:ident) => {
constant! {
Expand Down Expand Up @@ -2074,12 +2074,12 @@ fn integer_decode_f64(f: f64) -> (u64, i16, i8) {
(mantissa, exponent, sign)
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(feature = "force-libm")))]
float_impl_std!(f32 integer_decode_f32);
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(feature = "force-libm")))]
float_impl_std!(f64 integer_decode_f64);

#[cfg(all(not(feature = "std"), feature = "libm"))]
#[cfg(any(feature = "force-libm", all(not(feature = "std"), feature = "libm")))]
impl Float for f32 {
float_impl_libm!(f32 integer_decode_f32);

Expand Down Expand Up @@ -2125,7 +2125,7 @@ impl Float for f32 {
}
}

#[cfg(all(not(feature = "std"), feature = "libm"))]
#[cfg(any(feature = "force-libm", all(not(feature = "std"), feature = "libm")))]
impl Float for f64 {
float_impl_libm!(f64 integer_decode_f64);

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ macro_rules! float_trait_impl {
None => return Err(PFE { kind: Invalid }),
};

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(feature = "force-libm")))]
fn pow(base: $t, exp: usize) -> $t {
Float::powi(base, exp as i32)
}
Expand Down
6 changes: 3 additions & 3 deletions src/ops/euclid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ macro_rules! euclid_forward_impl {
euclid_forward_impl!(isize i8 i16 i32 i64 i128);
euclid_forward_impl!(usize u8 u16 u32 u64 u128);

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(feature = "force-libm")))]
euclid_forward_impl!(f32 f64);

#[cfg(not(feature = "std"))]
#[cfg(any(feature = "force-libm", not(feature = "std")))]
impl Euclid for f32 {
#[inline]
fn div_euclid(&self, v: &f32) -> f32 {
Expand All @@ -113,7 +113,7 @@ impl Euclid for f32 {
}
}

#[cfg(not(feature = "std"))]
#[cfg(any(feature = "force-libm", not(feature = "std")))]
impl Euclid for f64 {
#[inline]
fn div_euclid(&self, v: &f64) -> f64 {
Expand Down