diff --git a/library/compiler-builtins/libm/src/math/round.rs b/library/compiler-builtins/libm/src/math/round.rs index 9a9723cfbb65..efbe68ae4b99 100644 --- a/library/compiler-builtins/libm/src/math/round.rs +++ b/library/compiler-builtins/libm/src/math/round.rs @@ -5,20 +5,20 @@ const TOINT: f64 = 1.0 / f64::EPSILON; #[inline] #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] pub fn round(mut x: f64) -> f64 { - let (f, i) = (x, x.to_bits()); + let i = x.to_bits(); let e: u64 = i >> 52 & 0x7ff; let mut y: f64; if e >= 0x3ff + 52 { return x; } - if i >> 63 != 0 { - x = -x; - } if e < 0x3ff - 1 { // raise inexact if x!=0 force_eval!(x + TOINT); - return 0.0 * f; + return 0.0 * x; + } + if i >> 63 != 0 { + x = -x; } y = x + TOINT - TOINT - x; if y > 0.5 { @@ -35,3 +35,8 @@ pub fn round(mut x: f64) -> f64 { y } } + +#[test] +fn negative_zero() { + assert_eq!(round(-0.0_f64).to_bits(), (-0.0_f64).to_bits()); +} diff --git a/library/compiler-builtins/libm/src/math/roundf.rs b/library/compiler-builtins/libm/src/math/roundf.rs index 839d9469a49e..559d3a926809 100644 --- a/library/compiler-builtins/libm/src/math/roundf.rs +++ b/library/compiler-builtins/libm/src/math/roundf.rs @@ -12,13 +12,13 @@ pub fn roundf(mut x: f32) -> f32 { if e >= 0x7f + 23 { return x; } - if i >> 31 != 0 { - x = -x; - } if e < 0x7f - 1 { force_eval!(x + TOINT); return 0.0 * x; } + if i >> 31 != 0 { + x = -x; + } y = x + TOINT - TOINT - x; if y > 0.5f32 { y = y + x - 1.0; @@ -33,3 +33,8 @@ pub fn roundf(mut x: f32) -> f32 { y } } + +#[test] +fn negative_zero() { + assert_eq!(roundf(-0.0_f32).to_bits(), (-0.0_f32).to_bits()); +}