[libc-commits] [libc] [llvm] [libc][math] Refactor sincos implementation to header only (PR #177522)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 26 21:11:59 PST 2026


https://github.com/tnuha updated https://github.com/llvm/llvm-project/pull/177522

>From c2ca2c419dc1241fa1e39c69df6c42324b813da2 Mon Sep 17 00:00:00 2001
From: ash <ashkernel02 at gmail.com>
Date: Thu, 22 Jan 2026 15:19:36 -0700
Subject: [PATCH 01/13] [libc][math] Refactor sincos implementation to header
 only in src/__support/math directory. Part of llvm#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
---
 libc/shared/math.h                     |   1 +
 libc/shared/math/sincos.h              |  23 +++
 libc/src/__support/math/CMakeLists.txt |  21 +++
 libc/src/__support/math/sincos.h       | 232 +++++++++++++++++++++++++
 libc/src/math/generic/sincos.cpp       | 210 +---------------------
 5 files changed, 279 insertions(+), 208 deletions(-)
 create mode 100644 libc/shared/math/sincos.h
 create mode 100644 libc/src/__support/math/sincos.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 6b66c5e75e457..a9c356cf8708c 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -78,5 +78,6 @@
 #include "math/rsqrtf.h"
 #include "math/rsqrtf16.h"
 #include "math/sin.h"
+#include "math/sincos.h"
 
 #endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/sincos.h b/libc/shared/math/sincos.h
new file mode 100644
index 0000000000000..fec49a02feb12
--- /dev/null
+++ b/libc/shared/math/sincos.h
@@ -0,0 +1,23 @@
+//===-- Shared sincos 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_SINCOS_H
+#define LLVM_LIBC_SHARED_MATH_SINCOS_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sincos.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sincos;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SINCOS_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 0e7a082434fb5..5e9a1d76ef875 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1053,6 +1053,27 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  sincos
+  HDRS
+    sincos.h
+  DEPENDS
+    libc.hdr.errno_macros
+    src.__support.FPUtil.FEnvImpl
+    src.__support.FPUtil.FPBits
+    src.__support.FPUtil.double_double
+    src.__support.FPUtil.dyadic_float
+    src.__support.FPUtil.except_value_utils
+    src.__support.FPUtil.multiply_add
+    src.__support.FPUtil.rounding_mode
+    src.__support.common
+    src.__support.macros.config
+    src.__support.macros.optimization
+    src.__support.macros.properties.cpu_features
+    src.__support.math.range_reduction_double_common
+    src.__support.math.sincos_eval
+)
+
 add_header_library(
   sincos_eval
   HDRS
diff --git a/libc/src/__support/math/sincos.h b/libc/src/__support/math/sincos.h
new file mode 100644
index 0000000000000..4e4be20e2b30f
--- /dev/null
+++ b/libc/src/__support/math/sincos.h
@@ -0,0 +1,232 @@
+//===-- Implementation header for sincos ------------------------*- 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_SINCOS_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SINCOS_H
+
+#include "hdr/errno_macros.h"
+#include "range_reduction_double_common.h"
+#include "sincos_eval.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"            // LIBC_UNLIKELY
+#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
+
+#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+#include "src/__support/math/range_reduction_double_fma.h"
+#else
+#include "src/__support/math/range_reduction_double_nofma.h"
+#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+using DoubleDouble = fputil::DoubleDouble;
+using Float128 = typename fputil::DyadicFloat<128>;
+
+// LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) {
+static constexpr void sincos(double x, double *sin_x, double *cos_x) {
+  using namespace math::range_reduction_double_internal;
+  using FPBits = typename fputil::FPBits<double>;
+  FPBits xbits(x);
+
+  uint16_t x_e = xbits.get_biased_exponent();
+
+  DoubleDouble y;
+  unsigned k = 0;
+  LargeRangeReduction range_reduction_large{};
+
+  // |x| < 2^16
+  if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {
+    // |x| < 2^-7
+    if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 7)) {
+      // |x| < 2^-27
+      if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 27)) {
+        // Signed zeros.
+        if (LIBC_UNLIKELY(x == 0.0)) {
+          *sin_x = x;
+          *cos_x = 1.0;
+          return;
+        }
+
+        // For |x| < 2^-27, max(|sin(x) - x|, |cos(x) - 1|) < ulp(x)/2.
+#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+        *sin_x = fputil::multiply_add(x, -0x1.0p-54, x);
+        *cos_x = fputil::multiply_add(x, -x, 1.0);
+#else
+        *cos_x = fputil::round_result_slightly_down(1.0);
+
+        if (LIBC_UNLIKELY(x_e < 4)) {
+          int rounding_mode = fputil::quick_get_round();
+          if (rounding_mode == FE_TOWARDZERO ||
+              (xbits.sign() == Sign::POS && rounding_mode == FE_DOWNWARD) ||
+              (xbits.sign() == Sign::NEG && rounding_mode == FE_UPWARD))
+            *sin_x = FPBits(xbits.uintval() - 1).get_val();
+        }
+        *sin_x = fputil::multiply_add(x, -0x1.0p-54, x);
+#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+        return;
+      }
+      // No range reduction needed.
+      k = 0;
+      y.lo = 0.0;
+      y.hi = x;
+    } else {
+      // Small range reduction.
+      k = range_reduction_small(x, y);
+    }
+  } else {
+    // Inf or NaN
+    if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        *sin_x = *cos_x = FPBits::quiet_nan().get_val();
+        return;
+      }
+
+      // sin(+-Inf) = NaN
+      if (xbits.get_mantissa() == 0) {
+        fputil::set_errno_if_required(EDOM);
+        fputil::raise_except_if_required(FE_INVALID);
+      }
+      *sin_x = *cos_x = x + FPBits::quiet_nan().get_val();
+      return;
+    }
+
+    // Large range reduction.
+    k = range_reduction_large.fast(x, y);
+  }
+
+  DoubleDouble sin_y, cos_y;
+
+  [[maybe_unused]] double err =
+      math::sincos_eval_internal::sincos_eval(y, sin_y, cos_y);
+
+  // Look up sin(k * pi/128) and cos(k * pi/128)
+#ifdef LIBC_MATH_HAS_SMALL_TABLES
+  // Memory saving versions.  Use 65-entry table.
+  auto get_idx_dd = [](unsigned kk) -> DoubleDouble {
+    unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);
+    DoubleDouble ans = SIN_K_PI_OVER_128[idx];
+    if (kk & 128) {
+      ans.hi = -ans.hi;
+      ans.lo = -ans.lo;
+    }
+    return ans;
+  };
+  DoubleDouble sin_k = get_idx_dd(k);
+  DoubleDouble cos_k = get_idx_dd(k + 64);
+#else
+  // Fast look up version, but needs 256-entry table.
+  // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
+  DoubleDouble sin_k = SIN_K_PI_OVER_128[k & 255];
+  DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255];
+#endif // LIBC_MATH_HAS_SMALL_TABLES
+
+  DoubleDouble msin_k{-sin_k.lo, -sin_k.hi};
+
+  // After range reduction, k = round(x * 128 / pi) and y = x - k * (pi / 128).
+  // So k is an integer and -pi / 256 <= y <= pi / 256.
+  // Then sin(x) = sin((k * pi/128 + y)
+  //             = sin(y) * cos(k*pi/128) + cos(y) * sin(k*pi/128)
+  DoubleDouble sin_k_cos_y = fputil::quick_mult(cos_y, sin_k);
+  DoubleDouble cos_k_sin_y = fputil::quick_mult(sin_y, cos_k);
+  //      cos(x) = cos((k * pi/128 + y)
+  //             = cos(y) * cos(k*pi/128) - sin(y) * sin(k*pi/128)
+  DoubleDouble cos_k_cos_y = fputil::quick_mult(cos_y, cos_k);
+  DoubleDouble msin_k_sin_y = fputil::quick_mult(sin_y, msin_k);
+
+  DoubleDouble sin_dd =
+      fputil::exact_add<false>(sin_k_cos_y.hi, cos_k_sin_y.hi);
+  DoubleDouble cos_dd =
+      fputil::exact_add<false>(cos_k_cos_y.hi, msin_k_sin_y.hi);
+  sin_dd.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
+  cos_dd.lo += msin_k_sin_y.lo + cos_k_cos_y.lo;
+
+#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+  *sin_x = sin_dd.hi + sin_dd.lo;
+  *cos_x = cos_dd.hi + cos_dd.lo;
+  return;
+#else
+  // Accurate test and pass for correctly rounded implementation.
+
+  double sin_lp = sin_dd.lo + err;
+  double sin_lm = sin_dd.lo - err;
+  double cos_lp = cos_dd.lo + err;
+  double cos_lm = cos_dd.lo - err;
+
+  double sin_upper = sin_dd.hi + sin_lp;
+  double sin_lower = sin_dd.hi + sin_lm;
+  double cos_upper = cos_dd.hi + cos_lp;
+  double cos_lower = cos_dd.hi + cos_lm;
+
+  // Ziv's rounding test.
+  if (LIBC_LIKELY(sin_upper == sin_lower && cos_upper == cos_lower)) {
+    *sin_x = sin_upper;
+    *cos_x = cos_upper;
+    return;
+  }
+
+  Float128 u_f128, sin_u, cos_u;
+  if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT))
+    u_f128 = range_reduction_small_f128(x);
+  else
+    u_f128 = range_reduction_large.accurate();
+
+  math::sincos_eval_internal::sincos_eval(u_f128, sin_u, cos_u);
+
+  auto get_sin_k = [](unsigned kk) -> Float128 {
+    unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);
+    Float128 ans = SIN_K_PI_OVER_128_F128[idx];
+    if (kk & 128)
+      ans.sign = Sign::NEG;
+    return ans;
+  };
+
+  // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
+  Float128 sin_k_f128 = get_sin_k(k);
+  Float128 cos_k_f128 = get_sin_k(k + 64);
+  Float128 msin_k_f128 = get_sin_k(k + 128);
+
+  // TODO: Add assertion if Ziv's accuracy tests fail in debug mode.
+  // https://github.com/llvm/llvm-project/issues/96452.
+
+  if (sin_upper == sin_lower)
+    *sin_x = sin_upper;
+  else
+    // sin(x) = sin((k * pi/128 + u)
+    //        = sin(u) * cos(k*pi/128) + cos(u) * sin(k*pi/128)
+    *sin_x = static_cast<double>(
+        fputil::quick_add(fputil::quick_mul(sin_k_f128, cos_u),
+                          fputil::quick_mul(cos_k_f128, sin_u)));
+
+  if (cos_upper == cos_lower)
+    *cos_x = cos_upper;
+  else
+    // cos(x) = cos((k * pi/128 + u)
+    //        = cos(u) * cos(k*pi/128) - sin(u) * sin(k*pi/128)
+    *cos_x = static_cast<double>(
+        fputil::quick_add(fputil::quick_mul(cos_k_f128, cos_u),
+                          fputil::quick_mul(msin_k_f128, sin_u)));
+
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINCOS_H
diff --git a/libc/src/math/generic/sincos.cpp b/libc/src/math/generic/sincos.cpp
index 38661deaa886a..346531ea53255 100644
--- a/libc/src/math/generic/sincos.cpp
+++ b/libc/src/math/generic/sincos.cpp
@@ -6,218 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "src/__support/math/sincos.h"
 #include "src/math/sincos.h"
