Skip to content

Commit 39ac651

Browse files
committed
Implement round
1 parent 667035f commit 39ac651

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
#![deny(warnings)]
1313
#![no_std]
1414

15+
macro_rules! force_eval {
16+
($e:expr) => {
17+
unsafe { ::core::ptr::read_volatile(&$e); }
18+
}
19+
}
20+
1521
mod math;
1622

1723
#[cfg(todo)]

src/math/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ mod fabs;
22
mod fabsf;
33
mod fmodf;
44
mod powf;
5+
mod round;
56
mod scalbnf;
67
mod sqrtf;
78

89
pub use self::fabs::fabs;
910
pub use self::fabsf::fabsf;
1011
pub use self::fmodf::fmodf;
1112
pub use self::powf::powf;
13+
pub use self::round::round;
1214
pub use self::scalbnf::scalbnf;
1315
pub use self::sqrtf::sqrtf;
1416

src/math/round.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use core::f64;
2+
3+
const TOINT: f64 = 1.0 / f64::EPSILON;
4+
5+
pub fn round(mut x: f64) -> f64 {
6+
let (f, i) = (x, x.to_bits());
7+
let e: u64 = i >> 52 & 0x7ff;
8+
let mut y: f64;
9+
10+
if e >= 0x3ff + 52 {
11+
return x;
12+
}
13+
if i >> 63 != 0 {
14+
x = -x;
15+
}
16+
if e < 0x3ff - 1 {
17+
// raise inexact if x!=0
18+
force_eval!(x + TOINT);
19+
return 0.0 * f;
20+
}
21+
y = x + TOINT - TOINT - x;
22+
if y > 0.5 {
23+
y = y + x - 1.0;
24+
} else if y <= -0.5 {
25+
y = y + x + 1.0;
26+
} else {
27+
y = y + x;
28+
}
29+
30+
if i >> 63 != 0 {
31+
-y
32+
} else {
33+
y
34+
}
35+
}

test-generator/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ f64_f64! {
618618
// log10,
619619
// log1p,
620620
// log2,
621-
// round,
621+
round,
622622
// sin,
623623
// sinh,
624624
// sqrt,

0 commit comments

Comments
 (0)