[libc-commits] [libc] [llvm] [libc][math] Implement double-precision acosh (PR #199953)

Aayush Shrivastava via libc-commits libc-commits at lists.llvm.org
Thu May 28 03:36:11 PDT 2026


https://github.com/iamaayushrivastava updated https://github.com/llvm/llvm-project/pull/199953

>From 1e932451d72eebc598b7972e1556b015ddf05a4e Mon Sep 17 00:00:00 2001
From: Aayush Shrivastava <iamaayushrivastava at gmail.com>
Date: Wed, 27 May 2026 01:21:50 +0530
Subject: [PATCH 1/2] [libc][math] Implement double-precision acosh

---
 libc/config/linux/x86_64/entrypoints.txt |  1 +
 libc/src/__support/math/CMakeLists.txt   | 13 ++++
 libc/src/__support/math/acosh.h          | 67 ++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt     | 10 +++
 libc/src/math/generic/acosh.cpp          | 16 +++++
 libc/test/src/math/CMakeLists.txt        | 12 ++++
 libc/test/src/math/acosh_test.cpp        | 87 ++++++++++++++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt  | 12 ++++
 libc/test/src/math/smoke/acosh_test.cpp  | 66 ++++++++++++++++++
 9 files changed, 284 insertions(+)
 create mode 100644 libc/src/__support/math/acosh.h
 create mode 100644 libc/src/math/generic/acosh.cpp
 create mode 100644 libc/test/src/math/acosh_test.cpp
 create mode 100644 libc/test/src/math/smoke/acosh_test.cpp

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

>From bf3b6ee797da913966af741c46c14b4be73b9a33 Mon Sep 17 00:00:00 2001
From: Aayush Shrivastava <iamaayushrivastava at gmail.com>
Date: Thu, 28 May 2026 16:05:41 +0530
Subject: [PATCH 2/2] [libc][math] Address review and complete acosh
 double-precision registration

---
 libc/config/baremetal/aarch64/entrypoints.txt |  1 +
 libc/config/baremetal/arm/entrypoints.txt     |  1 +
 libc/config/baremetal/riscv/entrypoints.txt   |  1 +
 libc/config/darwin/aarch64/entrypoints.txt    |  1 +
 libc/config/freebsd/x86_64/entrypoints.txt    |  2 +
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/arm/entrypoints.txt         |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/windows/entrypoints.txt           |  1 +
 libc/include/math.yaml                        |  6 +++
 libc/shared/math.h                            |  1 +
 libc/shared/math/acosh.h                      | 23 ++++++++++++
 libc/src/__support/math/CMakeLists.txt        |  1 +
 libc/src/__support/math/acosh.h               | 37 ++++++++-----------
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  1 +
 libc/test/src/math/smoke/acosh_test.cpp       | 18 ++++-----
 .../llvm-project-overlay/libc/BUILD.bazel     | 19 ++++++++++
 18 files changed, 87 insertions(+), 30 deletions(-)
 create mode 100644 libc/shared/math/acosh.h

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index dcb50135232e2..fc736ec4bae9e 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -345,6 +345,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/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index fac62bac939cc..ba61b39cfc3df 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -357,6 +357,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/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index a3b96225ff09d..011ec15f679d4 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -354,6 +354,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 
     # math.h entrypoints
     libc.src.math.acosf
+    libc.src.math.acosh
     libc.src.math.acoshf
     libc.src.math.acospif
     libc.src.math.asinf
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 914d2b7918da1..3f1544f1032a6 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -166,6 +166,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/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 31d421555624d..094df4187e2d9 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -120,6 +120,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 
     # math.h entrypoints
     libc.src.math.acosf
+    libc.src.math.acosh
     libc.src.math.acoshf
     libc.src.math.asinf
     libc.src.math.asinhf
@@ -291,6 +292,7 @@ set(TARGET_LIBM_ENTRYPOINTS
 
 list(APPEND TARGET_LIBM_ENTRYPOINTS
     libc.src.math.acos
+    libc.src.math.acosh
     libc.src.math.acospif
     libc.src.math.asin
     libc.src.math.asinpi
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index a50923fddec54..5925c3870ec56 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -470,6 +470,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/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 49b30ef1830f3..ff837b6561814 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -270,6 +270,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/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 51c769d853a52..9cf3478319580 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -474,6 +474,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/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 94d1d00e676d9..8c20202c1e9e3 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -146,6 +146,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/include/math.yaml b/libc/include/math.yaml
index e50be8f05cc65..8177f9fc6217e 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
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 2baeb07294a3b..e60d3d172fc9a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -14,6 +14,7 @@
 #include "math/acos.h"
 #include "math/acosf.h"
 #include "math/acosf16.h"
+#include "math/acosh.h"
 #include "math/acoshf.h"
 #include "math/acoshf16.h"
 #include "math/acospif.h"
diff --git a/libc/shared/math/acosh.h b/libc/shared/math/acosh.h
new file mode 100644
index 0000000000000..6dcf4921f3606
--- /dev/null
+++ b/libc/shared/math/acosh.h
@@ -0,0 +1,23 @@
+//===-- Shared acosh function -----------------------------------*- 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_SHARED_MATH_ACOSH_H
+#define LLVM_LIBC_SHARED_MATH_ACOSH_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/acosh.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::acosh;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ACOSH_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4308a015a3986..4796e195da4a3 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -104,6 +104,7 @@ add_header_library(
     .log
     libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
     libc.src.__support.FPUtil.sqrt
     libc.src.__support.macros.config
     libc.src.__support.macros.optimization
diff --git a/libc/src/__support/math/acosh.h b/libc/src/__support/math/acosh.h
index a3485558d8580..efa5190c583e4 100644
--- a/libc/src/__support/math/acosh.h
+++ b/libc/src/__support/math/acosh.h
@@ -12,6 +12,7 @@
 #include "log.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/multiply_add.h"
 #include "src/__support/FPUtil/sqrt.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
@@ -25,38 +26,32 @@ LIBC_INLINE double acosh(double x) {
   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)) {
+  // acosh is defined only for x >= 1.  The comparison x <= 1.0 is false for
+  // NaN, so NaN falls through to the large-x / NaN path below.
+  if (LIBC_UNLIKELY(x <= 1.0)) {
+    if (x == 1.0)
+      return 0.0;
     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).
+  // For large x (x >= 2^28) and for NaN / +inf.
   if (LIBC_UNLIKELY(x_u >= 0x41b0000000000000ULL)) {
+    if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+      return x; // +inf (negative inf is excluded by x <= 1.0 above)
+    }
+    // acosh(x) = log(2x) + O(1/x^2); for x >= 2^28 the correction is < 0.5 ULP.
     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;
+  double x2m1 = fputil::multiply_add(x, x, -1.0);
   return math::log(x + fputil::sqrt<double>(x2m1));
 }
 
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 84a8d4bf79b3d..8d6801ba06096 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -11,6 +11,7 @@ add_fp_unittest(
     libc.src.__support.math.acos
     libc.src.__support.math.acosf
     libc.src.__support.math.acosf16
+    libc.src.__support.math.acosh
     libc.src.__support.math.acoshf
     libc.src.__support.math.acoshf16
     libc.src.__support.math.acospif
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 634778380dc8e..71baadc07f029 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -323,6 +323,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   LIBC_NAMESPACE::shared::sincos(0.0, &sin, &cos);
 
   EXPECT_FP_EQ(0x1.921fb54442d18p+0, LIBC_NAMESPACE::shared::acos(0.0));
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::acosh(1.0));
   EXPECT_FP_EQ(0., LIBC_NAMESPACE::shared::asin(0.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::asinpi(0.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::atan(0.0));
diff --git a/libc/test/src/math/smoke/acosh_test.cpp b/libc/test/src/math/smoke/acosh_test.cpp
index a35a6e1c99d55..8a6ff95d9cbef 100644
--- a/libc/test/src/math/smoke/acosh_test.cpp
+++ b/libc/test/src/math/smoke/acosh_test.cpp
@@ -13,6 +13,8 @@
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
+#include "hdr/stdint_proxy.h"
+
 using LlvmLibcAcoshTest = LIBC_NAMESPACE::testing::FPTest<double>;
 
 TEST_F(LlvmLibcAcoshTest, SpecialNumbers) {
@@ -22,19 +24,17 @@ TEST_F(LlvmLibcAcoshTest, SpecialNumbers) {
   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_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosh(0.0));
   EXPECT_MATH_ERRNO(EDOM);
 
-  // acosh(1) = 0.
   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
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 87963e4524b39..0d05d9a0705c9 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3489,6 +3489,20 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_acosh",
+    hdrs = ["src/__support/math/acosh.h"],
+    deps = [
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_multiply_add",
+        ":__support_fputil_sqrt",
+        ":__support_macros_config",
+        ":__support_macros_optimization",
+        ":__support_math_log",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_acoshf",
     hdrs = ["src/__support/math/acoshf.h"],
@@ -9498,6 +9512,11 @@ libc_math_function(
     ],
 )
 
+libc_math_function(
+    name = "acosh",
+    additional_deps = [":__support_math_acosh"],
+)
+
 libc_math_function(
     name = "acoshf",
     additional_deps = [":__support_math_acoshf"],



More information about the libc-commits mailing list