[llvm] [APFloat] add power (PR #122889)
Iman Hosseini via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 14 07:35:00 PST 2025
https://github.com/ImanHosseini updated https://github.com/llvm/llvm-project/pull/122889
>From 10da74e43550f58760ed899282cb4d1002ff6070 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 11:35:31 +0000
Subject: [PATCH 1/3] add apfloat power
---
llvm/include/llvm/ADT/APFloat.h | 21 +++++++++++++++++++++
llvm/unittests/ADT/APFloatTest.cpp | 15 +++++++++++++++
2 files changed, 36 insertions(+)
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index 9792749230cbf9..803949cf9abcbf 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1536,6 +1536,27 @@ inline APFloat abs(APFloat X) {
return X;
}
+/// Returns X^N for N >= 0.
+/// Returns X^N for N >= 0.
+inline APFloat pow(const APFloat &X, const int &N) {
+ assert(N >= 0 && "negative exponents not supported.");
+ APFloat Acc = APFloat::getOne(X.getSemantics());
+ if (N == 0) {
+ return APFloat::getOne(X.getSemantics());
+ }
+ APFloat Base = X;
+ int64_t RemainingExponent = N;
+ while (RemainingExponent > 0) {
+ while (RemainingExponent % 2 == 0) {
+ Base = Base * Base;
+ RemainingExponent /= 2;
+ }
+ --RemainingExponent;
+ Acc = Acc * Base;
+ }
+ return Acc;
+};
+
/// Returns the negated value of the argument.
inline APFloat neg(APFloat X) {
X.changeSign();
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index f291c814886d35..0e4fe151af10af 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -3793,6 +3793,21 @@ TEST(APFloatTest, abs) {
EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(MSmallestNormalized)));
}
+TEST(APFloatTest, pow) {
+ APFloat One = APFloat(APFloat::IEEEsingle(), "1.0");
+ APFloat Two = APFloat(APFloat::IEEEsingle(), "2.0");
+ APFloat Four = APFloat(APFloat::IEEEsingle(), "4.0");
+ APFloat Eight = APFloat(APFloat::IEEEsingle(), "8.0");
+ APFloat NegTwo = APFloat(APFloat::IEEEsingle(), "-2.0");
+ APFloat NegEight = APFloat(APFloat::IEEEsingle(), "-8.0");
+
+ EXPECT_TRUE(One.bitwiseIsEqual(pow(One, 0)));
+ EXPECT_TRUE(One.bitwiseIsEqual(pow(One, 3)));
+ EXPECT_TRUE(Four.bitwiseIsEqual(pow(Two, 2)));
+ EXPECT_TRUE(Eight.bitwiseIsEqual(pow(Two, 3)));
+ EXPECT_TRUE(NegEight.bitwiseIsEqual(pow(NegTwo, 3)));
+}
+
TEST(APFloatTest, neg) {
APFloat One = APFloat(APFloat::IEEEsingle(), "1.0");
APFloat NegOne = APFloat(APFloat::IEEEsingle(), "-1.0");
>From eb093a1b06402fbdfe89769aa7ea1caee7c9a4e9 Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 12:58:19 +0000
Subject: [PATCH 2/3] rmv extra ;
---
llvm/include/llvm/ADT/APFloat.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index 803949cf9abcbf..eeace70dfad29f 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1555,7 +1555,7 @@ inline APFloat pow(const APFloat &X, const int &N) {
Acc = Acc * Base;
}
return Acc;
-};
+}
/// Returns the negated value of the argument.
inline APFloat neg(APFloat X) {
>From 38468c983bbd6047dd842dc3d5d5550a198e736d Mon Sep 17 00:00:00 2001
From: ImanHosseini <imanhosseini.17 at gmail.com>
Date: Tue, 14 Jan 2025 15:34:38 +0000
Subject: [PATCH 3/3] N -> int64_t to be similar to APInt pow
---
llvm/include/llvm/ADT/APFloat.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index eeace70dfad29f..6b669eca6f82b5 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1537,8 +1537,7 @@ inline APFloat abs(APFloat X) {
}
/// Returns X^N for N >= 0.
-/// 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.");
APFloat Acc = APFloat::getOne(X.getSemantics());
if (N == 0) {
More information about the llvm-commits
mailing list