From 394fb9f2bcf0fa2e6d0ab6d0787e7859cdd9e8ac 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`. --- library/compiler-builtins/src/float/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/library/compiler-builtins/src/float/mod.rs b/library/compiler-builtins/src/float/mod.rs index 5eedf544f120..af83986447e1 100644 --- a/library/compiler-builtins/src/float/mod.rs +++ b/library/compiler-builtins/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);