diff --git a/include/llvm/ADT/APFloat.h b/include/llvm/ADT/APFloat.h index 33f997e1734..ffd89750422 100644 --- a/include/llvm/ADT/APFloat.h +++ b/include/llvm/ADT/APFloat.h @@ -346,22 +346,57 @@ public: unsigned int convertToHexString(char *dst, unsigned int hexDigits, bool upperCase, roundingMode) const; + /// \name IEEE-754R 5.7.2 General operations. + /// @{ + + /// IEEE-754R isSignMinus: Returns true if and only if the current value is + /// negative. + /// + /// This applies to zeros and NaNs as well. + bool isNegative() const { return sign; } + + /// IEEE-754R isNormal: Returns true if and only if the current value is normal. + /// + /// This implies that the current value of the float is not zero, subnormal, + /// infinite, or NaN following the definition of normality from IEEE-754R. + /// + /// The current implementation of isNormal() differs from this by treating + /// subnormal values as normal values. + bool isIEEENormal() const { return !isDenormal() && isNormal(); } + + /// Returns true if and only if the current value is zero, subnormal, or + /// normal. + /// + /// This means that the value is not infinite or NaN. + bool isFinite() const { return !isNaN() && !isInfinity(); } + + /// Returns true if and only if the float is plus or minus zero. + bool isZero() const { return category == fcZero; } + + /// IEEE-754R isSubnormal(): Returns true if and only if the float is a + /// denormal. + bool isDenormal() const; + + /// IEEE-754R isInfinite(): Returns true if and only if the float is infinity. + bool isInfinity() const { return category == fcInfinity; } + + /// Returns true if and only if the float is a quiet or signaling NaN. + bool isNaN() const { return category == fcNaN; } + + /// Returns true if and only if the float is a signaling NaN. + bool isSignaling() const; + + /// @} + /// \name Simple Queries /// @{ fltCategory getCategory() const { return category; } const fltSemantics &getSemantics() const { return *semantics; } - bool isZero() const { return category == fcZero; } bool isNonZero() const { return category != fcZero; } bool isNormal() const { return category == fcNormal; } - bool isNaN() const { return category == fcNaN; } - bool isInfinity() const { return category == fcInfinity; } - bool isNegative() const { return sign; } bool isPosZero() const { return isZero() && !isNegative(); } bool isNegZero() const { return isZero() && isNegative(); } - bool isDenormal() const; - /// IEEE-754R 5.7.2: isSignaling. Returns true if this is a signaling NaN. - bool isSignaling() const; /// @} diff --git a/unittests/ADT/APFloatTest.cpp b/unittests/ADT/APFloatTest.cpp index 14e99159a97..e1b9158d555 100644 --- a/unittests/ADT/APFloatTest.cpp +++ b/unittests/ADT/APFloatTest.cpp @@ -1397,4 +1397,65 @@ TEST(APFloatTest, PPCDoubleDouble) { EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]); #endif } + +TEST(APFloatTest, isNegative) { + APFloat t(APFloat::IEEEsingle, "0x1p+0"); + EXPECT_FALSE(t.isNegative()); + t = APFloat(APFloat::IEEEsingle, "-0x1p+0"); + EXPECT_TRUE(t.isNegative()); + + EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNegative()); + EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, true).isNegative()); + + EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNegative()); + EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, true).isNegative()); + + EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isNegative()); + EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, true).isNegative()); + + EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNegative()); + EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, true).isNegative()); +} + +TEST(APFloatTest, isIEEENormal) { + APFloat t(APFloat::IEEEsingle, "0x1p+0"); + EXPECT_TRUE(t.isIEEENormal()); + + EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isIEEENormal()); + EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isIEEENormal()); + EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isIEEENormal()); + EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isIEEENormal()); + EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-159").isIEEENormal()); +} + +TEST(APFloatTest, isFinite) { + APFloat t(APFloat::IEEEsingle, "0x1p+0"); + EXPECT_TRUE(t.isFinite()); + EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isFinite()); + EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, false).isFinite()); + EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isFinite()); + EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isFinite()); + EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p-159").isFinite()); +} + +TEST(APFloatTest, isInfinity) { + APFloat t(APFloat::IEEEsingle, "0x1p+0"); + EXPECT_FALSE(t.isInfinity()); + EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, false).isInfinity()); + EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isInfinity()); + EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isInfinity()); + EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isInfinity()); + EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-159").isInfinity()); +} + +TEST(APFloatTest, isNaN) { + APFloat t(APFloat::IEEEsingle, "0x1p+0"); + EXPECT_FALSE(t.isNaN()); + EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNaN()); + EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNaN()); + EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, false).isNaN()); + EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNaN()); + EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-159").isNaN()); +} + }