177e1d9beSKirill Okhotnikov //===-- Single-precision atan function ------------------------------------===// 277e1d9beSKirill Okhotnikov // 377e1d9beSKirill Okhotnikov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 477e1d9beSKirill Okhotnikov // See https://llvm.org/LICENSE.txt for license information. 577e1d9beSKirill Okhotnikov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 677e1d9beSKirill Okhotnikov // 777e1d9beSKirill Okhotnikov //===----------------------------------------------------------------------===// 877e1d9beSKirill Okhotnikov 977e1d9beSKirill Okhotnikov #include "src/math/atanf.h" 10a6296214Slntue #include "inv_trigf_utils.h" 1177e1d9beSKirill Okhotnikov #include "src/__support/FPUtil/FPBits.h" 12a6296214Slntue #include "src/__support/FPUtil/PolyEval.h" 13a6296214Slntue #include "src/__support/FPUtil/except_value_utils.h" 14a6296214Slntue #include "src/__support/FPUtil/multiply_add.h" 15a6296214Slntue #include "src/__support/FPUtil/nearest_integer.h" 16a9824312STue Ly #include "src/__support/FPUtil/rounding_mode.h" 17*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 18737e1cd1SGuillaume Chatelet #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY 1977e1d9beSKirill Okhotnikov 20*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 2177e1d9beSKirill Okhotnikov 2277e1d9beSKirill Okhotnikov LLVM_LIBC_FUNCTION(float, atanf, (float x)) { 2377e1d9beSKirill Okhotnikov using FPBits = typename fputil::FPBits<float>; 24f0d05bb6STue Ly 25a6296214Slntue constexpr double FINAL_SIGN[2] = {1.0, -1.0}; 26a6296214Slntue constexpr double SIGNED_PI_OVER_2[2] = {0x1.921fb54442d18p0, 27a6296214Slntue -0x1.921fb54442d18p0}; 28a6296214Slntue 29a6296214Slntue FPBits x_bits(x); 30a6296214Slntue Sign sign = x_bits.sign(); 31a6296214Slntue x_bits.set_sign(Sign::POS); 32a6296214Slntue uint32_t x_abs = x_bits.uintval(); 33a6296214Slntue 34a6296214Slntue // x is inf or nan, |x| < 2^-4 or |x|= > 16. 35a6296214Slntue if (LIBC_UNLIKELY(x_abs <= 0x3d80'0000U || x_abs >= 0x4180'0000U)) { 36a6296214Slntue double x_d = static_cast<double>(x); 37a6296214Slntue double const_term = 0.0; 38a6296214Slntue if (LIBC_UNLIKELY(x_abs >= 0x4180'0000)) { 39a6296214Slntue // atan(+-Inf) = +-pi/2. 40a1fb5145Slntue if (x_bits.is_inf()) { 41a1fb5145Slntue volatile double sign_pi_over_2 = SIGNED_PI_OVER_2[sign.is_neg()]; 42a1fb5145Slntue return static_cast<float>(sign_pi_over_2); 43a1fb5145Slntue } 44a6296214Slntue if (x_bits.is_nan()) 45f0d05bb6STue Ly return x; 46a6296214Slntue // x >= 16 47a6296214Slntue x_d = -1.0 / x_d; 48a6296214Slntue const_term = SIGNED_PI_OVER_2[sign.is_neg()]; 49a6296214Slntue } 50a6296214Slntue // 0 <= x < 1/16; 51a6296214Slntue if (LIBC_UNLIKELY(x_bits.is_zero())) 5274ec4679SDominic Chen return x; 53a6296214Slntue // x <= 2^-12; 54a6296214Slntue if (LIBC_UNLIKELY(x_abs < 0x3980'0000)) { 55a6296214Slntue #if defined(LIBC_TARGET_CPU_HAS_FMA) 56a6296214Slntue return fputil::multiply_add(x, -0x1.0p-25f, x); 57a6296214Slntue #else 58a6296214Slntue double x_d = static_cast<double>(x); 59a6296214Slntue return static_cast<float>(fputil::multiply_add(x_d, -0x1.0p-25, x_d)); 60a6296214Slntue #endif // LIBC_TARGET_CPU_HAS_FMA 6177e1d9beSKirill Okhotnikov } 62a6296214Slntue // Use Taylor polynomial: 63a6296214Slntue // atan(x) ~ x * (1 - x^2 / 3 + x^4 / 5 - x^6 / 7 + x^8 / 9 - x^10 / 11). 642be72258Slntue constexpr double ATAN_TAYLOR[6] = { 652be72258Slntue 0x1.0000000000000p+0, -0x1.5555555555555p-2, 0x1.999999999999ap-3, 662be72258Slntue -0x1.2492492492492p-3, 0x1.c71c71c71c71cp-4, -0x1.745d1745d1746p-4, 672be72258Slntue }; 68a6296214Slntue double x2 = x_d * x_d; 69a6296214Slntue double x4 = x2 * x2; 702be72258Slntue double c0 = fputil::multiply_add(x2, ATAN_TAYLOR[1], ATAN_TAYLOR[0]); 712be72258Slntue double c1 = fputil::multiply_add(x2, ATAN_TAYLOR[3], ATAN_TAYLOR[2]); 722be72258Slntue double c2 = fputil::multiply_add(x2, ATAN_TAYLOR[5], ATAN_TAYLOR[4]); 73a6296214Slntue double p = fputil::polyeval(x4, c0, c1, c2); 74a6296214Slntue double r = fputil::multiply_add(x_d, p, const_term); 75a6296214Slntue return static_cast<float>(r); 7677e1d9beSKirill Okhotnikov } 7777e1d9beSKirill Okhotnikov 78a6296214Slntue // Range reduction steps: 79a6296214Slntue // 1) atan(x) = sign(x) * atan(|x|) 80a6296214Slntue // 2) If |x| > 1, atan(|x|) = pi/2 - atan(1/|x|) 81a6296214Slntue // 3) For 1/16 < x <= 1, we find k such that: |x - k/16| <= 1/32. 82a6296214Slntue // 4) Then we use polynomial approximation: 83a6296214Slntue // atan(x) ~ atan((k/16) + (x - (k/16)) * Q(x - k/16) 84a6296214Slntue // = P(x - k/16) 85a6296214Slntue double x_d, const_term, final_sign; 86a6296214Slntue int idx; 87a6296214Slntue 88a6296214Slntue if (x_abs > 0x3f80'0000U) { 89a6296214Slntue // |x| > 1, we need to invert x, so we will perform range reduction in 90a6296214Slntue // double precision. 91a6296214Slntue x_d = 1.0 / static_cast<double>(x_bits.get_val()); 92a6296214Slntue double k_d = fputil::nearest_integer(x_d * 0x1.0p4); 93a6296214Slntue x_d = fputil::multiply_add(k_d, -0x1.0p-4, x_d); 94a6296214Slntue idx = static_cast<int>(k_d); 95a6296214Slntue final_sign = FINAL_SIGN[sign.is_pos()]; 96a6296214Slntue // Adjust constant term of the polynomial by +- pi/2. 97a6296214Slntue const_term = fputil::multiply_add(final_sign, ATAN_COEFFS[idx][0], 98a6296214Slntue SIGNED_PI_OVER_2[sign.is_neg()]); 9977e1d9beSKirill Okhotnikov } else { 100a6296214Slntue // Exceptional value: 1012be72258Slntue if (LIBC_UNLIKELY(x_abs == 0x3d8d'6b23U)) { // |x| = 0x1.1ad646p-4 1022be72258Slntue return sign.is_pos() ? fputil::round_result_slightly_down(0x1.1a6386p-4f) 1032be72258Slntue : fputil::round_result_slightly_up(-0x1.1a6386p-4f); 10477e1d9beSKirill Okhotnikov } 105a6296214Slntue // Perform range reduction in single precision. 106a6296214Slntue float x_f = x_bits.get_val(); 107a6296214Slntue float k_f = fputil::nearest_integer(x_f * 0x1.0p4f); 108a6296214Slntue x_f = fputil::multiply_add(k_f, -0x1.0p-4f, x_f); 109a6296214Slntue x_d = static_cast<double>(x_f); 110a6296214Slntue idx = static_cast<int>(k_f); 111a6296214Slntue final_sign = FINAL_SIGN[sign.is_neg()]; 112a6296214Slntue const_term = final_sign * ATAN_COEFFS[idx][0]; 11377e1d9beSKirill Okhotnikov } 11477e1d9beSKirill Okhotnikov 115a6296214Slntue double p = atan_eval(x_d, idx); 116a6296214Slntue double r = fputil::multiply_add(final_sign * x_d, p, const_term); 117a6296214Slntue 118a6296214Slntue return static_cast<float>(r); 11977e1d9beSKirill Okhotnikov } 12077e1d9beSKirill Okhotnikov 121*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 122