[libc-commits] [libc] [llvm] [libc] use constexpr for sinpif (PR #176580)
via libc-commits
libc-commits at lists.llvm.org
Sat Jan 17 09:54:44 PST 2026
https://github.com/DannyDaoBoYang updated https://github.com/llvm/llvm-project/pull/176580
>From ebcb49a1161f656bc04b959047f7d9167001de29 Mon Sep 17 00:00:00 2001
From: DannyDaoBoYang <34634047+DannyDaoBoYang at users.noreply.github.com>
Date: Sat, 17 Jan 2026 12:38:22 -0500
Subject: [PATCH 1/2] [libc] use constexpr for sinpif
---
libc/shared/math.h | 1 +
libc/shared/math/sinpif.h | 22 ++++
libc/src/__support/math/CMakeLists.txt | 16 +++
libc/src/__support/math/sinpif.h | 120 ++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 9 +-
libc/src/math/generic/sinpif.cpp | 105 +--------------
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 3 +-
.../llvm-project-overlay/libc/BUILD.bazel | 7 +-
9 files changed, 171 insertions(+), 113 deletions(-)
create mode 100644 libc/shared/math/sinpif.h
create mode 100644 libc/src/__support/math/sinpif.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index b75e60c4f1973..21e037bed7a51 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -71,5 +71,6 @@
#include "math/rsqrtf.h"
#include "math/rsqrtf16.h"
#include "math/sin.h"
+#include "math/sinpif.h"
#endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/sinpif.h b/libc/shared/math/sinpif.h
new file mode 100644
index 0000000000000..82fe1937f1c83
--- /dev/null
+++ b/libc/shared/math/sinpif.h
@@ -0,0 +1,22 @@
+//===-- Shared sin 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_SINPIF_H
+#define LLVM_LIBC_SHARED_MATH_SINPIF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sinpif.h"
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sinpif;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SINPIF_H
\ No newline at end of file
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 97852c8688182..15b4b39b3eaff 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1044,6 +1044,22 @@ add_header_library(
libc.src.__support.FPUtil.multiply_add
)
+add_header_library(
+ sinpif
+ HDRS
+ sinpif.h
+ DEPENDS
+ .exp10f_utils
+ libc.src.__support.FPUtil.FEnvImpl
+ libc.src.__support.FPUtil.FPBits
+ libc.src.__support.FPUtil.PolyEval
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+ libc.src.__support.math.sincosf_utils
+)
+
add_header_library(
log
HDRS
diff --git a/libc/src/__support/math/sinpif.h b/libc/src/__support/math/sinpif.h
new file mode 100644
index 0000000000000..c1e97ddd0c3c8
--- /dev/null
+++ b/libc/src/__support/math/sinpif.h
@@ -0,0 +1,120 @@
+//===-- Implementation header for sinpif -------------------------*- 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_SINPIF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF_H
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.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
+#include "src/__support/math/sincosf_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+LIBC_INLINE static constexpr float sinpif(float x) {
+ using FPBits = typename fputil::FPBits<float>;
+ FPBits xbits(x);
+
+ uint32_t x_u = xbits.uintval();
+ uint32_t x_abs = x_u & 0x7fff'ffffU;
+ double xd = static_cast<double>(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(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
+ // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
+ // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
+ // computed using degree-7 and degree-6 minimax polynomials generated by
+ // Sollya respectively.
+
+ // |x| <= 1/16
+ if (LIBC_UNLIKELY(x_abs <= 0x3d80'0000U)) {
+
+ if (LIBC_UNLIKELY(x_abs < 0x33CD'01D7U)) {
+ if (LIBC_UNLIKELY(x_abs == 0U)) {
+ // For signed zeros.
+ return x;
+ }
+
+ // For very small values we can approximate sinpi(x) with x * pi
+ // An exhaustive test shows that this is accurate for |x| < 9.546391 ×
+ // 10-8
+ double xdpi = xd * 0x1.921fb54442d18p1;
+ return static_cast<float>(xdpi);
+ }
+
+ // |x| < 1/16.
+ double xsq = xd * xd;
+
+ // Degree-9 polynomial approximation:
+ // sinpi(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9
+ // = x (1 + a_3 x^2 + ... + a_9 x^8)
+ // = x * P(x^2)
+ // generated by Sollya with the following commands:
+ // > display = hexadecimal;
+ // > Q = fpminimax(sin(pi * x)/x, [|0, 2, 4, 6, 8|], [|D...|], [0, 1/16]);
+ double result = fputil::polyeval(
+ xsq, 0x1.921fb54442d18p1, -0x1.4abbce625bbf2p2, 0x1.466bc675e116ap1,
+ -0x1.32d2c0b62d41cp-1, 0x1.501ec4497cb7dp-4);
+ return static_cast<float>(xd * result);
+ }
+
+ // Numbers greater or equal to 2^23 are always integers or NaN
+ if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) {
+
+ // check for NaN values
+ if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ if (x_abs == 0x7f80'0000U) {
+ 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();
+ }
+
+ // Combine the results with the sine of sum formula:
+ // sin(x * pi) = sin((k + y)*pi/32)
+ // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
+ // = sin_y * cos_k + (1 + cosm1_y) * sin_k
+ // = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
+ double sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
+ sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
+
+ if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
+ return FPBits::zero(xbits.sign()).get_val();
+
+ return static_cast<float>(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_SINPIF_H
\ No newline at end of file
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6fb260a7523d4..bbe9ea50fb209 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -433,14 +433,7 @@ add_entrypoint_object(
HDRS
../sinpif.h
DEPENDS
- libc.src.__support.math.sincosf_utils
- libc.src.__support.FPUtil.fenv_impl
- libc.src.__support.FPUtil.fp_bits
- libc.src.__support.FPUtil.fma
- libc.src.__support.FPUtil.multiply_add
- libc.src.__support.FPUtil.polyeval
- libc.src.__support.common
- libc.src.__support.macros.optimization
+ libc.src.__support.math.sinpif
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/sinpif.cpp b/libc/src/math/generic/sinpif.cpp
index f3383f19db60f..f7a6e1acb6534 100644
--- a/libc/src/math/generic/sinpif.cpp
+++ b/libc/src/math/generic/sinpif.cpp
@@ -7,111 +7,10 @@
//===----------------------------------------------------------------------===//
#include "src/math/sinpif.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
-#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/PolyEval.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
-#include "src/__support/math/sincosf_utils.h"
-
+#include "src/__support/math/sinpif.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float, sinpif, (float x)) {
- using FPBits = typename fputil::FPBits<float>;
- FPBits xbits(x);
-
- uint32_t x_u = xbits.uintval();
- uint32_t x_abs = x_u & 0x7fff'ffffU;
- double xd = static_cast<double>(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(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
- // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
- // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
- // computed using degree-7 and degree-6 minimax polynomials generated by
- // Sollya respectively.
-
- // |x| <= 1/16
- if (LIBC_UNLIKELY(x_abs <= 0x3d80'0000U)) {
-
- if (LIBC_UNLIKELY(x_abs < 0x33CD'01D7U)) {
- if (LIBC_UNLIKELY(x_abs == 0U)) {
- // For signed zeros.
- return x;
- }
-
- // For very small values we can approximate sinpi(x) with x * pi
- // An exhaustive test shows that this is accurate for |x| < 9.546391 ×
- // 10-8
- double xdpi = xd * 0x1.921fb54442d18p1;
- return static_cast<float>(xdpi);
- }
-
- // |x| < 1/16.
- double xsq = xd * xd;
-
- // Degree-9 polynomial approximation:
- // sinpi(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9
- // = x (1 + a_3 x^2 + ... + a_9 x^8)
- // = x * P(x^2)
- // generated by Sollya with the following commands:
- // > display = hexadecimal;
- // > Q = fpminimax(sin(pi * x)/x, [|0, 2, 4, 6, 8|], [|D...|], [0, 1/16]);
- double result = fputil::polyeval(
- xsq, 0x1.921fb54442d18p1, -0x1.4abbce625bbf2p2, 0x1.466bc675e116ap1,
- -0x1.32d2c0b62d41cp-1, 0x1.501ec4497cb7dp-4);
- return static_cast<float>(xd * result);
- }
-
- // Numbers greater or equal to 2^23 are always integers or NaN
- if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) {
-
- // check for NaN values
- if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
- if (xbits.is_signaling_nan()) {
- fputil::raise_except_if_required(FE_INVALID);
- return FPBits::quiet_nan().get_val();
- }
-
- if (x_abs == 0x7f80'0000U) {
- 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();
- }
-
- // Combine the results with the sine of sum formula:
- // sin(x * pi) = sin((k + y)*pi/32)
- // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
- // = sin_y * cos_k + (1 + cosm1_y) * sin_k
- // = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
- double sin_k, cos_k, sin_y, cosm1_y;
- sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
-
- if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
- return FPBits::zero(xbits.sign()).get_val();
-
- return static_cast<float>(fputil::multiply_add(
- sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
+ return math::sinpif(x);
}
-
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 3a9bf9f8d4cf4..7bfd26f9d6145 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -67,4 +67,5 @@ add_fp_unittest(
libc.src.__support.math.rsqrtf
libc.src.__support.math.rsqrtf16
libc.src.__support.math.sin
+ libc.src.__support.math.sinpif
)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 84563d851a002..d04876318f002 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -69,7 +69,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f));
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp2f(0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::expm1f(0.0f));
-
+
EXPECT_FP_EQ_ALL_ROUNDING(0.75f,
LIBC_NAMESPACE::shared::frexpf(24.0f, &exponent));
EXPECT_EQ(exponent, 5);
@@ -78,6 +78,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
ASSERT_FP_EQ(float(-1 * (8 << 5)), LIBC_NAMESPACE::shared::ldexpf(-8.0f, 5));
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::rsqrtf(1.0f));
+ EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::sinpif(0.0f));
}
TEST(LlvmLibcSharedMathTest, AllDouble) {
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index f1d390207e45e..1eb9b99e61a33 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3267,6 +3267,11 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_sinpif",
+ hdrs = ["src/__support/math/sinpif.h"],
+ deps = [":__support_sincosf_utils",],
+)
############################### complex targets ################################
libc_function(
@@ -4886,7 +4891,7 @@ libc_math_function(
libc_math_function(
name = "sinpif",
additional_deps = [
- ":__support_sincosf_utils",
+ ":__support_math_sinpif",
],
)
>From e9494a012db214bde405ae10a01a133468e5bb6b Mon Sep 17 00:00:00 2001
From: DannyDaoBoYang <34634047+DannyDaoBoYang at users.noreply.github.com>
Date: Sat, 17 Jan 2026 12:54:30 -0500
Subject: [PATCH 2/2] newline at end of line
---
libc/src/__support/math/sinpif.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math/sinpif.h b/libc/src/__support/math/sinpif.h
index c1e97ddd0c3c8..14a5b3001fe22 100644
--- a/libc/src/__support/math/sinpif.h
+++ b/libc/src/__support/math/sinpif.h
@@ -117,4 +117,4 @@ LIBC_INLINE static constexpr float sinpif(float x) {
}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF_H
\ No newline at end of file
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF_H
More information about the libc-commits
mailing list