[libc-commits] [libc] [llvm] [libc][math] Refactor sinpif16 to header only. (PR #178503)
via libc-commits
libc-commits at lists.llvm.org
Sun Feb 8 06:50:17 PST 2026
https://github.com/DannyDaoBoYang updated https://github.com/llvm/llvm-project/pull/178503
>From dc2b00ade1c1caba4396b3041dd7d6324643a6ae Mon Sep 17 00:00:00 2001
From: DannyDaoBoYang <34634047+DannyDaoBoYang at users.noreply.github.com>
Date: Wed, 28 Jan 2026 14:41:00 -0500
Subject: [PATCH] refactor sinpif16
Update BUILD.bazel
end of line
file format
---
libc/shared/math.h | 1 +
libc/shared/math/sinpif16.h | 23 +++++
libc/src/__support/math/CMakeLists.txt | 15 ++++
libc/src/__support/math/sinpif16.h | 88 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 9 +-
libc/src/math/generic/sinpif16.cpp | 69 +--------------
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 21 ++++-
9 files changed, 150 insertions(+), 78 deletions(-)
create mode 100644 libc/shared/math/sinpif16.h
create mode 100644 libc/src/__support/math/sinpif16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 452fe3fddc911..fe24c0a06f9f2 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -95,6 +95,7 @@
#include "math/sinhf.h"
#include "math/sinhf16.h"
#include "math/sinpif.h"
+#include "math/sinpif16.h"
#include "math/sqrtf16.h"
#include "math/tan.h"
#include "math/tanf.h"
diff --git a/libc/shared/math/sinpif16.h b/libc/shared/math/sinpif16.h
new file mode 100644
index 0000000000000..09aed817a0c16
--- /dev/null
+++ b/libc/shared/math/sinpif16.h
@@ -0,0 +1,23 @@
+//===-- Shared sinpif16 function --------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_SINPIF16_H
+#define LLVM_LIBC_SHARED_MATH_SINPIF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sinpif16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sinpif16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SINPIF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 0fa48661e58e8..f57c7dca50a7a 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1178,6 +1178,21 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ sinpif16
+ HDRS
+ sinpif16.h
+ DEPENDS
+ .sincosf16_utils
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.FEnvImpl
+ libc.src.__support.FPUtil.FPBits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
llogb
HDRS
diff --git a/libc/src/__support/math/sinpif16.h b/libc/src/__support/math/sinpif16.h
new file mode 100644
index 0000000000000..d97522505c211
--- /dev/null
+++ b/libc/src/__support/math/sinpif16.h
@@ -0,0 +1,88 @@
+//===-- Implementation header for sinpif16 -------------------------* C++*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H
+
+#include "sincosf16_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE static float16 sinpif16(float16 x) {
+ using namespace sincosf16_internal;
+ using FPBits = typename fputil::FPBits<float16>;
+ FPBits xbits(x);
+
+ uint16_t x_u = xbits.uintval();
+ uint16_t x_abs = x_u & 0x7fff;
+ float xf = x;
+
+ // Range reduction:
+ // For |x| > 1/32, we perform range reduction as follows:
+ // Find k and y such that:
+ // x = (k + y) * 1/32
+ // k is an integer
+ // |y| < 0.5
+ //
+ // This is done by performing:
+ // k = round(x * 32)
+ // y = x * 32 - k
+ //
+ // Once k and y are computed, we then deduce the answer by the sine of sum
+ // formula:
+ // sin(x * pi) = sin((k + y) * pi/32)
+ // = sin(k * pi/32) * cos(y * pi/32) +
+ // sin(y * pi/32) * cos(k * pi/32)
+
+ // For signed zeros
+ if (LIBC_UNLIKELY(x_abs == 0U))
+ return x;
+
+ // Numbers greater or equal to 2^10 are integers, or infinity, or NaN
+ if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
+ // Check for NaN or infinity values
+ if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ // If value is equal to infinity
+ if (x_abs == 0x7c00) {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ }
+
+ return x + FPBits::quiet_nan().get_val();
+ }
+ return FPBits::zero(xbits.sign()).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 && sin_k == 0))
+ return FPBits::zero(xbits.sign()).get_val();
+
+ // Since, cosm1_y = cos_y - 1, therefore:
+ // sin(x * pi) = cos_k * sin_y + sin_k + (cosm1_y * sin_k)
+ return fputil::cast<float16>(fputil::multiply_add(
+ sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 2475aa52c3c0e..ff38177bc7d0d 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -444,14 +444,7 @@ add_entrypoint_object(
HDRS
../sinpif16.h
DEPENDS
- libc.hdr.errno_macros
- libc.hdr.fenv_macros
- libc.src.__support.FPUtil.cast
- libc.src.__support.FPUtil.fenv_impl
- libc.src.__support.FPUtil.fp_bits
- libc.src.__support.FPUtil.multiply_add
- libc.src.__support.macros.optimization
- libc.src.__support.math.sincosf16_utils
+ libc.src.__support.math.sinpif16
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/sinpif16.cpp b/libc/src/math/generic/sinpif16.cpp
index 311e6f989ebf1..e7051beb32746 100644
--- a/libc/src/math/generic/sinpif16.cpp
+++ b/libc/src/math/generic/sinpif16.cpp
@@ -7,75 +7,10 @@
//===----------------------------------------------------------------------===//
#include "src/math/sinpif16.h"
-#include "hdr/errno_macros.h"
-#include "hdr/fenv_macros.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
-#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/cast.h"
-#include "src/__support/FPUtil/multiply_add.h"
-#include "src/__support/math/sincosf16_utils.h"
+#include "src/__support/math/sinpif16.h"
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(float16, sinpif16, (float16 x)) {
- using namespace sincosf16_internal;
- using FPBits = typename fputil::FPBits<float16>;
- FPBits xbits(x);
-
- uint16_t x_u = xbits.uintval();
- uint16_t x_abs = x_u & 0x7fff;
- float xf = x;
-
- // Range reduction:
- // For |x| > 1/32, we perform range reduction as follows:
- // Find k and y such that:
- // x = (k + y) * 1/32
- // k is an integer
- // |y| < 0.5
- //
- // This is done by performing:
- // k = round(x * 32)
- // y = x * 32 - k
- //
- // Once k and y are computed, we then deduce the answer by the sine of sum
- // formula:
- // sin(x * pi) = sin((k + y) * pi/32)
- // = sin(k * pi/32) * cos(y * pi/32) +
- // sin(y * pi/32) * cos(k * pi/32)
-
- // For signed zeros
- if (LIBC_UNLIKELY(x_abs == 0U))
- return x;
-
- // Numbers greater or equal to 2^10 are integers, or infinity, or NaN
- if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
- // Check for NaN or infinity values
- if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
- if (xbits.is_signaling_nan()) {
- fputil::raise_except_if_required(FE_INVALID);
- return FPBits::quiet_nan().get_val();
- }
- // If value is equal to infinity
- if (x_abs == 0x7c00) {
- fputil::set_errno_if_required(EDOM);
- fputil::raise_except_if_required(FE_INVALID);
- }
-
- return x + FPBits::quiet_nan().get_val();
- }
- return FPBits::zero(xbits.sign()).get_val();
- }
-
- float sin_k, cos_k, sin_y, cosm1_y;
- sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
-
- if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
- return FPBits::zero(xbits.sign()).get_val();
-
- // Since, cosm1_y = cos_y - 1, therefore:
- // sin(x * pi) = cos_k * sin_y + sin_k + (cosm1_y * sin_k)
- return fputil::cast<float16>(fputil::multiply_add(
- sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
-}
+LLVM_LIBC_FUNCTION(float16, sinpif16, (float16 x)) { return math::sinpif16(x); }
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index fb9874ab3ec03..180610f4e8d20 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -93,6 +93,7 @@ add_fp_unittest(
libc.src.__support.math.sinhf
libc.src.__support.math.sinhf16
libc.src.__support.math.sinpif
+ libc.src.__support.math.sinpif16
libc.src.__support.math.tan
libc.src.__support.math.tanf
)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 92511cc55267e..cac0825f588e6 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -55,6 +55,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::sinf16(0.0f16));
+ EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::sinpif16(0.0f16));
}
#endif // LIBC_TYPES_HAS_FLOAT16
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 87b2a392287a6..da598c9eb8ec1 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3634,6 +3634,23 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_sinpif16",
+ hdrs = ["src/__support/math/sinpif16.h"],
+ deps = [
+ ":__support_common",
+ ":__support_fputil_cast",
+ ":__support_fputil_fenv_impl",
+ ":__support_fputil_fp_bits",
+ ":__support_fputil_multiply_add",
+ ":__support_fputil_nearest_integer",
+ ":__support_fputil_polyeval",
+ ":__support_macros_config",
+ ":__support_macros_optimization",
+ ":__support_math_sincosf16_utils",
+ ],
+)
+
libc_support_library(
name = "__support_math_tan",
hdrs = ["src/__support/math/tan.h"],
@@ -5286,9 +5303,7 @@ libc_math_function(
libc_math_function(
name = "sinpif16",
additional_deps = [
- ":__support_fputil_nearest_integer",
- ":__support_fputil_polyeval",
- ":__support_math_sincosf16_utils",
+ ":__support_math_sinpif16",
],
)
More information about the libc-commits
mailing list