Skip to content

[libc][math] Fix fast path with small inputs for double precision sin/cos. - #221012

Merged
lntue merged 3 commits into
llvm:mainfrom
lntue:cos
Sep 14, 2026
Merged

lntue merged 3 commits into
llvm:mainfrom
lntue:cos

Conversation

@lntue

@lntue lntue commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

#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 #220934.

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

@lntue
lntue requested a review from a team as a code owner September 3, 2026 18:05
@lntue

lntue commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

@yklcs @zimmermann6: PTAL. Thanks!

@llvmorg-github-actions

llvmorg-github-actions Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-github-workflow

@llvm/pr-subscribers-libc

Author: lntue

Changes

#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 #220934.

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


Patch is 49.58 MiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/221012.diff

11 Files Affected:

  • (modified) libc/src/__support/math/cos.h (+3-2)
  • (modified) libc/src/__support/math/sin.h (+2-2)
  • (modified) libc/test/src/math/exhaustive/CMakeLists.txt (+44)
  • (added) libc/test/src/math/exhaustive/cos.bin ()
  • (added) libc/test/src/math/exhaustive/cos.wc (+1090178)
  • (added) libc/test/src/math/exhaustive/cos_test.cpp (+21)
  • (added) libc/test/src/math/exhaustive/sin.bin ()
  • (added) libc/test/src/math/exhaustive/sin.wc (+1091085)
  • (added) libc/test/src/math/exhaustive/sin_test.cpp (+21)
  • (added) libc/test/src/math/exhaustive/worst_case_test.h (+335)
  • (added) libc/utils/mathtools/wc_to_bin.py (+302)
