Skip to content

Conversation

bassiounix
Copy link
Contributor

@bassiounix bassiounix commented Aug 18, 2025

This was referenced Aug 18, 2025
@bassiounix bassiounix added bazel "Peripheral" support tier build system: utils/bazel libc labels Aug 18, 2025 — with Graphite App
Copy link
Contributor Author

bassiounix commented Aug 18, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@bassiounix bassiounix requested a review from lntue August 18, 2025 23:01
@bassiounix bassiounix marked this pull request as ready for review August 18, 2025 23:01
@llvmbot
Copy link
Member

llvmbot commented Aug 18, 2025

@llvm/pr-subscribers-libc

Author: Muhammad Bassiouni (bassiounix)

Changes

Part of #147386

in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450


Full diff: https://github.com/llvm/llvm-project/pull/154222.diff

9 Files Affected:

  • (modified) libc/shared/math.h (+1)
  • (added) libc/shared/math/cospif16.h (+28)
  • (modified) libc/src/__support/math/CMakeLists.txt (+13)
  • (added) libc/src/__support/math/cospif16.h (+99)
  • (modified) libc/src/math/generic/CMakeLists.txt (+1-8)
  • (modified) libc/src/math/generic/cospif16.cpp (+2-72)
  • (modified) libc/test/shared/CMakeLists.txt (+1)
  • (modified) libc/test/shared/shared_math_test.cpp (+1)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+13-3)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6ab5df103bfb3..69d785b3e0291 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -38,6 +38,7 @@
 #include "math/coshf.h"
 #include "math/coshf16.h"
 #include "math/cospif.h"
+#include "math/cospif16.h"
 #include "math/erff.h"
 #include "math/exp.h"
 #include "math/exp10.h"
