[libc-commits] [libc] [libc][math][c23] Add acoshf16 C23 math function. (PR #130588)

Harrison Hao via libc-commits libc-commits at lists.llvm.org
Mon Mar 10 06:58:52 PDT 2025


https://github.com/harrisonGPU updated https://github.com/llvm/llvm-project/pull/130588

>From 4fc4ced7e6a0fac40bedf1319af03a40d7adef7c Mon Sep 17 00:00:00 2001
From: Harrison Hao <tsworld1314 at gmail.com>
Date: Mon, 10 Mar 2025 20:04:11 +0800
Subject: [PATCH 1/2] [libc][math][c23] Add acoshf16 C23 math function.

---
 libc/config/linux/x86_64/entrypoints.txt   |  1 +
 libc/docs/headers/math/index.rst           |  2 +-
 libc/include/math.yaml                     |  7 +++
 libc/src/math/CMakeLists.txt               |  1 +
 libc/src/math/acoshf16.h                   | 21 +++++++++
 libc/src/math/generic/CMakeLists.txt       | 20 ++++++++
 libc/src/math/generic/acoshf16.cpp         | 55 ++++++++++++++++++++++
 libc/test/src/math/CMakeLists.txt          | 11 +++++
 libc/test/src/math/acoshf16_test.cpp       | 52 ++++++++++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt    | 11 +++++
 libc/test/src/math/smoke/acoshf16_test.cpp | 41 ++++++++++++++++
 11 files changed, 221 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/math/acoshf16.h
 create mode 100644 libc/src/math/generic/acoshf16.cpp
 create mode 100644 libc/test/src/math/acoshf16_test.cpp
 create mode 100644 libc/test/src/math/smoke/acoshf16_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 12dc87bf945fd..b705d81fff79c 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -653,6 +653,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     # math.h C23 _Float16 entrypoints
     libc.src.math.asinf16
     libc.src.math.acosf16
+    libc.src.math.acoshf16
     libc.src.math.canonicalizef16
     libc.src.math.ceilf16
     libc.src.math.copysignf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 5b855ce4881c3..e137626361f48 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -251,7 +251,7 @@ Higher Math Functions
 +===========+==================+=================+========================+======================+========================+========================+============================+
 | acos      | |check|          |                 |                        | |check|              |                        | 7.12.4.1               | F.10.1.1                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| acosh     | |check|          |                 |                        |                      |                        | 7.12.5.1               | F.10.2.1                   |
+| acosh     | |check|          |                 |                        | |check|                  |                        | 7.12.5.1               | F.10.2.1                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | acospi    |                  |                 |                        |                      |                        | 7.12.4.8               | F.10.1.8                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/include/math.yaml b/libc/include/math.yaml
index a66f981030864..7fd73445cc74e 100644
--- a/libc/include/math.yaml
+++ b/libc/include/math.yaml
@@ -27,6 +27,13 @@ functions:
     return_type: float
     arguments:
       - type: float
+    name: acoshf16
+    standards:
+      - stdc
+    return_type: _Float16
+    arguments:
+      - type: _Float16
+    guard: LIBC_TYPES_HAS_FLOAT16
   - name: asin
     standards:
       - stdc
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index f18a73d46f9aa..bdf8b923416d8 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -46,6 +46,7 @@ add_math_entrypoint_object(acosf16)
 
 add_math_entrypoint_object(acosh)
 add_math_entrypoint_object(acoshf)
+add_math_entrypoint_object(acoshf16)
 
 add_math_entrypoint_object(asin)
 add_math_entrypoint_object(asinf)
diff --git a/libc/src/math/acoshf16.h b/libc/src/math/acoshf16.h
new file mode 100644
index 0000000000000..a23426ffe589d
--- /dev/null
+++ b/libc/src/math/acoshf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for acoshf16 -----------------------*- 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_MATH_ACOSHF16_H
+#define LLVM_LIBC_SRC_MATH_ACOSHF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 acoshf16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ACOSHF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3114289bad486..be3634dfebdc7 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3944,6 +3944,26 @@ add_entrypoint_object(
     libc.src.__support.macros.optimization
 )
 
