Skip to content

Commit

Permalink
[APFloat] Overload unary operator-
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jayfoad committed Mar 6, 2020
1 parent 4cf0ddd commit 3ecfdc7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions llvm/include/llvm/ADT/APFloat.h
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions llvm/unittests/ADT/APFloatTest.cpp
Expand Up @@ -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) {
Expand Down

0 comments on commit 3ecfdc7

Please sign in to comment.