Skip to content

Commit 7379ef8

Browse files
bors[bot]P1n3appl3
andcommitted
74: Implement roundf r=japaric a=P1n3appl3 closes rust-lang#32 Co-authored-by: Joseph Ryan <[email protected]>
2 parents a666d85 + 87eac33 commit 7379ef8

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub trait F32Ext: private::Sealed {
4040
#[cfg(todo)]
4141
fn ceil(self) -> Self;
4242

43-
#[cfg(todo)]
4443
fn round(self) -> Self;
4544

4645
fn trunc(self) -> Self;
@@ -154,7 +153,6 @@ impl F32Ext for f32 {
154153
ceilf(self)
155154
}
156155

157-
#[cfg(todo)]
158156
#[inline]
159157
fn round(self) -> Self {
160158
roundf(self)

src/math/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
macro_rules! force_eval {
22
($e:expr) => {
3-
unsafe { ::core::ptr::read_volatile(&$e); }
4-
}
3+
unsafe {
4+
::core::ptr::read_volatile(&$e);
5+
}
6+
};
57
}
68

79
mod fabs;
810
mod fabsf;
911
mod fmodf;
1012
mod powf;
1113
mod round;
14+
mod roundf;
1215
mod scalbn;
1316
mod scalbnf;
1417
mod sqrt;
@@ -29,6 +32,7 @@ pub use self::{
2932
fmodf::fmodf,
3033
powf::powf,
3134
round::round,
35+
roundf::roundf,
3236
scalbn::scalbn,
3337
scalbnf::scalbnf,
3438
sqrt::sqrt,

src/math/roundf.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use core::f32;
2+
3+
const TOINT: f32 = 1.0 / f32::EPSILON;
4+
5+
pub fn roundf(mut x: f32) -> f32 {
6+
let i = x.to_bits();
7+
let e: u32 = i >> 23 & 0xff;
8+
let mut y: f32;
9+
10+
if e >= 0x7f + 23 {
11+
return x;
12+
}
13+
if i >> 31 != 0 {
14+
x = -x;
15+
}
16+
if e < 0x7f - 1 {
17+
force_eval!(x + TOINT);
18+
return 0.0 * x;
19+
}
20+
y = x + TOINT - TOINT - x;
21+
if y > 0.5f32 {
22+
y = y + x - 1.0;
23+
} else if y <= -0.5f32 {
24+
y = y + x + 1.0;
25+
} else {
26+
y = y + x;
27+
}
28+
if i >> 31 != 0 {
29+
-y
30+
} else {
31+
y
32+
}
33+
}

test-generator/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ f32_f32! {
666666
// log10f,
667667
// log2f,
668668
logf,
669-
// roundf,
669+
roundf,
670670
// sinf,
671671
// sinhf,
672672
// tanf,

0 commit comments

Comments
 (0)