[llvm] add power function to APInt (PR #122788)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 14 21:51:26 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;
----------------
topperc wrote:
`Base *= Base` might give the APInt the opportunity to update `Base` in place for large bit widths. Though I don't think that optimization exists for multiply. It does exist for add/sub.
https://github.com/llvm/llvm-project/pull/122788
More information about the llvm-commits
mailing list