From e218830a3483b9808402e601e2e01e7c9017babd Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 13 May 2021 18:20:34 -0700 Subject: [PATCH] use round_to_nearest_ties_to_even to implement round --- src/f16.rs | 6 ++---- src/prim.rs | 9 +++++++++ src/scalar.rs | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/f16.rs b/src/f16.rs index 4609c58..1ed978d 100644 --- a/src/f16.rs +++ b/src/f16.rs @@ -226,11 +226,9 @@ impl F16 { #[cfg(not(feature = "std"))] todo!(); } + /// round to nearest, ties to unspecified pub fn round(self) -> Self { - #[cfg(feature = "std")] - return f32::from(self).round().to(); - #[cfg(not(feature = "std"))] - todo!(); + return PrimFloat::round(f32::from(self)).to(); } #[cfg(feature = "fma")] pub fn fma(self, a: Self, b: Self) -> Self { diff --git a/src/prim.rs b/src/prim.rs index 41eca19..93c7940 100644 --- a/src/prim.rs +++ b/src/prim.rs @@ -160,6 +160,8 @@ pub trait PrimFloat: } fn is_finite(self) -> bool; fn trunc(self) -> Self; + /// round to nearest, ties to unspecified + fn round(self) -> Self; fn copy_sign(self, sign: Self) -> Self; } @@ -222,6 +224,13 @@ macro_rules! impl_float { #[cfg(not(feature = "std"))] return crate::algorithms::base::trunc(Scalar, Value(self)).0; } + fn round(self) -> Self { + #[cfg(feature = "std")] + return $float::round(self); + #[cfg(not(feature = "std"))] + return crate::algorithms::base::round_to_nearest_ties_to_even(Scalar, Value(self)) + .0; + } fn copy_sign(self, sign: Self) -> Self { #[cfg(feature = "std")] return $float::copysign(self); diff --git a/src/scalar.rs b/src/scalar.rs index c1a1ec9..3011df7 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -380,7 +380,7 @@ macro_rules! impl_float { #[cfg(feature = "std")] return Value(self.0.round()); #[cfg(not(feature = "std"))] - todo!(); + return crate::algorithms::base::round_to_nearest_ties_to_even(Scalar, self); } #[cfg(feature = "fma")] fn fma(self, a: Self, b: Self) -> Self { -- 2.30.2