Merge rust-lang/libm#74
74: Implement roundf r=japaric a=P1n3appl3 closes rust-lang/libm#32 Co-authored-by: Joseph Ryan <josephryan3.14@gmail.com>
This commit is contained in:
commit
127a907c48
4 changed files with 40 additions and 5 deletions
|
|
@ -40,7 +40,6 @@ pub trait F32Ext: private::Sealed {
|
|||
#[cfg(todo)]
|
||||
fn ceil(self) -> Self;
|
||||
|
||||
#[cfg(todo)]
|
||||
fn round(self) -> Self;
|
||||
|
||||
fn trunc(self) -> Self;
|
||||
|
|
@ -154,7 +153,6 @@ impl F32Ext for f32 {
|
|||
ceilf(self)
|
||||
}
|
||||
|
||||
#[cfg(todo)]
|
||||
#[inline]
|
||||
fn round(self) -> Self {
|
||||
roundf(self)
|
||||
|
|
|
|||
|
|
@ -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 scalbn;
|
||||
mod scalbnf;
|
||||
mod sqrt;
|
||||
|
|
@ -29,6 +32,7 @@ pub use self::{
|
|||
fmodf::fmodf,
|
||||
powf::powf,
|
||||
round::round,
|
||||
roundf::roundf,
|
||||
scalbn::scalbn,
|
||||
scalbnf::scalbnf,
|
||||
sqrt::sqrt,
|
||||
|
|
|
|||
33
library/compiler-builtins/libm/src/math/roundf.rs
Normal file
33
library/compiler-builtins/libm/src/math/roundf.rs
Normal 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
|
||||
}
|
||||
}
|
||||
|
|
@ -666,7 +666,7 @@ f32_f32! {
|
|||
// log10f,
|
||||
// log2f,
|
||||
logf,
|
||||
// roundf,
|
||||
roundf,
|
||||
// sinf,
|
||||
// sinhf,
|
||||
// tanf,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue