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/// This file contains the implementation of sinhbf16.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16_H
15#define LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16_H
16
17#include "hdr/fenv_macros.h"
18#include "hdr/stdint_proxy.h"
19#include "src/__support/FPUtil/FEnvImpl.h"
20#include "src/__support/FPUtil/FPBits.h"
21#include "src/__support/FPUtil/bfloat16.h"
22#include "src/__support/FPUtil/cast.h"
23#include "src/__support/FPUtil/except_value_utils.h"
24#include "src/__support/FPUtil/rounding_mode.h"
25#include "src/__support/macros/attributes.h"
26#include "src/__support/macros/config.h"
27#include "src/__support/macros/optimization.h"
28#include "src/__support/math/sinhcoshbf16_utils.h"
29
30namespace LIBC_NAMESPACE_DECL {
31
32namespace math {
33
34LIBC_INLINE bfloat16 sinhbf16(bfloat16 x) {
35 using FPBits = fputil::FPBits<bfloat16>;
36 FPBits x_bits(x);
37 uint16_t x_u = x_bits.uintval();
38 uint16_t x_abs = x_u & 0x7fff;
39
40 // |x| <= 0.11328125
41 if (x_abs <= 0x3de8) {
42 if (x_abs == 0U)
43 return x;
44
45#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
46 if (x_u & 0x8000) {
47 if (fputil::fenv_is_round_down())
48 return FPBits(static_cast<uint16_t>(x_u + 1)).get_val();
49 } else {
50 if (fputil::fenv_is_round_up())
51 return FPBits(static_cast<uint16_t>(x_u + 1)).get_val();
52 }
53#endif
54 return FPBits(static_cast<uint16_t>(x_u)).get_val();
55 }
56
57 // |x| >= 89.5
58 if (LIBC_UNLIKELY(x_abs >= 0x42b3)) {
59
60 // sinh(inf) = inf
61 if (x_bits.is_inf())
62 return x;
63
64 // sinh(NaN) = NaN
65 if (x_bits.is_nan()) {
66 if (x_bits.is_signaling_nan()) {
67 fputil::raise_except_if_required(FE_INVALID);
68 return FPBits::quiet_nan().get_val();
69 }
70 return x;
71 }
72
73 return fputil::set_overflow<bfloat16>(x_bits.sign());
74 }
75
76 float xf = static_cast<float>(x);
77
78 // |x| >= 6.875
79 // return e^x / 2
80 if (x_abs >= 0x40dc) {
81 float abs_result =
82 math::sinhcoshbf16_internal::exp_half(xf: x_bits.abs().get_val());
83 if (x_bits.is_neg()) {
84 abs_result = -abs_result;
85 }
86 return fputil::cast<bfloat16>(x: abs_result);
87 }
88
89 // sinh(x) = (e^x - e^(-x)) / 2.
90 float result =
91 math::sinhcoshbf16_internal::eval_sinh_or_cosh</*is_sinh*/ true>(xf);
92
93 return fputil::cast<bfloat16>(x: result);
94}
95
96} // namespace math
97} // namespace LIBC_NAMESPACE_DECL
98
99#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16_H
100