Add minnum / maxnum to APFloat

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219475 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Matt Arsenault 2014-10-10 05:21:32 +00:00
parent f4ec6697b8
commit c08f0e3743
2 changed files with 44 additions and 0 deletions

View File

@ -649,6 +649,28 @@ private:
hash_code hash_value(const APFloat &Arg); hash_code hash_value(const APFloat &Arg);
APFloat scalbn(APFloat X, int Exp); APFloat scalbn(APFloat X, int Exp);
/// 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
inline APFloat minnum(const APFloat &A, const APFloat &B) {
if (A.isNaN())
return B;
if (B.isNaN())
return A;
return (B.compare(A) == APFloat::cmpLessThan) ? B : A;
}
/// Implements IEEE maxNum semantics. Returns the larger of the 2 arguments if
/// both are not NaN. If either argument is a NaN, returns the other argument.
LLVM_READONLY
inline APFloat maxnum(const APFloat &A, const APFloat &B) {
if (A.isNaN())
return B;
if (B.isNaN())
return A;
return (A.compare(B) == APFloat::cmpLessThan) ? B : A;
}
} // namespace llvm } // namespace llvm
#endif // LLVM_ADT_APFLOAT_H #endif // LLVM_ADT_APFLOAT_H

View File

@ -476,6 +476,28 @@ TEST(APFloatTest, FMA) {
} }
} }
TEST(APFloatTest, MinNum) {
APFloat f1(1.0);
APFloat f2(2.0);
APFloat nan = APFloat::getNaN(APFloat::IEEEdouble);
EXPECT_EQ(1.0, minnum(f1, f2).convertToDouble());
EXPECT_EQ(1.0, minnum(f2, f1).convertToDouble());
EXPECT_EQ(1.0, minnum(f1, nan).convertToDouble());
EXPECT_EQ(1.0, minnum(nan, f1).convertToDouble());
}
TEST(APFloatTest, MaxNum) {
APFloat f1(1.0);
APFloat f2(2.0);
APFloat nan = APFloat::getNaN(APFloat::IEEEdouble);
EXPECT_EQ(2.0, maxnum(f1, f2).convertToDouble());
EXPECT_EQ(2.0, maxnum(f2, f1).convertToDouble());
EXPECT_EQ(1.0, maxnum(f1, nan).convertToDouble());
EXPECT_EQ(1.0, minnum(nan, f1).convertToDouble());
}
TEST(APFloatTest, Denormal) { TEST(APFloatTest, Denormal) {
APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven; APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;