[llvm] 8a02fd3 - APFloat: Add getExactLog2Abs

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 23 16:16:47 PDT 2023


Author: Matt Arsenault
Date: 2023-08-23T19:16:41-04:00
New Revision: 8a02fd3f94ff706c88cec14f1501cac6b6248162

URL: https://github.com/llvm/llvm-project/commit/8a02fd3f94ff706c88cec14f1501cac6b6248162
DIFF: https://github.com/llvm/llvm-project/commit/8a02fd3f94ff706c88cec14f1501cac6b6248162.diff

LOG: APFloat: Add getExactLog2Abs

Like the recently added getExactLog2 except ignore the sign bit.

https://reviews.llvm.org/D158102

Added: 
    

Modified: 
    llvm/include/llvm/ADT/APFloat.h
    llvm/lib/Support/APFloat.cpp
    llvm/unittests/ADT/APFloatTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index c6d037a97774bb..ce258b86c2146b 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -489,10 +489,17 @@ class IEEEFloat final : public APFloatBase {
   /// return true.
   bool getExactInverse(APFloat *inv) const;
 
+  // If this is an exact power of two, return the exponent while ignoring the
+  // sign bit. If it's not an exact power of 2, return INT_MIN
+  LLVM_READONLY
+  int getExactLog2Abs() const;
+
   // If this is an exact power of two, return the exponent. If it's not an exact
   // power of 2, return INT_MIN
   LLVM_READONLY
-  int getExactLog2() const;
+  int getExactLog2() const {
+    return isNegative() ? INT_MIN : getExactLog2Abs();
+  }
 
   /// Returns the exponent of the internal representation of the APFloat.
   ///
@@ -754,6 +761,8 @@ class DoubleAPFloat final : public APFloatBase {
 
   LLVM_READONLY
   int getExactLog2() const;
+  LLVM_READONLY
+  int getExactLog2Abs() const;
 
   friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp, roundingMode);
   friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp, roundingMode);
@@ -1324,6 +1333,11 @@ class APFloat : public APFloatBase {
     APFLOAT_DISPATCH_ON_SEMANTICS(getExactInverse(inv));
   }
 
+  LLVM_READONLY
+  int getExactLog2Abs() const {
+    APFLOAT_DISPATCH_ON_SEMANTICS(getExactLog2Abs());
+  }
+
   LLVM_READONLY
   int getExactLog2() const {
     APFLOAT_DISPATCH_ON_SEMANTICS(getExactLog2());

diff  --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index 3d8615db705a72..77ba7a0a021537 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -4292,8 +4292,8 @@ bool IEEEFloat::getExactInverse(APFloat *inv) const {
   return true;
 }
 
-int IEEEFloat::getExactLog2() const {
-  if (!isFinite() || isZero() || isNegative())
+int IEEEFloat::getExactLog2Abs() const {
+  if (!isFinite() || isZero())
     return INT_MIN;
 
   const integerPart *Parts = significandParts();
@@ -5121,6 +5121,11 @@ int DoubleAPFloat::getExactLog2() const {
   return INT_MIN;
 }
 
+int DoubleAPFloat::getExactLog2Abs() const {
+  // TODO: Implement me
+  return INT_MIN;
+}
+
 DoubleAPFloat scalbn(const DoubleAPFloat &Arg, int Exp,
                      APFloat::roundingMode RM) {
   assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics");

diff  --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 705e9f102e506c..baf055e503b7e7 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -6597,6 +6597,7 @@ TEST(APFloatTest, getExactLog2) {
     if (I == APFloat::S_PPCDoubleDouble) {
       // Not implemented
       EXPECT_EQ(INT_MIN, One.getExactLog2());
+      EXPECT_EQ(INT_MIN, One.getExactLog2Abs());
       continue;
     }
 
@@ -6607,10 +6608,16 @@ TEST(APFloatTest, getExactLog2) {
     EXPECT_EQ(0, One.getExactLog2());
     EXPECT_EQ(INT_MIN, APFloat(Semantics, "3.0").getExactLog2());
     EXPECT_EQ(INT_MIN, APFloat(Semantics, "-3.0").getExactLog2());
+    EXPECT_EQ(INT_MIN, APFloat(Semantics, "3.0").getExactLog2Abs());
+    EXPECT_EQ(INT_MIN, APFloat(Semantics, "-3.0").getExactLog2Abs());
     EXPECT_EQ(3, APFloat(Semantics, "8.0").getExactLog2());
     EXPECT_EQ(INT_MIN, APFloat(Semantics, "-8.0").getExactLog2());
-    EXPECT_EQ(INT_MIN, APFloat(Semantics, "-0.25").getExactLog2());
     EXPECT_EQ(-2, APFloat(Semantics, "0.25").getExactLog2());
+    EXPECT_EQ(-2, APFloat(Semantics, "0.25").getExactLog2Abs());
+    EXPECT_EQ(INT_MIN, APFloat(Semantics, "-0.25").getExactLog2());
+    EXPECT_EQ(-2, APFloat(Semantics, "-0.25").getExactLog2Abs());
+    EXPECT_EQ(3, APFloat(Semantics, "8.0").getExactLog2Abs());
+    EXPECT_EQ(3, APFloat(Semantics, "-8.0").getExactLog2Abs());
 
     EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, false).getExactLog2());
     EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, true).getExactLog2());
@@ -6619,6 +6626,13 @@ TEST(APFloatTest, getExactLog2) {
     EXPECT_EQ(INT_MIN, APFloat::getNaN(Semantics, false).getExactLog2());
     EXPECT_EQ(INT_MIN, APFloat::getNaN(Semantics, true).getExactLog2());
 
+    EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, false).getExactLog2Abs());
+    EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, true).getExactLog2Abs());
+    EXPECT_EQ(INT_MIN, APFloat::getInf(Semantics).getExactLog2Abs());
+    EXPECT_EQ(INT_MIN, APFloat::getInf(Semantics, true).getExactLog2Abs());
+    EXPECT_EQ(INT_MIN, APFloat::getNaN(Semantics, false).getExactLog2Abs());
+    EXPECT_EQ(INT_MIN, APFloat::getNaN(Semantics, true).getExactLog2Abs());
+
     EXPECT_EQ(INT_MIN,
               scalbn(One, MinExp - Precision - 1, APFloat::rmNearestTiesToEven)
                   .getExactLog2());


        


More information about the llvm-commits mailing list