[llvm] add power function to APFloat and APInt (PR #122788)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 13 12:51:25 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Iman Hosseini (ImanHosseini)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/122788.diff
3 Files Affected:
- (modified) llvm/include/llvm/ADT/APFloat.h (+20)
- (modified) llvm/include/llvm/ADT/APInt.h (+3)
- (modified) llvm/lib/Support/APInt.cpp (+19)
``````````diff
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;
+};
``````````
</details>
https://github.com/llvm/llvm-project/pull/122788
More information about the llvm-commits
mailing list