Skip to content

Commit 9c6454f

Browse files
committed
Add asinf16() function
1 parent 713482f commit 9c6454f

File tree

11 files changed

+272
-1
lines changed

11 files changed

+272
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ endif()
642642
if(LIBC_TYPES_HAS_FLOAT16)
643643
list(APPEND TARGET_LIBM_ENTRYPOINTS
644644
# math.h C23 _Float16 entrypoints
645+
libc.src.math.asinf16
645646
libc.src.math.canonicalizef16
646647
libc.src.math.ceilf16
647648
libc.src.math.copysignf16

libc/docs/headers/math/index.rst

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ add_math_entrypoint_object(asin)
4949
add_math_entrypoint_object(asinf)
5050
add_math_entrypoint_object(asinh)
5151
add_math_entrypoint_object(asinhf)
52+
add_math_entrypoint_object(asinf16)
5253

5354
add_math_entrypoint_object(atan)
5455
add_math_entrypoint_object(atanf)

libc/src/math/asinf16.h

Lines changed: 21 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
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

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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/cast.h"
15+
#include "src/__support/FPUtil/multiply_add.h"
16+
#include "src/__support/FPUtil/PolyEval.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+
float xsq = xf * xf;
35+
36+
// |x| <= 0x1p-1, |x| <= 0.5
37+
if (x_abs <= 0x3800) {
38+
if (LIBC_UNLIKELY(x_abs <= 0x1a1e)) {
39+
// asinf16(+/-0) = +/-0
40+
if (LIBC_UNLIKELY(x_abs == 0U)) {
41+
return x;
42+
}
43+
44+
// Exhaustive tests show that, when:
45+
// x > 0, and rounding upward, then,
46+
// asin(x) = x * 2^-11 + x
47+
// else, in other rounding modes,
48+
// asin(x) = x
49+
int rounding = fputil::quick_get_round();
50+
51+
if ((xbits.is_pos() && rounding == FE_UPWARD) ||
52+
(xbits.is_neg() && rounding == FE_DOWNWARD)) {
53+
return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
54+
}
55+
return x;
56+
}
57+
58+
// Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:
59+
// > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
60+
float result = fputil::polyeval(xsq, 0x1.000002p0f, 0x1.554c2ap-3f, 0x1.3541ccp-4f, 0x1.43b2d6p-5f, 0x1.a0d73ep-5f);
61+
return fputil::cast<float16>(xf * result);
62+
}
63+
64+
// |x| > 1, asinf16(x) = NaN
65+
if (LIBC_UNLIKELY(x_abs > 0x3c00)) {
66+
// |x| <= +/-inf
67+
if (LIBC_UNLIKELY(x_abs <= 0x7c00)) {
68+
fputil::set_errno_if_required(EDOM);
69+
fputil::raise_except_if_required(FE_INVALID);
70+
}
71+
72+
return FPBits::quiet_nan().get_val();
73+
}
74+
75+
// When |x| > 0.5, assume that 0.5 < |x| <= 1,
76+
//
77+
// Step-by-step range-reduction proof:
78+
// 1: Let y = asin(x), such that, x = sin(y)
79+
// 2: From complimentary angle identity:
80+
// x = sin(y) = cos(pi/2 - y)
81+
// 3: Let z = pi/2 - y, such that x = cos(z)
82+
// 4: From double angle formula; cos(2A) = 1 - sin^2(A):
83+
// z = 2A, z/2 = A
84+
// cos(z) = 1 - 2 * sin^2(z/2)
85+
// 5: Make sin(z/2) subject of the formula:
86+
// sin(z/2) = sqrt((1 - cos(z))/2)
87+
// 6: Recall [3]; x = cos(z). Therefore:
88+
// sin(z/2) = sqrt((1 - x)/2)
89+
// 7: Let u = (1 - x)/2
90+
// 8: Therefore:
91+
// arcsin(sqrt(u)) = z/2
92+
// 2 * arcsin(sqrt(u)) = z
93+
// 9: Recall [3], z = pi/2 - y. Therefore:
94+
// y = pi/2 - z
95+
// y = pi/2 - 2 * arcsin(sqrt(u))
96+
// 10: Recall [1], y = asin(x). Therefore:
97+
// arcsin(x) = pi/2 - 2 * arcsin(sqrt(u))
98+
//
99+
// WHY?
100+
// 11: Recall [7], u = (1 - x)/2
101+
// 12: Since 0.5 < x <= 1, therefore:
102+
// 0 <= u <= 0.25 and 0 <= sqrt(u) <= 0.5
103+
//
104+
// Hence, we can reuse the same [0, 0.5] domain polynomial approximation for Step [10]
105+
// as `sqrt(u)` is in range.
106+
107+
// 0x1p-1 < |x| <= 0x1p0, 0.5 < |x| <= 1.0
108+
float xf_abs = (xf < 0 ? -xf : xf);
109+
float sign = ((xbits.uintval() >> 15) == 1 ? -1.0 : 1.0);
110+
float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f);
111+
float u_sqrt = fputil::sqrt<float>(u);
112+
113+
// Degree-6 minimax odd polynomial of asin(x) generated by Sollya with:
114+
// > P = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8|], [|SG...|], [0, 0.5]);
115+
float asin_sqrt_u = u_sqrt * fputil::polyeval(u, 0x1.000002p0f, 0x1.554c2ap-3f, 0x1.3541ccp-4f, 0x1.43b2d6p-5f, 0x1.a0d73ep-5f);
116+
117+
return fputil::cast<float16>(sign * fputil::multiply_add(-2.0f, asin_sqrt_u, PI_2));
118+
}
119+
120+
}

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
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
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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, LIBC_NAMESPACE::asinf16(x), 0.5);
31+
}
32+
}
33+
34+
TEST_F(LlvmLibcAsinf16Test, NegativeRange) {
35+
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
36+
float16 x = FPBits(v).get_val();
37+
38+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asin, x, LIBC_NAMESPACE::asinf16(x), 0.5);
39+
}
40+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3954,6 +3954,17 @@ add_fp_unittest(
39543954
libc.src.__support.FPUtil.fp_bits
39553955
)
39563956

3957+
add_fp_unittest(
3958+
asinf16_test
3959+
SUITE
3960+
libc-math-smoke-tests
3961+
SRCS
3962+
asinf16_test.cpp
3963+
DEPENDS
3964+
libc.src.errno.errno
3965+
libc.src.math.asinf16
3966+
)
3967+
39573968
add_fp_unittest(
39583969
acosf_test
39593970
SUITE

0 commit comments

Comments
 (0)