1ca8b14deSlntue //===-- Double-precision atan2 function -----------------------------------===// 2ca8b14deSlntue // 3ca8b14deSlntue // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4ca8b14deSlntue // See https://llvm.org/LICENSE.txt for license information. 5ca8b14deSlntue // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6ca8b14deSlntue // 7ca8b14deSlntue //===----------------------------------------------------------------------===// 8ca8b14deSlntue 9ca8b14deSlntue #include "src/math/atan2.h" 10ca8b14deSlntue #include "inv_trigf_utils.h" 11ca8b14deSlntue #include "src/__support/FPUtil/FPBits.h" 12ca8b14deSlntue #include "src/__support/FPUtil/PolyEval.h" 13ca8b14deSlntue #include "src/__support/FPUtil/double_double.h" 14ca8b14deSlntue #include "src/__support/FPUtil/multiply_add.h" 15ca8b14deSlntue #include "src/__support/FPUtil/nearest_integer.h" 16ca8b14deSlntue #include "src/__support/FPUtil/rounding_mode.h" 17ca8b14deSlntue #include "src/__support/macros/config.h" 18ca8b14deSlntue #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY 19ca8b14deSlntue 20ca8b14deSlntue namespace LIBC_NAMESPACE_DECL { 21ca8b14deSlntue 22ca8b14deSlntue namespace { 23ca8b14deSlntue 24ca8b14deSlntue using DoubleDouble = fputil::DoubleDouble; 25ca8b14deSlntue 26ca8b14deSlntue // atan(i/64) with i = 0..64, generated by Sollya with: 27ca8b14deSlntue // > for i from 0 to 64 do { 28ca8b14deSlntue // a = round(atan(i/64), D, RN); 29ca8b14deSlntue // b = round(atan(i/64) - a, D, RN); 30ca8b14deSlntue // print("{", b, ",", a, "},"); 31ca8b14deSlntue // }; 32ca8b14deSlntue constexpr fputil::DoubleDouble ATAN_I[65] = { 33ca8b14deSlntue {0.0, 0.0}, 34ca8b14deSlntue {-0x1.220c39d4dff5p-61, 0x1.fff555bbb729bp-7}, 35ca8b14deSlntue {-0x1.5ec431444912cp-60, 0x1.ffd55bba97625p-6}, 36ca8b14deSlntue {-0x1.86ef8f794f105p-63, 0x1.7fb818430da2ap-5}, 37ca8b14deSlntue {-0x1.c934d86d23f1dp-60, 0x1.ff55bb72cfdeap-5}, 38ca8b14deSlntue {0x1.ac4ce285df847p-58, 0x1.3f59f0e7c559dp-4}, 39ca8b14deSlntue {-0x1.cfb654c0c3d98p-58, 0x1.7ee182602f10fp-4}, 40ca8b14deSlntue {0x1.f7b8f29a05987p-58, 0x1.be39ebe6f07c3p-4}, 41ca8b14deSlntue {-0x1.cd37686760c17p-59, 0x1.fd5ba9aac2f6ep-4}, 42ca8b14deSlntue {-0x1.b485914dacf8cp-59, 0x1.1e1fafb043727p-3}, 43ca8b14deSlntue {0x1.61a3b0ce9281bp-57, 0x1.3d6eee8c6626cp-3}, 44ca8b14deSlntue {-0x1.054ab2c010f3dp-58, 0x1.5c9811e3ec26ap-3}, 45ca8b14deSlntue {0x1.347b0b4f881cap-58, 0x1.7b97b4bce5b02p-3}, 46ca8b14deSlntue {0x1.cf601e7b4348ep-59, 0x1.9a6a8e96c8626p-3}, 47ca8b14deSlntue {0x1.17b10d2e0e5abp-61, 0x1.b90d7529260a2p-3}, 48ca8b14deSlntue {0x1.c648d1534597ep-57, 0x1.d77d5df205736p-3}, 49ca8b14deSlntue {0x1.8ab6e3cf7afbdp-57, 0x1.f5b75f92c80ddp-3}, 50ca8b14deSlntue {0x1.62e47390cb865p-56, 0x1.09dc597d86362p-2}, 51ca8b14deSlntue {0x1.30ca4748b1bf9p-57, 0x1.18bf5a30bf178p-2}, 52ca8b14deSlntue {-0x1.077cdd36dfc81p-56, 0x1.278372057ef46p-2}, 53ca8b14deSlntue {-0x1.963a544b672d8p-57, 0x1.362773707ebccp-2}, 54ca8b14deSlntue {-0x1.5d5e43c55b3bap-56, 0x1.44aa436c2af0ap-2}, 55ca8b14deSlntue {-0x1.2566480884082p-57, 0x1.530ad9951cd4ap-2}, 56ca8b14deSlntue {-0x1.a725715711fp-56, 0x1.614840309cfe2p-2}, 57ca8b14deSlntue {-0x1.c63aae6f6e918p-56, 0x1.6f61941e4def1p-2}, 58ca8b14deSlntue {0x1.69c885c2b249ap-56, 0x1.7d5604b63b3f7p-2}, 59ca8b14deSlntue {0x1.b6d0ba3748fa8p-56, 0x1.8b24d394a1b25p-2}, 60ca8b14deSlntue {0x1.9e6c988fd0a77p-56, 0x1.98cd5454d6b18p-2}, 61ca8b14deSlntue {-0x1.24dec1b50b7ffp-56, 0x1.a64eec3cc23fdp-2}, 62ca8b14deSlntue {0x1.ae187b1ca504p-56, 0x1.b3a911da65c6cp-2}, 63ca8b14deSlntue {-0x1.cc1ce70934c34p-56, 0x1.c0db4c94ec9fp-2}, 64ca8b14deSlntue {-0x1.a2cfa4418f1adp-56, 0x1.cde53432c1351p-2}, 65ca8b14deSlntue {0x1.a2b7f222f65e2p-56, 0x1.dac670561bb4fp-2}, 66ca8b14deSlntue {0x1.0e53dc1bf3435p-56, 0x1.e77eb7f175a34p-2}, 67ca8b14deSlntue {-0x1.a3992dc382a23p-57, 0x1.f40dd0b541418p-2}, 68ca8b14deSlntue {-0x1.b32c949c9d593p-55, 0x1.0039c73c1a40cp-1}, 69ca8b14deSlntue {-0x1.d5b495f6349e6p-56, 0x1.0657e94db30dp-1}, 70ca8b14deSlntue {0x1.974fa13b5404fp-58, 0x1.0c6145b5b43dap-1}, 71ca8b14deSlntue {-0x1.2bdaee1c0ee35p-58, 0x1.1255d9bfbd2a9p-1}, 72ca8b14deSlntue {0x1.c621cec00c301p-55, 0x1.1835a88be7c13p-1}, 73ca8b14deSlntue {-0x1.928df287a668fp-58, 0x1.1e00babdefeb4p-1}, 74ca8b14deSlntue {0x1.c421c9f38224ep-57, 0x1.23b71e2cc9e6ap-1}, 75ca8b14deSlntue {-0x1.09e73b0c6c087p-56, 0x1.2958e59308e31p-1}, 76ca8b14deSlntue {0x1.c5d5e9ff0cf8dp-55, 0x1.2ee628406cbcap-1}, 77ca8b14deSlntue {0x1.1021137c71102p-55, 0x1.345f01cce37bbp-1}, 78ca8b14deSlntue {-0x1.2304331d8bf46p-55, 0x1.39c391cd4171ap-1}, 79ca8b14deSlntue {0x1.ecf8b492644fp-56, 0x1.3f13fb89e96f4p-1}, 80ca8b14deSlntue {-0x1.f76d0163f79c8p-56, 0x1.445065b795b56p-1}, 81ca8b14deSlntue {0x1.2419a87f2a458p-56, 0x1.4978fa3269ee1p-1}, 82ca8b14deSlntue {0x1.4a33dbeb3796cp-55, 0x1.4e8de5bb6ec04p-1}, 83ca8b14deSlntue {-0x1.1bb74abda520cp-55, 0x1.538f57b89061fp-1}, 84ca8b14deSlntue {-0x1.5e5c9d8c5a95p-56, 0x1.587d81f732fbbp-1}, 85ca8b14deSlntue {0x1.0028e4bc5e7cap-57, 0x1.5d58987169b18p-1}, 86ca8b14deSlntue {-0x1.2b785350ee8c1p-57, 0x1.6220d115d7b8ep-1}, 87ca8b14deSlntue {-0x1.6ea6febe8bbbap-56, 0x1.66d663923e087p-1}, 88ca8b14deSlntue {-0x1.a80386188c50ep-55, 0x1.6b798920b3d99p-1}, 89ca8b14deSlntue {-0x1.8c34d25aadef6p-56, 0x1.700a7c5784634p-1}, 90ca8b14deSlntue {0x1.7b2a6165884a1p-59, 0x1.748978fba8e0fp-1}, 91ca8b14deSlntue {0x1.406a08980374p-55, 0x1.78f6bbd5d315ep-1}, 92ca8b14deSlntue {0x1.560821e2f3aa9p-55, 0x1.7d528289fa093p-1}, 93ca8b14deSlntue {-0x1.bf76229d3b917p-56, 0x1.819d0b7158a4dp-1}, 94ca8b14deSlntue {0x1.6b66e7fc8b8c3p-57, 0x1.85d69576cc2c5p-1}, 95ca8b14deSlntue {-0x1.55b9a5e177a1bp-55, 0x1.89ff5ff57f1f8p-1}, 96ca8b14deSlntue {-0x1.ec182ab042f61p-56, 0x1.8e17aa99cc05ep-1}, 97ca8b14deSlntue {0x1.1a62633145c07p-55, 0x1.921fb54442d18p-1}, 98ca8b14deSlntue }; 99ca8b14deSlntue 100ca8b14deSlntue // Approximate atan(x) for |x| <= 2^-7. 101ca8b14deSlntue // Using degree-9 Taylor polynomial: 102ca8b14deSlntue // P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9; 103ca8b14deSlntue // Then the absolute error is bounded by: 104ca8b14deSlntue // |atan(x) - P(x)| < |x|^11/11 < 2^(-7*11) / 11 < 2^-80. 105ca8b14deSlntue // And the relative error is bounded by: 106ca8b14deSlntue // |(atan(x) - P(x))/atan(x)| < |x|^10 / 10 < 2^-73. 107ca8b14deSlntue // For x = x_hi + x_lo, fully expand the polynomial and drop any terms less than 108ca8b14deSlntue // ulp(x_hi^3 / 3) gives us: 109ca8b14deSlntue // P(x) ~ x_hi - x_hi^3/3 + x_hi^5/5 - x_hi^7/7 + x_hi^9/9 + 110ca8b14deSlntue // + x_lo * (1 - x_hi^2 + x_hi^4) 111ca8b14deSlntue DoubleDouble atan_eval(const DoubleDouble &x) { 112ca8b14deSlntue DoubleDouble p; 113ca8b14deSlntue p.hi = x.hi; 114ca8b14deSlntue double x_hi_sq = x.hi * x.hi; 115ca8b14deSlntue // c0 ~ x_hi^2 * 1/5 - 1/3 116ca8b14deSlntue double c0 = fputil::multiply_add(x_hi_sq, 0x1.999999999999ap-3, 117ca8b14deSlntue -0x1.5555555555555p-2); 118ca8b14deSlntue // c1 ~ x_hi^2 * 1/9 - 1/7 119ca8b14deSlntue double c1 = fputil::multiply_add(x_hi_sq, 0x1.c71c71c71c71cp-4, 120ca8b14deSlntue -0x1.2492492492492p-3); 121ca8b14deSlntue // x_hi^3 122ca8b14deSlntue double x_hi_3 = x_hi_sq * x.hi; 123ca8b14deSlntue // x_hi^4 124ca8b14deSlntue double x_hi_4 = x_hi_sq * x_hi_sq; 125ca8b14deSlntue // d0 ~ 1/3 - x_hi^2 / 5 + x_hi^4 / 7 - x_hi^6 / 9 126ca8b14deSlntue double d0 = fputil::multiply_add(x_hi_4, c1, c0); 127ca8b14deSlntue // x_lo - x_lo * x_hi^2 + x_lo * x_hi^4 128ca8b14deSlntue double d1 = fputil::multiply_add(x_hi_4 - x_hi_sq, x.lo, x.lo); 129ca8b14deSlntue // p.lo ~ -x_hi^3/3 + x_hi^5/5 - x_hi^7/7 + x_hi^9/9 + 130ca8b14deSlntue // + x_lo * (1 - x_hi^2 + x_hi^4) 131ca8b14deSlntue p.lo = fputil::multiply_add(x_hi_3, d0, d1); 132ca8b14deSlntue return p; 133ca8b14deSlntue } 134ca8b14deSlntue 135ca8b14deSlntue } // anonymous namespace 136ca8b14deSlntue 137ca8b14deSlntue // There are several range reduction steps we can take for atan2(y, x) as 138ca8b14deSlntue // follow: 139ca8b14deSlntue 140ca8b14deSlntue // * Range reduction 1: signness 141ca8b14deSlntue // atan2(y, x) will return a number between -PI and PI representing the angle 142ca8b14deSlntue // forming by the 0x axis and the vector (x, y) on the 0xy-plane. 143ca8b14deSlntue // In particular, we have that: 144ca8b14deSlntue // atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant) 145ca8b14deSlntue // = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant) 146ca8b14deSlntue // = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant) 147ca8b14deSlntue // = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant) 148ca8b14deSlntue // Since atan function is odd, we can use the formula: 149ca8b14deSlntue // atan(-u) = -atan(u) 150ca8b14deSlntue // to adjust the above conditions a bit further: 151ca8b14deSlntue // atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant) 152ca8b14deSlntue // = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant) 153ca8b14deSlntue // = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant) 154ca8b14deSlntue // = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant) 155ca8b14deSlntue // Which can be simplified to: 156ca8b14deSlntue // atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0 157ca8b14deSlntue // = sign(y) * (pi - atan( |y|/|x| )) if x < 0 158ca8b14deSlntue 159ca8b14deSlntue // * Range reduction 2: reciprocal 160ca8b14deSlntue // Now that the argument inside atan is positive, we can use the formula: 161ca8b14deSlntue // atan(1/x) = pi/2 - atan(x) 162ca8b14deSlntue // to make the argument inside atan <= 1 as follow: 163ca8b14deSlntue // atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x 164ca8b14deSlntue // = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y| 165ca8b14deSlntue // = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x 166ca8b14deSlntue // = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y| 167ca8b14deSlntue 168ca8b14deSlntue // * Range reduction 3: look up table. 169ca8b14deSlntue // After the previous two range reduction steps, we reduce the problem to 170ca8b14deSlntue // compute atan(u) with 0 <= u <= 1, or to be precise: 171ca8b14deSlntue // atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|). 172ca8b14deSlntue // An accurate polynomial approximation for the whole [0, 1] input range will 173ca8b14deSlntue // require a very large degree. To make it more efficient, we reduce the input 174ca8b14deSlntue // range further by finding an integer idx such that: 175ca8b14deSlntue // | n/d - idx/64 | <= 1/128. 176ca8b14deSlntue // In particular, 177ca8b14deSlntue // idx := round(2^6 * n/d) 178ca8b14deSlntue // Then for the fast pass, we find a polynomial approximation for: 179ca8b14deSlntue // atan( n/d ) ~ atan( idx/64 ) + (n/d - idx/64) * Q(n/d - idx/64) 180ca8b14deSlntue // For the accurate pass, we use the addition formula: 181ca8b14deSlntue // atan( n/d ) - atan( idx/64 ) = atan( (n/d - idx/64)/(1 + (n*idx)/(64*d)) ) 182ca8b14deSlntue // = atan( (n - d*(idx/64))/(d + n*(idx/64)) ) 183ca8b14deSlntue // And for the fast pass, we use degree-9 Taylor polynomial to compute the RHS: 184ca8b14deSlntue // atan(u) ~ P(u) = u - u^3/3 + u^5/5 - u^7/7 + u^9/9 185ca8b14deSlntue // with absolute errors bounded by: 186ca8b14deSlntue // |atan(u) - P(u)| < |u|^11 / 11 < 2^-80 187ca8b14deSlntue // and relative errors bounded by: 188ca8b14deSlntue // |(atan(u) - P(u)) / P(u)| < u^10 / 11 < 2^-73. 189ca8b14deSlntue 190ca8b14deSlntue LLVM_LIBC_FUNCTION(double, atan2, (double y, double x)) { 191ca8b14deSlntue using FPBits = fputil::FPBits<double>; 192ca8b14deSlntue 193ca8b14deSlntue constexpr double IS_NEG[2] = {1.0, -1.0}; 194ca8b14deSlntue constexpr DoubleDouble ZERO = {0.0, 0.0}; 195ca8b14deSlntue constexpr DoubleDouble MZERO = {-0.0, -0.0}; 196ca8b14deSlntue constexpr DoubleDouble PI = {0x1.1a62633145c07p-53, 0x1.921fb54442d18p+1}; 197ca8b14deSlntue constexpr DoubleDouble MPI = {-0x1.1a62633145c07p-53, -0x1.921fb54442d18p+1}; 198ca8b14deSlntue constexpr DoubleDouble PI_OVER_2 = {0x1.1a62633145c07p-54, 199ca8b14deSlntue 0x1.921fb54442d18p0}; 200ca8b14deSlntue constexpr DoubleDouble MPI_OVER_2 = {-0x1.1a62633145c07p-54, 201ca8b14deSlntue -0x1.921fb54442d18p0}; 202ca8b14deSlntue constexpr DoubleDouble PI_OVER_4 = {0x1.1a62633145c07p-55, 203ca8b14deSlntue 0x1.921fb54442d18p-1}; 204ca8b14deSlntue constexpr DoubleDouble THREE_PI_OVER_4 = {0x1.a79394c9e8a0ap-54, 205ca8b14deSlntue 0x1.2d97c7f3321d2p+1}; 206ca8b14deSlntue // Adjustment for constant term: 207ca8b14deSlntue // CONST_ADJ[x_sign][y_sign][recip] 208ca8b14deSlntue constexpr DoubleDouble CONST_ADJ[2][2][2] = { 209ca8b14deSlntue {{ZERO, MPI_OVER_2}, {MZERO, MPI_OVER_2}}, 210ca8b14deSlntue {{MPI, PI_OVER_2}, {MPI, PI_OVER_2}}}; 211ca8b14deSlntue 212ca8b14deSlntue FPBits x_bits(x), y_bits(y); 213ca8b14deSlntue bool x_sign = x_bits.sign().is_neg(); 214ca8b14deSlntue bool y_sign = y_bits.sign().is_neg(); 215ca8b14deSlntue x_bits = x_bits.abs(); 216ca8b14deSlntue y_bits = y_bits.abs(); 217ca8b14deSlntue uint64_t x_abs = x_bits.uintval(); 218ca8b14deSlntue uint64_t y_abs = y_bits.uintval(); 219ca8b14deSlntue bool recip = x_abs < y_abs; 220ca8b14deSlntue uint64_t min_abs = recip ? x_abs : y_abs; 221ca8b14deSlntue uint64_t max_abs = !recip ? x_abs : y_abs; 222ca8b14deSlntue unsigned min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN); 223ca8b14deSlntue unsigned max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN); 224ca8b14deSlntue 225ca8b14deSlntue double num = FPBits(min_abs).get_val(); 226ca8b14deSlntue double den = FPBits(max_abs).get_val(); 227ca8b14deSlntue 228ca8b14deSlntue // Check for exceptional cases, whether inputs are 0, inf, nan, or close to 229ca8b14deSlntue // overflow, or close to underflow. 230ca8b14deSlntue if (LIBC_UNLIKELY(max_exp > 0x7ffU - 128U || min_exp < 128U)) { 231ca8b14deSlntue if (x_bits.is_nan() || y_bits.is_nan()) 232ca8b14deSlntue return FPBits::quiet_nan().get_val(); 233*0f4b3c40Slntue unsigned x_except = x == 0.0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1); 234*0f4b3c40Slntue unsigned y_except = y == 0.0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1); 235ca8b14deSlntue 236ca8b14deSlntue // Exceptional cases: 237ca8b14deSlntue // EXCEPT[y_except][x_except][x_is_neg] 238ca8b14deSlntue // with x_except & y_except: 239ca8b14deSlntue // 0: zero 240ca8b14deSlntue // 1: finite, non-zero 241ca8b14deSlntue // 2: infinity 242ca8b14deSlntue constexpr DoubleDouble EXCEPTS[3][3][2] = { 243ca8b14deSlntue {{ZERO, PI}, {ZERO, PI}, {ZERO, PI}}, 244ca8b14deSlntue {{PI_OVER_2, PI_OVER_2}, {ZERO, ZERO}, {ZERO, PI}}, 245ca8b14deSlntue {{PI_OVER_2, PI_OVER_2}, 246ca8b14deSlntue {PI_OVER_2, PI_OVER_2}, 247ca8b14deSlntue {PI_OVER_4, THREE_PI_OVER_4}}, 248ca8b14deSlntue }; 249ca8b14deSlntue 250ca8b14deSlntue if ((x_except != 1) || (y_except != 1)) { 251ca8b14deSlntue DoubleDouble r = EXCEPTS[y_except][x_except][x_sign]; 252ca8b14deSlntue return fputil::multiply_add(IS_NEG[y_sign], r.hi, IS_NEG[y_sign] * r.lo); 253ca8b14deSlntue } 254ca8b14deSlntue bool scale_up = min_exp < 128U; 255ca8b14deSlntue bool scale_down = max_exp > 0x7ffU - 128U; 256ca8b14deSlntue // At least one input is denormal, multiply both numerator and denominator 257ca8b14deSlntue // by some large enough power of 2 to normalize denormal inputs. 258ca8b14deSlntue if (scale_up) { 259ca8b14deSlntue num *= 0x1.0p64; 260ca8b14deSlntue if (!scale_down) 261ca8b14deSlntue den *= 0x1.0p64; 262ca8b14deSlntue } else if (scale_down) { 263ca8b14deSlntue den *= 0x1.0p-64; 264ca8b14deSlntue if (!scale_up) 265ca8b14deSlntue num *= 0x1.0p-64; 266ca8b14deSlntue } 267ca8b14deSlntue 268ca8b14deSlntue min_abs = FPBits(num).uintval(); 269ca8b14deSlntue max_abs = FPBits(den).uintval(); 270ca8b14deSlntue min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN); 271ca8b14deSlntue max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN); 272ca8b14deSlntue } 273ca8b14deSlntue 274ca8b14deSlntue double final_sign = IS_NEG[(x_sign != y_sign) != recip]; 275ca8b14deSlntue DoubleDouble const_term = CONST_ADJ[x_sign][y_sign][recip]; 276ca8b14deSlntue unsigned exp_diff = max_exp - min_exp; 277ca8b14deSlntue // We have the following bound for normalized n and d: 278ca8b14deSlntue // 2^(-exp_diff - 1) < n/d < 2^(-exp_diff + 1). 279ca8b14deSlntue if (LIBC_UNLIKELY(exp_diff > 54)) { 280ca8b14deSlntue return fputil::multiply_add(final_sign, const_term.hi, 281ca8b14deSlntue final_sign * (const_term.lo + num / den)); 282ca8b14deSlntue } 283ca8b14deSlntue 284ca8b14deSlntue double k = fputil::nearest_integer(64.0 * num / den); 285ca8b14deSlntue unsigned idx = static_cast<unsigned>(k); 286ca8b14deSlntue // k = idx / 64 287ca8b14deSlntue k *= 0x1.0p-6; 288ca8b14deSlntue 289ca8b14deSlntue // Range reduction: 290ca8b14deSlntue // atan(n/d) - atan(k/64) = atan((n/d - k/64) / (1 + (n/d) * (k/64))) 291ca8b14deSlntue // = atan((n - d * k/64)) / (d + n * k/64)) 292ca8b14deSlntue DoubleDouble num_k = fputil::exact_mult(num, k); 293ca8b14deSlntue DoubleDouble den_k = fputil::exact_mult(den, k); 294ca8b14deSlntue 295ca8b14deSlntue // num_dd = n - d * k 296ca8b14deSlntue DoubleDouble num_dd = fputil::exact_add(num - den_k.hi, -den_k.lo); 297ca8b14deSlntue // den_dd = d + n * k 298ca8b14deSlntue DoubleDouble den_dd = fputil::exact_add(den, num_k.hi); 299ca8b14deSlntue den_dd.lo += num_k.lo; 300ca8b14deSlntue 301ca8b14deSlntue // q = (n - d * k) / (d + n * k) 302ca8b14deSlntue DoubleDouble q = fputil::div(num_dd, den_dd); 303ca8b14deSlntue // p ~ atan(q) 304ca8b14deSlntue DoubleDouble p = atan_eval(q); 305ca8b14deSlntue 306ca8b14deSlntue DoubleDouble r = fputil::add(const_term, fputil::add(ATAN_I[idx], p)); 307ca8b14deSlntue r.hi *= final_sign; 308ca8b14deSlntue r.lo *= final_sign; 309ca8b14deSlntue 310ca8b14deSlntue return r.hi + r.lo; 311ca8b14deSlntue } 312ca8b14deSlntue 313ca8b14deSlntue } // namespace LIBC_NAMESPACE_DECL 314