Skip to content

Add a feature to enforce libm in std environment#375

Open
ElouanPetereau wants to merge 3 commits intorust-num:masterfrom
ElouanPetereau:feature/force_libm
Open

Add a feature to enforce libm in std environment#375
ElouanPetereau wants to merge 3 commits intorust-num:masterfrom
ElouanPetereau:feature/force_libm

Conversation

@ElouanPetereau
Copy link

This pull request adds a feature named force-libm to num-traits.

Why

Currently, when both std and libm features are enabled, std always wins due to the not(feature = "std") guard on the libm code paths. There is no way to use libm implementations in an std environment afaik.

This is something I need for reproducible cross-platform float math and, since I saw not open issue regarding this,I chose to propose my solution if you are interested.

How

I added a new cumulative force-libm Cargo feature which implies that libm is also enabled. It can be combined with std (for everything else std provides) while routing float math through libm.
When enabled, it overrides std for float math operations by changing the conditional compilation gates:

  • #[cfg(feature = "std")] on math impls becomes #[cfg(all(feature = "std", not(feature = "force-libm")))]
  • #[cfg(all(not(feature = "std"), feature = "libm"))] becomes #[cfg(any(feature = "force-libm", all(not(feature = "std"), feature = "libm")))]

I also updated test_full.sh to verifies that the feature works. It checks that the linked test binary contains libm::math symbols when force-libm is enabled and does not when using std alone.
This is the only reliable solution I found but feel free to propose other solution.


Thank you for your work in maintaining this project 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant