xref: /aosp_15_r20/external/llvm-libc/src/math/generic/atanhf.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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       return x;
28     }
29     // |x| == 1.0
30     if (x_abs == 0x3F80'0000U) {
31       fputil::set_errno_if_required(ERANGE);
32       fputil::raise_except_if_required(FE_DIVBYZERO);
33       return FPBits::inf(sign).get_val();
34     } else {
35       fputil::set_errno_if_required(EDOM);
36       fputil::raise_except_if_required(FE_INVALID);
37       return FPBits::quiet_nan().get_val();
38     }
39   }
40 
41   // |x| < ~0.10
42   if (LIBC_UNLIKELY(x_abs <= 0x3dcc'0000U)) {
43     // |x| <= 2^-26
44     if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
45       return static_cast<float>(LIBC_UNLIKELY(x_abs == 0)
46                                     ? x
47                                     : (x + 0x1.5555555555555p-2 * x * x * x));
48     }
49 
50     double xdbl = x;
51     double x2 = xdbl * xdbl;
52     // Pure Taylor series.
53     double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2,
54                                  0x1.999999999999ap-3, 0x1.2492492492492p-3,
55                                  0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4);
56     return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl));
57   }
58   double xdbl = x;
59   return static_cast<float>(0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0)));
60 }
61 
62 } // namespace LIBC_NAMESPACE_DECL
63