xref: /llvm-project/libc/src/math/generic/coshf16.cpp (revision ed3d05178274890fb804f43ae1bcdfd33b5fd8f0)
1*ed3d0517SOverMighty //===-- Half-precision cosh(x) function -----------------------------------===//
2*ed3d0517SOverMighty //
3*ed3d0517SOverMighty // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*ed3d0517SOverMighty // See https://llvm.org/LICENSE.txt for license information.
5*ed3d0517SOverMighty // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*ed3d0517SOverMighty //
7*ed3d0517SOverMighty //===----------------------------------------------------------------------===//
8*ed3d0517SOverMighty 
9*ed3d0517SOverMighty #include "src/math/coshf16.h"
10*ed3d0517SOverMighty #include "expxf16.h"
11*ed3d0517SOverMighty #include "hdr/errno_macros.h"
12*ed3d0517SOverMighty #include "hdr/fenv_macros.h"
13*ed3d0517SOverMighty #include "src/__support/FPUtil/FEnvImpl.h"
14*ed3d0517SOverMighty #include "src/__support/FPUtil/FPBits.h"
15*ed3d0517SOverMighty #include "src/__support/FPUtil/except_value_utils.h"
16*ed3d0517SOverMighty #include "src/__support/FPUtil/rounding_mode.h"
17*ed3d0517SOverMighty #include "src/__support/common.h"
18*ed3d0517SOverMighty #include "src/__support/macros/config.h"
19*ed3d0517SOverMighty #include "src/__support/macros/optimization.h"
20*ed3d0517SOverMighty 
21*ed3d0517SOverMighty namespace LIBC_NAMESPACE_DECL {
22*ed3d0517SOverMighty 
23*ed3d0517SOverMighty static constexpr fputil::ExceptValues<float16, 9> COSHF16_EXCEPTS_POS = {{
24*ed3d0517SOverMighty     // x = 0x1.6ap-5, coshf16(x) = 0x1p+0 (RZ)
25*ed3d0517SOverMighty     {0x29a8U, 0x3c00U, 1U, 0U, 1U},
26*ed3d0517SOverMighty     // x = 0x1.8c4p+0, coshf16(x) = 0x1.3a8p+1 (RZ)
27*ed3d0517SOverMighty     {0x3e31U, 0x40eaU, 1U, 0U, 0U},
28*ed3d0517SOverMighty     // x = 0x1.994p+0, coshf16(x) = 0x1.498p+1 (RZ)
29*ed3d0517SOverMighty     {0x3e65U, 0x4126U, 1U, 0U, 0U},
30*ed3d0517SOverMighty     // x = 0x1.b6p+0, coshf16(x) = 0x1.6d8p+1 (RZ)
31*ed3d0517SOverMighty     {0x3ed8U, 0x41b6U, 1U, 0U, 1U},
32*ed3d0517SOverMighty     // x = 0x1.aap+1, coshf16(x) = 0x1.be8p+3 (RZ)
33*ed3d0517SOverMighty     {0x42a8U, 0x4afaU, 1U, 0U, 1U},
34*ed3d0517SOverMighty     // x = 0x1.cc4p+1, coshf16(x) = 0x1.23cp+4 (RZ)
35*ed3d0517SOverMighty     {0x4331U, 0x4c8fU, 1U, 0U, 0U},
36*ed3d0517SOverMighty     // x = 0x1.288p+2, coshf16(x) = 0x1.9b4p+5 (RZ)
37*ed3d0517SOverMighty     {0x44a2U, 0x526dU, 1U, 0U, 0U},
38*ed3d0517SOverMighty     // x = 0x1.958p+2, coshf16(x) = 0x1.1a4p+8 (RZ)
39*ed3d0517SOverMighty     {0x4656U, 0x5c69U, 1U, 0U, 0U},
40*ed3d0517SOverMighty     // x = 0x1.5fp+3, coshf16(x) = 0x1.c54p+14 (RZ)
41*ed3d0517SOverMighty     {0x497cU, 0x7715U, 1U, 0U, 1U},
42*ed3d0517SOverMighty }};
43*ed3d0517SOverMighty 
44*ed3d0517SOverMighty static constexpr fputil::ExceptValues<float16, 4> COSHF16_EXCEPTS_NEG = {{
45*ed3d0517SOverMighty     // x = -0x1.6ap-5, coshf16(x) = 0x1p+0 (RZ)
46*ed3d0517SOverMighty     {0xa9a8U, 0x3c00U, 1U, 0U, 1U},
47*ed3d0517SOverMighty     // x = -0x1.b6p+0, coshf16(x) = 0x1.6d8p+1 (RZ)
48*ed3d0517SOverMighty     {0xbed8U, 0x41b6U, 1U, 0U, 1U},
49*ed3d0517SOverMighty     // x = -0x1.288p+2, coshf16(x) = 0x1.9b4p+5 (RZ)
50*ed3d0517SOverMighty     {0xc4a2U, 0x526dU, 1U, 0U, 0U},
51*ed3d0517SOverMighty     // x = -0x1.5fp+3, coshf16(x) = 0x1.c54p+14 (RZ)
52*ed3d0517SOverMighty     {0xc97cU, 0x7715U, 1U, 0U, 1U},
53*ed3d0517SOverMighty }};
54*ed3d0517SOverMighty 
55*ed3d0517SOverMighty LLVM_LIBC_FUNCTION(float16, coshf16, (float16 x)) {
56*ed3d0517SOverMighty   using FPBits = fputil::FPBits<float16>;
57*ed3d0517SOverMighty   FPBits x_bits(x);
58*ed3d0517SOverMighty 
59*ed3d0517SOverMighty   uint16_t x_u = x_bits.uintval();
60*ed3d0517SOverMighty   uint16_t x_abs = x_u & 0x7fffU;
61*ed3d0517SOverMighty 
62*ed3d0517SOverMighty   // When |x| >= acosh(2^16), or x is NaN.
63*ed3d0517SOverMighty   if (LIBC_UNLIKELY(x_abs >= 0x49e5U)) {
64*ed3d0517SOverMighty     // cosh(NaN) = NaN
65*ed3d0517SOverMighty     if (x_bits.is_nan()) {
66*ed3d0517SOverMighty       if (x_bits.is_signaling_nan()) {
67*ed3d0517SOverMighty         fputil::raise_except_if_required(FE_INVALID);
68*ed3d0517SOverMighty         return FPBits::quiet_nan().get_val();
69*ed3d0517SOverMighty       }
70*ed3d0517SOverMighty 
71*ed3d0517SOverMighty       return x;
72*ed3d0517SOverMighty     }
73*ed3d0517SOverMighty 
74*ed3d0517SOverMighty     // When |x| >= acosh(2^16).
75*ed3d0517SOverMighty     if (x_abs >= 0x49e5U) {
76*ed3d0517SOverMighty       // cosh(+/-inf) = +inf
77*ed3d0517SOverMighty       if (x_bits.is_inf())
78*ed3d0517SOverMighty         return FPBits::inf().get_val();
79*ed3d0517SOverMighty 
80*ed3d0517SOverMighty       switch (fputil::quick_get_round()) {
81*ed3d0517SOverMighty       case FE_TONEAREST:
82*ed3d0517SOverMighty       case FE_UPWARD:
83*ed3d0517SOverMighty         fputil::set_errno_if_required(ERANGE);
84*ed3d0517SOverMighty         fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
85*ed3d0517SOverMighty         return FPBits::inf().get_val();
86*ed3d0517SOverMighty       default:
87*ed3d0517SOverMighty         return FPBits::max_normal().get_val();
88*ed3d0517SOverMighty       }
89*ed3d0517SOverMighty     }
90*ed3d0517SOverMighty   }
91*ed3d0517SOverMighty 
92*ed3d0517SOverMighty   if (x_bits.is_pos()) {
93*ed3d0517SOverMighty     if (auto r = COSHF16_EXCEPTS_POS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
94*ed3d0517SOverMighty       return r.value();
95*ed3d0517SOverMighty   } else {
96*ed3d0517SOverMighty     if (auto r = COSHF16_EXCEPTS_NEG.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
97*ed3d0517SOverMighty       return r.value();
98*ed3d0517SOverMighty   }
99*ed3d0517SOverMighty 
100*ed3d0517SOverMighty   return eval_sinh_or_cosh</*IsSinh=*/false>(x);
101*ed3d0517SOverMighty }
102*ed3d0517SOverMighty 
103*ed3d0517SOverMighty } // namespace LIBC_NAMESPACE_DECL
104