From 7fd61457eaeb35a8f6a6fdd820b7254c993f2603 Mon Sep 17 00:00:00 2001 From: Jay Foad Date: Thu, 27 Feb 2020 09:50:29 +0000 Subject: [PATCH] [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 --- include/llvm/ADT/APFloat.h | 7 +++++++ unittests/ADT/APFloatTest.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/llvm/ADT/APFloat.h b/include/llvm/ADT/APFloat.h index ce8fd6536b4..99d1b12feda 100644 --- a/include/llvm/ADT/APFloat.h +++ b/include/llvm/ADT/APFloat.h @@ -1036,6 +1036,13 @@ public: 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/unittests/ADT/APFloatTest.cpp b/unittests/ADT/APFloatTest.cpp index 6822161729b..9f68bf151e2 100644 --- a/unittests/ADT/APFloatTest.cpp +++ b/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) {