[llvm] [APFloat][NFC] Add unit test coverage for `getArbitraryFPSemantics` (PR #216501)
Chinmay Deshpande via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 15 11:41:11 PDT 2026
https://github.com/chinmaydd created https://github.com/llvm/llvm-project/pull/216501
`isValidArbitraryFPFormat` and `getArbitraryFPFormatSizeInBits` have unit tests, but `getArbitraryFPSemantics`, the mapping the conversion intrinsics actually lower through, had none. Cover the formats it supports, the valid formats it does not support yet, and invalid format strings, and check that the two tables agree on the size of every format with lowerable semantics.
Also add the missing Float8E5M3FNU case to the `getArbitraryFPFormatSizeInBits` test.
AI disclosure: This PR was generated with the assistance of AI, and I have reviewed it.
>From a479bce38b6f5dba0acd38c6b23ffd18ce1c667b Mon Sep 17 00:00:00 2001
From: Chinmay Deshpande <chdeshpa at amd.com>
Date: Sat, 15 Aug 2026 14:32:28 -0400
Subject: [PATCH] [APFloat][NFC] Add unit test coverage for
getArbitraryFPSemantics
isValidArbitraryFPFormat and getArbitraryFPFormatSizeInBits have unit
tests, but getArbitraryFPSemantics, the mapping the conversion
intrinsics actually lower through, had none. Cover the formats it
supports, the valid formats it does not support yet, and invalid format
strings, and check that the two tables agree on the size of every
format with lowerable semantics.
Also add the missing Float8E5M3FNU case to the
getArbitraryFPFormatSizeInBits test.
Change-Id: I8fa9580ca6f03fd5e7b1de5354b71283cebb0f95
---
llvm/unittests/ADT/APFloatTest.cpp | 45 ++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 4ef2814e1a7b3..0c5103c4b49e4 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -10765,6 +10765,7 @@ TEST(APFloatTest, getArbitraryFPFormatSizeInBits) {
EXPECT_EQ(8u, APFloat::getArbitraryFPFormatSizeInBits("Float8E4M3B11FNUZ"));
EXPECT_EQ(8u, APFloat::getArbitraryFPFormatSizeInBits("Float8E3M4"));
EXPECT_EQ(8u, APFloat::getArbitraryFPFormatSizeInBits("Float8E8M0FNU"));
+ EXPECT_EQ(8u, APFloat::getArbitraryFPFormatSizeInBits("Float8E5M3FNU"));
EXPECT_EQ(6u, APFloat::getArbitraryFPFormatSizeInBits("Float6E3M2FN"));
EXPECT_EQ(6u, APFloat::getArbitraryFPFormatSizeInBits("Float6E2M3FN"));
EXPECT_EQ(4u, APFloat::getArbitraryFPFormatSizeInBits("Float4E2M1FN"));
@@ -10776,6 +10777,50 @@ TEST(APFloatTest, getArbitraryFPFormatSizeInBits) {
EXPECT_EQ(0u, APFloat::getArbitraryFPFormatSizeInBits("unknown"));
}
+TEST(APFloatTest, getArbitraryFPSemantics) {
+ // Formats that can currently be lowered map to their semantics.
+ EXPECT_EQ(&APFloat::Float8E5M2(),
+ APFloat::getArbitraryFPSemantics("Float8E5M2"));
+ EXPECT_EQ(&APFloat::Float8E4M3FN(),
+ APFloat::getArbitraryFPSemantics("Float8E4M3FN"));
+ EXPECT_EQ(&APFloat::Float6E3M2FN(),
+ APFloat::getArbitraryFPSemantics("Float6E3M2FN"));
+ EXPECT_EQ(&APFloat::Float6E2M3FN(),
+ APFloat::getArbitraryFPSemantics("Float6E2M3FN"));
+ EXPECT_EQ(&APFloat::Float4E2M1FN(),
+ APFloat::getArbitraryFPSemantics("Float4E2M1FN"));
+
+ // Formats that are valid but cannot be lowered yet report no semantics.
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("Float8E5M2FNUZ"));
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("Float8E4M3"));
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("Float8E4M3FNUZ"));
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("Float8E4M3B11FNUZ"));
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("Float8E3M4"));
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("Float8E8M0FNU"));
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("Float8E5M3FNU"));
+
+ // Invalid formats report no semantics.
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics(""));
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("Float8"));
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("float8e5m2"));
+ EXPECT_EQ(nullptr, APFloat::getArbitraryFPSemantics("unknown"));
+}
+
+// The two arbitrary FP format tables must agree: every format with lowerable
+// semantics reports the size of those semantics.
+TEST(APFloatTest, ArbitraryFPSemanticsMatchSizeInBits) {
+ for (StringRef Format :
+ {"Float8E5M2", "Float8E5M2FNUZ", "Float8E4M3", "Float8E4M3FN",
+ "Float8E4M3FNUZ", "Float8E4M3B11FNUZ", "Float8E3M4", "Float8E8M0FNU",
+ "Float8E5M3FNU", "Float6E3M2FN", "Float6E2M3FN", "Float4E2M1FN"}) {
+ ASSERT_TRUE(APFloat::isValidArbitraryFPFormat(Format)) << Format;
+ if (const fltSemantics *Sem = APFloat::getArbitraryFPSemantics(Format))
+ EXPECT_EQ(APFloat::getSizeInBits(*Sem),
+ APFloat::getArbitraryFPFormatSizeInBits(Format))
+ << Format;
+ }
+}
+
TEST(APFloatTest, DecimalStringPreservesInexactStatus) {
APFloat F(APFloat::IEEEsingle());
More information about the llvm-commits
mailing list