[llvm] Fix bitcasting E8M0 APFloat to APInt (PR #113298)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 22 09:11:23 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Sergey Kozub (sergey-kozub)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/113298.diff


2 Files Affected:

- (modified) llvm/lib/Support/APFloat.cpp (+1-1) 
- (modified) llvm/unittests/ADT/APFloatTest.cpp (+9) 


``````````diff
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index a33b6c4a6ddc63..157a6a3c561884 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -3663,7 +3663,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;
   }
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 665cff9c424594..a4d72f2dd369ac 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -7614,6 +7614,15 @@ TEST(APFloatTest, ConvertDoubleToE8M0FNU) {
   EXPECT_EQ(status, APFloat::opUnderflow | APFloat::opInexact);
 }
 
+TEST(APFloatTest, Float8E8M0FNUBitcastToAPInt) {
+  // Regression test for verifying the low bit of the exponent when bitcasting
+  // to integer (zero mantissa).
+  APFloat f0(APFloat::Float8E8M0FNU(), "0.5");
+  APFloat f1(APFloat::Float8E8M0FNU(), "1.0");
+  EXPECT_EQ(f0.bitcastToAPInt(), 126) << f0;
+  EXPECT_EQ(f1.bitcastToAPInt(), 127) << f1;
+}
+
 TEST(APFloatTest, Float6E3M2FNFromString) {
   // Exactly representable
   EXPECT_EQ(28, APFloat(APFloat::Float6E3M2FN(), "28").convertToDouble());

``````````

</details>


https://github.com/llvm/llvm-project/pull/113298


More information about the llvm-commits mailing list