[llvm] add power function to APInt (PR #122788)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 14 21:51:27 PST 2025
================
@@ -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 * Base;
+ RemainingExponent /= 2;
+ }
+ --RemainingExponent;
+ Acc = Acc * Base;
----------------
topperc wrote:
Acc *= Base
https://github.com/llvm/llvm-project/pull/122788
More information about the llvm-commits
mailing list