Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions llvm/include/llvm/ADT/APFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,26 @@ inline APFloat abs(APFloat X) {
return X;
}

/// Returns X^N for N >= 0.
inline APFloat pow(const APFloat &X, int64_t 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();
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/ADT/APInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, int64_t N);

/// Compute GCD of two unsigned APInt values.
///
/// This function returns the greatest common divisor of the two APInt values
Expand Down
19 changes: 19 additions & 0 deletions llvm/lib/Support/APInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, int64_t 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;
};
Loading