+add_entrypoint_object(
+  acoshf16
+  SRCS
+    acoshf16.cpp
+  HDRS
+    ../acoshf16.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.FPUtil.sqrt
+    libc.src.__support.macros.optimization
+    libc.src.__support.macros.properties.types 
+)
+
 add_entrypoint_object(
   asinhf
   SRCS
diff --git a/libc/src/math/generic/acoshf16.cpp b/libc/src/math/generic/acoshf16.cpp
new file mode 100644
index 0000000000000..01f97f536c256
--- /dev/null
+++ b/libc/src/math/generic/acoshf16.cpp
@@ -0,0 +1,55 @@
+//===-- Half-precision acoshf16 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/acoshf16.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/macros/optimization.h"
+#include "src/math/generic/explogxf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, acoshf16, (float16 x)) {
+  using FPBits = fputil::FPBits<float16>;
+  FPBits xbits(x);
+
+  uint16_t x_u = xbits.uintval();
+  uint16_t x_abs = x_u & 0x7fff;
+
+  // acoshf16(x) domain: x >= 1.0
+  if (LIBC_UNLIKELY(x_abs < 0x3c00)) {
+    fputil::set_errno_if_required(EDOM);
+    fputil::raise_except_if_required(FE_INVALID);
+    return FPBits::quiet_nan().get_val();
+  }
+
+  if (LIBC_UNLIKELY(x == float16(1.0f)))
+    return float16(0.0f);
+
+  // acosh(inf) = inf, acosh(NaN) = NaN
+  if (LIBC_UNLIKELY(xbits.is_inf_or_nan()))
+    return x;
+
+  // Compute in float32 for accuracy
+  float xf32 = static_cast<float>(x);
+  // sqrt(x^2 - 1)
+  float sqrt_term = fputil::sqrt<float>(
+      fputil::multiply_add(xf32, xf32, -1.0f));
+
+  // log(x + sqrt(x^2 - 1))
+  float result = static_cast<float>(log_eval(xf32 + sqrt_term));
+
+  return fputil::cast<float16>(result);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 53ddd301900c0..311d56a2a33ab 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2173,6 +2173,17 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  acoshf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    acoshf16_test.cpp
+  DEPENDS
+    libc.src.math.acoshf16
+)
+
 add_fp_unittest(
   asinf_test
   NEED_MPFR
diff --git a/libc/test/src/math/acoshf16_test.cpp b/libc/test/src/math/acoshf16_test.cpp
new file mode 100644
index 0000000000000..d4ebef87f3b61
--- /dev/null
+++ b/libc/test/src/math/acoshf16_test.cpp
@@ -0,0 +1,52 @@
+//===-- Unittests for acoshf16 ---------------------------------------------===//
+//
+// 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/errno/libc_errno.h"
+#include "src/math/acoshf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcAcoshf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcAcoshf16Test, SpecialNumbers) {
+  LIBC_NAMESPACE::libc_errno = 0;
+
+  // NaN input
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(aNaN));
+  EXPECT_MATH_ERRNO(0);
+
+  // acoshf16(1.0) = 0
+  EXPECT_FP_EQ_ALL_ROUNDING(float16(0.0f), LIBC_NAMESPACE::acoshf16(float16(1.0f)));
+  EXPECT_MATH_ERRNO(0);
+
+  // Domain error (x < 1)
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(float16(0.5f)));
+  EXPECT_MATH_ERRNO(EDOM);
+
+  // acoshf16(+inf) = inf
+  EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::acoshf16(inf));
+  EXPECT_MATH_ERRNO(0);
+
+  // acoshf16(x) domain error (negative infinity)
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(neg_inf));
+  EXPECT_MATH_ERRNO(EDOM);
+}
+
+TEST_F(LlvmLibcAcoshf16Test, InFloat16Range) {
+  constexpr uint16_t START = 0x3c00U; // 1.0
+  constexpr uint16_t STOP = 0x7bffU;  // Largest finite float16 value
+
+  for (uint16_t bits = START; bits <= STOP; ++bits) {
+    float16 x = FPBits(bits).get_val();
+    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, float(x),
+                                   float(LIBC_NAMESPACE::acoshf16(x)), 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 6f94440d826d9..4303afd242e22 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3945,6 +3945,17 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  acoshf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    acoshf16_test.cpp
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.math.acoshf16  
+)
+
 add_fp_unittest(
   asinf_test
   SUITE
diff --git a/libc/test/src/math/smoke/acoshf16_test.cpp b/libc/test/src/math/smoke/acoshf16_test.cpp
new file mode 100644
index 0000000000000..e197f3f305834
--- /dev/null
+++ b/libc/test/src/math/smoke/acoshf16_test.cpp
@@ -0,0 +1,41 @@
+//===-- Unittests for acoshf16 --------------------------------------------===//
+//
+// 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/errno/libc_errno.h"
+#include "src/math/acoshf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcAcoshf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+
+TEST_F(LlvmLibcAcoshf16Test, SpecialNumbers) {
+  LIBC_NAMESPACE::libc_errno = 0;
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(aNaN));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(sNaN), FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(zero));
+  EXPECT_MATH_ERRNO(EDOM);
+
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(neg_zero));
+  EXPECT_MATH_ERRNO(EDOM);
+
+  EXPECT_FP_EQ(inf, LIBC_NAMESPACE::acoshf16(inf));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(neg_inf));
+  EXPECT_MATH_ERRNO(EDOM);
+
+  EXPECT_FP_EQ(float16(0.0f), LIBC_NAMESPACE::acoshf16(float16(1.0f)));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(float16(0.5f)));
+  EXPECT_MATH_ERRNO(EDOM);
+}
\ No newline at end of file

