Skip to content

Commit

Permalink
Fix bitcasting E8M0 APFloat to APInt (#113298)
Browse files Browse the repository at this point in the history
Fixes a bug in APFloat handling of E8M0 type (zero mantissa).

Related PRs:
- #107127
- #111028
  • Loading branch information
sergey-kozub authored Oct 22, 2024
1 parent 2fdf49d commit 7b308b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions llvm/lib/Support/APFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -3663,7 +3664,7 @@ APInt IEEEFloat::convertIEEEFloatToAPInt() const {
std::array<uint64_t, (S.sizeInBits + 63) / 64> 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;
}
Expand Down
3 changes: 3 additions & 0 deletions llvm/unittests/ADT/APFloatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 7b308b1

Please sign in to comment.