Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rint and rintf on x87. #284

Merged
merged 1 commit into from
Oct 6, 2023
Merged

Fix rint and rintf on x87. #284

merged 1 commit into from
Oct 6, 2023

Conversation

plugwash
Copy link

@plugwash plugwash commented Oct 5, 2023

While working on updating the libm crate in Debian I ran into a test failure on Debian i386.

---- math::rintf::tests::sanity_check stdout ----
thread 'math::rintf::tests::sanity_check' panicked at 'assertion failed: `(left == right)`
  left: `2.8`,
 right: `3.0`', src/math/rintf.rs:42:9

---- math::rint::tests::sanity_check stdout ----
thread 'math::rint::tests::sanity_check' panicked at 'assertion failed: `(left == right)`
  left: `2.7998046875`,
 right: `3.0`', src/math/rint.rs:42:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Debian i386 uses the x87 FPU. The x87 FPU suffers from excess precision, where intermediate values are calculated in a higher precsion than their nominal data types can store. Most of the time this results in the results of calculations being slightly more accurate than expected, but when code tries to play tricks with rounding it can result in massively incorrect results.

To work around this, the libm crate contains a force_eval! macro which forces the value to be stored to memory and hence forces it to be rounded to it's nominal data type. This macro is used in a number of places where we need to ensure values are correctly rounded to their nominal format. This was implemented by my previous accepted pull request. #249

However since that was implemented, two new functions rint and rintf were added. These did not take any steps to force rounding to the storage format and hence fail on x87. This PR implements forced rounding in those functions on x87. On other architectures it should be a no-op.

@Amanieu Amanieu merged commit 06f8ba3 into rust-lang:master Oct 6, 2023
12 checks passed
@Amanieu
Copy link
Member

Amanieu commented Oct 6, 2023

Published in v0.2.8

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.

2 participants