-
Notifications
You must be signed in to change notification settings - Fork 12k
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
: Add helpers to query NaN/inf semantics
#116315
Conversation
✅ With the latest revision this PR passed the C/C++ code formatter. |
1668bd7
to
da41f0f
Compare
da41f0f
to
7af3864
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.
The latest revision looks good to me.
Please wait for the builds to complete before merging
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/35/builds/3700 Here is the relevant piece of the build log for the reference
|
@llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-llvm-adt Author: Matthias Springer (matthias-springer) Changes
Full diff: https://github.com/llvm/llvm-project/pull/116315.diff 3 Files Affected:
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index 4b5a85945ec17c..4ca928bf4f49e3 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -311,7 +311,8 @@ struct APFloatBase {
static unsigned int semanticsIntSizeInBits(const fltSemantics&, bool);
static bool semanticsHasZero(const fltSemantics &);
static bool semanticsHasSignedRepr(const fltSemantics &);
- static bool semanticsHasNanOrInf(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.
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index 58bf002e0fed2e..81e297c3ab033e 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -375,7 +375,11 @@ bool APFloatBase::semanticsHasSignedRepr(const fltSemantics &semantics) {
return semantics.hasSignedRepr;
}
-bool APFloatBase::semanticsHasNanOrInf(const fltSemantics &semantics) {
+bool APFloatBase::semanticsHasInf(const fltSemantics &semantics) {
+ return semantics.nonFiniteBehavior == fltNonfiniteBehavior::IEEE754;
+}
+
+bool APFloatBase::semanticsHasNaN(const fltSemantics &semantics) {
return semantics.nonFiniteBehavior != fltNonfiniteBehavior::FiniteOnly;
}
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index ab9db8ed0fa85c..f291c814886d35 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -832,8 +832,9 @@ TEST(APFloatTest, IsSmallestNormalized) {
EXPECT_FALSE(APFloat::getZero(Semantics, false).isSmallestNormalized());
EXPECT_FALSE(APFloat::getZero(Semantics, true).isSmallestNormalized());
- if (APFloat::semanticsHasNanOrInf(Semantics)) {
+ if (APFloat::semanticsHasNaN(Semantics)) {
// Types that do not support Inf will return NaN when asked for Inf.
+ // (But only if they support NaN.)
EXPECT_FALSE(APFloat::getInf(Semantics, false).isSmallestNormalized());
EXPECT_FALSE(APFloat::getInf(Semantics, true).isSmallestNormalized());
@@ -2557,6 +2558,14 @@ TEST(APFloatTest, isInfinity) {
EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isInfinity());
EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isInfinity());
EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isInfinity());
+
+ for (unsigned I = 0; I != APFloat::S_MaxSemantics + 1; ++I) {
+ const fltSemantics &Semantics =
+ APFloat::EnumToSemantics(static_cast<APFloat::Semantics>(I));
+ if (APFloat::semanticsHasInf(Semantics)) {
+ EXPECT_TRUE(APFloat::getInf(Semantics).isInfinity());
+ }
+ }
}
TEST(APFloatTest, isNaN) {
@@ -2567,6 +2576,14 @@ TEST(APFloatTest, isNaN) {
EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNaN());
EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNaN());
EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isNaN());
+
+ for (unsigned I = 0; I != APFloat::S_MaxSemantics + 1; ++I) {
+ const fltSemantics &Semantics =
+ APFloat::EnumToSemantics(static_cast<APFloat::Semantics>(I));
+ if (APFloat::semanticsHasNaN(Semantics)) {
+ EXPECT_TRUE(APFloat::getNaN(Semantics).isNaN());
+ }
+ }
}
TEST(APFloatTest, isFiniteNonZero) {
@@ -7345,8 +7362,9 @@ TEST(APFloatTest, getExactLog2) {
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, false).getExactLog2Abs());
EXPECT_EQ(INT_MIN, APFloat::getZero(Semantics, true).getExactLog2Abs());
- if (APFloat::semanticsHasNanOrInf(Semantics)) {
+ if (APFloat::semanticsHasNaN(Semantics)) {
// Types that do not support Inf will return NaN when asked for Inf.
+ // (But only if they support NaN.)
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());
|
APFloat
changes extracted from #116176 as per reviewer comments.