Skip to content

Commit 75ec8ec

Browse files
committed
Add asinhf16() function
1 parent 389ed47 commit 75ec8ec

File tree

11 files changed

+237
-1
lines changed

11 files changed

+237
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ set(TARGET_LIBM_ENTRYPOINTS
411411
libc.src.math.acoshf
412412
libc.src.math.asinf
413413
libc.src.math.asinhf
414+
libc.src.math.asinhf16
414415
libc.src.math.atan2
415416
libc.src.math.atan2f
416417
libc.src.math.atanf

libc/docs/headers/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ Higher Math Functions
257257
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
258258
| asin | |check| | | | |check| | | 7.12.4.2 | F.10.1.2 |
259259
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
260-
| asinh | |check| | | | | | 7.12.5.2 | F.10.2.2 |
260+
| asinh | |check| | | | |check| | | 7.12.5.2 | F.10.2.2 |
261261
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
262262
| asinpi | | | | | | 7.12.4.9 | F.10.1.9 |
263263
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/include/math.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ functions:
5252
return_type: float
5353
arguments:
5454
- type: float
55+
- name: asinhf16
56+
standards:
57+
- stdc
58+
return_type: _Float16
59+
arguments:
60+
- type: _Float16
61+
guard: LIBC_TYPES_HAS_FLOAT16
5562
- name: atan2
5663
standards:
5764
- stdc

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ add_math_entrypoint_object(asinf16)
5353

5454
add_math_entrypoint_object(asinh)
5555
add_math_entrypoint_object(asinhf)
56+
add_math_entrypoint_object(asinhf16)
5657

5758
add_math_entrypoint_object(atan)
5859
add_math_entrypoint_object(atanf)

libc/src/math/asinhf16.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for asinhf16 -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_ASINHF16_H
10+
#define LLVM_LIBC_SRC_MATH_ASINHF16_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
float16 asinhf16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_ASINHF16_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3959,6 +3959,26 @@ add_entrypoint_object(
39593959
libc.src.__support.macros.optimization
39603960
)
39613961

3962+
add_entrypoint_object(
3963+
asinhf16
3964+
SRCS
3965+
asinhf16.cpp
3966+
HDRS
3967+
../asinhf16.h
3968+
DEPENDS
3969+
.explogxf
3970+
libc.hdr.fenv_macros
3971+
libc.src.__support.FPUtil.cast
3972+
libc.src.__support.FPUtil.fenv_impl
3973+
libc.src.__support.FPUtil.fp_bits
3974+
libc.src.__support.FPUtil.fp_bits
3975+
libc.src.__support.FPUtil.multiply_add
3976+
libc.src.__support.FPUtil.polyeval
3977+
libc.src.__support.FPUtil.sqrt
3978+
libc.src.__support.macros.optimization
3979+
libc.src.__support.macros.properties.types
3980+
)
3981+
39623982
add_entrypoint_object(
39633983
atanhf
39643984
SRCS

libc/src/math/generic/asinhf16.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===-- Half-precision asinhf16(x) function --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
6+
//
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "src/math/asinhf16.h"
11+
#include "src/math/generic/explogxf.h"
12+
#include "src/__support/FPUtil/except_value_utils.h"
13+
#include "src/__support/FPUtil/generic/sqrt.h"
14+
#include "src/__support/FPUtil/multiply_add.h"
15+
#include "src/__support/common.h"
16+
#include "src/__support/macros/config.h"
17+
#include "src/__support/macros/properties/types.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
22+
static constexpr size_t N_EXCEPTS = 8;
23+
24+
static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ASINHF16_EXCEPTS{{
25+
// (input, RZ output, RU offset, RD offset, RN offset)
26+
{0x3769, 0x372A, 1, 0, 1},
27+
{0x3B5B, 0x3A96, 1, 0, 0},
28+
{0x4B1F, 0x42B3, 1, 0, 0},
29+
{0x4C9B, 0x4336, 1, 0, 1},
30+
{0xB769, 0xB72A, 0, 1, 1},
31+
{0xBB5B, 0xBA96, 0, 1, 0},
32+
{0xCB1F, 0xC2B3, 0, 1, 0},
33+
{0xCC9B, 0xC336, 0, 1, 1}
34+
}};
35+
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
36+
37+
LLVM_LIBC_FUNCTION(float16, asinhf16, (float16 x)) {
38+
using FPBits = fputil::FPBits<float16>;
39+
FPBits xbits(x);
40+
41+
float x_d = x;
42+
uint16_t x_u = xbits.uintval();
43+
uint16_t x_abs = x_u & 0x7fff;
44+
45+
if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) {
46+
if (xbits.is_signaling_nan()) {
47+
fputil::raise_except_if_required(FE_INVALID);
48+
return FPBits::quiet_nan().get_val();
49+
}
50+
51+
return x;
52+
}
53+
54+
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
55+
// Handle exceptional values
56+
if (auto r = ASINHF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
57+
return r.value();
58+
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
59+
60+
const float SIGN[2] = {1.0f, -1.0f};
61+
float x_sign = SIGN[x_u >> 15];
62+
63+
// |x| <= 0.25
64+
if (LIBC_UNLIKELY(x_abs <= 0x3400)) {
65+
if (LIBC_UNLIKELY(x_abs == 0))
66+
return x;
67+
if (LIBC_UNLIKELY((fputil::get_round() == FE_UPWARD) &&
68+
(x_u >= 0x8401) && (x_u <= 0x90E6)))
69+
return static_cast<float16>(x_d + 0x1p-24f);
70+
71+
float x_sq = x_d * x_d;
72+
// Generated by Sollya with:
73+
// > P = fpminimax(asinh(x)/x, [|0, 2, 4, 6, 8|], [|SG...|],[0, 2^-2]);
74+
float p = fputil::polyeval(x_sq, 1.0f, -0x1.555556p-3f, 0x1.3334dep-4f,
75+
-0x1.6f3e2p-5f, 0x1.51d012p-5f);
76+
77+
return static_cast<float16>(fputil::multiply_add(x_d, p, 0.0f));
78+
}
79+
80+
// General case: asinh(x) = ln(x + sqrt(x^2 + 1))
81+
float sqrt_term = fputil::sqrt<float>(fputil::multiply_add(x_d, x_d, 1.0f));
82+
return fputil::cast<float16>(x_sign * log_eval(
83+
fputil::multiply_add(x_d, x_sign, sqrt_term)));
84+
}
85+
}

libc/test/src/math/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,18 @@ add_fp_unittest(
21602160
libc.src.__support.FPUtil.fp_bits
21612161
)
21622162

2163+
2164+
add_fp_unittest(
2165+
asinhf16_test
2166+
NEED_MPFR
2167+
SUITE
2168+
libc-math-unittests
2169+
SRCS
2170+
asinhf16_test.cpp
2171+
DEPENDS
2172+
libc.src.math.asinhf16
2173+
)
2174+
21632175
add_fp_unittest(
21642176
acoshf_test
21652177
NEED_MPFR

libc/test/src/math/asinhf16_test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===-- Exhaustive test for asinhf16 ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/asinhf16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcAsinhf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
15+
16+
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
17+
18+
// Range: [0, Inf]
19+
static constexpr uint16_t POS_START = 0x0000U;
20+
static constexpr uint16_t POS_STOP = 0x7c00U;
21+
22+
// Range: [-Inf, 0]
23+
static constexpr uint16_t NEG_START = 0x8000U;
24+
static constexpr uint16_t NEG_STOP = 0xfc00U;
25+
26+
TEST_F(LlvmLibcAsinhf16Test, PositiveRange) {
27+
for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
28+
float16 x = FPBits(v).get_val();
29+
30+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, x,
31+
LIBC_NAMESPACE::asinhf16(x), 0.5);
32+
}
33+
}
34+
35+
TEST_F(LlvmLibcAsinhf16Test, NegativeRange) {
36+
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
37+
float16 x = FPBits(v).get_val();
38+
39+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, x,
40+
LIBC_NAMESPACE::asinhf16(x), 0.5);
41+
}
42+
}

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3933,6 +3933,18 @@ add_fp_unittest(
39333933
libc.src.__support.FPUtil.fp_bits
39343934
)
39353935

