]> git.proxmox.com Git - rustc.git/blame - vendor/libm/src/math/rintf.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / vendor / libm / src / math / rintf.rs
CommitLineData
49aad941
FG
1#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
2pub 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 {
4b012472
FG
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
49aad941 17 } else {
4b012472
FG
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
49aad941
FG
24 };
25
26 if ans == 0.0 {
27 if is_positive {
28 0.0
29 } else {
30 -0.0
31 }
32 } else {
33 ans
34 }
35 }
36}
37
38// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
39#[cfg(not(target_arch = "powerpc64"))]
40#[cfg(test)]
41mod tests {
42 use super::rintf;
43
44 #[test]
45 fn negative_zero() {
46 assert_eq!(rintf(-0.0_f32).to_bits(), (-0.0_f32).to_bits());
47 }
48
49 #[test]
50 fn sanity_check() {
51 assert_eq!(rintf(-1.0), -1.0);
52 assert_eq!(rintf(2.8), 3.0);
53 assert_eq!(rintf(-0.5), -0.0);
54 assert_eq!(rintf(0.5), 0.0);
55 assert_eq!(rintf(-1.5), -2.0);
56 assert_eq!(rintf(1.5), 2.0);
57 }
58}