const_soft_float/soft_f32/
round.rs

1use super::SoftF32;
2
3pub(crate) const fn round(x: SoftF32) -> SoftF32 {
4    SoftF32::trunc(x.add(SoftF32::copysign(
5        SoftF32(0.5).sub(SoftF32(0.25).mul(SoftF32(f32::EPSILON))),
6        x,
7    )))
8}
9
10#[cfg(test)]
11mod tests {
12    use super::SoftF32;
13
14    #[test]
15    fn negative_zero() {
16        assert_eq!(
17            SoftF32::round(SoftF32(-0.0)).to_bits(),
18            SoftF32(-0.0).to_bits()
19        );
20    }
21
22    #[test]
23    fn sanity_check() {
24        assert_eq!((SoftF32(-1.0)).round().0, -1.0);
25        assert_eq!((SoftF32(2.8)).round().0, 3.0);
26        assert_eq!((SoftF32(-0.5)).round().0, -1.0);
27        assert_eq!((SoftF32(0.5)).round().0, 1.0);
28        assert_eq!((SoftF32(-1.5)).round().0, -2.0);
29        assert_eq!((SoftF32(1.5)).round().0, 2.0);
30    }
31}