[libc-commits] [libc] [libc][math] Fix `cospif16` error handling (PR #236371)
Connector Switch via libc-commits
libc-commits at lists.llvm.org
Wed Oct 28 03:47:41 PDT 2026
https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/236371
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.
>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] [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 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) {
More information about the libc-commits
mailing list