[libc-commits] [libc] 1cb219f - [libc][math] Fix -Wc++23-extensions warnings in erfcf16 and erff16. (#215613)
via libc-commits
libc-commits at lists.llvm.org
Fri Aug 14 09:07:21 PDT 2026
Author: original-cooling-space
Date: 2026-08-14T09:07:15-07:00
New Revision: 1cb219f75aaeb7c2b98d85ec87d92d3c602b7a78
URL: https://github.com/llvm/llvm-project/commit/1cb219f75aaeb7c2b98d85ec87d92d3c602b7a78
DIFF: https://github.com/llvm/llvm-project/commit/1cb219f75aaeb7c2b98d85ec87d92d3c602b7a78.diff
LOG: [libc][math] Fix -Wc++23-extensions warnings in erfcf16 and erff16. (#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
was 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 0.0f16;
// The original approach
return fputil::cast<float16>(0.0f);
// Intue suggested
FPBits::zero().get_val();
```
The conditional expression uses `constexpr float16` to store the result
in a variable for processing.
## Test case
I added two additional tests to the two files.
- `libc/src/__support/math/erfcf16.h`
- `libc/src/__support/math/erff16.h`
0ne is for test cases greater than 2.0 and another to test cases less
than 1.0. This test also added test cases with negative values. I ran
the tests using `ninja -C build llvm-lit`.
Added:
Modified:
libc/src/__support/math/erfcf16.h
libc/src/__support/math/erff16.h
Removed:
################################################################################
diff --git a/libc/src/__support/math/erfcf16.h b/libc/src/__support/math/erfcf16.h
index ad096527855d5..7608586001996 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 FPBits::zero().get_val();
}
// Polynomial approximation:
diff --git a/libc/src/__support/math/erff16.h b/libc/src/__support/math/erff16.h
index 8e3a92c092e22..68b6575957989 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;
+ 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);
+ 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;
More information about the libc-commits
mailing list