Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

implement trunc and truncf #65

Merged
merged 1 commit into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub trait F32Ext {
#[cfg(todo)]
fn round(self) -> Self;

#[cfg(todo)]
fn trunc(self) -> Self;

#[cfg(todo)]
Expand Down Expand Up @@ -163,7 +162,6 @@ impl F32Ext for f32 {
roundf(self)
}

#[cfg(todo)]
#[inline]
fn trunc(self) -> Self {
truncf(self)
Expand Down Expand Up @@ -372,7 +370,6 @@ pub trait F64Ext {

fn round(self) -> Self;

#[cfg(todo)]
fn trunc(self) -> Self;

#[cfg(todo)]
Expand Down Expand Up @@ -494,7 +491,6 @@ impl F64Ext for f64 {
round(self)
}

#[cfg(todo)]
#[inline]
fn trunc(self) -> Self {
trunc(self)
Expand Down
4 changes: 4 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod sqrtf;
mod logf;
mod expf;
mod floor;
mod trunc;
mod truncf;

//mod service;

Expand All @@ -30,6 +32,8 @@ pub use self::{
logf::logf,
expf::expf,
floor::floor,
trunc::trunc,
truncf::truncf,
};

fn isnanf(x: f32) -> bool {
Expand Down
24 changes: 24 additions & 0 deletions src/math/trunc.rs
Original file line number Diff line number Diff line change
@@ -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)
}
24 changes: 24 additions & 0 deletions src/math/truncf.rs
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions test-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ fn main() -> Result<(), Box<Error>> {
f32_f32! {
// acosf,
// floorf,
// truncf
truncf,
// asinf,
// atanf,
// cbrtf,
Expand Down Expand Up @@ -625,7 +625,7 @@ f64_f64! {
// sqrt,
// tan,
// tanh,
// trunc,
trunc,
fabs,
}

Expand Down