Skip to content

Commit

Permalink
[llvm] APFloat: Add helpers to query NaN/inf semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-springer committed Nov 15, 2024
1 parent 17bc738 commit 1668bd7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion llvm/include/llvm/ADT/APFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ struct APFloatBase {
static unsigned int semanticsIntSizeInBits(const fltSemantics&, bool);
static bool semanticsHasZero(const fltSemantics &);
static bool semanticsHasSignedRepr(const fltSemantics &);
static bool semanticsHasNanOrInf(const fltSemantics &);
static bool semanticsHasInf(const fltSemantics &);
static bool semanticsHasNaN(const fltSemantics &);

// Returns true if any number described by \p Src can be precisely represented
// by a normal (not subnormal) value in \p Dst.
Expand Down
7 changes: 6 additions & 1 deletion llvm/lib/Support/APFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,12 @@ bool APFloatBase::semanticsHasSignedRepr(const fltSemantics &semantics) {
return semantics.hasSignedRepr;
}

bool APFloatBase::semanticsHasNanOrInf(const fltSemantics &semantics) {
bool APFloatBase::semanticsHasInf(const fltSemantics &semantics) {
return semantics.nonFiniteBehavior != fltNonfiniteBehavior::NanOnly &&
semantics.nonFiniteBehavior != fltNonfiniteBehavior::FiniteOnly;
}

bool APFloatBase::semanticsHasNaN(const fltSemantics &semantics) {
return semantics.nonFiniteBehavior != fltNonfiniteBehavior::FiniteOnly;
}

Expand Down
22 changes: 20 additions & 2 deletions llvm/unittests/ADT/APFloatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,9 @@ TEST(APFloatTest, IsSmallestNormalized) {
EXPECT_FALSE(APFloat::getZero(Semantics, false).isSmallestNormalized());
EXPECT_FALSE(APFloat::getZero(Semantics, true).isSmallestNormalized());

if (APFloat::semanticsHasNanOrInf(Semantics)) {
if (APFloat::semanticsHasNaN(Semantics)) {
// Types that do not support Inf will return NaN when asked for Inf.
// (But only if they support NaN.)
EXPECT_FALSE(APFloat::getInf(Semantics, false).isSmallestNormalized());
EXPECT_FALSE(APFloat::getInf(Semantics, true).isSmallestNormalized());

Expand Down Expand Up @@ -2557,6 +2558,14 @@ TEST(APFloatTest, isInfinity) {
EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isInfinity());
EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isInfinity());
EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isInfinity());

for (unsigned I = 0; I != APFloat::S_MaxSemantics + 1; ++I) {
const fltSemantics &Semantics =
APFloat::EnumToSemantics(static_cast<APFloat::Semantics>(I));
if (APFloat::semanticsHasInf(fltSemantics)) {
EXPECT_TRUE(APFloat::getInf(fltSemantics).isInfinity());
}
}
}

TEST(APFloatTest, isNaN) {
Expand All @@ -2567,6 +2576,14 @@ TEST(APFloatTest, isNaN) {
EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNaN());
EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNaN());
EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isNaN());

for (unsigned I = 0; I != APFloat::S_MaxSemantics + 1; ++I) {
const fltSemantics &Semantics =
APFloat::EnumToSemantics(static_cast<APFloat::Semantics>(I));
if (APFloat::semanticsHasNaN(fltSemantics)) {
EXPECT_TRUE(APFloat::getNaN(fltSemantics).isNaN());
}
}
}

TEST(APFloatTest, isFiniteNonZero) {
Expand Down Expand Up @@ -7345,8 +7362,9 @@ TEST(APFloatTest, getExactLog2) {
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, false).getExactLog2Abs());
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, true).getExactLog2Abs());

if (APFloat::semanticsHasNanOrInf(Semantics)) {
if (APFloat::semanticsHasNaN(Semantics)) {
// Types that do not support Inf will return NaN when asked for Inf.
// (But only if they support NaN.)
EXPECT_EQ(INT_MIN, APFloat::getInf(Semantics).getExactLog2());
EXPECT_EQ(INT_MIN, APFloat::getInf(Semantics, true).getExactLog2());
EXPECT_EQ(INT_MIN, APFloat::getNaN(Semantics, false).getExactLog2());
Expand Down

0 comments on commit 1668bd7

Please sign in to comment.