Skip to content

[llvm] APFloat: Query hasNanOrInf from semantics #116158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

matthias-springer
Copy link
Member

Whether a floating point type supports NaN or infinity can be queried from its semantics. No need to hard-code a list of types.

@llvmbot
Copy link
Member

llvmbot commented Nov 14, 2024

@llvm/pr-subscribers-llvm-support

@llvm/pr-subscribers-llvm-adt

Author: Matthias Springer (matthias-springer)

Changes

Whether a floating point type supports NaN or infinity can be queried from its semantics. No need to hard-code a list of types.


Full diff: https://github.com/llvm/llvm-project/pull/116158.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/APFloat.h (+4-10)
  • (modified) llvm/lib/Support/APFloat.cpp (+9)
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.

Copy link

github-actions bot commented Nov 14, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@tschuett tschuett requested a review from arsenm November 14, 2024 05:29
@matthias-springer matthias-springer force-pushed the users/matthias-springer/apfloat_nan_or_inf branch from 42160f6 to e6b49cc Compare November 14, 2024 11:42
Copy link
Contributor

@durga4github durga4github left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,

Please wait for the builds to complete and then commit

@matthias-springer matthias-springer merged commit 2f55de4 into main Nov 15, 2024
8 checks passed
@matthias-springer matthias-springer deleted the users/matthias-springer/apfloat_nan_or_inf branch November 15, 2024 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants