[libc-commits] [libc] [libc][math] Implement double precision sincos correctly rounded to all rounding modes. (PR #96719)
via libc-commits
libc-commits at lists.llvm.org
Tue Jun 25 18:16:43 PDT 2024
https://github.com/lntue created https://github.com/llvm/llvm-project/pull/96719
Sharing the same algorithm as double precision sin: https://github.com/llvm/llvm-project/pull/95736 and cos: https://github.com/llvm/llvm-project/pull/96591
>From 4b120d38c4793c1a1c6c22166e222df7b49a2316 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Wed, 26 Jun 2024 01:12:32 +0000
Subject: [PATCH] [libc][math] Implement double precision sincos correctly
rounded to all rounding modes.
---
libc/config/darwin/arm/entrypoints.txt | 1 +
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/arm/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/config/windows/entrypoints.txt | 1 +
libc/docs/math/index.rst | 2 +-
libc/src/math/generic/CMakeLists.txt | 22 ++
libc/src/math/generic/sincos.cpp | 247 ++++++++++++++++++++++
libc/test/src/math/CMakeLists.txt | 12 ++
libc/test/src/math/sincos_test.cpp | 120 +++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 10 +
libc/test/src/math/smoke/sincos_test.cpp | 41 ++++
13 files changed, 459 insertions(+), 1 deletion(-)
create mode 100644 libc/src/math/generic/sincos.cpp
create mode 100644 libc/test/src/math/sincos_test.cpp
create mode 100644 libc/test/src/math/smoke/sincos_test.cpp
diff --git a/libc/config/darwin/arm/entrypoints.txt b/libc/config/darwin/arm/entrypoints.txt
index 2843c469559ba..4d5536318724d 100644
--- a/libc/config/darwin/arm/entrypoints.txt
+++ b/libc/config/darwin/arm/entrypoints.txt
@@ -225,6 +225,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbn
libc.src.math.scalbnf
libc.src.math.scalbnl
+ libc.src.math.sincos
libc.src.math.sincosf
libc.src.math.sinhf
libc.src.math.sin
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 746cc675e8fcd..f23b3da06308c 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -480,6 +480,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbn
libc.src.math.scalbnf
libc.src.math.scalbnl
+ libc.src.math.sincos
libc.src.math.sincosf
libc.src.math.sinhf
libc.src.math.sin
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 01f2cc83700c8..a9d0a12598889 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -358,6 +358,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbn
libc.src.math.scalbnf
libc.src.math.scalbnl
+ libc.src.math.sincos
libc.src.math.sincosf
libc.src.math.sin
libc.src.math.sinf
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 70102a08f1255..7ef5ed899e83f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -488,6 +488,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbn
libc.src.math.scalbnf
libc.src.math.scalbnl
+ libc.src.math.sincos
libc.src.math.sincosf
libc.src.math.sinhf
libc.src.math.sin
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 34748ff5950ad..bea2eb32eccd9 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -510,6 +510,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnf
libc.src.math.scalbnl
libc.src.math.sin
+ libc.src.math.sincos
libc.src.math.sincosf
libc.src.math.sinhf
libc.src.math.sinf
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index a4898724daf86..b33d572cf999a 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -251,6 +251,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnl
libc.src.math.sin
libc.src.math.sincosf
+ libc.src.math.sincosf
libc.src.math.sinf
libc.src.math.sinhf
libc.src.math.sqrt
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index e05149d8e1dc9..6b06f1515143d 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -326,7 +326,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| sin | |check| | |check| | | | | 7.12.4.6 | F.10.1.6 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| sincos | |check| | large | | | | | |
+| sincos | |check| | |check| | | | | | |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| sinh | |check| | | | | | 7.12.5.5 | F.10.2.5 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 41a77c8710f6b..5fe3b8f7b4ad3 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -261,6 +261,28 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ sincos
+ SRCS
+ sincos.cpp
+ HDRS
+ ../sincos.h
+ DEPENDS
+ .range_reduction_double
+ .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
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
sincosf
SRCS
diff --git a/libc/src/math/generic/sincos.cpp b/libc/src/math/generic/sincos.cpp
new file mode 100644
index 0000000000000..a0dd3a018af59
--- /dev/null
+++ b/libc/src/math/generic/sincos.cpp
@@ -0,0 +1,247 @@
+//===-- Double-precision sincos function ----------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#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/optimization.h" // LIBC_UNLIKELY
+#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
+#include "src/math/generic/sincos_eval.h"
+
+#ifdef LIBC_TARGET_CPU_HAS_FMA
+#include "range_reduction_double_fma.h"
+
+using LIBC_NAMESPACE::fma::FAST_PASS_EXPONENT;
+using LIBC_NAMESPACE::fma::ONE_TWENTY_EIGHT_OVER_PI;
+using LIBC_NAMESPACE::fma::range_reduction_small;
+using LIBC_NAMESPACE::fma::SIN_K_PI_OVER_128;
+
+LIBC_INLINE constexpr bool NO_FMA = false;
+#else
+#include "range_reduction_double_nofma.h"
+
+using LIBC_NAMESPACE::nofma::FAST_PASS_EXPONENT;
+using LIBC_NAMESPACE::nofma::ONE_TWENTY_EIGHT_OVER_PI;
+using LIBC_NAMESPACE::nofma::range_reduction_small;
+using LIBC_NAMESPACE::nofma::SIN_K_PI_OVER_128;
+
+LIBC_INLINE constexpr bool NO_FMA = true;
+#endif // LIBC_TARGET_CPU_HAS_FMA
+
+// TODO: We might be able to improve the performance of large range reduction of
+// non-FMA targets further by operating directly on 25-bit chunks of 128/pi and
+// pre-split SIN_K_PI_OVER_128, but that might double the memory footprint of
+// those lookup table.
+#include "range_reduction_double_common.h"
+
+#if ((LIBC_MATH & LIBC_MATH_SKIP_ACCURATE_PASS) != 0)
+#define LIBC_MATH_SINCOS_SKIP_ACCURATE_PASS
+#endif
+
+namespace LIBC_NAMESPACE {
+
+using DoubleDouble = fputil::DoubleDouble;
+using Float128 = typename fputil::DyadicFloat<128>;
+
+LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) {
+ using FPBits = typename fputil::FPBits<double>;
+ FPBits xbits(x);
+
+ uint16_t x_e = xbits.get_biased_exponent();
+
+ DoubleDouble y;
+ unsigned k;
+ generic::LargeRangeReduction<NO_FMA> range_reduction_large;
+
+ // |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
+ if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {
+ // |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
+ *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
+ return;
+ }
+
+ // // Small range reduction.
+ k = range_reduction_small(x, y);
+ } else {
+ // Inf or NaN
+ if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {
+ // 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.compute_high_part(x);
+ y = range_reduction_large.fast();
+ }
+
+ DoubleDouble sin_y, cos_y;
+
+ generic::sincos_eval(y, sin_y, cos_y);
+
+ // Look up sin(k * pi/128) and cos(k * pi/128)
+ // Memory saving versions:
+
+ // Use 128-entry table instead:
+ // DoubleDouble sin_k = SIN_K_PI_OVER_128[k & 127];
+ // uint64_t sin_s = static_cast<uint64_t>(k & 128) << (63 - 7);
+ // sin_k.hi = FPBits(FPBits(sin_k.hi).uintval() ^ sin_s).get_val();
+ // sin_k.lo = FPBits(FPBits(sin_k.hi).uintval() ^ sin_s).get_val();
+ // DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 127];
+ // uint64_t cos_s = static_cast<uint64_t>((k + 64) & 128) << (63 - 7);
+ // cos_k.hi = FPBits(FPBits(cos_k.hi).uintval() ^ cos_s).get_val();
+ // cos_k.lo = FPBits(FPBits(cos_k.hi).uintval() ^ cos_s).get_val();
+
+ // Use 64-entry table instead:
+ // 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);
+
+ // 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];
+ 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<NO_FMA>(cos_y, sin_k);
+ DoubleDouble cos_k_sin_y = fputil::quick_mult<NO_FMA>(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<NO_FMA>(cos_y, cos_k);
+ DoubleDouble msin_k_sin_y = fputil::quick_mult<NO_FMA>(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_SINCOS_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.
+
+#ifdef LIBC_TARGET_CPU_HAS_FMA
+ constexpr double ERR = 0x1.0p-70;
+#else
+ // TODO: Improve non-FMA fast pass accuracy.
+ constexpr double ERR = 0x1.0p-66;
+#endif // LIBC_TARGET_CPU_HAS_FMA
+
+ 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 = generic::range_reduction_small_f128(x);
+ else
+ u_f128 = range_reduction_large.accurate();
+
+ generic::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 = generic::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_SINCOS_SKIP_ACCURATE_PASS
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index ba588662f469e..1ddb5c7c33664 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -72,6 +72,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ sincos_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ sincos_test.cpp
+ DEPENDS
+ libc.src.math.sincos
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
tanf_test
NEED_MPFR
diff --git a/libc/test/src/math/sincos_test.cpp b/libc/test/src/math/sincos_test.cpp
new file mode 100644
index 0000000000000..c0cbff7084d19
--- /dev/null
+++ b/libc/test/src/math/sincos_test.cpp
@@ -0,0 +1,120 @@
+//===-- Unittests for sincos ----------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/sincos.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcSincosTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+using LIBC_NAMESPACE::testing::tlog;
+
+#define ASSERT_SINCOS_MATCH_ALL_ROUNDING(input) \
+ do { \
+ double sin_x, cos_x; \
+ namespace mpfr = LIBC_NAMESPACE::testing::mpfr; \
+ \
+ mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest); \
+ if (__r1.success) { \
+ LIBC_NAMESPACE::sincos(input, &sin_x, &cos_x); \
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sin, input, sin_x, 0.5, \
+ mpfr::RoundingMode::Nearest); \
+ ASSERT_MPFR_MATCH(mpfr::Operation::Cos, input, cos_x, 0.5, \
+ mpfr::RoundingMode::Nearest); \
+ } \
+ \
+ mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward); \
+ if (__r2.success) { \
+ LIBC_NAMESPACE::sincos(input, &sin_x, &cos_x); \
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sin, input, sin_x, 0.5, \
+ mpfr::RoundingMode::Upward); \
+ ASSERT_MPFR_MATCH(mpfr::Operation::Cos, input, cos_x, 0.5, \
+ mpfr::RoundingMode::Upward); \
+ } \
+ \
+ mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward); \
+ if (__r3.success) { \
+ LIBC_NAMESPACE::sincos(input, &sin_x, &cos_x); \
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sin, input, sin_x, 0.5, \
+ mpfr::RoundingMode::Downward); \
+ ASSERT_MPFR_MATCH(mpfr::Operation::Cos, input, cos_x, 0.5, \
+ mpfr::RoundingMode::Downward); \
+ } \
+ \
+ mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero); \
+ if (__r4.success) { \
+ LIBC_NAMESPACE::sincos(input, &sin_x, &cos_x); \
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sin, input, sin_x, 0.5, \
+ mpfr::RoundingMode::TowardZero); \
+ ASSERT_MPFR_MATCH(mpfr::Operation::Cos, input, cos_x, 0.5, \
+ mpfr::RoundingMode::TowardZero); \
+ } \
+ } while (0)
+
+TEST_F(LlvmLibcSincosTest, TrickyInputs) {
+ constexpr double INPUTS[] = {
+ 0x1.8000000000009p-23, 0x1.8000000000024p-22,
+ 0x1.800000000009p-21, 0x1.20000000000f3p-20,
+ 0x1.800000000024p-20, 0x1.e0000000001c2p-20,
+ 0x1.940c877fb7dacp-7, -0x1.f42fb19b5b9b2p-6,
+ -0x1.0285070f9f1bcp-5, 0x1.23f40dccdef72p+0,
+ 0x1.43cf16358c9d7p+0, 0x1.addf3b9722265p+0,
+ 0x1.ae78d360afa15p+0, 0x1.e31b55306f22cp+2,
+ 0x1.e639103a05997p+2, -0x1.f7898d5a756ddp+2,
+ 0x1.1685973506319p+3, 0x1.5f09cad750ab1p+3,
+ -0x1.aaf85537ea4c7p+3, 0x1.4f2b874135d27p+4,
+ 0x1.13114266f9764p+4, 0x1.a211877de55dbp+4,
+ -0x1.a5eece87e8606p+4, 0x1.a65d441ea6dcep+4,
+ 0x1.1ffb509f3db15p+5, 0x1.2345d1e090529p+5,
+ 0x1.c96e28eb679f8p+5, 0x1.be886d9c2324dp+6,
+ -0x1.ab514bfc61c76p+7, -0x1.14823229799c2p+7,
+ 0x1.48ff1782ca91dp+8, 0x1.dcbfda0c7559ep+8,
+ 0x1.dcbfda0c7559ep+8, 0x1.2e566149bf5fdp+9,
+ 0x1.cb996c60f437ep+9, 0x1.ae945054939c2p+10,
+ -0x1.119471e9216cdp+10, 0x1.ae945054939c2p+10,
+ 0x1.fffffffffdb6p+24, 0x1.fd4da4ef37075p+29,
+ 0x1.55202aefde314p+31, 0x1.b951f1572eba5p+31,
+ 0x1.7776c2343ba4ep+101, 0x1.85fc0f04c0128p+101,
+ 0x1.678309fa50d58p+110, 0x1.fffffffffef4ep+199,
+ 0x1.3eec5912ea7cdp+331, -0x1.3eec5912ea7cdp+331,
+ -0x1.6deb37da81129p+205, 0x1.08087e9aad90bp+887,
+ -0x1.08087e9aad90bp+887, -0x1.8bb5847d49973p+845,
+ 0x1.6ac5b262ca1ffp+849, 0x1.f08b14e1c4d0fp+890,
+ 0x1.2b5fe88a9d8d5p+903, 0x1.f6d7518808571p+1023,
+ -0x1.a880417b7b119p+1023, 0x1.00a33764a0a83p-7,
+ 0x1.fe81868fc47fep+1, 0x1.0da8cc189b47dp-10,
+ 0x1.da1838053b866p+5,
+
+ };
+ constexpr int N = sizeof(INPUTS) / sizeof(INPUTS[0]);
+
+ for (int i = 0; i < N; ++i) {
+ ASSERT_SINCOS_MATCH_ALL_ROUNDING(INPUTS[i]);
+ }
+}
+
+TEST_F(LlvmLibcSincosTest, InDoubleRange) {
+ constexpr uint64_t COUNT = 123'451;
+ uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0x1.0p-50).uintval();
+ uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(0x1.0p200).uintval();
+ uint64_t STEP = (STOP - START) / COUNT;
+
+ auto test = [&](mpfr::RoundingMode rounding_mode) {
+ for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ if (isnan(x) || isinf(x))
+ continue;
+
+ ASSERT_SINCOS_MATCH_ALL_ROUNDING(x);
+ }
+ };
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index d79e296913e32..1ec585337e25c 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3687,3 +3687,13 @@ add_fp_unittest(
DEPENDS
libc.src.math.cos
)
+
+add_fp_unittest(
+ sincos_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ sincos_test.cpp
+ DEPENDS
+ libc.src.math.sincos
+)
diff --git a/libc/test/src/math/smoke/sincos_test.cpp b/libc/test/src/math/smoke/sincos_test.cpp
new file mode 100644
index 0000000000000..371c0ad63cbf5
--- /dev/null
+++ b/libc/test/src/math/smoke/sincos_test.cpp
@@ -0,0 +1,41 @@
+//===-- Unittests for sincos ----------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/sincos.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcSincosTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcSincosTest, SpecialNumbers) {
+ double sin_x, cos_x;
+
+ LIBC_NAMESPACE::sincos(aNaN, &sin_x, &cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, sin_x);
+
+ LIBC_NAMESPACE::sincos(zero, &sin_x, &cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(1.0, cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, sin_x);
+
+ LIBC_NAMESPACE::sincos(neg_zero, &sin_x, &cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(1.0, cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, sin_x);
+
+ LIBC_NAMESPACE::sincos(inf, &sin_x, &cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, sin_x);
+
+ LIBC_NAMESPACE::sincos(neg_inf, &sin_x, &cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, cos_x);
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, sin_x);
+
+ LIBC_NAMESPACE::sincos(0x1.0p-28, &sin_x, &cos_x);
+ EXPECT_FP_EQ(1.0, cos_x);
+ EXPECT_FP_EQ(0x1.0p-28, sin_x);
+}
More information about the libc-commits
mailing list