65: implement trunc and truncf r=japaric a=erikdesjardins

closes rust-lang/libm#58, closes rust-lang/libm#59

Co-authored-by: Erik <erikdesjardins@users.noreply.github.com>
This commit is contained in:
bors[bot] 2018-07-13 20:21:17 +00:00
commit 4816a1501a
5 changed files with 54 additions and 6 deletions

View file

@ -44,7 +44,6 @@ pub trait F32Ext {
#[cfg(todo)]
fn round(self) -> Self;
#[cfg(todo)]
fn trunc(self) -> Self;
#[cfg(todo)]
@ -163,7 +162,6 @@ impl F32Ext for f32 {
roundf(self)
}
#[cfg(todo)]
#[inline]
fn trunc(self) -> Self {
truncf(self)
@ -372,7 +370,6 @@ pub trait F64Ext {
fn round(self) -> Self;
#[cfg(todo)]
fn trunc(self) -> Self;
#[cfg(todo)]
@ -494,7 +491,6 @@ impl F64Ext for f64 {
round(self)
}
#[cfg(todo)]
#[inline]
fn trunc(self) -> Self {
trunc(self)

View file

@ -15,6 +15,8 @@ mod sqrtf;
mod logf;
mod expf;
mod floor;
mod trunc;
mod truncf;
//mod service;
@ -30,6 +32,8 @@ pub use self::{
logf::logf,
expf::expf,
floor::floor,
trunc::trunc,
truncf::truncf,
};
fn isnanf(x: f32) -> bool {

View file

@ -0,0 +1,24 @@
use core::f64;
#[inline]
pub fn trunc(x: f64) -> f64 {
let x1p120 = f64::from_bits(0x4770000000000000); // 0x1p120f === 2 ^ 120
let mut i: u64 = x.to_bits();
let mut e: i64 = (i >> 52 & 0x7ff) as i64 - 0x3ff + 12;
let m: u64;
if e >= 52 + 12 {
return x;
}
if e < 12 {
e = 1;
}
m = -1i64 as u64 >> e;
if (i & m) == 0 {
return x;
}
force_eval!(x + x1p120);
i &= !m;
f64::from_bits(i)
}

View file

@ -0,0 +1,24 @@
use core::f32;
#[inline]
pub fn truncf(x: f32) -> f32 {
let x1p120 = f32::from_bits(0x7b800000); // 0x1p120f === 2 ^ 120
let mut i: u32 = x.to_bits();
let mut e: i32 = (i >> 23 & 0xff) as i32 - 0x7f + 9;
let m: u32;
if e >= 23 + 9 {
return x;
}
if e < 9 {
e = 1;
}
m = -1i32 as u32 >> e;
if (i & m) == 0 {
return x;
}
force_eval!(x + x1p120);
i &= !m;
f32::from_bits(i)
}

View file

@ -562,7 +562,7 @@ fn main() -> Result<(), Box<Error>> {
f32_f32! {
// acosf,
// floorf,
// truncf
truncf,
// asinf,
// atanf,
// cbrtf,
@ -625,7 +625,7 @@ f64_f64! {
// sqrt,
// tan,
// tanh,
// trunc,
trunc,
fabs,
}