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

Commit e339ea3

Browse files
authored
Merge pull request #272 from Jules-Bertholet/roundeven
2 parents 6de6db5 + b0ecb14 commit e339ea3

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

libm/crates/libm-bench/benches/bench.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ macro_rules! bessel {
107107

108108
unary!(
109109
acos, acosh, asin, atan, cbrt, ceil, cos, cosh, erf, exp, exp2, exp10, expm1, fabs, floor, j0,
110-
j1, lgamma, log, log1p, log2, log10, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc, y0, y1
110+
j1, lgamma, log, log1p, log2, log10, rint, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc,
111+
y0, y1
111112
);
112113
binary!(atan2, copysign, fdim, fmax, fmin, fmod, hypot, pow);
113114
trinary!(fma);

libm/src/math/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ mod remainder;
170170
mod remainderf;
171171
mod remquo;
172172
mod remquof;
173+
mod rint;
174+
mod rintf;
173175
mod round;
174176
mod roundf;
175177
mod scalbn;
@@ -284,6 +286,8 @@ pub use self::remainder::remainder;
284286
pub use self::remainderf::remainderf;
285287
pub use self::remquo::remquo;
286288
pub use self::remquof::remquof;
289+
pub use self::rint::rint;
290+
pub use self::rintf::rintf;
287291
pub use self::round::round;
288292
pub use self::roundf::roundf;
289293
pub use self::scalbn::scalbn;

libm/src/math/rint.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
2+
pub fn rint(x: f64) -> f64 {
3+
let one_over_e = 1.0 / f64::EPSILON;
4+
let as_u64: u64 = x.to_bits();
5+
let exponent: u64 = as_u64 >> 52 & 0x7ff;
6+
let is_positive = (as_u64 >> 63) == 0;
7+
if exponent >= 0x3ff + 52 {
8+
x
9+
} else {
10+
let ans = if is_positive {
11+
x + one_over_e - one_over_e
12+
} else {
13+
x - one_over_e + one_over_e
14+
};
15+
16+
if ans == 0.0 {
17+
if is_positive {
18+
0.0
19+
} else {
20+
-0.0
21+
}
22+
} else {
23+
ans
24+
}
25+
}
26+
}
27+
28+
// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
29+
#[cfg(not(target_arch = "powerpc64"))]
30+
#[cfg(test)]
31+
mod tests {
32+
use super::rint;
33+
34+
#[test]
35+
fn negative_zero() {
36+
assert_eq!(rint(-0.0_f64).to_bits(), (-0.0_f64).to_bits());
37+
}
38+
39+
#[test]
40+
fn sanity_check() {
41+
assert_eq!(rint(-1.0), -1.0);
42+
assert_eq!(rint(2.8), 3.0);
43+
assert_eq!(rint(-0.5), -0.0);
44+
assert_eq!(rint(0.5), 0.0);
45+
assert_eq!(rint(-1.5), -2.0);
46+
assert_eq!(rint(1.5), 2.0);
47+
}
48+
}

libm/src/math/rintf.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
2+
pub fn rintf(x: f32) -> f32 {
3+
let one_over_e = 1.0 / f32::EPSILON;
4+
let as_u32: u32 = x.to_bits();
5+
let exponent: u32 = as_u32 >> 23 & 0xff;
6+
let is_positive = (as_u32 >> 31) == 0;
7+
if exponent >= 0x7f + 23 {
8+
x
9+
} else {
10+
let ans = if is_positive {
11+
x + one_over_e - one_over_e
12+
} else {
13+
x - one_over_e + one_over_e
14+
};
15+
16+
if ans == 0.0 {
17+
if is_positive {
18+
0.0
19+
} else {
20+
-0.0
21+
}
22+
} else {
23+
ans
24+
}
25+
}
26+
}
27+
28+
// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
29+
#[cfg(not(target_arch = "powerpc64"))]
30+
#[cfg(test)]
31+
mod tests {
32+
use super::rintf;
33+
34+
#[test]
35+
fn negative_zero() {
36+
assert_eq!(rintf(-0.0_f32).to_bits(), (-0.0_f32).to_bits());
37+
}
38+
39+
#[test]
40+
fn sanity_check() {
41+
assert_eq!(rintf(-1.0), -1.0);
42+
assert_eq!(rintf(2.8), 3.0);
43+
assert_eq!(rintf(-0.5), -0.0);
44+
assert_eq!(rintf(0.5), 0.0);
45+
assert_eq!(rintf(-1.5), -2.0);
46+
assert_eq!(rintf(1.5), 2.0);
47+
}
48+
}

0 commit comments

Comments
 (0)