| 1 | //===-- Single-precision atan2f function ----------------------------------===// |
| 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 | #include "src/math/atan2f.h" |
| 10 | #include "hdr/fenv_macros.h" |
| 11 | #include "inv_trigf_utils.h" |
| 12 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 13 | #include "src/__support/FPUtil/FPBits.h" |
| 14 | #include "src/__support/FPUtil/PolyEval.h" |
| 15 | #include "src/__support/FPUtil/double_double.h" |
| 16 | #include "src/__support/FPUtil/multiply_add.h" |
| 17 | #include "src/__support/FPUtil/nearest_integer.h" |
| 18 | #include "src/__support/FPUtil/rounding_mode.h" |
| 19 | #include "src/__support/macros/config.h" |
| 20 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 21 | |
| 22 | #if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) && \ |
| 23 | defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT) |
| 24 | |
| 25 | // We use float-float implementation to reduce size. |
| 26 | #include "src/math/generic/atan2f_float.h" |
| 27 | |
| 28 | #else |
| 29 | |
| 30 | namespace LIBC_NAMESPACE_DECL { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 35 | |
| 36 | // Look up tables for accurate pass: |
| 37 | |
| 38 | // atan(i/16) with i = 0..16, generated by Sollya with: |
| 39 | // > for i from 0 to 16 do { |
| 40 | // a = round(atan(i/16), D, RN); |
| 41 | // b = round(atan(i/16) - a, D, RN); |
| 42 | // print("{", b, ",", a, "},"); |
| 43 | // }; |
| 44 | constexpr fputil::DoubleDouble ATAN_I[17] = { |
| 45 | {0.0, 0.0}, |
| 46 | {-0x1.c934d86d23f1dp-60, 0x1.ff55bb72cfdeap-5}, |
| 47 | {-0x1.cd37686760c17p-59, 0x1.fd5ba9aac2f6ep-4}, |
| 48 | {0x1.347b0b4f881cap-58, 0x1.7b97b4bce5b02p-3}, |
| 49 | {0x1.8ab6e3cf7afbdp-57, 0x1.f5b75f92c80ddp-3}, |
| 50 | {-0x1.963a544b672d8p-57, 0x1.362773707ebccp-2}, |
| 51 | {-0x1.c63aae6f6e918p-56, 0x1.6f61941e4def1p-2}, |
| 52 | {-0x1.24dec1b50b7ffp-56, 0x1.a64eec3cc23fdp-2}, |
| 53 | {0x1.a2b7f222f65e2p-56, 0x1.dac670561bb4fp-2}, |
| 54 | {-0x1.d5b495f6349e6p-56, 0x1.0657e94db30dp-1}, |
| 55 | {-0x1.928df287a668fp-58, 0x1.1e00babdefeb4p-1}, |
| 56 | {0x1.1021137c71102p-55, 0x1.345f01cce37bbp-1}, |
| 57 | {0x1.2419a87f2a458p-56, 0x1.4978fa3269ee1p-1}, |
| 58 | {0x1.0028e4bc5e7cap-57, 0x1.5d58987169b18p-1}, |
| 59 | {-0x1.8c34d25aadef6p-56, 0x1.700a7c5784634p-1}, |
| 60 | {-0x1.bf76229d3b917p-56, 0x1.819d0b7158a4dp-1}, |
| 61 | {0x1.1a62633145c07p-55, 0x1.921fb54442d18p-1}, |
| 62 | }; |
| 63 | |
| 64 | // Taylor polynomial, generated by Sollya with: |
| 65 | // > for i from 0 to 8 do { |
| 66 | // j = (-1)^(i + 1)/(2*i + 1); |
| 67 | // a = round(j, D, RN); |
| 68 | // b = round(j - a, D, RN); |
| 69 | // print("{", b, ",", a, "},"); |
| 70 | // }; |
| 71 | constexpr fputil::DoubleDouble COEFFS[9] = { |
| 72 | {0.0, 1.0}, // 1 |
| 73 | {-0x1.5555555555555p-56, -0x1.5555555555555p-2}, // -1/3 |
| 74 | {-0x1.999999999999ap-57, 0x1.999999999999ap-3}, // 1/5 |
| 75 | {-0x1.2492492492492p-57, -0x1.2492492492492p-3}, // -1/7 |
| 76 | {0x1.c71c71c71c71cp-58, 0x1.c71c71c71c71cp-4}, // 1/9 |
| 77 | {0x1.745d1745d1746p-59, -0x1.745d1745d1746p-4}, // -1/11 |
| 78 | {-0x1.3b13b13b13b14p-58, 0x1.3b13b13b13b14p-4}, // 1/13 |
| 79 | {-0x1.1111111111111p-60, -0x1.1111111111111p-4}, // -1/15 |
| 80 | {0x1.e1e1e1e1e1e1ep-61, 0x1.e1e1e1e1e1e1ep-5}, // 1/17 |
| 81 | }; |
| 82 | |
| 83 | // Veltkamp's splitting of a double precision into hi + lo, where the hi part is |
| 84 | // slightly smaller than an even split, so that the product of |
| 85 | // hi * (s1 * k + s2) is exact, |
| 86 | // where: |
| 87 | // s1, s2 are single precsion, |
| 88 | // 1/16 <= s1/s2 <= 1 |
| 89 | // 1/16 <= k <= 1 is an integer. |
| 90 | // So the maximal precision of (s1 * k + s2) is: |
| 91 | // prec(s1 * k + s2) = 2 + log2(msb(s2)) - log2(lsb(k_d * s1)) |
| 92 | // = 2 + log2(msb(s1)) + 4 - log2(lsb(k_d)) - log2(lsb(s1)) |
| 93 | // = 2 + log2(lsb(s1)) + 23 + 4 - (-4) - log2(lsb(s1)) |
| 94 | // = 33. |
| 95 | // Thus, the Veltkamp splitting constant is C = 2^33 + 1. |
| 96 | // This is used when FMA instruction is not available. |
| 97 | [[maybe_unused]] constexpr fputil::DoubleDouble split_d(double a) { |
| 98 | fputil::DoubleDouble r{0.0, 0.0}; |
| 99 | constexpr double C = 0x1.0p33 + 1.0; |
| 100 | double t1 = C * a; |
| 101 | double t2 = a - t1; |
| 102 | r.hi = t1 + t2; |
| 103 | r.lo = a - r.hi; |
| 104 | return r; |
| 105 | } |
| 106 | |
| 107 | // Compute atan( num_d / den_d ) in double-double precision. |
| 108 | // num_d = min(|x|, |y|) |
| 109 | // den_d = max(|x|, |y|) |
| 110 | // q_d = num_d / den_d |
| 111 | // idx, k_d = round( 2^4 * num_d / den_d ) |
| 112 | // final_sign = sign of the final result |
| 113 | // const_term = the constant term in the final expression. |
| 114 | float atan2f_double_double(double num_d, double den_d, double q_d, int idx, |
| 115 | double k_d, double final_sign, |
| 116 | const fputil::DoubleDouble &const_term) { |
| 117 | fputil::DoubleDouble q; |
| 118 | double num_r, den_r; |
| 119 | |
| 120 | if (idx != 0) { |
| 121 | // The following range reduction is accurate even without fma for |
| 122 | // 1/16 <= n/d <= 1. |
| 123 | // atan(n/d) - atan(idx/16) = atan((n/d - idx/16) / (1 + (n/d) * (idx/16))) |
| 124 | // = atan((n - d*(idx/16)) / (d + n*idx/16)) |
| 125 | k_d *= 0x1.0p-4; |
| 126 | num_r = fputil::multiply_add(k_d, -den_d, num_d); // Exact |
| 127 | den_r = fputil::multiply_add(k_d, num_d, den_d); // Exact |
| 128 | q.hi = num_r / den_r; |
| 129 | } else { |
| 130 | // For 0 < n/d < 1/16, we just need to calculate the lower part of their |
| 131 | // quotient. |
| 132 | q.hi = q_d; |
| 133 | num_r = num_d; |
| 134 | den_r = den_d; |
| 135 | } |
| 136 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 137 | q.lo = fputil::multiply_add(q.hi, -den_r, num_r) / den_r; |
| 138 | #else |
| 139 | // Compute `(num_r - q.hi * den_r) / den_r` accurately without FMA |
| 140 | // instructions. |
| 141 | fputil::DoubleDouble q_hi_dd = split_d(q.hi); |
| 142 | double t1 = fputil::multiply_add(q_hi_dd.hi, -den_r, num_r); // Exact |
| 143 | double t2 = fputil::multiply_add(q_hi_dd.lo, -den_r, t1); |
| 144 | q.lo = t2 / den_r; |
| 145 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| 146 | |
| 147 | // Taylor polynomial, evaluating using Horner's scheme: |
| 148 | // P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9 - x^11/11 + x^13/13 - x^15/15 |
| 149 | // + x^17/17 |
| 150 | // = x*(1 + x^2*(-1/3 + x^2*(1/5 + x^2*(-1/7 + x^2*(1/9 + x^2* |
| 151 | // *(-1/11 + x^2*(1/13 + x^2*(-1/15 + x^2 * 1/17)))))))) |
| 152 | fputil::DoubleDouble q2 = fputil::quick_mult(q, q); |
| 153 | fputil::DoubleDouble p_dd = |
| 154 | fputil::polyeval(q2, COEFFS[0], COEFFS[1], COEFFS[2], COEFFS[3], |
| 155 | COEFFS[4], COEFFS[5], COEFFS[6], COEFFS[7], COEFFS[8]); |
| 156 | fputil::DoubleDouble r_dd = |
| 157 | fputil::add(const_term, fputil::multiply_add(q, p_dd, ATAN_I[idx])); |
| 158 | r_dd.hi *= final_sign; |
| 159 | r_dd.lo *= final_sign; |
| 160 | |
| 161 | // Make sure the sum is normalized: |
| 162 | fputil::DoubleDouble rr = fputil::exact_add(r_dd.hi, r_dd.lo); |
| 163 | // Round to odd. |
| 164 | uint64_t rr_bits = cpp::bit_cast<uint64_t>(rr.hi); |
| 165 | if (LIBC_UNLIKELY(((rr_bits & 0xfff'ffff) == 0) && (rr.lo != 0.0))) { |
| 166 | Sign hi_sign = fputil::FPBits<double>(rr.hi).sign(); |
| 167 | Sign lo_sign = fputil::FPBits<double>(rr.lo).sign(); |
| 168 | if (hi_sign == lo_sign) { |
| 169 | ++rr_bits; |
| 170 | } else if ((rr_bits & fputil::FPBits<double>::FRACTION_MASK) > 0) { |
| 171 | --rr_bits; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return static_cast<float>(cpp::bit_cast<double>(rr_bits)); |
| 176 | } |
| 177 | |
| 178 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 179 | |
| 180 | } // anonymous namespace |
| 181 | |
| 182 | // There are several range reduction steps we can take for atan2(y, x) as |
| 183 | // follow: |
| 184 | |
| 185 | // * Range reduction 1: signness |
| 186 | // atan2(y, x) will return a number between -PI and PI representing the angle |
| 187 | // forming by the 0x axis and the vector (x, y) on the 0xy-plane. |
| 188 | // In particular, we have that: |
| 189 | // atan2(y, x) = atan( y/x ) if x >= 0 and y >= 0 (I-quadrant) |
| 190 | // = pi + atan( y/x ) if x < 0 and y >= 0 (II-quadrant) |
| 191 | // = -pi + atan( y/x ) if x < 0 and y < 0 (III-quadrant) |
| 192 | // = atan( y/x ) if x >= 0 and y < 0 (IV-quadrant) |
| 193 | // Since atan function is odd, we can use the formula: |
| 194 | // atan(-u) = -atan(u) |
| 195 | // to adjust the above conditions a bit further: |
| 196 | // atan2(y, x) = atan( |y|/|x| ) if x >= 0 and y >= 0 (I-quadrant) |
| 197 | // = pi - atan( |y|/|x| ) if x < 0 and y >= 0 (II-quadrant) |
| 198 | // = -pi + atan( |y|/|x| ) if x < 0 and y < 0 (III-quadrant) |
| 199 | // = -atan( |y|/|x| ) if x >= 0 and y < 0 (IV-quadrant) |
| 200 | // Which can be simplified to: |
| 201 | // atan2(y, x) = sign(y) * atan( |y|/|x| ) if x >= 0 |
| 202 | // = sign(y) * (pi - atan( |y|/|x| )) if x < 0 |
| 203 | |
| 204 | // * Range reduction 2: reciprocal |
| 205 | // Now that the argument inside atan is positive, we can use the formula: |
| 206 | // atan(1/x) = pi/2 - atan(x) |
| 207 | // to make the argument inside atan <= 1 as follow: |
| 208 | // atan2(y, x) = sign(y) * atan( |y|/|x|) if 0 <= |y| <= x |
| 209 | // = sign(y) * (pi/2 - atan( |x|/|y| ) if 0 <= x < |y| |
| 210 | // = sign(y) * (pi - atan( |y|/|x| )) if 0 <= |y| <= -x |
| 211 | // = sign(y) * (pi/2 + atan( |x|/|y| )) if 0 <= -x < |y| |
| 212 | |
| 213 | // * Range reduction 3: look up table. |
| 214 | // After the previous two range reduction steps, we reduce the problem to |
| 215 | // compute atan(u) with 0 <= u <= 1, or to be precise: |
| 216 | // atan( n / d ) where n = min(|x|, |y|) and d = max(|x|, |y|). |
| 217 | // An accurate polynomial approximation for the whole [0, 1] input range will |
| 218 | // require a very large degree. To make it more efficient, we reduce the input |
| 219 | // range further by finding an integer idx such that: |
| 220 | // | n/d - idx/16 | <= 1/32. |
| 221 | // In particular, |
| 222 | // idx := 2^-4 * round(2^4 * n/d) |
| 223 | // Then for the fast pass, we find a polynomial approximation for: |
| 224 | // atan( n/d ) ~ atan( idx/16 ) + (n/d - idx/16) * Q(n/d - idx/16) |
| 225 | // For the accurate pass, we use the addition formula: |
| 226 | // atan( n/d ) - atan( idx/16 ) = atan( (n/d - idx/16)/(1 + (n*idx)/(16*d)) ) |
| 227 | // = atan( (n - d * idx/16)/(d + n * idx/16) ) |
| 228 | // And finally we use Taylor polynomial to compute the RHS in the accurate pass: |
| 229 | // atan(u) ~ P(u) = u - u^3/3 + u^5/5 - u^7/7 + u^9/9 - u^11/11 + u^13/13 - |
| 230 | // - u^15/15 + u^17/17 |
| 231 | // It's error in double-double precision is estimated in Sollya to be: |
| 232 | // > P = x - x^3/3 + x^5/5 -x^7/7 + x^9/9 - x^11/11 + x^13/13 - x^15/15 |
| 233 | // + x^17/17; |
| 234 | // > dirtyinfnorm(atan(x) - P, [-2^-5, 2^-5]); |
| 235 | // 0x1.aec6f...p-100 |
| 236 | // which is about rounding errors of double-double (2^-104). |
| 237 | |
| 238 | LLVM_LIBC_FUNCTION(float, atan2f, (float y, float x)) { |
| 239 | using FPBits = typename fputil::FPBits<float>; |
| 240 | constexpr double IS_NEG[2] = {1.0, -1.0}; |
| 241 | constexpr double PI = 0x1.921fb54442d18p1; |
| 242 | constexpr double PI_LO = 0x1.1a62633145c07p-53; |
| 243 | constexpr double PI_OVER_4 = 0x1.921fb54442d18p-1; |
| 244 | constexpr double PI_OVER_2 = 0x1.921fb54442d18p0; |
| 245 | constexpr double THREE_PI_OVER_4 = 0x1.2d97c7f3321d2p+1; |
| 246 | // Adjustment for constant term: |
| 247 | // CONST_ADJ[x_sign][y_sign][recip] |
| 248 | constexpr fputil::DoubleDouble CONST_ADJ[2][2][2] = { |
| 249 | {{{0.0, 0.0}, {-PI_LO / 2, -PI_OVER_2}}, |
| 250 | {{-0.0, -0.0}, {-PI_LO / 2, -PI_OVER_2}}}, |
| 251 | {{{-PI_LO, -PI}, {PI_LO / 2, PI_OVER_2}}, |
| 252 | {{-PI_LO, -PI}, {PI_LO / 2, PI_OVER_2}}}}; |
| 253 | |
| 254 | FPBits x_bits(x), y_bits(y); |
| 255 | bool x_sign = x_bits.sign().is_neg(); |
| 256 | bool y_sign = y_bits.sign().is_neg(); |
| 257 | x_bits.set_sign(Sign::POS); |
| 258 | y_bits.set_sign(Sign::POS); |
| 259 | uint32_t x_abs = x_bits.uintval(); |
| 260 | uint32_t y_abs = y_bits.uintval(); |
| 261 | uint32_t max_abs = x_abs > y_abs ? x_abs : y_abs; |
| 262 | uint32_t min_abs = x_abs <= y_abs ? x_abs : y_abs; |
| 263 | float num_f = FPBits(min_abs).get_val(); |
| 264 | float den_f = FPBits(max_abs).get_val(); |
| 265 | double num_d = static_cast<double>(num_f); |
| 266 | double den_d = static_cast<double>(den_f); |
| 267 | |
| 268 | if (LIBC_UNLIKELY(max_abs >= 0x7f80'0000U || num_d == 0.0)) { |
| 269 | if (x_bits.is_nan() || y_bits.is_nan()) { |
| 270 | if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan()) |
| 271 | fputil::raise_except_if_required(FE_INVALID); |
| 272 | return FPBits::quiet_nan().get_val(); |
| 273 | } |
| 274 | double x_d = static_cast<double>(x); |
| 275 | double y_d = static_cast<double>(y); |
| 276 | size_t x_except = (x_d == 0.0) ? 0 : (x_abs == 0x7f80'0000 ? 2 : 1); |
| 277 | size_t y_except = (y_d == 0.0) ? 0 : (y_abs == 0x7f80'0000 ? 2 : 1); |
| 278 | |
| 279 | // Exceptional cases: |
| 280 | // EXCEPT[y_except][x_except][x_is_neg] |
| 281 | // with x_except & y_except: |
| 282 | // 0: zero |
| 283 | // 1: finite, non-zero |
| 284 | // 2: infinity |
| 285 | constexpr double EXCEPTS[3][3][2] = { |
| 286 | {{0.0, PI}, {0.0, PI}, {0.0, PI}}, |
| 287 | {{PI_OVER_2, PI_OVER_2}, {0.0, 0.0}, {0.0, PI}}, |
| 288 | {{PI_OVER_2, PI_OVER_2}, |
| 289 | {PI_OVER_2, PI_OVER_2}, |
| 290 | {PI_OVER_4, THREE_PI_OVER_4}}, |
| 291 | }; |
| 292 | |
| 293 | double r = IS_NEG[y_sign] * EXCEPTS[y_except][x_except][x_sign]; |
| 294 | |
| 295 | return static_cast<float>(r); |
| 296 | } |
| 297 | |
| 298 | bool recip = x_abs < y_abs; |
| 299 | double final_sign = IS_NEG[(x_sign != y_sign) != recip]; |
| 300 | fputil::DoubleDouble const_term = CONST_ADJ[x_sign][y_sign][recip]; |
| 301 | double q_d = num_d / den_d; |
| 302 | |
| 303 | double k_d = fputil::nearest_integer(q_d * 0x1.0p4); |
| 304 | int idx = static_cast<int>(k_d); |
| 305 | double r; |
| 306 | |
| 307 | #ifdef LIBC_MATH_HAS_SMALL_TABLES |
| 308 | double p = atan_eval_no_table(num_d, den_d, k_d * 0x1.0p-4); |
| 309 | r = final_sign * (p + (const_term.hi + ATAN_K_OVER_16[idx])); |
| 310 | #else |
| 311 | q_d = fputil::multiply_add(k_d, -0x1.0p-4, q_d); |
| 312 | |
| 313 | double p = atan_eval(q_d, idx); |
| 314 | r = final_sign * |
| 315 | fputil::multiply_add(q_d, p, const_term.hi + ATAN_COEFFS[idx][0]); |
| 316 | #endif // LIBC_MATH_HAS_SMALL_TABLES |
| 317 | |
| 318 | #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 319 | return static_cast<float>(r); |
| 320 | #else |
| 321 | constexpr uint32_t LOWER_ERR = 4; |
| 322 | // Mask sticky bits in double precision before rounding to single precision. |
| 323 | constexpr uint32_t MASK = |
| 324 | mask_trailing_ones<uint32_t, fputil::FPBits<double>::SIG_LEN - |
| 325 | FPBits::SIG_LEN - 1>(); |
| 326 | constexpr uint32_t UPPER_ERR = MASK - LOWER_ERR; |
| 327 | |
| 328 | uint32_t r_bits = static_cast<uint32_t>(cpp::bit_cast<uint64_t>(r)) & MASK; |
| 329 | |
| 330 | // Ziv's rounding test. |
| 331 | if (LIBC_LIKELY(r_bits > LOWER_ERR && r_bits < UPPER_ERR)) |
| 332 | return static_cast<float>(r); |
| 333 | |
| 334 | return atan2f_double_double(num_d, den_d, q_d, idx, k_d, final_sign, |
| 335 | const_term); |
| 336 | #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 337 | } |
| 338 | |
| 339 | } // namespace LIBC_NAMESPACE_DECL |
| 340 | |
| 341 | #endif |
| 342 | |