[libc-commits] [libc] [libc][math] Fix signed zeros for powf when underflow happens. (PR #112601)
via libc-commits
libc-commits at lists.llvm.org
Thu Oct 17 21:03:07 PDT 2024
https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/112601
>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 1/2] [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));
}
>From 7b34e70ef40f9c901ce0241c96cb139dc081dad7 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Fri, 18 Oct 2024 04:02:44 +0000
Subject: [PATCH 2/2] Address comments.
---
libc/src/math/generic/powf.cpp | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/libc/src/math/generic/powf.cpp b/libc/src/math/generic/powf.cpp
index 96a977c466f90f..83477c6ef2aceb 100644
--- a/libc/src/math/generic/powf.cpp
+++ b/libc/src/math/generic/powf.cpp
@@ -856,10 +856,8 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
exp2_hi_mid_dd.hi = exp2_hi_mid;
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;
+ return static_cast<float>(r_dd);
}
} // namespace LIBC_NAMESPACE_DECL
More information about the libc-commits
mailing list