[libc-commits] [libc] [libc][math] Fix log1p SEGV with large inputs when FTZ/DAZ flags are set. (PR #115541)

via libc-commits libc-commits at lists.llvm.org
Fri Nov 8 12:27:55 PST 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff bbcd35270ef4597402b924d547d845893e7fd165 a3b6177c3f2197ac5cf24d566ea77ca56476d3de --extensions cpp -- libc/src/math/generic/log1p.cpp libc/test/src/math/smoke/log1p_test.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libc/src/math/generic/log1p.cpp b/libc/src/math/generic/log1p.cpp
index ec1f2bc00f..38f3bed05d 100644
--- a/libc/src/math/generic/log1p.cpp
+++ b/libc/src/math/generic/log1p.cpp
@@ -823,7 +823,7 @@ constexpr Float128 BIG_COEFFS[4]{
 };
 
 [[maybe_unused]] LIBC_INLINE double log1p_accurate(int e_x, int index,
-                                  fputil::DoubleDouble m_x) {
+                                                   fputil::DoubleDouble m_x) {
   Float128 e_x_f128(static_cast<float>(e_x));
   Float128 sum = fputil::quick_mul(LOG_2, e_x_f128);
   sum = fputil::quick_add(sum, LOG_R1[index]);
@@ -982,8 +982,10 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
   //   |m_dd.lo| < 2^-52.
   // This is exact.
   uint64_t m_hi = static_cast<uint64_t>(static_cast<int64_t>(x_u) - s_u);
-  uint64_t m_lo = (x_dd.lo != 0.0) ? FPBits_t(x_dd.lo).uintval() :
-                  static_cast<uint64_t>(cpp::bit_cast<int64_t>(x_dd.lo) - s_u);
+  uint64_t m_lo =
+      (x_dd.lo != 0.0)
+          ? FPBits_t(x_dd.lo).uintval()
+          : static_cast<uint64_t>(cpp::bit_cast<int64_t>(x_dd.lo) - s_u);
   fputil::DoubleDouble m_dd{FPBits_t(m_lo).get_val(), FPBits_t(m_hi).get_val()};
 
   // Perform range reduction:
diff --git a/libc/test/src/math/smoke/log1p_test.cpp b/libc/test/src/math/smoke/log1p_test.cpp
index 0d7b5ac80b..b98c0f26a8 100644
--- a/libc/test/src/math/smoke/log1p_test.cpp
+++ b/libc/test/src/math/smoke/log1p_test.cpp
@@ -25,7 +25,8 @@ TEST_F(LlvmLibcLog1pTest, SpecialNumbers) {
   EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::log1p(-1.0),
                               FE_DIVBYZERO);
 
-  EXPECT_FP_EQ(0x1.62c829bf8fd9dp9, LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
+  EXPECT_FP_EQ(0x1.62c829bf8fd9dp9,
+               LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
 }
 
 #ifdef LIBC_TEST_FTZ_DAZ
@@ -36,21 +37,24 @@ TEST_F(LlvmLibcLog1pTest, FTZMode) {
   ModifyMXCSR mxcsr(FTZ);
 
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::log1p(min_denormal));
-  EXPECT_FP_EQ(0x1.62c829bf8fd9dp9, LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
+  EXPECT_FP_EQ(0x1.62c829bf8fd9dp9,
+               LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
 }
 
 TEST_F(LlvmLibcLog1pTest, DAZMode) {
   ModifyMXCSR mxcsr(DAZ);
 
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::log1p(min_denormal));
-  EXPECT_FP_EQ(0x1.62c829bf8fd9dp9, LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
+  EXPECT_FP_EQ(0x1.62c829bf8fd9dp9,
+               LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
 }
 
 TEST_F(LlvmLibcLog1pTest, FTZDAZMode) {
   ModifyMXCSR mxcsr(FTZ | DAZ);
 
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::log1p(min_denormal));
-  EXPECT_FP_EQ(0x1.62c829bf8fd9dp9, LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
+  EXPECT_FP_EQ(0x1.62c829bf8fd9dp9,
+               LIBC_NAMESPACE::log1p(0x1.9b536cac3a09dp1023));
 }
 
 #endif

``````````

</details>


https://github.com/llvm/llvm-project/pull/115541


More information about the libc-commits mailing list