libm/math/
roundf.rs

1use core::f32;
2
3use super::{copysignf, truncf};
4
5#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
6pub fn roundf(x: f32) -> f32 {
7    truncf(x + copysignf(0.5 - 0.25 * f32::EPSILON, x))
8}
9
10// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
11#[cfg(not(target_arch = "powerpc64"))]
12#[cfg(test)]
13mod tests {
14    use super::roundf;
15
16    #[test]
17    fn negative_zero() {
18        assert_eq!(roundf(-0.0_f32).to_bits(), (-0.0_f32).to_bits());
19    }
20
21    #[test]
22    fn sanity_check() {
23        assert_eq!(roundf(-1.0), -1.0);
24        assert_eq!(roundf(2.8), 3.0);
25        assert_eq!(roundf(-0.5), -1.0);
26        assert_eq!(roundf(0.5), 1.0);
27        assert_eq!(roundf(-1.5), -2.0);
28        assert_eq!(roundf(1.5), 2.0);
29    }
30}