From 64f131a58e2b59de202f9008f79c584e9b3adad4 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Fri, 25 Oct 2024 14:30:03 -0500 Subject: [PATCH] Add an `abs` function to the `Float` trait There is no in-crate use for this yet, but we will make use of it in `libm`. --- src/float/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/float/mod.rs b/src/float/mod.rs index 5eedf544..af839864 100644 --- a/src/float/mod.rs +++ b/src/float/mod.rs @@ -98,6 +98,11 @@ pub(crate) trait Float: /// Constructs a `Self` from its parts. Inputs are treated as bits and shifted into position. fn from_parts(negative: bool, exponent: Self::Int, significand: Self::Int) -> Self; + fn abs(self) -> Self { + let abs_mask = !Self::SIGN_MASK ; + Self::from_bits(self.to_bits() & abs_mask) + } + /// Returns (normalized exponent, normalized significand) fn normalize(significand: Self::Int) -> (i32, Self::Int);