[libc-commits] [libc] [libc][math][c23] Implement `asinpi` for `f32` and `f64` (PR #181475)
Mohamed Emad via libc-commits
libc-commits at lists.llvm.org
Sat Feb 14 05:35:53 PST 2026
https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/181475
>From a6bdea9ae786a6f8814ccff1170c3e7b0cca0940 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sat, 14 Feb 2026 14:43:43 +0200
Subject: [PATCH] [libc][math][c23] Implement `asinpi` for `f32` and `f64`
---
libc/docs/headers/math/index.rst | 2 +-
libc/shared/math.h | 3 +
libc/shared/math/asinpi.h | 22 ++++
libc/shared/math/asinpif.h | 22 ++++
libc/shared/math/asinpif16.h | 22 ++++
libc/src/__support/math/CMakeLists.txt | 42 +++++++
libc/src/__support/math/asinpi.h | 138 ++++++++++++++++++++++
libc/src/__support/math/asinpif.h | 127 ++++++++++++++++++++
libc/src/__support/math/asinpif16.h | 135 +++++++++++++++++++++
libc/src/math/CMakeLists.txt | 2 +
libc/src/math/asinpi.h | 20 ++++
libc/src/math/asinpif.h | 20 ++++
libc/src/math/generic/CMakeLists.txt | 32 +++--
libc/src/math/generic/asinpi.cpp | 16 +++
libc/src/math/generic/asinpif.cpp | 16 +++
libc/src/math/generic/asinpif16.cpp | 113 +-----------------
libc/test/shared/CMakeLists.txt | 3 +
libc/test/shared/shared_math_test.cpp | 3 +
libc/test/src/math/CMakeLists.txt | 23 ++++
libc/test/src/math/asinpi_test.cpp | 30 +++++
libc/test/src/math/asinpif_test.cpp | 38 ++++++
libc/test/src/math/smoke/CMakeLists.txt | 22 +++-
libc/test/src/math/smoke/asinpi_test.cpp | 47 ++++++++
libc/test/src/math/smoke/asinpif_test.cpp | 46 ++++++++
24 files changed, 821 insertions(+), 123 deletions(-)
create mode 100644 libc/shared/math/asinpi.h
create mode 100644 libc/shared/math/asinpif.h
create mode 100644 libc/shared/math/asinpif16.h
create mode 100644 libc/src/__support/math/asinpi.h
create mode 100644 libc/src/__support/math/asinpif.h
create mode 100644 libc/src/__support/math/asinpif16.h
create mode 100644 libc/src/math/asinpi.h
create mode 100644 libc/src/math/asinpif.h
create mode 100644 libc/src/math/generic/asinpi.cpp
create mode 100644 libc/src/math/generic/asinpif.cpp
create mode 100644 libc/test/src/math/asinpi_test.cpp
create mode 100644 libc/test/src/math/asinpif_test.cpp
create mode 100644 libc/test/src/math/smoke/asinpi_test.cpp
create mode 100644 libc/test/src/math/smoke/asinpif_test.cpp
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..fb8234dd2fc06
--- /dev/null
+++ b/libc/src/__support/math/asinpi.h
@@ -0,0 +1,138 @@
+//===-- 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 x^1
+ 0x1.b2995e7b7b5fdp-5, // 0.053051647697298 x^3
+ 0x1.8723a1d588a36p-6, // 0.023873241463784 x^5
+ 0x1.d1a452f20430dp-7, // 0.014210262776062 x^7
+ 0x1.3ce52a3a09f61p-7, // 0.009670873278153 x^9
+ 0x1.d2b33e303d375p-8, // 0.007121279413913 x^11
+ 0x1.69fde663c674fp-8, // 0.005523556468484 x^13
+ 0x1.235134885f19bp-8, // 0.004445147824637 x^15
+ 0x1.e1f567da15ce2p-9, // 0.003677052428468 x^17
+ 0x1.9744e53b4851bp-9, // 0.003107216818208 x^19
+ 0x1.5e0eb8d6eaa8p-9, // 0.002670726836603 x^21
+ 0x1.3116f30e47f56p-9, // 0.002327649278541 x^23
+};
+
+static constexpr 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)) {
+ return asinpi_polyeval(x);
+ }
+
+ // 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(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..75c83d99ff0a3
--- /dev/null
+++ b/libc/src/__support/math/asinpif.h
@@ -0,0 +1,127 @@
+//===-- 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 {
+
+static constexpr 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, 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<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..dcf0c2b2bf41b
--- /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 {
+
+static constexpr 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 = 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<float16>(signed_result(result));
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ASINPIF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index e37e22fdb58e6..a61f9048e495a 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -58,6 +58,8 @@ add_math_entrypoint_object(asinh)
add_math_entrypoint_object(asinhf)
add_math_entrypoint_object(asinhf16)
+add_math_entrypoint_object(asinpi)
+add_math_entrypoint_object(asinpif)
add_math_entrypoint_object(asinpif16)
add_math_entrypoint_object(atan)
diff --git a/libc/src/math/asinpi.h b/libc/src/math/asinpi.h
new file mode 100644
index 0000000000000..025e086ebdb5e
--- /dev/null
+++ b/libc/src/math/asinpi.h
@@ -0,0 +1,20 @@
+//===-- 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_MATH_ASINPI_H
+#define LLVM_LIBC_SRC_MATH_ASINPI_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+double asinpi(double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ASINPI_H
diff --git a/libc/src/math/asinpif.h b/libc/src/math/asinpif.h
new file mode 100644
index 0000000000000..b66bdcb16e4c9
--- /dev/null
+++ b/libc/src/math/asinpif.h
@@ -0,0 +1,20 @@
+//===-- 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_MATH_ASINPIF_H
+#define LLVM_LIBC_SRC_MATH_ASINPIF_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float asinpif(float x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ASINPIF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 96dc0e7ca4851..d0f8a979a34c7 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4180,6 +4180,27 @@ add_entrypoint_object(
libc.src.__support.math.asinhf16
)
+add_entrypoint_object(
+ asinpi
+ SRCS
+ asinpi.cpp
+ HDRS
+ ../asinpi.h
+ DEPENDS
+ libc.src.__support.math.asinpi
+)
+
+add_entrypoint_object(
+ asinpif
+ SRCS
+ asinpif.cpp
+ HDRS
+ ../asinpif.h
+ DEPENDS
+ libc.src.__support.math.asinpif
+)
+
+
add_entrypoint_object(
asinpif16
SRCS
@@ -4187,16 +4208,7 @@ add_entrypoint_object(
HDRS
../asinpif16.h
DEPENDS
- libc.hdr.errno_macros
- libc.hdr.fenv_macros
- libc.src.__support.FPUtil.cast
- libc.src.__support.FPUtil.except_value_utils
- libc.src.__support.FPUtil.fenv_impl
- libc.src.__support.FPUtil.fp_bits
- libc.src.__support.FPUtil.multiply_add
- libc.src.__support.FPUtil.polyeval
- libc.src.__support.FPUtil.sqrt
- libc.src.__support.macros.optimization
+ libc.src.__support.math.asinpif16
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/asinpi.cpp b/libc/src/math/generic/asinpi.cpp
new file mode 100644
index 0000000000000..f784f676b1d3b
--- /dev/null
+++ b/libc/src/math/generic/asinpi.cpp
@@ -0,0 +1,16 @@
+//===-- Double-precision asinpi(x) function -------------------------------===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/asinpi.h"
+#include "src/__support/math/asinpi.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, asinpi, (double x)) { return math::asinpi(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/asinpif.cpp b/libc/src/math/generic/asinpif.cpp
new file mode 100644
index 0000000000000..f302ecf4e2861
--- /dev/null
+++ b/libc/src/math/generic/asinpif.cpp
@@ -0,0 +1,16 @@
+//===-- Single-precision asinpif(x) function ------------------------------===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/asinpif.h"
+#include "src/__support/math/asinpif.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float, asinpif, (float x)) { return math::asinpif(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/asinpif16.cpp b/libc/src/math/generic/asinpif16.cpp
index 395f4c4ebc070..2b2cfc5c44281 100644
--- a/libc/src/math/generic/asinpif16.cpp
+++ b/libc/src/math/generic/asinpif16.cpp
@@ -6,122 +6,13 @@
//
//===----------------------------------------------------------------------===//
+#include "src/__support/math/asinpif16.h"
#include "src/math/asinpif16.h"
-#include "hdr/errno_macros.h"
-#include "hdr/fenv_macros.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
-#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/PolyEval.h"
-#include "src/__support/FPUtil/cast.h"
-#include "src/__support/FPUtil/except_value_utils.h"
-#include "src/__support/FPUtil/multiply_add.h"
-#include "src/__support/FPUtil/sqrt.h"
-#include "src/__support/macros/optimization.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(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)) {
- // aspinf16(NaN) = NaN
- if (xbits.is_nan()) {
- if (xbits.is_signaling_nan()) {
- fputil::raise_except_if_required(FE_INVALID);
- return FPBits::quiet_nan().get_val();
- }
- return x;
- }
-
- // 1 < |x| <= +/-inf
- 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 POLY_COEFFS[] = {
- 0x1.45f306dc9c889p-2, // x^1
- 0x1.b2995e7b7b5fdp-5, // x^3
- 0x1.8723a1d588a36p-6, // x^5
- 0x1.d1a452f20430dp-7, // x^7
- 0x1.3ce52a3a09f61p-7, // x^9
- 0x1.d2b33e303d375p-8, // x^11
- 0x1.69fde663c674fp-8, // x^13
- 0x1.235134885f19bp-8, // x^15
- };
- // polynomial evaluation using horner's method
- // work only for |x| in [0, 0.5]
- auto asinpi_polyeval = [&](double x) -> double {
- return x * fputil::polyeval(x * x, 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)) {
- // Use polynomial approximation of asin(x)/pi in the range [0, 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 fputil::cast<float16>(signed_result(result));
+ return math::asinpif16(x);
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7d72ced3e057c..932760bb0dd7d 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -18,6 +18,9 @@ add_fp_unittest(
libc.src.__support.math.asinf16
libc.src.__support.math.asinhf
libc.src.__support.math.asinhf16
+ libc.src.__support.math.asinpi
+ libc.src.__support.math.asinpif
+ libc.src.__support.math.asinpif16
libc.src.__support.math.atan
libc.src.__support.math.atan2
libc.src.__support.math.atan2f
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 9e024387efbfa..e614e5f4ce717 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -21,6 +21,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::sqrtf16(1.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinf16(0.0f16));
+ EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinpif16(0.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinhf16(0.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::atanf16(0.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::atanhf16(0.0f16));
@@ -80,6 +81,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::acoshf(1.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::asinf(0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::asinhf(0.0f));
+ EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::asinpif(0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atan2f(0.0f, 0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atanf(0.0f));
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::atanhf(0.0f));
@@ -123,6 +125,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_FP_EQ(0x1.921fb54442d18p+0, LIBC_NAMESPACE::shared::acos(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::asin(0.0));
+ EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::asinpi(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::atan(0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::atan2(0.0, 0.0));
EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::cbrt(0.0));
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index ff5c511922171..d641aa3987e41 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2461,6 +2461,29 @@ add_fp_unittest(
libc.src.math.asinf16
)
+add_fp_unittest(
+ asinpi_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ asinpi_test.cpp
+ DEPENDS
+ libc.src.math.asinpi
+)
+
+add_fp_unittest(
+ asinpif_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ asinpif_test.cpp
+ DEPENDS
+ libc.src.math.asinpif
+)
+
+
add_fp_unittest(
asinpif16_test
NEED_MPFR
diff --git a/libc/test/src/math/asinpi_test.cpp b/libc/test/src/math/asinpi_test.cpp
new file mode 100644
index 0000000000000..ebb12166aea12
--- /dev/null
+++ b/libc/test/src/math/asinpi_test.cpp
@@ -0,0 +1,30 @@
+//===-- Unittests for asinpi (MPFR) ---------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/asinpi.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+using LlvmLibcAsinpiTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcAsinpiTest, InDoubleRange) {
+ constexpr uint64_t COUNT = 100'000;
+ // Step through [0, 1.0] in double bits
+ constexpr uint64_t ONE_BITS = 0x3FF0000000000000ULL;
+ const uint64_t STEP = ONE_BITS / COUNT;
+ for (uint64_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinpi, x,
+ LIBC_NAMESPACE::asinpi(x), 0.5);
+ ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinpi, -x,
+ LIBC_NAMESPACE::asinpi(-x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/asinpif_test.cpp b/libc/test/src/math/asinpif_test.cpp
new file mode 100644
index 0000000000000..073d11bc53f21
--- /dev/null
+++ b/libc/test/src/math/asinpif_test.cpp
@@ -0,0 +1,38 @@
+//===-- Unittests for asinpif (MPFR) --------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/asinpif.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+using LlvmLibcAsinpifTest = LIBC_NAMESPACE::testing::FPTest<float>;
+
+static constexpr mpfr::RoundingMode ROUNDING_MODES[] = {
+ mpfr::RoundingMode::Nearest,
+ mpfr::RoundingMode::Upward,
+ mpfr::RoundingMode::Downward,
+ mpfr::RoundingMode::TowardZero,
+};
+
+TEST_F(LlvmLibcAsinpifTest, InFloatRange) {
+ constexpr uint32_t COUNT = 100'000;
+ // Test in [-1, 1]
+ const uint32_t STEP = 0x3F800000 / COUNT; // step through [0, 1.0f] in bits
+ for (mpfr::RoundingMode rm : ROUNDING_MODES) {
+ for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+ float x = FPBits(v).get_val();
+ ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinpi, x,
+ LIBC_NAMESPACE::asinpif(x), 0.5);
+ ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinpi, -x,
+ LIBC_NAMESPACE::asinpif(-x), 0.5);
+ }
+ }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 5afd3a9f22967..95bc91d6ed1a0 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4703,11 +4703,31 @@ add_fp_unittest(
libc.src.math.asinhf16
)
+add_fp_unittest(
+ asinpi_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ asinpi_test.cpp
+ DEPENDS
+ libc.src.math.asinpi
+)
+
+add_fp_unittest(
+ asinpif_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ asinpif_test.cpp
+ DEPENDS
+ libc.src.math.asinpif
+)
+
add_fp_unittest(
asinpif16_test
NEED_MPFR
SUITE
- libc-math-unittests
+ libc-math-smoke-tests
SRCS
asinpif16_test.cpp
DEPENDS
diff --git a/libc/test/src/math/smoke/asinpi_test.cpp b/libc/test/src/math/smoke/asinpi_test.cpp
new file mode 100644
index 0000000000000..24e11cdd5b8d5
--- /dev/null
+++ b/libc/test/src/math/smoke/asinpi_test.cpp
@@ -0,0 +1,47 @@
+//===-- Unittests for asinpi ----------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/asinpi.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcAsinpiTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcAsinpiTest, SpecialNumbers) {
+ // asinpi(+0) = +0
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0, LIBC_NAMESPACE::asinpi(0.0));
+ // asinpi(-0) = -0
+ EXPECT_FP_EQ_ALL_ROUNDING(-0.0, LIBC_NAMESPACE::asinpi(-0.0));
+
+ // asinpi(1) = 0.5
+ EXPECT_FP_EQ_ALL_ROUNDING(0.5, LIBC_NAMESPACE::asinpi(1.0));
+ // asinpi(-1) = -0.5
+ EXPECT_FP_EQ_ALL_ROUNDING(-0.5, LIBC_NAMESPACE::asinpi(-1.0));
+
+ // asinpi(NaN) = NaN
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpi(FPBits::quiet_nan().get_val()));
+
+ // asinpi(inf) = NaN
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpi(inf));
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpi(neg_inf));
+
+ // asinpi(x) = NaN for |x| > 1
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpi(2.0));
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpi(-2.0));
+}
+
+TEST_F(LlvmLibcAsinpiTest, KnownValues) {
+ // asinpi(0.5) = 1/6
+ double result = LIBC_NAMESPACE::asinpi(0.5);
+ double expected = 0x1.5555555555555p-3; // 1/6
+ EXPECT_FP_EQ_WITH_TOLERANCE(result, expected, 1);
+
+ // asinpi(-0.5) = -1/6
+ result = LIBC_NAMESPACE::asinpi(-0.5);
+ EXPECT_FP_EQ_WITH_TOLERANCE(result, -expected, 1);
+}
diff --git a/libc/test/src/math/smoke/asinpif_test.cpp b/libc/test/src/math/smoke/asinpif_test.cpp
new file mode 100644
index 0000000000000..d37d55f0c6fdd
--- /dev/null
+++ b/libc/test/src/math/smoke/asinpif_test.cpp
@@ -0,0 +1,46 @@
+//===-- Unittests for asinpif ---------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/asinpif.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcAsinpifTest = LIBC_NAMESPACE::testing::FPTest<float>;
+
+TEST_F(LlvmLibcAsinpifTest, SpecialNumbers) {
+ // asinpif(+0) = +0
+ EXPECT_FP_EQ_ALL_ROUNDING(0.0f, LIBC_NAMESPACE::asinpif(0.0f));
+ // asinpif(-0) = -0
+ EXPECT_FP_EQ_ALL_ROUNDING(-0.0f, LIBC_NAMESPACE::asinpif(-0.0f));
+
+ // asinpif(1) = 0.5
+ EXPECT_FP_EQ_ALL_ROUNDING(0.5f, LIBC_NAMESPACE::asinpif(1.0f));
+ // asinpif(-1) = -0.5
+ EXPECT_FP_EQ_ALL_ROUNDING(-0.5f, LIBC_NAMESPACE::asinpif(-1.0f));
+
+ // asinpif(NaN) = NaN
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpif(FPBits::quiet_nan().get_val()));
+
+ // asinpif(inf) = NaN (domain error)
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpif(inf));
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpif(neg_inf));
+
+ // asinpif(x) = NaN for |x| > 1
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpif(2.0f));
+ EXPECT_FP_IS_NAN(LIBC_NAMESPACE::asinpif(-2.0f));
+}
+
+TEST_F(LlvmLibcAsinpifTest, KnownValues) {
+ // asinpif(0.5) = asin(0.5)/pi = (pi/6)/pi = 1/6
+ float result = LIBC_NAMESPACE::asinpif(0.5f);
+ EXPECT_FP_EQ_WITH_TOLERANCE(result, 0x1.555556p-3f, 1); // ~0.16667
+
+ // asinpif(-0.5) = -1/6
+ result = LIBC_NAMESPACE::asinpif(-0.5f);
+ EXPECT_FP_EQ_WITH_TOLERANCE(result, -0x1.555556p-3f, 1);
+}
More information about the libc-commits
mailing list