Skip to content

Commit ad56f62

Browse files
authored
[APFloat][NFC]extract fltSemantics::isRepresentableBy to header (#122636)
isRepresentableBy is useful to check float point type compatibility
1 parent bab7920 commit ad56f62

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

llvm/include/llvm/ADT/APFloat.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ struct APFloatBase {
281281
/// anything real.
282282
static const fltSemantics &Bogus() LLVM_READNONE;
283283

284+
// Returns true if any number described by this semantics can be precisely
285+
// represented by the specified semantics. Does not take into account
286+
// the value of fltNonfiniteBehavior, hasZero, hasSignedRepr.
287+
static bool isRepresentableBy(const fltSemantics &A, const fltSemantics &B);
288+
284289
/// @}
285290

286291
/// IEEE-754R 5.11: Floating Point Comparison Relations.

llvm/lib/Support/APFloat.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,6 @@ struct fltSemantics {
125125

126126
/* Whether this semantics can represent signed values */
127127
bool hasSignedRepr = true;
128-
129-
// Returns true if any number described by this semantics can be precisely
130-
// represented by the specified semantics. Does not take into account
131-
// the value of fltNonfiniteBehavior.
132-
bool isRepresentableBy(const fltSemantics &S) const {
133-
return maxExponent <= S.maxExponent && minExponent >= S.minExponent &&
134-
precision <= S.precision;
135-
}
136128
};
137129

138130
static constexpr fltSemantics semIEEEhalf = {15, -14, 11, 16};
@@ -290,6 +282,12 @@ const fltSemantics &APFloatBase::x87DoubleExtended() {
290282
}
291283
const fltSemantics &APFloatBase::Bogus() { return semBogus; }
292284

285+
bool APFloatBase::isRepresentableBy(const fltSemantics &A,
286+
const fltSemantics &B) {
287+
return A.maxExponent <= B.maxExponent && A.minExponent >= B.minExponent &&
288+
A.precision <= B.precision;
289+
}
290+
293291
constexpr RoundingMode APFloatBase::rmNearestTiesToEven;
294292
constexpr RoundingMode APFloatBase::rmTowardPositive;
295293
constexpr RoundingMode APFloatBase::rmTowardNegative;
@@ -5527,7 +5525,7 @@ APFloat::opStatus APFloat::convertToInteger(APSInt &result,
55275525
double APFloat::convertToDouble() const {
55285526
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEdouble)
55295527
return getIEEE().convertToDouble();
5530-
assert(getSemantics().isRepresentableBy(semIEEEdouble) &&
5528+
assert(isRepresentableBy(getSemantics(), semIEEEdouble) &&
55315529
"Float semantics is not representable by IEEEdouble");
55325530
APFloat Temp = *this;
55335531
bool LosesInfo;
@@ -5541,7 +5539,7 @@ double APFloat::convertToDouble() const {
55415539
float128 APFloat::convertToQuad() const {
55425540
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
55435541
return getIEEE().convertToQuad();
5544-
assert(getSemantics().isRepresentableBy(semIEEEquad) &&
5542+
assert(isRepresentableBy(getSemantics(), semIEEEquad) &&
55455543
"Float semantics is not representable by IEEEquad");
55465544
APFloat Temp = *this;
55475545
bool LosesInfo;
@@ -5555,7 +5553,7 @@ float128 APFloat::convertToQuad() const {
55555553
float APFloat::convertToFloat() const {
55565554
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEsingle)
55575555
return getIEEE().convertToFloat();
5558-
assert(getSemantics().isRepresentableBy(semIEEEsingle) &&
5556+
assert(isRepresentableBy(getSemantics(), semIEEEsingle) &&
55595557
"Float semantics is not representable by IEEEsingle");
55605558
APFloat Temp = *this;
55615559
bool LosesInfo;

0 commit comments

Comments
 (0)