[libc-commits] [libc] b654416 - [libc][math] Fix atan2f128 when the exponent difference is larger than the precision. (#183552)

via libc-commits libc-commits at lists.llvm.org
Thu Feb 26 08:30:55 PST 2026


Author: lntue
Date: 2026-02-26T11:30:50-05:00
New Revision: b654416f2ca958545f9c1e619d724fe2fd460d63

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

LOG: [libc][math] Fix atan2f128 when the exponent difference is larger than the precision. (#183552)

Added: 
    

Modified: 
    libc/src/__support/math/atan2f128.h
    libc/test/src/math/smoke/atan2f128_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/math/atan2f128.h b/libc/src/__support/math/atan2f128.h
index b2891bdec4c76..d04cf6f49d4ea 100644
--- a/libc/src/__support/math/atan2f128.h
+++ b/libc/src/__support/math/atan2f128.h
@@ -161,9 +161,11 @@ LIBC_INLINE constexpr float128 atan2f128(float128 y, float128 x) {
   // We have the following bound for normalized n and d:
   //   2^(-exp_
diff  - 1) < n/d < 2^(-exp_
diff  + 1).
   if (LIBC_UNLIKELY(exp_
diff  > FPBits::FRACTION_LEN + 2)) {
+    Float128 quotient = rounded_div(num, den);
+    Float128 result = quick_add(const_term, quotient);
     if (final_sign)
-      const_term.sign = const_term.sign.negate();
-    return static_cast<float128>(const_term);
+      result.sign = result.sign.negate();
+    return static_cast<float128>(result);
   }
 
   // Take 24 leading bits of num and den to convert to float for fast division.

diff  --git a/libc/test/src/math/smoke/atan2f128_test.cpp b/libc/test/src/math/smoke/atan2f128_test.cpp
index 9d539f80bbd79..28b15ae71cac8 100644
--- a/libc/test/src/math/smoke/atan2f128_test.cpp
+++ b/libc/test/src/math/smoke/atan2f128_test.cpp
@@ -25,4 +25,9 @@ TEST_F(LlvmLibcAtan2f128Test, SpecialNumbers) {
   float128 y = 0x1.fffffffffffffffffffffffffff2p1q;
   float128 r = 0x1.921fb54442d18469898cc51701b3p-1q;
   EXPECT_FP_EQ(r, LIBC_NAMESPACE::atan2f128(x, y));
+
+  x = -0x1.f122e07fff556143p+3524q;
+  y = 0x1.f122e07fff55615b75p+6316q;
+  r = -0x1.ffffffffffffffe6cfcdc604fc99p-2793q;
+  EXPECT_FP_EQ(r, LIBC_NAMESPACE::atan2f128(x, y));
 }


        


More information about the libc-commits mailing list