Skip to content

Commit ee3e17d

Browse files
authored
[libc][math][c23] Add acoshf16 C23 math function. (llvm#130588)
Implementation of acoshf16 function for 16-bit inputs.
1 parent 417390d commit ee3e17d

File tree

12 files changed

+284
-1
lines changed

12 files changed

+284
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

+1
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ endif()
645645
if(LIBC_TYPES_HAS_FLOAT16)
646646
list(APPEND TARGET_LIBM_ENTRYPOINTS
647647
# math.h C23 _Float16 entrypoints
648+
libc.src.math.acoshf16
648649
libc.src.math.canonicalizef16
649650
libc.src.math.ceilf16
650651
libc.src.math.copysignf16

libc/config/linux/x86_64/entrypoints.txt

+1
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
654654
# math.h C23 _Float16 entrypoints
655655
libc.src.math.asinf16
656656
libc.src.math.acosf16
657+
libc.src.math.acoshf16
657658
libc.src.math.canonicalizef16
658659
libc.src.math.ceilf16
659660
libc.src.math.copysignf16

libc/docs/headers/math/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ Higher Math Functions
251251
+===========+==================+=================+========================+======================+========================+========================+============================+
252252
| acos | |check| | | | |check| | | 7.12.4.1 | F.10.1.1 |
253253
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
254-
| acosh | |check| | | | | | 7.12.5.1 | F.10.2.1 |
254+
| acosh | |check| | | | |check| | | 7.12.5.1 | F.10.2.1 |
255255
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
256256
| acospi | | | | | | 7.12.4.8 | F.10.1.8 |
257257
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/include/math.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ functions:
2727
return_type: float
2828
arguments:
2929
- type: float
30+
name: acoshf16
31+
standards:
32+
- stdc
33+
return_type: _Float16
34+
arguments:
35+
- type: _Float16
36+
guard: LIBC_TYPES_HAS_FLOAT16
3037
- name: asin
3138
standards:
3239
- stdc

libc/src/math/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ add_math_entrypoint_object(acosf16)
4646

4747
add_math_entrypoint_object(acosh)
4848
add_math_entrypoint_object(acoshf)
49+
add_math_entrypoint_object(acoshf16)
4950

5051
add_math_entrypoint_object(asin)
5152
add_math_entrypoint_object(asinf)

libc/src/math/acoshf16.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for acoshf16 ----------------------*- 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_ACOSHF16_H
10+
#define LLVM_LIBC_SRC_MATH_ACOSHF16_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 acoshf16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_ACOSHF16_H

libc/src/math/generic/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -3944,6 +3944,27 @@ add_entrypoint_object(
39443944
libc.src.__support.macros.optimization
39453945
)
39463946

3947+
add_entrypoint_object(
3948+
acoshf16
3949+
SRCS
3950+
acoshf16.cpp
3951+
HDRS
3952+
../acoshf16.h
3953+
DEPENDS
3954+
.explogxf
3955+
libc.hdr.errno_macros
3956+
libc.hdr.fenv_macros
3957+
libc.src.__support.FPUtil.cast
3958+
libc.src.__support.FPUtil.except_value_utils
3959+
libc.src.__support.FPUtil.fenv_impl
3960+
libc.src.__support.FPUtil.fp_bits
3961+
libc.src.__support.FPUtil.multiply_add
3962+
libc.src.__support.FPUtil.polyeval
3963+
libc.src.__support.FPUtil.sqrt
3964+
libc.src.__support.macros.optimization
3965+
libc.src.__support.macros.properties.types
3966+
)
3967+
39473968
add_entrypoint_object(
39483969
asinhf
39493970
SRCS

libc/src/math/generic/acoshf16.cpp

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//===-- Half-precision acosh(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+
#include "src/math/acoshf16.h"
10+
#include "explogxf.h"
11+
#include "hdr/errno_macros.h"
12+
#include "hdr/fenv_macros.h"
13+
#include "src/__support/FPUtil/FEnvImpl.h"
14+
#include "src/__support/FPUtil/FPBits.h"
15+
#include "src/__support/FPUtil/PolyEval.h"
16+
#include "src/__support/FPUtil/cast.h"
17+
#include "src/__support/FPUtil/except_value_utils.h"
18+
#include "src/__support/FPUtil/multiply_add.h"
19+
#include "src/__support/FPUtil/sqrt.h"
20+
#include "src/__support/common.h"
21+
#include "src/__support/macros/config.h"
22+
#include "src/__support/macros/optimization.h"
23+
24+
namespace LIBC_NAMESPACE_DECL {
25+
26+
static constexpr size_t N_EXCEPTS = 2;
27+
static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ACOSHF16_EXCEPTS{{
28+
// (input, RZ output, RU offset, RD offset, RN offset)
29+
// x = 0x1.6dcp+1, acoshf16(x) = 0x1.b6p+0 (RZ)
30+
{0x41B7, 0x3ED8, 1, 0, 0},
31+
// x = 0x1.39p+0, acoshf16(x) = 0x1.4f8p-1 (RZ)
32+
{0x3CE4, 0x393E, 1, 0, 1},
33+
}};
34+
35+
LLVM_LIBC_FUNCTION(float16, acoshf16, (float16 x)) {
36+
using FPBits = fputil::FPBits<float16>;
37+
FPBits xbits(x);
38+
uint16_t x_u = xbits.uintval();
39+
40+
// Check for NaN input first.
41+
if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) {
42+
if (xbits.is_signaling_nan()) {
43+
fputil::raise_except_if_required(FE_INVALID);
44+
return FPBits::quiet_nan().get_val();
45+
}
46+
if (xbits.is_neg()) {
47+
fputil::set_errno_if_required(EDOM);
48+
fputil::raise_except_if_required(FE_INVALID);
49+
return FPBits::quiet_nan().get_val();
50+
}
51+
return x;
52+
}
53+
54+
// Domain error for inputs less than 1.0.
55+
if (LIBC_UNLIKELY(x <= 1.0f)) {
56+
if (x == 1.0f)
57+
return FPBits::zero().get_val();
58+
fputil::set_errno_if_required(EDOM);
59+
fputil::raise_except_if_required(FE_INVALID);
60+
return FPBits::quiet_nan().get_val();
61+
}
62+
63+
if (auto r = ACOSHF16_EXCEPTS.lookup(xbits.uintval());
64+
LIBC_UNLIKELY(r.has_value()))
65+
return r.value();
66+
67+
float xf = x;
68+
// High-precision polynomial approximation for inputs close to 1.0
69+
// ([1, 1.25)).
70+
//
71+
// Brief derivation:
72+
// 1. Expand acosh(1 + delta) using Taylor series around delta=0:
73+
// acosh(1 + delta) ≈ sqrt(2 * delta) * [1 - delta/12 + 3*delta^2/160
74+
// - 5*delta^3/896 + 35*delta^4/18432 + ...]
75+
// 2. Truncate the series to fit accurately for delta in [0, 0.25].
76+
// 3. Polynomial coefficients (from sollya) used here are:
77+
// P(delta) ≈ 1 - 0x1.555556p-4 * delta + 0x1.333334p-6 * delta^2
78+
// - 0x1.6db6dcp-8 * delta^3 + 0x1.f1c71cp-10 * delta^4
79+
// 4. The Sollya commands used to generate these coefficients were:
80+
// > display = hexadecimal;
81+
// > round(1/12, SG, RN);
82+
// > round(3/160, SG, RN);
83+
// > round(5/896, SG, RN);
84+
// > round(35/18432, SG, RN);
85+
// With hexadecimal display mode enabled, the outputs were:
86+
// 0x1.555556p-4
87+
// 0x1.333334p-6
88+
// 0x1.6db6dcp-8
89+
// 0x1.f1c71cp-10
90+
// 5. The maximum absolute error, estimated using:
91+
// dirtyinfnorm(acosh(1 + x) - sqrt(2*x) * P(x), [0, 0.25])
92+
// is:
93+
// 0x1.d84281p-22
94+
if (LIBC_UNLIKELY(x_u < 0x3D00U)) {
95+
float delta = xf - 1.0f;
96+
float sqrt_2_delta = fputil::sqrt<float>(2.0 * delta);
97+
float pe = fputil::polyeval(delta, 0x1p+0f, -0x1.555556p-4f, 0x1.333334p-6f,
98+
-0x1.6db6dcp-8f, 0x1.f1c71cp-10f);
99+
float approx = sqrt_2_delta * pe;
100+
return fputil::cast<float16>(approx);
101+
}
102+
103+
// acosh(x) = log(x + sqrt(x^2 - 1))
104+
float sqrt_term = fputil::sqrt<float>(fputil::multiply_add(xf, xf, -1.0f));
105+
float result = static_cast<float>(log_eval(xf + sqrt_term));
106+
107+
return fputil::cast<float16>(result);
108+
}
109+
110+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/math/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,17 @@ add_fp_unittest(
21732173
libc.src.__support.FPUtil.fp_bits
21742174
)
21752175

2176+
add_fp_unittest(
2177+
acoshf16_test
2178+
NEED_MPFR
2179+
SUITE
2180+
libc-math-unittests
2181+
SRCS
2182+
acoshf16_test.cpp
2183+
DEPENDS
2184+
libc.src.math.acoshf16
2185+
)
2186+
21762187
add_fp_unittest(
21772188
asinf_test
21782189
NEED_MPFR

libc/test/src/math/acoshf16_test.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Unittests for acoshf16 --------------------------------------------===//
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/acoshf16.h"
11+
#include "test/UnitTest/FPMatcher.h"
12+
#include "test/UnitTest/Test.h"
13+
#include "utils/MPFRWrapper/MPFRUtils.h"
14+
#include <stdint.h>
15+
16+
using LlvmLibcAcoshf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
17+
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
18+
19+
static constexpr uint16_t START = 0x3c00U;
20+
static constexpr uint16_t STOP = 0x7c00;
21+
22+
TEST_F(LlvmLibcAcoshf16Test, PositiveRange) {
23+
for (uint16_t v = START; v <= STOP; ++v) {
24+
float16 x = FPBits(v).get_val();
25+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, x,
26+
LIBC_NAMESPACE::acoshf16(x), 0.5);
27+
}
28+
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -3945,6 +3945,18 @@ add_fp_unittest(
39453945
libc.src.__support.FPUtil.fp_bits
39463946
)
39473947

3948+
add_fp_unittest(
3949+
acoshf16_test
3950+
SUITE
3951+
libc-math-smoke-tests
3952+
SRCS
3953+
acoshf16_test.cpp
3954+
DEPENDS
3955+
libc.src.errno.errno
3956+
libc.src.math.acoshf16
3957+
libc.src.__support.FPUtil.cast
3958+
)
3959+
39483960
add_fp_unittest(
39493961
asinf_test
39503962
SUITE
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===-- Unittests for acoshf16 --------------------------------------------===//
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/__support/FPUtil/cast.h"
10+
#include "src/errno/libc_errno.h"
11+
#include "src/math/acoshf16.h"
12+
#include "test/UnitTest/FPMatcher.h"
13+
#include "test/UnitTest/Test.h"
14+
15+
using LlvmLibcAcoshf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
16+
17+
TEST_F(LlvmLibcAcoshf16Test, SpecialNumbers) {
18+
LIBC_NAMESPACE::libc_errno = 0;
19+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(aNaN));
20+
EXPECT_MATH_ERRNO(0);
21+
22+
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(sNaN), FE_INVALID);
23+
EXPECT_MATH_ERRNO(0);
24+
25+
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(zero), FE_INVALID);
26+
EXPECT_MATH_ERRNO(EDOM);
27+
28+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(neg_zero));
29+
EXPECT_MATH_ERRNO(EDOM);
30+
31+
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(neg_zero),
32+
FE_INVALID);
33+
EXPECT_MATH_ERRNO(EDOM);
34+
35+
EXPECT_FP_EQ(inf, LIBC_NAMESPACE::acoshf16(inf));
36+
EXPECT_MATH_ERRNO(0);
37+
38+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(neg_inf));
39+
EXPECT_MATH_ERRNO(EDOM);
40+
41+
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(neg_inf),
42+
FE_INVALID);
43+
EXPECT_MATH_ERRNO(EDOM);
44+
45+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::acoshf16(
46+
LIBC_NAMESPACE::fputil::cast<float16>(1.0)));
47+
EXPECT_MATH_ERRNO(0);
48+
49+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(
50+
LIBC_NAMESPACE::fputil::cast<float16>(0.5)));
51+
EXPECT_MATH_ERRNO(EDOM);
52+
53+
EXPECT_FP_EQ_WITH_EXCEPTION(
54+
aNaN,
55+
LIBC_NAMESPACE::acoshf16(LIBC_NAMESPACE::fputil::cast<float16>(-1.0)),
56+
FE_INVALID);
57+
EXPECT_MATH_ERRNO(EDOM);
58+
59+
EXPECT_FP_EQ_WITH_EXCEPTION(
60+
aNaN,
61+
LIBC_NAMESPACE::acoshf16(LIBC_NAMESPACE::fputil::cast<float16>(-2.0)),
62+
FE_INVALID);
63+
EXPECT_MATH_ERRNO(EDOM);
64+
65+
EXPECT_FP_EQ_WITH_EXCEPTION(
66+
aNaN,
67+
LIBC_NAMESPACE::acoshf16(LIBC_NAMESPACE::fputil::cast<float16>(-3.0)),
68+
FE_INVALID);
69+
EXPECT_MATH_ERRNO(EDOM);
70+
}

0 commit comments

Comments
 (0)