| 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 | #[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::rintf; |
| 35 | |
| 36 | #[test ] |
| 37 | fn negative_zero() { |
| 38 | assert_eq!(rintf(-0.0_f32).to_bits(), (-0.0_f32).to_bits()); |
| 39 | } |
| 40 | |
| 41 | #[test ] |
| 42 | fn sanity_check() { |
| 43 | assert_eq!(rintf(-1.0), -1.0); |
| 44 | assert_eq!(rintf(2.8), 3.0); |
| 45 | assert_eq!(rintf(-0.5), -0.0); |
| 46 | assert_eq!(rintf(0.5), 0.0); |
| 47 | assert_eq!(rintf(-1.5), -2.0); |
| 48 | assert_eq!(rintf(1.5), 2.0); |
| 49 | } |
| 50 | } |
| 51 | |