rint/rintf instead of roundeven/roundevenf

This commit is contained in:
Jules Bertholet 2022-11-07 12:53:26 -05:00
parent e3efddc2c2
commit 292221cf4c
4 changed files with 22 additions and 33 deletions

View file

@ -39,9 +39,6 @@ mod musl_reference_tests {
"jnf.rs",
"j1.rs",
"j1f.rs",
// musl doens't have these
"roundeven.rs",
"roundevenf.rs",
];
struct Function {

View file

@ -170,9 +170,9 @@ mod remainder;
mod remainderf;
mod remquo;
mod remquof;
mod rint;
mod rintf;
mod round;
mod roundeven;
mod roundevenf;
mod roundf;
mod scalbn;
mod scalbnf;
@ -286,9 +286,9 @@ 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::roundeven::roundeven;
pub use self::roundevenf::roundevenf;
pub use self::roundf::roundf;
pub use self::scalbn::scalbn;
pub use self::scalbnf::scalbnf;

View file

@ -1,9 +1,5 @@
// Source: musl libm rint
// (equivalent to roundeven when rounding mode is default,
// which Rust assumes)
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn roundeven(x: f64) -> f64 {
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;
@ -31,20 +27,20 @@ pub fn roundeven(x: f64) -> f64 {
#[cfg(test)]
mod tests {
use super::roundeven;
use super::rint;
#[test]
fn negative_zero() {
assert_eq!(roundeven(-0.0_f64).to_bits(), (-0.0_f64).to_bits());
assert_eq!(rint(-0.0_f64).to_bits(), (-0.0_f64).to_bits());
}
#[test]
fn sanity_check() {
assert_eq!(roundeven(-1.0), -1.0);
assert_eq!(roundeven(2.8), 3.0);
assert_eq!(roundeven(-0.5), -0.0);
assert_eq!(roundeven(0.5), 0.0);
assert_eq!(roundeven(-1.5), -2.0);
assert_eq!(roundeven(1.5), 2.0);
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);
}
}

View file

@ -1,9 +1,5 @@
// Source: musl libm rintf
// (equivalent to roundevenf when rounding mode is default,
// which Rust assumes)
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn roundevenf(x: f32) -> f32 {
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;
@ -31,20 +27,20 @@ pub fn roundevenf(x: f32) -> f32 {
#[cfg(test)]
mod tests {
use super::roundevenf;
use super::rintf;
#[test]
fn negative_zero() {
assert_eq!(roundevenf(-0.0_f32).to_bits(), (-0.0_f32).to_bits());
assert_eq!(rintf(-0.0_f32).to_bits(), (-0.0_f32).to_bits());
}
#[test]
fn sanity_check() {
assert_eq!(roundevenf(-1.0), -1.0);
assert_eq!(roundevenf(2.8), 3.0);
assert_eq!(roundevenf(-0.5), -0.0);
assert_eq!(roundevenf(0.5), 0.0);
assert_eq!(roundevenf(-1.5), -2.0);
assert_eq!(roundevenf(1.5), 2.0);
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);
}
}