-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[APFloat][NFC]extract fltSemantics::isRepresentableBy
to header
#122636
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
[APFloat][NFC]extract fltSemantics::isRepresentableBy
to header
#122636
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
@llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-llvm-adt Author: Congcong Cai (HerrCai0907) ChangesisRepresentableBy is useful to check float point type compatibility Full diff: https://github.com/llvm/llvm-project/pull/122636.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index bf80fa5a06580b..528ba391bfc1bc 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -281,6 +281,11 @@ struct APFloatBase {
/// anything real.
static const fltSemantics &Bogus() LLVM_READNONE;
+ // Returns true if any number described by this semantics can be precisely
+ // represented by the specified semantics. Does not take into account
+ // the value of fltNonfiniteBehavior.
+ static bool isRepresentableBy(const fltSemantics &A, const fltSemantics &B);
+
/// @}
/// IEEE-754R 5.11: Floating Point Comparison Relations.
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index c9adfca8b3b768..b0d92ae37fe8f6 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -125,14 +125,6 @@ struct fltSemantics {
/* Whether this semantics can represent signed values */
bool hasSignedRepr = true;
-
- // Returns true if any number described by this semantics can be precisely
- // represented by the specified semantics. Does not take into account
- // the value of fltNonfiniteBehavior.
- bool isRepresentableBy(const fltSemantics &S) const {
- return maxExponent <= S.maxExponent && minExponent >= S.minExponent &&
- precision <= S.precision;
- }
};
static constexpr fltSemantics semIEEEhalf = {15, -14, 11, 16};
@@ -290,6 +282,12 @@ const fltSemantics &APFloatBase::x87DoubleExtended() {
}
const fltSemantics &APFloatBase::Bogus() { return semBogus; }
+bool APFloatBase::isRepresentableBy(const fltSemantics &A,
+ const fltSemantics &B) {
+ return A.maxExponent <= B.maxExponent && A.minExponent >= B.minExponent &&
+ A.precision <= B.precision;
+}
+
constexpr RoundingMode APFloatBase::rmNearestTiesToEven;
constexpr RoundingMode APFloatBase::rmTowardPositive;
constexpr RoundingMode APFloatBase::rmTowardNegative;
@@ -5527,7 +5525,7 @@ APFloat::opStatus APFloat::convertToInteger(APSInt &result,
double APFloat::convertToDouble() const {
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEdouble)
return getIEEE().convertToDouble();
- assert(getSemantics().isRepresentableBy(semIEEEdouble) &&
+ assert(isRepresentableBy(getSemantics(), semIEEEdouble) &&
"Float semantics is not representable by IEEEdouble");
APFloat Temp = *this;
bool LosesInfo;
@@ -5541,7 +5539,7 @@ double APFloat::convertToDouble() const {
float128 APFloat::convertToQuad() const {
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
return getIEEE().convertToQuad();
- assert(getSemantics().isRepresentableBy(semIEEEquad) &&
+ assert(isRepresentableBy(getSemantics(), semIEEEquad) &&
"Float semantics is not representable by IEEEquad");
APFloat Temp = *this;
bool LosesInfo;
@@ -5555,7 +5553,7 @@ float128 APFloat::convertToQuad() const {
float APFloat::convertToFloat() const {
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEsingle)
return getIEEE().convertToFloat();
- assert(getSemantics().isRepresentableBy(semIEEEsingle) &&
+ assert(isRepresentableBy(getSemantics(), semIEEEsingle) &&
"Float semantics is not representable by IEEEsingle");
APFloat Temp = *this;
bool LosesInfo;
|
@matthias-springer could you confirm if this is compatible with https://discourse.llvm.org/t/rethink-on-approach-to-low-precision-fp-types/82361 . IIUC, it should be. |
bool APFloatBase::isRepresentableBy(const fltSemantics &A, | ||
const fltSemantics &B) { | ||
return A.maxExponent <= B.maxExponent && A.minExponent >= B.minExponent && | ||
A.precision <= B.precision; |
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.
I think this should take into account hasZero
and hasSignedRepr
. Or the documentation should make clear that it doesn't.
// Returns true if any number described by this semantics can be precisely | ||
// represented by the specified semantics. Does not take into account | ||
// the value of fltNonfiniteBehavior. | ||
static bool isRepresentableBy(const fltSemantics &A, const fltSemantics &B); |
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.
What's your use case for this function? Given that this function does not take into account fltNonfiniteBehavior
, hasZero
, hasSignedRepr
, I'm wondering how useful it is in practice. (I'm also wondering, why it does not take into account fltNonfiniteBehavior
.)
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.
For the usecase of this functions. I want to warn for implicit loss of accuracy when floating point conversation happened in compiler frontend type systems.
It needs API to detect floating point type compatibility.
(I'm also wondering, why it does not take into account fltNonfiniteBehavior.)
The original usage of this function is in APFloat::convertTo*
series function to check compatibility.
At least for these cases, I think it only needs to check the floating point number compatibility in normal range instead to consider NaN
and Inf
.
Since this patch more like a refactor, I don't want to change the meaning of this API. Actually I try to add these requirements and test break.
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.
As an NFC change, this looks good to me.
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.
If we are changing the semantics of this, can we do that as a separate PR?
I don't see any problem, either. |
43cbcd2
to
ded43fc
Compare
fltSemantics::isRepresentableBy
to headerfltSemantics::isRepresentableBy
to header
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 for the NFC change
isRepresentableBy is useful to check float point type compatibility
ded43fc
to
1d0bb33
Compare
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/16/builds/11922 Here is the relevant piece of the build log for the reference
|
isRepresentableBy is useful to check float point type compatibility