[libc-commits] [libc] [libc][math] Implement double-precision acosh (PR #199953)
via libc-commits
libc-commits at lists.llvm.org
Wed May 27 03:52:48 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Aayush Shrivastava (iamaayushrivastava)
<details>
<summary>Changes</summary>
This PR adds an implementation of the double-precision inverse hyperbolic cosine function `acosh` to the `math` library.
---
Full diff: https://github.com/llvm/llvm-project/pull/199953.diff
9 Files Affected:
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
- (modified) libc/src/__support/math/CMakeLists.txt (+13)
- (added) libc/src/__support/math/acosh.h (+67)
- (modified) libc/src/math/generic/CMakeLists.txt (+10)
- (added) libc/src/math/generic/acosh.cpp (+16)
- (modified) libc/test/src/math/CMakeLists.txt (+12)
- (added) libc/test/src/math/acosh_test.cpp (+87)
- (modified) libc/test/src/math/smoke/CMakeLists.txt (+12)
- (added) libc/test/src/math/smoke/acosh_test.cpp (+66)
``````````diff
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5545790fecd85..9f05ecd0fa127 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -534,6 +534,7 @@ 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
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 9e3ec26cdc881..4308a015a3986 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -96,6 +96,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(
acospif
HDRS
diff --git a/libc/src/__support/math/acosh.h b/libc/src/__support/math/acosh.h
new file mode 100644
index 0000000000000..a3485558d8580
--- /dev/null
+++ b/libc/src/__support/math/acosh.h
@@ -0,0 +1,67 @@
+//===-- 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);
+ uint64_t x_u = xbits.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;
+ }
+
+ // acosh(+inf) = +inf.
+ if (LIBC_UNLIKELY(xbits.is_inf()))
+ return x;
+
+ // Domain error: acosh(x) is undefined for x < 1.
+ if (LIBC_UNLIKELY(x_u < 0x3ff0000000000000ULL)) {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // acosh(1) = 0.
+ if (LIBC_UNLIKELY(x_u == 0x3ff0000000000000ULL))
+ return 0.0;
+
+ // For large x (x >= 2^28): acosh(x) ~ log(2x) = log(x) + log(2).
+ if (LIBC_UNLIKELY(x_u >= 0x41b0000000000000ULL)) {
+ 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/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 7ccbddba07b8d..3a7e3356f6315 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3691,6 +3691,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
diff --git a/libc/src/math/generic/acosh.cpp b/libc/src/math/generic/acosh.cpp
new file mode 100644
index 0000000000000..6f9490a5e309c
--- /dev/null
+++ b/libc/src/math/generic/acosh.cpp
@@ -0,0 +1,16 @@
+//===-- Double-precision acosh implementation -----------------------------===//
+//
+// 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/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 4213c11eca515..ddd14326ef2b9 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2464,6 +2464,18 @@ add_fp_unittest(
libc.src.math.asinhf16
)
+add_fp_unittest(
+ acosh_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ acosh_test.cpp
+ DEPENDS
+ 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..5774518d2ea04
--- /dev/null
+++ b/libc/test/src/math/acosh_test.cpp
@@ -0,0 +1,87 @@
+//===-- 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 "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;
+
+using LIBC_NAMESPACE::testing::tlog;
+
+TEST_F(LlvmLibcAcoshTest, InDoubleRange) {
+ // acosh is defined on [1, +inf).
+ // We test two sub-ranges:
+ // [1.0, 2.0) — near the branch point
+ // [2.0, large) — general range
+ constexpr uint64_t COUNT = 123'451;
+ uint64_t START = FPBits(1.0).uintval();
+ uint64_t STOP = FPBits(0x1.0p52).uintval(); // 2^52
+ uint64_t STEP = (STOP - START) / COUNT;
+
+ auto test = [&](mpfr::RoundingMode rounding_mode) {
+ mpfr::ForceRoundingMode __r(rounding_mode);
+ if (!__r.success)
+ return;
+
+ uint64_t fails = 0;
+ uint64_t count = 0;
+ uint64_t cc = 0;
+ double mx = 0.0, mr = 0.0;
+ double tol = 0.5;
+
+ for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ if (FPBits(v).is_inf_or_nan())
+ continue;
+ double result = LIBC_NAMESPACE::acosh(x);
+ ++cc;
+ if (FPBits(result).is_inf_or_nan())
+ continue;
+
+ ++count;
+
+ if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Acosh, x, result,
+ 0.5, rounding_mode)) {
+ ++fails;
+ while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Acosh, x,
+ result, tol, rounding_mode)) {
+ mx = x;
+ mr = result;
+
+ if (tol > 1000.0)
+ break;
+
+ tol *= 2.0;
+ }
+ }
+ }
+ if (fails) {
+ tlog << " Acosh failed: " << fails << "/" << count << "/" << cc
+ << " tests.\n";
+ tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
+ EXPECT_MPFR_MATCH(mpfr::Operation::Acosh, mx, mr, 0.5, rounding_mode);
+ }
+ };
+
+ tlog << " Test Rounding To Nearest...\n";
+ test(mpfr::RoundingMode::Nearest);
+
+ tlog << " Test Rounding Downward...\n";
+ test(mpfr::RoundingMode::Downward);
+
+ tlog << " Test Rounding Upward...\n";
+ test(mpfr::RoundingMode::Upward);
+
+ tlog << " Test Rounding Toward Zero...\n";
+ test(mpfr::RoundingMode::TowardZero);
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 28b85b1a25bbd..db779a1268bd5 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4793,6 +4793,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..a35a6e1c99d55
--- /dev/null
+++ b/libc/test/src/math/smoke/acosh_test.cpp
@@ -0,0 +1,66 @@
+//===-- 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 "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(inf, LIBC_NAMESPACE::acosh(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ // acosh(x) for x < 1 is a domain error.
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acosh(0.0), FE_INVALID);
+ EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acosh(-1.0), FE_INVALID);
+ EXPECT_MATH_ERRNO(EDOM);
+
+ // acosh(1) = 0.
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::acosh(1.0));
+ EXPECT_MATH_ERRNO(0);
+}
+
+#ifdef LIBC_TEST_FTZ_DAZ
+
+using namespace LIBC_NAMESPACE::testing;
+
+TEST_F(LlvmLibcAcoshTest, FTZMode) {
+ ModifyMXCSR mxcsr(FTZ);
+
+ // acosh(x) for x < 1 is still a domain error in FTZ mode.
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::acosh(min_denormal));
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::acosh(max_denormal));
+}
+
+TEST_F(LlvmLibcAcoshTest, DAZMode) {
+ ModifyMXCSR mxcsr(DAZ);
+
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::acosh(min_denormal));
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::acosh(max_denormal));
+}
+
+TEST_F(LlvmLibcAcoshTest, FTZDAZMode) {
+ ModifyMXCSR mxcsr(FTZ | DAZ);
+
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::acosh(min_denormal));
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::acosh(max_denormal));
+}
+
+#endif
``````````
</details>
https://github.com/llvm/llvm-project/pull/199953
More information about the libc-commits
mailing list