Skip to content

Commit 46cc3e3

Browse files
amemovIanWood1
authored andcommitted
[libc][math][c23] Add acospif16() function (llvm#134664)
Addresses llvm#132211 llvm#132754 Part of llvm#95250
1 parent 7fe119a commit 46cc3e3

File tree

15 files changed

+308
-1
lines changed

15 files changed

+308
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
662662
# math.h C23 _Float16 entrypoints
663663
libc.src.math.acosf16
664664
libc.src.math.acoshf16
665+
libc.src.math.acospif16
665666
libc.src.math.asinf16
666667
libc.src.math.asinhf16
667668
libc.src.math.canonicalizef16

libc/docs/headers/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ Higher Math Functions
253253
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
254254
| acosh | |check| | | | |check| | | 7.12.5.1 | F.10.2.1 |
255255
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
256-
| acospi | | | | | | 7.12.4.8 | F.10.1.8 |
256+
| acospi | | | | |check| | | 7.12.4.8 | F.10.1.8 |
257257
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
258258
| asin | |check| | | | |check| | | 7.12.4.2 | F.10.1.2 |
259259
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/include/math.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ functions:
3434
arguments:
3535
- type: _Float16
3636
guard: LIBC_TYPES_HAS_FLOAT16
37+
name: acospif16
38+
standards:
39+
- stdc
40+
return_type: _Float16
41+
arguments:
42+
- type: _Float16
43+
guard: LIBC_TYPES_HAS_FLOAT16
3744
- name: asin
3845
standards:
3946
- stdc

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ add_math_entrypoint_object(acosh)
4848
add_math_entrypoint_object(acoshf)
4949
add_math_entrypoint_object(acoshf16)
5050

51+
add_math_entrypoint_object(acospif16)
52+
5153
add_math_entrypoint_object(asin)
5254
add_math_entrypoint_object(asinf)
5355
add_math_entrypoint_object(asinf16)

libc/src/math/acospif16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4112,6 +4112,25 @@ add_entrypoint_object(
41124112
libc.src.__support.macros.properties.types
41134113
)
41144114

4115+
add_entrypoint_object(
4116+
acospif16
4117+
SRCS
4118+
acospif16.cpp
4119+
HDRS
4120+
../acospif16.h
4121+
DEPENDS
4122+
libc.hdr.errno_macros
4123+
libc.hdr.fenv_macros
4124+
libc.src.__support.FPUtil.cast
4125+
libc.src.__support.FPUtil.fenv_impl
4126+
libc.src.__support.FPUtil.fp_bits
4127+
libc.src.__support.FPUtil.multiply_add
4128+
libc.src.__support.FPUtil.polyeval
4129+
libc.src.__support.FPUtil.sqrt
4130+
libc.src.__support.macros.optimization
4131+
libc.src.__support.macros.properties.types
4132+
)
4133+
41154134
add_header_library(
41164135
atan_utils
41174136
HDRS

libc/src/math/generic/acospif16.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
//===-- Half-precision acospi 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/acospif16.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+
LLVM_LIBC_FUNCTION(float16, acospif16, (float16 x)) {
23+
using FPBits = fputil::FPBits<float16>;
24+
FPBits xbits(x);
25+
26+
uint16_t x_u = xbits.uintval();
27+
uint16_t x_abs = x_u & 0x7fff;
28+
uint16_t x_sign = x_u >> 15;
29+
30+
// |x| > 0x1p0, |x| > 1, or x is NaN.
31+
if (LIBC_UNLIKELY(x_abs > 0x3c00)) {
32+
// acospif16(NaN) = NaN
33+
if (xbits.is_nan()) {
34+
if (xbits.is_signaling_nan()) {
35+
fputil::raise_except_if_required(FE_INVALID);
36+
return FPBits::quiet_nan().get_val();
37+
}
38+
39+
return x;
40+
}
41+
42+
// 1 < |x| <= +inf
43+
fputil::raise_except_if_required(FE_INVALID);
44+
fputil::set_errno_if_required(EDOM);
45+
46+
return FPBits::quiet_nan().get_val();
47+
}
48+
49+
// |x| == 0x1p0, x is 1 or -1
50+
// if x is (-)1, return 1
51+
// if x is (+)1, return 0
52+
if (LIBC_UNLIKELY(x_abs == 0x3c00))
53+
return fputil::cast<float16>(x_sign ? 1.0f : 0.0f);
54+
55+
float xf = x;
56+
float xsq = xf * xf;
57+
58+
// Degree-6 minimax polynomial coefficients of asin(x) generated by Sollya
59+
// with: > P = fpminimax(asin(x)/(pi * x), [|0, 2, 4, 6, 8|], [|SG...|], [0,
60+
// 0.5]);
61+
constexpr float POLY_COEFFS[5] = {0x1.45f308p-2f, 0x1.b2900cp-5f,
62+
0x1.897e36p-6f, 0x1.9efafcp-7f,
63+
0x1.06d884p-6f};
64+
// |x| <= 0x1p-1, |x| <= 0.5
65+
if (x_abs <= 0x3800) {
66+
// if x is 0, return 0.5
67+
if (LIBC_UNLIKELY(x_abs == 0))
68+
return fputil::cast<float16>(0.5f);
69+
70+
// Note that: acos(x) = pi/2 + asin(-x) = pi/2 - asin(x), then
71+
// acospi(x) = 0.5 - asin(x)/pi
72+
float interm =
73+
fputil::polyeval(xsq, POLY_COEFFS[0], POLY_COEFFS[1], POLY_COEFFS[2],
74+
POLY_COEFFS[3], POLY_COEFFS[4]);
75+
76+
return fputil::cast<float16>(fputil::multiply_add(-xf, interm, 0.5f));
77+
}
78+
79+
// When |x| > 0.5, assume that 0.5 < |x| <= 1
80+
//
81+
// Step-by-step range-reduction proof:
82+
// 1: Let y = asin(x), such that, x = sin(y)
83+
// 2: From complimentary angle identity:
84+
// x = sin(y) = cos(pi/2 - y)
85+
// 3: Let z = pi/2 - y, such that x = cos(z)
86+
// 4: From double angle formula; cos(2A) = 1 - 2 * sin^2(A):
87+
// z = 2A, z/2 = A
88+
// cos(z) = 1 - 2 * sin^2(z/2)
89+
// 5: Make sin(z/2) subject of the formula:
90+
// sin(z/2) = sqrt((1 - cos(z))/2)
91+
// 6: Recall [3]; x = cos(z). Therefore:
92+
// sin(z/2) = sqrt((1 - x)/2)
93+
// 7: Let u = (1 - x)/2
94+
// 8: Therefore:
95+
// asin(sqrt(u)) = z/2
96+
// 2 * asin(sqrt(u)) = z
97+
// 9: Recall [3]; z = pi/2 - y. Therefore:
98+
// y = pi/2 - z
99+
// y = pi/2 - 2 * asin(sqrt(u))
100+
// 10: Recall [1], y = asin(x). Therefore:
101+
// asin(x) = pi/2 - 2 * asin(sqrt(u))
102+
// 11: Recall that: acos(x) = pi/2 + asin(-x) = pi/2 - asin(x)
103+
// Therefore:
104+
// acos(x) = pi/2 - (pi/2 - 2 * asin(sqrt(u)))
105+
// acos(x) = 2 * asin(sqrt(u))
106+
// acospi(x) = 2 * (asin(sqrt(u)) / pi)
107+
//
108+
// THE RANGE REDUCTION, HOW?
109+
// 12: Recall [7], u = (1 - x)/2
110+
// 13: Since 0.5 < x <= 1, therefore:
111+
// 0 <= u <= 0.25 and 0 <= sqrt(u) <= 0.5
112+
//
113+
// Hence, we can reuse the same [0, 0.5] domain polynomial approximation for
114+
// Step [11] as `sqrt(u)` is in range.
115+
// When -1 < x <= -0.5, the identity:
116+
// acos(x) = pi - acos(-x)
117+
// acospi(x) = 1 - acos(-x)/pi
118+
// allows us to compute for the negative x value (lhs)
119+
// with a positive x value instead (rhs).
120+
121+
float xf_abs = (xf < 0 ? -xf : xf);
122+
float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f);
123+
float sqrt_u = fputil::sqrt<float>(u);
124+
125+
float asin_sqrt_u =
126+
sqrt_u * fputil::polyeval(u, POLY_COEFFS[0], POLY_COEFFS[1],
127+
POLY_COEFFS[2], POLY_COEFFS[3], POLY_COEFFS[4]);
128+
129+
// Same as acos(x), but devided the expression with pi
130+
return fputil::cast<float16>(
131+
x_sign ? fputil::multiply_add(-2.0f, asin_sqrt_u, 1.0f)
132+
: 2.0f * asin_sqrt_u);
133+
}
134+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,17 @@ add_fp_unittest(
22692269
libc.src.math.acosf16
22702270
)
22712271

2272+
add_fp_unittest(
2273+
acospif16_test
2274+
NEED_MPFR
2275+
SUITE
2276+
libc-math-unittests
2277+
SRCS
2278+
acospif16_test.cpp
2279+
DEPENDS
2280+
libc.src.math.acospif16
2281+
)
2282+
22722283
add_fp_unittest(
22732284
atanf_test
22742285
NEED_MPFR

libc/test/src/math/acospif16_test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===-- Exhaustive test for acospif16 -------------------------------------===//
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/acospif16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcAcospif16Test = 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(LlvmLibcAcospif16Test, 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::Acospi, x,
31+
LIBC_NAMESPACE::acospif16(x), 0.5);
32+
}
33+
}
34+
35+
TEST_F(LlvmLibcAcospif16Test, 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::Acospi, x,
40+
LIBC_NAMESPACE::acospif16(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
@@ -4040,6 +4040,17 @@ add_fp_unittest(
40404040
libc.src.math.acosf16
40414041
)
40424042

4043+
add_fp_unittest(
4044+
acospif16_test
4045+
SUITE
4046+
libc-math-smoke-tests
4047+
SRCS
4048+
acospif16_test.cpp
4049+
DEPENDS
4050+
libc.src.errno.errno
4051+
libc.src.math.acospif16
4052+
)
4053+
40434054
add_fp_unittest(
40444055
atanf_test
40454056
SUITE
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- Unittests for acospif16 -------------------------------------------===//
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/acospif16.h"
11+
#include "test/UnitTest/FPMatcher.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
using LlvmLibcAcospif16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
15+
TEST_F(LlvmLibcAcospif16Test, SpecialNumbers) {
16+
LIBC_NAMESPACE::libc_errno = 0;
17+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(aNaN));
18+
EXPECT_MATH_ERRNO(0);
19+
20+
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acospif16(sNaN),
21+
FE_INVALID);
22+
EXPECT_MATH_ERRNO(0);
23+
24+
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::acospif16(1.0f));
25+
EXPECT_MATH_ERRNO(0);
26+
27+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(inf));
28+
EXPECT_MATH_ERRNO(EDOM);
29+
30+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(neg_inf));
31+
EXPECT_MATH_ERRNO(EDOM);
32+
33+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(2.0f));
34+
EXPECT_MATH_ERRNO(EDOM);
35+
36+
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acospif16(-2.0f));
37+
EXPECT_MATH_ERRNO(EDOM);
38+
}

