diff options
-rw-r--r-- | llvm/include/llvm/ADT/APFloat.h | 3 | ||||
-rw-r--r-- | llvm/lib/Support/APFloat.cpp | 9 | ||||
-rw-r--r-- | llvm/unittests/ADT/APFloatTest.cpp | 16 |
3 files changed, 27 insertions, 1 deletions
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h index 5984749ea9a..3fe04060fd5 100644 --- a/llvm/include/llvm/ADT/APFloat.h +++ b/llvm/include/llvm/ADT/APFloat.h @@ -448,6 +448,9 @@ public: /// Returns true if and only if the number has the largest possible finite /// magnitude in the current semantics. bool isLargest() const; + + /// Returns true if and only if the number is an exact integer. + bool isInteger() const; /// @} diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp index 91b3db59d0e..19b8221b60c 100644 --- a/llvm/lib/Support/APFloat.cpp +++ b/llvm/lib/Support/APFloat.cpp @@ -768,6 +768,15 @@ APFloat::isLargest() const { } bool +APFloat::isInteger() const { + // This could be made more efficient; I'm going for obviously correct. + if (!isFinite()) return false; + APFloat truncated = *this; + truncated.roundToIntegral(rmTowardZero); + return compare(truncated) == cmpEqual; +} + +bool APFloat::bitwiseIsEqual(const APFloat &rhs) const { if (this == &rhs) return true; diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp index a4445f6e465..55c3f48f00d 100644 --- a/llvm/unittests/ADT/APFloatTest.cpp +++ b/llvm/unittests/ADT/APFloatTest.cpp @@ -1313,7 +1313,21 @@ TEST(APFloatTest, roundToIntegral) { P = APFloat::getInf(APFloat::IEEEdouble, true); P.roundToIntegral(APFloat::rmTowardZero); EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() < 0.0); - +} + +TEST(APFloatTest, isInteger) { + APFloat T(-0.0); + EXPECT_TRUE(T.isInteger()); + T = APFloat(3.14159); + EXPECT_FALSE(T.isInteger()); + T = APFloat::getNaN(APFloat::IEEEdouble); + EXPECT_FALSE(T.isInteger()); + T = APFloat::getInf(APFloat::IEEEdouble); + EXPECT_FALSE(T.isInteger()); + T = APFloat::getInf(APFloat::IEEEdouble, true); + EXPECT_FALSE(T.isInteger()); + T = APFloat::getLargest(APFloat::IEEEdouble); + EXPECT_TRUE(T.isInteger()); } TEST(APFloatTest, getLargest) { |