Skip to content

Commit 1668bd7

Browse files
[llvm] APFloat: Add helpers to query NaN/inf semantics
1 parent 17bc738 commit 1668bd7

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

llvm/include/llvm/ADT/APFloat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ struct APFloatBase {
311311
static unsigned int semanticsIntSizeInBits(const fltSemantics&, bool);
312312
static bool semanticsHasZero(const fltSemantics &);
313313
static bool semanticsHasSignedRepr(const fltSemantics &);
314-
static bool semanticsHasNanOrInf(const fltSemantics &);
314+
static bool semanticsHasInf(const fltSemantics &);
315+
static bool semanticsHasNaN(const fltSemantics &);
315316

316317
// Returns true if any number described by \p Src can be precisely represented
317318
// by a normal (not subnormal) value in \p Dst.

llvm/lib/Support/APFloat.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,12 @@ bool APFloatBase::semanticsHasSignedRepr(const fltSemantics &semantics) {
375375
return semantics.hasSignedRepr;
376376
}
377377

378-
bool APFloatBase::semanticsHasNanOrInf(const fltSemantics &semantics) {
378+
bool APFloatBase::semanticsHasInf(const fltSemantics &semantics) {
379+
return semantics.nonFiniteBehavior != fltNonfiniteBehavior::NanOnly &&
380+
semantics.nonFiniteBehavior != fltNonfiniteBehavior::FiniteOnly;
381+
}
382+
383+
bool APFloatBase::semanticsHasNaN(const fltSemantics &semantics) {
379384
return semantics.nonFiniteBehavior != fltNonfiniteBehavior::FiniteOnly;
380385
}
381386

llvm/unittests/ADT/APFloatTest.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,9 @@ TEST(APFloatTest, IsSmallestNormalized) {
832832
EXPECT_FALSE(APFloat::getZero(Semantics, false).isSmallestNormalized());
833833
EXPECT_FALSE(APFloat::getZero(Semantics, true).isSmallestNormalized());
834834

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

@@ -2557,6 +2558,14 @@ TEST(APFloatTest, isInfinity) {
25572558
EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isInfinity());
25582559
EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isInfinity());
25592560
EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isInfinity());
2561+
2562+
for (unsigned I = 0; I != APFloat::S_MaxSemantics + 1; ++I) {
2563+
const fltSemantics &Semantics =
2564+
APFloat::EnumToSemantics(static_cast<APFloat::Semantics>(I));
2565+
if (APFloat::semanticsHasInf(fltSemantics)) {
2566+
EXPECT_TRUE(APFloat::getInf(fltSemantics).isInfinity());
2567+
}
2568+
}
25602569
}
25612570

25622571
TEST(APFloatTest, isNaN) {
@@ -2567,6 +2576,14 @@ TEST(APFloatTest, isNaN) {
25672576
EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNaN());
25682577
EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNaN());
25692578
EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isNaN());
2579+
2580+
for (unsigned I = 0; I != APFloat::S_MaxSemantics + 1; ++I) {
2581+
const fltSemantics &Semantics =
2582+
APFloat::EnumToSemantics(static_cast<APFloat::Semantics>(I));
2583+
if (APFloat::semanticsHasNaN(fltSemantics)) {
2584+
EXPECT_TRUE(APFloat::getNaN(fltSemantics).isNaN());
2585+
}
2586+
}
25702587
}
25712588

25722589
TEST(APFloatTest, isFiniteNonZero) {
@@ -7345,8 +7362,9 @@ TEST(APFloatTest, getExactLog2) {
73457362
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, false).getExactLog2Abs());
73467363
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, true).getExactLog2Abs());
73477364

7348-
if (APFloat::semanticsHasNanOrInf(Semantics)) {
7365+
if (APFloat::semanticsHasNaN(Semantics)) {
73497366
// Types that do not support Inf will return NaN when asked for Inf.
7367+
// (But only if they support NaN.)
73507368
EXPECT_EQ(INT_MIN, APFloat::getInf(Semantics).getExactLog2());
73517369
EXPECT_EQ(INT_MIN, APFloat::getInf(Semantics, true).getExactLog2());
73527370
EXPECT_EQ(INT_MIN, APFloat::getNaN(Semantics, false).getExactLog2());

0 commit comments

Comments
 (0)