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

Iman Hosseini via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 11:31:47 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 01/10] 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 02/10] 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 03/10] 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 04/10] 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 05/10] 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;

>From a30e29fedb287f407309204cf0d16f07c9fe9b9b Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 13:34:40 +0000
Subject: [PATCH 06/10] Test (Signed)MaxValue

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

diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index a5009761fd6c00..7fe5fad7479861 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -60,6 +60,20 @@ TEST(APIntTest, PowerThreeTo3) {
   EXPECT_EQ(ThreeTo3, V_27);
 }
 
+// Test that SignedMaxValue^3 == SignedMaxValue
+TEST(APIntTest, PowerSignedMaxValue) {
+  APInt SignedMaxValue = APInt::getSignedMaxValue(32);
+  APInt MaxTo3 = APIntOps::pow(SignedMaxValue, 3);
+  EXPECT_EQ(MaxTo3, SignedMaxValue);
+}
+
+// Test that MaxValue^3 == MaxValue
+TEST(APIntTest, PowerMaxValue) {
+  APInt MaxValue = APInt::getMaxValue(32);
+  APInt MaxTo3 = APIntOps::pow(MaxValue, 3);
+  EXPECT_EQ(MaxValue, MaxTo3);
+}
+
 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
 TEST(APIntTest, ShiftLeftByZero) {
   APInt One = APInt::getZero(65) + 1;

>From 13cfbb3f891e3d7c252a0da29fbb476424a7a192 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 15:31:00 +0000
Subject: [PATCH 07/10] Test 0^0 == 1

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

diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 7fe5fad7479861..3bdfafcd395b33 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -74,6 +74,14 @@ TEST(APIntTest, PowerMaxValue) {
   EXPECT_EQ(MaxValue, MaxTo3);
 }
 
+// Test that MaxValue^3 == MaxValue
+TEST(APIntTest, ZeroToZero) {
+  APInt Zero = APInt::getZero(32);
+  APInt One = APInt::getZero(32) + 1;
+  APInt ZeroToZero = APIntOps::pow(Zero, 0);
+  EXPECT_EQ(ZeroToZero, One);
+}
+
 // Test that APInt shift left works when bitwidth > 64 and shiftamt == 0
 TEST(APIntTest, ShiftLeftByZero) {
   APInt One = APInt::getZero(65) + 1;

>From 34d9b963bee7495ee826a260a7d06f5452f83d05 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 16:02:46 +0000
Subject: [PATCH 08/10] 0^0 returns 1

---
 llvm/include/llvm/ADT/APInt.h    | 1 +
 llvm/unittests/ADT/APIntTest.cpp | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 81ac17c87a9012..9e97cf85a47e72 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2264,6 +2264,7 @@ APInt mulhs(const APInt &C1, const APInt &C2);
 APInt mulhu(const APInt &C1, const APInt &C2);
 
 /// Compute X^N for N>=0.
+/// 0^0 would return 1.
 APInt pow(const APInt &X, int64_t N);
 
 /// Compute GCD of two unsigned APInt values.
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 3bdfafcd395b33..3779e652d4e7fb 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -74,7 +74,7 @@ TEST(APIntTest, PowerMaxValue) {
   EXPECT_EQ(MaxValue, MaxTo3);
 }
 
-// Test that MaxValue^3 == MaxValue
+// Test that 0^0 == 1
 TEST(APIntTest, ZeroToZero) {
   APInt Zero = APInt::getZero(32);
   APInt One = APInt::getZero(32) + 1;

>From d02314b8d2f157c90bde87b43b07e45fd0cacf30 Mon Sep 17 00:00:00 2001
From: Iman Hosseini <hosseini.iman at yahoo.com>
Date: Tue, 14 Jan 2025 17:25:55 +0000
Subject: [PATCH 09/10] Update llvm/include/llvm/ADT/APInt.h

Co-authored-by: Jakub Kuderski <kubakuderski at gmail.com>
---
 llvm/include/llvm/ADT/APInt.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 9e97cf85a47e72..02d58d8c3d31ce 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.
-/// 0^0 would return 1.
+/// 0^0 is supported and returns 1.
 APInt pow(const APInt &X, int64_t N);
 
 /// Compute GCD of two unsigned APInt values.

>From 564f2dc7a7a097ecd205bbc2c564f972a277b451 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 19:31:26 +0000
Subject: [PATCH 10/10] remove parens.

---
 llvm/lib/Support/APInt.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index eb6692ad393d72..24a0d7108b0f71 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3112,9 +3112,8 @@ 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) {
+  if (N == 0)
     return Acc;
-  }
   APInt Base = X;
   int64_t RemainingExponent = N;
   while (RemainingExponent > 0) {



More information about the llvm-commits mailing list