-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[libc][math] Refactor acos implementation to header-only in src/__support/math folder. #148409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…__support/math folder.
…_support/math folder.
…upport/math folder
…pport/math folder.
…upport/math folder.
…_support/math folder.
…port/math folder.
@llvm/pr-subscribers-libc Author: Muhammad Bassiouni (bassiounix) ChangesPart of #147386 in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450 Please merge #148408 first @lntue Patch is 199.40 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/148409.diff 45 Files Affected:
diff --git a/libc/shared/math.h b/libc/shared/math.h
index e2950e075a81d..8dcfaf0352339 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -11,10 +11,18 @@
#include "libc_common.h"
+#include "math/acos.h"
+#include "math/exp.h"
+#include "math/exp10.h"
+#include "math/exp10f.h"
+#include "math/exp10f16.h"
#include "math/expf.h"
#include "math/expf16.h"
#include "math/frexpf.h"
#include "math/frexpf128.h"
#include "math/frexpf16.h"
+#include "math/ldexpf.h"
+#include "math/ldexpf128.h"
+#include "math/ldexpf16.h"
#endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/acos.h b/libc/shared/math/acos.h
new file mode 100644
index 0000000000000..73c6b512e16f4
--- /dev/null
+++ b/libc/shared/math/acos.h
@@ -0,0 +1,23 @@
+//===-- Shared acos function ------------------------------------*- 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_ACOS_H
+#define LLVM_LIBC_SHARED_MATH_ACOS_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/acos.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::acos;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_ACOS_H
diff --git a/libc/shared/math/exp.h b/libc/shared/math/exp.h
new file mode 100644
index 0000000000000..7cdd6331e613a
--- /dev/null
+++ b/libc/shared/math/exp.h
@@ -0,0 +1,23 @@
+//===-- Shared exp function -------------------------------------*- 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_EXP_H
+#define LLVM_LIBC_SHARED_MATH_EXP_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/exp.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::exp;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_EXP_H
diff --git a/libc/shared/math/exp10.h b/libc/shared/math/exp10.h
new file mode 100644
index 0000000000000..3d36d9103705f
--- /dev/null
+++ b/libc/shared/math/exp10.h
@@ -0,0 +1,23 @@
+//===-- Shared exp10 function -----------------------------------*- 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_EXP10_H
+#define LLVM_LIBC_SHARED_MATH_EXP10_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/exp10.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::exp10;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_EXP10_H
diff --git a/libc/shared/math/exp10f.h b/libc/shared/math/exp10f.h
new file mode 100644
index 0000000000000..556e78ab3b7a7
--- /dev/null
+++ b/libc/shared/math/exp10f.h
@@ -0,0 +1,24 @@
+//===-- Shared exp10f function -----------------------------------*- 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_EXP10F_H
+#define LLVM_LIBC_SHARED_MATH_EXP10F_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/exp10f.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::exp10f;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_EXP10F_H
diff --git a/libc/shared/math/exp10f16.h b/libc/shared/math/exp10f16.h
new file mode 100644
index 0000000000000..8acdbdb7c70a1
--- /dev/null
+++ b/libc/shared/math/exp10f16.h
@@ -0,0 +1,29 @@
+//===-- Shared exp10f16 function --------------------------------*- 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_EXP10F_H
+#define LLVM_LIBC_SHARED_MATH_EXP10F_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/math/exp10f16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::exp10f16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_EXP10F_H
diff --git a/libc/shared/math/ldexpf.h b/libc/shared/math/ldexpf.h
new file mode 100644
index 0000000000000..497933c47321f
--- /dev/null
+++ b/libc/shared/math/ldexpf.h
@@ -0,0 +1,23 @@
+//===-- Shared ldexpf function ----------------------------------*- 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_LDEXPF_H
+#define LLVM_LIBC_SHARED_MATH_LDEXPF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/ldexpf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::ldexpf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_LDEXPF_H
diff --git a/libc/shared/math/ldexpf128.h b/libc/shared/math/ldexpf128.h
new file mode 100644
index 0000000000000..d4066beb809c7
--- /dev/null
+++ b/libc/shared/math/ldexpf128.h
@@ -0,0 +1,29 @@
+//===-- Shared ldexpf128 function -------------------------------*- 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_LDEXPF128_H
+#define LLVM_LIBC_SHARED_MATH_LDEXPF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/ldexpf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::ldexpf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_LDEXPF128_H
diff --git a/libc/shared/math/ldexpf16.h b/libc/shared/math/ldexpf16.h
new file mode 100644
index 0000000000000..d4e39c449943e
--- /dev/null
+++ b/libc/shared/math/ldexpf16.h
@@ -0,0 +1,31 @@
+//===-- Shared ldexpf16 function --------------------------------*- 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_ldexpf16_H
+#define LLVM_LIBC_SHARED_MATH_ldexpf16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/math/ldexpf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace shared {
+
+using math::ldexpf16;
+
+} // namespace shared
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_ldexpf16_H
diff --git a/libc/src/__support/FPUtil/double_double.h b/libc/src/__support/FPUtil/double_double.h
index c27885aadc028..8e54e845de493 100644
--- a/libc/src/__support/FPUtil/double_double.h
+++ b/libc/src/__support/FPUtil/double_double.h
@@ -151,8 +151,8 @@ LIBC_INLINE DoubleDouble quick_mult(double a, const DoubleDouble &b) {
}
template <size_t SPLIT_B = 27>
-LIBC_INLINE DoubleDouble quick_mult(const DoubleDouble &a,
- const DoubleDouble &b) {
+LIBC_INLINE constexpr DoubleDouble quick_mult(const DoubleDouble &a,
+ const DoubleDouble &b) {
DoubleDouble r = exact_mult<double, SPLIT_B>(a.hi, b.hi);
double t1 = multiply_add(a.hi, b.lo, r.lo);
double t2 = multiply_add(a.lo, b.hi, t1);
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 0baf12a26a9d5..4a29c2975d523 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1,3 +1,36 @@
+add_header_library(
+ acos
+ HDRS
+ acos.h
+ DEPENDS
+ .asin_utils
+ libc.src.__support.math.asin_utils
+ libc.src.__support.FPUtil.double_double
+ libc.src.__support.FPUtil.dyadic_float
+ 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.macros.properties.types
+ libc.src.__support.macros.properties.cpu_features
+)
+
+add_header_library(
+ asin_utils
+ HDRS
+ asin_utils.h
+ DEPENDS
+ libc.src.__support.integer_literals
+ libc.src.__support.FPUtil.double_double
+ libc.src.__support.FPUtil.dyadic_float
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.nearest_integer
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
exp_float_constants
HDRS
@@ -82,3 +115,153 @@ add_header_library(
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
)
+
+add_header_library(
+ ldexpf128
+ HDRS
+ ldexpf128.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.manipulation_functions
+ libc.include.llvm-libc-types.float128
+)
+
+add_header_library(
+ ldexpf16
+ HDRS
+ ldexpf16.h
+ DEPENDS
+ libc.src.__support.macros.properties.types
+ libc.src.__support.FPUtil.manipulation_functions
+ libc.include.llvm-libc-macros.float16_macros
+)
+
+add_header_library(
+ ldexpf
+ HDRS
+ ldexpf.h
+ DEPENDS
+ libc.src.__support.FPUtil.manipulation_functions
+)
+
+add_header_library(
+ exp_constants
+ HDRS
+ exp_constants.h
+ DEPENDS
+ libc.src.__support.FPUtil.triple_double
+)
+
+add_header_library(
+ exp_utils
+ HDRS
+ exp_utils.h
+ DEPENDS
+ libc.src.__support.CPP.optional
+ libc.src.__support.CPP.bit
+ libc.src.__support.FPUtil.fp_bits
+)
+
+add_header_library(
+ exp
+ HDRS
+ exp.h
+ DEPENDS
+ .exp_constants
+ .exp_utils
+ libc.src.__support.CPP.bit
+ libc.src.__support.CPP.optional
+ libc.src.__support.FPUtil.dyadic_float
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.nearest_integer
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.FPUtil.triple_double
+ libc.src.__support.integer_literals
+ libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ exp10
+ HDRS
+ exp10.h
+ DEPENDS
+ .exp_constants
+ .exp_utils
+ libc.src.__support.CPP.bit
+ libc.src.__support.CPP.optional
+ libc.src.__support.FPUtil.dyadic_float
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.nearest_integer
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.FPUtil.triple_double
+ libc.src.__support.integer_literals
+ libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ exp10f_utils
+ HDRS
+ exp10f_utils.h
+ DEPENDS
+ libc.src.__support.FPUtil.basic_operations
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.nearest_integer
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.common
+ libc.src.__support.math.exp_utils
+)
+
+add_header_library(
+ exp10f
+ HDRS
+ exp10f.h
+ DEPENDS
+ .exp10f_utils
+ libc.src.__support.macros.config
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.macros.optimization
+)
+
+add_header_library(
+ exp10_float16_constants
+ HDRS
+ exp10_float16_constants.h
+ DEPENDS
+ libc.src.__support.CPP.array
+)
+
+add_header_library(
+ exp10f16_utils
+ HDRS
+ exp10f16_utils.h
+ DEPENDS
+ .expf16_utils
+ .exp10_float16_constants
+ libc.src.__support.FPUtil.fp_bits
+)
+
+add_header_library(
+ exp10f16
+ HDRS
+ exp10f16.h
+ DEPENDS
+ .exp10f16_utils
+ libc.src.__support.FPUtil.fp_bits
+ src.__support.FPUtil.FEnvImpl
+ src.__support.FPUtil.FPBits
+ src.__support.FPUtil.cast
+ src.__support.FPUtil.rounding_mode
+ src.__support.FPUtil.except_value_utils
+ src.__support.macros.optimization
+ src.__support.macros.properties.cpu_features
+)
diff --git a/libc/src/__support/math/acos.h b/libc/src/__support/math/acos.h
new file mode 100644
index 0000000000000..a7287f11aa302
--- /dev/null
+++ b/libc/src/__support/math/acos.h
@@ -0,0 +1,285 @@
+//===-- Implementation header for acos --------------------------*- 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_ACOS_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ACOS_H
+
+#include "asin_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/dyadic_float.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" // LIBC_UNLIKELY
+#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+using DoubleDouble = fputil::DoubleDouble;
+using Float128 = fputil::DyadicFloat<128>;
+
+static constexpr double acos(double x) {
+ using FPBits = fputil::FPBits<double>;
+
+ FPBits xbits(x);
+ int x_exp = xbits.get_biased_exponent();
+
+ // |x| < 0.5.
+ if (x_exp < FPBits::EXP_BIAS - 1) {
+ // |x| < 2^-55.
+ if (LIBC_UNLIKELY(x_exp < FPBits::EXP_BIAS - 55)) {
+ // When |x| < 2^-55, acos(x) = pi/2
+#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS)
+ return PI_OVER_TWO.hi;
+#else
+ // Force the evaluation and prevent constant propagation so that it
+ // is rounded correctly for FE_UPWARD rounding mode.
+ return (xbits.abs().get_val() + 0x1.0p-160) + PI_OVER_TWO.hi;
+#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ }
+
+#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ // acos(x) = pi/2 - asin(x)
+ // = pi/2 - x * P(x^2)
+ double p = asin_eval(x * x);
+ return PI_OVER_TWO.hi + fputil::multiply_add(-x, p, PI_OVER_TWO.lo);
+#else
+ unsigned idx = 0;
+ DoubleDouble x_sq = fputil::exact_mult(x, x);
+ double err = xbits.abs().get_val() * 0x1.0p-51;
+ // Polynomial approximation:
+ // p ~ asin(x)/x
+ DoubleDouble p = asin_eval(x_sq, idx, err);
+ // asin(x) ~ x * p
+ DoubleDouble r0 = fputil::exact_mult(x, p.hi);
+ // acos(x) = pi/2 - asin(x)
+ // ~ pi/2 - x * p
+ // = pi/2 - x * (p.hi + p.lo)
+ double r_hi = fputil::multiply_add(-x, p.hi, PI_OVER_TWO.hi);
+ // Use Dekker's 2SUM algorithm to compute the lower part.
+ double r_lo = ((PI_OVER_TWO.hi - r_hi) - r0.hi) - r0.lo;
+ r_lo = fputil::multiply_add(-x, p.lo, r_lo + PI_OVER_TWO.lo);
+
+ // Ziv's accuracy test.
+
+ double r_upper = r_hi + (r_lo + err);
+ double r_lower = r_hi + (r_lo - err);
+
+ if (LIBC_LIKELY(r_upper == r_lower))
+ return r_upper;
+
+ // Ziv's accuracy test failed, perform 128-bit calculation.
+
+ // Recalculate mod 1/64.
+ idx = static_cast<unsigned>(fputil::nearest_integer(x_sq.hi * 0x1.0p6));
+
+ // Get x^2 - idx/64 exactly. When FMA is available, double-double
+ // multiplication will be correct for all rounding modes. Otherwise we use
+ // Float128 directly.
+ Float128 x_f128(x);
+
+#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+ // u = x^2 - idx/64
+ Float128 u_hi(
+ fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, x_sq.hi));
+ Float128 u = fputil::quick_add(u_hi, Float128(x_sq.lo));
+#else
+ Float128 x_sq_f128 = fputil::quick_mul(x_f128, x_f128);
+ Float128 u = fputil::quick_add(
+ x_sq_f128, Float128(static_cast<double>(idx) * (-0x1.0p-6)));
+#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+
+ Float128 p_f128 = asin_eval(u, idx);
+ // Flip the sign of x_f128 to perform subtraction.
+ x_f128.sign = x_f128.sign.negate();
+ Float128 r =
+ fputil::quick_add(PI_OVER_TWO_F128, fputil::quick_mul(x_f128, p_f128));
+
+ return static_cast<double>(r);
+#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ }
+ // |x| >= 0.5
+
+ double x_abs = xbits.abs().get_val();
+
+ // Maintaining the sign:
+ constexpr double SIGN[2] = {1.0, -1.0};
+ double x_sign = SIGN[xbits.is_neg()];
+ // |x| >= 1
+ if (LIBC_UNLIKELY(x_exp >= FPBits::EXP_BIAS)) {
+ // x = +-1, asin(x) = +- pi/2
+ if (x_abs == 1.0) {
+ // x = 1, acos(x) = 0,
+ // x = -1, acos(x) = pi
+ return x == 1.0 ? 0.0 : fputil::multiply_add(-x_sign, PI.hi, PI.lo);
+ }
+ // |x| > 1, return NaN.
+ if (xbits.is_quiet_nan())
+ return x;
+
+ // Set domain error for non-NaN input.
+ if (!xbits.is_nan())
+ fputil::set_errno_if_required(EDOM);
+
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // When |x| >= 0.5, we perform range reduction as follow:
+ //
+ // When 0.5 <= x < 1, let:
+ // y = acos(x)
+ // We will use the double angle formula:
+ // cos(2y) = 1 - 2 sin^2(y)
+ // and the complement angle identity:
+ // x = cos(y) = 1 - 2 sin^2 (y/2)
+ // So:
+ // sin(y/2) = sqrt( (1 - x)/2 )
+ // And hence:
+ // y/2 = asin( sqrt( (1 - x)/2 ) )
+ // Equivalently:
+ // acos(x) = y = 2 * asin( sqrt( (1 - x)/2 ) )
+ // Let u = (1 - x)/2, then:
+ // acos(x) = 2 * asin( sqrt(u) )
+ // Moreover, since 0.5 <= x < 1:
+ // 0 < u <= 1/4, and 0 < sqrt(u) <= 0.5,
+ // And hence we can reuse the same polynomial approximation of asin(x) when
+ // |x| <= 0.5:
+ // acos(x) ~ 2 * sqrt(u) * P(u).
+ //
+ // When -1 < x <= -0.5, we reduce to the previous case using the formula:
+ // acos(x) = pi - acos(-x)
+ // = pi - 2 * asin ( sqrt( (1 + x)/2 ) )
+ // ~ pi - 2 * sqrt(u) * P(u),
+ // where u = (1 - |x|)/2.
+
+ // u = (1 - |x|)/2
+ double u = fputil::multiply_add(x_abs, -0.5, 0.5);
+ // v_hi + v_lo ~ sqrt(u).
+ // Let:
+ // h = u - v_hi^2 = (sqrt(u) - v_hi) * (sqrt(u) + v_hi)
+ // Then:
+ // sqrt(u) = v_hi + h / (sqrt(u) + v_hi)
+ // ~ v_hi + h / (2 * v_hi)
+ // So we can use:
+ // v_lo = h / (2 * v_hi).
+ double v_hi = fputil::sqrt<double>(u);
+
+#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr DoubleDouble CONST_TERM[2] = {{0.0, 0.0}, PI};
+ DoubleDouble const_term = CONST_TERM[xbits.is_neg()];
+
+ double p = asin_eval(u);
+ double scale = x_sign * 2.0 * v_hi;
+ double r = const_term.hi + fputil::multiply_add(scale, p, const_term.lo);
+ return r;
+#else
+
+#ifdef LIBC_TARGET_CPU_HAS_FMA_...
[truncated]
|
Part of #147386
in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
Please merge #148408 first