| |
| |
| |
| |
| |
| |
| |
|
|
| #include "src/math/atan2.h" |
| #include "atan_utils.h" |
| #include "src/__support/FPUtil/FEnvImpl.h" |
| #include "src/__support/FPUtil/FPBits.h" |
| #include "src/__support/FPUtil/double_double.h" |
| #include "src/__support/FPUtil/multiply_add.h" |
| #include "src/__support/FPUtil/nearest_integer.h" |
| #include "src/__support/macros/config.h" |
| #include "src/__support/macros/optimization.h" |
|
|
| namespace LIBC_NAMESPACE_DECL { |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| LLVM_LIBC_FUNCTION(double, atan2, (double y, double x)) { |
| using FPBits = fputil::FPBits<double>; |
|
|
| constexpr double IS_NEG[2] = {1.0, -1.0}; |
| constexpr DoubleDouble ZERO = {0.0, 0.0}; |
| constexpr DoubleDouble MZERO = {-0.0, -0.0}; |
| constexpr DoubleDouble PI = {0x1.1a62633145c07p-53, 0x1.921fb54442d18p+1}; |
| constexpr DoubleDouble MPI = {-0x1.1a62633145c07p-53, -0x1.921fb54442d18p+1}; |
| constexpr DoubleDouble PI_OVER_2 = {0x1.1a62633145c07p-54, |
| 0x1.921fb54442d18p0}; |
| constexpr DoubleDouble MPI_OVER_2 = {-0x1.1a62633145c07p-54, |
| -0x1.921fb54442d18p0}; |
| constexpr DoubleDouble PI_OVER_4 = {0x1.1a62633145c07p-55, |
| 0x1.921fb54442d18p-1}; |
| constexpr DoubleDouble THREE_PI_OVER_4 = {0x1.a79394c9e8a0ap-54, |
| 0x1.2d97c7f3321d2p+1}; |
| |
| |
| constexpr DoubleDouble CONST_ADJ[2][2][2] = { |
| {{ZERO, MPI_OVER_2}, {MZERO, MPI_OVER_2}}, |
| {{MPI, PI_OVER_2}, {MPI, PI_OVER_2}}}; |
|
|
| FPBits x_bits(x), y_bits(y); |
| bool x_sign = x_bits.sign().is_neg(); |
| bool y_sign = y_bits.sign().is_neg(); |
| x_bits = x_bits.abs(); |
| y_bits = y_bits.abs(); |
| uint64_t x_abs = x_bits.uintval(); |
| uint64_t y_abs = y_bits.uintval(); |
| bool recip = x_abs < y_abs; |
| uint64_t min_abs = recip ? x_abs : y_abs; |
| uint64_t max_abs = !recip ? x_abs : y_abs; |
| unsigned min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN); |
| unsigned max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN); |
|
|
| double num = FPBits(min_abs).get_val(); |
| double den = FPBits(max_abs).get_val(); |
|
|
| |
| |
| if (LIBC_UNLIKELY(max_exp > 0x7ffU - 128U || min_exp < 128U)) { |
| if (x_bits.is_nan() || y_bits.is_nan()) { |
| if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan()) |
| fputil::raise_except_if_required(FE_INVALID); |
| return FPBits::quiet_nan().get_val(); |
| } |
| unsigned x_except = x == 0.0 ? 0 : (FPBits(x_abs).is_inf() ? 2 : 1); |
| unsigned y_except = y == 0.0 ? 0 : (FPBits(y_abs).is_inf() ? 2 : 1); |
|
|
| |
| |
| |
| |
| |
| |
| constexpr DoubleDouble EXCEPTS[3][3][2] = { |
| {{ZERO, PI}, {ZERO, PI}, {ZERO, PI}}, |
| {{PI_OVER_2, PI_OVER_2}, {ZERO, ZERO}, {ZERO, PI}}, |
| {{PI_OVER_2, PI_OVER_2}, |
| {PI_OVER_2, PI_OVER_2}, |
| {PI_OVER_4, THREE_PI_OVER_4}}, |
| }; |
|
|
| if ((x_except != 1) || (y_except != 1)) { |
| DoubleDouble r = EXCEPTS[y_except][x_except][x_sign]; |
| return fputil::multiply_add(IS_NEG[y_sign], r.hi, IS_NEG[y_sign] * r.lo); |
| } |
| bool scale_up = min_exp < 128U; |
| bool scale_down = max_exp > 0x7ffU - 128U; |
| |
| |
| if (scale_up) { |
| num *= 0x1.0p64; |
| if (!scale_down) |
| den *= 0x1.0p64; |
| } else if (scale_down) { |
| den *= 0x1.0p-64; |
| if (!scale_up) |
| num *= 0x1.0p-64; |
| } |
|
|
| min_abs = FPBits(num).uintval(); |
| max_abs = FPBits(den).uintval(); |
| min_exp = static_cast<unsigned>(min_abs >> FPBits::FRACTION_LEN); |
| max_exp = static_cast<unsigned>(max_abs >> FPBits::FRACTION_LEN); |
| } |
|
|
| double final_sign = IS_NEG[(x_sign != y_sign) != recip]; |
| DoubleDouble const_term = CONST_ADJ[x_sign][y_sign][recip]; |
| unsigned exp_diff = max_exp - min_exp; |
| |
| |
| if (LIBC_UNLIKELY(exp_diff > 54)) { |
| return fputil::multiply_add(final_sign, const_term.hi, |
| final_sign * (const_term.lo + num / den)); |
| } |
|
|
| double k = fputil::nearest_integer(64.0 * num / den); |
| unsigned idx = static_cast<unsigned>(k); |
| |
| k *= 0x1.0p-6; |
|
|
| |
| |
| |
| DoubleDouble num_k = fputil::exact_mult(num, k); |
| DoubleDouble den_k = fputil::exact_mult(den, k); |
|
|
| |
| DoubleDouble num_dd = fputil::exact_add(num - den_k.hi, -den_k.lo); |
| |
| DoubleDouble den_dd = fputil::exact_add(den, num_k.hi); |
| den_dd.lo += num_k.lo; |
|
|
| |
| DoubleDouble q = fputil::div(num_dd, den_dd); |
| |
| DoubleDouble p = atan_eval(q); |
|
|
| DoubleDouble r = fputil::add(const_term, fputil::add(ATAN_I[idx], p)); |
| r.hi *= final_sign; |
| r.lo *= final_sign; |
|
|
| return r.hi + r.lo; |
| } |
|
|
| } |
|
|