diff --git a/libc/src/__support/math/cos.h b/libc/src/__support/math/cos.h
index 427132454a2e3..ec88f02a499fe 100644
--- a/libc/src/__support/math/cos.h
+++ b/libc/src/__support/math/cos.h
@@ -116,8 +116,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..8c16a9ac66d9b 100644
--- a/libc/src/__support/math/sin.h
+++ b/libc/src/__support/math/sin.h
@@ -127,8 +127,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;
diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index 46b5b138cfb11..0c338425a59df 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -20,6 +20,16 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
+add_header_library(
+  worst_case_test
+  HDRS
+    worst_case_test.h
+  DEPENDS
+    libc.src.__support.CPP.type_traits
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.macros.properties.types
+)
+
 add_fp_unittest(
   sqrtf_test
   NO_RUN_POSTBUILD
@@ -67,6 +77,23 @@ add_fp_unittest(
     -lpthread
 )
 
+add_fp_unittest(
+  sin_test
+  NEED_MPFR
+  SUITE
+    libc_math_exhaustive_tests
+  SRCS
+    sin_test.cpp
+  COMPILE_OPTIONS
+    "-DLIBC_MATH_EXHAUSTIVE_TEST_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\""
+  DEPENDS
+    .worst_case_test
+    libc.src.math.sin
+    libc.src.__support.FPUtil.fp_bits
+  LINK_LIBRARIES
+    -lpthread
+)
+
 add_fp_unittest(
   sinf_float_test
   NO_RUN_POSTBUILD
@@ -114,6 +141,23 @@ add_fp_unittest(
     -lpthread
 )
 
+add_fp_unittest(
+  cos_test
+  NEED_MPFR
+  SUITE
+    libc_math_exhaustive_tests
+  SRCS
+    cos_test.cpp
+  COMPILE_OPTIONS
+    "-DLIBC_MATH_EXHAUSTIVE_TEST_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\""
+  DEPENDS
+    .worst_case_test
+    libc.src.math.cos
+    libc.src.__support.FPUtil.fp_bits
+  LINK_LIBRARIES
+    -lpthread
+)
+
 add_fp_unittest(
   cosf_float_test
   NO_RUN_POSTBUILD
diff --git a/libc/test/src/math/exhaustive/cos.bin b/libc/test/src/math/exhaustive/cos.bin
new file mode 100644
index 0000000000000..adbe198eea018
Binary files /dev/null and b/libc/test/src/math/exhaustive/cos.bin differ
diff --git a/libc/test/src/math/exhaustive/cos.wc b/libc/test/src/math/exhaustive/cos.wc
new file mode 100644
index 0000000000000..b6cecad6daeaa
--- /dev/null
+++ b/libc/test/src/math/exhaustive/cos.wc
@@ -0,0 +1,1090178 @@
+# Copied from the CORE-MATH project:
+# Commit: 77792c2c3933e950ae6b058e65018376402f0692
+# Link: https://gitlab.inria.fr/core-math/core-math/-/blob/77792c2c3933e950ae6b058e65018376402f0692/src/binary64/cos/cos.wc
+#
+# worst cases from https://www.vinc17.net/research/testlibm/testlibm-data.xz
+# (update from 2020-11-27), from 0 to pi, with 46 to 54 identical bits after
+# the round bit
+0x1.0009effd4bedap-12
+0x1.00147eec5cfa5p+0
+0x1.0066bbcf2cf68p-7
+0x1.00786b0bf421p-2
+0x1.00a33764a0a83p-7
+0x1.00c82b482ed92p-10
+0x1.00fd9e73df0d7p-6
+0x1.00ff807f60deep-23
+0x1.012b5104d296ep-20
+0x1.0155dcbae56fdp-2
+0x1.018eea667029bp-8
+0x1.01d2bb8888acp-15
+0x1.0203b71ba100fp-1
+0x1.0240a42cca393p-7
+0x1.0255d06667798p-9
+0x1.0281750d15454p-13
+0x1.02eea46ed8ed8p-13
+0x1.0309272a36a6dp-5
+0x1.0309d23227c1cp-9
+0x1.033627ce2fa85p-17
+0x1.0346a25a58d14p-20
+0x1.0393a5192debdp-5
+0x1.03c9d2f89a3fp-19
+0x1.03f81f636b80cp-24
+0x1.03f81f636b80dp-24
+0x1.04033855e1075p-11
+0x1.0415e772526bap-18
+0x1.04256a58681fp-10
+0x1.0471adb4edfeep-5
+0x1.049f5db55a8d4p-7
+0x1.04f3bd03c0a67p-23
+0x1.05002513b4397p-11
+0x1.054f8fb7ecd2bp-10
+0x1.05871c6ef5f9ap-4
+0x1.0596c07259f41p-12
+0x1.05c4bdcd96862p-6
+0x1.05ddcaf5301fap-19
+0x1.0609c0277cc82p-9
+0x1.062a2f8ff7e5p+0
+0x1.062cee77c5b7p-22
+0x1.063c902c7efddp-7
+0x1.064844339302ap-20
+0x1.065be6e6ee03cp-1
+0x1.06902396a041ap-15
+0x1.069ac97bc4e2bp-15
+0x1.06beb681df3acp-3
+0x1.06d7d5ff711d7p-18
+0x1.06e825da8fc2ep-23
+0x1.0726700ceb224p-22
+0x1.07384ee71e351p-14
+0x1.07620aed5f3ebp-16
+0x1.0799a2750d20cp-16
+0x1.07b6816807de8p-18
+0x1.0802013515f0dp-14
+0x1.0813eb8986375p-1
+0x1.082e878937bc2p-21
+0x1.08389bcf5b135p-12
+0x1.08502b19e596cp-15
+0x1.089af94d250c8p-22
+0x1.08a2f7a471808p-14
+0x1.08d8dd3b1d9adp-23
+0x1.09030a9e04fe1p-5
+0x1.090379dc5cdf1p-2
+0x1.092e9866d37dfp-18
+0x1.0942eb6ac3945p-9
+0x1.098d979f9a611p-11
+0x1.09db6b32eacaap-20
+0x1.09dde79741019p-12
+0x1.09dfa0cd069a3p-7
+0x1.0a1cddb64182cp-21
+0x1.0a21a92dbc4e4p-16
+0x1.0a260a6d5e26p-3
+0x1.0a3b5537758b5p-6
+0x1.0a726e1fa2176p-19
+0x1.0a98217d380e2p-18
+0x1.0b035b10a76adp-22
+0x1.0b3a4d7b1a88dp-6
+0x1.0b557c0899ffdp-8
+0x1.0b55a4268b1b6p-4
+0x1.0b8508c1cd6cfp-13
+0x1.0b99ca1f1a3a9p-16
+0x1.0bbb307acafdbp-24
+0x1.0bbb307acafdcp-24
+0x1.0bc9c8aeb59p-10
+0x1.0bd74777db82bp-9
+0x1.0bfbedfb16285p+0
+0x1.0c5e882473c02p-9
+0x1.0c63457ea6c89p-21
+0x1.0c86069092edcp+0
+0x1.0ca62555f3db6p-15
+0x1.0ce1104d88f72p-20
+0x1.0cf74185fad62p-17
+0x1.0d222a177fc14p-7
+0x1.0d399c9118d15p-17
+0x1.0d55af1ee4d59p-2
+0x1.0d60c3c5475bcp-18
+0x1.0d6f70db8a684p-9
+0x1.0d726986bc5f6p-1
+0x1.0d7306ec97f65p-8
+0x1.0dccc7f11f3dcp-20
+0x1.0e1d3ff0d689fp-10
+0x1.0e6daf7b229cbp-13
+0x1.0e95a539f492fp-23
+0x1.0ee00d3b3271dp-17
+0x1.0eeb7f28b36f3p-4
+0x1.0efcae56c1c2fp-13
+0x1.0f16c6f632009p+0
+0x1.0f58b5ceb6ddp-7
+0x1.0f5ba326bf09fp-3
+0x1.0f6fef7efd034p-15
+0x1.0f876ccdf6cd6p-25
+0x1.0f876ccdf6cd7p-25
+0x1.0f876ccdf6cd8p-25
+0x1.0f876ccdf6cd9p-25
+0x1.0f876ccdf6cdap-25
+0x1.0f876ccdf6cdbp-25
+0x1.0f876ccdf6cdcp-25
+0x1.0f876ccdf6cddp-25
+0x1.0fa725e4c5992p-3
+0x1.0fb2d32b58231p-16
+0x1.0fb3ca5264e51p-8
+0x1.0fb6b9c0eec57p-1
+0x1.0fe46bc74befcp-16
+0x1.0fe54c3cb261fp-7
+0x1.103de347f44b2p-2
+0x1.10785dd689a2cp-23
+0x1.10cef3d50b378p-7
+0x1.111b1e895dbb6p-15
+0x1.113b853bb1c7bp-21
+0x1.118ba622af6c2p-12
+0x1.119fb3481dadcp-19
+0x1.11a08114c99bcp+0
+0x1.11ba803f22334p-1
+0x1.11ecf152ce294p-15
+0x1.11fba5383ae35p-10
+0x1.12504a83bf97cp-4
+0x1.1257c5187fd0cp-23
+0x1.125e4d33f452ap-19
+0x1.129ab23ef8e77p-4
+0x1.129d27053e897p-16
+0x1.12b8d27bbb801p-16
+0x1.12cb70c3db8edp-11
+0x1.132c344d41917p-20
+0x1.132d691f8e642p-11
+0x1.13463fa37014ep-24
+0x1.13463fa37014fp-24
+0x1.135e8fa6764d1p-8
+0x1.139257da38dd9p-13
+0x1.13e33d92f728bp-19
+0x1.14128c5cbc579p-20
+0x1.146de9a66c2e3p+0
+0x1.14a44438bc90ap-14
+0x1.14bda59e0b1ccp-5
+0x1.14e6131e7e992p-16
+0x1.150e52ce7b42fp-20
+0x1.156bdfe78d61cp-17
+0x1.157d0e8476c35p-14
+0x1.1580f32deb95ap-12
+0x1.15842cb04f9cep-2
+0x1.1585deb6ba74ap-5
+0x1.1587ffe289232p-16
+0x1.158c51479c47p-8
+0x1.1593414ad6304p-20
+0x1.159c927b3af6ap-10
+0x1.15b7074b3b174p-10
+0x1.15ffb712e0068p-2
+0x1.16bd721903c8cp-22
+0x1.16c7b71cbd4a5p-6
+0x1.16ce071e7bc41p-9
+0x1.16e534ee3658p-4
+0x1.1768fbd8bb775p-19
+0x1.176f1c575ed0ap-17
+0x1.17a82cbd9719ap-22
+0x1.1858dbf50d023p-18
+0x1.1868900d4ee7cp-6
+0x1.18e34bf8c4eb5p-19
+0x1.191146902560fp-5
+0x1.193d77e90efb3p-20
+0x1.1966b779ce28ep-16
+0x1.19b9281f6c9a3p-20
+0x1.19ee501ca2ed8p-4
+0x1.1a515594b91e4p-4
+0x1.1a56d90014f4fp-8
+0x1.1a63ca083948fp-22
+0x1.1a6adf0a3e3a1p-17
+0x1.1a9dc8f6df104p-24
+0x1.1a9dc8f6df105p-24
+0x1.1aa629d3661f8p-18
+0x1.1b018fd73b3afp-10
+0x1.1b7a780b78b7cp-20
+0x1.1b8178cdc4d46p-10
+0x1.1b854e0f496a4p-23
+0x1.1b894c120c0d1p-11
+0x1.1baf00b2f6b01p-1
+0x1.1bff52edc4f6bp-18
+0x1.1c2d9650ac5f2p+0
+0x1.1c89c8f144c14p-19
+0x1.1ce006f86c811p-7
+0x1.1d15b222972a6p-8
+0x1.1d34977ff1f78p-3
+0x1.1d3bd4c2d25bbp-6
+0x1.1d5224aae2ee5p-23
+0x1.1d89544e795ddp-11
+0x1.1d941e7a698aap-4
+0x1.1dbeda257d186p-18
+0x1.1defe2e717fe8p-21
+0x1.1e18d2ac6d0b7p-4
+0x1.1e1dc195bda88p-18
+0x1.1e33e5cf0d212p-20
+0x1.1e400c741b302p+0
+0x1.1e4cefa05247bp-12
+0x1.1e52449c31695p-7
+0x1.1e67b91f44e41p-13
+0x1.1e89a929dcfe4p-17
+0x1.1e93589c45b0bp-3
+0x1.1ebbdad3504afp-12
+0x1.1f002e82f934bp-12
+0x1.1f09b0dac465ep-13
+0x1.1f51924a145cfp-20
+0x1.1f66bac56a63p-18
+0x1.1f7ef6ffd562p+0
+0x1.1f9179036f10bp-18
+0x1.1fed159a1a7d1p-15
+0x1.1ff1c6c28756ap-21
+0x1.1fff16e5ba0e3p-2
+0x1.205339f203314p-5
+0x1.20dde7159134bp-5
+0x1.21547cffaf122p-9
+0x1.21745c6355307p-1
+0x1.217532b8faae7p+0
+0x1.21c5b70d9f824p-24
+0x1.21c5b70d9f825p-24
+0x1.21c6f0699a3fdp+0
+0x1.21fb96090e8d7p-19
+0x1.220ac2d73f088p-2
+0x1.22441458601b7p-11
+0x1.224ed17253cbdp-9
+0x1.22688446706d4p-12
+0x1.226f24f6b2a4ap-22
+0x1.22a788fc76de9p-23
+0x1.22aabfda0128bp-8
+0x1.22b826533dc99p+0
+0x1.22d0cef700b3dp-5
+0x1.22e592f36a441p-7
+0x1.2311f8b98c4dap-7
+0x1.2312bee38106dp-11
+0x1.231e7ee26822ap-11
+0x1.2321a2e3a75fp-7
+0x1.2348a840c4061p-3
+0x1.23611763fb2eap-17
+0x1.237a9ee850ebep-21
+0x1.23af5e894b6b6p-5
+0x1.2405b40296b02p-7
+0x1.242c3f47285ecp-16
+0x1.2431146c59d1ep-22
+0x1.2465868d778cep-4
+0x1.24844c6d40d56p-1
+0x1.24db30555b59fp-5
+0x1.24f0918070c1fp-2
+0x1.24f27b2c5bb13p-7
+0x1.2542754b8d66dp-6
+0x1.259a0dabed36cp-19
+0x1.261407e4db319p-10
+0x1.2674f5eaeb1dbp-11
+0x1.26851b567579fp-19
+0x1.26c10efaf424fp-21
+0x1.26c68a755cb6p-17
+0x1.26e74384d2ff2p-20
+0x1.270df70e010f7p-4
+0x1.273ebd3a4814fp-1
+0x1.27491078a6625p-9
+0x1.2792c2b861fcbp-2
+0x1.27af0c8a68e7bp-3
+0x1.27d34e8b8edf4p-8
+0x1.27dcb1c952737p-17
+0x1.2811ac720627fp-17
+0x1.2813ee0832006p-16
+0x1.28317fa63f278p-8
+0x1.28627e5124b25p-15
+0x1.288a409406f95p-22
+0x1.28954946ed8fcp-14
+0x1.28ac8a94e392ap-5
+0x1.28b84886ff618p-1
+0x1.28c17b9337835p-24
+0x1.28c43e16ac9c5p-9
+0x1.28f2eb01c4439p-3
+0x1.29262f89dd7d5p-17
+0x1.293c94f90c2ep-17
+0x1.2941a1b12c672p-15
+0x1.294ddc15fb767p-4
+0x1.299e011188579p-23
+0x1.29e2c94e6a932p-21
+0x1.2a42fa30fbce9p-22
+0x1.2a5d66148ebeep-5
+0x1.2ae9777fd976fp-9
+0x1.2b0899b756771p+0
+0x1.2b20f3e932ad6p-15
+0x1.2b2dbc26dc2fcp-9
+0x1.2b36032f4da93p-8
+0x1.2b5524ae12792p-23
+0x1.2b63f2b841a49p-17
+0x1.2b7543e4f33bfp-12
+0x1.2be3882b6c0d8p-3
+0x1.2c283a686a3d6p-6
+0x1.2c2d3afef0ea7p-11
+0x1.2c2fc595456a4p-25
+0x1.2c2fc595456a5p-25
+0x1.2c2fc595456a6p-25
+0x1.2c2fc595456a7p-25
+0x1.2c2fc595456a8p-25
+0x1.2c2fc595456a9p-25
+0x1.2c2fc595456aap-25
+0x1.2c716ce89ea7ep-14
+0x1.2ca23543864cap-5
+0x1.2cbb48e008bc3p-12
+0x1.2cf34c32c5dddp-12
+0x1.2d183c429d539p-19
+0x1.2d5f94c8ada4ap-8
+0x1.2d6b3e43fd3c7p-1
+0x1.2dace1e9bd0cbp-22
+0x1.2dacf636e1032p-1
+0x1.2dd14981b9371p-6
+0x1.2df6a1c35768ep-15
+0x1.2e00e6cd06767p-2
+0x1.2e0ae5857311dp+0
+0x1.2e25c3ff11312p-17
+0x1.2e25f298d8ccfp-15
+0x1.2e39e2747c612p-4
+0x1.2e7b919c1db02p-12
+0x1.2ea064a1429bep-13
+0x1.2ea625c2d6e47p-11
+0x1.2ebb1ad94a7fep-2
+0x1.2ebbf4fafdd4fp-23
+0x1.2f1c59cb1485ap-6
+0x1.2f69282620bfp-8
+0x1.2f8feb56b3ba9p-19
+0x1.2f9422c23c47fp-24
+0x1.2fbff941f2ee7p-20
+0x1.2fc714c71e63ep-15
+0x1.2fc793664b914p-14
+0x1.3035e0474fff7p-22
+0x1.305b15020a46ep-6
+0x1.306bb705ae7c8p-23
+0x1.30792b3878273p-21
+0x1.307b84c405717p-4
+0x1.30b781e0842ffp-16
+0x1.30bed7646b2aep+0
+0x1.3107b6cfe539p-9
+0x1.3112a8fa1cabap-16
+0x1.31460dc71e70cp-20
+0x1.315bb5c786675p-16
+0x1.31b3d50e6befep-19
+0x1.3211068b78e2dp-5
+0x1.3256bfd5915b2p-18
+0x1.330fa58afd01bp-16
+0x1.3327aafc5f676p-11
+0x1.3337fa10616ecp-15
+0x1.337248f501e1dp-15
+0x1.33c42213ee0cep-23
+0x1.3414b71172b27p-8
+0x1.34363b3a0a6e9p-15
+0x1.343f24bf8a3cp-20
+0x1.347167982efaap-14
+0x1.34954e8c4aeefp-5
+0x1.34c3e99692243p-20
+0x1.3561f5bf797bcp+0
+0x1.356cdebc9b5e7p-23
+0x1.357d450e2b016p-15
+0x1.35949056ed8d7p-4
+0x1.35b3a711dc35ap-1
+0x1.35c1fe2237f1cp+0
+0x1.3640630445306p-24
+0x1.3640630445307p-24
+0x1.366b5f5ddc8b7p-16
+0x1.36752d91e27fbp-22
+0x1.36ebd48ac6863p-21
+0x1.3737da81c30f1p-2
+0x1.3753a8fe33b86p-11
+0x1.375f2ef830a68p-14
+0x1.37b1311fb4936p-22
+0x1.38a17a50ca203p-19
+0x1.38d9c3b2b4accp-8
+0x1.3905fe4ea04d9p-16
+0x1.39220e302c85ep-10
+0x1.3936d54e1166ep-9
+0x1.394436ba0a65dp-17
+0x1.394e47655bfcap-11
+0x1.395f131813174p-17
+0x1.397e67150be9ap-10
+0x1.39859d24894f2p-20
+0x1.399d22fbf571cp-17
+0x1.399fbd1103091p-20
+0x1.39b7cfc10d06p-15
+0x1.3a812ae77e3edp-13
+0x1.3a84c7fd4e55ap-3
+0x1.3aaa089690851p-15
+0x1.3aca9ac4d718dp+0
+0x1.3af5d7338517fp-22
+0x1.3b5014b5ccae6p+0
+0x1.3b5eee0617959p-8
+0x1.3b8aa628d15c5p-18
+0x1.3bc5a65c0de88p-22
+0x1.3bd47cfa7cd96p-7
+0x1.3c07e72107f3ap-4
+0x1.3c2d5ab84bc6ap-22
+0x1.3c3a74f16ea86p-10
+0x1.3c4cd9b2af155p-14
+0x1.3cc8a99af5454p-24
+0x1.3cc8a99af5455p-24
+0x1.3cf7ae71a999p-10
+0x1.3d0aa45e70437p-14
+0x1.3d0c8431cda22p-20
+0x1.3d133b9f8a99ep-8
+0x1.3d63ac7e703fep-22
+0x1.3d86803bc471ep-4
+0x1.3d97474f76df7p-23
+0x1.3db525da16263p-13
+0x1.3dd65e4c465edp-12
+0x1.3dfd2710f8618p+0
+0x1.3e28f6b3a4bfap-8
+0x1.3e4eabac265fcp-13
+0x1.3e52acccd2a65p-18
+0x1.3e606ac68f6acp-6
+0x1.3e7c889f28ed5p-17
+0x1.3eee63d4237f7p-15
+0x1.3f0945b9c77e1p-7
+0x1.3f1de3fc8fd1bp-13
+0x1.3f2a4880cf004p-13
+0x1.3f32f17fe8d09p-23
+0x1.3f3cca4a31d1fp-11
+0x1.3fc985cc6d9adp-4
+0x1.3fd1bb2c76af3p-12
+0x1.4013664e50955p-12
+0x1.402e502aeacbp-12
+0x1.40621409fe6ebp-12
+0x1.4071abcf91d0ep-17
+0x1.40859b60ea732p-13
+0x1.40af40cec3cb6p-5
+0x1.40cc8b6d657c7p-23
+0x1.4129131bfba71p-14
+0x1.415503b361585p-14
+0x1.4155b8580a62cp-8
+0x1.41b544157164fp-5
+0x1.41bdd79630457p-8
+0x1.41bf5603f5ddp+0
+0x1.41d5df65736fbp-4
+0x1.41ef38a0d9b5fp-10
+0x1.420505dc634dp-11
+0x1.42550643e9932p-19
+0x1.4257ddd23d978p-13
+0x1.42641cf569577p-23
+0x1.4296eaf19f7ffp-22
+0x1.42b9ff9e6146p-8
+0x1.42bb6cb5f934fp-5
+0x1.430f568ab515fp-3
+0x1.432ef7d6894f7p-3
+0x1.432f24fb01c7bp-24
+0x1.432f24fb01c7cp-24
+0x1.4389cdff3d9dp-16
+0x1.438aa602291b4p-15
+0x1.439d2cfa5fd75p-19
+0x1.43ac699c67a35p-10
+0x1.43c7177258038p-22
+0x1.44a6045114759p-2
+0x1.44bc1f9b10332p-11
+0x1.457dc11333738p-15
+0x1.4588cefdd9ac1p-11
+0x1.4596b4e2ab3c8p-20
+0x1.45c1a864fbec2p+0
+0x1.45f8d9bdba327p-11
+0x1.460991ebc46dbp-17
+0x1.46178e11bbb0fp-21
+0x1.46241d691d79dp-22
+0x1.463d6847941efp-3
+0x1.465655f122ff4p-25
+0x1.465655f122ff5p-25
+0x1.465655f122ff6p-25
+0x1.465655f122ff7p-25
+0x1.465655f122ff8p-25
+0x1.465655f122ff9p-25
+0x1.466c7e6fefc1dp-18
+0x1.46d3ef5840eecp-4
+0x1.46dc2d029995bp-18
+0x1.471b3cb68ce6fp-16
+0x1.47516da434364p-17
+0x1.47712d7f8ed7ep-5
+0x1.479c69fd152abp-4
+0x1.47d367e590653p-15
+0x1.48228de97ee2p-1
+0x1.48260d7181265p-1
+0x1.4862215bd7688p-7
+0x1.487cc90b2b7p-22
+0x1.487f6549706c8p-12
+0x1.48e6a8a73f91ep-17
+0x1.49103b48e235ap-3
+0x1.4928e67e3718ep-2
+0x1.49440ebf7ab99p-22
+0x1.4947b0066f25cp-14
+0x1.4975cd5768089p-24
+0x1.49a087228cfep-19
+0x1.49c8554b076e5p-16
+0x1.4a0adc22eeedcp-22
+0x1.4a31c464a3a0cp-16
+0x1.4a38f03a476c9p-16
+0x1.4a3a76f14e88p-16
+0x1.4a4c7400dd5cep-5
+0x1.4a7068f9c96e7p-19
+0x1.4a987b06f2056p-16
+0x1.4abfff12ff688p-2
+0x1.4b1dc56db35cep-19
+0x1.4b7d09603edb3p-9
+0x1.4b97115860211p-22
+0x1.4b9d149c82ef4p-13
+0x1.4bc876ba7f6f2p-23
+0x1.4bd94ffa6c63ep-9
+0x1.4c302d61cc84ep-2
+0x1.4c68f1ac0cd68p-5
+0x1.4c739e9177d6ep-4
+0x1.4d15237611989p-21
+0x1.4d3bf6295c9ebp-11
+0x1.4dc063f1180aep-2
+0x1.4dd59d11cd66p-4
+0x1.4dd9ab059e799p-21
+0x1.4e3bc377b0eabp-21
+0x1.4ef61de25cfc3p-11
+0x1.4f191140bf584p-6
+0x1.4f8cfd94bf1bdp-2
+0x1.4f9237351a966p-21
+0x1.4f9e6bbc4ecb4p-24
+0x1.4f9e6bbc4ecb5p-24
+0x1.4fc537edfc15p-5
+0x1.4fdb840109affp-1
+0x1.5024904bd8e9bp-21
+0x1.50401c989b798p-9
+0x1.506177f5491c1p-23
+0x1.5065ddc80b191p-8
+0x1.507de94d91399p-1
+0x1.50904d66680c7p-14
+0x1.50bf1ce48537ep-14
+0x1.51391b9872e9dp-3
+0x1.517e952229c29p-4
+0x1.5193f76d73c29p-1
+0x1.51c1df061030bp-21
+0x1.521313ca416ecp-6
+0x1.5216b80a8c14p-22
+0x1.522e45e58fbbep+0
+0x1.526dc0bcb57a2p-19
+0x1.527e587b04c7ep-17
+0x1.52836595a7b0cp-13
+0x1.52b10cb4cd1b6p-20
+0x1.52cc4186a0d8p-21
+0x1.52dc1f542009ap-19
+0x1.5322aeef0edf4p-17
+0x1.5326eea9a6231p-15
+0x1.5339776247562p-6
+0x1.533c03bfe72c5p-20
+0x1.5355cea0e9453p-15
+0x1.5369480174816p-23
+0x1.53a295cd6db9fp-20
+0x1.53f7c8e92c8f4p-7
+0x1.54034e0c48356p-11
+0x1.540605f85d78bp-21
+0x1.542724e64d172p-20
+0x1.542cb69322f9ep-8
+0x1.54b695c047f9bp-18
+0x1.54c2335bc9a08p-8
+0x1.5504c91eb9e32p+0
+0x1.5523a70d1966fp-20
+0x1.5539ae8c09bf5p-16
+0x1.5568be5477317p-1
+0x1.557aa8a039247p-22
+0x1.55aaa002a9d5bp-24
+0x1.55aaa002a9d5cp-24
+0x1.55c52c5821a41p-17
+0x1.55fe80bec9781p-21
+0x1.560bf25c4fb09p-3
+0x1.563a7624c613bp-3
+0x1.56925d1659495p-10
+0x1.56e529af6a4f4p-14
+0x1.56e7778b6a3bp-6
+0x1.56fe432b02298p-16
+0x1.572436eddd27bp-5
+0x1.5727917f693aep-15
+0x1.575dc9847b593p-12
+0x1.57736c34d999ap-18
+0x1.57a11b096629p-6
+0x1.57bb3f94dcf32p-7
+0x1.57cd8b00e7276p-14
+0x1.57dbb3aaaf238p-16
+0x1.57ecde3cc78a7p-13
+0x1.580cbeee3b848p-2
+0x1.58a8a87867997p-9
+0x1.58ca2fbebdd2cp-21
+0x1.590a8656ae107p-14
+0x1.59597c02bffb6p-4
+0x1.59647cddf1cacp-23
+0x1.5966158e499fcp-12
+0x1.59a2f9f1c0c21p-12
+0x1.59ca078738629p-3
+0x1.5a2abac5b84c8p-16
+0x1.5a3a0c56e3271p-14
+0x1.5abeaf570881ep-20
+0x1.5b0c37daaa2c3p-17
+0x1.5b33aa15dffap-14
+0x1.5b504e3a7cb6ap-1
+0x1.5b788a6af3d01p-21
+0x1.5b901d181253cp-21
+0x1.5b9be5d52a9dcp-24
+0x1.5ba1f92eb76a4p-18
+0x1.5bc57c98660abp-9
+0x1.5bcb04cb65924p-22
+0x1.5c026409ac9ebp-18
+0x1.5c583b6f7ab0ap-23
+0x1.5c6c247696fa8p-12
+0x1.5c7bc0114b248p-15
+0x1.5cb550c840ed4p-11
+0x1.5cd9bf4a45dedp-5
+0x1.5ce0c9cacf94cp-15
+0x1.5d025985f7175p-11
+0x1.5d0abe01e9693p-14
+0x1.5d11da1850dbcp-11
+0x1.5d4ed1b09d3a4p-21
+0x1.5d6f90adf1279p-9
+0x1.5dddc0e277a97p-17
+0x1.5df2d554590fbp-21
+0x1.5e489c4ee9a4ap-17
+0x1.5e5944f62ae11p-6
+0x1.5e5c1cd6c4e6fp-22
+0x1.5e6e8e52e9578p-4
+0x1.5e8add236a58dp-25
+0x1.5e8add236a58ep-25
+0x1.5e8add236a58fp-25
+0x1.5e8add236a59p-25
+0x1.5e8add236a591p-25
+0x1.5e8add236a592p-25
+0x1.5eaa21c91a04fp-13
+0x1.5ebd77df08893p-17
+0x1.5f02218b3700dp-16
+0x1.5f1bc3537aceap-9
+0x1.5f1c5549eb31p-7
+0x1.5f44f9f3b2913p-4
+0x1.5f680156ed209p-2
+0x1.5f6f40c07e007p-11
+0x1.5fb9cf33f5119p-1
+0x1.5fc24160e975ep-2
+0x1.5ff45ce6073f2p-21
+0x1.6026ba97e044dp-7
+0x1.60345fbde0494p-15
+0x1.606a572a9e61bp-7
+0x1.6077a8d6da273p-6
+0x1.607fe8c2a1368p-21
+0x1.609ab25efc716p-16
+0x1.60ae6071ba98cp-21
+0x1.60b61eb4eb1d9p-15
+0x1.60c3f7fea552p-18
+0x1.60e4bb1bb2e9fp-8
+0x1.60e8be543e6c8p-2
+0x1.610377aa2b314p-5
+0x1.617398f2aaa49p-24
+0x1.617398f2aaa4ap-24
+0x1.6175430384addp-3
+0x1.61bf1076ce3e4p-15
+0x1.61ef0fc7aca03p-8
+0x1.61faf00de0755p-19
+0x1.62140864c6b4bp-2
+0x1.628668a72685cp-20
+0x1.63207ae7ed617p-9
+0x1.634036f440d9dp-14
+0x1.634c747cad374p-12
+0x1.634d7cf3985b5p-21
+0x1.6356c0c2112f6p-16
+0x1.639d5fa45dc83p-16
+0x1.639e2653e4222p-23
+0x1.6405c0ac31058p-21
+0x1.649cc398d94b9p-17
+0x1.64aba89785441p-17
+0x1.64af4ea49404p+0
+0x1.64d3f349c366cp-11
+0x1.64ddee993bcc2p-19
+0x1.64f072652c58bp-17
+0x1.6549bf83dd60fp-5
+0x1.65755e29dd4a5p-4
+0x1.65b8b87ef964p-4
+0x1.65cc538bbc041p-7
+0x1.65f0fb64fe38cp-19
+0x1.65f320a9951f4p-22
+0x1.65f5fcffeaa58p-20
+0x1.668156c24eadbp-19
+0x1.66890c61fd5aap-16
+0x1.66a72d5899fd8p-20
+0x1.66ab1d2f4f41ap-16
+0x1.6724b786ba6c8p-20
+0x1.672b0d73a49c2p-6
+0x1.6732f8d0e2f78p-24
+0x1.6732f8d0e2f79p-24
+0x1.673b9fe297c3ep+0
+0x1.674e568fe6b7fp-6
+0x1.6789fae091c02p-16
+0x1.679e49ab6dc28p-1
+0x1.682913c63dd0ap-13
+0x1.682bb611ae95bp-2
+0x1.6865dad5ede51p-17
+0x1.686bef95059f6p-11
+0x1.688a428da5096p-4
+0x1.68b5f3a04d1d6p-14
+0x1.6922eb720331bp-5
+0x1.69ff229757ca1p-2
+0x1.6a38cb43d335cp-13
+0x1.6a4c38abdcbacp-10
+0x1.6a5b44858f56bp-4
+0x1.6a6ab91ba5b1p-19
+0x1.6aaa772e29924p-8
+0x1.6aada09d8a7e1p-18
+0x1.6af263c72ad44p-6
+0x1.6af7ebb63caecp-3
+0x1.6b1636542416cp-20
+0x1.6b2e66e3bec52p-7
+0x1.6b87ec3ad8729p+0
+0x1.6b8a6273d7c21p+0
+0x1.6b944399e987ep-8
+0x1.6bb5e0378b9fbp-5
+0x1.6bd3fcd1b43eep-14
+0x1.6bf8e685b9a0fp-11
+0x1.6c00b40b14a7dp-19
+0x1.6c1c715b976e9p-14
+0x1.6c24907302482p-20
+0x1.6c4eb71399f89p-3
+0x1.6c66002583abcp-8
+0x1.6c78b68893712p-18
+0x1.6cdb2bbb212edp-24
+0x1.6d858a2e37f64p-12
+0x1.6d97e60102b3dp-18
+0x1.6dacba5fbfd6bp-19
+0x1.6db315ff2a4d6p-15
+0x1.6dd2922a862cbp-15
+0x1.6e634512b6333p-21
+0x1.6ea868bda0dbcp-10
+0x1.6eafe6a7...
[truncated]

@github-actions

github-actions Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

✅ With the latest revision this PR passed the Python code formatter.

@yklcs

yklcs commented Sep 4, 2026

Copy link
Copy Markdown

Thanks Tue, our tests are passing as well.
Guessing there's a reason why the new worst-case tests are placed under the exhaustive directory?

@lntue

lntue commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks Tue, our tests are passing as well. Guessing there's a reason why the new worst-case tests are placed under the exhaustive directory?

I didn't know how long the tests will take in our limited power pre-commit CIs yet, so just in case it takes too long, we will have to make it build-only for precommit CIs, and only run it in our dedicated post commit CIs. So far looks like only our riscv64 qemu precommit CI bot struggle with it. I could imagine when we have many more of these tests, we might have to run all of them in our dedicated post commit CIs.

Also at the moment they mimic other single precision exhaustive test setup, so using quite many things from libc++ that we don't have coverage in LLVM libc yet.

@yklcs

yklcs commented Sep 4, 2026

Copy link
Copy Markdown

Thanks Tue, our tests are passing as well. Guessing there's a reason why the new worst-case tests are placed under the exhaustive directory?

I didn't know how long the tests will take in our limited power pre-commit CIs yet, so just in case it takes too long, we will have to make it build-only for precommit CIs, and only run it in our dedicated post commit CIs. So far looks like only our riscv64 qemu precommit CI bot struggle with it. I could imagine when we have many more of these tests, we might have to run all of them in our dedicated post commit CIs.

Also at the moment they mimic other single precision exhaustive test setup, so using quite many things from libc++ that we don't have coverage in LLVM libc yet.

Ah I see. Thanks for the quick fix!

@kaladron
kaladron self-requested a review September 4, 2026 14:47
}
#endif

const char *candidate_dirs[] = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot make a macro to get the libc path on build instead of this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@boomanaiden154 boomanaiden154 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we split the testing infrastructure updates out from the actual fix so we can get the fix in sooner?

I'm not a big fan of checking in ~10MB files containing a bunch of worst cases. This seems like something CI should acquire from elsewhere. We also probably want to avoid checking in more binary files.

@lntue

lntue commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Can we split the testing infrastructure updates out from the actual fix so we can get the fix in sooner?

I'm not a big fan of checking in ~10MB files containing a bunch of worst cases. This seems like something CI should acquire from elsewhere. We also probably want to avoid checking in more binary files.

I just add the infra updates to see how long it takes if running these in precommit CIs. And look like it does take awhile, so I will remove the CI changes, and look for setting up a dedicated postcommit CI instead.

@lntue

lntue commented Sep 12, 2026

Copy link
Copy Markdown
Contributor Author

Can we split the testing infrastructure updates out from the actual fix so we can get the fix in sooner?

I'm not a big fan of checking in ~10MB files containing a bunch of worst cases. This seems like something CI should acquire from elsewhere. We also probably want to avoid checking in more binary files.

Infra update is removed.

@lntue
lntue merged commit dbac421 into llvm:main Sep 14, 2026
45 checks passed
@lntue
lntue deleted the cos branch September 14, 2026 02:49
aobolensk pushed a commit to aobolensk/llvm-project that referenced this pull request Sep 15, 2026
…/cos. (llvm#221012)

llvm#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 llvm#220934.

Assisted-by: Gemini is used to generate the worst-case text-to-binary
tool.
anilkund pushed a commit to anilkund/llvm-project that referenced this pull request Sep 16, 2026
…/cos. (llvm#221012)

llvm#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 llvm#220934.

Assisted-by: Gemini is used to generate the worst-case text-to-binary
tool.
lntue added a commit to lntue/llvm-project that referenced this pull request Sep 29, 2026
…/cos. (llvm#221012)

llvm#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 llvm#220934.

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

(cherry picked from commit dbac421)
tru pushed a commit that referenced this pull request Sep 30, 2026
…/cos. (#221012)

#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 #220934.

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

(cherry picked from commit dbac421)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libc][math] cos is incorrectly rounded

4 participants