1 | //===-- Half-precision atanh(x) 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/atanhf16.h" |
10 | #include "explogxf.h" |
11 | #include "hdr/errno_macros.h" |
12 | #include "hdr/fenv_macros.h" |
13 | #include "src/__support/FPUtil/FEnvImpl.h" |
14 | #include "src/__support/FPUtil/FPBits.h" |
15 | #include "src/__support/FPUtil/PolyEval.h" |
16 | #include "src/__support/FPUtil/cast.h" |
17 | #include "src/__support/FPUtil/except_value_utils.h" |
18 | #include "src/__support/FPUtil/multiply_add.h" |
19 | #include "src/__support/common.h" |
20 | #include "src/__support/macros/config.h" |
21 | #include "src/__support/macros/optimization.h" |
22 | |
23 | namespace LIBC_NAMESPACE_DECL { |
24 | |
25 | static constexpr size_t N_EXCEPTS = 1; |
26 | static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ATANHF16_EXCEPTS{{ |
27 | // (input, RZ output, RU offset, RD offset, RN offset) |
28 | // x = 0x1.a5cp-4, atanhf16(x) = 0x1.a74p-4 (RZ) |
29 | {0x2E97, 0x2E9D, 1, 0, 0}, |
30 | }}; |
31 | |
32 | LLVM_LIBC_FUNCTION(float16, atanhf16, (float16 x)) { |
33 | using FPBits = fputil::FPBits<float16>; |
34 | |
35 | FPBits xbits(x); |
36 | Sign sign = xbits.sign(); |
37 | uint16_t x_abs = xbits.abs().uintval(); |
38 | |
39 | // |x| >= 1 |
40 | if (LIBC_UNLIKELY(x_abs >= 0x3c00U)) { |
41 | if (xbits.is_nan()) { |
42 | if (xbits.is_signaling_nan()) { |
43 | fputil::raise_except_if_required(FE_INVALID); |
44 | return FPBits::quiet_nan().get_val(); |
45 | } |
46 | return x; |
47 | } |
48 | |
49 | // |x| == 1.0 |
50 | if (x_abs == 0x3c00U) { |
51 | fputil::set_errno_if_required(ERANGE); |
52 | fputil::raise_except_if_required(FE_DIVBYZERO); |
53 | return FPBits::inf(sign).get_val(); |
54 | } |
55 | // |x| > 1.0 |
56 | fputil::set_errno_if_required(EDOM); |
57 | fputil::raise_except_if_required(FE_INVALID); |
58 | return FPBits::quiet_nan().get_val(); |
59 | } |
60 | |
61 | if (auto r = ATANHF16_EXCEPTS.lookup(xbits.uintval()); |
62 | LIBC_UNLIKELY(r.has_value())) |
63 | return r.value(); |
64 | |
65 | // For |x| less than approximately 0.24 |
66 | if (LIBC_UNLIKELY(x_abs <= 0x33f3U)) { |
67 | // atanh(+/-0) = +/-0 |
68 | if (LIBC_UNLIKELY(x_abs == 0U)) |
69 | return x; |
70 | // The Taylor expansion of atanh(x) is: |
71 | // atanh(x) = x + x^3/3 + x^5/5 + x^7/7 + x^9/9 + x^11/11 |
72 | // = x * [1 + x^2/3 + x^4/5 + x^6/7 + x^8/9 + x^10/11] |
73 | // When |x| < 2^-5 (0x0800U), this can be approximated by: |
74 | // atanh(x) ≈ x + (1/3)*x^3 |
75 | if (LIBC_UNLIKELY(x_abs < 0x0800U)) { |
76 | float xf = x; |
77 | return fputil::cast<float16>(xf + 0x1.555556p-2f * xf * xf * xf); |
78 | } |
79 | |
80 | // For 2^-5 <= |x| <= 0x1.fccp-3 (~0.24): |
81 | // Let t = x^2. |
82 | // Define P(t) ≈ (1/3)*t + (1/5)*t^2 + (1/7)*t^3 + (1/9)*t^4 + (1/11)*t^5. |
83 | // Coefficients (from Sollya, RN, hexadecimal): |
84 | // 1/3 = 0x1.555556p-2, 1/5 = 0x1.99999ap-3, 1/7 = 0x1.24924ap-3, |
85 | // 1/9 = 0x1.c71c72p-4, 1/11 = 0x1.745d18p-4 |
86 | // Thus, atanh(x) ≈ x * (1 + P(x^2)). |
87 | float xf = x; |
88 | float x2 = xf * xf; |
89 | float pe = fputil::polyeval(x2, 0.0f, 0x1.555556p-2f, 0x1.99999ap-3f, |
90 | 0x1.24924ap-3f, 0x1.c71c72p-4f, 0x1.745d18p-4f); |
91 | return fputil::cast<float16>(fputil::multiply_add(xf, pe, xf)); |
92 | } |
93 | |
94 | float xf = x; |
95 | return fputil::cast<float16>(0.5 * log_eval_f((xf + 1.0f) / (xf - 1.0f))); |
96 | } |
97 | |
98 | } // namespace LIBC_NAMESPACE_DECL |
99 | |