[libc-commits] [libc] 6656029 - [libc][nfc] update get_explicit_mantissa

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Jul 7 10:13:32 PDT 2022


Author: Michael Jones
Date: 2022-07-07T10:13:24-07:00
New Revision: 6656029a4914e4d8ae5ec948e9d4ba173e9f3e86

URL: https://github.com/llvm/llvm-project/commit/6656029a4914e4d8ae5ec948e9d4ba173e9f3e86
DIFF: https://github.com/llvm/llvm-project/commit/6656029a4914e4d8ae5ec948e9d4ba173e9f3e86.diff

LOG: [libc][nfc] update get_explicit_mantissa

The get_explicit_mantissa function returns the mantissa of an FPBits
floating point value with the implicit leading 1, if appropriate. This
function existed previously, but did not handle non-normal numbers
properly.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D129241

Added: 
    

Modified: 
    libc/src/__support/FPUtil/FPBits.h
    libc/src/__support/FPUtil/x86_64/LongDoubleBits.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index f972b97de1b0f..62befc1e53eb0 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -59,11 +59,6 @@ template <typename T> struct FPBits {
 
   UIntType get_mantissa() const { return bits & FloatProp::MANTISSA_MASK; }
 
-  // The function return mantissa with implicit bit set for normal values.
-  constexpr UIntType get_explicit_mantissa() {
-    return (FloatProp::MANTISSA_MASK + 1) | (FloatProp::MANTISSA_MASK & bits);
-  }
-
   void set_unbiased_exponent(UIntType expVal) {
     expVal = (expVal << (FloatProp::MANTISSA_WIDTH)) & FloatProp::EXPONENT_MASK;
     bits &= ~(FloatProp::EXPONENT_MASK);
@@ -75,6 +70,15 @@ template <typename T> struct FPBits {
                     (FloatProp::MANTISSA_WIDTH));
   }
 
+  // The function return mantissa with the implicit bit set iff the current
+  // value is a valid normal number.
+  constexpr UIntType get_explicit_mantissa() {
+    return ((get_unbiased_exponent() > 0 && !is_inf_or_nan())
+                ? (FloatProp::MANTISSA_MASK + 1)
+                : 0) |
+           (FloatProp::MANTISSA_MASK & bits);
+  }
+
   void set_sign(bool signVal) {
     bits |= FloatProp::SIGN_MASK;
     if (!signVal)

diff  --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
index 468176b27ee4f..a3584c9d38851 100644
--- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
+++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
@@ -60,6 +60,10 @@ template <> struct FPBits<long double> {
 
   UIntType get_mantissa() const { return bits & FloatProp::MANTISSA_MASK; }
 
+  UIntType get_explicit_mantissa() const {
+    return bits & (FloatProp::MANTISSA_MASK | FloatProp::EXPLICIT_BIT_MASK);
+  }
+
   void set_unbiased_exponent(UIntType expVal) {
     expVal =
         (expVal << (FloatProp::BIT_WIDTH - 1 - FloatProp::EXPONENT_WIDTH)) &


        


More information about the libc-commits mailing list