[llvm-branch-commits] [libc] d3b23d8 - [libc][math] Fix fast path with small inputs for double precision sin/cos. (#221012)

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Sep 30 02:07:48 PDT 2026


Author: lntue
Date: 2026-09-30T11:07:29+02:00
New Revision: d3b23d8b0c191783734c7bf1d5f0f77464412760

URL: https://github.com/llvm/llvm-project/commit/d3b23d8b0c191783734c7bf1d5f0f77464412760
DIFF: https://github.com/llvm/llvm-project/commit/d3b23d8b0c191783734c7bf1d5f0f77464412760.diff

LOG: [libc][math] Fix fast path with small inputs for double precision sin/cos. (#221012)

https://github.com/llvm/llvm-project/pull/201748 added a fast branch for
small inputs to double precision sin/cos,
but the Ziv error bounds were too tight.

Fixes in this PR:
- Updating the Ziv error bounds to accommodate rounding errors.
- Copy binary64 sin/cos hard-to-round cases from the CORE-MATH project.
- Add python tool to convert textual hard to round cases to binary
files, and add those binary files.
- Add hard-to-round tests for double precision sin/cos in the exhaustive
test folder.

Fixes https://github.com/llvm/llvm-project/issues/220934.

Assisted-by: Gemini is used to generate the worst-case text-to-binary
tool.

(cherry picked from commit dbac421efa19a5901f2511958040e18e8188b112)

Added: 
    

Modified: 
    libc/src/__support/math/cos.h
    libc/src/__support/math/sin.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/math/cos.h b/libc/src/__support/math/cos.h
index 427132454a2e3..bd63d8d5d87cf 100644
--- a/libc/src/__support/math/cos.h
+++ b/libc/src/__support/math/cos.h
@@ -1,10 +1,15 @@
-//===-- Implementation header for cos ---------------------------*- 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for cos.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COS_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_COS_H
@@ -116,8 +121,9 @@ LIBC_INLINE double cos(double x) {
 #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
       return 1.0 + r_lo;
 #else
-      // Overall errors <= ulp(x^2/2) + 2^-69.
-      double err = fputil::multiply_add(x_sq, 0x1.0p-53, 0x1.0p-69);
+      // Overall errors <= 1.0 * ulp(x^2) + 2^-69 for default rounding mode
+      //                <= 2.0 * ulp(x^2) + 2^-69 for directed rounding mode.
+      double err = fputil::multiply_add(x_sq, 0x1.0p-51, 0x1.0p-69);
       double r_lo_u = r_lo + err;
       double r_lo_l = r_lo - err;
       double r_upper = 1.0 + r_lo_u;

diff  --git a/libc/src/__support/math/sin.h b/libc/src/__support/math/sin.h
index 0c8933c10fd31..5038ed55fb006 100644
--- a/libc/src/__support/math/sin.h
+++ b/libc/src/__support/math/sin.h
@@ -1,10 +1,15 @@
-//===-- Implementation header for sin ---------------------------*- 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for sin.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SIN_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_SIN_H
@@ -127,8 +132,8 @@ LIBC_INLINE double sin(double x) {
 #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
       return x + r_lo;
 #else
-      // Overall errors <= 2 * ulp(x^3/6) + |x| * 2^-68.
-      double err = fputil::multiply_add(x_sq, 0x1.0p-53, 0x1.0p-68);
+      // Overall errors <= ulp(x^3) + |x| * 2^-68.
+      double err = fputil::multiply_add(x_sq, 0x1.0p-52, 0x1.0p-68);
       double r_lo_u = fputil::multiply_add(x, err, r_lo);
       double r_lo_l = fputil::multiply_add(-x, err, r_lo);
       double r_upper = x + r_lo_u;


        


More information about the llvm-branch-commits mailing list