[llvm] add power function to APFloat and APInt (PR #122788)
Iman Hosseini via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 14 02:27:20 PST 2025
https://github.com/ImanHosseini updated https://github.com/llvm/llvm-project/pull/122788
>From 93da5978f1600c000cfcc14507d5056cd23ddf3d Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Mon, 13 Jan 2025 20:47:15 +0000
Subject: [PATCH 1/3] add power function
---
llvm/include/llvm/ADT/APFloat.h | 20 ++++++++++++++++++++
llvm/include/llvm/ADT/APInt.h | 3 +++
llvm/lib/Support/APInt.cpp | 19 +++++++++++++++++++
3 files changed, 42 insertions(+)
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;
+};
>From 3a8a0881cc3fd662ab3620d1de3d9b9638cd1771 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Mon, 13 Jan 2025 21:18:22 +0000
Subject: [PATCH 2/3] int -> int64_t
---
llvm/include/llvm/ADT/APFloat.h | 2 +-
llvm/include/llvm/ADT/APInt.h | 2 +-
llvm/lib/Support/APInt.cpp | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index 236469d43678fe..b6c5613eb43562 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1532,7 +1532,7 @@ inline APFloat abs(APFloat X) {
}
/// Returns X^N for N >= 0.
-inline APFloat pow(const APFloat &X, const int &N) {
+inline APFloat pow(const APFloat &X, int64_t N) {
assert(N >= 0 && "negative exponents not supported.");
if (N == 0) {
return APFloat::getOne(X.getSemantics());
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index e93f1759875688..167a8459ef64f7 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2264,7 +2264,7 @@ APInt mulhs(const APInt &C1, const APInt &C2);
APInt mulhu(const APInt &C1, const APInt &C2);
/// Compute X^N for N>0.
-APInt pow(const APInt &X, const int &N);
+APInt pow(const APInt &X, int64_t N);
/// Compute GCD of two unsigned APInt values.
///
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index d95c1bc8e7194e..527fc9b51cb65a 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3109,7 +3109,7 @@ APInt APIntOps::mulhu(const APInt &C1, const APInt &C2) {
return (C1Ext * C2Ext).extractBits(C1.getBitWidth(), C1.getBitWidth());
}
-APInt APIntOps::pow(const APInt &X, const int &N) {
+APInt APIntOps::pow(const APInt &X, int64_t N) {
assert(N >= 0 && "negative exponents not supported.");
if (N == 0) {
return APInt(X.getBitWidth(), 1);
>From 40f5262da320f5ebbb3f8b125943241723993ed9 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 10:27:02 +0000
Subject: [PATCH 3/3] undo APFloat change
---
llvm/include/llvm/ADT/APFloat.h | 20 --------------------
llvm/include/llvm/ADT/APInt.h | 2 +-
2 files changed, 1 insertion(+), 21 deletions(-)
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index b6c5613eb43562..bf80fa5a06580b 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1531,26 +1531,6 @@ 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();
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 167a8459ef64f7..81ac17c87a9012 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2263,7 +2263,7 @@ 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.
+/// Compute X^N for N>=0.
APInt pow(const APInt &X, int64_t N);
/// Compute GCD of two unsigned APInt values.
More information about the llvm-commits
mailing list