• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
rint(x: f64) -> f642 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             #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
12             let x = force_eval!(x);
13             let xplusoneovere = x + one_over_e;
14             #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
15             let xplusoneovere = force_eval!(xplusoneovere);
16             xplusoneovere - one_over_e
17         } else {
18             #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
19             let x = force_eval!(x);
20             let xminusoneovere = x - one_over_e;
21             #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))]
22             let xminusoneovere = force_eval!(xminusoneovere);
23             xminusoneovere + one_over_e
24         };
25 
26         if ans == 0.0 { if is_positive { 0.0 } else { -0.0 } } else { ans }
27     }
28 }
29 
30 // PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
31 #[cfg(not(target_arch = "powerpc64"))]
32 #[cfg(test)]
33 mod tests {
34     use super::rint;
35 
36     #[test]
negative_zero()37     fn negative_zero() {
38         assert_eq!(rint(-0.0_f64).to_bits(), (-0.0_f64).to_bits());
39     }
40 
41     #[test]
sanity_check()42     fn sanity_check() {
43         assert_eq!(rint(-1.0), -1.0);
44         assert_eq!(rint(2.8), 3.0);
45         assert_eq!(rint(-0.5), -0.0);
46         assert_eq!(rint(0.5), 0.0);
47         assert_eq!(rint(-1.5), -2.0);
48         assert_eq!(rint(1.5), 2.0);
49     }
50 }
51