-#include "hdr/errno_macros.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
-#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/double_double.h"
-#include "src/__support/FPUtil/dyadic_float.h"
-#include "src/__support/FPUtil/except_value_utils.h"
-#include "src/__support/FPUtil/multiply_add.h"
-#include "src/__support/FPUtil/rounding_mode.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/optimization.h"            // LIBC_UNLIKELY
-#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
-#include "src/__support/math/range_reduction_double_common.h"
-#include "src/__support/math/sincos_eval.h"
-
-#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-#include "src/__support/math/range_reduction_double_fma.h"
-#else
-#include "src/__support/math/range_reduction_double_nofma.h"
-#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
 
 namespace LIBC_NAMESPACE_DECL {
-
-using DoubleDouble = fputil::DoubleDouble;
-using Float128 = typename fputil::DyadicFloat<128>;
-
 LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) {
-  using namespace math::range_reduction_double_internal;
-  using FPBits = typename fputil::FPBits<double>;
-  FPBits xbits(x);
-
-  uint16_t x_e = xbits.get_biased_exponent();
-
-  DoubleDouble y;
-  unsigned k;
-  LargeRangeReduction range_reduction_large{};
-
-  // |x| < 2^16
-  if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {
-    // |x| < 2^-7
-    if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 7)) {
-      // |x| < 2^-27
-      if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 27)) {
-        // Signed zeros.
-        if (LIBC_UNLIKELY(x == 0.0)) {
-          *sin_x = x;
-          *cos_x = 1.0;
-          return;
-        }
-
-        // For |x| < 2^-27, max(|sin(x) - x|, |cos(x) - 1|) < ulp(x)/2.
-#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-        *sin_x = fputil::multiply_add(x, -0x1.0p-54, x);
-        *cos_x = fputil::multiply_add(x, -x, 1.0);
-#else
-        *cos_x = fputil::round_result_slightly_down(1.0);
-
-        if (LIBC_UNLIKELY(x_e < 4)) {
-          int rounding_mode = fputil::quick_get_round();
-          if (rounding_mode == FE_TOWARDZERO ||
-              (xbits.sign() == Sign::POS && rounding_mode == FE_DOWNWARD) ||
-              (xbits.sign() == Sign::NEG && rounding_mode == FE_UPWARD))
-            *sin_x = FPBits(xbits.uintval() - 1).get_val();
-        }
-        *sin_x = fputil::multiply_add(x, -0x1.0p-54, x);
-#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-        return;
-      }
-      // No range reduction needed.
-      k = 0;
-      y.lo = 0.0;
-      y.hi = x;
-    } else {
-      // Small range reduction.
-      k = range_reduction_small(x, y);
-    }
-  } else {
-    // Inf or NaN
-    if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {
-      if (xbits.is_signaling_nan()) {
-        fputil::raise_except_if_required(FE_INVALID);
-        *sin_x = *cos_x = FPBits::quiet_nan().get_val();
-        return;
-      }
-
-      // sin(+-Inf) = NaN
-      if (xbits.get_mantissa() == 0) {
-        fputil::set_errno_if_required(EDOM);
-        fputil::raise_except_if_required(FE_INVALID);
-      }
-      *sin_x = *cos_x = x + FPBits::quiet_nan().get_val();
-      return;
-    }
-
-    // Large range reduction.
-    k = range_reduction_large.fast(x, y);
-  }
-
-  DoubleDouble sin_y, cos_y;
-
-  [[maybe_unused]] double err =
-      math::sincos_eval_internal::sincos_eval(y, sin_y, cos_y);
-
-  // Look up sin(k * pi/128) and cos(k * pi/128)
-#ifdef LIBC_MATH_HAS_SMALL_TABLES
-  // Memory saving versions.  Use 65-entry table.
-  auto get_idx_dd = [](unsigned kk) -> DoubleDouble {
-    unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);
-    DoubleDouble ans = SIN_K_PI_OVER_128[idx];
-    if (kk & 128) {
-      ans.hi = -ans.hi;
-      ans.lo = -ans.lo;
-    }
-    return ans;
-  };
-  DoubleDouble sin_k = get_idx_dd(k);
-  DoubleDouble cos_k = get_idx_dd(k + 64);
-#else
-  // Fast look up version, but needs 256-entry table.
-  // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
-  DoubleDouble sin_k = SIN_K_PI_OVER_128[k & 255];
-  DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255];
-#endif // LIBC_MATH_HAS_SMALL_TABLES
-
-  DoubleDouble msin_k{-sin_k.lo, -sin_k.hi};
-
-  // After range reduction, k = round(x * 128 / pi) and y = x - k * (pi / 128).
-  // So k is an integer and -pi / 256 <= y <= pi / 256.
-  // Then sin(x) = sin((k * pi/128 + y)
-  //             = sin(y) * cos(k*pi/128) + cos(y) * sin(k*pi/128)
-  DoubleDouble sin_k_cos_y = fputil::quick_mult(cos_y, sin_k);
-  DoubleDouble cos_k_sin_y = fputil::quick_mult(sin_y, cos_k);
-  //      cos(x) = cos((k * pi/128 + y)
-  //             = cos(y) * cos(k*pi/128) - sin(y) * sin(k*pi/128)
-  DoubleDouble cos_k_cos_y = fputil::quick_mult(cos_y, cos_k);
-  DoubleDouble msin_k_sin_y = fputil::quick_mult(sin_y, msin_k);
-
-  DoubleDouble sin_dd =
-      fputil::exact_add<false>(sin_k_cos_y.hi, cos_k_sin_y.hi);
-  DoubleDouble cos_dd =
-      fputil::exact_add<false>(cos_k_cos_y.hi, msin_k_sin_y.hi);
-  sin_dd.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
-  cos_dd.lo += msin_k_sin_y.lo + cos_k_cos_y.lo;
-
-#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-  *sin_x = sin_dd.hi + sin_dd.lo;
-  *cos_x = cos_dd.hi + cos_dd.lo;
-  return;
-#else
-  // Accurate test and pass for correctly rounded implementation.
-
-  double sin_lp = sin_dd.lo + err;
-  double sin_lm = sin_dd.lo - err;
-  double cos_lp = cos_dd.lo + err;
-  double cos_lm = cos_dd.lo - err;
-
-  double sin_upper = sin_dd.hi + sin_lp;
-  double sin_lower = sin_dd.hi + sin_lm;
-  double cos_upper = cos_dd.hi + cos_lp;
-  double cos_lower = cos_dd.hi + cos_lm;
-
-  // Ziv's rounding test.
-  if (LIBC_LIKELY(sin_upper == sin_lower && cos_upper == cos_lower)) {
-    *sin_x = sin_upper;
-    *cos_x = cos_upper;
-    return;
-  }
-
-  Float128 u_f128, sin_u, cos_u;
-  if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT))
-    u_f128 = range_reduction_small_f128(x);
-  else
-    u_f128 = range_reduction_large.accurate();
-
-  math::sincos_eval_internal::sincos_eval(u_f128, sin_u, cos_u);
-
-  auto get_sin_k = [](unsigned kk) -> Float128 {
-    unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);
-    Float128 ans = SIN_K_PI_OVER_128_F128[idx];
-    if (kk & 128)
-      ans.sign = Sign::NEG;
-    return ans;
-  };
-
-  // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
-  Float128 sin_k_f128 = get_sin_k(k);
-  Float128 cos_k_f128 = get_sin_k(k + 64);
-  Float128 msin_k_f128 = get_sin_k(k + 128);
-
-  // TODO: Add assertion if Ziv's accuracy tests fail in debug mode.
-  // https://github.com/llvm/llvm-project/issues/96452.
-
-  if (sin_upper == sin_lower)
-    *sin_x = sin_upper;
-  else
-    // sin(x) = sin((k * pi/128 + u)
-    //        = sin(u) * cos(k*pi/128) + cos(u) * sin(k*pi/128)
-    *sin_x = static_cast<double>(
-        fputil::quick_add(fputil::quick_mul(sin_k_f128, cos_u),
-                          fputil::quick_mul(cos_k_f128, sin_u)));
-
-  if (cos_upper == cos_lower)
-    *cos_x = cos_upper;
-  else
-    // cos(x) = cos((k * pi/128 + u)
-    //        = cos(u) * cos(k*pi/128) - sin(u) * sin(k*pi/128)
-    *cos_x = static_cast<double>(
-        fputil::quick_add(fputil::quick_mul(cos_k_f128, cos_u),
-                          fputil::quick_mul(msin_k_f128, sin_u)));
-
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+  return math::sincos(x, sin_x, cos_x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL

>From 9122894efe1dc014393a5ebfc88e54f715c59fc5 Mon Sep 17 00:00:00 2001
From: ash <ashkernel02 at gmail.com>
Date: Fri, 23 Jan 2026 08:08:18 -0700
Subject: [PATCH 02/13] inline, comment removed

---
 libc/src/__support/math/sincos.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/math/sincos.h b/libc/src/__support/math/sincos.h
index 4e4be20e2b30f..b07149d11034b 100644
--- a/libc/src/__support/math/sincos.h
+++ b/libc/src/__support/math/sincos.h
@@ -37,8 +37,8 @@ namespace math {
 using DoubleDouble = fputil::DoubleDouble;
 using Float128 = typename fputil::DyadicFloat<128>;
 
-// LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) {
-static constexpr void sincos(double x, double *sin_x, double *cos_x) {
+LIBC_INLINE static constexpr void sincos(double x, double *sin_x,
+                                         double *cos_x) {
   using namespace math::range_reduction_double_internal;
   using FPBits = typename fputil::FPBits<double>;
   FPBits xbits(x);

>From 5aabdd696db9b5ca31f38b675c9c902ae2980029 Mon Sep 17 00:00:00 2001
From: ash <ashkernel02 at gmail.com>
Date: Fri, 23 Jan 2026 08:22:24 -0700
Subject: [PATCH 03/13] generic CMakeLists, manually updated BUILD.bazel

---
 libc/src/math/generic/CMakeLists.txt          | 12 +---------
 .../llvm-project-overlay/libc/BUILD.bazel     | 24 +++++++++++++++----
 2 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index bf799ce41363c..3a020b8ae65a5 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -413,17 +413,7 @@ add_entrypoint_object(
   HDRS
     ../sincos.h
   DEPENDS
-    libc.src.__support.math.range_reduction_double
-    libc.src.__support.math.sincos_eval
-    libc.hdr.errno_macros
-    libc.src.errno.errno
-    libc.src.__support.FPUtil.double_double
-    libc.src.__support.FPUtil.dyadic_float
-    libc.src.__support.FPUtil.except_value_utils
-    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.sincos
 )
 
 add_entrypoint_object(
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 7251f491bc757..4139bd384120c 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3259,6 +3259,24 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_sincos",
+    hdrs = ["src/__support/math/sincos.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_double_double",
+        ":__support_fputil_dyadic_float",
+        ":__support_fputil_except_value_utils",
+        ":__support_fputil_multiply_add",
+        ":__support_fputil_rounding_mode",
+        ":__support_macros_optimization",
+        ":__support_macros_properties_cpu_features",
+        ":__support_macros_config"
+    ],
+)
+
 libc_support_library(
     name = "__support_sincos_eval",
     hdrs = [
@@ -4927,11 +4945,7 @@ libc_math_function(
 libc_math_function(
     name = "sincos",
     additional_deps = [
-        ":__support_fputil_multiply_add",
-        ":__support_macros_optimization",
-        ":__support_macros_properties_cpu_features",
-        ":__support_range_reduction_double",
-        ":__support_sincos_eval",
+        ":__support_math_sincos",
     ],
 )
 

>From a5ded227bf12dd35128e77c6829bcb67b25d6541 Mon Sep 17 00:00:00 2001
From: Ash <97464181+tnuha at users.noreply.github.com>
Date: Fri, 23 Jan 2026 22:13:05 +0000
Subject: [PATCH 04/13] Update libc/src/math/generic/sincos.cpp

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/src/math/generic/sincos.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/math/generic/sincos.cpp b/libc/src/math/generic/sincos.cpp
index 346531ea53255..7306188650167 100644
--- a/libc/src/math/generic/sincos.cpp
+++ b/libc/src/math/generic/sincos.cpp
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/__support/math/sincos.h"
 #include "src/math/sincos.h"
+#include "src/__support/math/sincos.h"
 
 namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) {

>From 3b795ea599014daedce66625ca6351df5fb062aa Mon Sep 17 00:00:00 2001
From: Ash <97464181+tnuha at users.noreply.github.com>
Date: Fri, 23 Jan 2026 22:13:15 +0000
Subject: [PATCH 05/13] Update libc/src/math/generic/CMakeLists.txt

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/src/math/generic/CMakeLists.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3a020b8ae65a5..745942a14f4fa 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -414,6 +414,7 @@ add_entrypoint_object(
     ../sincos.h
   DEPENDS
     libc.src.__support.math.sincos
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(

>From bd6ecb7cc05b3a0843296de92fe1826b7997009d Mon Sep 17 00:00:00 2001
From: Ash <97464181+tnuha at users.noreply.github.com>
Date: Fri, 23 Jan 2026 22:13:28 +0000
Subject: [PATCH 06/13] Update libc/src/__support/math/sincos.h

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/src/__support/math/sincos.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/libc/src/__support/math/sincos.h b/libc/src/__support/math/sincos.h
index b07149d11034b..677ebfc480e53 100644
--- a/libc/src/__support/math/sincos.h
+++ b/libc/src/__support/math/sincos.h
@@ -34,11 +34,10 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-using DoubleDouble = fputil::DoubleDouble;
-using Float128 = typename fputil::DyadicFloat<128>;
-
 LIBC_INLINE static constexpr void sincos(double x, double *sin_x,
                                          double *cos_x) {
+  using DoubleDouble = fputil::DoubleDouble;
+  using Float128 = typename fputil::DyadicFloat<128>;
   using namespace math::range_reduction_double_internal;
   using FPBits = typename fputil::FPBits<double>;
   FPBits xbits(x);

>From e20a483e1ee4e27ae7bd5a42d5a718cfccc0795f Mon Sep 17 00:00:00 2001
From: Ash <97464181+tnuha at users.noreply.github.com>
Date: Fri, 23 Jan 2026 22:13:41 +0000
Subject: [PATCH 07/13] Update libc/src/__support/math/CMakeLists.txt

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/src/__support/math/CMakeLists.txt | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 5e9a1d76ef875..ee493fcda57eb 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1058,20 +1058,16 @@ add_header_library(
   HDRS
     sincos.h
   DEPENDS
+    .range_reduction_double
+    .sincos_eval
     libc.hdr.errno_macros
-    src.__support.FPUtil.FEnvImpl
-    src.__support.FPUtil.FPBits
-    src.__support.FPUtil.double_double
-    src.__support.FPUtil.dyadic_float
-    src.__support.FPUtil.except_value_utils
-    src.__support.FPUtil.multiply_add
-    src.__support.FPUtil.rounding_mode
-    src.__support.common
-    src.__support.macros.config
-    src.__support.macros.optimization
-    src.__support.macros.properties.cpu_features
-    src.__support.math.range_reduction_double_common
-    src.__support.math.sincos_eval
+    libc.src.__support.FPUtil.double_double
+    libc.src.__support.FPUtil.dyadic_float
+    libc.src.__support.FPUtil.except_value_utils
+    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(

>From cce0e46aec2d9bfdaa243b4d80124fb07539043a Mon Sep 17 00:00:00 2001
From: ash <ashkernel02 at gmail.com>
Date: Sat, 24 Jan 2026 20:23:02 -0700
Subject: [PATCH 08/13] update shared

---
 libc/test/shared/CMakeLists.txt       | 1 +
 libc/test/shared/shared_math_test.cpp | 4 ++++
 2 files changed, 5 insertions(+)

diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 3be8f992aa486..aa65be4aafec3 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -74,4 +74,5 @@ add_fp_unittest(
     libc.src.__support.math.rsqrtf
     libc.src.__support.math.rsqrtf16
     libc.src.__support.math.sin
+    libc.src.__support.math.sincos
 )
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 655d96692f1b8..b830c7e7e6991 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -87,6 +87,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
+  double sin, cos;
+  LIBC_NAMESPACE::shared::sincos(0.0, &sin, &cos);
   EXPECT_FP_EQ(0x1.921fb54442d18p+0, LIBC_NAMESPACE::shared::acos(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::asin(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::atan(0.0));
@@ -101,6 +103,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::fsqrt(0.0));
   EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log(1.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0));
+  EXPECT_FP_EQ(1.0, cos);
+  EXPECT_FP_EQ(0.0, sin);
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {

>From 7ae2dd56bd19e5989f0b923d91dda0a25741b40f Mon Sep 17 00:00:00 2001
From: Ash <97464181+tnuha at users.noreply.github.com>
Date: Tue, 27 Jan 2026 03:11:01 +0000
Subject: [PATCH 09/13] Update libc/src/__support/math/sincos.h

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/src/__support/math/sincos.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/math/sincos.h b/libc/src/__support/math/sincos.h
index 677ebfc480e53..ecfea0c70c315 100644
--- a/libc/src/__support/math/sincos.h
+++ b/libc/src/__support/math/sincos.h
@@ -25,9 +25,9 @@
 #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
 
 #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-#include "src/__support/math/range_reduction_double_fma.h"
+#include "range_reduction_double_fma.h"
 #else
-#include "src/__support/math/range_reduction_double_nofma.h"
+#include "range_reduction_double_nofma.h"
 #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
 
 namespace LIBC_NAMESPACE_DECL {

>From aead1f3fb4d876ba4aadc849ca4d0834ea7d108e Mon Sep 17 00:00:00 2001
From: Ash <97464181+tnuha at users.noreply.github.com>
Date: Tue, 27 Jan 2026 03:11:16 +0000
Subject: [PATCH 10/13] Update libc/src/__support/math/sincos.h

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 libc/src/__support/math/sincos.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/math/sincos.h b/libc/src/__support/math/sincos.h
index ecfea0c70c315..9d7eb9fba194e 100644
--- a/libc/src/__support/math/sincos.h
+++ b/libc/src/__support/math/sincos.h
@@ -34,7 +34,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE static constexpr void sincos(double x, double *sin_x,
+LIBC_INLINE static void sincos(double x, double *sin_x,
                                          double *cos_x) {
   using DoubleDouble = fputil::DoubleDouble;
   using Float128 = typename fputil::DyadicFloat<128>;

>From e55d1e2a101113454b47dcfce5ad49a04f40b042 Mon Sep 17 00:00:00 2001
From: ash <ashkernel02 at gmail.com>
Date: Mon, 26 Jan 2026 20:27:12 -0700
Subject: [PATCH 11/13] math dependencies

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index de7812639d793..4015604eae2d2 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3417,7 +3417,9 @@ libc_support_library(
         ":__support_fputil_rounding_mode",
         ":__support_macros_optimization",
         ":__support_macros_properties_cpu_features",
-        ":__support_macros_config"
+        ":__support_macros_config",
+        ":__support_sincos_eval",
+        ":range_reduction_double_common",
     ],
 )
 

>From 3906a69213fc3f9046547535b064c4b433c7aa8f Mon Sep 17 00:00:00 2001
From: Ash <97464181+tnuha at users.noreply.github.com>
Date: Tue, 27 Jan 2026 05:08:45 +0000
Subject: [PATCH 12/13] Update
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Co-authored-by: Muhammad Bassiouni <60100307+bassiounix at users.noreply.github.com>
---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 +
 1 file changed, 1 insertion(+)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 22dc64000e0cc..61cc5a0c160ea 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5193,6 +5193,7 @@ libc_math_function(
     name = "sincos",
     additional_deps = [
         ":__support_math_sincos",
+        ":errno",
     ],
 )
 

>From 085441bd09fcde046742af6a2126d6643847f5e9 Mon Sep 17 00:00:00 2001
From: ash <ashkernel02 at gmail.com>
Date: Mon, 26 Jan 2026 22:12:03 -0700
Subject: [PATCH 13/13] support range reduction double common

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 61cc5a0c160ea..dceac92bd3a0c 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3467,7 +3467,7 @@ libc_support_library(
         ":__support_macros_properties_cpu_features",
         ":__support_macros_config",
         ":__support_sincos_eval",
-        ":range_reduction_double_common",
+        ":__support_range_reduction_double_common",
     ],
 )
 



More information about the libc-commits mailing list