[llvm] add power function to APInt (PR #122788)

Iman Hosseini via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 03:26:36 PST 2025


https://github.com/ImanHosseini updated https://github.com/llvm/llvm-project/pull/122788

>From 93da5978f1600c000cfcc14507d5056cd23ddf3d Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Mon, 13 Jan 2025 20:47:15 +0000
Subject: [PATCH 1/5] add power function

---
 llvm/include/llvm/ADT/APFloat.h | 20 ++++++++++++++++++++
 llvm/include/llvm/ADT/APInt.h   |  3 +++
 llvm/lib/Support/APInt.cpp      | 19 +++++++++++++++++++
 3 files changed, 42 insertions(+)

diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index bf80fa5a06580b..236469d43678fe 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1531,6 +1531,26 @@ inline APFloat abs(APFloat X) {
   return X;
 }
 
+/// Returns X^N for N >= 0.
+inline APFloat pow(const APFloat &X, const int &N) {
+  assert(N >= 0 && "negative exponents not supported.");
+  if (N == 0) {
+    return APFloat::getOne(X.getSemantics());
+  }
+  APFloat Acc = X;
+  int64_t RemainingExponent = N;
+  while (RemainingExponent > 1) {
+    if (RemainingExponent % 2 == 0) {
+      Acc = Acc * Acc;
+      RemainingExponent /= 2;
+    } else {
+      Acc = Acc * X;
+      RemainingExponent--;
+    }
+  }
+  return Acc;
+};
+
 /// Returns the negated value of the argument.
 inline APFloat neg(APFloat X) {
   X.changeSign();
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 225390f1af60b3..e93f1759875688 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2263,6 +2263,9 @@ APInt mulhs(const APInt &C1, const APInt &C2);
 /// Returns the high N bits of the multiplication result.
 APInt mulhu(const APInt &C1, const APInt &C2);
 
+/// Compute X^N for N>0.
+APInt pow(const APInt &X, const int &N);
+
 /// Compute GCD of two unsigned APInt values.
 ///
 /// This function returns the greatest common divisor of the two APInt values
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index ea8295f95c751a..d95c1bc8e7194e 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3108,3 +3108,22 @@ APInt APIntOps::mulhu(const APInt &C1, const APInt &C2) {
   APInt C2Ext = C2.zext(FullWidth);
   return (C1Ext * C2Ext).extractBits(C1.getBitWidth(), C1.getBitWidth());
 }
+
+APInt APIntOps::pow(const APInt &X, const int &N) {
+  assert(N >= 0 && "negative exponents not supported.");
+  if (N == 0) {
+    return APInt(X.getBitWidth(), 1);
+  }
+  APInt Acc = X;
+  int64_t RemainingExponent = N;
+  while (RemainingExponent > 1) {
+    if (RemainingExponent % 2 == 0) {
+      Acc = Acc * Acc;
+      RemainingExponent /= 2;
+    } else {
+      Acc = Acc * X;
+      RemainingExponent--;
+    }
+  }
+  return Acc;
+};

>From 3a8a0881cc3fd662ab3620d1de3d9b9638cd1771 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Mon, 13 Jan 2025 21:18:22 +0000
Subject: [PATCH 2/5] int -> int64_t

---
 llvm/include/llvm/ADT/APFloat.h | 2 +-
 llvm/include/llvm/ADT/APInt.h   | 2 +-
 llvm/lib/Support/APInt.cpp      | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index 236469d43678fe..b6c5613eb43562 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1532,7 +1532,7 @@ inline APFloat abs(APFloat X) {
 }
 
 /// Returns X^N for N >= 0.
-inline APFloat pow(const APFloat &X, const int &N) {
+inline APFloat pow(const APFloat &X, int64_t N) {
   assert(N >= 0 && "negative exponents not supported.");
   if (N == 0) {
     return APFloat::getOne(X.getSemantics());
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index e93f1759875688..167a8459ef64f7 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2264,7 +2264,7 @@ APInt mulhs(const APInt &C1, const APInt &C2);
 APInt mulhu(const APInt &C1, const APInt &C2);
 
 /// Compute X^N for N>0.
-APInt pow(const APInt &X, const int &N);
+APInt pow(const APInt &X, int64_t N);
 
 /// Compute GCD of two unsigned APInt values.
 ///
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index d95c1bc8e7194e..527fc9b51cb65a 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3109,7 +3109,7 @@ APInt APIntOps::mulhu(const APInt &C1, const APInt &C2) {
   return (C1Ext * C2Ext).extractBits(C1.getBitWidth(), C1.getBitWidth());
 }
 
-APInt APIntOps::pow(const APInt &X, const int &N) {
+APInt APIntOps::pow(const APInt &X, int64_t N) {
   assert(N >= 0 && "negative exponents not supported.");
   if (N == 0) {
     return APInt(X.getBitWidth(), 1);

>From 40f5262da320f5ebbb3f8b125943241723993ed9 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 10:27:02 +0000
Subject: [PATCH 3/5] undo APFloat change

---
 llvm/include/llvm/ADT/APFloat.h | 20 --------------------
 llvm/include/llvm/ADT/APInt.h   |  2 +-
 2 files changed, 1 insertion(+), 21 deletions(-)

diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index b6c5613eb43562..bf80fa5a06580b 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1531,26 +1531,6 @@ inline APFloat abs(APFloat X) {
   return X;
 }
 
-/// Returns X^N for N >= 0.
-inline APFloat pow(const APFloat &X, int64_t N) {
-  assert(N >= 0 && "negative exponents not supported.");
-  if (N == 0) {
-    return APFloat::getOne(X.getSemantics());
-  }
-  APFloat Acc = X;
-  int64_t RemainingExponent = N;
-  while (RemainingExponent > 1) {
-    if (RemainingExponent % 2 == 0) {
-      Acc = Acc * Acc;
-      RemainingExponent /= 2;
-    } else {
-      Acc = Acc * X;
-      RemainingExponent--;
-    }
-  }
-  return Acc;
-};
-
 /// Returns the negated value of the argument.
 inline APFloat neg(APFloat X) {
   X.changeSign();
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 167a8459ef64f7..81ac17c87a9012 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2263,7 +2263,7 @@ APInt mulhs(const APInt &C1, const APInt &C2);
 /// Returns the high N bits of the multiplication result.
 APInt mulhu(const APInt &C1, const APInt &C2);
 
-/// Compute X^N for N>0.
+/// Compute X^N for N>=0.
 APInt pow(const APInt &X, int64_t N);
 
 /// Compute GCD of two unsigned APInt values.

>From 073727cc0167db19eaa0ed6864674bb8c1805879 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 10:41:55 +0000
Subject: [PATCH 4/5] Add unit tests.

---
 llvm/unittests/ADT/APIntTest.cpp | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 4d5553fcbd1e3f..89dd01e71771e0 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -29,6 +29,29 @@ TEST(APIntTest, ValueInit) {
   EXPECT_TRUE(!Zero.sext(64));
 }
 
+// Test that 0^5 == 0
+TEST(APIntTest, PowZeroTo5) {
+  APInt Zero = APInt();
+  EXPECT_TRUE(!Zero);
+  APInt ZeroTo5 = APIntOps::pow(Zero, 5);
+  EXPECT_TRUE(!ZeroTo5);
+}
+
+// Test that 1^16 == 1
+TEST(APIntTest, PowOneTo16) {
+  APInt One = APInt::getZero(32) + 1;
+  APInt OneTo16 = APIntOps::pow(One, 16);
+  EXPECT_EQ(One, OneTo16);
+}
+
+// Test that 2^10 == 1024
+TEST(APIntTest, PowerTwoTo10) {
+  APInt Two = APInt::getZero(32) + 2;
+  APInt TwoTo20 = APIntOps::pow(Two, 10);
+  APInt V_1024 = APInt::getZero(32) + 1024;
+  EXPECT_EQ(TwoTo20, V_1024);
+}
+
 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
 TEST(APIntTest, ShiftLeftByZero) {
   APInt One = APInt::getZero(65) + 1;

>From 77d8c07deeb1c7ca1d5f35324a2a4752f1383392 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 11:26:19 +0000
Subject: [PATCH 5/5] Add test. Change impl.

---
 llvm/lib/Support/APInt.cpp       | 16 ++++++++--------
 llvm/unittests/ADT/APIntTest.cpp |  8 ++++++++
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 527fc9b51cb65a..eb6692ad393d72 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3111,19 +3111,19 @@ APInt APIntOps::mulhu(const APInt &C1, const APInt &C2) {
 
 APInt APIntOps::pow(const APInt &X, int64_t N) {
   assert(N >= 0 && "negative exponents not supported.");
+  APInt Acc = APInt(X.getBitWidth(), 1);
   if (N == 0) {
-    return APInt(X.getBitWidth(), 1);
+    return Acc;
   }
-  APInt Acc = X;
+  APInt Base = X;
   int64_t RemainingExponent = N;
-  while (RemainingExponent > 1) {
-    if (RemainingExponent % 2 == 0) {
-      Acc = Acc * Acc;
+  while (RemainingExponent > 0) {
+    while (RemainingExponent % 2 == 0) {
+      Base = Base * Base;
       RemainingExponent /= 2;
-    } else {
-      Acc = Acc * X;
-      RemainingExponent--;
     }
+    --RemainingExponent;
+    Acc = Acc * Base;
   }
   return Acc;
 };
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 89dd01e71771e0..a5009761fd6c00 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -52,6 +52,14 @@ TEST(APIntTest, PowerTwoTo10) {
   EXPECT_EQ(TwoTo20, V_1024);
 }
 
+// Test that 3^3 == 27
+TEST(APIntTest, PowerThreeTo3) {
+  APInt Three = APInt::getZero(32) + 3;
+  APInt ThreeTo3 = APIntOps::pow(Three, 3);
+  APInt V_27 = APInt::getZero(32) + 27;
+  EXPECT_EQ(ThreeTo3, V_27);
+}
+
 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
 TEST(APIntTest, ShiftLeftByZero) {
   APInt One = APInt::getZero(65) + 1;



More information about the llvm-commits mailing list