| 1 | //===-- Single-precision tanpi 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 | #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF_H |
| 11 | |
| 12 | #include "sincosf_utils.h" |
| 13 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | #include "src/__support/FPUtil/FPBits.h" |
| 15 | #include "src/__support/FPUtil/cast.h" |
| 16 | #include "src/__support/FPUtil/except_value_utils.h" |
| 17 | #include "src/__support/FPUtil/is_integer.h" |
| 18 | #include "src/__support/FPUtil/multiply_add.h" |
| 19 | #include "src/__support/common.h" |
| 20 | #include "src/__support/macros/config.h" |
| 21 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 22 | |
| 23 | namespace LIBC_NAMESPACE_DECL { |
| 24 | |
| 25 | namespace math { |
| 26 | |
| 27 | LIBC_INLINE float tanpif(float x) { |
| 28 | using namespace sincosf_utils_internal; |
| 29 | |
| 30 | using FPBits = typename fputil::FPBits<float>; |
| 31 | FPBits xbits(x); |
| 32 | |
| 33 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 34 | constexpr size_t N_EXCEPTS = 3; |
| 35 | constexpr fputil::ExceptValues<float, N_EXCEPTS> TANPIF_EXCEPTS{.values: { |
| 36 | // (input, RZ output, RU offset, RD offset, RN offset) |
| 37 | {.input: 0x38F26685, .rnd_towardzero_result: 0x39BE6182, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, |
| 38 | {.input: 0x3E933802, .rnd_towardzero_result: 0x3FA267DD, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, |
| 39 | {.input: 0x3F3663FF, .rnd_towardzero_result: 0xBFA267DD, .rnd_upward_offset: 0, .rnd_downward_offset: 1, .rnd_tonearest_offset: 0}, |
| 40 | }}; |
| 41 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 42 | |
| 43 | uint32_t x_u = xbits.uintval(); |
| 44 | uint32_t x_abs = x_u & 0x7fff'ffffU; |
| 45 | |
| 46 | // Handle NaN and Inf. |
| 47 | if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) { |
| 48 | if (xbits.is_signaling_nan()) { |
| 49 | fputil::raise_except_if_required(FE_INVALID); |
| 50 | return FPBits::quiet_nan().get_val(); |
| 51 | } |
| 52 | |
| 53 | if (x_abs == 0x7f80'0000U) { |
| 54 | fputil::set_errno_if_required(EDOM); |
| 55 | fputil::raise_except_if_required(FE_INVALID); |
| 56 | } |
| 57 | |
| 58 | return x + FPBits::quiet_nan().get_val(); |
| 59 | } |
| 60 | |
| 61 | // tanpi(x) underflows for 0 < |x| <= 0x1.45f3p-128. |
| 62 | if (LIBC_UNLIKELY(x_abs <= 0x0028'be60U)) { |
| 63 | // Handle signed zeros. |
| 64 | if (x_abs == 0U) |
| 65 | return x; |
| 66 | fputil::set_errno_if_required(ERANGE); |
| 67 | } |
| 68 | |
| 69 | if (LIBC_UNLIKELY(fputil::is_integer(x))) { |
| 70 | // Preserve the input sign for even integers and flip it for odd integers. |
| 71 | Sign sign = |
| 72 | fputil::is_odd_integer(x) ? xbits.sign().negate() : xbits.sign(); |
| 73 | return FPBits::zero(sign).get_val(); |
| 74 | } |
| 75 | |
| 76 | // Odd multiples of 1/4 have exact results of +1 or -1. |
| 77 | double xd = static_cast<double>(x); |
| 78 | if (LIBC_UNLIKELY(fputil::is_odd_integer(4.0 * xd))) |
| 79 | return fputil::is_odd_integer(x: fputil::multiply_sub(x: 2.0, y: xd, z: 0.5)) ? -1.0f |
| 80 | : 1.0f; |
| 81 | |
| 82 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 83 | // Handle exceptional values. |
| 84 | if (LIBC_UNLIKELY(x_abs <= 0x3F3663FF)) { |
| 85 | bool x_sign = x_u >> 31; |
| 86 | |
| 87 | if (auto r = TANPIF_EXCEPTS.lookup_odd(x_abs, sign: x_sign); |
| 88 | LIBC_UNLIKELY(r.has_value())) |
| 89 | return r.value(); |
| 90 | } |
| 91 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 92 | |
| 93 | // Range reduction: |
| 94 | // All remaining inputs are finite nonintegers, so |x| < 2^23. |
| 95 | // For |x| > 1/32, we perform range reduction as follows: |
| 96 | // Find k and y such that: |
| 97 | // x = (k + y) * 1/32 |
| 98 | // k is an integer |
| 99 | // |y| < 0.5 |
| 100 | // |
| 101 | // This is done by performing: |
| 102 | // k = round(x * 32) |
| 103 | // y = x * 32 - k |
| 104 | // |
| 105 | // Once k and y are computed, we then deduce the answer by the formula: |
| 106 | // tan(x) = sin(x) / cos(x) |
| 107 | // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k) |
| 108 | double sin_k, cos_k, sin_y, cosm1_y; |
| 109 | sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y); |
| 110 | |
| 111 | if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) { |
| 112 | fputil::set_errno_if_required(ERANGE); |
| 113 | fputil::raise_except_if_required(FE_DIVBYZERO); |
| 114 | |
| 115 | int32_t x_mp5_i = static_cast<int32_t>(xd - 0.5); |
| 116 | return FPBits::inf(sign: (x_mp5_i & 0x1) ? Sign::NEG : Sign::POS).get_val(); |
| 117 | } |
| 118 | |
| 119 | using fputil::multiply_add; |
| 120 | return fputil::cast<float>( |
| 121 | x: multiply_add(x: sin_y, y: cos_k, z: multiply_add(x: cosm1_y, y: sin_k, z: sin_k)) / |
| 122 | fputil::sub_multiply(x: multiply_add(x: cosm1_y, y: cos_k, z: cos_k), y: sin_y, z: sin_k)); |
| 123 | } |
| 124 | |
| 125 | } // namespace math |
| 126 | |
| 127 | } // namespace LIBC_NAMESPACE_DECL |
| 128 | |
| 129 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF_H |
| 130 | |