[libc-commits] [libc] [libc][math] Fix `cospif16` error handling (PR #236371)
Connector Switch via libc-commits
libc-commits at lists.llvm.org
Wed Nov 4 03:48:11 PST 2026
https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/236371
>From cc717cac3fa45b93b46e3fa5d8f7c75f438ac38f Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 28 Oct 2026 18:45:55 +0800
Subject: [PATCH 1/3] [libc][math] Fix error handling
---
libc/src/__support/math/cospif16.h | 13 +++++++++----
libc/test/src/math/smoke/cospif16_test.cpp | 5 ++++-
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/libc/src/__support/math/cospif16.h b/libc/src/__support/math/cospif16.h
index 52a91df0e6c7e3..fd7f8023b4dcd3 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 307e483d8ab8da..3a946b964c195e 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) {
>From cbb296e381b969d1971345df6e557e5576aa9731 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 4 Nov 2026 19:45:38 +0800
Subject: [PATCH 2/3] resolve f16 literals
---
libc/src/__support/math/cospif16.h | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/libc/src/__support/math/cospif16.h b/libc/src/__support/math/cospif16.h
index fd7f8023b4dcd3..8cdbe108a5bbae 100644
--- a/libc/src/__support/math/cospif16.h
+++ b/libc/src/__support/math/cospif16.h
@@ -52,14 +52,16 @@ LIBC_INLINE constexpr float16 cospif16(float16 x) {
// = cos(k * pi/32) * cos(y * pi/32) +
// sin(y * pi/32) * sin(k * pi/32)
+ // TODO: Revisit float16 literals after resolving C++17 compatibility:
+ // https://github.com/llvm/llvm-project/issues/207380
// For signed zeros
if (LIBC_UNLIKELY(x_abs == 0U))
- return 1.0f16;
+ return FPBits::one().get_val();
// 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 (x_abs & 0x1) ? -1.0f16 : 1.0f16;
+ return FPBits::one((x_abs & 0x1) ? Sign::NEG : Sign::POS).get_val();
// Check for NaN or infintiy values
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
@@ -76,18 +78,19 @@ LIBC_INLINE constexpr float16 cospif16(float16 x) {
return x + FPBits::quiet_nan().get_val();
}
- return 1.0f16;
+ return FPBits::one().get_val();
}
// 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;
+ return FPBits::one(fputil::is_odd_integer(x) ? Sign::NEG : Sign::POS)
+ .get_val();
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 0.0f16;
+ return FPBits::zero().get_val();
// Since, cosm1_y = cos_y - 1, therefore:
// cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
>From 387ac2818972f893f3e08ad75c0c4272241617af Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 4 Nov 2026 19:47:55 +0800
Subject: [PATCH 3/3] resolve f16 literals
---
libc/test/src/math/smoke/cospif16_test.cpp | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/libc/test/src/math/smoke/cospif16_test.cpp b/libc/test/src/math/smoke/cospif16_test.cpp
index 3a946b964c195e..1dcbde0587be68 100644
--- a/libc/test/src/math/smoke/cospif16_test.cpp
+++ b/libc/test/src/math/smoke/cospif16_test.cpp
@@ -21,7 +21,8 @@ TEST_F(LlvmLibcCospif16Test, SpecialNumbers) {
EXPECT_FP_IS_NAN(LIBC_NAMESPACE::cospif16(aNaN));
EXPECT_MATH_ERRNO(0);
- EXPECT_FP_EQ_WITH_EXCEPTION(1.0f16, LIBC_NAMESPACE::cospif16(zero), 0);
+ EXPECT_FP_EQ_WITH_EXCEPTION(FPBits::one().get_val(),
+ LIBC_NAMESPACE::cospif16(zero), 0);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ(FPBits::one().get_val(), LIBC_NAMESPACE::cospif16(neg_zero));
@@ -33,7 +34,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_FP_EQ_WITH_EXCEPTION(FPBits::one(Sign::NEG).get_val(),
+ LIBC_NAMESPACE::cospif16(FPBits::one().get_val()),
+ 0);
EXPECT_MATH_ERRNO(0);
}
More information about the libc-commits
mailing list