[libc-commits] [libc] [libc][math] Add double-precision acosh, asinh, atanh, cosh, sinh, tanh (PR #199442)
Aayush Shrivastava via libc-commits
libc-commits at lists.llvm.org
Sun May 24 12:40:39 PDT 2026
- Previous message: [libc-commits] [libc] [libc][math] Add double-precision acosh, asinh, atanh, cosh, sinh, tanh (PR #199442)
- Next message: [libc-commits] [libc] [libc][math] Add double-precision acosh, asinh, atanh, cosh, sinh, tanh (PR #199442)
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
https://github.com/iamaayushrivastava created https://github.com/llvm/llvm-project/pull/199442
Add missing double-precision hyperbolic math functions to LLVM libc.
>From 8be5fd148649d093cdf92c688a2f9ca1e740e650 Mon Sep 17 00:00:00 2001
From: Aayush Shrivastava <iamaayushrivastava at gmail.com>
Date: Mon, 25 May 2026 01:06:01 +0530
Subject: [PATCH] [libc][math] Add double-precision acosh, asinh, atanh, cosh,
sinh, tanh
---
libc/config/linux/x86_64/entrypoints.txt | 6 ++
libc/include/math.yaml | 52 +++++++++++++-
libc/src/__support/math/CMakeLists.txt | 79 +++++++++++++++++++++
libc/src/__support/math/acosh.h | 70 +++++++++++++++++++
libc/src/__support/math/asinh.h | 79 +++++++++++++++++++++
libc/src/__support/math/atanh.h | 71 +++++++++++++++++++
libc/src/__support/math/cosh.h | 72 +++++++++++++++++++
libc/src/__support/math/sinh.h | 88 ++++++++++++++++++++++++
libc/src/__support/math/tanh.h | 70 +++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 60 ++++++++++++++++
libc/src/math/generic/acosh.cpp | 16 +++++
libc/src/math/generic/asinh.cpp | 16 +++++
libc/src/math/generic/atanh.cpp | 16 +++++
libc/src/math/generic/cosh.cpp | 16 +++++
libc/src/math/generic/sinh.cpp | 16 +++++
libc/src/math/generic/tanh.cpp | 16 +++++
libc/test/src/math/CMakeLists.txt | 76 ++++++++++++++++++++
libc/test/src/math/acosh_test.cpp | 52 ++++++++++++++
libc/test/src/math/asinh_test.cpp | 41 +++++++++++
libc/test/src/math/atanh_test.cpp | 57 +++++++++++++++
libc/test/src/math/cosh_test.cpp | 55 +++++++++++++++
libc/test/src/math/sinh_test.cpp | 55 +++++++++++++++
libc/test/src/math/smoke/CMakeLists.txt | 70 +++++++++++++++++++
libc/test/src/math/smoke/acosh_test.cpp | 61 ++++++++++++++++
libc/test/src/math/smoke/asinh_test.cpp | 52 ++++++++++++++
libc/test/src/math/smoke/atanh_test.cpp | 65 +++++++++++++++++
libc/test/src/math/smoke/cosh_test.cpp | 72 +++++++++++++++++++
libc/test/src/math/smoke/sinh_test.cpp | 69 +++++++++++++++++++
libc/test/src/math/smoke/tanh_test.cpp | 52 ++++++++++++++
libc/test/src/math/tanh_test.cpp | 40 +++++++++++
30 files changed, 1559 insertions(+), 1 deletion(-)
create mode 100644 libc/src/__support/math/acosh.h
create mode 100644 libc/src/__support/math/asinh.h
create mode 100644 libc/src/__support/math/atanh.h
create mode 100644 libc/src/__support/math/cosh.h
create mode 100644 libc/src/__support/math/sinh.h
create mode 100644 libc/src/__support/math/tanh.h
create mode 100644 libc/src/math/generic/acosh.cpp
create mode 100644 libc/src/math/generic/asinh.cpp
create mode 100644 libc/src/math/generic/atanh.cpp
create mode 100644 libc/src/math/generic/cosh.cpp
create mode 100644 libc/src/math/generic/sinh.cpp
create mode 100644 libc/src/math/generic/tanh.cpp
create mode 100644 libc/test/src/math/acosh_test.cpp
create mode 100644 libc/test/src/math/asinh_test.cpp
create mode 100644 libc/test/src/math/atanh_test.cpp
create mode 100644 libc/test/src/math/cosh_test.cpp
create mode 100644 libc/test/src/math/sinh_test.cpp
create mode 100644 libc/test/src/math/smoke/acosh_test.cpp
create mode 100644 libc/test/src/math/smoke/asinh_test.cpp
create mode 100644 libc/test/src/math/smoke/atanh_test.cpp
create mode 100644 libc/test/src/math/smoke/cosh_test.cpp
create mode 100644 libc/test/src/math/smoke/sinh_test.cpp
create mode 100644 libc/test/src/math/smoke/tanh_test.cpp
create mode 100644 libc/test/src/math/tanh_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5545790fecd85..16d733fdde948 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -534,10 +534,12 @@ set(TARGET_LIBM_ENTRYPOINTS
# math.h entrypoints
libc.src.math.acos
libc.src.math.acosf
+ libc.src.math.acosh
libc.src.math.acoshf
libc.src.math.acospif
libc.src.math.asin
libc.src.math.asinf
+ libc.src.math.asinh
libc.src.math.asinhf
libc.src.math.asinpi
libc.src.math.asinpif
@@ -545,6 +547,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.atan2f
libc.src.math.atan
libc.src.math.atanf
+ libc.src.math.atanh
libc.src.math.atanhf
libc.src.math.canonicalize
libc.src.math.canonicalizef
@@ -560,6 +563,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.copysignl
libc.src.math.cos
libc.src.math.cosf
+ libc.src.math.cosh
libc.src.math.coshf
libc.src.math.cospif
libc.src.math.dfmal
@@ -746,6 +750,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.sincos
libc.src.math.sincosf
libc.src.math.sinf
+ libc.src.math.sinh
libc.src.math.sinhf
libc.src.math.sinpif
libc.src.math.sqrt
@@ -753,6 +758,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.sqrtl
libc.src.math.tan
libc.src.math.tanf
+ libc.src.math.tanh
libc.src.math.tanhf
libc.src.math.tanpif
libc.src.math.totalorder
diff --git a/libc/include/math.yaml b/libc/include/math.yaml
index e50be8f05cc65..a0c05a7cbf7fb 100644
--- a/libc/include/math.yaml
+++ b/libc/include/math.yaml
@@ -27,6 +27,12 @@ functions:
arguments:
- type: _Float16
guard: LIBC_TYPES_HAS_FLOAT16
+ - name: acosh
+ standards:
+ - stdc
+ return_type: double
+ arguments:
+ - type: double
- name: acoshf
standards:
- stdc
@@ -40,6 +46,12 @@ functions:
arguments:
- type: _Float16
guard: LIBC_TYPES_HAS_FLOAT16
+ - name: acospif
+ standards:
+ - stdc
+ return_type: float
+ arguments:
+ - type: float
- name: acospif16
standards:
- stdc
@@ -66,6 +78,12 @@ functions:
arguments:
- type: _Float16
guard: LIBC_TYPES_HAS_FLOAT16
+ - name: asinh
+ standards:
+ - stdc
+ return_type: double
+ arguments:
+ - type: double
- name: asinhf
standards:
- stdc
@@ -133,6 +151,14 @@ functions:
- type: float128
- type: float128
guard: LIBC_TYPES_HAS_FLOAT128
+ - name: atan2f16
+ standards:
+ - stdc
+ return_type: _Float16
+ arguments:
+ - type: _Float16
+ - type: _Float16
+ guard: LIBC_TYPES_HAS_FLOAT16
- name: atanf
standards:
- stdc
@@ -146,6 +172,12 @@ functions:
arguments:
- type: _Float16
guard: LIBC_TYPES_HAS_FLOAT16
+ - name: atanh
+ standards:
+ - stdc
+ return_type: double
+ arguments:
+ - type: double
- name: atanhf
standards:
- stdc
@@ -153,7 +185,7 @@ functions:
arguments:
- type: float
- name: atanhf16
- standatds:
+ standards:
- stdc
return_type: _Float16
arguments:
@@ -303,6 +335,12 @@ functions:
arguments:
- type: _Float16
guard: LIBC_TYPES_HAS_FLOAT16
+ - name: cosh
+ standards:
+ - stdc
+ return_type: double
+ arguments:
+ - type: double
- name: coshf
standards:
- stdc
@@ -2593,6 +2631,12 @@ functions:
- type: float
- type: float *
- type: float *
+ - name: sinh
+ standards:
+ - stdc
+ return_type: double
+ arguments:
+ - type: double
- name: sinhf
standards:
- stdc
@@ -2670,6 +2714,12 @@ functions:
arguments:
- type: _Float16
guard: LIBC_TYPES_HAS_FLOAT16
+ - name: tanh
+ standards:
+ - stdc
+ return_type: double
+ arguments:
+ - type: double
- name: tanhf
standards:
- stdc
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 9e3ec26cdc881..ba940e2f61e98 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -67,6 +67,19 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ acosh
+ HDRS
+ acosh.h
+ DEPENDS
+ .log
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
acoshf
HDRS
@@ -157,6 +170,20 @@ add_header_library(
libc.src.__support.macros.properties.cpu_features
)
+add_header_library(
+ asinh
+ HDRS
+ asinh.h
+ DEPENDS
+ .log
+ .log1p
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
asinhf
HDRS
@@ -354,6 +381,18 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ atanh
+ HDRS
+ atanh.h
+ DEPENDS
+ .log1p
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
atanhf
HDRS
@@ -2797,6 +2836,20 @@ add_header_library(
libc.src.__support.macros.properties.types
)
+add_header_library(
+ cosh
+ HDRS
+ cosh.h
+ DEPENDS
+ .exp
+ .expm1
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
coshf
HDRS
@@ -5221,6 +5274,20 @@ add_header_library(
libc.src.__support.macros.properties.types
)
+add_header_library(
+ sinh
+ HDRS
+ sinh.h
+ DEPENDS
+ .exp
+ .expm1
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
sinhf
HDRS
@@ -5371,6 +5438,18 @@ add_header_library(
libc.include.llvm-libc-macros.float16_macros
)
+add_header_library(
+ tanh
+ HDRS
+ tanh.h
+ DEPENDS
+ .expm1
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
tanhf
HDRS
diff --git a/libc/src/__support/math/acosh.h b/libc/src/__support/math/acosh.h
new file mode 100644
index 0000000000000..c25fed33f8791
--- /dev/null
+++ b/libc/src/__support/math/acosh.h
@@ -0,0 +1,70 @@
+//===-- Implementation header for acosh -------------------------*- 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_ACOSH_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ACOSH_H
+
+#include "log.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double acosh(double x) {
+ using FPBits = fputil::FPBits<double>;
+ FPBits xbits(x);
+
+ // acosh is only defined for x >= 1.
+ if (LIBC_UNLIKELY(x < 1.0)) {
+ if (x == x) {
+ // x < 1 and x is not NaN: domain error.
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ } else {
+ // x is NaN.
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // acosh(1) = 0.
+ if (x == 1.0)
+ return 0.0;
+
+ // acosh(+inf) = +inf.
+ if (LIBC_UNLIKELY(xbits.is_inf()))
+ return x;
+
+ uint64_t x_u = xbits.uintval();
+
+ // For large x (x >= 2^28), acosh(x) ~ log(2x) = log(x) + log(2).
+ if (LIBC_UNLIKELY(x_u >= 0x41b0000000000000ULL)) {
+ // log(2x) = log(x) + log(2).
+ constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
+ return math::log(x) + LOG_2;
+ }
+
+ // General case: acosh(x) = log(x + sqrt(x^2 - 1)).
+ double x2m1 = x * x - 1.0;
+ return math::log(x + fputil::sqrt<double>(x2m1));
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ACOSH_H
diff --git a/libc/src/__support/math/asinh.h b/libc/src/__support/math/asinh.h
new file mode 100644
index 0000000000000..73d920ed4874c
--- /dev/null
+++ b/libc/src/__support/math/asinh.h
@@ -0,0 +1,79 @@
+//===-- Implementation header for asinh -------------------------*- 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_ASINH_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINH_H
+
+#include "log.h"
+#include "log1p.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double asinh(double x) {
+ using FPBits = fputil::FPBits<double>;
+ FPBits xbits(x);
+
+ // Handle NaN.
+ if (LIBC_UNLIKELY(xbits.is_nan())) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ // Handle +/-inf: asinh(+/-inf) = +/-inf.
+ if (LIBC_UNLIKELY(xbits.is_inf()))
+ return x;
+
+ uint64_t x_abs_u = xbits.abs().uintval();
+ double x_abs = xbits.abs().get_val();
+ bool is_neg = xbits.is_neg();
+
+ // For very small |x| (|x| <= 2^-26), asinh(x) ~ x.
+ // Use FP arithmetic to ensure FTZ/DAZ mode behavior.
+ if (LIBC_UNLIKELY(x_abs_u <= 0x3e50000000000000ULL)) {
+ // asinh(+/-0) = +/-0, preserve sign of zero exactly.
+ if (LIBC_UNLIKELY(x_abs_u == 0))
+ return x;
+ double x2 = x_abs * x_abs;
+ double result = x_abs - x2 * x_abs * (1.0 / 6.0);
+ return is_neg ? -result : result;
+ }
+
+ double result;
+ // For large |x| (|x| >= 2^28), asinh(x) ~ log(2|x|) = log(|x|) + log(2).
+ if (LIBC_UNLIKELY(x_abs_u >= 0x41b0000000000000ULL)) {
+ constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
+ result = math::log(x_abs) + LOG_2;
+ } else if (x_abs_u >= 0x3fe0000000000000ULL) {
+ // |x| >= 0.5: asinh(x) = log(x + sqrt(x^2 + 1)).
+ result = math::log(x_abs + fputil::sqrt<double>(x_abs * x_abs + 1.0));
+ } else {
+ // |x| < 0.5: use log1p for better accuracy near 0.
+ // asinh(x) = log1p(x + x^2 / (1 + sqrt(1 + x^2)))
+ double x2 = x_abs * x_abs;
+ double sqrt1px2 = fputil::sqrt<double>(1.0 + x2);
+ result = math::log1p(x_abs + x2 / (1.0 + sqrt1px2));
+ }
+
+ return is_neg ? -result : result;
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ASINH_H
diff --git a/libc/src/__support/math/atanh.h b/libc/src/__support/math/atanh.h
new file mode 100644
index 0000000000000..cf85fe721505c
--- /dev/null
+++ b/libc/src/__support/math/atanh.h
@@ -0,0 +1,71 @@
+//===-- Implementation header for atanh -------------------------*- 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_ATANH_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANH_H
+
+#include "log1p.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double atanh(double x) {
+ using FPBits = fputil::FPBits<double>;
+ FPBits xbits(x);
+ Sign sign = xbits.sign();
+ uint64_t x_abs_u = xbits.abs().uintval();
+
+ // Handle NaN.
+ if (LIBC_UNLIKELY(xbits.is_nan())) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ // |x| >= 1.
+ if (LIBC_UNLIKELY(x_abs_u >= 0x3ff0000000000000ULL)) {
+ if (x_abs_u == 0x3ff0000000000000ULL) {
+ // |x| == 1: return +/-inf with ERANGE.
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_DIVBYZERO);
+ return FPBits::inf(sign).get_val();
+ }
+ // |x| > 1: domain error.
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // For very small |x| (|x| <= 2^-27), atanh(x) ~ x.
+ // Use FP arithmetic to ensure FTZ/DAZ mode behavior.
+ if (LIBC_UNLIKELY(x_abs_u <= 0x3e40000000000000ULL)) {
+ double x_abs = xbits.abs().get_val();
+ double x2 = x_abs * x_abs;
+ double result = x_abs + x2 * x_abs * (1.0 / 3.0);
+ return sign == Sign::NEG ? -result : result;
+ }
+
+ // General case: atanh(x) = 0.5 * log((1+x)/(1-x))
+ // = 0.5 * log1p(2x / (1-x))
+ double x_abs = xbits.abs().get_val();
+ double result = 0.5 * math::log1p(2.0 * x_abs / (1.0 - x_abs));
+ return sign == Sign::NEG ? -result : result;
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATANH_H
diff --git a/libc/src/__support/math/cosh.h b/libc/src/__support/math/cosh.h
new file mode 100644
index 0000000000000..8c65e642b98c1
--- /dev/null
+++ b/libc/src/__support/math/cosh.h
@@ -0,0 +1,72 @@
+//===-- Implementation header for cosh --------------------------*- 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_COSH_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COSH_H
+
+#include "exp.h"
+#include "expm1.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double cosh(double x) {
+ using FPBits = fputil::FPBits<double>;
+ FPBits xbits(x);
+ // cosh is even, work with |x|.
+ xbits.set_sign(Sign::POS);
+ x = xbits.get_val();
+
+ uint64_t x_u = xbits.uintval();
+
+ // Handle NaN: cosh(NaN) = NaN.
+ if (LIBC_UNLIKELY(xbits.is_nan())) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ // cosh(+inf) = +inf.
+ if (LIBC_UNLIKELY(xbits.is_inf()))
+ return x + FPBits::inf().get_val();
+
+ // For very small |x| (|x| <= 2^-27), cosh(x) ~ 1.
+ if (LIBC_UNLIKELY(x_u <= 0x3e40000000000000ULL))
+ return 1.0 + x;
+
+ // For |x| >= 710, overflow.
+ // log(DBL_MAX) ~ 709.78, so exp overflows for x > ~709.78.
+ if (LIBC_UNLIKELY(x_u >= 0x4086340000000000ULL)) {
+ int rounding = fputil::quick_get_round();
+ if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
+ return FPBits::max_normal().get_val();
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_OVERFLOW);
+ return x + FPBits::inf().get_val();
+ }
+
+ // cosh(x) = (exp(x) + exp(-x)) / 2.
+ // Use expm1 for small x to avoid cancellation:
+ // cosh(x) = 1 + expm1(x)^2 / (2 * (1 + expm1(x))) ... but for simplicity:
+ double ex = math::exp(x);
+ return (ex + 1.0 / ex) * 0.5;
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSH_H
diff --git a/libc/src/__support/math/sinh.h b/libc/src/__support/math/sinh.h
new file mode 100644
index 0000000000000..bbc7af2a5cacd
--- /dev/null
+++ b/libc/src/__support/math/sinh.h
@@ -0,0 +1,88 @@
+//===-- Implementation header for sinh --------------------------*- 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_SINH_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SINH_H
+
+#include "exp.h"
+#include "expm1.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double sinh(double x) {
+ using FPBits = fputil::FPBits<double>;
+ FPBits xbits(x);
+ bool is_neg = xbits.is_neg();
+ // Work with |x|.
+ xbits.set_sign(Sign::POS);
+ double x_abs = xbits.get_val();
+ uint64_t x_abs_u = xbits.uintval();
+
+ // Handle NaN: sinh(NaN) = NaN.
+ if (LIBC_UNLIKELY(xbits.is_nan())) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ // sinh(+/-inf) = +/-inf.
+ if (LIBC_UNLIKELY(xbits.is_inf()))
+ return x;
+
+ // For very small |x| (|x| <= 2^-26), sinh(x) ~ x.
+ // Use FP arithmetic to ensure FTZ/DAZ mode behavior.
+ if (LIBC_UNLIKELY(x_abs_u <= 0x3e50000000000000ULL)) {
+ double x2 = x_abs * x_abs;
+ double result = x_abs + x2 * x_abs * (1.0 / 6.0);
+ return is_neg ? -result : result;
+ }
+
+ // For |x| >= 710, overflow.
+ if (LIBC_UNLIKELY(x_abs_u >= 0x4086340000000000ULL)) {
+ int rounding = fputil::quick_get_round();
+ if ((rounding == FE_DOWNWARD && !is_neg) ||
+ (rounding == FE_UPWARD && is_neg) || rounding == FE_TOWARDZERO)
+ return is_neg ? -FPBits::max_normal().get_val()
+ : FPBits::max_normal().get_val();
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_OVERFLOW);
+ return x + (is_neg ? -FPBits::inf().get_val() : FPBits::inf().get_val());
+ }
+
+ double result;
+ // For small |x| (|x| < 1), use expm1 to avoid cancellation:
+ // sinh(x) = expm1(x) - expm1(x)^2 / (2*(1+expm1(x)))
+ // = expm1(x) * (1 + expm1(x)) / (2*(1+expm1(x))) ... keep it simple:
+ // sinh(x) = (exp(x) - exp(-x)) / 2
+ if (x_abs_u < 0x3ff0000000000000ULL) {
+ // |x| < 1: use expm1 for accuracy.
+ double em1 = math::expm1(x_abs);
+ result = em1 / 2.0 + em1 / (2.0 * (1.0 + em1));
+ } else {
+ // |x| >= 1.
+ double ex = math::exp(x_abs);
+ result = (ex - 1.0 / ex) * 0.5;
+ }
+
+ return is_neg ? -result : result;
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINH_H
diff --git a/libc/src/__support/math/tanh.h b/libc/src/__support/math/tanh.h
new file mode 100644
index 0000000000000..7e0b47dac463f
--- /dev/null
+++ b/libc/src/__support/math/tanh.h
@@ -0,0 +1,70 @@
+//===-- Implementation header for tanh --------------------------*- 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_TANH_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_TANH_H
+
+#include "expm1.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double tanh(double x) {
+ using FPBits = fputil::FPBits<double>;
+ FPBits xbits(x);
+ bool is_neg = xbits.is_neg();
+ // Work with |x|.
+ xbits.set_sign(Sign::POS);
+ double x_abs = xbits.get_val();
+ uint64_t x_abs_u = xbits.uintval();
+
+ // Handle NaN: tanh(NaN) = NaN.
+ if (LIBC_UNLIKELY(xbits.is_nan())) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ // For very small |x| (|x| <= 2^-27), tanh(x) ~ x.
+ // Use x - x^3/3 to ensure FP operations are performed (for FTZ/DAZ modes).
+ if (LIBC_UNLIKELY(x_abs_u <= 0x3e40000000000000ULL)) {
+ // tanh(+/-0) = +/-0, preserve sign of zero exactly.
+ if (LIBC_UNLIKELY(x_abs_u == 0))
+ return x;
+ double x2 = x_abs * x_abs;
+ double result = x_abs - x2 * x_abs * (1.0 / 3.0);
+ return is_neg ? -result : result;
+ }
+
+ // For |x| >= 20, tanh(x) ~ sign(x) * 1.
+ // (e^40 - 1)/(e^40 + 1) rounds to 1 in double precision.
+ if (LIBC_UNLIKELY(x_abs_u >= 0x4034000000000000ULL)) {
+ // tanh(+/-inf) = +/-1.
+ if (xbits.is_inf())
+ return is_neg ? -1.0 : 1.0;
+ return is_neg ? -1.0 : 1.0;
+ }
+
+ // General case: tanh(x) = expm1(2x) / (expm1(2x) + 2).
+ double e2xm1 = math::expm1(2.0 * x_abs);
+ double result = e2xm1 / (e2xm1 + 2.0);
+ return is_neg ? -result : result;
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANH_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 7ccbddba07b8d..50864369754b2 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3631,6 +3631,16 @@ add_entrypoint_object(
libc.src.__support.math.ufromfpxbf16
)
+add_entrypoint_object(
+ cosh
+ SRCS
+ cosh.cpp
+ HDRS
+ ../cosh.h
+ DEPENDS
+ libc.src.__support.math.cosh
+)
+
add_entrypoint_object(
coshf
SRCS
@@ -3651,6 +3661,16 @@ add_entrypoint_object(
libc.src.__support.math.coshf16
)
+add_entrypoint_object(
+ sinh
+ SRCS
+ sinh.cpp
+ HDRS
+ ../sinh.h
+ DEPENDS
+ libc.src.__support.math.sinh
+)
+
add_entrypoint_object(
sinhf
SRCS
@@ -3671,6 +3691,16 @@ add_entrypoint_object(
libc.src.__support.math.sinhf16
)
+add_entrypoint_object(
+ tanh
+ SRCS
+ tanh.cpp
+ HDRS
+ ../tanh.h
+ DEPENDS
+ libc.src.__support.math.tanh
+)
+
add_entrypoint_object(
tanhf
SRCS
@@ -3691,6 +3721,16 @@ add_entrypoint_object(
libc.src.__support.math.tanhf16
)
+add_entrypoint_object(
+ acosh
+ SRCS
+ acosh.cpp
+ HDRS
+ ../acosh.h
+ DEPENDS
+ libc.src.__support.math.acosh
+)
+
add_entrypoint_object(
acoshf
SRCS
@@ -3722,6 +3762,16 @@ add_entrypoint_object(
libc.src.__support.math.acospif
)
+add_entrypoint_object(
+ asinh
+ SRCS
+ asinh.cpp
+ HDRS
+ ../asinh.h
+ DEPENDS
+ libc.src.__support.math.asinh
+)
+
add_entrypoint_object(
asinhf
SRCS
@@ -3773,6 +3823,16 @@ add_entrypoint_object(
libc.src.__support.math.asinpif16
)
+add_entrypoint_object(
+ atanh
+ SRCS
+ atanh.cpp
+ HDRS
+ ../atanh.h
+ DEPENDS
+ libc.src.__support.math.atanh
+)
+
add_entrypoint_object(
atanhf
SRCS
diff --git a/libc/src/math/generic/acosh.cpp b/libc/src/math/generic/acosh.cpp
new file mode 100644
index 0000000000000..24286644bc9d2
--- /dev/null
+++ b/libc/src/math/generic/acosh.cpp
@@ -0,0 +1,16 @@
+//===-- Double-precision acosh 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/acosh.h"
+#include "src/__support/math/acosh.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, acosh, (double x)) { return math::acosh(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/asinh.cpp b/libc/src/math/generic/asinh.cpp
new file mode 100644
index 0000000000000..6570007a5ac81
--- /dev/null
+++ b/libc/src/math/generic/asinh.cpp
@@ -0,0 +1,16 @@
+//===-- Double-precision asinh 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/asinh.h"
+#include "src/__support/math/asinh.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, asinh, (double x)) { return math::asinh(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/atanh.cpp b/libc/src/math/generic/atanh.cpp
new file mode 100644
index 0000000000000..7713f81a10697
--- /dev/null
+++ b/libc/src/math/generic/atanh.cpp
@@ -0,0 +1,16 @@
+//===-- Double-precision atanh 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/atanh.h"
+#include "src/__support/math/atanh.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, atanh, (double x)) { return math::atanh(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/cosh.cpp b/libc/src/math/generic/cosh.cpp
new file mode 100644
index 0000000000000..2f39fb5845070
--- /dev/null
+++ b/libc/src/math/generic/cosh.cpp
@@ -0,0 +1,16 @@
+//===-- Double-precision cosh 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/cosh.h"
+#include "src/__support/math/cosh.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, cosh, (double x)) { return math::cosh(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/sinh.cpp b/libc/src/math/generic/sinh.cpp
new file mode 100644
index 0000000000000..1820f7133f03b
--- /dev/null
+++ b/libc/src/math/generic/sinh.cpp
@@ -0,0 +1,16 @@
+//===-- Double-precision sinh 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/sinh.h"
+#include "src/__support/math/sinh.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, sinh, (double x)) { return math::sinh(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/tanh.cpp b/libc/src/math/generic/tanh.cpp
new file mode 100644
index 0000000000000..6fd796478130f
--- /dev/null
+++ b/libc/src/math/generic/tanh.cpp
@@ -0,0 +1,16 @@
+//===-- Double-precision tanh 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/tanh.h"
+#include "src/__support/math/tanh.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, tanh, (double x)) { return math::tanh(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 4213c11eca515..d3b0629d3c51f 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2299,6 +2299,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ cosh_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ cosh_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.cosh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
coshf_test
NEED_MPFR
@@ -2326,6 +2339,19 @@ add_fp_unittest(
libc.src.math.coshf16
)
+add_fp_unittest(
+ sinh_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ sinh_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.sinh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
sinhf_test
NEED_MPFR
@@ -2353,6 +2379,18 @@ add_fp_unittest(
libc.src.math.sinhf16
)
+add_fp_unittest(
+ tanh_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ tanh_test.cpp
+ DEPENDS
+ libc.src.math.tanh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
tanhf_test
NEED_MPFR
@@ -2376,6 +2414,19 @@ add_fp_unittest(
libc.src.math.tanhf16
)
+add_fp_unittest(
+ atanh_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ atanh_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.atanh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
atanhf_test
NEED_MPFR
@@ -2441,6 +2492,18 @@ add_fp_unittest(
libc.src.stdlib.srand
)
+add_fp_unittest(
+ asinh_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ asinh_test.cpp
+ DEPENDS
+ libc.src.math.asinh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
asinhf_test
NEED_MPFR
@@ -2464,6 +2527,19 @@ add_fp_unittest(
libc.src.math.asinhf16
)
+add_fp_unittest(
+ acosh_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ acosh_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.acosh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
acoshf_test
NEED_MPFR
diff --git a/libc/test/src/math/acosh_test.cpp b/libc/test/src/math/acosh_test.cpp
new file mode 100644
index 0000000000000..b34633d766ce8
--- /dev/null
+++ b/libc/test/src/math/acosh_test.cpp
@@ -0,0 +1,52 @@
+//===-- Unittests for acosh -----------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/acosh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcAcoshTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcAcoshTest, SpecialNumbers) {
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosh(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosh(0.0));
+ EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosh(-1.0));
+ EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::acosh(1.0));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::acosh(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosh(neg_inf));
+ EXPECT_MATH_ERRNO(EDOM);
+}
+
+TEST_F(LlvmLibcAcoshTest, InDoubleRange) {
+ constexpr uint64_t COUNT = 100'000;
+ constexpr uint64_t STEP = (UINT64_MAX - 0x3ff0000000000000ULL) / COUNT;
+ for (uint64_t i = 0, v = 0x3ff0000000000000ULL; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 1.0)
+ continue;
+ ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, x,
+ LIBC_NAMESPACE::acosh(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/asinh_test.cpp b/libc/test/src/math/asinh_test.cpp
new file mode 100644
index 0000000000000..78ca07b6cdb07
--- /dev/null
+++ b/libc/test/src/math/asinh_test.cpp
@@ -0,0 +1,41 @@
+//===-- Unittests for asinh -----------------------------------------------===//
+//
+// 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 "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/asinh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcAsinhTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcAsinhTest, SpecialNumbers) {
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::asinh(aNaN));
+
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::asinh(0.0));
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::asinh(neg_zero));
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::asinh(inf));
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, LIBC_NAMESPACE::asinh(neg_inf));
+}
+
+TEST_F(LlvmLibcAsinhTest, InDoubleRange) {
+ constexpr uint64_t COUNT = 100'000;
+ constexpr uint64_t STEP = UINT64_MAX / COUNT;
+ for (uint64_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ if (FPBits(v).is_nan() || FPBits(v).is_inf())
+ continue;
+ ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, x,
+ LIBC_NAMESPACE::asinh(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/atanh_test.cpp b/libc/test/src/math/atanh_test.cpp
new file mode 100644
index 0000000000000..78cf6a20099d3
--- /dev/null
+++ b/libc/test/src/math/atanh_test.cpp
@@ -0,0 +1,57 @@
+//===-- Unittests for atanh -----------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/atanh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcAtanhTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcAtanhTest, SpecialNumbers) {
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanh(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::atanh(0.0));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::atanh(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::atanh(1.0));
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, LIBC_NAMESPACE::atanh(-1.0));
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanh(2.0));
+ EXPECT_MATH_ERRNO(EDOM);
+}
+
+TEST_F(LlvmLibcAtanhTest, InDoubleRange) {
+ constexpr uint64_t COUNT = 100'000;
+ // Range: (-1, 1) — use the range of doubles with |x| < 1.
+ constexpr uint64_t STEP = 0x3ff0000000000000ULL / COUNT;
+ for (uint64_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ if (FPBits(v).is_nan() || FPBits(v).is_inf())
+ continue;
+ ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atanh, x,
+ LIBC_NAMESPACE::atanh(x), 0.5);
+ // Also test negative values.
+ if (x != 0.0)
+ ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atanh, -x,
+ LIBC_NAMESPACE::atanh(-x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/cosh_test.cpp b/libc/test/src/math/cosh_test.cpp
new file mode 100644
index 0000000000000..8c6a684313500
--- /dev/null
+++ b/libc/test/src/math/cosh_test.cpp
@@ -0,0 +1,55 @@
+//===-- Unittests for cosh ------------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/cosh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcCoshTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcCoshTest, SpecialNumbers) {
+ EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::cosh(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ(inf, LIBC_NAMESPACE::cosh(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ(inf, LIBC_NAMESPACE::cosh(neg_inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::cosh(0.0));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::cosh(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+}
+
+TEST_F(LlvmLibcCoshTest, Overflow) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ inf, LIBC_NAMESPACE::cosh(FPBits(uint64_t(0x7fefffffffffffffULL)).get_val()),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+}
+
+TEST_F(LlvmLibcCoshTest, InDoubleRange) {
+ constexpr uint64_t COUNT = 100'000;
+ constexpr uint64_t STEP = UINT64_MAX / COUNT;
+ for (uint64_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ if (FPBits(v).is_nan() || FPBits(v).is_inf())
+ continue;
+ ASSERT_MPFR_MATCH(mpfr::Operation::Cosh, x, LIBC_NAMESPACE::cosh(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/sinh_test.cpp b/libc/test/src/math/sinh_test.cpp
new file mode 100644
index 0000000000000..88a29548624bf
--- /dev/null
+++ b/libc/test/src/math/sinh_test.cpp
@@ -0,0 +1,55 @@
+//===-- Unittests for sinh ------------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/sinh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcSinhTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcSinhTest, SpecialNumbers) {
+ EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinh(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ(inf, LIBC_NAMESPACE::sinh(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::sinh(neg_inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::sinh(0.0));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinh(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+}
+
+TEST_F(LlvmLibcSinhTest, Overflow) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ inf, LIBC_NAMESPACE::sinh(FPBits(uint64_t(0x7fefffffffffffffULL)).get_val()),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+}
+
+TEST_F(LlvmLibcSinhTest, InDoubleRange) {
+ constexpr uint64_t COUNT = 100'000;
+ constexpr uint64_t STEP = UINT64_MAX / COUNT;
+ for (uint64_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ if (FPBits(v).is_nan() || FPBits(v).is_inf())
+ continue;
+ ASSERT_MPFR_MATCH(mpfr::Operation::Sinh, x, LIBC_NAMESPACE::sinh(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 28b85b1a25bbd..07062d37ab076 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4622,6 +4622,18 @@ add_fp_unittest(
UNIT_TEST_ONLY
)
+add_fp_unittest(
+ cosh_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ cosh_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.cosh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
coshf_test
SUITE
@@ -4648,6 +4660,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ sinh_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ sinh_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.sinh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
sinhf_test
SUITE
@@ -4674,6 +4698,17 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ tanh_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ tanh_test.cpp
+ DEPENDS
+ libc.src.math.tanh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
tanhf_test
SUITE
@@ -4699,6 +4734,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.cast
)
+add_fp_unittest(
+ atanh_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ atanh_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.atanh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
atanhf_test
SUITE
@@ -4734,6 +4781,17 @@ add_fp_unittest(
libc.src.math.atanpif16
)
+add_fp_unittest(
+ asinh_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ asinh_test.cpp
+ DEPENDS
+ libc.src.math.asinh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
asinhf_test
SUITE
@@ -4793,6 +4851,18 @@ add_fp_unittest(
libc.src.math.asinpif16
)
+add_fp_unittest(
+ acosh_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ acosh_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.acosh
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
acoshf_test
SUITE
diff --git a/libc/test/src/math/smoke/acosh_test.cpp b/libc/test/src/math/smoke/acosh_test.cpp
new file mode 100644
index 0000000000000..113b5639205dd
--- /dev/null
+++ b/libc/test/src/math/smoke/acosh_test.cpp
@@ -0,0 +1,61 @@
+//===-- Unittests for acosh -----------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/acosh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcAcoshTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcAcoshTest, SpecialNumbers) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acosh(sNaN), FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosh(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosh(0.0));
+ EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::acosh(1.0));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::acosh(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosh(neg_inf));
+ EXPECT_MATH_ERRNO(EDOM);
+}
+
+#ifdef LIBC_TEST_FTZ_DAZ
+
+using namespace LIBC_NAMESPACE::testing;
+
+TEST_F(LlvmLibcAcoshTest, FTZMode) {
+ ModifyMXCSR mxcsr(FTZ);
+
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::acosh(min_denormal));
+}
+
+TEST_F(LlvmLibcAcoshTest, DAZMode) {
+ ModifyMXCSR mxcsr(DAZ);
+
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::acosh(min_denormal));
+}
+
+TEST_F(LlvmLibcAcoshTest, FTZDAZMode) {
+ ModifyMXCSR mxcsr(FTZ | DAZ);
+
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::acosh(min_denormal));
+}
+
+#endif
diff --git a/libc/test/src/math/smoke/asinh_test.cpp b/libc/test/src/math/smoke/asinh_test.cpp
new file mode 100644
index 0000000000000..604645014b077
--- /dev/null
+++ b/libc/test/src/math/smoke/asinh_test.cpp
@@ -0,0 +1,52 @@
+//===-- Unittests for asinh -----------------------------------------------===//
+//
+// 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 "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/asinh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcAsinhTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcAsinhTest, SpecialNumbers) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::asinh(sNaN), FE_INVALID);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::asinh(aNaN));
+
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::asinh(0.0));
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::asinh(neg_zero));
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::asinh(inf));
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, LIBC_NAMESPACE::asinh(neg_inf));
+}
+
+#ifdef LIBC_TEST_FTZ_DAZ
+
+using namespace LIBC_NAMESPACE::testing;
+
+TEST_F(LlvmLibcAsinhTest, FTZMode) {
+ ModifyMXCSR mxcsr(FTZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::asinh(min_denormal));
+}
+
+TEST_F(LlvmLibcAsinhTest, DAZMode) {
+ ModifyMXCSR mxcsr(DAZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::asinh(min_denormal));
+}
+
+TEST_F(LlvmLibcAsinhTest, FTZDAZMode) {
+ ModifyMXCSR mxcsr(FTZ | DAZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::asinh(min_denormal));
+}
+
+#endif
diff --git a/libc/test/src/math/smoke/atanh_test.cpp b/libc/test/src/math/smoke/atanh_test.cpp
new file mode 100644
index 0000000000000..0f69bfb2f1a60
--- /dev/null
+++ b/libc/test/src/math/smoke/atanh_test.cpp
@@ -0,0 +1,65 @@
+//===-- Unittests for atanh -----------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/atanh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcAtanhTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcAtanhTest, SpecialNumbers) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::atanh(sNaN), FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanh(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::atanh(0.0));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::atanh(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::atanh(1.0), FE_DIVBYZERO);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::atanh(-1.0),
+ FE_DIVBYZERO);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::atanh(2.0), FE_INVALID);
+ EXPECT_MATH_ERRNO(EDOM);
+}
+
+#ifdef LIBC_TEST_FTZ_DAZ
+
+using namespace LIBC_NAMESPACE::testing;
+
+TEST_F(LlvmLibcAtanhTest, FTZMode) {
+ ModifyMXCSR mxcsr(FTZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::atanh(min_denormal));
+}
+
+TEST_F(LlvmLibcAtanhTest, DAZMode) {
+ ModifyMXCSR mxcsr(DAZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::atanh(min_denormal));
+}
+
+TEST_F(LlvmLibcAtanhTest, FTZDAZMode) {
+ ModifyMXCSR mxcsr(FTZ | DAZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::atanh(min_denormal));
+}
+
+#endif
diff --git a/libc/test/src/math/smoke/cosh_test.cpp b/libc/test/src/math/smoke/cosh_test.cpp
new file mode 100644
index 0000000000000..2d19c73963957
--- /dev/null
+++ b/libc/test/src/math/smoke/cosh_test.cpp
@@ -0,0 +1,72 @@
+//===-- Unittests for cosh ------------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/cosh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcCoshTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcCoshTest, SpecialNumbers) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::cosh(sNaN), FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::cosh(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::cosh(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::cosh(neg_inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::cosh(0.0));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::cosh(-0.0));
+ EXPECT_MATH_ERRNO(0);
+}
+
+TEST_F(LlvmLibcCoshTest, Overflow) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ inf,
+ LIBC_NAMESPACE::cosh(FPBits(uint64_t(0x7fefffffffffffffULL)).get_val()),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+}
+
+#ifdef LIBC_TEST_FTZ_DAZ
+
+using namespace LIBC_NAMESPACE::testing;
+
+TEST_F(LlvmLibcCoshTest, FTZMode) {
+ ModifyMXCSR mxcsr(FTZ);
+
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::cosh(min_denormal));
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::cosh(max_denormal));
+}
+
+TEST_F(LlvmLibcCoshTest, DAZMode) {
+ ModifyMXCSR mxcsr(DAZ);
+
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::cosh(min_denormal));
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::cosh(max_denormal));
+}
+
+TEST_F(LlvmLibcCoshTest, FTZDAZMode) {
+ ModifyMXCSR mxcsr(FTZ | DAZ);
+
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::cosh(min_denormal));
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::cosh(max_denormal));
+}
+
+#endif
diff --git a/libc/test/src/math/smoke/sinh_test.cpp b/libc/test/src/math/smoke/sinh_test.cpp
new file mode 100644
index 0000000000000..fb10173ad8243
--- /dev/null
+++ b/libc/test/src/math/smoke/sinh_test.cpp
@@ -0,0 +1,69 @@
+//===-- Unittests for sinh ------------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/sinh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcSinhTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcSinhTest, SpecialNumbers) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::sinh(sNaN), FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::sinh(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::sinh(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, LIBC_NAMESPACE::sinh(neg_inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::sinh(0.0));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::sinh(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+}
+
+TEST_F(LlvmLibcSinhTest, Overflow) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ inf,
+ LIBC_NAMESPACE::sinh(FPBits(uint64_t(0x7fefffffffffffffULL)).get_val()),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+}
+
+#ifdef LIBC_TEST_FTZ_DAZ
+
+using namespace LIBC_NAMESPACE::testing;
+
+TEST_F(LlvmLibcSinhTest, FTZMode) {
+ ModifyMXCSR mxcsr(FTZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::sinh(min_denormal));
+}
+
+TEST_F(LlvmLibcSinhTest, DAZMode) {
+ ModifyMXCSR mxcsr(DAZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::sinh(min_denormal));
+}
+
+TEST_F(LlvmLibcSinhTest, FTZDAZMode) {
+ ModifyMXCSR mxcsr(FTZ | DAZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::sinh(min_denormal));
+}
+
+#endif
diff --git a/libc/test/src/math/smoke/tanh_test.cpp b/libc/test/src/math/smoke/tanh_test.cpp
new file mode 100644
index 0000000000000..850ea36337c13
--- /dev/null
+++ b/libc/test/src/math/smoke/tanh_test.cpp
@@ -0,0 +1,52 @@
+//===-- Unittests for tanh ------------------------------------------------===//
+//
+// 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 "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/tanh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcTanhTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcTanhTest, SpecialNumbers) {
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tanh(sNaN), FE_INVALID);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::tanh(aNaN));
+
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::tanh(0.0));
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::tanh(neg_zero));
+
+ EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::tanh(inf));
+ EXPECT_FP_EQ_ALL_ROUNDING(-1.0, LIBC_NAMESPACE::tanh(neg_inf));
+}
+
+#ifdef LIBC_TEST_FTZ_DAZ
+
+using namespace LIBC_NAMESPACE::testing;
+
+TEST_F(LlvmLibcTanhTest, FTZMode) {
+ ModifyMXCSR mxcsr(FTZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::tanh(min_denormal));
+}
+
+TEST_F(LlvmLibcTanhTest, DAZMode) {
+ ModifyMXCSR mxcsr(DAZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::tanh(min_denormal));
+}
+
+TEST_F(LlvmLibcTanhTest, FTZDAZMode) {
+ ModifyMXCSR mxcsr(FTZ | DAZ);
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::tanh(min_denormal));
+}
+
+#endif
diff --git a/libc/test/src/math/tanh_test.cpp b/libc/test/src/math/tanh_test.cpp
new file mode 100644
index 0000000000000..83f9c1201074d
--- /dev/null
+++ b/libc/test/src/math/tanh_test.cpp
@@ -0,0 +1,40 @@
+//===-- Unittests for tanh ------------------------------------------------===//
+//
+// 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 "hdr/math_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/tanh.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcTanhTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcTanhTest, SpecialNumbers) {
+ EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::tanh(aNaN));
+
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::tanh(0.0));
+ EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::tanh(neg_zero));
+
+ EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::tanh(inf));
+ EXPECT_FP_EQ(-1.0, LIBC_NAMESPACE::tanh(neg_inf));
+}
+
+TEST_F(LlvmLibcTanhTest, InDoubleRange) {
+ constexpr uint64_t COUNT = 100'000;
+ constexpr uint64_t STEP = UINT64_MAX / COUNT;
+ for (uint64_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ if (FPBits(v).is_nan() || FPBits(v).is_inf())
+ continue;
+ ASSERT_MPFR_MATCH(mpfr::Operation::Tanh, x, LIBC_NAMESPACE::tanh(x), 0.5);
+ }
+}
- Previous message: [libc-commits] [libc] [libc][math] Add double-precision acosh, asinh, atanh, cosh, sinh, tanh (PR #199442)
- Next message: [libc-commits] [libc] [libc][math] Add double-precision acosh, asinh, atanh, cosh, sinh, tanh (PR #199442)
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
More information about the libc-commits
mailing list