Skip to content

Commit

Permalink
Merge pull request #272 from Jules-Bertholet/roundeven
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed Nov 9, 2022
2 parents 593764b + d965031 commit a2420eb
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/libm-bench/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ macro_rules! bessel {

unary!(
acos, acosh, asin, atan, cbrt, ceil, cos, cosh, erf, exp, exp2, exp10, expm1, fabs, floor, j0,
j1, lgamma, log, log1p, log2, log10, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc, y0, y1
j1, lgamma, log, log1p, log2, log10, rint, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc,
y0, y1
);
binary!(atan2, copysign, fdim, fmax, fmin, fmod, hypot, pow);
trinary!(fma);
Expand Down
4 changes: 4 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ mod remainder;
mod remainderf;
mod remquo;
mod remquof;
mod rint;
mod rintf;
mod round;
mod roundf;
mod scalbn;
Expand Down Expand Up @@ -284,6 +286,8 @@ pub use self::remainder::remainder;
pub use self::remainderf::remainderf;
pub use self::remquo::remquo;
pub use self::remquof::remquof;
pub use self::rint::rint;
pub use self::rintf::rintf;
pub use self::round::round;
pub use self::roundf::roundf;
pub use self::scalbn::scalbn;
Expand Down
48 changes: 48 additions & 0 deletions src/math/rint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn rint(x: f64) -> f64 {
let one_over_e = 1.0 / f64::EPSILON;
let as_u64: u64 = x.to_bits();
let exponent: u64 = as_u64 >> 52 & 0x7ff;
let is_positive = (as_u64 >> 63) == 0;
if exponent >= 0x3ff + 52 {
x
} else {
let ans = if is_positive {
x + one_over_e - one_over_e
} else {
x - one_over_e + one_over_e
};

if ans == 0.0 {
if is_positive {
0.0
} else {
-0.0
}
} else {
ans
}
}
}

// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
#[cfg(not(target_arch = "powerpc64"))]
#[cfg(test)]
mod tests {
use super::rint;

#[test]
fn negative_zero() {
assert_eq!(rint(-0.0_f64).to_bits(), (-0.0_f64).to_bits());
}

#[test]
fn sanity_check() {
assert_eq!(rint(-1.0), -1.0);
assert_eq!(rint(2.8), 3.0);
assert_eq!(rint(-0.5), -0.0);
assert_eq!(rint(0.5), 0.0);
assert_eq!(rint(-1.5), -2.0);
assert_eq!(rint(1.5), 2.0);
}
}
48 changes: 48 additions & 0 deletions src/math/rintf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn rintf(x: f32) -> f32 {
let one_over_e = 1.0 / f32::EPSILON;
let as_u32: u32 = x.to_bits();
let exponent: u32 = as_u32 >> 23 & 0xff;
let is_positive = (as_u32 >> 31) == 0;
if exponent >= 0x7f + 23 {
x
} else {
let ans = if is_positive {
x + one_over_e - one_over_e
} else {
x - one_over_e + one_over_e
};

if ans == 0.0 {
if is_positive {
0.0
} else {
-0.0
}
} else {
ans
}
}
}

// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
#[cfg(not(target_arch = "powerpc64"))]
#[cfg(test)]
mod tests {
use super::rintf;

#[test]
fn negative_zero() {
assert_eq!(rintf(-0.0_f32).to_bits(), (-0.0_f32).to_bits());
}

#[test]
fn sanity_check() {
assert_eq!(rintf(-1.0), -1.0);
assert_eq!(rintf(2.8), 3.0);
assert_eq!(rintf(-0.5), -0.0);
assert_eq!(rintf(0.5), 0.0);
assert_eq!(rintf(-1.5), -2.0);
assert_eq!(rintf(1.5), 2.0);
}
}

0 comments on commit a2420eb

Please sign in to comment.