[llvm] r219473 - [ADT] Add the scalbn function for APFloat.
Chandler Carruth
chandlerc at gmail.com
Thu Oct 9 21:54:30 PDT 2014
Author: chandlerc
Date: Thu Oct 9 23:54:30 2014
New Revision: 219473
URL: http://llvm.org/viewvc/llvm-project?rev=219473&view=rev
Log:
[ADT] Add the scalbn function for APFloat.
Modified:
llvm/trunk/include/llvm/ADT/APFloat.h
llvm/trunk/lib/Support/APFloat.cpp
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=219473&r1=219472&r2=219473&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APFloat.h (original)
+++ llvm/trunk/include/llvm/ADT/APFloat.h Thu Oct 9 23:54:30 2014
@@ -507,6 +507,9 @@ public:
return Result;
}
+ /// \brief Returns: X * 2^Exp for integral exponents.
+ friend APFloat scalbn(APFloat X, int Exp);
+
private:
/// \name Simple Queries
@@ -628,11 +631,12 @@ private:
unsigned int sign : 1;
};
-/// See friend declaration above.
+/// See friend declarations above.
///
-/// This additional declaration is required in order to compile LLVM with IBM
+/// These additional declarations are required in order to compile LLVM with IBM
/// xlC compiler.
hash_code hash_value(const APFloat &Arg);
+APFloat scalbn(APFloat X, int Exp);
} // namespace llvm
Modified: llvm/trunk/lib/Support/APFloat.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APFloat.cpp?rev=219473&r1=219472&r2=219473&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APFloat.cpp (original)
+++ llvm/trunk/lib/Support/APFloat.cpp Thu Oct 9 23:54:30 2014
@@ -3906,3 +3906,20 @@ APFloat::makeZero(bool Negative) {
exponent = semantics->minExponent-1;
APInt::tcSet(significandParts(), 0, partCount());
}
+
+APFloat llvm::scalbn(APFloat X, int Exp) {
+ if (X.isInfinity() || X.isZero() || X.isNaN())
+ return std::move(X);
+
+ auto MaxExp = X.getSemantics().maxExponent;
+ auto MinExp = X.getSemantics().minExponent;
+ if (Exp > (MaxExp - X.exponent))
+ // Overflow saturates to infinity.
+ return APFloat::getInf(X.getSemantics(), X.isNegative());
+ if (Exp < (MinExp - X.exponent))
+ // Underflow saturates to zero.
+ return APFloat::getZero(X.getSemantics(), X.isNegative());
+
+ X.exponent += Exp;
+ return std::move(X);
+}
Modified: llvm/trunk/unittests/ADT/APFloatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APFloatTest.cpp?rev=219473&r1=219472&r2=219473&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APFloatTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APFloatTest.cpp Thu Oct 9 23:54:30 2014
@@ -2742,4 +2742,47 @@ TEST(APFloatTest, logb) {
EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "-0x7Ep+0")
.bitwiseIsEqual(logb(MSmallestNormalized)));
}
+
+TEST(APFloatTest, scalbn) {
+ EXPECT_TRUE(
+ APFloat(APFloat::IEEEsingle, "0x1p+0")
+ .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), 0)));
+ EXPECT_TRUE(
+ APFloat(APFloat::IEEEsingle, "0x1p+42")
+ .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), 42)));
+ EXPECT_TRUE(
+ APFloat(APFloat::IEEEsingle, "0x1p-42")
+ .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), -42)));
+
+ 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 QPNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
+ APFloat QMNaN = APFloat::getNaN(APFloat::IEEEsingle, true);
+ APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
+
+ EXPECT_TRUE(PInf.bitwiseIsEqual(scalbn(PInf, 0)));
+ EXPECT_TRUE(MInf.bitwiseIsEqual(scalbn(MInf, 0)));
+ EXPECT_TRUE(PZero.bitwiseIsEqual(scalbn(PZero, 0)));
+ EXPECT_TRUE(MZero.bitwiseIsEqual(scalbn(MZero, 0)));
+ EXPECT_TRUE(QPNaN.bitwiseIsEqual(scalbn(QPNaN, 0)));
+ EXPECT_TRUE(QMNaN.bitwiseIsEqual(scalbn(QMNaN, 0)));
+ EXPECT_TRUE(SNaN.bitwiseIsEqual(scalbn(SNaN, 0)));
+
+ EXPECT_TRUE(
+ PInf.bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), 128)));
+ EXPECT_TRUE(MInf.bitwiseIsEqual(
+ scalbn(APFloat(APFloat::IEEEsingle, "-0x1p+0"), 128)));
+ EXPECT_TRUE(
+ PInf.bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+127"), 1)));
+ EXPECT_TRUE(PZero.bitwiseIsEqual(
+ scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), -127)));
+ EXPECT_TRUE(MZero.bitwiseIsEqual(
+ scalbn(APFloat(APFloat::IEEEsingle, "-0x1p+0"), -127)));
+ EXPECT_TRUE(PZero.bitwiseIsEqual(
+ scalbn(APFloat(APFloat::IEEEsingle, "0x1p-126"), -1)));
+ EXPECT_TRUE(PZero.bitwiseIsEqual(
+ scalbn(APFloat(APFloat::IEEEsingle, "0x1p-126"), -1)));
+}
}
More information about the llvm-commits
mailing list