Skip to content

Commit 521296d

Browse files
committed
use higher-precision type for poly coeffs
1 parent c20d7b1 commit 521296d

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

libc/src/math/generic/asinpif16.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ LLVM_LIBC_FUNCTION(float16, asinpif16, (float16 x)) {
5959
bool is_neg = static_cast<bool>(x_uint >> 15);
6060
float16 x_abs = is_neg ? -x : x;
6161

62-
auto signed_result = [is_neg](float16 r) -> float16 {
63-
return is_neg ? -r : r;
64-
};
62+
auto signed_result = [is_neg](auto r) -> auto { return is_neg ? -r : r; };
6563

6664
if (LIBC_UNLIKELY(x_abs > 1.0f16)) {
6765
// aspinf16(NaN) = NaN
@@ -105,22 +103,22 @@ LLVM_LIBC_FUNCTION(float16, asinpif16, (float16 x)) {
105103
//
106104
// it's very accurate in the range [0, 0.5] and has a maximum error of
107105
// 0.0000000000000001 in the range [0, 0.5].
108-
static constexpr float16 POLY_COEFFS[10] = {
109-
0.318309886183791f16, // x^1
110-
0.0530516476972984f16, // x^3
111-
0.0238732414637843f16, // x^5
112-
0.0142102627760621f16, // x^7
113-
0.00967087327815336f16, // x^9
114-
0.00712127941391293f16, // x^11
115-
0.00552355646848375f16, // x^13
116-
0.00444514782463692f16, // x^15
117-
0.00367705242846804f16, // x^17
118-
0.00310721681820837f16 // x^19
106+
static constexpr float POLY_COEFFS[10] = {
107+
0.318309886183791f, // x^1
108+
0.0530516476972984f, // x^3
109+
0.0238732414637843f, // x^5
110+
0.0142102627760621f, // x^7
111+
0.00967087327815336f, // x^9
112+
0.00712127941391293f, // x^11
113+
0.00552355646848375f, // x^13
114+
0.00444514782463692f, // x^15
115+
0.00367705242846804f, // x^17
116+
0.00310721681820837f // x^19
119117
};
120118

121119
// polynomial evaluation using horner's method
122120
// work only for |x| in [0, 0.5]
123-
auto asinpi_polyeval = [](float16 xsq) -> float16 {
121+
auto asinpi_polyeval = [](float xsq) -> float {
124122
return fputil::polyeval(xsq, POLY_COEFFS[0], POLY_COEFFS[1], POLY_COEFFS[2],
125123
POLY_COEFFS[3], POLY_COEFFS[4], POLY_COEFFS[5],
126124
POLY_COEFFS[6], POLY_COEFFS[7], POLY_COEFFS[8],
@@ -131,7 +129,7 @@ LLVM_LIBC_FUNCTION(float16, asinpif16, (float16 x)) {
131129
if (LIBC_UNLIKELY(x_abs <= ONE_OVER_TWO)) {
132130
// Use polynomial approximation of asin(x)/pi in the range [0, 0.5]
133131
float16 xsq = x * x;
134-
float16 result = x * asinpi_polyeval(xsq);
132+
float result = x * asinpi_polyeval(xsq);
135133
return fputil::cast<float16>(signed_result(result));
136134
}
137135

@@ -162,10 +160,11 @@ LLVM_LIBC_FUNCTION(float16, asinpif16, (float16 x)) {
162160
// = 0.5 - 0.5 * x
163161
// = multiply_add(-0.5, x, 0.5)
164162

165-
float16 u = fputil::multiply_add(-ONE_OVER_TWO, x_abs, ONE_OVER_TWO);
166-
float16 u_sqrt = fputil::sqrt<float16>(u);
167-
float16 asinpi_sqrt_u = u_sqrt * asinpi_polyeval(u);
168-
float16 result = fputil::multiply_add(-2.0f16, asinpi_sqrt_u, ONE_OVER_TWO);
163+
float u = static_cast<float>(
164+
fputil::multiply_add(-ONE_OVER_TWO, x_abs, ONE_OVER_TWO));
165+
float u_sqrt = fputil::sqrt<float>(static_cast<float>(u));
166+
float asinpi_sqrt_u = u_sqrt * asinpi_polyeval(u);
167+
float result = fputil::multiply_add(-2.0f16, asinpi_sqrt_u, ONE_OVER_TWO);
169168

170169
return fputil::cast<float16>(signed_result(result));
171170
}

0 commit comments

Comments
 (0)