61: Implement `round` r=japaric a=notriddle

Fixes rust-lang/libm#31 

Co-authored-by: Michael Howell <michael@notriddle.com>
This commit is contained in:
bors[bot] 2018-07-13 01:44:18 +00:00
commit 2d2e8810d8
4 changed files with 44 additions and 3 deletions

View file

@ -377,7 +377,6 @@ pub trait F64Ext {
#[cfg(todo)]
fn ceil(self) -> Self;
#[cfg(todo)]
fn round(self) -> Self;
#[cfg(todo)]
@ -498,7 +497,6 @@ impl F64Ext for f64 {
ceil(self)
}
#[cfg(todo)]
#[inline]
fn round(self) -> Self {
round(self)

View file

@ -1,7 +1,14 @@
macro_rules! force_eval {
($e:expr) => {
unsafe { ::core::ptr::read_volatile(&$e); }
}
}
mod fabs;
mod fabsf;
mod fmodf;
mod powf;
mod round;
mod scalbnf;
mod sqrtf;
@ -9,6 +16,7 @@ pub use self::fabs::fabs;
pub use self::fabsf::fabsf;
pub use self::fmodf::fmodf;
pub use self::powf::powf;
pub use self::round::round;
pub use self::scalbnf::scalbnf;
pub use self::sqrtf::sqrtf;

View file

@ -0,0 +1,35 @@
use core::f64;
const TOINT: f64 = 1.0 / f64::EPSILON;
pub fn round(mut x: f64) -> f64 {
let (f, i) = (x, x.to_bits());
let e: u64 = i >> 52 & 0x7ff;
let mut y: f64;
if e >= 0x3ff + 52 {
return x;
}
if i >> 63 != 0 {
x = -x;
}
if e < 0x3ff - 1 {
// raise inexact if x!=0
force_eval!(x + TOINT);
return 0.0 * f;
}
y = x + TOINT - TOINT - x;
if y > 0.5 {
y = y + x - 1.0;
} else if y <= -0.5 {
y = y + x + 1.0;
} else {
y = y + x;
}
if i >> 63 != 0 {
-y
} else {
y
}
}

View file

@ -618,7 +618,7 @@ f64_f64! {
// log10,
// log1p,
// log2,
// round,
round,
// sin,
// sinh,
// sqrt,