Skip to content

Commit d857770

Browse files
committed
Add acosf16() function
1 parent 2207e3e commit d857770

File tree

11 files changed

+300
-1
lines changed

11 files changed

+300
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
648648
list(APPEND TARGET_LIBM_ENTRYPOINTS
649649
# math.h C23 _Float16 entrypoints
650650
libc.src.math.asinf16
651+
libc.src.math.acosf16
651652
libc.src.math.canonicalizef16
652653
libc.src.math.ceilf16
653654
libc.src.math.copysignf16

libc/docs/headers/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ Higher Math Functions
250250
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
251251
| <Func> | <Func_f> (float) | <Func> (double) | <Func_l> (long double) | <Func_f16> (float16) | <Func_f128> (float128) | C23 Definition Section | C23 Error Handling Section |
252252
+===========+==================+=================+========================+======================+========================+========================+============================+
253-
| acos | |check| | | | | | 7.12.4.1 | F.10.1.1 |
253+
| acos | |check| | | | |check| | | 7.12.4.1 | F.10.1.1 |
254254
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
255255
| acosh | |check| | | | | | 7.12.5.1 | F.10.2.1 |
256256
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/include/math.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ functions:
1414
return_type: float
1515
arguments:
1616
- type: float
17+
- name: acosf16
18+
standards:
19+
- stdc
20+
return_type: _Float16
21+
arguments:
22+
- type: _Float16
23+
guard: LIBC_TYPES_HAS_FLOAT16
1724
- name: acoshf
1825
standards:
1926
- stdc

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ endfunction()
4242

4343
add_math_entrypoint_object(acos)
4444
add_math_entrypoint_object(acosf)
45+
add_math_entrypoint_object(acosf16)
46+
4547
add_math_entrypoint_object(acosh)
4648
add_math_entrypoint_object(acoshf)
4749

libc/src/math/acosf16.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for acosf16 -----------------------*- 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_ACOSF16_H
10+
#define LLVM_LIBC_SRC_MATH_ACOSF16_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 acosf16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_ACOSF16_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4034,6 +4034,25 @@ add_entrypoint_object(
40344034
.inv_trigf_utils
40354035
)
40364036

