diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h index bf80fa5a06580..9792749230cbf 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, hasZero, hasSignedRepr. + 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 c9adfca8b3b76..b0d92ae37fe8f 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;