Skip to content

Commit 7ee56b9

Browse files
wldfngrsovermighty
andauthored
[libc][math][c23] Add asinf16() function (#124212)
Co-authored-by: OverMighty <[email protected]>
1 parent 65a9254 commit 7ee56b9

File tree

11 files changed

+290
-1
lines changed

11 files changed

+290
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

+1
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ endif()
647647
if(LIBC_TYPES_HAS_FLOAT16)
648648
list(APPEND TARGET_LIBM_ENTRYPOINTS
649649
# math.h C23 _Float16 entrypoints
650+
libc.src.math.asinf16
650651
libc.src.math.canonicalizef16
651652
libc.src.math.ceilf16
652653
libc.src.math.copysignf16

libc/docs/headers/math/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ Higher Math Functions
256256
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
257257
| acospi | | | | | | 7.12.4.8 | F.10.1.8 |
258258
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
259-
| asin | |check| | | | | | 7.12.4.2 | F.10.1.2 |
259+
| asin | |check| | | | |check| | | 7.12.4.2 | F.10.1.2 |
260260
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
261261
| asinh | |check| | | | | | 7.12.5.2 | F.10.2.2 |
262262
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/include/math.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ functions:
3232
return_type: float
3333
arguments:
3434
- type: float
35+
- name: asinf16
36+
standards:
37+
- stdc
38+
return_type: _Float16
39+
arguments:
40+
- type: _Float16
41+
guard: LIBC_TYPES_HAS_FLOAT16
3542
- name: asinhf
3643
standards:
3744
- stdc

libc/src/math/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ add_math_entrypoint_object(acoshf)
4747

4848
add_math_entrypoint_object(asin)
4949
add_math_entrypoint_object(asinf)
50+
add_math_entrypoint_object(asinf16)
51+
5052
add_math_entrypoint_object(asinh)
5153
add_math_entrypoint_object(asinhf)
5254

libc/src/math/asinf16.h

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

libc/src/math/generic/CMakeLists.txt

+19
Original file line numberDiff line numberDiff line change
@@ -4598,6 +4598,25 @@ add_entrypoint_object(
45984598
${libc_opt_high_flag}
45994599
)
46004600

4601+
add_entrypoint_object(
4602+
asinf16
4603+
SRCS
4604+
asinf16.cpp
4605+
HDRS
4606+
../asinf16.h
4607+
DEPENDS
4608+
libc.hdr.errno_macros
4609+
libc.hdr.fenv_macros
4610+
libc.src.__support.FPUtil.cast
4611+
libc.src.__support.FPUtil.fenv_impl
4612+
libc.src.__support.FPUtil.fp_bits
4613+
libc.src.__support.FPUtil.multiply_add
4614+
libc.src.__support.FPUtil.polyeval
4615+
libc.src.__support.FPUtil.sqrt
4616+
libc.src.__support.macros.optimization
4617+
libc.src.__support.macros.properties.types
4618+
)
4619+
46014620
add_entrypoint_object(
46024621
acosf
46034622
SRCS

libc/src/math/generic/asinf16.cpp

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//===-- Half-precision asinf16(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/asinf16.h"
10+
#include "hdr/errno_macros.h"
11+
#include "hdr/fenv_macros.h"
12+
#include "src/__support/FPUtil/FEnvImpl.h"
13+
#include "src/__support/FPUtil/FPBits.h"
14+
#include "src/__support/FPUtil/PolyEval.h"
15+
#include "src/__support/FPUtil/cast.h"
16+
#include "src/__support/FPUtil/multiply_add.h"
17+
#include "src/__support/FPUtil/sqrt.h"
18+
#include "src/__support/macros/optimization.h"
19+
20+
namespace LIBC_NAMESPACE_DECL {
21+
22+
// Generated by Sollya using the following command:
23+
// > round(pi/2, D, RN);
24+
static constexpr float PI_2 = 0x1.921fb54442d18p0f;
25+
26+
LLVM_LIBC_FUNCTION(float16, asinf16, (float16 x)) {
27+
using FPBits = fputil::FPBits<float16>;
28+
FPBits xbits(x);
29+
30+
uint16_t x_u = xbits.uintval();
31+
uint16_t x_abs = x_u & 0x7fff;
32+
float xf = x;
33+
34+
// |x| > 0x1p0, |x| > 1, or x is NaN.
35+
if (LIBC_UNLIKELY(x_abs > 0x3c00)) {
36+
// asinf16(NaN) = NaN
37+
if (xbits.is_nan()) {
38+
if (xbits.is_signaling_nan()) {
39+
fputil::raise_except_if_required(FE_INVALID);
40+
return FPBits::quiet_nan().get_val();
41+
}
42+
43+
return x;
44+
}
45+
46+
// 1 < |x| <= +/-inf
47+
fputil::raise_except_if_required(FE_INVALID);
48+
fputil::set_errno_if_required(EDOM);
49+
50+
return FPBits::quiet_nan().get_val();
51+
}
52+
53+
float xsq = xf * xf;
54+
55+
// |x| <= 0x1p-1, |x| <= 0.5
56+
if (x_abs <= 0x3800) {
57+
// asinf16(+/-0) = +/-0
58+
if (LIBC_UNLIKELY(x_abs == 0))
59+
return x;
60+
61+
// Exhaustive tests show that,
62+
// for |x| <= 0x1.878p-9, when:
63+
// x > 0, and rounding upward, or
64+
// x < 0, and rounding downward, then,
65+
// asin(x) = x * 2^-11 + x
66+
// else, in other rounding modes,
67+
// asin(x) = x
68+
if (LIBC_UNLIKELY(x_abs <= 0x1a1e)) {
69+
int rounding = fputil::quick_get_round();
70+
71+
if ((xbits.is_pos() && rounding == FE_UPWARD) ||
72+
(xbits.is_neg() && rounding == FE_DOWNWARD))
73+
return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
74+
return x;
75+
}
76+
77+
// Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:
78+
// > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
79+
float result =
80+
fputil::polyeval(xsq, 0x1.000002p0f, 0x1.554c2ap-3f, 0x1.3541ccp-4f,
81+
0x1.43b2d6p-5f, 0x1.a0d73ep-5f);
82+
return fputil::cast<float16>(xf * result);
83+
}
84+
85+
// When |x| > 0.5, assume that 0.5 < |x| <= 1,
86+
//
87+
// Step-by-step range-reduction proof:
88+
// 1: Let y = asin(x), such that, x = sin(y)
89+
// 2: From complimentary angle identity:
90+
// x = sin(y) = cos(pi/2 - y)
91+
// 3: Let z = pi/2 - y, such that x = cos(z)
92+
// 4: From double angle formula; cos(2A) = 1 - sin^2(A):
93+
// z = 2A, z/2 = A
94+
// cos(z) = 1 - 2 * sin^2(z/2)
95+
// 5: Make sin(z/2) subject of the formula:
96+
// sin(z/2) = sqrt((1 - cos(z))/2)
97+
// 6: Recall [3]; x = cos(z). Therefore:
98+
// sin(z/2) = sqrt((1 - x)/2)
99+
// 7: Let u = (1 - x)/2
100+
// 8: Therefore:
101+
// asin(sqrt(u)) = z/2
102+
// 2 * asin(sqrt(u)) = z
103+
// 9: Recall [3], z = pi/2 - y. Therefore:
104+
// y = pi/2 - z
105+
// y = pi/2 - 2 * asin(sqrt(u))
106+
// 10: Recall [1], y = asin(x). Therefore:
107+
// asin(x) = pi/2 - 2 * asin(sqrt(u))
108+
//
109+
// WHY?
110+
// 11: Recall [7], u = (1 - x)/2
111+
// 12: Since 0.5 < x <= 1, therefore:
112+
// 0 <= u <= 0.25 and 0 <= sqrt(u) <= 0.5
113+
//
114+
// Hence, we can reuse the same [0, 0.5] domain polynomial approximation for
115+
// Step [10] as `sqrt(u)` is in range.
116+
117+
// 0x1p-1 < |x| <= 0x1p0, 0.5 < |x| <= 1.0
118+
float xf_abs = (xf < 0 ? -xf : xf);
119+
float sign = (xbits.uintval() >> 15 == 1 ? -1.0 : 1.0);
120+
float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f);
121+
float u_sqrt = fputil::sqrt<float>(u);
122+
123+
// Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:
124+
// > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
125+
float asin_sqrt_u =
126+
u_sqrt * fputil::polyeval(u, 0x1.000002p0f, 0x1.554c2ap-3f,
127+
0x1.3541ccp-4f, 0x1.43b2d6p-5f, 0x1.a0d73ep-5f);
128+
129+
return fputil::cast<float16>(sign *
130+
fputil::multiply_add(-2.0f, asin_sqrt_u, PI_2));
131+
}
132+
133+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/math/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,17 @@ add_fp_unittest(
21862186
libc.src.__support.FPUtil.fp_bits
21872187
)
21882188

2189+
add_fp_unittest(
2190+
asinf16_test
2191+
NEED_MPFR
2192+
SUITE
2193+
libc-math-unittests
2194+
SRCS
2195+
asinf16_test.cpp
2196+
DEPENDS
2197+
libc.src.math.asinf16
2198+
)
2199+
21892200
add_fp_unittest(
21902201
acosf_test
21912202
NEED_MPFR

libc/test/src/math/asinf16_test.cpp

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

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

+11
Original file line numberDiff line numberDiff line change
@@ -3957,6 +3957,17 @@ add_fp_unittest(
39573957
libc.src.__support.FPUtil.fp_bits
39583958
)
39593959

3960+
add_fp_unittest(
3961+
asinf16_test
3962+
SUITE
3963+
libc-math-smoke-tests
3964+
SRCS
3965+
asinf16_test.cpp
3966+
DEPENDS
3967+
libc.src.errno.errno
3968+
libc.src.math.asinf16
3969+
)
3970+
39603971
add_fp_unittest(
39613972
acosf_test
39623973
SUITE
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===-- Unittests for asinf16 ---------------------------------------------===//
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/asinf16.h"
12+
#include "test/UnitTest/FPMatcher.h"
13+
#include "test/UnitTest/Test.h"
14+
15+
using LlvmLibcAsinf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
16+
17+
TEST_F(LlvmLibcAsinf16Test, SpecialNumbers) {
18+
LIBC_NAMESPACE::libc_errno = 0;
19+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::asinf16(aNaN));
20+
EXPECT_MATH_ERRNO(0);
21+
22+
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::asinf16(sNaN), FE_INVALID);
23+
EXPECT_MATH_ERRNO(0);
24+
25+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::asinf16(zero));
26+
EXPECT_MATH_ERRNO(0);
27+
28+
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::asinf16(neg_zero));
29+
EXPECT_MATH_ERRNO(0);
30+
31+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::asinf16(inf));
32+
EXPECT_MATH_ERRNO(EDOM);
33+
34+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::asinf16(neg_inf));
35+
EXPECT_MATH_ERRNO(EDOM);
36+
37+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::asinf16(2.0f));
38+
EXPECT_MATH_ERRNO(EDOM);
39+
40+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::asinf16(-2.0f));
41+
EXPECT_MATH_ERRNO(EDOM);
42+
}

0 commit comments

Comments
 (0)