4037+
add_entrypoint_object(
4038+
acosf16
4039+
SRCS
4040+
acosf16.cpp
4041+
HDRS
4042+
../acosf16.h
4043+
DEPENDS
4044+
libc.hdr.errno_macros
4045+
libc.hdr.fenv_macros
4046+
libc.src.__support.FPUtil.cast
4047+
libc.src.__support.FPUtil.fenv_impl
4048+
libc.src.__support.FPUtil.fp_bits
4049+
libc.src.__support.FPUtil.multiply_add
4050+
libc.src.__support.FPUtil.polyeval
4051+
libc.src.__support.FPUtil.sqrt
4052+
libc.src.__support.macros.optimization
4053+
libc.src.__support.macros.properties.types
4054+
)
4055+
40374056
add_entrypoint_object(
40384057
atanf
40394058
SRCS

libc/src/math/generic/acosf16.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//===-- Half-precision acosf16(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/acosf16.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/macros/optimization.h"
21+
22+
namespace LIBC_NAMESPACE_DECL {
23+
24+
// Generated by Sollya using the following command:
25+
// > round(pi/2, D, RN);
26+
// > round(pi, D, RN);
27+
static constexpr float PI_2 = 0x1.921fb54442d18p0f;
28+
static constexpr float PI = 0x1.921fb54442d18p1f;
29+
30+
static constexpr size_t N_EXCEPTS = 2;
31+
32+
static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ACOSF16_EXCEPTS{{
33+
// (input, RZ output, RU offset, RD offset, RN offset)
34+
{0xacaf, 0x3e93, 1, 0, 0},
35+
{0xb874, 0x4052, 1, 0, 1},
36+
}};
37+
38+
LLVM_LIBC_FUNCTION(float16, acosf16, (float16 x)) {
39+
using FPBits = fputil::FPBits<float16>;
40+
FPBits xbits(x);
41+
42+
uint16_t x_u = xbits.uintval();
43+
uint16_t x_abs = x_u & 0x7fff;
44+
uint16_t x_sign = x_u >> 15;
45+
float xf = x;
46+
47+
// Handle exceptional values
48+
if (auto r = ACOSF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
49+
return r.value();
50+
51+
// |x| == 0x1p0, x is 1 or -1
52+
// if x is (-)1, return pi, else
53+
// if x is (+)1, return 0
54+
if (LIBC_UNLIKELY(x_abs == 0x3c00))
55+
return fputil::cast<float16>(x_sign ? 0x1.921fb54442d18p1 : 0.0f);
56+
57+
// |x| > 0x1p0, |x| > 1, or x is NaN.
58+
if (LIBC_UNLIKELY(x_abs > 0x3c00)) {
59+
// acosf16(NaN) = NaN
60+
if (xbits.is_nan()) {
61+
if (xbits.is_signaling_nan()) {
62+
fputil::raise_except_if_required(FE_INVALID);
63+
return FPBits::quiet_nan().get_val();
64+
}
65+
66+
return x;
67+
}
68+
69+
// 1 < |x| <= +/-inf
70+
fputil::raise_except_if_required(FE_INVALID);
71+
fputil::set_errno_if_required(EDOM);
72+
73+
return FPBits::quiet_nan().get_val();
74+
}
75+
76+
float xsq = xf * xf;
77+
78+
// |x| <= 0x1p-1, |x| <= 0.5
79+
if (x_abs <= 0x3800) {
80+
// if x is 0, return pi/2
81+
if (LIBC_UNLIKELY(x_abs == 0))
82+
return fputil::cast<float16>(PI_2);
83+
84+
// Note that: acos(x) = pi/2 + asin(-x) = pi/2 - asin(x)
85+
// Degree-6 minimax polynomial of asin(x) generated by Sollya with:
86+
// > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
87+
float interm =
88+
fputil::polyeval(xsq, 0x1.000002p0f, 0x1.554c2ap-3f, 0x1.3541ccp-4f,
89+
0x1.43b2d6p-5f, 0x1.a0d73ep-5f);
90+
return fputil::cast<float16>(fputil::multiply_add(-xf, interm, PI_2));
91+
}
92+
93+
// When |x| > 0.5, assume that 0.5 < |x| <= 1
94+
//
95+
// Step-by-step range-reduction proof:
96+
// 1: Let y = asin(x), such that, x = sin(y)
97+
// 2: From complimentary angle identity:
98+
// x = sin(y) = cos(pi/2 - y)
99+
// 3: Let z = pi/2 - y, such that x = cos(z)
100+
// 4: From double angle formula; cos(2A) = 1 - sin^2(A):
101+
// z = 2A, z/2 = A
102+
// cos(z) = 1 - 2 * sin*2(z/2)
103+
// 5: Make sin(z/2) subject of the formula:
104+
// sin(z/2) = sqrt((1 - cos(z))/2)
105+
// 6: Recall [3]; x = cos(z). Therefore:
106+
// sin(z/2) = sqrt((1 - x)/2)
107+
// 7: Let u = (1 - x)/2
108+
// 8: Therefore:
109+
// asin(sqrt(u)) = z/2
110+
// 2 * asin(sqrt(u)) = z
111+
// 9: Recall [3]; z = pi/2 - y. Therefore:
112+
// y = pi/2 - z
113+
// y = pi/2 - 2 * asin(sqrt(u))
114+
// 10: Recall [1], y = asin(x). Therefore:
115+
// asin(x) = pi/2 - 2 * asin(sqrt(u))
116+
// 11: Recall that: acos(x) = pi/2 + asin(-x) = pi/2 - asin(x)
117+
// Therefore:
118+
// acos(x) = pi/2 - (pi/2 - 2 * asin(sqrt(u)))
119+
// acos(x) = 2 * asin(sqrt(u))
120+
//
121+
// THE RANGE REDUCTION, HOW?
122+
// 12: Recall [7], u = (1 - x)/2
123+
// 13: Since 0.5 < x <= 1, therefore:
124+
// 0 <= u <= 0.25 and 0 <= sqrt(u) <= 0.5
125+
//
126+
// Hence, we can reuse the same [0, 0.5] domain polynomial approximation for
127+
// Step [11] as `sqrt(u)` is in range.
128+
// When -1 < x <= -0.5, the identity:
129+
// acos(x) = pi - acos(-x)
130+
// allows us to compute for the negative x value (lhs)
131+
// with a positive x value instead (rhs).
132+
133+
float xf_abs = (xf < 0 ? -xf : xf);
134+
float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f);
135+
float sqrt_u = fputil::sqrt<float>(u);
136+
137+
// Degree-6 minimax polynomial of asin(x) generated by Sollya with:
138+
// > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
139+
float asin_sqrt_u =
140+
sqrt_u * fputil::polyeval(u, 0x1.000002p0f, 0x1.554c2ap-3f,
141+
0x1.3541ccp-4f, 0x1.43b2d6p-5f, 0x1.a0d73ep-5f);
142+
143+
return fputil::cast<float16>(
144+
x_sign ? fputil::multiply_add(-2.0f, asin_sqrt_u, PI) : 2 * asin_sqrt_u);
145+
}
146+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,17 @@ add_fp_unittest(
22102210
libc.src.__support.FPUtil.fp_bits
22112211
)
22122212

2213+
add_fp_unittest(
2214+
acosf16_test
2215+
NEED_MPFR
2216+
SUITE
2217+
libc-math-unittests
2218+
SRCS
2219+
acosf16_test.cpp
2220+
DEPENDS
2221+
libc.src.math.acosf16
2222+
)
2223+
22132224
add_fp_unittest(
22142225
atanf_test
22152226
NEED_MPFR

libc/test/src/math/acosf16_test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===-- Exhaustive test for acosf16 ---------------------------------------===//
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/acosf16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcAcosf16Test = 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(LlvmLibcAcosf16Test, 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::Acos, x,
31+
LIBC_NAMESPACE::acosf16(x), 0.5);
32+
}
33+
}
34+
35+
TEST_F(LlvmLibcAcosf16Test, 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::Acos, x,
40+
LIBC_NAMESPACE::acosf16(x), 0.5);
41+
}
42+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,6 +3980,17 @@ add_fp_unittest(
39803980
libc.src.__support.FPUtil.fp_bits
39813981
)
39823982

