77: adding ceilf and floorf  r=japaric a=jackmott

Finishes issues rust-lang/libm#56  and rust-lang/libm#54 

Co-authored-by: Jack Mott <jack.mott@gmail.com>
This commit is contained in:
bors[bot] 2018-07-14 02:35:57 +00:00
commit 56e1b0d52b
5 changed files with 65 additions and 6 deletions

View file

@ -34,10 +34,8 @@ pub fn _eq(a: u64, b: u64) -> bool {
///
/// This trait is sealed and cannot be implemented outside of `libm`.
pub trait F32Ext: private::Sealed {
#[cfg(todo)]
fn floor(self) -> Self;
#[cfg(todo)]
fn ceil(self) -> Self;
fn round(self) -> Self;
@ -141,13 +139,11 @@ pub trait F32Ext: private::Sealed {
}
impl F32Ext for f32 {
#[cfg(todo)]
#[inline]
fn floor(self) -> Self {
floorf(self)
}
#[cfg(todo)]
#[inline]
fn ceil(self) -> Self {
ceilf(self)

View file

@ -0,0 +1,29 @@
use core::f32;
pub fn ceilf(x: f32) -> f32 {
let mut ui = x.to_bits();
let e = (((ui >> 23) & 0xff) - 0x7f) as i32;
if e >= 23 {
return x;
}
if e >= 0 {
let m = 0x007fffff >> e;
if (ui & m) == 0 {
return x;
}
force_eval!(x + f32::from_bits(0x7b800000));
if ui >> 31 == 0 {
ui += m;
}
ui &= !m;
} else {
force_eval!(x + f32::from_bits(0x7b800000));
if ui >> 31 != 0 {
return -0.0;
} else if ui << 1 != 0 {
return 1.0;
}
}
return f32::from_bits(ui);
}

View file

@ -0,0 +1,30 @@
use core::f32;
#[inline]
pub fn floorf(x: f32) -> f32 {
let mut ui = x.to_bits();
let e = (((ui >> 23) & 0xff) - 0x7f) as i32;
if e >= 23 {
return x;
}
if e >= 0 {
let m: u32 = 0x007fffff >> e;
if (ui & m) == 0 {
return x;
}
force_eval!(x + f32::from_bits(0x7b800000));
if ui >> 31 != 0 {
ui += m;
}
ui &= !m;
} else {
force_eval!(x + f32::from_bits(0x7b800000));
if ui >> 31 == 0 {
ui = 0;
} else if ui << 1 != 0 {
return -1.0;
}
}
return f32::from_bits(ui);
}

View file

@ -6,8 +6,10 @@ macro_rules! force_eval {
};
}
mod ceilf;
mod fabs;
mod fabsf;
mod floorf;
mod fmodf;
mod powf;
mod round;
@ -27,8 +29,10 @@ mod hypotf;
//mod service;
pub use self::{
ceilf::ceilf,
fabs::fabs,
fabsf::fabsf,
floorf::floorf,
fmodf::fmodf,
powf::powf,
round::round,

View file

@ -652,12 +652,12 @@ fn main() -> Result<(), Box<Error>> {
// With signature `fn(f32) -> f32`
f32_f32! {
// acosf,
// floorf,
floorf,
truncf,
// asinf,
// atanf,
// cbrtf,
// ceilf,
ceilf,
// cosf,
// coshf,
// exp2f,