Skip to content

Commit 2f55de4

Browse files
[llvm] APFloat: Query hasNanOrInf from semantics (#116158)
Whether a floating point type supports NaN or infinity can be queried from its semantics. No need to hard-code a list of types.
1 parent eec21cc commit 2f55de4

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

llvm/include/llvm/ADT/APFloat.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ 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 &);
314315

315316
// Returns true if any number described by \p Src can be precisely represented
316317
// by a normal (not subnormal) value in \p Dst.
@@ -1127,21 +1128,6 @@ class APFloat : public APFloatBase {
11271128
/// \param Semantics - type float semantics
11281129
static APFloat getAllOnesValue(const fltSemantics &Semantics);
11291130

1130-
/// Returns true if the given semantics supports either NaN or Infinity.
1131-
///
1132-
/// \param Sem - type float semantics
1133-
static bool hasNanOrInf(const fltSemantics &Sem) {
1134-
switch (SemanticsToEnum(Sem)) {
1135-
default:
1136-
return true;
1137-
// Below Semantics do not support {NaN or Inf}
1138-
case APFloat::S_Float6E3M2FN:
1139-
case APFloat::S_Float6E2M3FN:
1140-
case APFloat::S_Float4E2M1FN:
1141-
return false;
1142-
}
1143-
}
1144-
11451131
/// Returns true if the given semantics has actual significand.
11461132
///
11471133
/// \param Sem - type float semantics

llvm/lib/Support/APFloat.cpp

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

378+
bool APFloatBase::semanticsHasNanOrInf(const fltSemantics &semantics) {
379+
return semantics.nonFiniteBehavior != fltNonfiniteBehavior::FiniteOnly;
380+
}
381+
378382
bool APFloatBase::isRepresentableAsNormalIn(const fltSemantics &Src,
379383
const fltSemantics &Dst) {
380384
// Exponent range must be larger.

llvm/unittests/ADT/APFloatTest.cpp

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

835-
if (APFloat::hasNanOrInf(Semantics)) {
835+
if (APFloat::semanticsHasNanOrInf(Semantics)) {
836+
// Types that do not support Inf will return NaN when asked for Inf.
836837
EXPECT_FALSE(APFloat::getInf(Semantics, false).isSmallestNormalized());
837838
EXPECT_FALSE(APFloat::getInf(Semantics, true).isSmallestNormalized());
838839

@@ -7344,7 +7345,8 @@ TEST(APFloatTest, getExactLog2) {
73447345
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, false).getExactLog2Abs());
73457346
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, true).getExactLog2Abs());
73467347

7347-
if (APFloat::hasNanOrInf(Semantics)) {
7348+
if (APFloat::semanticsHasNanOrInf(Semantics)) {
7349+
// Types that do not support Inf will return NaN when asked for Inf.
73487350
EXPECT_EQ(INT_MIN, APFloat::getInf(Semantics).getExactLog2());
73497351
EXPECT_EQ(INT_MIN, APFloat::getInf(Semantics, true).getExactLog2());
73507352
EXPECT_EQ(INT_MIN, APFloat::getNaN(Semantics, false).getExactLog2());

0 commit comments

Comments
 (0)