|
1 | 1 | //! Architecture-specific support for x86-32 without SSE2 |
| 2 | +//! |
| 3 | +//! We use an alternative implementation on x86, because the |
| 4 | +//! main implementation fails with the x87 FPU used by |
| 5 | +//! debian i386, probably due to excess precision issues. |
| 6 | +//! |
| 7 | +//! See https://github.com/rust-lang/compiler-builtins/pull/976 for discussion on why these |
| 8 | +//! functions are implemented in this way. |
2 | 9 |
|
3 | | -use super::super::fabs; |
| 10 | +// FIXME: when the MSRV allows, use naked functions instead. |
4 | 11 |
|
5 | | -/// Use an alternative implementation on x86, because the |
6 | | -/// main implementation fails with the x87 FPU used by |
7 | | -/// debian i386, probably due to excess precision issues. |
8 | | -/// Basic implementation taken from https://github.com/rust-lang/libm/issues/219. |
9 | | -pub fn ceil(x: f64) -> f64 { |
10 | | - if fabs(x).to_bits() < 4503599627370496.0_f64.to_bits() { |
11 | | - let truncated = x as i64 as f64; |
12 | | - if truncated < x { |
13 | | - return truncated + 1.0; |
14 | | - } else { |
15 | | - return truncated; |
16 | | - } |
17 | | - } else { |
18 | | - return x; |
| 12 | +pub extern "C" fn ceil(mut x: f64) -> f64 { |
| 13 | + unsafe { |
| 14 | + core::arch::asm!( |
| 15 | + "fld qword ptr [{x}]", |
| 16 | + // Save the FPU control word, using `x` as scratch space. |
| 17 | + "fstcw [{x}]", |
| 18 | + // Set rounding control to 0b10 (+∞). |
| 19 | + "mov word ptr [{x} + 2], 0x0b7f", |
| 20 | + "fldcw [{x} + 2]", |
| 21 | + // Round. |
| 22 | + "frndint", |
| 23 | + // Restore FPU control word. |
| 24 | + "fldcw [{x}]", |
| 25 | + // Save rounded value to memory. |
| 26 | + "fstp qword ptr [{x}]", |
| 27 | + x = in(reg) &mut x, |
| 28 | + // All the x87 FPU stack is used, all registers must be clobbered |
| 29 | + out("st(0)") _, out("st(1)") _, |
| 30 | + out("st(2)") _, out("st(3)") _, |
| 31 | + out("st(4)") _, out("st(5)") _, |
| 32 | + out("st(6)") _, out("st(7)") _, |
| 33 | + options(nostack), |
| 34 | + ); |
19 | 35 | } |
| 36 | + x |
20 | 37 | } |
21 | 38 |
|
22 | | -/// Use an alternative implementation on x86, because the |
23 | | -/// main implementation fails with the x87 FPU used by |
24 | | -/// debian i386, probably due to excess precision issues. |
25 | | -/// Basic implementation taken from https://github.com/rust-lang/libm/issues/219. |
26 | | -pub fn floor(x: f64) -> f64 { |
27 | | - if fabs(x).to_bits() < 4503599627370496.0_f64.to_bits() { |
28 | | - let truncated = x as i64 as f64; |
29 | | - if truncated > x { |
30 | | - return truncated - 1.0; |
31 | | - } else { |
32 | | - return truncated; |
33 | | - } |
34 | | - } else { |
35 | | - return x; |
| 39 | +pub extern "C" fn floor(mut x: f64) -> f64 { |
| 40 | + unsafe { |
| 41 | + core::arch::asm!( |
| 42 | + "fld qword ptr [{x}]", |
| 43 | + // Save the FPU control word, using `x` as scratch space. |
| 44 | + "fstcw [{x}]", |
| 45 | + // Set rounding control to 0b01 (-∞). |
| 46 | + "mov word ptr [{x} + 2], 0x077f", |
| 47 | + "fldcw [{x} + 2]", |
| 48 | + // Round. |
| 49 | + "frndint", |
| 50 | + // Restore FPU control word. |
| 51 | + "fldcw [{x}]", |
| 52 | + // Save rounded value to memory. |
| 53 | + "fstp qword ptr [{x}]", |
| 54 | + x = in(reg) &mut x, |
| 55 | + // All the x87 FPU stack is used, all registers must be clobbered |
| 56 | + out("st(0)") _, out("st(1)") _, |
| 57 | + out("st(2)") _, out("st(3)") _, |
| 58 | + out("st(4)") _, out("st(5)") _, |
| 59 | + out("st(6)") _, out("st(7)") _, |
| 60 | + options(nostack), |
| 61 | + ); |
36 | 62 | } |
| 63 | + x |
37 | 64 | } |
0 commit comments