[libc-commits] [libc] [libc][math] Fix `cospif16` error handling (PR #236371)
via libc-commits
libc-commits at lists.llvm.org
Wed Oct 28 03:48:24 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Connector Switch (c8ef)
<details>
<summary>Changes</summary>
Follow up of https://github.com/llvm/llvm-project/pull/235780, tested `cospif16` with CORE-MATH's exhaustive checker in all four rounding modes. With `CORE_MATH_SUPPORT_ERRNO` and `CORE_MATH_CHECK_INEXACT` enabled.
---
Full diff: https://github.com/llvm/llvm-project/pull/236371.diff
2 Files Affected:
- (modified) libc/src/__support/math/cospif16.h (+9-4)
- (modified) libc/test/src/math/smoke/cospif16_test.cpp (+4-1)
``````````diff
diff --git a/libc/src/__support/math/cospif16.h b/libc/src/__support/math/cospif16.h
index 52a91df0e6c7e..fd7f8023b4dcd 100644
--- a/libc/src/__support/math/cospif16.h
+++ b/libc/src/__support/math/cospif16.h
@@ -17,6 +17,7 @@
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/is_integer.h"
#include "src/__support/FPUtil/multiply_add.h"
#include "src/__support/macros/optimization.h"
@@ -53,12 +54,12 @@ LIBC_INLINE constexpr float16 cospif16(float16 x) {
// For signed zeros
if (LIBC_UNLIKELY(x_abs == 0U))
- return fputil::cast<float16>(1.0f);
+ return 1.0f16;
// Numbers greater or equal to 2^10 are integers, or infinity, or NaN
if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
if (LIBC_UNLIKELY(x_abs <= 0x67FF))
- return fputil::cast<float16>((x_abs & 0x1) ? -1.0f : 1.0f);
+ return (x_abs & 0x1) ? -1.0f16 : 1.0f16;
// Check for NaN or infintiy values
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
@@ -75,14 +76,18 @@ LIBC_INLINE constexpr float16 cospif16(float16 x) {
return x + FPBits::quiet_nan().get_val();
}
- return fputil::cast<float16>(1.0f);
+ return 1.0f16;
}
+ // Integers have exact results of +1 or -1.
+ if (LIBC_UNLIKELY(fputil::is_integer(x)))
+ return fputil::is_odd_integer(x) ? -1.0f16 : 1.0f16;
+
float sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0))
- return fputil::cast<float16>(0.0f);
+ return 0.0f16;
// Since, cosm1_y = cos_y - 1, therefore:
// cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
diff --git a/libc/test/src/math/smoke/cospif16_test.cpp b/libc/test/src/math/smoke/cospif16_test.cpp
index 307e483d8ab8d..3a946b964c195 100644
--- a/libc/test/src/math/smoke/cospif16_test.cpp
+++ b/libc/test/src/math/smoke/cospif16_test.cpp
@@ -21,7 +21,7 @@ TEST_F(LlvmLibcCospif16Test, SpecialNumbers) {
EXPECT_FP_IS_NAN(LIBC_NAMESPACE::cospif16(aNaN));
EXPECT_MATH_ERRNO(0);
- EXPECT_FP_EQ(FPBits::one().get_val(), LIBC_NAMESPACE::cospif16(zero));
+ EXPECT_FP_EQ_WITH_EXCEPTION(1.0f16, LIBC_NAMESPACE::cospif16(zero), 0);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ(FPBits::one().get_val(), LIBC_NAMESPACE::cospif16(neg_zero));
@@ -32,6 +32,9 @@ TEST_F(LlvmLibcCospif16Test, SpecialNumbers) {
EXPECT_FP_IS_NAN(LIBC_NAMESPACE::cospif16(neg_inf));
EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(-1.0f16, LIBC_NAMESPACE::cospif16(1.0f16), 0);
+ EXPECT_MATH_ERRNO(0);
}
TEST_F(LlvmLibcCospif16Test, Integers) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/236371
More information about the libc-commits
mailing list