1//===-- Implementation header for coshf -------------------------*- C++ -*-===//
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#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H
11
12#include "src/__support/FPUtil/FPBits.h"
13#include "src/__support/FPUtil/except_value_utils.h"
14#include "src/__support/macros/config.h"
15#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
16#include "src/__support/math/sinhfcoshf_utils.h"
17
18namespace LIBC_NAMESPACE_DECL {
19
20namespace math {
21
22LIBC_INLINE float coshf(float x) {
23 using namespace sinhfcoshf_internal;
24 using FPBits = typename fputil::FPBits<float>;
25
26 FPBits xbits(x);
27 xbits.set_sign(Sign::POS);
28 x = xbits.get_val();
29
30 uint32_t x_u = xbits.uintval();
31
32 // When |x| >= 90, or x is inf or nan
33 if (LIBC_UNLIKELY(x_u >= 0x42b4'0000U || x_u <= 0x3280'0000U)) {
34 // |x| <= 2^-26
35 if (x_u <= 0x3280'0000U) {
36 return 1.0f + x;
37 }
38
39 if (xbits.is_inf_or_nan())
40 return x + FPBits::inf().get_val();
41
42 return fputil::set_overflow<float>();
43 }
44
45 // TODO: We should be able to reduce the latency and reciprocal throughput
46 // further by using a low degree (maybe 3-7 ?) minimax polynomial for small
47 // but not too small inputs, such as |x| < 2^-2, or |x| < 2^-3.
48
49 // cosh(x) = (e^x + e^(-x)) / 2.
50 return static_cast<float>(exp_pm_eval</*is_sinh*/ false>(x));
51}
52
53} // namespace math
54
55} // namespace LIBC_NAMESPACE_DECL
56
57#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H
58