|
| 1 | +//===-- Implementation header for atanhf ------------------------*- C++ -*-===// |
| 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_ATANHF_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H |
| 11 | + |
| 12 | +#include "acoshf_utils.h" |
| 13 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | +#include "src/__support/FPUtil/FPBits.h" |
| 15 | +#include "src/__support/macros/config.h" |
| 16 | +#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 17 | + |
| 18 | +namespace LIBC_NAMESPACE_DECL { |
| 19 | + |
| 20 | +namespace math { |
| 21 | + |
| 22 | +LIBC_INLINE static constexpr float atanhf(float x) { |
| 23 | + using namespace acoshf_internal; |
| 24 | + using FPBits = typename fputil::FPBits<float>; |
| 25 | + |
| 26 | + FPBits xbits(x); |
| 27 | + Sign sign = xbits.sign(); |
| 28 | + uint32_t x_abs = xbits.abs().uintval(); |
| 29 | + |
| 30 | + // |x| >= 1.0 |
| 31 | + if (LIBC_UNLIKELY(x_abs >= 0x3F80'0000U)) { |
| 32 | + if (xbits.is_nan()) { |
| 33 | + if (xbits.is_signaling_nan()) { |
| 34 | + fputil::raise_except_if_required(FE_INVALID); |
| 35 | + return FPBits::quiet_nan().get_val(); |
| 36 | + } |
| 37 | + return x; |
| 38 | + } |
| 39 | + // |x| == 1.0 |
| 40 | + if (x_abs == 0x3F80'0000U) { |
| 41 | + fputil::set_errno_if_required(ERANGE); |
| 42 | + fputil::raise_except_if_required(FE_DIVBYZERO); |
| 43 | + return FPBits::inf(sign).get_val(); |
| 44 | + } else { |
| 45 | + fputil::set_errno_if_required(EDOM); |
| 46 | + fputil::raise_except_if_required(FE_INVALID); |
| 47 | + return FPBits::quiet_nan().get_val(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // |x| < ~0.10 |
| 52 | + if (LIBC_UNLIKELY(x_abs <= 0x3dcc'0000U)) { |
| 53 | + // |x| <= 2^-26 |
| 54 | + if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) { |
| 55 | + return static_cast<float>(LIBC_UNLIKELY(x_abs == 0) |
| 56 | + ? x |
| 57 | + : (x + 0x1.5555555555555p-2 * x * x * x)); |
| 58 | + } |
| 59 | + |
| 60 | + double xdbl = x; |
| 61 | + double x2 = xdbl * xdbl; |
| 62 | + // Pure Taylor series. |
| 63 | + double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2, |
| 64 | + 0x1.999999999999ap-3, 0x1.2492492492492p-3, |
| 65 | + 0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4); |
| 66 | + return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl)); |
| 67 | + } |
| 68 | + double xdbl = x; |
| 69 | + return static_cast<float>(0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0))); |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace math |
| 73 | + |
| 74 | +} // namespace LIBC_NAMESPACE_DECL |
| 75 | + |
| 76 | +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATANHF_H |
0 commit comments