[llvm-branch-commits] [libc] [libc][math] Fix fast path with small inputs for double precision sin/cos (#221012) (PR #225294)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Sep 29 04:51:21 PDT 2026
https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/225294
>From 5f9ace2d5846527eb79d6c86d4e7a23fddab08e7 Mon Sep 17 00:00:00 2001
From: lntue <lntue at google.com>
Date: Sun, 13 Sep 2026 22:49:38 -0400
Subject: [PATCH] [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)
---
libc/src/__support/math/cos.h | 12 +++++++++---
libc/src/__support/math/sin.h | 11 ++++++++---
2 files changed, 17 insertions(+), 6 deletions(-)
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