Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from
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
22 changes: 0 additions & 22 deletions libm-test/src/precision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,6 @@ impl MaybeOverride<(f32,)> for SpecialCase {

impl MaybeOverride<(f64,)> for SpecialCase {
fn check_float<F: Float>(input: (f64,), actual: F, expected: F, ctx: &CheckCtx) -> CheckAction {
if cfg!(x86_no_sse)
&& ctx.base_name == BaseName::Ceil
&& ctx.basis == CheckBasis::Musl
&& input.0 < 0.0
&& input.0 > -1.0
&& expected == F::ZERO
&& actual == F::ZERO
{
// musl returns -0.0, we return +0.0
return XFAIL("i586 ceil signed zero");
}

if cfg!(x86_no_sse)
&& (ctx.base_name == BaseName::Rint || ctx.base_name == BaseName::Roundeven)
&& (expected - actual).abs() <= F::ONE
Expand All @@ -292,16 +280,6 @@ impl MaybeOverride<(f64,)> for SpecialCase {
return XFAIL("i586 rint rounding mode");
}

if cfg!(x86_no_sse)
&& (ctx.fn_ident == Identifier::Ceil || ctx.fn_ident == Identifier::Floor)
&& expected.eq_repr(F::NEG_ZERO)
&& actual.eq_repr(F::ZERO)
{
// FIXME: the x87 implementations do not keep the distinction between -0.0 and 0.0.
// See https://github.com/rust-lang/libm/pull/404#issuecomment-2572399955
return XFAIL("i586 ceil/floor signed zero");
}

if cfg!(x86_no_sse)
&& (ctx.fn_ident == Identifier::Exp10 || ctx.fn_ident == Identifier::Exp2)
{
Expand Down
82 changes: 58 additions & 24 deletions libm/src/math/arch/i586.rs
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
/// (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
Copy link
Contributor

@tgross35 tgross35 Jul 17, 2025

Choose a reason for hiding this comment

The 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 let mut cw_stash = [0u16; 2]; and then these accesses can be -4({stash_ptr}) so we save a register vs. two different u16 locals

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually even better, let mut cw_stash = MaybeUninit::<[u16; 2]>::uninit(); to save the zero init instruction

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this to work

https://rust.godbolt.org/z/44Tjcx3bb

the -4 stuff didn't work (also, for u16, why -4?)

Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
Loading