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