[llvm] 7476d59 - Fix APFloat::toString on Float8E5M2 values.
Benjamin Kramer via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 13 00:52:15 PST 2022
Author: Reed
Date: 2022-12-13T09:52:07+01:00
New Revision: 7476d59414d3f94b6e14e6917a6c1151135a28c1
URL: https://github.com/llvm/llvm-project/commit/7476d59414d3f94b6e14e6917a6c1151135a28c1
DIFF: https://github.com/llvm/llvm-project/commit/7476d59414d3f94b6e14e6917a6c1151135a28c1.diff
LOG: Fix APFloat::toString on Float8E5M2 values.
Before, an APInt with value 10 was created, whose width was the significand width. But 10 cannot fit in Float8E5M2's significand.
Differential Revision: https://reviews.llvm.org/D138540
Added:
Modified:
llvm/lib/Support/APFloat.cpp
llvm/unittests/ADT/APFloatTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index 00e863ae091d4..587521d55c656 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -4099,6 +4099,11 @@ void IEEEFloat::toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision,
// 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);
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index bb0f6713abb4b..468b936cfd34d 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -5152,6 +5152,26 @@ TEST(APFloatTest, Float8E4M3FNExhaustivePair) {
}
}
+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());
More information about the llvm-commits
mailing list