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

Sergey Kozub via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 22 09:59:04 PDT 2024


https://github.com/sergey-kozub updated https://github.com/llvm/llvm-project/pull/113298

>From f276e39a83f3b5ea017bb6013d2cd537d9705a1c Mon Sep 17 00:00:00 2001
From: Sergey Kozub <skozub at nvidia.com>
Date: Tue, 22 Oct 2024 13:24:28 +0200
Subject: [PATCH] Fix bitcasting E8M0 APFloat to APInt

---
 llvm/lib/Support/APFloat.cpp       | 5 +++--
 llvm/unittests/ADT/APFloatTest.cpp | 3 +++
 2 files changed, 6 insertions(+), 2 deletions(-)

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<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..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());



More information about the llvm-commits mailing list