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