-
Notifications
You must be signed in to change notification settings - Fork 243
implement floor
and ceil
in assembly on i586
#976
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
Open
folkertdev
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
folkertdev:i586-floor-ceil
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−53
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,64 @@ | ||
//! Architecture-specific support for x86-32 without SSE2 | ||
//! | ||
//! We use an alternative implementation on x86, because the | ||
//! main implementation fails with the x87 FPU used by | ||
//! debian i386, probably due to excess precision issues. | ||
//! | ||
//! See https://github.com/rust-lang/compiler-builtins/pull/976 for discussion on why these | ||
//! functions are implemented in this way. | ||
use super::super::fabs; | ||
// FIXME: when the MSRV allows, use naked functions instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there actually any reason to do this now? Being able to inline this assembly should be advantageous, and from a quick look at godbolt it looks like this function is only 13 instructions (compared to 14 from the NetBSD implementations). |
||
|
||
/// Use an alternative implementation on x86, because the | ||
/// main implementation fails with the x87 FPU used by | ||
/// debian i386, probably due to excess precision issues. | ||
/// Basic implementation taken from https://github.com/rust-lang/libm/issues/219. | ||
pub fn ceil(x: f64) -> f64 { | ||
if fabs(x).to_bits() < 4503599627370496.0_f64.to_bits() { | ||
let truncated = x as i64 as f64; | ||
if truncated < x { | ||
return truncated + 1.0; | ||
} else { | ||
return truncated; | ||
} | ||
} else { | ||
return x; | ||
pub extern "C" fn ceil(mut x: f64) -> f64 { | ||
unsafe { | ||
core::arch::asm!( | ||
"fld qword ptr [{x}]", | ||
// Save the FPU control word, using `x` as scratch space. | ||
"fstcw [{x}]", | ||
// Set rounding control to 0b10 (+∞). | ||
"mov word ptr [{x} + 2], 0x0b7f", | ||
"fldcw [{x} + 2]", | ||
// Round. | ||
"frndint", | ||
// Restore FPU control word. | ||
"fldcw [{x}]", | ||
// Save rounded value to memory. | ||
"fstp qword ptr [{x}]", | ||
x = in(reg) &mut x, | ||
// All the x87 FPU stack is used, all registers must be clobbered | ||
out("st(0)") _, out("st(1)") _, | ||
out("st(2)") _, out("st(3)") _, | ||
out("st(4)") _, out("st(5)") _, | ||
out("st(6)") _, out("st(7)") _, | ||
options(nostack), | ||
); | ||
} | ||
x | ||
} | ||
|
||
/// Use an alternative implementation on x86, because the | ||
/// main implementation fails with the x87 FPU used by | ||
/// debian i386, probably due to excess precision issues. | ||
/// Basic implementation taken from https://github.com/rust-lang/libm/issues/219. | ||
pub fn floor(x: f64) -> f64 { | ||
if fabs(x).to_bits() < 4503599627370496.0_f64.to_bits() { | ||
let truncated = x as i64 as f64; | ||
if truncated > x { | ||
return truncated - 1.0; | ||
} else { | ||
return truncated; | ||
} | ||
} else { | ||
return x; | ||
pub extern "C" fn floor(mut x: f64) -> f64 { | ||
unsafe { | ||
core::arch::asm!( | ||
"fld qword ptr [{x}]", | ||
// Save the FPU control word, using `x` as scratch space. | ||
"fstcw [{x}]", | ||
// Set rounding control to 0b01 (-∞). | ||
"mov word ptr [{x} + 2], 0x077f", | ||
"fldcw [{x} + 2]", | ||
// Round. | ||
"frndint", | ||
// Restore FPU control word. | ||
"fldcw [{x}]", | ||
// Save rounded value to memory. | ||
"fstp qword ptr [{x}]", | ||
x = in(reg) &mut x, | ||
// All the x87 FPU stack is used, all registers must be clobbered | ||
out("st(0)") _, out("st(1)") _, | ||
out("st(2)") _, out("st(3)") _, | ||
out("st(4)") _, out("st(5)") _, | ||
out("st(6)") _, out("st(7)") _, | ||
options(nostack), | ||
); | ||
} | ||
x | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.