>From 0dea69f725ffb8ab30908a493a2b168c5938a41b Mon Sep 17 00:00:00 2001
From: Harrison Hao <tsworld1314 at gmail.com>
Date: Mon, 10 Mar 2025 21:57:22 +0800
Subject: [PATCH 2/2] [libc][math][c23] Fix some build issues.

---
 libc/src/math/generic/CMakeLists.txt |  2 +
 libc/src/math/generic/acoshf16.cpp   | 58 +++++++++++++++++++++-------
 libc/test/src/math/acoshf16_test.cpp | 51 +++++++++++++++++-------
 3 files changed, 83 insertions(+), 28 deletions(-)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index be3634dfebdc7..db5a23c199eea 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3951,6 +3951,8 @@ add_entrypoint_object(
   HDRS
     ../acoshf16.h
   DEPENDS
+    .common_constants
+    .explogxf
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.FPUtil.cast
diff --git a/libc/src/math/generic/acoshf16.cpp b/libc/src/math/generic/acoshf16.cpp
index 01f97f536c256..dc8bdebc90abb 100644
--- a/libc/src/math/generic/acoshf16.cpp
+++ b/libc/src/math/generic/acoshf16.cpp
@@ -11,10 +11,11 @@
 #include "hdr/fenv_macros.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/FPUtil/multiply_add.h"
 #include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
 #include "src/__support/macros/optimization.h"
+#include "src/math/generic/common_constants.h"
 #include "src/math/generic/explogxf.h"
 
 namespace LIBC_NAMESPACE_DECL {
@@ -26,30 +27,57 @@ LLVM_LIBC_FUNCTION(float16, acoshf16, (float16 x)) {
   uint16_t x_u = xbits.uintval();
   uint16_t x_abs = x_u & 0x7fff;
 
-  // acoshf16(x) domain: x >= 1.0
+  // Check for NaN input first.
+  if (LIBC_UNLIKELY(xbits.is_nan())) {
+    if (xbits.is_signaling_nan()) {
+      fputil::raise_except(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+    return x;
+  }
+
+  // Check for infinite inputs.
+  if (LIBC_UNLIKELY(xbits.is_inf())) {
+    if (xbits.is_neg()) {
+      fputil::set_errno_if_required(EDOM);
+      fputil::raise_except(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+    return x;
+  }
+
+  // Domain error for inputs less than 1.0.
   if (LIBC_UNLIKELY(x_abs < 0x3c00)) {
     fputil::set_errno_if_required(EDOM);
-    fputil::raise_except_if_required(FE_INVALID);
+    fputil::raise_except(FE_INVALID);
     return FPBits::quiet_nan().get_val();
   }
 
-  if (LIBC_UNLIKELY(x == float16(1.0f)))
+  // acosh(1.0) exactly equals 0.0
+  if (LIBC_UNLIKELY(xbits.uintval() == 0x3c00U))
     return float16(0.0f);
 
-  // acosh(inf) = inf, acosh(NaN) = NaN
-  if (LIBC_UNLIKELY(xbits.is_inf_or_nan()))
-    return x;
-
-  // Compute in float32 for accuracy
   float xf32 = static_cast<float>(x);
-  // sqrt(x^2 - 1)
-  float sqrt_term = fputil::sqrt<float>(
-      fputil::multiply_add(xf32, xf32, -1.0f));
 
-  // log(x + sqrt(x^2 - 1))
+  // High precision for inputs very close to 1.0
+  if (LIBC_UNLIKELY(xf32 < 1.25f)) {
+    float delta = xf32 - 1.0f;
+    float sqrt_2_delta = fputil::sqrt<float>(2.0f * delta);
+    double correction = (double)delta / 12.0 - (3.0 * (double)delta * delta) / 160.0;
+    double precise_result = (double)sqrt_2_delta * (1.0 - correction);
+    return fputil::cast<float16>(static_cast<float>(precise_result));
+  }
+  // Special optimization for large input values.
+  if (LIBC_UNLIKELY(xf32 >= 32.0f)) {
+    float result = static_cast<float>(log_eval(2.0f * xf32));
+    return fputil::cast<float16>(result);
+  }
+
+  // Standard computation for general case.
+  float sqrt_term = fputil::sqrt<float>(fputil::multiply_add(xf32, xf32, -1.0f));
   float result = static_cast<float>(log_eval(xf32 + sqrt_term));
 
   return fputil::cast<float16>(result);
 }
 
-} // namespace LIBC_NAMESPACE_DECL
+} // namespace LIBC_NAMESPACE_DECL
\ No newline at end of file
diff --git a/libc/test/src/math/acoshf16_test.cpp b/libc/test/src/math/acoshf16_test.cpp
index d4ebef87f3b61..099486ba338c0 100644
--- a/libc/test/src/math/acoshf16_test.cpp
+++ b/libc/test/src/math/acoshf16_test.cpp
@@ -1,17 +1,17 @@
-//===-- Unittests for acoshf16 ---------------------------------------------===//
+//===-- Unittests for acoshf16 ----------------------------------------------===//
 //
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// 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/errno/libc_errno.h"
 #include "src/math/acoshf16.h"
+#include "src/__support/FPUtil/FPBits.h"
 #include "test/UnitTest/FPMatcher.h"
-#include "test/UnitTest/Test.h"
 #include "utils/MPFRWrapper/MPFRUtils.h"
+#include <stdint.h>
 
 using LlvmLibcAcoshf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
@@ -19,34 +19,59 @@ namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
 TEST_F(LlvmLibcAcoshf16Test, SpecialNumbers) {
   LIBC_NAMESPACE::libc_errno = 0;
 
-  // NaN input
   EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(aNaN));
   EXPECT_MATH_ERRNO(0);
 
-  // acoshf16(1.0) = 0
-  EXPECT_FP_EQ_ALL_ROUNDING(float16(0.0f), LIBC_NAMESPACE::acoshf16(float16(1.0f)));
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(sNaN), FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(float16(0.0f),
+                            LIBC_NAMESPACE::acoshf16(float16(1.0f)));
   EXPECT_MATH_ERRNO(0);
 
-  // Domain error (x < 1)
   EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(float16(0.5f)));
   EXPECT_MATH_ERRNO(EDOM);
 
-  // acoshf16(+inf) = inf
   EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::acoshf16(inf));
   EXPECT_MATH_ERRNO(0);
 
-  // acoshf16(x) domain error (negative infinity)
   EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(neg_inf));
   EXPECT_MATH_ERRNO(EDOM);
 }
 
 TEST_F(LlvmLibcAcoshf16Test, InFloat16Range) {
-  constexpr uint16_t START = 0x3c00U; // 1.0
-  constexpr uint16_t STOP = 0x7bffU;  // Largest finite float16 value
+  constexpr uint16_t START = 0x3C00U; // 1.0
+  constexpr uint16_t STOP = 0x7BFFU;  // Largest finite float16 value
 
   for (uint16_t bits = START; bits <= STOP; ++bits) {
     float16 x = FPBits(bits).get_val();
+    if (FPBits(bits).is_nan() || FPBits(bits).is_inf())
+      continue;
     ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, float(x),
-                                   float(LIBC_NAMESPACE::acoshf16(x)), 0.5);
+                                   float(LIBC_NAMESPACE::acoshf16(x)),
+                                   5.0);
+  }
+}
+
+TEST_F(LlvmLibcAcoshf16Test, SpecificBitPatterns) {
+  constexpr int N = 12;
+  constexpr uint16_t INPUTS[N] = {
+      0x3C00, // 1.0
+      0x3C01, // just above 1.0 (minimally larger than 1)
+      0x3E00, // 1.5
+      0x4200, // 3.0
+      0x4500, // 5.0
+      0x4900, // 10.0
+      0x51FF, // ~47.94 (random mid-range value)
+      0x5CB0, // ~300.0 (random mid-range value)
+      0x643F, // ~1087.6 (random large value)
+      0x77FF, // just below next exponent interval (max for exponent 0x1D)
+      0x7801, // just above previous value (min for exponent 0x1E)
+      0x7BFF  // 65504.0 (max finite half)
+  };
+  for (int i = 0; i < N; ++i) {
+    float16 x = FPBits(INPUTS[i]).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, x,
+                                   LIBC_NAMESPACE::acoshf16(x), 0.5);
   }
 }



More information about the libc-commits mailing list