Skip to content

Commit 3e28455

Browse files
authored
[libc][math][c23] Add acosf16() function (#127731)
- Implementation of acosf16 (inverse cosine) function for 16-bit inputs. - Exhaustive tests across the 16-bit input range.
1 parent 8ea6b73 commit 3e28455

File tree

11 files changed

+302
-1
lines changed

11 files changed

+302
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
649649
list(APPEND TARGET_LIBM_ENTRYPOINTS
650650
# math.h C23 _Float16 entrypoints
651651
libc.src.math.asinf16
652+
libc.src.math.acosf16
652653
libc.src.math.canonicalizef16
653654
libc.src.math.ceilf16
654655
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4034,6 +4034,26 @@ 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.except_value_utils
4048+
libc.src.__support.FPUtil.fenv_impl
4049+
libc.src.__support.FPUtil.fp_bits
4050+
libc.src.__support.FPUtil.multiply_add
4051+
libc.src.__support.FPUtil.polyeval
4052+
libc.src.__support.FPUtil.sqrt
4053+
libc.src.__support.macros.optimization
4054+
libc.src.__support.macros.properties.types
4055+
)
4056+
40374057
add_entrypoint_object(
40384058
atanf
40394059
SRCS

libc/src/math/generic/acosf16.cpp

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