Skip to content

Commit 0a3a5e2

Browse files
65: implement trunc and truncf r=japaric a=erikdesjardins closes rust-lang#58, closes rust-lang#59 Co-authored-by: Erik <[email protected]>
2 parents 46d8a41 + 471b163 commit 0a3a5e2

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub trait F32Ext {
4444
#[cfg(todo)]
4545
fn round(self) -> Self;
4646

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

5049
#[cfg(todo)]
@@ -163,7 +162,6 @@ impl F32Ext for f32 {
163162
roundf(self)
164163
}
165164

166-
#[cfg(todo)]
167165
#[inline]
168166
fn trunc(self) -> Self {
169167
truncf(self)
@@ -372,7 +370,6 @@ pub trait F64Ext {
372370

373371
fn round(self) -> Self;
374372

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

378375
#[cfg(todo)]
@@ -494,7 +491,6 @@ impl F64Ext for f64 {
494491
round(self)
495492
}
496493

497-
#[cfg(todo)]
498494
#[inline]
499495
fn trunc(self) -> Self {
500496
trunc(self)

src/math/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ mod sqrtf;
1515
mod logf;
1616
mod expf;
1717
mod floor;
18+
mod trunc;
19+
mod truncf;
1820

1921
//mod service;
2022

@@ -30,6 +32,8 @@ pub use self::{
3032
logf::logf,
3133
expf::expf,
3234
floor::floor,
35+
trunc::trunc,
36+
truncf::truncf,
3337
};
3438

3539
fn isnanf(x: f32) -> bool {

src/math/trunc.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use core::f64;
2+
3+
#[inline]
4+
pub fn trunc(x: f64) -> f64 {
5+
let x1p120 = f64::from_bits(0x4770000000000000); // 0x1p120f === 2 ^ 120
6+
7+
let mut i: u64 = x.to_bits();
8+
let mut e: i64 = (i >> 52 & 0x7ff) as i64 - 0x3ff + 12;
9+
let m: u64;
10+
11+
if e >= 52 + 12 {
12+
return x;
13+
}
14+
if e < 12 {
15+
e = 1;
16+
}
17+
m = -1i64 as u64 >> e;
18+
if (i & m) == 0 {
19+
return x;
20+
}
21+
force_eval!(x + x1p120);
22+
i &= !m;
23+
f64::from_bits(i)
24+
}

src/math/truncf.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use core::f32;
2+
3+
#[inline]
4+
pub fn truncf(x: f32) -> f32 {
5+
let x1p120 = f32::from_bits(0x7b800000); // 0x1p120f === 2 ^ 120
6+
7+
let mut i: u32 = x.to_bits();
8+
let mut e: i32 = (i >> 23 & 0xff) as i32 - 0x7f + 9;
9+
let m: u32;
10+
11+
if e >= 23 + 9 {
12+
return x;
13+
}
14+
if e < 9 {
15+
e = 1;
16+
}
17+
m = -1i32 as u32 >> e;
18+
if (i & m) == 0 {
19+
return x;
20+
}
21+
force_eval!(x + x1p120);
22+
i &= !m;
23+
f32::from_bits(i)
24+
}

test-generator/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ fn main() -> Result<(), Box<Error>> {
562562
f32_f32! {
563563
// acosf,
564564
// floorf,
565-
// truncf
565+
truncf,
566566
// asinf,
567567
// atanf,
568568
// cbrtf,
@@ -625,7 +625,7 @@ f64_f64! {
625625
// sqrt,
626626
// tan,
627627
// tanh,
628-
// trunc,
628+
trunc,
629629
fabs,
630630
}
631631

0 commit comments

Comments
 (0)