[ADT] Add an (ADL-friendly) abs free function for APFloat that returns

by value having cleared the sign bit.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219485 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chandler Carruth 2014-10-10 08:27:22 +00:00
parent f2f5070d79
commit 082e667c15
2 changed files with 44 additions and 0 deletions

View File

@ -649,6 +649,12 @@ private:
hash_code hash_value(const APFloat &Arg);
APFloat scalbn(APFloat X, int Exp);
/// \brief Returns the absolute value of the argument.
inline APFloat abs(APFloat X) {
X.clearSign();
return X;
}
/// Implements IEEE minNum semantics. Returns the smaller of the 2 arguments if
/// both are not NaN. If either argument is a NaN, returns the other argument.
LLVM_READONLY

View File

@ -2714,6 +2714,44 @@ TEST(APFloatTest, operatorOverloads) {
EXPECT_TRUE(One.bitwiseIsEqual(Two / Two));
}
TEST(APFloatTest, abs) {
APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
APFloat PQNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
APFloat MQNaN = APFloat::getNaN(APFloat::IEEEsingle, true);
APFloat PSNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
APFloat MSNaN = APFloat::getSNaN(APFloat::IEEEsingle, true);
APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
APFloat PSmallestNormalized =
APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
APFloat MSmallestNormalized =
APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
EXPECT_TRUE(PInf.bitwiseIsEqual(abs(PInf)));
EXPECT_TRUE(PInf.bitwiseIsEqual(abs(MInf)));
EXPECT_TRUE(PZero.bitwiseIsEqual(abs(PZero)));
EXPECT_TRUE(PZero.bitwiseIsEqual(abs(MZero)));
EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(PQNaN)));
EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(MQNaN)));
EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(PSNaN)));
EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(MSNaN)));
EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(PNormalValue)));
EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(MNormalValue)));
EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(PLargestValue)));
EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(MLargestValue)));
EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(PSmallestValue)));
EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(MSmallestValue)));
EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(PSmallestNormalized)));
EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(MSmallestNormalized)));
}
TEST(APFloatTest, ilogb) {
EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle, "0x1p+0")));
EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle, "-0x1p+0")));