[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 09:58:07 PDT 2026
https://github.com/zhangweize9-cyber created https://github.com/llvm/llvm-project/pull/215613
Change the original logic—which directly returns a fixed value—to use `fputil::cast` for the calculation.
## Problem Description
I encountered this issue while debugging the `llvm-mc` module. When I running `ninja -C build llvm-mc`, During compilation, the terminal displayed the warning `-Wc++23-extensions warnings`.
## Solution
I replaced the original code, which looked something like this, with the following method:
```h
// before
return 1.0f16;
// after
return fputil::cast<float16>(1.0f);
```
The conditional expression uses `static expr float16` to store the result in a variable for processing.
## Test case
I added two additional tests to the file `libc/test/src/__support/math_extras_test.cpp`—one to test cases greater than 2.0 and another to test cases less than 1.0. I ran the tests using `ninja -C build llvm-lit`.
>From 60a37d809ac9db25c5e88562b6baadf41a7828c3 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/__support/math_extras_test.cpp | 20 ++++++++++++++++++++
3 files changed, 27 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/__support/math_extras_test.cpp b/libc/test/src/__support/math_extras_test.cpp
index f5d4dae371827..d04b0447bc379 100644
--- a/libc/test/src/__support/math_extras_test.cpp
+++ b/libc/test/src/__support/math_extras_test.cpp
@@ -8,6 +8,8 @@
#include "src/__support/integer_literals.h"
#include "src/__support/macros/config.h"
+#include "src/__support/math/erfcf16.h"
+#include "src/__support/math/erff16.h"
#include "src/__support/math_extras.h"
#include "src/__support/uint128.h" // UInt<128>
#include "test/UnitTest/Test.h"
@@ -166,4 +168,22 @@ TYPED_TEST(LlvmLibcBlockMathExtrasTest, sub_overflow, UnsignedTypes) {
}
}
+TYPED_TEST(LlvmLibcErfcf16Test, SpecialValues) {
+ using LIBC_NAMESPACE_DECL::f16;
+
+ EXPECT_EQ(f16(1.0), LIBC_NAMESPACE_DECL::erfcf16(f16(0.0)));
+
+ EXPECT_TRUE(LIBC_NAMESPACE_DECL::TYPED_TEST(erfcf16(f16(10.0))) >= f16(0.0));
+ EXPECT_TRUE(LIBC_NAMESPACE_DECL::TYPED_TEST(erfcf16(f16(0.0))) <= f16(2.0));
+}
+
+TYPED_TEST(LlvmLibcErff16Test, SpecialValues) {
+ using LIBC_NAMESPACE_DECL::f16;
+
+ EXPECT_EQ(f16(1.0), LIBC_NAMESPACE_DECL::erff16(f16(0.0)));
+
+ EXPECT_TRUE(LIBC_NAMESPACE_DECL::TYPED_TEST(erff16(f16(10.0))) >= f16(0.0));
+ EXPECT_TRUE(LIBC_NAMESPACE_DECL::TYPED_TEST(erff16(f16(0.0))) <= f16(2.0));
+}
+
} // namespace LIBC_NAMESPACE_DECL
More information about the libc-commits
mailing list