-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[libc][math][c23] Add acoshf16 C23 math function. #130588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
✅ With the latest revision this PR passed the C/C++ code formatter. |
5113bfe
to
0dea69f
Compare
9af9063
to
2c14051
Compare
@llvm/pr-subscribers-libc Author: Harrison Hao (harrisonGPU) ChangesImplementation of acoshf16 function for 16-bit inputs. Full diff: https://github.com./llvm/llvm-project/pull/130588.diff 11 Files Affected:
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..8dc7c8c1a0966 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..f471ecfeb6154
--- /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..fd5ecd72b765f 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3944,6 +3944,27 @@ add_entrypoint_object(
libc.src.__support.macros.optimization
)
+add_entrypoint_object(
+ acoshf16
+ SRCS
+ acoshf16.cpp
+ HDRS
+ ../acoshf16.h
+ DEPENDS
+ .common_constants
+ .explogxf
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.cast
+ 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..1a4f68987ee6b
--- /dev/null
+++ b/libc/src/math/generic/acoshf16.cpp
@@ -0,0 +1,88 @@
+//===-- 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 "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/generic/sqrt.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/macros/optimization.h"
+#include "src/math/generic/explogxf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static constexpr size_t N_EXCEPTS = 2;
+static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ACOSHF16_EXCEPTS{
+ {// (input, RZ output, RU offset, RD offset, RN offset)
+ {0x41B7, 0x3ED8, 1, 0, 0},
+ {0x3CE4, 0x393E, 1, 0, 1}}};
+
+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;
+
+ // Check for NaN input first.
+ 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;
+ }
+
+ // Check for infinite inputs.
+ if (LIBC_UNLIKELY(xbits.is_inf())) {
+ if (xbits.is_neg()) {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ // Domain error for inputs less than 1.0.
+ if (LIBC_UNLIKELY(x_abs < 0x3c00U)) {
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // acosh(1.0) exactly equals 0.0
+ if (LIBC_UNLIKELY(x_u == 0x3c00U))
+ return float16(0.0f);
+
+ if (auto r = ACOSHF16_EXCEPTS.lookup(xbits.uintval());
+ LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+
+ float xf32 = x;
+ // 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.0 * delta);
+ float x2 = delta;
+ float pe = fputil::polyeval(x2, 0x1.0000000000000p+0f,
+ -0x1.55551a83a9472p-4f, 0x1.331601c4b8ecfp-6f,
+ -0x1.6890f49eb0acbp-8f, 0x1.8f3a617040a6ap-10f);
+ float approx = sqrt_2_delta * pe;
+ return fputil::cast<float16>(approx);
+ }
+
+ // 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
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..a779d3110f329
--- /dev/null
+++ b/libc/test/src/math/acoshf16_test.cpp
@@ -0,0 +1,76 @@
+//===-- 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/__support/FPUtil/cast.h"
+#include "src/errno/libc_errno.h"
+#include "src/math/acoshf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+#include <stdint.h>
+
+using LlvmLibcAcoshf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+static constexpr uint16_t START = 0x3c00U;
+static constexpr uint16_t STOP = 0x7bffU;
+
+TEST_F(LlvmLibcAcoshf16Test, SpecialNumbers) {
+ LIBC_NAMESPACE::libc_errno = 0;
+
+ EXPECT_FP_EQ_ALL_ROUNDING(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_ALL_ROUNDING(float16(0.0f),
+ LIBC_NAMESPACE::acoshf16(float16(1.0f)));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(float16(0.5f)));
+ EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::acoshf16(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(neg_inf));
+ EXPECT_MATH_ERRNO(EDOM);
+}
+
+TEST_F(LlvmLibcAcoshf16Test, PositiveRange) {
+ for (uint16_t v = START; v <= STOP; ++v) {
+ float16 x = FPBits(v).get_val();
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, x,
+ LIBC_NAMESPACE::acoshf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcAcoshf16Test, SpecificBitPatterns) {
+ constexpr int N = 12;
+ constexpr uint16_t INPUTS[N] = {
+ 0x3C00, // x = 1.0
+ 0x3C01, // x = just above 1.0 (minimally larger than 1)
+ 0x3E00, // x = 1.5
+ 0x4200, // x = 3.0
+ 0x4500, // x = 5.0
+ 0x4900, // x = 10.0
+ 0x51FF, // x = ~47.94
+ 0x5CB0, // x = ~300.0
+ 0x643F, // x = ~1087.6
+ 0x77FF, // x = just below next exponent interval (max for exponent 0x1D)
+ 0x7801, // x = just above previous value (min for exponent 0x1E)
+ 0x7BFF // x = 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);
+ }
+}
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..b8d13b9fe88c3
--- /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);
+}
|
3922002
to
6d9a926
Compare
6d9a926
to
6c54d4b
Compare
You don't need to force-push if you didn't rebase btw. |
Yes, I did rebase. |
c7f1f4f
to
c8f5517
Compare
ping @overmighty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Please wait to see if @overmighty has any other comments.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/71/builds/18786 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/104/builds/18785 Here is the relevant piece of the build log for the reference
|
@harrisonGPU aarch64 failure:
Can you investigate to see if it can be fixed quickly, or temporarily disable the assertion statement if you need more time? |
I will try to investigate this failing test, and for now, I have temporarily disabled it. #132580 |
Implementation of acoshf16 function for 16-bit inputs.