[PATCH] D138540: Fix APFloat::toString on Float8E5M2 values.

Reed Wanderman-Milne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 22 20:18:22 PST 2022


reedwm created this revision.
reedwm added reviewers: stellaraccident, bkramer.
Herald added a subscriber: hiraditya.
Herald added a project: All.
reedwm requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Before, an APInt with value 10 was created, whose width was the significand width. But 10 cannot fit in Float8E5M2's significand.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138540

Files:
  llvm/lib/Support/APFloat.cpp
  llvm/unittests/ADT/APFloatTest.cpp


Index: llvm/unittests/ADT/APFloatTest.cpp
===================================================================
--- llvm/unittests/ADT/APFloatTest.cpp
+++ llvm/unittests/ADT/APFloatTest.cpp
@@ -5152,6 +5152,26 @@
   }
 }
 
+TEST(APFloatTest, F8ToString) {
+  for (APFloat::Semantics S :
+       {APFloat::S_Float8E5M2, APFloat::S_Float8E4M3FN}) {
+    SCOPED_TRACE("Semantics=" + std::to_string(S));
+    for (int i = 0; i < 256; i++) {
+      SCOPED_TRACE("i=" + std::to_string(i));
+      APFloat test(APFloat::Float8E5M2(), APInt(8, i));
+      llvm::SmallString<128> str;
+      test.toString(str);
+
+      if (test.isNaN()) {
+        EXPECT_EQ(str, "NaN");
+      } else {
+        APFloat test2(APFloat::Float8E5M2(), str);
+        EXPECT_TRUE(test.bitwiseIsEqual(test2));
+      }
+    }
+  }
+}
+
 TEST(APFloatTest, IEEEdoubleToDouble) {
   APFloat DPosZero(0.0);
   APFloat DPosZeroToDouble(DPosZero.convertToDouble());
Index: llvm/lib/Support/APFloat.cpp
===================================================================
--- llvm/lib/Support/APFloat.cpp
+++ llvm/lib/Support/APFloat.cpp
@@ -4100,6 +4100,11 @@
 
   // Fill the buffer.
   unsigned precision = significand.getBitWidth();
+  if (precision < 4) {
+    // We need enough precision to store the value 10.
+    precision = 4;
+    significand = significand.zext(precision);
+  }
   APInt ten(precision, 10);
   APInt digit(precision, 0);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138540.477374.patch
Type: text/x-patch
Size: 1413 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221123/428bedf0/attachment.bin>


More information about the llvm-commits mailing list