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