aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/APInt.cpp
diff options
context:
space:
mode:
authorIman Hosseini <imanhosseini@google.com>2025-01-17 14:40:31 +0000
committerGitHub <noreply@github.com>2025-01-17 14:40:31 +0000
commit8ae1cb2bcb55293cce31bb75c38d6b4e8a13cc23 (patch)
tree434c76e3876d25161af7c99928acc9fc5b56e216 /llvm/lib/Support/APInt.cpp
parenta18f4bdb18d59858e384540a62c9145c888cc9b2 (diff)
downloadllvm-8ae1cb2bcb55293cce31bb75c38d6b4e8a13cc23.zip
llvm-8ae1cb2bcb55293cce31bb75c38d6b4e8a13cc23.tar.gz
llvm-8ae1cb2bcb55293cce31bb75c38d6b4e8a13cc23.tar.bz2
add power function to APInt (#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. --------- Co-authored-by: ImanHosseini <imanhosseini.17@gmail.com> Co-authored-by: Jakub Kuderski <kubakuderski@gmail.com>
Diffstat (limited to 'llvm/lib/Support/APInt.cpp')
-rw-r--r--llvm/lib/Support/APInt.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index ea8295f..38cf485 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3108,3 +3108,21 @@ 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, int64_t N) {
+ assert(N >= 0 && "negative exponents not supported.");
+ APInt Acc = APInt(X.getBitWidth(), 1);
+ if (N == 0)
+ return Acc;
+ APInt Base = X;
+ int64_t RemainingExponent = N;
+ while (RemainingExponent > 0) {
+ while (RemainingExponent % 2 == 0) {
+ Base *= Base;
+ RemainingExponent /= 2;
+ }
+ --RemainingExponent;
+ Acc *= Base;
+ }
+ return Acc;
+};