[llvm] [llvm] `APFloat`: Query `hasNanOrInf` from semantics (PR #116158)

Matthias Springer via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 13 20:07:22 PST 2024


https://github.com/matthias-springer created https://github.com/llvm/llvm-project/pull/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.

>From 42160f67225be148e9f24dbb21e9aa84249d02a5 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 | 14 ++++----------
 llvm/lib/Support/APFloat.cpp    |  9 +++++++++
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index 97547fb577e0ec..4bf1d80fe864a3 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -311,6 +311,8 @@ struct APFloatBase {
   static unsigned int semanticsIntSizeInBits(const fltSemantics&, bool);
   static bool semanticsHasZero(const fltSemantics &);
   static bool semanticsHasSignedRepr(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.
@@ -1127,19 +1129,11 @@ 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.
+  /// Returns true if the given semantics supports 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;
-    }
+    return semanticsHasNan(Sem) || semanticsHasInf(Sem);
   }
 
   /// Returns true if the given semantics has actual significand.
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index c566d489d11b03..8b9d9af2ca65b3 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -375,6 +375,15 @@ bool APFloatBase::semanticsHasSignedRepr(const fltSemantics &semantics) {
   return semantics.hasSignedRepr;
 }
 
+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;
+}
+
 bool APFloatBase::isRepresentableAsNormalIn(const fltSemantics &Src,
                                             const fltSemantics &Dst) {
   // Exponent range must be larger.



More information about the llvm-commits mailing list