[libc-commits] [libc] [libc][math] Fix signed zeros for powf when underflow happens. (PR #112601)

via libc-commits libc-commits at lists.llvm.org
Wed Oct 16 12:14:13 PDT 2024


https://github.com/lntue created https://github.com/llvm/llvm-project/pull/112601

None

>From bed5228abaaf30ce238683e3547109ddfbe10750 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Wed, 16 Oct 2024 18:59:00 +0000
Subject: [PATCH] [libc][math] Fix signed zeros for powf when underflow
 happens.

---
 libc/src/math/generic/powf.cpp         | 8 +++++---
 libc/test/src/math/smoke/powf_test.cpp | 2 ++
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/libc/src/math/generic/powf.cpp b/libc/src/math/generic/powf.cpp
index 8ce2465ba229cb..96a977c466f90f 100644
--- a/libc/src/math/generic/powf.cpp
+++ b/libc/src/math/generic/powf.cpp
@@ -855,9 +855,11 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
           : 0.0;
   exp2_hi_mid_dd.hi = exp2_hi_mid;
 
-  return static_cast<float>(
-             powf_double_double(idx_x, dx, y6, lo6_hi, exp2_hi_mid_dd)) +
-         0.0f;
+  double r_dd = powf_double_double(idx_x, dx, y6, lo6_hi, exp2_hi_mid_dd);
+  float r_f = static_cast<float>(r_dd);
+
+  // Only fix signed zeros for exact zeros results.
+  return r_dd != 0 ? r_f : r_f + 0.0f;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/smoke/powf_test.cpp b/libc/test/src/math/smoke/powf_test.cpp
index bd4f98e30fbdc7..6adb9e9feb2278 100644
--- a/libc/test/src/math/smoke/powf_test.cpp
+++ b/libc/test/src/math/smoke/powf_test.cpp
@@ -190,4 +190,6 @@ TEST_F(LlvmLibcPowfTest, SpecialNumbers) {
                                   FE_UNDERFLOW);
     }
   }
+
+  EXPECT_FP_EQ(-0.0f, LIBC_NAMESPACE::powf(-0.015625f, 25.0f));
 }



More information about the libc-commits mailing list