/rust/registry/src/index.crates.io-6f17d22bba15001f/libm-0.2.11/src/math/rintf.rs
Line | Count | Source (jump to first uncovered line) |
1 | | #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
2 | 0 | pub fn rintf(x: f32) -> f32 { |
3 | 0 | let one_over_e = 1.0 / f32::EPSILON; |
4 | 0 | let as_u32: u32 = x.to_bits(); |
5 | 0 | let exponent: u32 = as_u32 >> 23 & 0xff; |
6 | 0 | let is_positive = (as_u32 >> 31) == 0; |
7 | 0 | if exponent >= 0x7f + 23 { |
8 | 0 | x |
9 | | } else { |
10 | 0 | let ans = if is_positive { |
11 | | #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))] |
12 | | let x = force_eval!(x); |
13 | 0 | let xplusoneovere = x + one_over_e; |
14 | 0 | #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))] |
15 | 0 | let xplusoneovere = force_eval!(xplusoneovere); |
16 | 0 | xplusoneovere - one_over_e |
17 | | } else { |
18 | | #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))] |
19 | | let x = force_eval!(x); |
20 | 0 | let xminusoneovere = x - one_over_e; |
21 | 0 | #[cfg(all(target_arch = "x86", not(target_feature = "sse2")))] |
22 | 0 | let xminusoneovere = force_eval!(xminusoneovere); |
23 | 0 | xminusoneovere + one_over_e |
24 | | }; |
25 | | |
26 | 0 | if ans == 0.0 { if is_positive { 0.0 } else { -0.0 } } else { ans } |
27 | | } |
28 | 0 | } |
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 | | } |