Skip to content

Commit 897bad2

Browse files
committed
Fix bitcasting E8M0 APFloat to APInt
1 parent aea60ab commit 897bad2

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

llvm/lib/Support/APFloat.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3663,7 +3663,7 @@ APInt IEEEFloat::convertIEEEFloatToAPInt() const {
36633663
std::array<uint64_t, (S.sizeInBits + 63) / 64> words;
36643664
auto words_iter =
36653665
std::copy_n(mysignificand.begin(), mysignificand.size(), words.begin());
3666-
if constexpr (significand_mask != 0) {
3666+
if constexpr (significand_mask != 0 || trailing_significand_bits == 0) {
36673667
// Clear the integer bit.
36683668
words[mysignificand.size() - 1] &= significand_mask;
36693669
}

llvm/unittests/ADT/APFloatTest.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -7614,6 +7614,15 @@ TEST(APFloatTest, ConvertDoubleToE8M0FNU) {
76147614
EXPECT_EQ(status, APFloat::opUnderflow | APFloat::opInexact);
76157615
}
76167616

7617+
TEST(APFloatTest, Float8E8M0FNUBitcastToAPInt) {
7618+
// Regression test for verifying the low bit of the exponent when bitcasting
7619+
// to integer (zero mantissa).
7620+
APFloat f0(APFloat::Float8E8M0FNU(), "0.5");
7621+
APFloat f1(APFloat::Float8E8M0FNU(), "1.0");
7622+
EXPECT_EQ(f0.bitcastToAPInt(), 126) << f0;
7623+
EXPECT_EQ(f1.bitcastToAPInt(), 127) << f1;
7624+
}
7625+
76177626
TEST(APFloatTest, Float6E3M2FNFromString) {
76187627
// Exactly representable
76197628
EXPECT_EQ(28, APFloat(APFloat::Float6E3M2FN(), "28").convertToDouble());

0 commit comments

Comments
 (0)