[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 05:31:52 PDT 2025
https://github.com/harrisonGPU updated https://github.com/llvm/llvm-project/pull/130588
>From af894d520af60d751e06f48ab7d003b63f27d671 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] [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
More information about the libc-commits
mailing list