[libc-commits] [libc] [libc][math][c23] Implement `asinpi` for `f32` and `f64` (PR #181475)
via libc-commits
libc-commits at lists.llvm.org
Sat Feb 14 04:57:20 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Mohamed Emad (hulxv)
<details>
<summary>Changes</summary>
Currently, `asinpi` is implemented for float16 in #<!-- -->152690. This PR implements it for single-precision and double-precision with the header-only approach that's tracked in #<!-- -->147386.
---
Patch is 40.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/181475.diff
24 Files Affected:
- (modified) libc/docs/headers/math/index.rst (+1-1)
- (modified) libc/shared/math.h (+3)
- (added) libc/shared/math/asinpi.h (+22)
- (added) libc/shared/math/asinpif.h (+22)
- (added) libc/shared/math/asinpif16.h (+22)
- (modified) libc/src/__support/math/CMakeLists.txt (+42)
- (added) libc/src/__support/math/asinpi.h (+139)
- (added) libc/src/__support/math/asinpif.h (+125)
- (added) libc/src/__support/math/asinpif16.h (+135)
- (modified) libc/src/math/CMakeLists.txt (+2)
- (added) libc/src/math/asinpi.h (+20)
- (added) libc/src/math/asinpif.h (+20)
- (modified) libc/src/math/generic/CMakeLists.txt (+22-10)
- (added) libc/src/math/generic/asinpi.cpp (+16)
- (added) libc/src/math/generic/asinpif.cpp (+16)
- (modified) libc/src/math/generic/asinpif16.cpp (+2-111)
- (modified) libc/test/shared/CMakeLists.txt (+3)
- (modified) libc/test/shared/shared_math_test.cpp (+3)
- (modified) libc/test/src/math/CMakeLists.txt (+23)
- (added) libc/test/src/math/asinpi_test.cpp (+30)
- (added) libc/test/src/math/asinpif_test.cpp (+38)
- (modified) libc/test/src/math/smoke/CMakeLists.txt (+21-1)
- (added) libc/test/src/math/smoke/asinpi_test.cpp (+47)
- (added) libc/test/src/math/smoke/asinpif_test.cpp (+46)
``````````diff
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 51bb17ff8dab6..a1160e043d5d2 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -269,7 +269,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| asinh | |check| | | | |check| | | | 7.12.5.2 | F.10.2.2 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| asinpi | | | | |check| | | | 7.12.4.9 | F.10.1.9 |
+| asinpi | |check| | |check| | | |check| | | | 7.12.4.9 | F.10.1.9 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| atan | |check| | 1 ULP | | |check| | | | 7.12.4.3 | F.10.1.3 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 66132326c2257..86edda3620604 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -22,6 +22,9 @@
#include "math/asinf16.h"
#include "math/asinhf.h"
#include "math/asinhf16.h"
+#include "math/asinpi.h"
+#include "math/asinpif.h"
+#include "math/asinpif16.h"
#include "math/atan.h"
#include "math/atan2.h"
#include "math/atan2f.h"
diff --git a/libc/shared/math/asinpi.h b/libc/shared/math/asinpi.h
new file mode 100644
index 0000000000000..3ee51f95a8a76
--- /dev/null
+++ b/libc/shared/math/asinpi.h
@@ -0,0 +1,22 @@
+//===-- Shared asinpi header ------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_ASINPI_H
+#define LLVM_LIBC_SHARED_MATH_ASINPI_H
+
+#include "src/__support/math/asinpi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::asinpi;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ASINPI_H
diff --git a/libc/shared/math/asinpif.h b/libc/shared/math/asinpif.h
new file mode 100644
index 0000000000000..627d1f19f2fc2
--- /dev/null
+++ b/libc/shared/math/asinpif.h
@@ -0,0 +1,22 @@
+//===-- Shared asinpif header -----------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_ASINPIF_H
+#define LLVM_LIBC_SHARED_MATH_ASINPIF_H
+
+#include "src/__support/math/asinpif.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::asinpif;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ASINPIF_H
diff --git a/libc/shared/math/asinpif16.h b/libc/shared/math/asinpif16.h
new file mode 100644
index 0000000000000..2db795319b767
--- /dev/null
+++ b/libc/shared/math/asinpif16.h
@@ -0,0 +1,22 @@
+//===-- Shared asinpif16 header ---------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_ASINPIF16_H
+#define LLVM_LIBC_SHARED_MATH_ASINPIF16_H
+
+#include "src/__support/math/asinpif16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::asinpif16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ASINPIF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c0293c0969217..920bbb3308f0f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -172,6 +172,48 @@ add_header_library(
libc.src.__support.macros.optimization
)
+add_header_library(
+ asinpi
+ HDRS
+ asinpi.h
+ DEPENDS
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ asinpif
+ HDRS
+ asinpif.h
+ DEPENDS
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ asinpif16
+ HDRS
+ asinpif16.h
+ DEPENDS
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.sqrt
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
atan_utils
HDRS
diff --git a/libc/src/__support/math/asinpi.h b/libc/src/__support/math/asinpi.h
new file mode 100644
index 0000000000000..b3a3a9fe171cb
--- /dev/null
+++ b/libc/src/__support/math/asinpi.h
@@ -0,0 +1,139 @@
+//===-- Implementation header for asinpi ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ASINPI_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINPI_H
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+// the coefficients for the polynomial approximation of asin(x)/pi in the
+// range [0, 0.5] extracted using python-sympy
+//
+// Python code to generate the coefficients:
+// > from sympy import *
+// > import math
+// > x = symbols('x')
+// > print(series(asin(x)/math.pi, x, 0, 25))
+//
+// OUTPUT:
+//
+// 0.318309886183791*x + 0.0530516476972984*x**3 + 0.0238732414637843*x**5 +
+// 0.0142102627760621*x**7 + 0.00967087327815336*x**9 +
+// 0.00712127941391293*x**11 + 0.00552355646848375*x**13 +
+// 0.00444514782463692*x**15 + 0.00367705242846804*x**17 +
+// 0.00310721681820837*x**19 + 0.00267072683660291*x**21 +
+// 0.00232764927854127*x**23 + O(x**25)
+static constexpr double ASINPI_POLY_COEFFS[] = {
+ 0x1.45f306dc9c889p-2, // 0.318309886183791
+ 0x1.b2995e7b7b5fdp-5, // 0.053051647697298
+ 0x1.8723a1d588a36p-6, // 0.023873241463784
+ 0x1.d1a452f20430dp-7, // 0.014210262776062
+ 0x1.3ce52a3a09f61p-7, // 0.009670873278153
+ 0x1.d2b33e303d375p-8, // 0.007121279413913
+ 0x1.69fde663c674fp-8, // 0.005523556468484
+ 0x1.235134885f19bp-8, // 0.004445147824637
+ 0x1.e1f567da15ce2p-9, // 0.003677052428468
+ 0x1.9744e53b4851bp-9, // 0.003107216818208
+ 0x1.5e0eb8d6eaa8p-9, // 0.002670726836603
+ 0x1.3116f30e47f56p-9, // 0.002327649278541
+};
+
+LIBC_INLINE double asinpi(double x) {
+ using FPBits = fputil::FPBits<double>;
+
+ FPBits xbits(x);
+ bool is_neg = xbits.is_neg();
+ double x_abs = fputil::cast<double>(xbits.abs().get_val());
+
+ auto signed_result = [is_neg](auto r) -> auto { return is_neg ? -r : r; };
+
+ if (LIBC_UNLIKELY(x_abs > 1.0)) {
+ if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ fputil::raise_except_if_required(FE_INVALID);
+ fputil::set_errno_if_required(EDOM);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // polynomial evaluation using horner's method
+ // work only for |x| in [0, 0.5]
+ auto asinpi_polyeval = [&](double v) -> double {
+ return v * fputil::polyeval(v * v, ASINPI_POLY_COEFFS[0],
+ ASINPI_POLY_COEFFS[1], ASINPI_POLY_COEFFS[2],
+ ASINPI_POLY_COEFFS[3], ASINPI_POLY_COEFFS[4],
+ ASINPI_POLY_COEFFS[5], ASINPI_POLY_COEFFS[6],
+ ASINPI_POLY_COEFFS[7], ASINPI_POLY_COEFFS[8],
+ ASINPI_POLY_COEFFS[9], ASINPI_POLY_COEFFS[10],
+ ASINPI_POLY_COEFFS[11]);
+ };
+
+ // if |x| <= 0.5:
+ if (LIBC_UNLIKELY(x_abs <= 0.5)) {
+ double result = asinpi_polyeval(fputil::cast<double>(x));
+ return fputil::cast<float16>(result);
+ }
+
+ // If |x| > 0.5, we need to use the range reduction method:
+ // y = asin(x) => x = sin(y)
+ // because: sin(a) = cos(pi/2 - a)
+ // therefore:
+ // x = cos(pi/2 - y)
+ // let z = pi/2 - y,
+ // x = cos(z)
+ // because: cos(2a) = 1 - 2 * sin^2(a), z = 2a, a = z/2
+ // therefore:
+ // cos(z) = 1 - 2 * sin^2(z/2)
+ // sin(z/2) = sqrt((1 - cos(z))/2)
+ // sin(z/2) = sqrt((1 - x)/2)
+ // let u = (1 - x)/2
+ // then:
+ // sin(z/2) = sqrt(u)
+ // z/2 = asin(sqrt(u))
+ // z = 2 * asin(sqrt(u))
+ // pi/2 - y = 2 * asin(sqrt(u))
+ // y = pi/2 - 2 * asin(sqrt(u))
+ // y/pi = 1/2 - 2 * asin(sqrt(u))/pi
+ //
+ // Finally, we can write:
+ // asinpi(x) = 1/2 - 2 * asinpi(sqrt(u))
+ // where u = (1 - x) /2
+ // = 0.5 - 0.5 * x
+ // = multiply_add(-0.5, x, 0.5)
+
+ double u = fputil::multiply_add(-0.5, x_abs, 0.5);
+ double asinpi_sqrt_u = asinpi_polyeval(fputil::sqrt<double>(u));
+ double result = fputil::multiply_add(-2.0, asinpi_sqrt_u, 0.5);
+
+ return signed_result(result);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ASINPI_H
diff --git a/libc/src/__support/math/asinpif.h b/libc/src/__support/math/asinpif.h
new file mode 100644
index 0000000000000..1f330c166dcbb
--- /dev/null
+++ b/libc/src/__support/math/asinpif.h
@@ -0,0 +1,125 @@
+//===-- Implementation header for asinpif -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ASINPIF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINPIF_H
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/types.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE float asinpif(float x) {
+ using FPBits = fputil::FPBits<float>;
+
+ FPBits xbits(x);
+ bool is_neg = xbits.is_neg();
+ double x_abs = fputil::cast<double>(xbits.abs().get_val());
+
+ auto signed_result = [is_neg](auto r) -> auto { return is_neg ? -r : r; };
+
+ if (LIBC_UNLIKELY(x_abs > 1.0)) {
+ if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ fputil::raise_except_if_required(FE_INVALID);
+ fputil::set_errno_if_required(EDOM);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // the coefficients for the polynomial approximation of asin(x)/pi in the
+ // range [0, 0.5] extracted using python-sympy
+ //
+ // Python code to generate the coefficients:
+ // > from sympy import *
+ // > import math
+ // > x = symbols('x')
+ // > print(series(asin(x)/math.pi, x, 0, 21))
+ //
+ // OUTPUT:
+ //
+ // 0.318309886183791*x + 0.0530516476972984*x**3 + 0.0238732414637843*x**5 +
+ // 0.0142102627760621*x**7 + 0.00967087327815336*x**9 +
+ // 0.00712127941391293*x**11 + 0.00552355646848375*x**13 +
+ // 0.00444514782463692*x**15 + 0.00367705242846804*x**17 +
+ // 0.00310721681820837*x**19 + O(x**21)
+ constexpr double ASINPI_POLY_COEFFS[] = {
+ 0x1.45f306dc9c889p-2, 0x1.b2995e7b7b5fdp-5, 0x1.8723a1d588a36p-6,
+ 0x1.d1a452f20430dp-7, 0x1.3ce52a3a09f61p-7, 0x1.d2b33e303d375p-8,
+ 0x1.69fde663c674fp-8, 0x1.235134885f19bp-8,
+ };
+
+ // polynomial evaluation using horner's method
+ // work only for |x| in [0, 0.5]
+ auto asinpi_polyeval = [&](double v) -> double {
+ return v * fputil::polyeval(v * v, POLY_COEFFS[0], POLY_COEFFS[1],
+ POLY_COEFFS[2], POLY_COEFFS[3], POLY_COEFFS[4],
+ POLY_COEFFS[5], POLY_COEFFS[6], POLY_COEFFS[7]);
+ };
+
+ // if |x| <= 0.5:
+ if (LIBC_UNLIKELY(x_abs <= 0.5)) {
+ double result = asinpi_polyeval(fputil::cast<double>(x));
+ return fputil::cast<float>(result);
+ }
+
+ // If |x| > 0.5, we need to use the range reduction method:
+ // y = asin(x) => x = sin(y)
+ // because: sin(a) = cos(pi/2 - a)
+ // therefore:
+ // x = cos(pi/2 - y)
+ // let z = pi/2 - y,
+ // x = cos(z)
+ // because: cos(2a) = 1 - 2 * sin^2(a), z = 2a, a = z/2
+ // therefore:
+ // cos(z) = 1 - 2 * sin^2(z/2)
+ // sin(z/2) = sqrt((1 - cos(z))/2)
+ // sin(z/2) = sqrt((1 - x)/2)
+ // let u = (1 - x)/2
+ // then:
+ // sin(z/2) = sqrt(u)
+ // z/2 = asin(sqrt(u))
+ // z = 2 * asin(sqrt(u))
+ // pi/2 - y = 2 * asin(sqrt(u))
+ // y = pi/2 - 2 * asin(sqrt(u))
+ // y/pi = 1/2 - 2 * asin(sqrt(u))/pi
+ //
+ // Finally, we can write:
+ // asinpi(x) = 1/2 - 2 * asinpi(sqrt(u))
+ // where u = (1 - x) /2
+ // = 0.5 - 0.5 * x
+ // = multiply_add(-0.5, x, 0.5)
+
+ double u = fputil::multiply_add(-0.5, x_abs, 0.5);
+ double asinpi_sqrt_u = asinpi_polyeval(fputil::sqrt<double>(u));
+ double result = fputil::multiply_add(-2.0, asinpi_sqrt_u, 0.5);
+
+ return fputil::cast<float>(signed_result(result));
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ASINPIF_H
diff --git a/libc/src/__support/math/asinpif16.h b/libc/src/__support/math/asinpif16.h
new file mode 100644
index 0000000000000..1a5872524d1e4
--- /dev/null
+++ b/libc/src/__support/math/asinpif16.h
@@ -0,0 +1,135 @@
+//===-- Implementation header for asinpif16 ---------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ASINPIF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINPIF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/types.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE float16 asinpif16(float16 x) {
+ using FPBits = fputil::FPBits<float16>;
+
+ FPBits xbits(x);
+ bool is_neg = xbits.is_neg();
+ double x_abs = fputil::cast<double>(xbits.abs().get_val());
+
+ auto signed_result = [is_neg](auto r) -> auto { return is_neg ? -r : r; };
+
+ if (LIBC_UNLIKELY(x_abs > 1.0)) {
+ if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ fputil::raise_except_if_required(FE_INVALID);
+ fputil::set_errno_if_required(EDOM);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // the coefficients for the polynomial approximation of asin(x)/pi in the
+ // range [0, 0.5] extracted using python-sympy
+ //
+ // Python code to generate the coefficients:
+ // > from sympy import *
+ // > import math
+ // > x = symbols('x')
+ // > print(series(asin(x)/math.pi, x, 0, 21))
+ //
+ // OUTPUT:
+ //
+ // 0.318309886183791*x + 0.0530516476972984*x**3 + 0.0238732414637843*x**5 +
+ // 0.0142102627760621*x**7 + 0.00967087327815336*x**9 +
+ // 0.00712127941391293*x**11 + 0.00552355646848375*x**13 +
+ // 0.00444514782463692*x**15 + 0.00367705242846804*x**17 +
+ // 0.00310721681820837*x**19 + O(x**21)
+ //
+ // it's very accurate in the range [0, 0.5] and has a maximum error of
+ // 0.0000000000000001 in the range [0, 0.5].
+ constexpr double ASINPI_POLY_COEFFS[] = {
+ 0x1.45f306dc9c889p-2, 0x1.b2995e7b7b5fdp-5, 0x1.8723a1d588a36p-6,
+ 0x1.d1a452f20430dp-7, 0x1.3ce52a3a09f61p-7, 0x1.d2b33e303d375p-8,
+ 0x1.69fde663c674fp-8, 0x1.235134885f19bp-8,
+ };
+ // polynomial evaluation using horner's method
+ // work only for |x| in [0, 0.5]
+ auto asinpi_polyeval = [&](double v) -> double {
+ return v * fputil::polyeval(v * v, ASINPI_POLY_COEFFS[0],
+ ASINPI_POLY_COEFFS[1], ASINPI_POLY_COEFFS[2],
+ ASINPI_POLY_COEFFS[3], ASINPI_POLY_COEFFS[4],
+ ASINPI_POLY_COEFFS[5], ASINPI_POLY_COEFFS[6],
+ ASINPI_POLY_COEFFS[7]);
+ };
+
+ // if |x| <= 0.5:
+ if (LIBC_UNLIKELY(x_abs <= 0.5)) {
+ double result = asinpi_polyeval(fputil::cast<double>(x));
+ return fputil::cast<float16>(result);
+ }
+
+ // If |x| > 0.5, we need to use the range reduction method:
+ // y = asin(x) => x = sin(y)
+ // because: sin(a) = cos(pi/2 - a)
+ // therefore:
+ // x = cos(pi/2 - y)
+ // let z = pi/2 - y,
+ // x = cos(z)
+ // because: cos(2a) = 1 - 2 * sin^2(a), z = 2a, a = z/2
+ // therefore:
+ // cos(z) = 1 - 2 * sin^2(z/2)
+ // sin(z/2) = sqrt((1 - cos(z))/2)
+ // sin(z/2) = sqrt((1 - x)/2)
+ // let u = (1 - x)/2
+ // then:
+ // sin(z/2) = sqrt(u)
+ // z/2 = asin(sqrt(u))
+ // z =...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/181475
More information about the libc-commits
mailing list