[libc-commits] [libc] [libc][math] Fix -Wc++23-extensions warnings in erfcf16 and erff16. (PR #215613)
via libc-commits
libc-commits at lists.llvm.org
Tue Aug 11 12:53:35 PDT 2026
https://github.com/zhangweize9-cyber updated https://github.com/llvm/llvm-project/pull/215613
>From d5258b39fedb9f523285e16ec1393c21a40c215b Mon Sep 17 00:00:00 2001
From: zhangweize9-cyber <zhangweize9 at gmail.com>
Date: Wed, 12 Aug 2026 00:33:40 +0800
Subject: [PATCH] [libc][math] Fix -Wc++23-extensions warnings in erfcf16 and
erff16.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Change the original logic—which directly returns a fixed value—to use `fputil::cast` for the calculation.
---
libc/src/__support/math/erfcf16.h | 4 ++--
libc/src/__support/math/erff16.h | 7 ++++--
libc/test/src/math/erfcf16_test.cpp | 5 +++++
libc/test/src/math/erff16_test.cpp | 34 +++++++++++++++++++++++++++++
4 files changed, 46 insertions(+), 4 deletions(-)
diff --git a/libc/src/__support/math/erfcf16.h b/libc/src/__support/math/erfcf16.h
index ad096527855d5..2de95420b465b 100644
--- a/libc/src/__support/math/erfcf16.h
+++ b/libc/src/__support/math/erfcf16.h
@@ -93,7 +93,7 @@ LIBC_INLINE float16 erfcf16(float16 x) {
}
if (LIBC_UNLIKELY(x_abs == 0))
- return 1.0f16;
+ return fputil::cast<float16>(1.0f);
// Asymptotic behavior: erfc(x) rounds to 0 or 2 for |x| >= 4.0.
if (LIBC_UNLIKELY(x_abs >= 0x4400U)) { // |x| >= 4.0
@@ -109,7 +109,7 @@ LIBC_INLINE float16 erfcf16(float16 x) {
if (fputil::fenv_is_round_up())
return FPBits::min_subnormal().get_val();
#endif
- return 0.0f16;
+ return fputil::cast<float16>(0.0f);
}
// Polynomial approximation:
diff --git a/libc/src/__support/math/erff16.h b/libc/src/__support/math/erff16.h
index 8e3a92c092e22..508d137a488d6 100644
--- a/libc/src/__support/math/erff16.h
+++ b/libc/src/__support/math/erff16.h
@@ -134,9 +134,11 @@ LIBC_INLINE float16 erff16(float16 x) {
return x;
}
// Inf -> returns 1.0 or -1.0
- return is_neg ? -1.0f16 : 1.0f16;
+ static constexpr float16 ONE_F16 = fputil::cast<float16>(1.0f);
+ return is_neg ? -ONE_F16 : ONE_F16;
}
+ // FIXME: Handle f16 constant emission without triggering -Wc++23-extensions
return fputil::cast<float16>(is_neg ? -1.0f - xf * 0x1.0p-28f
: 1.0f - xf * 0x1.0p-28f);
}
@@ -144,7 +146,8 @@ LIBC_INLINE float16 erff16(float16 x) {
// Polynomial approximation:
// erf(x) ~ x * (c0 + c1 * x^2 + c2 * x^4 + ... + c7 * x^14)
- int idx = static_cast<int>(xbits.abs().get_val() * 8.0f16);
+ static constexpr float16 EIGHT_F16 = fputil::cast<float16>(8.0f);
+ int idx = static_cast<int>(xbits.abs().get_val() * EIGHT_F16);
float xsq = xf * xf;
float x4 = xsq * xsq;
diff --git a/libc/test/src/math/erfcf16_test.cpp b/libc/test/src/math/erfcf16_test.cpp
index d06bca5dc23a6..2047dedd41584 100644
--- a/libc/test/src/math/erfcf16_test.cpp
+++ b/libc/test/src/math/erfcf16_test.cpp
@@ -41,3 +41,8 @@ TEST_F(LlvmLibcErfcf16Test, NegativeRange) {
LIBC_NAMESPACE::erfcf16(x), 0.5);
}
}
+
+TEST_F(LlvmLibcErfcf16Test, NegativeValues) {
+ float16 neg_val = static_cast<float16>(-1.0);
+ EXPECT_TRUE(LlvmLibcErfcf16Test(neg_val) > static_cast<float16>(1.0));
+}
diff --git a/libc/test/src/math/erff16_test.cpp b/libc/test/src/math/erff16_test.cpp
index 522cc85ab15d6..532036f565a55 100644
--- a/libc/test/src/math/erff16_test.cpp
+++ b/libc/test/src/math/erff16_test.cpp
@@ -41,3 +41,37 @@ TEST_F(LlvmLibcErff16Test, NegativeRange) {
LIBC_NAMESPACE::erff16(x), 0.5);
}
}
+
+TEST_F(LlvmLibcErff16Test, NegativeValues) {
+ float16 pos_res = static_cast<float16>(1.0);
+ float16 neg_res = static_cast<float16>(-1.0);
+ EXPECT_FP_EQ(neg_res, -pos_res);
+}
+
+// TODO: Testing whether positive and negative values, as well as 0, are
+// positive or negative.
+TEST_F(LlvmLibcErff16Test, SpecialValues) {
+ // Is the test result 0?
+ EXPECT_FP_EQ(float16(1.0), LlvmLibcErff16Test(static_cast<float16>(0.0)));
+
+ float16 val_compare = static_cast<float16>(1.0);
+
+ // Is the result positive?
+ float16 val_pos = static_cast<float16>(0.125);
+ EXPECT_TRUE(LlvmLibcErff16Test(val_pos) < val_compare);
+
+ // Is the result negative?
+ float16 val_neg = static_cast<float16>(-1.0);
+ EXPECT_TRUE(LlvmLibcErff16Test(val_neg) > val_compare);
+}
+
+// TODO: Test on Properties of Odd Functions.
+TEST_F(LlvmLibcErff16Test, OddFunction) {
+ float16 pos_input = static_cast<float16>(0.5);
+ float16 neg_input = static_cast<float16>(-0.5);
+
+ float16 pos_res = LlvmLibcErff16Test(pos_input);
+ float16 neg_res = LlvmLibcErff16Test(neg_input);
+
+ EXPECT_FP_EQ(neg_res, -pos_res);
+}
More information about the libc-commits
mailing list