3936+
add_fp_unittest(
3937+
asinhf16_test
3938+
SUITE
3939+
libc-math-smoke-tests
3940+
SRCS
3941+
asinhf16_test.cpp
3942+
DEPENDS
3943+
libc.src.errno.errno
3944+
libc.src.math.asinhf16
3945+
libc.src.__support.FPUtil.fp_bits
3946+
)
3947+
39363948
add_fp_unittest(
39373949
acoshf_test
39383950
SUITE
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===-- Unittests for asinhf16 ----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/errno/libc_errno.h"
10+
#include "src/math/asinhf16.h"
11+
#include "test/UnitTest/FPMatcher.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
using LlvmLibcAsinhf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
15+
16+
TEST_F(LlvmLibcAsinhf16Test, SpecialNumbers) {
17+
LIBC_NAMESPACE::libc_errno = 0;
18+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::asinhf16(aNaN));
19+
EXPECT_MATH_ERRNO(0);
20+
21+
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::asinhf16(sNaN), FE_INVALID);
22+
EXPECT_MATH_ERRNO(0);
23+
24+
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::asinhf16(0.0f));
25+
EXPECT_MATH_ERRNO(0);
26+
27+
EXPECT_FP_EQ(-0.0f, LIBC_NAMESPACE::asinhf16(-0.0f));
28+
EXPECT_MATH_ERRNO(0);
29+
30+
EXPECT_FP_EQ(inf, LIBC_NAMESPACE::asinhf16(inf));
31+
EXPECT_MATH_ERRNO(0);
32+
33+
EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::asinhf16(neg_inf));
34+
EXPECT_MATH_ERRNO(0);
35+
}

0 commit comments

Comments
 (0)