1//===----------------------------------------------------------------------===//
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/// \file
10/// Implementation of the coshbf16 function, which computes the hyperbolic
11/// cosine of a bfloat16 value.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSHBF16_H
16#define LLVM_LIBC_SRC___SUPPORT_MATH_COSHBF16_H
17
18#include "hdr/fenv_macros.h"
19#include "hdr/stdint_proxy.h"
20#include "src/__support/FPUtil/FEnvImpl.h"
21#include "src/__support/FPUtil/FPBits.h"
22#include "src/__support/FPUtil/bfloat16.h"
23#include "src/__support/FPUtil/cast.h"
24#include "src/__support/FPUtil/except_value_utils.h"
25#include "src/__support/FPUtil/rounding_mode.h"
26#include "src/__support/macros/attributes.h"
27#include "src/__support/macros/config.h"
28#include "src/__support/macros/optimization.h"
29#include "src/__support/math/sinhcoshbf16_utils.h"
30
31namespace LIBC_NAMESPACE_DECL {
32
33namespace math {
34
35LIBC_INLINE bfloat16 coshbf16(bfloat16 x) {
36 using FPBits = fputil::FPBits<bfloat16>;
37 FPBits x_bits(x);
38
39 // cosh is an even function, cosh(-x) = cosh(x), so we only need |x|.
40 // We bound the cases below by comparing the bits of |x| as an integer.
41 // NaN and Inf have the largest bits of |x|, so they are handled together with
42 // the overflow case below.
43 uint16_t x_abs_uint = x_bits.abs().uintval();
44
45 constexpr uint16_t ONE_UINT = 0x3F80; // 0x1p0 = 1.0
46
47 // For 0 < |x| <= 0.0879, cosh(x) = 1 + x^2/2 + ... lies strictly between 1
48 // and the next bfloat16 above 1, and is closer to 1. So the result is 1 in
49 // all rounding modes except for RU, where it is the next value above 1.
50 // x = 0 is the exception, since cosh(0) = 1 exactly.
51 constexpr uint16_t ROUNDS_TO_ONE_ABS_BOUND = 0x3DB4; // 0x1.68p-4 = 0.0879
52 if (x_abs_uint <= ROUNDS_TO_ONE_ABS_BOUND) {
53 if (x_abs_uint == 0U)
54 return FPBits(ONE_UINT).get_val();
55
56#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
57 constexpr uint16_t ONE_NEXT_UP_UINT = 0x3F81; // 0x1.02p0 = 1.0078125
58 if (fputil::fenv_is_round_up())
59 return FPBits(ONE_NEXT_UP_UINT).get_val();
60#endif
61 return FPBits(ONE_UINT).get_val();
62 }
63
64 // cosh(x) overflows bfloat16 for |x| >= 89.5. cosh(x) > 0 for all x, so the
65 // overflow is always to +Inf, regardless of the sign of x.
66 constexpr uint16_t OVERFLOW_ABS_BOUND = 0x42B3; // 0x1.66p6 = 89.5
67 if (LIBC_UNLIKELY(x_abs_uint >= OVERFLOW_ABS_BOUND)) {
68 if (x_bits.is_nan()) {
69 // If it is a signaling NaN, then we raise an FE_INVALID exception
70 // and return a quiet NaN
71 if (x_bits.is_signaling_nan()) {
72 fputil::raise_except_if_required(FE_INVALID);
73 return FPBits::quiet_nan().get_val();
74 }
75 // If not, just return the NaN
76 return x;
77 }
78
79 // cosh(+-Inf) = +Inf
80 if (x_bits.is_inf())
81 return FPBits::inf().get_val();
82
83 return fputil::set_overflow<bfloat16>();
84 }
85
86 // For 6.875 <= |x| < 89.5, e^(-|x|) / e^|x| = e^(-2|x|) < 2^-19 is far below
87 // the precision of bfloat16, so we can use
88 // cosh(x) = (e^|x| + e^(-|x|)) / 2 ~ e^|x| / 2.
89 constexpr uint16_t EXP_HALF_ABS_BOUND = 0x40DC; // 0x1.b8p2 = 6.875
90 if (x_abs_uint >= EXP_HALF_ABS_BOUND) {
91 float x_abs_float = fputil::cast<float>(x: x_bits.abs().get_val());
92 return fputil::cast<bfloat16>(x: sinhcoshbf16_internal::exp_half(xf: x_abs_float));
93 }
94
95 // For the remaining inputs, 0.0879 < |x| < 6.875, we compute
96 // cosh(x) = (e^x + e^(-x)) / 2
97 // using the range reduction and the polynomial shared with sinhbf16 in
98 // sinhcoshbf16_utils.h.
99 float x_float = fputil::cast<float>(x);
100 return fputil::cast<bfloat16>(
101 x: sinhcoshbf16_internal::eval_sinh_or_cosh</*IsSinh=*/false>(xf: x_float));
102}
103
104} // namespace math
105} // namespace LIBC_NAMESPACE_DECL
106
107#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHBF16_H
108