[libc-commits] [PATCH] D129241: [libc][nfc] update get_explicit_mantissa

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Jul 6 16:56:39 PDT 2022


michaelrj created this revision.
michaelrj added reviewers: lntue, sivachandra.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
michaelrj requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129241

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


Index: libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
===================================================================
--- libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
+++ libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
@@ -60,6 +60,10 @@
 
   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)) &
Index: libc/src/__support/FPUtil/FPBits.h
===================================================================
--- libc/src/__support/FPUtil/FPBits.h
+++ libc/src/__support/FPUtil/FPBits.h
@@ -59,11 +59,6 @@
 
   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 @@
                     (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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129241.442724.patch
Type: text/x-patch
Size: 1793 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220706/9a5362cb/attachment.bin>


More information about the libc-commits mailing list