• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Single-precision cosh 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/coshf.h"
10 #include "src/__support/FPUtil/FPBits.h"
11 #include "src/__support/FPUtil/multiply_add.h"
12 #include "src/__support/FPUtil/rounding_mode.h"
13 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
14 #include "src/math/generic/explogxf.h"
15 
16 namespace LIBC_NAMESPACE {
17 
18 LLVM_LIBC_FUNCTION(float, coshf, (float x)) {
19   using FPBits = typename fputil::FPBits<float>;
20 
21   FPBits xbits(x);
22   xbits.set_sign(Sign::POS);
23   x = xbits.get_val();
24 
25   uint32_t x_u = xbits.uintval();
26 
27   // When |x| >= 90, or x is inf or nan
28   if (LIBC_UNLIKELY(x_u >= 0x42b4'0000U || x_u <= 0x3280'0000U)) {
29     // |x| <= 2^-26
30     if (x_u <= 0x3280'0000U) {
31       return 1.0f + x;
32     }
33 
34     if (xbits.is_inf_or_nan())
35       return x + FPBits::inf().get_val();
36 
37     int rounding = fputil::quick_get_round();
38     if (LIBC_UNLIKELY(rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO))
39       return FPBits::max_normal().get_val();
40 
41     fputil::set_errno_if_required(ERANGE);
42     fputil::raise_except_if_required(FE_OVERFLOW);
43 
44     return x + FPBits::inf().get_val();
45   }
46 
47   // TODO: We should be able to reduce the latency and reciprocal throughput
48   // further by using a low degree (maybe 3-7 ?) minimax polynomial for small
49   // but not too small inputs, such as |x| < 2^-2, or |x| < 2^-3.
50 
51   // cosh(x) = (e^x + e^(-x)) / 2.
52   return static_cast<float>(exp_pm_eval</*is_sinh*/ false>(x));
53 }
54 
55 } // namespace LIBC_NAMESPACE
56