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