-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
[llvm] APFloat
: Query hasNanOrInf
from semantics
#116158
Conversation
@llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-llvm-adt Author: Matthias Springer (matthias-springer) ChangesWhether 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:
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.
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
42160f6
to
e6b49cc
Compare
There was a problem hiding this 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
Whether a floating point type supports NaN or infinity can be queried from its semantics. No need to hard-code a list of types.