Skip to content

Commit

Permalink
Add minnum / maxnum to APFloat
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219475 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
arsenm committed Oct 10, 2014
1 parent f4ec669 commit c08f0e3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions include/llvm/ADT/APFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,28 @@ class APFloat {
hash_code hash_value(const APFloat &Arg);
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

#endif // LLVM_ADT_APFLOAT_H
22 changes: 22 additions & 0 deletions unittests/ADT/APFloatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;

Expand Down

0 comments on commit c08f0e3

Please sign in to comment.