| 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/multiply_add.h" |
| 18 | #include "src/__support/common.h" |
| 19 | #include "src/__support/macros/config.h" |
| 20 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 21 | |
| 22 | namespace LIBC_NAMESPACE_DECL { |
| 23 | |
| 24 | namespace math { |
| 25 | |
| 26 | LIBC_INLINE float tanpif(float x) { |
| 27 | using namespace sincosf_utils_internal; |
| 28 | |
| 29 | using FPBits = typename fputil::FPBits<float>; |
| 30 | FPBits xbits(x); |
| 31 | |
| 32 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 33 | constexpr size_t N_EXCEPTS = 3; |
| 34 | constexpr fputil::ExceptValues<float, N_EXCEPTS> TANPIF_EXCEPTS{.values: { |
| 35 | // (input, RZ output, RU offset, RD offset, RN offset) |
| 36 | {.input: 0x38F26685, .rnd_towardzero_result: 0x39BE6182, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, |
| 37 | {.input: 0x3E933802, .rnd_towardzero_result: 0x3FA267DD, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, |
| 38 | {.input: 0x3F3663FF, .rnd_towardzero_result: 0xBFA267DD, .rnd_upward_offset: 0, .rnd_downward_offset: 1, .rnd_tonearest_offset: 0}, |
| 39 | }}; |
| 40 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 41 | |
| 42 | uint32_t x_u = xbits.uintval(); |
| 43 | uint32_t x_abs = x_u & 0x7fff'ffffU; |
| 44 | double xd = static_cast<double>(xbits.get_val()); |
| 45 | |
| 46 | // Handle exceptional values |
| 47 | if (LIBC_UNLIKELY(x_abs <= 0x3F3663FF)) { |
| 48 | if (LIBC_UNLIKELY(x_abs == 0U)) |
| 49 | return x; |
| 50 | |
| 51 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 52 | bool x_sign = x_u >> 31; |
| 53 | |
| 54 | if (auto r = TANPIF_EXCEPTS.lookup_odd(x_abs, sign: x_sign); |
| 55 | LIBC_UNLIKELY(r.has_value())) |
| 56 | return r.value(); |
| 57 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 58 | } |
| 59 | |
| 60 | // Numbers greater or equal to 2^23 are always integers, or infinity, or NaN |
| 61 | if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) { |
| 62 | // x is inf or NaN. |
| 63 | if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) { |
| 64 | if (xbits.is_signaling_nan()) { |
| 65 | fputil::raise_except_if_required(FE_INVALID); |
| 66 | return FPBits::quiet_nan().get_val(); |
| 67 | } |
| 68 | |
| 69 | if (x_abs == 0x7f80'0000U) { |
| 70 | fputil::set_errno_if_required(EDOM); |
| 71 | fputil::raise_except_if_required(FE_INVALID); |
| 72 | } |
| 73 | |
| 74 | return x + FPBits::quiet_nan().get_val(); |
| 75 | } |
| 76 | |
| 77 | return FPBits::zero(sign: xbits.sign()).get_val(); |
| 78 | } |
| 79 | |
| 80 | // Range reduction: |
| 81 | // For |x| > 1/32, we perform range reduction as follows: |
| 82 | // Find k and y such that: |
| 83 | // x = (k + y) * 1/32 |
| 84 | // k is an integer |
| 85 | // |y| < 0.5 |
| 86 | // |
| 87 | // This is done by performing: |
| 88 | // k = round(x * 32) |
| 89 | // y = x * 32 - k |
| 90 | // |
| 91 | // Once k and y are computed, we then deduce the answer by the formula: |
| 92 | // tan(x) = sin(x) / cos(x) |
| 93 | // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k) |
| 94 | double sin_k, cos_k, sin_y, cosm1_y; |
| 95 | sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y); |
| 96 | |
| 97 | if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) { |
| 98 | fputil::set_errno_if_required(EDOM); |
| 99 | fputil::raise_except_if_required(FE_DIVBYZERO); |
| 100 | |
| 101 | int32_t x_mp5_i = static_cast<int32_t>(xd - 0.5); |
| 102 | return FPBits::inf(sign: (x_mp5_i & 0x1) ? Sign::NEG : Sign::POS).get_val(); |
| 103 | } |
| 104 | |
| 105 | using fputil::multiply_add; |
| 106 | return fputil::cast<float>( |
| 107 | x: multiply_add(x: sin_y, y: cos_k, z: multiply_add(x: cosm1_y, y: sin_k, z: sin_k)) / |
| 108 | multiply_add(x: sin_y, y: -sin_k, z: multiply_add(x: cosm1_y, y: cos_k, z: cos_k))); |
| 109 | } |
| 110 | |
| 111 | } // namespace math |
| 112 | |
| 113 | } // namespace LIBC_NAMESPACE_DECL |
| 114 | |
| 115 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF_H |
| 116 | |