implement roundf

This commit is contained in:
Joseph Ryan
2018-07-13 20:46:09 -05:00
parent 2d2e8810d8
commit bc94c70687
4 changed files with 40 additions and 5 deletions

View File

@@ -41,7 +41,6 @@ pub trait F32Ext {
#[cfg(todo)]
fn ceil(self) -> Self;
#[cfg(todo)]
fn round(self) -> Self;
#[cfg(todo)]
@@ -160,7 +159,6 @@ impl F32Ext for f32 {
ceilf(self)
}
#[cfg(todo)]
#[inline]
fn round(self) -> Self {
roundf(self)

View File

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

View File

@@ -0,0 +1,33 @@
use core::f32;
const TOINT: f32 = 1.0 / f32::EPSILON;
pub fn roundf(mut x: f32) -> f32 {
let i = x.to_bits();
let e: u32 = i >> 23 & 0xff;
let mut y: f32;
if e >= 0x7f + 23 {
return x;
}
if i >> 31 != 0 {
x = -x;
}
if e < 0x7f - 1 {
force_eval!(x + TOINT);
return 0.0 * x;
}
y = x + TOINT - TOINT - x;
if y > 0.5f32 {
y = y + x - 1.0;
} else if y <= -0.5f32 {
y = y + x + 1.0;
} else {
y = y + x;
}
if i >> 31 != 0 {
-y
} else {
y
}
}

View File

@@ -574,7 +574,7 @@ f32_f32! {
// fdimf,
// log10f,
// log2f,
// roundf,
roundf,
// sinf,
// sinhf,
// tanf,