libc/utils/MPFRWrapper/MPCommon.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ MPFRNumber MPFRNumber::acosh() const {
7070
return result;
7171
}
7272

73+
MPFRNumber MPFRNumber::acospi() const {
74+
MPFRNumber result(*this);
75+
76+
#if MPFR_VERSION_MAJOR > 4 || \
77+
(MPFR_VERSION_MAJOR == 4 && MPFR_VERSION_MINOR >= 2)
78+
mpfr_acospi(result.value, value, mpfr_rounding);
79+
return result;
80+
#else
81+
MPFRNumber value_acos(0.0, 1280);
82+
mpfr_acos(value_acos.value, value, MPFR_RNDN);
83+
MPFRNumber value_pi(0.0, 1280);
84+
mpfr_const_pi(value_pi.value, MPFR_RNDN);
85+
mpfr_div(result.value, value_acos.value, value_pi.value, mpfr_rounding);
86+
return result;
87+
#endif
88+
}
89+
7390
MPFRNumber MPFRNumber::add(const MPFRNumber &b) const {
7491
MPFRNumber result(*this);
7592
mpfr_add(result.value, value, b.value, mpfr_rounding);

libc/utils/MPFRWrapper/MPCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class MPFRNumber {
181181
MPFRNumber abs() const;
182182
MPFRNumber acos() const;
183183
MPFRNumber acosh() const;
184+
MPFRNumber acospi() const;
184185
MPFRNumber add(const MPFRNumber &b) const;
185186
MPFRNumber asin() const;
186187
MPFRNumber asinh() const;

libc/utils/MPFRWrapper/MPFRUtils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
3232
return mpfrInput.acos();
3333
case Operation::Acosh:
3434
return mpfrInput.acosh();
35+
case Operation::Acospi:
36+
return mpfrInput.acospi();
3537
case Operation::Asin:
3638
return mpfrInput.asin();
3739
case Operation::Asinh:

libc/utils/MPFRWrapper/MPFRUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum class Operation : int {
2828
Abs,
2929
Acos,
3030
Acosh,
31+
Acospi,
3132
Asin,
3233
Asinh,
3334
Atan,

0 commit comments

Comments
 (0)