diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp index a33b6c4a6ddc63..5cb6de9bd847e6 100644 --- a/llvm/lib/Support/APFloat.cpp +++ b/llvm/lib/Support/APFloat.cpp @@ -408,7 +408,8 @@ exponentNaN(const fltSemantics &semantics) { if (semantics.nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { if (semantics.nanEncoding == fltNanEncoding::NegativeZero) return exponentZero(semantics); - return semantics.maxExponent; + if (semantics.hasSignedRepr) + return semantics.maxExponent; } return semantics.maxExponent + 1; } @@ -3663,7 +3664,7 @@ APInt IEEEFloat::convertIEEEFloatToAPInt() const { std::array words; auto words_iter = std::copy_n(mysignificand.begin(), mysignificand.size(), words.begin()); - if constexpr (significand_mask != 0) { + if constexpr (significand_mask != 0 || trailing_significand_bits == 0) { // Clear the integer bit. words[mysignificand.size() - 1] &= significand_mask; } diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp index 665cff9c424594..74aaf66973a19f 100644 --- a/llvm/unittests/ADT/APFloatTest.cpp +++ b/llvm/unittests/ADT/APFloatTest.cpp @@ -5985,6 +5985,9 @@ TEST(APFloatTest, Float8E8M0FNUExhaustive) { APFloat test(APFloat::Float8E8M0FNU(), APInt(8, i)); SCOPED_TRACE("i=" + std::to_string(i)); + // bitcastToAPInt + EXPECT_EQ(i, test.bitcastToAPInt()); + // isLargest if (i == 254) { EXPECT_TRUE(test.isLargest());