12be72258Slntue //===-- Single-precision atan2f function ----------------------------------===// 22be72258Slntue // 32be72258Slntue // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42be72258Slntue // See https://llvm.org/LICENSE.txt for license information. 52be72258Slntue // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 62be72258Slntue // 72be72258Slntue //===----------------------------------------------------------------------===// 82be72258Slntue 92be72258Slntue #include "src/math/atan2f.h" 102be72258Slntue #include "inv_trigf_utils.h" 112be72258Slntue #include "src/__support/FPUtil/FPBits.h" 122be72258Slntue #include "src/__support/FPUtil/PolyEval.h" 132be72258Slntue #include "src/__support/FPUtil/double_double.h" 142be72258Slntue #include "src/__support/FPUtil/multiply_add.h" 152be72258Slntue #include "src/__support/FPUtil/nearest_integer.h" 162be72258Slntue #include "src/__support/FPUtil/rounding_mode.h" 175ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 182be72258Slntue #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY 192be72258Slntue 205ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 212be72258Slntue 222be72258Slntue namespace { 232be72258Slntue 24*a7da7023Slntue #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS 25*a7da7023Slntue 262be72258Slntue // Look up tables for accurate pass: 272be72258Slntue 282be72258Slntue // atan(i/16) with i = 0..16, generated by Sollya with: 292be72258Slntue // > for i from 0 to 16 do { 302be72258Slntue // a = round(atan(i/16), D, RN); 312be72258Slntue // b = round(atan(i/16) - a, D, RN); 322be72258Slntue // print("{", b, ",", a, "},"); 332be72258Slntue // }; 342be72258Slntue constexpr fputil::DoubleDouble ATAN_I[17] = { 352be72258Slntue {0.0, 0.0}, 362be72258Slntue {-0x1.c934d86d23f1dp-60, 0x1.ff55bb72cfdeap-5}, 372be72258Slntue {-0x1.cd37686760c17p-59, 0x1.fd5ba9aac2f6ep-4}, 382be72258Slntue {0x1.347b0b4f881cap-58, 0x1.7b97b4bce5b02p-3}, 392be72258Slntue {0x1.8ab6e3cf7afbdp-57, 0x1.f5b75f92c80ddp-3}, 402be72258Slntue {-0x1.963a544b672d8p-57, 0x1.362773707ebccp-2}, 412be72258Slntue {-0x1.c63aae6f6e918p-56, 0x1.6f61941e4def1p-2}, 422be72258Slntue {-0x1.24dec1b50b7ffp-56, 0x1.a64eec3cc23fdp-2}, 432be72258Slntue {0x1.a2b7f222f65e2p-56, 0x1.dac670561bb4fp-2}, 442be72258Slntue {-0x1.d5b495f6349e6p-56, 0x1.0657e94db30dp-1}, 452be72258Slntue {-0x1.928df287a668fp-58, 0x1.1e00babdefeb4p-1}, 462be72258Slntue {0x1.1021137c71102p-55, 0x1.345f01cce37bbp-1}, 472be72258Slntue {0x1.2419a87f2a458p-56, 0x1.4978fa3269ee1p-1}, 482be72258Slntue {0x1.0028e4bc5e7cap-57, 0x1.5d58987169b18p-1}, 492be72258Slntue {-0x1.8c34d25aadef6p-56, 0x1.700a7c5784634p-1}, 502be72258Slntue {-0x1.bf76229d3b917p-56, 0x1.819d0b7158a4dp-1}, 512be72258Slntue {0x1.1a62633145c07p-55, 0x1.921fb54442d18p-1}, 522be72258Slntue }; 532be72258Slntue 542be72258Slntue // Taylor polynomial, generated by Sollya with: 552be72258Slntue // > for i from 0 to 8 do { 562be72258Slntue // j = (-1)^(i + 1)/(2*i + 1); 572be72258Slntue // a = round(j, D, RN); 582be72258Slntue // b = round(j - a, D, RN); 592be72258Slntue // print("{", b, ",", a, "},"); 602be72258Slntue // }; 612be72258Slntue constexpr fputil::DoubleDouble COEFFS[9] = { 622be72258Slntue {0.0, 1.0}, // 1 632be72258Slntue {-0x1.5555555555555p-56, -0x1.5555555555555p-2}, // -1/3 642be72258Slntue {-0x1.999999999999ap-57, 0x1.999999999999ap-3}, // 1/5 652be72258Slntue {-0x1.2492492492492p-57, -0x1.2492492492492p-3}, // -1/7 662be72258Slntue {0x1.c71c71c71c71cp-58, 0x1.c71c71c71c71cp-4}, // 1/9 672be72258Slntue {0x1.745d1745d1746p-59, -0x1.745d1745d1746p-4}, // -1/11 682be72258Slntue {-0x1.3b13b13b13b14p-58, 0x1.3b13b13b13b14p-4}, // 1/13 692be72258Slntue {-0x1.1111111111111p-60, -0x1.1111111111111p-4}, // -1/15 702be72258Slntue {0x1.e1e1e1e1e1e1ep-61, 0x1.e1e1e1e1e1e1ep-5}, // 1/17 712be72258Slntue }; 722be72258Slntue 732be72258Slntue // Veltkamp's splitting of a double precision into hi + lo, where the hi part is 742be72258Slntue // slightly smaller than an even split, so that the product of 752be72258Slntue // hi * (s1 * k + s2) is exact, 762be72258Slntue // where: 772be72258Slntue // s1, s2 are single precsion, 782be72258Slntue // 1/16 <= s1/s2 <= 1 792be72258Slntue // 1/16 <= k <= 1 is an integer. 802be72258Slntue // So the maximal precision of (s1 * k + s2) is: 812be72258Slntue // prec(s1 * k + s2) = 2 + log2(msb(s2)) - log2(lsb(k_d * s1)) 822be72258Slntue // = 2 + log2(msb(s1)) + 4 - log2(lsb(k_d)) - log2(lsb(s1)) 832be72258Slntue // = 2 + log2(lsb(s1)) + 23 + 4 - (-4) - log2(lsb(s1)) 842be72258Slntue // = 33. 852be72258Slntue // Thus, the Veltkamp splitting constant is C = 2^33 + 1. 862be72258Slntue // This is used when FMA instruction is not available. 872be72258Slntue [[maybe_unused]] constexpr fputil::DoubleDouble split_d(double a) { 882be72258Slntue fputil::DoubleDouble r{0.0, 0.0}; 892be72258Slntue constexpr double C = 0x1.0p33 + 1.0; 902be72258Slntue double t1 = C * a; 912be72258Slntue double t2 = a - t1; 922be72258Slntue r.hi = t1 + t2; 932be72258Slntue r.lo = a - r.hi; 942be72258Slntue return r; 952be72258Slntue } 962be72258Slntue 972be72258Slntue // Compute atan( num_d / den_d ) in double-double precision. 982be72258Slntue // num_d = min(|x|, |y|) 992be72258Slntue // den_d = max(|x|, |y|) 1002be72258Slntue // q_d = num_d / den_d 1012be72258Slntue // idx, k_d = round( 2^4 * num_d / den_d ) 1022be72258Slntue // final_sign = sign of the final result 1032be72258Slntue // const_term = the constant term in the final expression. 1042be72258Slntue float atan2f_double_double(double num_d, double den_d, double q_d, int idx, 1052be72258Slntue double k_d, double final_sign, 1062be72258Slntue const fputil::DoubleDouble &const_term) { 1072be72258Slntue fputil::DoubleDouble q; 1082be72258Slntue double num_r, den_r; 1092be72258Slntue 1102be72258Slntue if (idx != 0) { 1112be72258Slntue // The following range reduction is accurate even without fma for 1122be72258Slntue // 1/16 <= n/d <= 1. 1132be72258Slntue // atan(n/d) - atan(idx/16) = atan((n/d - idx/16) / (1 + (n/d) * (idx/16))) 1142be72258Slntue // = atan((n - d*(idx/16)) / (d + n*idx/16)) 1152be72258Slntue k_d *= 0x1.0p-4; 1162be72258Slntue num_r = fputil::multiply_add(k_d, -den_d, num_d); // Exact 1172be72258Slntue den_r = fputil::multiply_add(k_d, num_d, den_d); // Exact 1182be72258Slntue q.hi = num_r / den_r; 1192be72258Slntue } else { 1202be72258Slntue // For 0 < n/d < 1/16, we just need to calculate the lower part of their 1212be72258Slntue // quotient. 1222be72258Slntue q.hi = q_d; 1232be72258Slntue num_r = num_d; 1242be72258Slntue den_r = den_d; 1252be72258Slntue } 1262be72258Slntue #ifdef LIBC_TARGET_CPU_HAS_FMA 1272be72258Slntue q.lo = fputil::multiply_add(q.hi, -den_r, num_r) / den_r; 1282be72258Slntue #else 1292be72258Slntue // Compute `(num_r - q.hi * den_r) / den_r` accurately without FMA 1302be72258Slntue // instructions. 1312be72258Slntue fputil::DoubleDouble q_hi_dd = split_d(q.hi); 1322be72258Slntue double t1 = fputil::multiply_add(q_hi_dd.hi, -den_r, num_r); // Exact 1332be72258Slntue double t2 = fputil::multiply_add(q_hi_dd.lo, -den_r, t1); 1342be72258Slntue q.lo = t2 / den_r; 1352be72258Slntue #endif // LIBC_TARGET_CPU_HAS_FMA 1362be72258Slntue 1372be72258Slntue // Taylor polynomial, evaluating using Horner's scheme: 1382be72258Slntue // P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9 - x^11/11 + x^13/13 - x^15/15 1392be72258Slntue // + x^17/17 1402be72258Slntue // = x*(1 + x^2*(-1/3 + x^2*(1/5 + x^2*(-1/7 + x^2*(1/9 + x^2* 1412be72258Slntue // *(-1/11 + x^2*(1/13 + x^2*(-1/15 + x^2 * 1/17)))))))) 1422be72258Slntue fputil::DoubleDouble q2 = fputil::quick_mult(q, q); 1432be72258Slntue fputil::DoubleDouble p_dd = 1442be72258Slntue fputil::polyeval(q2, COEFFS[0], COEFFS[1], COEFFS[2], COEFFS[3], 1452be72258Slntue COEFFS[4], COEFFS[5], COEFFS[6], COEFFS[7], COEFFS[8]); 1462be72258Slntue fputil::DoubleDouble r_dd = 1472be72258Slntue fputil::add(const_term, fputil::multiply_add(q, p_dd, ATAN_I[idx])); 1482be72258Slntue r_dd.hi *= final_sign; 1492be72258Slntue r_dd.lo *= final_sign; 1502be72258Slntue 1512be72258Slntue // Make sure the sum is normalized: 1522be72258Slntue fputil::DoubleDouble rr = fputil::exact_add(r_dd.hi, r_dd.lo); 1532be72258Slntue // Round to odd. 1542be72258Slntue uint64_t rr_bits = cpp::bit_cast<uint64_t>(rr.hi); 1552be72258Slntue if (LIBC_UNLIKELY(((rr_bits & 0xfff'ffff) == 0) && (rr.lo != 0.0))) { 1562be72258Slntue Sign hi_sign = fputil::FPBits<double>(rr.hi).sign(); 1572be72258Slntue Sign lo_sign = fputil::FPBits<double>(rr.lo).sign(); 1582be72258Slntue if (hi_sign == lo_sign) { 1592be72258Slntue ++rr_bits; 1602be72258Slntue } else if ((rr_bits & fputil::FPBits<double>::FRACTION_MASK) > 0) { 1612be72258Slntue --rr_bits; 1622be72258Slntue } 1632be72258Slntue } 1642be72258Slntue 1652be72258Slntue return static_cast<float>(cpp::bit_cast<double>(rr_bits)); 1662be72258Slntue } 1672be72258Slntue 168*a7da7023Slntue #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS 169*a7da7023Slntue 1702be72258Slntue } // anonymous namespace 1712be72258Slntue 1722be72258Slntue // There are several range reduction steps we can take for atan2(y, x) as 1732be72258Slntue // follow: 1742be72258Slntue 1752be72258Slntue // * Range reduction 1: signness 1762be72258Slntue // atan2(y, x) will return a number between -PI and PI representing the angle 1772be72258Slntue // forming by the 0x axis and the vector (x, y) on the 0xy-plane. 1782be72258Slntue // In particular, we have that: 1792be72258Slntue // atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant) 1802be72258Slntue // = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant) 1812be72258Slntue // = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant) 1822be72258Slntue // = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant) 1832be72258Slntue // Since atan function is odd, we can use the formula: 1842be72258Slntue // atan(-u) = -atan(u) 1852be72258Slntue // to adjust the above conditions a bit further: 1862be72258Slntue // atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant) 1872be72258Slntue // = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant) 1882be72258Slntue // = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant) 1892be72258Slntue // = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant) 1902be72258Slntue // Which can be simplified to: 1912be72258Slntue // atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0 1922be72258Slntue // = sign(y) * (pi - atan( |y|/|x| )) if x < 0 1932be72258Slntue 1942be72258Slntue // * Range reduction 2: reciprocal 1952be72258Slntue // Now that the argument inside atan is positive, we can use the formula: 1962be72258Slntue // atan(1/x) = pi/2 - atan(x) 1972be72258Slntue // to make the argument inside atan <= 1 as follow: 1982be72258Slntue // atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x 1992be72258Slntue // = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y| 2002be72258Slntue // = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x 2012be72258Slntue // = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y| 2022be72258Slntue 2032be72258Slntue // * Range reduction 3: look up table. 2042be72258Slntue // After the previous two range reduction steps, we reduce the problem to 2052be72258Slntue // compute atan(u) with 0 <= u <= 1, or to be precise: 2062be72258Slntue // atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|). 2072be72258Slntue // An accurate polynomial approximation for the whole [0, 1] input range will 2082be72258Slntue // require a very large degree. To make it more efficient, we reduce the input 2092be72258Slntue // range further by finding an integer idx such that: 2102be72258Slntue // | n/d - idx/16 | <= 1/32. 2112be72258Slntue // In particular, 2122be72258Slntue // idx := 2^-4 * round(2^4 * n/d) 2132be72258Slntue // Then for the fast pass, we find a polynomial approximation for: 2142be72258Slntue // atan( n/d ) ~ atan( idx/16 ) + (n/d - idx/16) * Q(n/d - idx/16) 2152be72258Slntue // For the accurate pass, we use the addition formula: 2162be72258Slntue // atan( n/d ) - atan( idx/16 ) = atan( (n/d - idx/16)/(1 + (n*idx)/(16*d)) ) 2172be72258Slntue // = atan( (n - d * idx/16)/(d + n * idx/16) ) 2182be72258Slntue // And finally we use Taylor polynomial to compute the RHS in the accurate pass: 2192be72258Slntue // atan(u) ~ P(u) = u - u^3/3 + u^5/5 - u^7/7 + u^9/9 - u^11/11 + u^13/13 - 2202be72258Slntue // - u^15/15 + u^17/17 2212be72258Slntue // It's error in double-double precision is estimated in Sollya to be: 2222be72258Slntue // > P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9 - x^11/11 + x^13/13 - x^15/15 2232be72258Slntue // + x^17/17; 2242be72258Slntue // > dirtyinfnorm(atan(x) - P, [-2^-5, 2^-5]); 2252be72258Slntue // 0x1.aec6f...p-100 2262be72258Slntue // which is about rounding errors of double-double (2^-104). 2272be72258Slntue 2282be72258Slntue LLVM_LIBC_FUNCTION(float, atan2f, (float y, float x)) { 2292be72258Slntue using FPBits = typename fputil::FPBits<float>; 2302be72258Slntue constexpr double IS_NEG[2] = {1.0, -1.0}; 2312be72258Slntue constexpr double PI = 0x1.921fb54442d18p1; 2322be72258Slntue constexpr double PI_LO = 0x1.1a62633145c07p-53; 2332be72258Slntue constexpr double PI_OVER_4 = 0x1.921fb54442d18p-1; 2342be72258Slntue constexpr double PI_OVER_2 = 0x1.921fb54442d18p0; 2352be72258Slntue constexpr double THREE_PI_OVER_4 = 0x1.2d97c7f3321d2p+1; 2362be72258Slntue // Adjustment for constant term: 2372be72258Slntue // CONST_ADJ[x_sign][y_sign][recip] 2382be72258Slntue constexpr fputil::DoubleDouble CONST_ADJ[2][2][2] = { 2392be72258Slntue {{{0.0, 0.0}, {-PI_LO / 2, -PI_OVER_2}}, 2402be72258Slntue {{-0.0, -0.0}, {-PI_LO / 2, -PI_OVER_2}}}, 2412be72258Slntue {{{-PI_LO, -PI}, {PI_LO / 2, PI_OVER_2}}, 2422be72258Slntue {{-PI_LO, -PI}, {PI_LO / 2, PI_OVER_2}}}}; 2432be72258Slntue 2442be72258Slntue FPBits x_bits(x), y_bits(y); 2452be72258Slntue bool x_sign = x_bits.sign().is_neg(); 2462be72258Slntue bool y_sign = y_bits.sign().is_neg(); 2472be72258Slntue x_bits.set_sign(Sign::POS); 2482be72258Slntue y_bits.set_sign(Sign::POS); 2492be72258Slntue uint32_t x_abs = x_bits.uintval(); 2502be72258Slntue uint32_t y_abs = y_bits.uintval(); 2512be72258Slntue uint32_t max_abs = x_abs > y_abs ? x_abs : y_abs; 2522be72258Slntue uint32_t min_abs = x_abs <= y_abs ? x_abs : y_abs; 253952dafb0Slntue float num_f = FPBits(min_abs).get_val(); 254952dafb0Slntue float den_f = FPBits(max_abs).get_val(); 255952dafb0Slntue double num_d = static_cast<double>(num_f); 256952dafb0Slntue double den_d = static_cast<double>(den_f); 2572be72258Slntue 258952dafb0Slntue if (LIBC_UNLIKELY(max_abs >= 0x7f80'0000U || num_d == 0.0)) { 2592be72258Slntue if (x_bits.is_nan() || y_bits.is_nan()) 2602be72258Slntue return FPBits::quiet_nan().get_val(); 261952dafb0Slntue double x_d = static_cast<double>(x); 262952dafb0Slntue double y_d = static_cast<double>(y); 263952dafb0Slntue size_t x_except = (x_d == 0.0) ? 0 : (x_abs == 0x7f80'0000 ? 2 : 1); 264952dafb0Slntue size_t y_except = (y_d == 0.0) ? 0 : (y_abs == 0x7f80'0000 ? 2 : 1); 2652be72258Slntue 2662be72258Slntue // Exceptional cases: 2672be72258Slntue // EXCEPT[y_except][x_except][x_is_neg] 2682be72258Slntue // with x_except & y_except: 2692be72258Slntue // 0: zero 2702be72258Slntue // 1: finite, non-zero 2712be72258Slntue // 2: infinity 2722be72258Slntue constexpr double EXCEPTS[3][3][2] = { 2732be72258Slntue {{0.0, PI}, {0.0, PI}, {0.0, PI}}, 2742be72258Slntue {{PI_OVER_2, PI_OVER_2}, {0.0, 0.0}, {0.0, PI}}, 2752be72258Slntue {{PI_OVER_2, PI_OVER_2}, 2762be72258Slntue {PI_OVER_2, PI_OVER_2}, 2772be72258Slntue {PI_OVER_4, THREE_PI_OVER_4}}, 2782be72258Slntue }; 2792be72258Slntue 2802be72258Slntue double r = IS_NEG[y_sign] * EXCEPTS[y_except][x_except][x_sign]; 2812be72258Slntue 2822be72258Slntue return static_cast<float>(r); 2832be72258Slntue } 2842be72258Slntue 2852be72258Slntue bool recip = x_abs < y_abs; 2862be72258Slntue double final_sign = IS_NEG[(x_sign != y_sign) != recip]; 2872be72258Slntue fputil::DoubleDouble const_term = CONST_ADJ[x_sign][y_sign][recip]; 2882be72258Slntue double q_d = num_d / den_d; 2892be72258Slntue 290*a7da7023Slntue double k_d = fputil::nearest_integer(q_d * 0x1.0p4); 2912be72258Slntue int idx = static_cast<int>(k_d); 292*a7da7023Slntue double r; 293*a7da7023Slntue 294*a7da7023Slntue #ifdef LIBC_MATH_HAS_SMALL_TABLES 295*a7da7023Slntue double p = atan_eval_no_table(num_d, den_d, k_d * 0x1.0p-4); 296*a7da7023Slntue r = final_sign * (p + (const_term.hi + ATAN_K_OVER_16[idx])); 297*a7da7023Slntue #else 2982be72258Slntue q_d = fputil::multiply_add(k_d, -0x1.0p-4, q_d); 2992be72258Slntue 3002be72258Slntue double p = atan_eval(q_d, idx); 301*a7da7023Slntue r = final_sign * 3022be72258Slntue fputil::multiply_add(q_d, p, const_term.hi + ATAN_COEFFS[idx][0]); 303*a7da7023Slntue #endif // LIBC_MATH_HAS_SMALL_TABLES 3042be72258Slntue 305*a7da7023Slntue #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS 306*a7da7023Slntue return static_cast<float>(r); 307*a7da7023Slntue #else 3082be72258Slntue constexpr uint32_t LOWER_ERR = 4; 3092be72258Slntue // Mask sticky bits in double precision before rounding to single precision. 3102be72258Slntue constexpr uint32_t MASK = 3112be72258Slntue mask_trailing_ones<uint32_t, fputil::FPBits<double>::SIG_LEN - 3122be72258Slntue FPBits::SIG_LEN - 1>(); 3132be72258Slntue constexpr uint32_t UPPER_ERR = MASK - LOWER_ERR; 3142be72258Slntue 3152be72258Slntue uint32_t r_bits = static_cast<uint32_t>(cpp::bit_cast<uint64_t>(r)) & MASK; 3162be72258Slntue 3172be72258Slntue // Ziv's rounding test. 3182be72258Slntue if (LIBC_LIKELY(r_bits > LOWER_ERR && r_bits < UPPER_ERR)) 3192be72258Slntue return static_cast<float>(r); 3202be72258Slntue 3212be72258Slntue return atan2f_double_double(num_d, den_d, q_d, idx, k_d, final_sign, 3222be72258Slntue const_term); 323*a7da7023Slntue #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS 3242be72258Slntue } 3252be72258Slntue 3265ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 327