[llvm] [mlir] [APFloat] Report the sign and the zero a conversion cannot represent (PR #216056)
Matthias Springer via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 15 01:29:59 PDT 2026
================
@@ -2334,6 +2334,59 @@ TEST(APFloatTest, Float8E8M0FNUNaNConvert) {
}
}
+// Test that converting into a format that cannot represent the sign, or that
+// has no encoding for zero, reports the loss. Callers decide whether to keep
+// the result from losesInfo, and the value that comes out is not the value
+// that went in: the sign bit has nowhere to go, and zero is replaced by the
+// smallest normalized value.
+TEST(APFloatTest, ConvertLosesUnrepresentableSignAndZero) {
+ // Neither format has a sign; only Float8E8M0FNU also lacks a zero.
+ const fltSemantics *NoSignSemantics[] = {&APFloat::Float8E8M0FNU(),
+ &APFloat::Float8E5M3FNU()};
+
+ for (const fltSemantics *Sem : NoSignSemantics) {
+ for (double Value : {-2.0, -0.0}) {
+ APFloat test(Value);
+ bool losesInfo = false;
+ APFloat::opStatus status =
+ test.convert(*Sem, APFloat::rmNearestTiesToEven, &losesInfo);
+ EXPECT_TRUE(losesInfo);
+ EXPECT_EQ(status, APFloat::opInexact);
+ }
+
+ // The magnitude alone still converts exactly.
+ APFloat test(2.0);
+ bool losesInfo = true;
+ APFloat::opStatus status =
+ test.convert(*Sem, APFloat::rmNearestTiesToEven, &losesInfo);
+ EXPECT_FALSE(losesInfo);
----------------
matthias-springer wrote:
Same here.
https://github.com/llvm/llvm-project/pull/216056
More information about the llvm-commits
mailing list