diff --git a/libc/shared/math/cospif16.h b/libc/shared/math/cospif16.h
new file mode 100644
index 0000000000000..bbb1f32e4ec1a
--- /dev/null
+++ b/libc/shared/math/cospif16.h
@@ -0,0 +1,28 @@
+//===-- Shared cospif16 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_COSPIF16_H
+#define LLVM_LIBC_SHARED_MATH_COSPIF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/cospif.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::cospif16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_COSPIF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 8ce6cef732d76..39dc0e57f4472 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -444,6 +444,19 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  cospif16
+  HDRS
+    cospif16.h
+  DEPENDS
+    .sincosf16_utils
+    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
+)
+
 add_header_library(
   erff
   HDRS
diff --git a/libc/src/__support/math/cospif16.h b/libc/src/__support/math/cospif16.h
new file mode 100644
index 0000000000000..d07236cccac75
--- /dev/null
+++ b/libc/src/__support/math/cospif16.h
@@ -0,0 +1,99 @@
+//===-- Implementation header for cospif16 ----------------------*- 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_COSPIF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COSPIF16_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"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 cospif16(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 cosine of sum
+  // formula:
+  //   cos(x * pi) = cos((k + y) * pi/32)
+  //               = cos(k * pi/32) * cos(y * pi/32) +
+  //                 sin(y * pi/32) * sin(k * pi/32)
+
+  // For signed zeros
+  if (LIBC_UNLIKELY(x_abs == 0U))
+    return fputil::cast<float16>(1.0f);
+
+  // 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);
+
+    // Check for NaN or infintiy 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 fputil::cast<float16>(1.0f);
+  }
+
+  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);
+
+  // Since, cosm1_y = cos_y - 1, therefore:
+  //   cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
+  return fputil::cast<float16>(fputil::multiply_add(
+      cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6c0dc1eeed692..5eba5e3616472 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -325,14 +325,7 @@ add_entrypoint_object(
   HDRS
     ../cospif16.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.cospif16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/cospif16.cpp b/libc/src/math/generic/cospif16.cpp
index c99285b25c106..a4bae8e13eac8 100644
--- a/libc/src/math/generic/cospif16.cpp
+++ b/libc/src/math/generic/cospif16.cpp
@@ -7,80 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/cospif16.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/macros/optimization.h"
-#include "src/__support/math/sincosf16_utils.h"
+#include "src/__support/math/cospif16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float16, cospif16, (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 cosine of sum
-  // formula:
-  //   cos(x * pi) = cos((k + y) * pi/32)
-  //               = cos(k * pi/32) * cos(y * pi/32) +
-  //                 sin(y * pi/32) * sin(k * pi/32)
-
-  // For signed zeros
-  if (LIBC_UNLIKELY(x_abs == 0U))
-    return fputil::cast<float16>(1.0f);
-
-  // 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);
-
-    // Check for NaN or infintiy 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 fputil::cast<float16>(1.0f);
-  }
-
-  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 && cos_k == 0))
-    return fputil::cast<float16>(0.0f);
-
-  // Since, cosm1_y = cos_y - 1, therefore:
-  //   cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y
-  return fputil::cast<float16>(fputil::multiply_add(
-      cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k)));
-}
+LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) { return math::cospif16(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 89d5b243b20a3..48241d3f55287 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -34,6 +34,7 @@ add_fp_unittest(
     libc.src.__support.math.coshf
     libc.src.__support.math.coshf16
     libc.src.__support.math.cospif
+    libc.src.__support.math.cospif16
     libc.src.__support.math.erff
     libc.src.__support.math.exp
     libc.src.__support.math.exp10
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index c22543d3337ab..2e5a2d51146d4 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -23,6 +23,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::atanhf16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::cosf16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::coshf16(0.0f16));
+  EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::cospif16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::exp10f16(0.0f16));
 
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::expf16(0.0f16));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c4f347788fbc6..f8f7713655adb 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2407,6 +2407,18 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_cospif16",
+    hdrs = ["src/__support/math/cospif16.h"],
+    deps = [
+        ":__support_fputil_cast",
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_multiply_add",
+        ":__support_macros_optimization",
+        ":__support_math_sincosf16_utils",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_erff",
     hdrs = ["src/__support/math/erff.h"],
@@ -3242,9 +3254,7 @@ libc_math_function(
 libc_math_function(
     name = "cospif16",
     additional_deps = [
-        ":__support_fputil_multiply_add",
-        ":__support_macros_optimization",
-        ":__support_math_sincosf16_utils",
+        ":__support_math_cospif16",
     ],
 )
 

@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-19-_libc_math_refactor_cospif_implementation_to_header-only_in_src___support_math_folder branch from fb06fed to f3ee845 Compare August 18, 2025 23:10
@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-19-_libc_math_refactor_cospif16_implementation_to_header-only_in_src___support_math_folder branch 2 times, most recently from 6375583 to d8b38a7 Compare August 19, 2025 01:55
@bassiounix bassiounix requested a review from a team as a code owner August 19, 2025 01:55
@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-19-_libc_math_refactor_cospif16_implementation_to_header-only_in_src___support_math_folder branch from d8b38a7 to 229c69d Compare August 19, 2025 01:57
@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-19-_libc_math_refactor_cospif_implementation_to_header-only_in_src___support_math_folder branch from f3ee845 to 0828e20 Compare August 19, 2025 01:57
@bassiounix bassiounix removed the request for review from a team August 19, 2025 01:58
Copy link
Contributor Author

bassiounix commented Aug 22, 2025

Merge activity

  • Aug 22, 1:42 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Aug 22, 1:54 AM UTC: Graphite rebased this pull request as part of a merge.
  • Aug 22, 1:57 AM UTC: Graphite rebased this pull request as part of a merge.
  • Aug 22, 2:04 AM UTC: @bassiounix merged this pull request with Graphite.

@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-19-_libc_math_refactor_cospif_implementation_to_header-only_in_src___support_math_folder branch from 0828e20 to bb73a0a Compare August 22, 2025 01:44
Base automatically changed from users/bassiounix/spr/08-19-_libc_math_refactor_cospif_implementation_to_header-only_in_src___support_math_folder to main August 22, 2025 01:53
@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-19-_libc_math_refactor_cospif16_implementation_to_header-only_in_src___support_math_folder branch from 229c69d to 0d287b7 Compare August 22, 2025 01:54
@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-19-_libc_math_refactor_cospif16_implementation_to_header-only_in_src___support_math_folder branch from 0d287b7 to 51d48be Compare August 22, 2025 01:56
@bassiounix bassiounix merged commit 4d32320 into main Aug 22, 2025
20 checks passed
@bassiounix bassiounix deleted the users/bassiounix/spr/08-19-_libc_math_refactor_cospif16_implementation_to_header-only_in_src___support_math_folder branch August 22, 2025 02:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel libc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants