| |
| |
| |
| |
| |
| |
| |
|
|
| #include "src/math/acos.h" |
| #include "asin_utils.h" |
| #include "src/__support/FPUtil/FEnvImpl.h" |
| #include "src/__support/FPUtil/FPBits.h" |
| #include "src/__support/FPUtil/PolyEval.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" |
| #include "src/__support/macros/properties/cpu_features.h" |
|
|
| namespace LIBC_NAMESPACE_DECL { |
|
|
| using DoubleDouble = fputil::DoubleDouble; |
| using Float128 = fputil::DyadicFloat<128>; |
|
|
| LLVM_LIBC_FUNCTION(double, acos, (double x)) { |
| using FPBits = fputil::FPBits<double>; |
|
|
| FPBits xbits(x); |
| int x_exp = xbits.get_biased_exponent(); |
|
|
| |
| if (x_exp < FPBits::EXP_BIAS - 1) { |
| |
| if (LIBC_UNLIKELY(x_exp < FPBits::EXP_BIAS - 55)) { |
| |
| #if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) |
| return PI_OVER_TWO.hi; |
| #else |
| |
| |
| return (xbits.abs().get_val() + 0x1.0p-160) + PI_OVER_TWO.hi; |
| #endif |
| } |
|
|
| #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| |
| |
| double p = asin_eval(x * x); |
| return PI_OVER_TWO.hi + fputil::multiply_add(-x, p, PI_OVER_TWO.lo); |
| #else |
| unsigned idx; |
| DoubleDouble x_sq = fputil::exact_mult(x, x); |
| double err = xbits.abs().get_val() * 0x1.0p-51; |
| |
| |
| DoubleDouble p = asin_eval(x_sq, idx, err); |
| |
| DoubleDouble r0 = fputil::exact_mult(x, p.hi); |
| |
| |
| |
| double r_hi = fputil::multiply_add(-x, p.hi, PI_OVER_TWO.hi); |
| |
| 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); |
|
|
| |
|
|
| 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; |
|
|
| |
|
|
| |
| idx = static_cast<unsigned>(fputil::nearest_integer(x_sq.hi * 0x1.0p6)); |
|
|
| |
| |
| |
| Float128 x_f128(x); |
|
|
| #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| |
| 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 |
|
|
| Float128 p_f128 = asin_eval(u, idx); |
| |
| 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 |
| } |
| |
|
|
| double x_abs = xbits.abs().get_val(); |
|
|
| |
| constexpr double SIGN[2] = {1.0, -1.0}; |
| double x_sign = SIGN[xbits.is_neg()]; |
| |
| if (LIBC_UNLIKELY(x_exp >= FPBits::EXP_BIAS)) { |
| |
| if (x_abs == 1.0) { |
| |
| |
| return x == 1.0 ? 0.0 : fputil::multiply_add(-x_sign, PI.hi, PI.lo); |
| } |
| |
| if (xbits.is_quiet_nan()) |
| return x; |
|
|
| |
| if (!xbits.is_nan()) |
| fputil::set_errno_if_required(EDOM); |
|
|
| fputil::raise_except_if_required(FE_INVALID); |
| return FPBits::quiet_nan().get_val(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| double u = fputil::multiply_add(x_abs, -0.5, 0.5); |
| |
| |
| |
| |
| |
| |
| |
| |
| 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_DOUBLE |
| double h = fputil::multiply_add(v_hi, -v_hi, u); |
| #else |
| DoubleDouble v_hi_sq = fputil::exact_mult(v_hi, v_hi); |
| double h = (u - v_hi_sq.hi) - v_hi_sq.lo; |
| #endif |
|
|
| |
| |
| |
| double vh = v_hi * 2.0; |
| double vl = h / v_hi; |
|
|
| |
| |
| unsigned idx; |
| double err = vh * 0x1.0p-51; |
|
|
| DoubleDouble p = asin_eval(DoubleDouble{0.0, u}, idx, err); |
|
|
| |
| |
| DoubleDouble r0 = fputil::quick_mult(DoubleDouble{vl, vh}, p); |
|
|
| double r_hi, r_lo; |
| if (xbits.is_pos()) { |
| r_hi = r0.hi; |
| r_lo = r0.lo; |
| } else { |
| DoubleDouble r = fputil::exact_add(PI.hi, -r0.hi); |
| r_hi = r.hi; |
| r_lo = (PI.lo - r0.lo) + r.lo; |
| } |
|
|
| |
|
|
| 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; |
|
|
| |
| |
| idx = static_cast<unsigned>(fputil::nearest_integer(u * 0x1.0p6)); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| double vl_lo = fputil::multiply_add(-v_hi, vl, h) / v_hi; |
| #else |
| DoubleDouble vh_vl = fputil::exact_mult(v_hi, vl); |
| double vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi; |
| #endif |
| |
| double t = h * (-0.25) / u; |
| double vll = fputil::multiply_add(vl, t, vl_lo); |
| |
| Float128 m_v = fputil::quick_add( |
| Float128(vh), fputil::quick_add(Float128(vl), Float128(vll))); |
| m_v.sign = xbits.sign(); |
|
|
| |
| |
| |
| Float128 y_f128(fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, u)); |
|
|
| Float128 p_f128 = asin_eval(y_f128, idx); |
| Float128 r_f128 = fputil::quick_mul(m_v, p_f128); |
|
|
| if (xbits.is_neg()) |
| r_f128 = fputil::quick_add(PI_F128, r_f128); |
|
|
| return static_cast<double>(r_f128); |
| #endif |
| } |
|
|
| } |
|
|