-
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,71 @@ | ||
//! Architecture-specific support for x86-32 without SSE2 | ||
|
||
use super::super::fabs; | ||
|
||
/// 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; | ||
/// | ||
/// Based on https://github.com/NetBSD/src/blob/trunk/lib/libm/arch/i387/s_ceil.S | ||
folkertdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// (written by J.T. Conklin <[email protected]>). | ||
pub fn ceil(mut x: f64) -> f64 { | ||
// We save and later restore the FPU control word. | ||
let mut cw_stash = core::mem::MaybeUninit::<u16>::uninit(); | ||
let mut cw_tmp = core::mem::MaybeUninit::<u16>::uninit(); | ||
unsafe { | ||
core::arch::asm!( | ||
"fstcw ({stash_ptr})", // Save the cw | ||
"movw ({stash_ptr}), %dx", // ... | ||
"orw $0x0800, %dx", // Set rounding control to 0b10 (+∞), | ||
"andw $0xfbff, %dx", // preserving other controls | ||
"movw %dx, ({cw_ptr})", // Apply cw | ||
"fldcw ({cw_ptr})", // ... | ||
Comment on lines
+19
to
+20
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. Sorry, yeah, as @quaternic mentioned this needs a second stack slot. As a microopt this could be 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. Actually even better, 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. I got this to work https://rust.godbolt.org/z/44Tjcx3bb the 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. No idea, took that from the original asm without checking :) |
||
"fldl ({x_ptr})", // Push x to the stack | ||
"frndint", // Round | ||
"fldcw ({stash_ptr})", // Restore cw | ||
"fstpl ({x_ptr})", // Save rounded x to mem | ||
cw_ptr = in(reg) &mut cw_tmp, | ||
stash_ptr = in(reg) &mut cw_stash, | ||
x_ptr = in(reg) &mut x, | ||
out("dx") _, // Cw scratch | ||
// 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(att_syntax) | ||
) | ||
} | ||
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; | ||
/// | ||
/// Based on https://github.com/NetBSD/src/blob/trunk/lib/libm/arch/i387/s_floor.S | ||
/// (written by J.T. Conklin <[email protected]>). | ||
pub fn floor(mut x: f64) -> f64 { | ||
// We save and later restore the FPU control word. | ||
let mut cw_stash = core::mem::MaybeUninit::<u16>::uninit(); | ||
let mut cw_tmp = core::mem::MaybeUninit::<u16>::uninit(); | ||
unsafe { | ||
core::arch::asm!( | ||
"fstcw ({stash_ptr})", // Save the cw | ||
"movw ({stash_ptr}), %dx", // ... | ||
"orw $0x0400, %dx", // Set rounding control to 0b01 (-∞), | ||
"andw $0xf7ff, %dx", // preserving other controls | ||
"movw %dx, ({cw_ptr})", // Apply cw | ||
"fldcw ({cw_ptr})", // ... | ||
"fldl ({x_ptr})", // Push x to the stack | ||
"frndint", // Round | ||
"fldcw ({stash_ptr})", // Restore cw | ||
"fstpl ({x_ptr})", // Save rounded x to mem | ||
cw_ptr = in(reg) &mut cw_tmp, | ||
stash_ptr = in(reg) &mut cw_stash, | ||
x_ptr = in(reg) &mut x, | ||
out("dx") _, // Cw scratch | ||
// 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(att_syntax) | ||
) | ||
} | ||
x | ||
} |
Uh oh!
There was an error while loading. Please reload this page.