3983+
add_fp_unittest(
3984+
acosf16_test
3985+
SUITE
3986+
libc-math-smoke-tests
3987+
SRCS
3988+
acosf16_test.cpp
3989+
DEPENDS
3990+
libc.src.errno.errno
3991+
libc.src.math.acosf16
3992+
)
3993+
39833994
add_fp_unittest(
39843995
atanf_test
39853996
SUITE
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- Unittests for acosf16 ---------------------------------------------===//
2+
//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "src/errno/libc_errno.h"
11+
#include "src/math/acosf16.h"
12+
#include "test/UnitTest/FPMatcher.h"
13+
#include "test/UnitTest/Test.h"
14+
15+
using LlvmLibcAcosf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
16+
17+
TEST_F(LlvmLibcAcosf16Test, SpecialNumbers) {
18+
LIBC_NAMESPACE::libc_errno = 0;
19+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acosf16(aNaN));
20+
EXPECT_MATH_ERRNO(0);
21+
22+
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acosf16(sNaN), FE_INVALID);
23+
EXPECT_MATH_ERRNO(0);
24+
25+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::acosf16(1.0f));
26+
EXPECT_MATH_ERRNO(0);
27+
28+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acosf16(inf));
29+
EXPECT_MATH_ERRNO(EDOM);
30+
31+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acosf16(neg_inf));
32+
EXPECT_MATH_ERRNO(EDOM);
33+
34+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acosf16(2.0f));
35+
EXPECT_MATH_ERRNO(EDOM);
36+
37+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acosf16(-2.0f));
38+
EXPECT_MATH_ERRNO(EDOM);
39+
}

0 commit comments

Comments
 (0)