[llvm] r219453 - [ADT] Add basic operator overloads for arithmetic to APFloat to make
Chandler Carruth
chandlerc at gmail.com
Thu Oct 9 16:26:16 PDT 2014
Author: chandlerc
Date: Thu Oct 9 18:26:15 2014
New Revision: 219453
URL: http://llvm.org/viewvc/llvm-project?rev=219453&view=rev
Log:
[ADT] Add basic operator overloads for arithmetic to APFloat to make
code using it more readable.
Also add a copySign static function that works more like the standard
function by accepting the value and sign-carying value as arguments.
No interesting logic here, but tests added to cover the basic API
additions and make sure they do something plausible.
Modified:
llvm/trunk/include/llvm/ADT/APFloat.h
llvm/trunk/unittests/ADT/APFloatTest.cpp
Modified: llvm/trunk/include/llvm/ADT/APFloat.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APFloat.h?rev=219453&r1=219452&r2=219453&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APFloat.h (original)
+++ llvm/trunk/include/llvm/ADT/APFloat.h Thu Oct 9 18:26:15 2014
@@ -304,6 +304,38 @@ public:
/// IEEE-754R 5.3.1: nextUp/nextDown.
opStatus next(bool nextDown);
+ /// \brief Operator+ overload which provides the default
+ /// \c nmNearestTiesToEven rounding mode and *no* error checking.
+ APFloat operator+(const APFloat &RHS) const {
+ APFloat Result = *this;
+ Result.add(RHS, rmNearestTiesToEven);
+ return Result;
+ }
+
+ /// \brief Operator- overload which provides the default
+ /// \c nmNearestTiesToEven rounding mode and *no* error checking.
+ APFloat operator-(const APFloat &RHS) const {
+ APFloat Result = *this;
+ Result.subtract(RHS, rmNearestTiesToEven);
+ return Result;
+ }
+
+ /// \brief Operator* overload which provides the default
+ /// \c nmNearestTiesToEven rounding mode and *no* error checking.
+ APFloat operator*(const APFloat &RHS) const {
+ APFloat Result = *this;
+ Result.multiply(RHS, rmNearestTiesToEven);
+ return Result;
+ }
+
+ /// \brief Operator/ overload which provides the default
+ /// \c nmNearestTiesToEven rounding mode and *no* error checking.
+ APFloat operator/(const APFloat &RHS) const {
+ APFloat Result = *this;
+ Result.divide(RHS, rmNearestTiesToEven);
+ return Result;
+ }
+
/// @}
/// \name Sign operations.
@@ -313,6 +345,13 @@ public:
void clearSign();
void copySign(const APFloat &);
+ /// \brief A static helper to produce a copy of an APFloat value with its sign
+ /// copied from some other APFloat.
+ static APFloat copySign(APFloat Value, const APFloat &Sign) {
+ Value.copySign(Sign);
+ return std::move(Value);
+ }
+
/// @}
/// \name Conversions
Modified: llvm/trunk/unittests/ADT/APFloatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APFloatTest.cpp?rev=219453&r1=219452&r2=219453&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APFloatTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APFloatTest.cpp Thu Oct 9 18:26:15 2014
@@ -1342,6 +1342,17 @@ TEST(APFloatTest, getZero) {
}
}
+TEST(APFloatTest, copySign) {
+ EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
+ APFloat::copySign(APFloat(42.0), APFloat(-1.0))));
+ EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
+ APFloat::copySign(APFloat(-42.0), APFloat(1.0))));
+ EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
+ APFloat::copySign(APFloat(-42.0), APFloat(-1.0))));
+ EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
+ APFloat::copySign(APFloat(42.0), APFloat(1.0))));
+}
+
TEST(APFloatTest, convert) {
bool losesInfo;
APFloat test(APFloat::IEEEdouble, "1.0");
@@ -2671,4 +2682,13 @@ TEST(APFloatTest, divide) {
}
}
+TEST(APFloatTest, operatorOverloads) {
+ // This is mostly testing that these operator overloads compile.
+ APFloat One = APFloat(APFloat::IEEEsingle, "0x1p+0");
+ APFloat Two = APFloat(APFloat::IEEEsingle, "0x2p+0");
+ EXPECT_TRUE(Two.bitwiseIsEqual(One + One));
+ EXPECT_TRUE(One.bitwiseIsEqual(Two - One));
+ EXPECT_TRUE(Two.bitwiseIsEqual(One * Two));
+ EXPECT_TRUE(One.bitwiseIsEqual(Two / Two));
+}
}
More information about the llvm-commits
mailing list