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

Iman Hosseini via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 13 12:50:54 PST 2025


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

I am trying to calculate power function for APFloat, APInt to constant fold vector reductions:  https://github.com/llvm/llvm-project/pull/122450 
I need this utility to fold N `mul`s into power.  

>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] 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;
+};



More information about the llvm-commits mailing list