[llvm] [APFloat] Correct upper boundary distances for directed decimal rounding (PR #230774)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 11 20:25:12 PDT 2026


https://github.com/aikhanjum updated https://github.com/llvm/llvm-project/pull/230774

>From 0d103de554ca6ecf8451462829510b8acdf7bf5d Mon Sep 17 00:00:00 2001
From: Aikhan Jumashukurov <117706338+aikhanj at users.noreply.github.com>
Date: Sat, 10 Oct 2026 04:17:22 -0400
Subject: [PATCH 1/2] [APFloat] Correct upper boundary distances for directed
 decimal rounding

An intermediate approximation just below a directed rounding boundary can
be accepted at insufficient precision because ulpsFromBoundary measures
the upper boundary at the full word width rather than the truncated width.
Mask the distance and the multiword upper-boundary comparison so the
existing precision retry handles these ambiguous results.

Add regressions for exact normal single and double values and a subnormal
half value, using both signs and all three directed rounding modes.

Partially addresses #228928. The nearest-rounding inexact status problem
is outside this change.

Assisted-by: OpenAI Codex
---
 llvm/lib/Support/APFloat.cpp       | 11 +++++----
 llvm/unittests/ADT/APFloatTest.cpp | 36 ++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index 3d1932454761f..27212ea5886a5 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -741,9 +741,9 @@ ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits,
   unsigned count = bits / APFloatBase::integerPartWidth;
   unsigned partBits = bits % APFloatBase::integerPartWidth + 1;
 
-  APFloatBase::integerPart part =
-      parts[count] & (~(APFloatBase::integerPart)0 >>
-                      (APFloatBase::integerPartWidth - partBits));
+  APFloatBase::integerPart mask = ~(APFloatBase::integerPart)0 >>
+                                  (APFloatBase::integerPartWidth - partBits);
+  APFloatBase::integerPart part = parts[count] & mask;
 
   APFloatBase::integerPart boundary;
   if (isNearest)
@@ -752,6 +752,9 @@ ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits,
     boundary = 0;
 
   if (count == 0) {
+    // Directed rounding has boundaries at both ends of the truncated bits.
+    if (!isNearest)
+      return std::min(part, -part & mask);
     if (part - boundary <= boundary - part)
       return part - boundary;
     else
@@ -764,7 +767,7 @@ ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits,
         return ~(APFloatBase::integerPart) 0; /* A lot.  */
 
     return parts[0];
-  } else if (part == boundary - 1) {
+  } else if (part == ((boundary - 1) & mask)) {
     while (--count)
       if (~parts[count])
         return ~(APFloatBase::integerPart) 0; /* A lot.  */
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 40a121232fd29..8d94201ecf1c3 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -1393,6 +1393,42 @@ TEST(APFloatTest, fromZeroDecimalLargeExponentString) {
   EXPECT_EQ(0.0,  APFloat(APFloat::IEEEdouble(), StringRef("0e1234" "\0" "2", 6)).convertToDouble());
 }
 
+TEST(APFloatTest, ExactDecimalStringsDirectedRounding) {
+  struct TestCase {
+    const fltSemantics &Sem;
+    const char *Decimal;
+    uint64_t Bits;
+  };
+  const TestCase Tests[] = {
+      {APFloat::IEEEsingle(), "0.100000001490116119384765625", 0x3DCCCCCD},
+      {APFloat::IEEEhalf(), "0.000017225742340087890625", 0x0121},
+      {APFloat::IEEEdouble(),
+       "0.1000000000000000055511151231257827021181583404541015625",
+       0x3FB999999999999A},
+  };
+  // These decimals are exact binary values. An approximation just below the
+  // upper truncation boundary must not be mistaken for a safe rounding result.
+  for (const TestCase &Test : Tests) {
+    for (bool Negative : {false, true}) {
+      APFloat Expected(
+          Test.Sem, APInt(APFloat::semanticsSizeInBits(Test.Sem), Test.Bits));
+      if (Negative)
+        Expected.changeSign();
+      std::string Input = std::string(Negative ? "-" : "") + Test.Decimal;
+      for (auto RM : {APFloat::rmTowardZero, APFloat::rmTowardPositive,
+                      APFloat::rmTowardNegative}) {
+        SCOPED_TRACE(Input);
+        SCOPED_TRACE(static_cast<unsigned>(RM));
+        APFloat Value(Test.Sem);
+        auto Status = Value.convertFromString(Input, RM);
+        ASSERT_TRUE(!!Status);
+        EXPECT_EQ(*Status, APFloat::opOK);
+        EXPECT_TRUE(Value.bitwiseIsEqual(Expected));
+      }
+    }
+  }
+}
+
 TEST(APFloatTest, DecimalStringsUseLargePowersOfFive) {
   // These values are just below one, so they cannot take the early obvious
   // underflow path. The number of fractional digits is the power passed to

>From 02fce67bf0eb3e40c0a0cf0928e391e20778aa78 Mon Sep 17 00:00:00 2001
From: Aikhan Jumashukurov <117706338+aikhanj at users.noreply.github.com>
Date: Sun, 11 Oct 2026 21:06:06 -0400
Subject: [PATCH 2/2] checkpoint: cover inexact directed decimal rounding

Add the near-boundary decimal suggested in PR review, with both signs and all directed modes. Check result bits and opInexact.

Assisted-by: OpenAI Codex
---
 llvm/unittests/ADT/APFloatTest.cpp | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 8d94201ecf1c3..dc499ea41f516 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -1429,6 +1429,30 @@ TEST(APFloatTest, ExactDecimalStringsDirectedRounding) {
   }
 }
 
+TEST(APFloatTest, InexactDecimalStringsDirectedRounding) {
+  // This decimal is just above 0x4792EDAAA478AD1E. An approximation on the
+  // other side of that boundary must not be accepted for directed rounding.
+  for (bool Negative : {false, true}) {
+    std::string Input = std::string(Negative ? "-" : "") + "629003483e28";
+    for (auto RM : {APFloat::rmTowardZero, APFloat::rmTowardPositive,
+                    APFloat::rmTowardNegative}) {
+      SCOPED_TRACE(Input);
+      SCOPED_TRACE(static_cast<unsigned>(RM));
+      bool RoundAway = Negative ? RM == APFloat::rmTowardNegative
+                                : RM == APFloat::rmTowardPositive;
+      APFloat Expected(APFloat::IEEEdouble(),
+                       APInt(64, 0x4792EDAAA478AD1E + RoundAway));
+      if (Negative)
+        Expected.changeSign();
+      APFloat Value(APFloat::IEEEdouble());
+      auto Status = Value.convertFromString(Input, RM);
+      ASSERT_TRUE(!!Status);
+      EXPECT_EQ(*Status, APFloat::opInexact);
+      EXPECT_TRUE(Value.bitwiseIsEqual(Expected));
+    }
+  }
+}
+
 TEST(APFloatTest, DecimalStringsUseLargePowersOfFive) {
   // These values are just below one, so they cannot take the early obvious
   // underflow path. The number of fractional digits is the power passed to



More information about the llvm-commits mailing list