[llvm] 3ecfdc7 - [APFloat] Overload unary operator-
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 6 01:11:51 PST 2020
Author: Jay Foad
Date: 2020-03-06T09:11:38Z
New Revision: 3ecfdc70cfb9823960aa4a43aaec2eb4eaab2241
URL: https://github.com/llvm/llvm-project/commit/3ecfdc70cfb9823960aa4a43aaec2eb4eaab2241
DIFF: https://github.com/llvm/llvm-project/commit/3ecfdc70cfb9823960aa4a43aaec2eb4eaab2241.diff
LOG: [APFloat] Overload unary operator-
Summary:
We already have overloaded binary arithemetic operators so you can write
A+B etc. This patch lets you write -A instead of neg(A).
Subscribers: hiraditya, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75236
Added:
Modified:
llvm/include/llvm/ADT/APFloat.h
llvm/unittests/ADT/APFloatTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index ce8fd6536b4b..99d1b12feda0 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -1036,6 +1036,13 @@ class APFloat : public APFloatBase {
APFLOAT_DISPATCH_ON_SEMANTICS(next(nextDown));
}
+ /// Negate an APFloat.
+ APFloat operator-() const {
+ APFloat Result(*this);
+ Result.changeSign();
+ return Result;
+ }
+
/// Add two APFloats, rounding ties to the nearest even.
/// No error checking.
APFloat operator+(const APFloat &RHS) const {
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 6822161729b2..9f68bf151e2b 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -2990,6 +2990,17 @@ TEST(APFloatTest, neg) {
EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf)));
EXPECT_TRUE(NegQNaN.bitwiseIsEqual(neg(QNaN)));
EXPECT_TRUE(QNaN.bitwiseIsEqual(neg(NegQNaN)));
+
+ EXPECT_TRUE(NegOne.bitwiseIsEqual(-One));
+ EXPECT_TRUE(One.bitwiseIsEqual(-NegOne));
+ EXPECT_TRUE(NegZero.bitwiseIsEqual(-Zero));
+ EXPECT_TRUE(Zero.bitwiseIsEqual(-NegZero));
+ EXPECT_TRUE(NegInf.bitwiseIsEqual(-Inf));
+ EXPECT_TRUE(Inf.bitwiseIsEqual(-NegInf));
+ EXPECT_TRUE(NegInf.bitwiseIsEqual(-Inf));
+ EXPECT_TRUE(Inf.bitwiseIsEqual(-NegInf));
+ EXPECT_TRUE(NegQNaN.bitwiseIsEqual(-QNaN));
+ EXPECT_TRUE(QNaN.bitwiseIsEqual(-NegQNaN));
}
TEST(APFloatTest, ilogb) {
More information about the llvm-commits
mailing list