[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 15:34:15 PST 2026


https://github.com/DannyDaoBoYang updated https://github.com/llvm/llvm-project/pull/178503

>From 8537d9cde82fae15e5e3aead2865b73081ecdf62 Mon Sep 17 00:00:00 2001
From: DannyDaoBoYang <34634047+DannyDaoBoYang at users.noreply.github.com>
Date: Sun, 8 Feb 2026 10:45:09 -0500
Subject: [PATCH 1/2] refactor sinpif16

---
 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            | 67 +-------------
 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(+), 76 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 66132326c2257..61b93e7c1108c 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -106,6 +106,7 @@
 #include "math/sqrt.h"
 #include "math/sqrtf.h"
 #include "math/sqrtf128.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 c0293c0969217..ed0b6029c9891 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1275,6 +1275,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..444309a72a63d
--- /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 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 96dc0e7ca4851..825c5240e0b2d 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -436,14 +436,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 94cbd33b76f9e..31a9ba45d5d3e 100644
--- a/libc/src/math/generic/sinpif16.cpp
+++ b/libc/src/math/generic/sinpif16.cpp
@@ -7,75 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #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 math::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)));
+  return math::sinpif16(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7d72ced3e057c..292861c7d5553 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -102,6 +102,7 @@ add_fp_unittest(
     libc.src.__support.math.sinhf16
     libc.src.__support.math.sqrtf128
     libc.src.__support.math.sinpif
+    libc.src.__support.math.sinpif16
     libc.src.__support.math.sqrt
     libc.src.__support.math.sqrtf
     libc.src.__support.math.tan
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 9e024387efbfa..89610f34f1f15 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -68,6 +68,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 ae7228c52d3c0..eaf4c0d33a9d4 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3769,6 +3769,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"],
@@ -5421,9 +5438,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",
     ],
 )
 

>From d981be39eb104d8b62a17e8070660b1aae353ae4 Mon Sep 17 00:00:00 2001
From: DannyDaoBoYang <34634047+DannyDaoBoYang at users.noreply.github.com>
Date: Sun, 8 Feb 2026 18:34:02 -0500
Subject: [PATCH 2/2] pre-processor guards for 16

---
 libc/shared/math.h                     | 2 +-
 libc/shared/math/sinpif16.h            | 6 ++++++
 libc/src/__support/math/CMakeLists.txt | 4 ++--
 libc/src/__support/math/sinpif16.h     | 6 ++++++
 libc/src/math/generic/sinpif16.cpp     | 4 +---
 5 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 61b93e7c1108c..c14cdd107df46 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -103,10 +103,10 @@
 #include "math/sinhf.h"
 #include "math/sinhf16.h"
 #include "math/sinpif.h"
+#include "math/sinpif16.h"
 #include "math/sqrt.h"
 #include "math/sqrtf.h"
 #include "math/sqrtf128.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
index 09aed817a0c16..b50ad766d6d84 100644
--- a/libc/shared/math/sinpif16.h
+++ b/libc/shared/math/sinpif16.h
@@ -9,6 +9,10 @@
 #ifndef LLVM_LIBC_SHARED_MATH_SINPIF16_H
 #define LLVM_LIBC_SHARED_MATH_SINPIF16_H
 
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
 #include "shared/libc_common.h"
 #include "src/__support/math/sinpif16.h"
 
@@ -20,4 +24,6 @@ using math::sinpif16;
 } // namespace shared
 } // namespace LIBC_NAMESPACE_DECL
 
+#endif // LIBC_TYPES_HAS_FLOAT16
+
 #endif // LLVM_LIBC_SHARED_MATH_SINPIF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index ed0b6029c9891..e105581ea494a 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1282,8 +1282,8 @@ add_header_library(
   DEPENDS
     .sincosf16_utils
     libc.src.__support.FPUtil.cast
-    libc.src.__support.FPUtil.FEnvImpl
-    libc.src.__support.FPUtil.FPBits
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.multiply_add
     libc.src.__support.common
     libc.src.__support.macros.config
diff --git a/libc/src/__support/math/sinpif16.h b/libc/src/__support/math/sinpif16.h
index 444309a72a63d..75d4c0c42fcc6 100644
--- a/libc/src/__support/math/sinpif16.h
+++ b/libc/src/__support/math/sinpif16.h
@@ -9,6 +9,10 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H
 
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
 #include "sincosf16_utils.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
@@ -85,4 +89,6 @@ LIBC_INLINE float16 sinpif16(float16 x) {
 } // namespace math
 } // namespace LIBC_NAMESPACE_DECL
 
+#endif // LIBC_TYPES_HAS_FLOAT16
+
 #endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF16_H
diff --git a/libc/src/math/generic/sinpif16.cpp b/libc/src/math/generic/sinpif16.cpp
index 31a9ba45d5d3e..e7051beb32746 100644
--- a/libc/src/math/generic/sinpif16.cpp
+++ b/libc/src/math/generic/sinpif16.cpp
@@ -11,8 +11,6 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float16, sinpif16, (float16 x)) {
-  return math::sinpif16(x);
-}
+LLVM_LIBC_FUNCTION(float16, sinpif16, (float16 x)) { return math::sinpif16(x); }
 
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list