[llvm] [llvm] `APFloat`: Query `hasNanOrInf` from semantics (PR #116158)
Matthias Springer via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 14 03:42:58 PST 2024
https://github.com/matthias-springer updated https://github.com/llvm/llvm-project/pull/116158
>From e6b49ccf53f0e01f49474150958769971fd5a077 Mon Sep 17 00:00:00 2001
From: Matthias Springer <mspringer at nvidia.com>
Date: Thu, 14 Nov 2024 04:59:07 +0100
Subject: [PATCH] [llvm] `APFloat`: Query `hasNanOrInf` from semantics
---
llvm/include/llvm/ADT/APFloat.h | 16 +---------------
llvm/lib/Support/APFloat.cpp | 4 ++++
llvm/unittests/ADT/APFloatTest.cpp | 6 ++++--
3 files changed, 9 insertions(+), 17 deletions(-)
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index 97547fb577e0ec..4b5a85945ec17c 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -311,6 +311,7 @@ struct APFloatBase {
static unsigned int semanticsIntSizeInBits(const fltSemantics&, bool);
static bool semanticsHasZero(const fltSemantics &);
static bool semanticsHasSignedRepr(const fltSemantics &);
+ static bool semanticsHasNanOrInf(const fltSemantics &);
// Returns true if any number described by \p Src can be precisely represented
// by a normal (not subnormal) value in \p Dst.
@@ -1127,21 +1128,6 @@ class APFloat : public APFloatBase {
/// \param Semantics - type float semantics
static APFloat getAllOnesValue(const fltSemantics &Semantics);
- /// Returns true if the given semantics supports either NaN or Infinity.
- ///
- /// \param Sem - type float semantics
- static bool hasNanOrInf(const fltSemantics &Sem) {
- switch (SemanticsToEnum(Sem)) {
- default:
- return true;
- // Below Semantics do not support {NaN or Inf}
- case APFloat::S_Float6E3M2FN:
- case APFloat::S_Float6E2M3FN:
- case APFloat::S_Float4E2M1FN:
- return false;
- }
- }
-
/// Returns true if the given semantics has actual significand.
///
/// \param Sem - type float semantics
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index c566d489d11b03..58bf002e0fed2e 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -375,6 +375,10 @@ bool APFloatBase::semanticsHasSignedRepr(const fltSemantics &semantics) {
return semantics.hasSignedRepr;
}
+bool APFloatBase::semanticsHasNanOrInf(const fltSemantics &semantics) {
+ return semantics.nonFiniteBehavior != fltNonfiniteBehavior::FiniteOnly;
+}
+
bool APFloatBase::isRepresentableAsNormalIn(const fltSemantics &Src,
const fltSemantics &Dst) {
// Exponent range must be larger.
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 74aaf66973a19f..ab9db8ed0fa85c 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -832,7 +832,8 @@ TEST(APFloatTest, IsSmallestNormalized) {
EXPECT_FALSE(APFloat::getZero(Semantics, false).isSmallestNormalized());
EXPECT_FALSE(APFloat::getZero(Semantics, true).isSmallestNormalized());
- if (APFloat::hasNanOrInf(Semantics)) {
+ if (APFloat::semanticsHasNanOrInf(Semantics)) {
+ // Types that do not support Inf will return NaN when asked for Inf.
EXPECT_FALSE(APFloat::getInf(Semantics, false).isSmallestNormalized());
EXPECT_FALSE(APFloat::getInf(Semantics, true).isSmallestNormalized());
@@ -7344,7 +7345,8 @@ TEST(APFloatTest, getExactLog2) {
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, false).getExactLog2Abs());
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, true).getExactLog2Abs());
- if (APFloat::hasNanOrInf(Semantics)) {
+ if (APFloat::semanticsHasNanOrInf(Semantics)) {
+ // Types that do not support Inf will return NaN when asked for Inf.
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());
More information about the llvm-commits
mailing list