| 1 | //===-- Implementation header for asin --------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ASIN_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_MATH_ASIN_H |
| 11 | |
| 12 | #include "asin_utils.h" |
| 13 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | #include "src/__support/FPUtil/FPBits.h" |
| 15 | #include "src/__support/FPUtil/double_double.h" |
| 16 | #include "src/__support/FPUtil/dyadic_float.h" |
| 17 | #include "src/__support/FPUtil/multiply_add.h" |
| 18 | #include "src/__support/FPUtil/sqrt.h" |
| 19 | #include "src/__support/macros/config.h" |
| 20 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 21 | #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA |
| 22 | #include "src/__support/math/asin_utils.h" |
| 23 | |
| 24 | namespace LIBC_NAMESPACE_DECL { |
| 25 | |
| 26 | namespace math { |
| 27 | |
| 28 | LIBC_INLINE double asin(double x) { |
| 29 | using namespace asin_internal; |
| 30 | using FPBits = fputil::FPBits<double>; |
| 31 | |
| 32 | FPBits xbits(x); |
| 33 | int x_exp = xbits.get_biased_exponent(); |
| 34 | |
| 35 | // |x| < 0.5. |
| 36 | if (x_exp < FPBits::EXP_BIAS - 1) { |
| 37 | // |x| < 2^-26. |
| 38 | if (LIBC_UNLIKELY(x_exp < FPBits::EXP_BIAS - 26)) { |
| 39 | // When |x| < 2^-26, the relative error of the approximation asin(x) ~ x |
| 40 | // is: |
| 41 | // |asin(x) - x| / |asin(x)| < |x^3| / (6|x|) |
| 42 | // = x^2 / 6 |
| 43 | // < 2^-54 |
| 44 | // < epsilon(1)/2. |
| 45 | // So the correctly rounded values of asin(x) are: |
| 46 | // = x + sign(x)*eps(x) if rounding mode = FE_TOWARDZERO, |
| 47 | // or (rounding mode = FE_UPWARD and x is |
| 48 | // negative), |
| 49 | // = x otherwise. |
| 50 | // To simplify the rounding decision and make it more efficient, we use |
| 51 | // fma(x, 2^-54, x) instead. |
| 52 | // Note: to use the formula x + 2^-54*x to decide the correct rounding, we |
| 53 | // do need fma(x, 2^-54, x) to prevent underflow caused by 2^-54*x when |
| 54 | // |x| < 2^-1022. For targets without FMA instructions, when x is close to |
| 55 | // denormal range, we normalize x, |
| 56 | #if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) |
| 57 | return x; |
| 58 | #elif defined(LIBC_TARGET_CPU_HAS_FMA_DOUBLE) |
| 59 | return fputil::multiply_add(x, 0x1.0p-54, x); |
| 60 | #else |
| 61 | if (xbits.abs().uintval() == 0) |
| 62 | return x; |
| 63 | // Get sign(x) * min_normal. |
| 64 | FPBits eps_bits = FPBits::min_normal(); |
| 65 | eps_bits.set_sign(xbits.sign()); |
| 66 | double eps = eps_bits.get_val(); |
| 67 | double normalize_const = (x_exp == 0) ? eps : 0.0; |
| 68 | double scaled_normal = |
| 69 | fputil::multiply_add(x: x + normalize_const, y: 0x1.0p54, z: eps); |
| 70 | return fputil::multiply_add(x: scaled_normal, y: 0x1.0p-54, z: -normalize_const); |
| 71 | #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 72 | } |
| 73 | |
| 74 | #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 75 | return x * asin_eval(x * x); |
| 76 | #else |
| 77 | using DFloat128 = fputil::DyadicFloat<128>; |
| 78 | using DoubleDouble = fputil::DoubleDouble; |
| 79 | |
| 80 | unsigned idx = 0; |
| 81 | DoubleDouble x_sq = fputil::exact_mult(a: x, b: x); |
| 82 | double err = xbits.abs().get_val() * 0x1.0p-51; |
| 83 | // Polynomial approximation: |
| 84 | // p ~ asin(x)/x |
| 85 | |
| 86 | DoubleDouble p = asin_eval(u: x_sq, idx, err); |
| 87 | // asin(x) ~ x * (ASIN_COEFFS[idx][0] + p) |
| 88 | DoubleDouble r0 = fputil::exact_mult(a: x, b: p.hi); |
| 89 | double r_lo = fputil::multiply_add(x, y: p.lo, z: r0.lo); |
| 90 | |
| 91 | // Ziv's accuracy test. |
| 92 | |
| 93 | double r_upper = r0.hi + (r_lo + err); |
| 94 | double r_lower = r0.hi + (r_lo - err); |
| 95 | |
| 96 | if (LIBC_LIKELY(r_upper == r_lower)) |
| 97 | return r_upper; |
| 98 | |
| 99 | // Ziv's accuracy test failed, perform 128-bit calculation. |
| 100 | |
| 101 | // Recalculate mod 1/64. |
| 102 | idx = static_cast<unsigned>(fputil::nearest_integer(x: x_sq.hi * 0x1.0p6)); |
| 103 | |
| 104 | // Get x^2 - idx/64 exactly. When FMA is available, double-double |
| 105 | // multiplication will be correct for all rounding modes. Otherwise we use |
| 106 | // DFloat128 directly. |
| 107 | DFloat128 x_f128(x); |
| 108 | |
| 109 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 110 | // u = x^2 - idx/64 |
| 111 | DFloat128 u_hi( |
| 112 | fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, x_sq.hi)); |
| 113 | DFloat128 u = fputil::quick_add(u_hi, DFloat128(x_sq.lo)); |
| 114 | #else |
| 115 | DFloat128 x_sq_f128 = fputil::quick_mul(a: x_f128, b: x_f128); |
| 116 | DFloat128 u = fputil::quick_add( |
| 117 | a: x_sq_f128, b: DFloat128(static_cast<double>(idx) * (-0x1.0p-6))); |
| 118 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 119 | |
| 120 | DFloat128 p_f128 = asin_eval(u, idx); |
| 121 | DFloat128 r = fputil::quick_mul(a: x_f128, b: p_f128); |
| 122 | |
| 123 | return static_cast<double>(r); |
| 124 | #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 125 | } |
| 126 | // |x| >= 0.5 |
| 127 | |
| 128 | double x_abs = xbits.abs().get_val(); |
| 129 | |
| 130 | // Maintaining the sign: |
| 131 | constexpr double SIGN[2] = {1.0, -1.0}; |
| 132 | double x_sign = SIGN[xbits.is_neg()]; |
| 133 | |
| 134 | // |x| >= 1 |
| 135 | if (LIBC_UNLIKELY(x_exp >= FPBits::EXP_BIAS)) { |
| 136 | // x = +-1, asin(x) = +- pi/2 |
| 137 | if (x_abs == 1.0) { |
| 138 | // return +- pi/2 |
| 139 | return fputil::multiply_add(x: x_sign, y: PI_OVER_TWO.hi, |
| 140 | z: x_sign * PI_OVER_TWO.lo); |
| 141 | } |
| 142 | // |x| > 1, return NaN. |
| 143 | if (xbits.is_quiet_nan()) |
| 144 | return x; |
| 145 | |
| 146 | // Set domain error for non-NaN input. |
| 147 | if (!xbits.is_nan()) |
| 148 | fputil::set_errno_if_required(EDOM); |
| 149 | |
| 150 | fputil::raise_except_if_required(FE_INVALID); |
| 151 | return FPBits::quiet_nan().get_val(); |
| 152 | } |
| 153 | |
| 154 | // When |x| >= 0.5, we perform range reduction as follow: |
| 155 | // |
| 156 | // Assume further that 0.5 <= x < 1, and let: |
| 157 | // y = asin(x) |
| 158 | // We will use the double angle formula: |
| 159 | // cos(2y) = 1 - 2 sin^2(y) |
| 160 | // and the complement angle identity: |
| 161 | // x = sin(y) = cos(pi/2 - y) |
| 162 | // = 1 - 2 sin^2 (pi/4 - y/2) |
| 163 | // So: |
| 164 | // sin(pi/4 - y/2) = sqrt( (1 - x)/2 ) |
| 165 | // And hence: |
| 166 | // pi/4 - y/2 = asin( sqrt( (1 - x)/2 ) ) |
| 167 | // Equivalently: |
| 168 | // asin(x) = y = pi/2 - 2 * asin( sqrt( (1 - x)/2 ) ) |
| 169 | // Let u = (1 - x)/2, then: |
| 170 | // asin(x) = pi/2 - 2 * asin( sqrt(u) ) |
| 171 | // Moreover, since 0.5 <= x < 1: |
| 172 | // 0 < u <= 1/4, and 0 < sqrt(u) <= 0.5, |
| 173 | // And hence we can reuse the same polynomial approximation of asin(x) when |
| 174 | // |x| <= 0.5: |
| 175 | // asin(x) ~ pi/2 - 2 * sqrt(u) * P(u), |
| 176 | |
| 177 | // u = (1 - |x|)/2 |
| 178 | double u = fputil::multiply_add(x: x_abs, y: -0.5, z: 0.5); |
| 179 | // v_hi + v_lo ~ sqrt(u). |
| 180 | // Let: |
| 181 | // h = u - v_hi^2 = (sqrt(u) - v_hi) * (sqrt(u) + v_hi) |
| 182 | // Then: |
| 183 | // sqrt(u) = v_hi + h / (sqrt(u) + v_hi) |
| 184 | // ~ v_hi + h / (2 * v_hi) |
| 185 | // So we can use: |
| 186 | // v_lo = h / (2 * v_hi). |
| 187 | // Then, |
| 188 | // asin(x) ~ pi/2 - 2*(v_hi + v_lo) * P(u) |
| 189 | double v_hi = fputil::sqrt<double>(x: u); |
| 190 | |
| 191 | #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 192 | double p = asin_eval(u); |
| 193 | double r = x_sign * fputil::multiply_add(-2.0 * v_hi, p, PI_OVER_TWO.hi); |
| 194 | return r; |
| 195 | #else |
| 196 | |
| 197 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 198 | double h = fputil::multiply_add(v_hi, -v_hi, u); |
| 199 | #else |
| 200 | DoubleDouble v_hi_sq = fputil::exact_mult(a: v_hi, b: v_hi); |
| 201 | double h = (u - v_hi_sq.hi) - v_hi_sq.lo; |
| 202 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 203 | |
| 204 | // Scale v_lo and v_hi by 2 from the formula: |
| 205 | // vh = v_hi * 2 |
| 206 | // vl = 2*v_lo = h / v_hi. |
| 207 | double vh = v_hi * 2.0; |
| 208 | double vl = h / v_hi; |
| 209 | |
| 210 | // Polynomial approximation: |
| 211 | // p ~ asin(sqrt(u))/sqrt(u) |
| 212 | unsigned idx = 0; |
| 213 | double err = vh * 0x1.0p-51; |
| 214 | |
| 215 | DoubleDouble p = asin_eval(u: DoubleDouble{.lo: 0.0, .hi: u}, idx, err); |
| 216 | |
| 217 | // Perform computations in double-double arithmetic: |
| 218 | // asin(x) = pi/2 - (v_hi + v_lo) * (ASIN_COEFFS[idx][0] + p) |
| 219 | DoubleDouble r0 = fputil::quick_mult(a: DoubleDouble{.lo: vl, .hi: vh}, b: p); |
| 220 | DoubleDouble r = fputil::exact_add(a: PI_OVER_TWO.hi, b: -r0.hi); |
| 221 | |
| 222 | double r_lo = PI_OVER_TWO.lo - r0.lo + r.lo; |
| 223 | |
| 224 | // Ziv's accuracy test. |
| 225 | |
| 226 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 227 | double r_upper = fputil::multiply_add( |
| 228 | r.hi, x_sign, fputil::multiply_add(r_lo, x_sign, err)); |
| 229 | double r_lower = fputil::multiply_add( |
| 230 | r.hi, x_sign, fputil::multiply_add(r_lo, x_sign, -err)); |
| 231 | #else |
| 232 | r_lo *= x_sign; |
| 233 | r.hi *= x_sign; |
| 234 | double r_upper = r.hi + (r_lo + err); |
| 235 | double r_lower = r.hi + (r_lo - err); |
| 236 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 237 | |
| 238 | if (LIBC_LIKELY(r_upper == r_lower)) |
| 239 | return r_upper; |
| 240 | |
| 241 | // Ziv's accuracy test failed, we redo the computations in DFloat128. |
| 242 | // Recalculate mod 1/64. |
| 243 | idx = static_cast<unsigned>(fputil::nearest_integer(x: u * 0x1.0p6)); |
| 244 | |
| 245 | // After the first step of Newton-Raphson approximating v = sqrt(u), we have |
| 246 | // that: |
| 247 | // sqrt(u) = v_hi + h / (sqrt(u) + v_hi) |
| 248 | // v_lo = h / (2 * v_hi) |
| 249 | // With error: |
| 250 | // sqrt(u) - (v_hi + v_lo) = h * ( 1/(sqrt(u) + v_hi) - 1/(2*v_hi) ) |
| 251 | // = -h^2 / (2*v * (sqrt(u) + v)^2). |
| 252 | // Since: |
| 253 | // (sqrt(u) + v_hi)^2 ~ (2sqrt(u))^2 = 4u, |
| 254 | // we can add another correction term to (v_hi + v_lo) that is: |
| 255 | // v_ll = -h^2 / (2*v_hi * 4u) |
| 256 | // = -v_lo * (h / 4u) |
| 257 | // = -vl * (h / 8u), |
| 258 | // making the errors: |
| 259 | // sqrt(u) - (v_hi + v_lo + v_ll) = O(h^3) |
| 260 | // well beyond 128-bit precision needed. |
| 261 | |
| 262 | // Get the rounding error of vl = 2 * v_lo ~ h / vh |
| 263 | // Get full product of vh * vl |
| 264 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 265 | double vl_lo = fputil::multiply_add(-v_hi, vl, h) / v_hi; |
| 266 | #else |
| 267 | DoubleDouble vh_vl = fputil::exact_mult(a: v_hi, b: vl); |
| 268 | double vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi; |
| 269 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 270 | // vll = 2*v_ll = -vl * (h / (4u)). |
| 271 | double t = h * (-0.25) / u; |
| 272 | double vll = fputil::multiply_add(x: vl, y: t, z: vl_lo); |
| 273 | // m_v = -(v_hi + v_lo + v_ll). |
| 274 | DFloat128 m_v = fputil::quick_add( |
| 275 | a: DFloat128(vh), b: fputil::quick_add(a: DFloat128(vl), b: DFloat128(vll))); |
| 276 | m_v.sign = Sign::NEG; |
| 277 | |
| 278 | // Perform computations in DFloat128: |
| 279 | // asin(x) = pi/2 - (v_hi + v_lo + vll) * P(u). |
| 280 | DFloat128 y_f128( |
| 281 | fputil::multiply_add(x: static_cast<double>(idx), y: -0x1.0p-6, z: u)); |
| 282 | |
| 283 | DFloat128 p_f128 = asin_eval(u: y_f128, idx); |
| 284 | DFloat128 r0_f128 = fputil::quick_mul(a: m_v, b: p_f128); |
| 285 | DFloat128 r_f128 = fputil::quick_add(a: PI_OVER_TWO_F128, b: r0_f128); |
| 286 | |
| 287 | if (xbits.is_neg()) |
| 288 | r_f128.sign = Sign::NEG; |
| 289 | |
| 290 | return static_cast<double>(r_f128); |
| 291 | #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 292 | } |
| 293 | |
| 294 | } // namespace math |
| 295 | |
| 296 | } // namespace LIBC_NAMESPACE_DECL |
| 297 | |
| 298 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_ASIN_H |
| 299 | |