[libc-commits] [libc] 1a9a933 - [libc][math] Fix `quick_add` incorrect exponent (#220374)

via libc-commits libc-commits at lists.llvm.org
Thu Sep 3 00:34:22 PDT 2026


Author: Zorojuro
Date: 2026-09-03T13:04:17+05:30
New Revision: 1a9a9331e4b8015cbabec9f553af32ddc6441192

URL: https://github.com/llvm/llvm-project/commit/1a9a9331e4b8015cbabec9f553af32ddc6441192
DIFF: https://github.com/llvm/llvm-project/commit/1a9a9331e4b8015cbabec9f553af32ddc6441192.diff

LOG: [libc][math] Fix `quick_add`  incorrect exponent (#220374)

Fixes #220140

Failure from the test added 
```sh
 FAILURE
Failed to match double(c1) against LIBC_NAMESPACE::testing::getMatcher< LIBC_NAMESPACE::testing::TestCond::EQ>(double(a) + double(b)).
Expected floating point value: 0x47F0000000000000 = (S: 0, E: 0x047F, M: 0x0000000000000000)
Actual floating point value: 0x3FF0000000000000 = (S: 0, E: 0x03FF, M: 0x0000000000000000)
```

Added: 
    

Modified: 
    libc/src/__support/FPUtil/dyadic_float.h
    libc/test/src/__support/FPUtil/dyadic_float_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index e32906e3cf9cf..b86caa29f688b 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -139,25 +139,21 @@ template <size_t Bits> struct DyadicFloat {
 
   // Used for aligning exponents.  Output might not be normalized.
   LIBC_INLINE constexpr DyadicFloat &shift_left(unsigned shift_length) {
-    if (shift_length < Bits) {
-      exponent -= static_cast<int>(shift_length);
+    exponent -= static_cast<int>(shift_length);
+    if (shift_length < Bits)
       mantissa <<= shift_length;
-    } else {
-      exponent = 0;
+    else
       mantissa = MantissaType(0);
-    }
     return *this;
   }
 
   // Used for aligning exponents.  Output might not be normalized.
   LIBC_INLINE constexpr DyadicFloat &shift_right(unsigned shift_length) {
-    if (shift_length < Bits) {
-      exponent += static_cast<int>(shift_length);
+    exponent += static_cast<int>(shift_length);
+    if (shift_length < Bits)
       mantissa >>= shift_length;
-    } else {
-      exponent = 0;
+    else
       mantissa = MantissaType(0);
-    }
     return *this;
   }
 

diff  --git a/libc/test/src/__support/FPUtil/dyadic_float_test.cpp b/libc/test/src/__support/FPUtil/dyadic_float_test.cpp
index 720b426033dff..3575f9694ab7f 100644
--- a/libc/test/src/__support/FPUtil/dyadic_float_test.cpp
+++ b/libc/test/src/__support/FPUtil/dyadic_float_test.cpp
@@ -44,6 +44,18 @@ TEST(LlvmLibcDyadicFloatTest, QuickAdd) {
 
   Float192 z = quick_add(x, y);
   EXPECT_FP_EQ_ALL_ROUNDING(double(x) + double(y), double(z));
+
+  DFloat128 a(0x1.0p0);
+  ASSERT_FP_EQ(0x1.0p0, double(a));
+
+  DFloat128 b(0x1.0p128);
+  ASSERT_FP_EQ(0x1.0p128, double(b));
+
+  DFloat128 c1 = quick_add(a, b);
+  EXPECT_FP_EQ(double(a) + double(b), double(c1));
+
+  DFloat128 c2 = quick_add(b, a);
+  EXPECT_FP_EQ(double(b) + double(a), double(c2));
 }
 
 TEST(LlvmLibcDyadicFloatTest, QuickMul) {


        


More information about the libc-commits mailing list