1//===-- Single-precision sinh 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#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINHF_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_SINHF_H
11
12#include "sinhfcoshf_utils.h"
13#include "src/__support/FPUtil/FEnvImpl.h"
14#include "src/__support/FPUtil/FPBits.h"
15#include "src/__support/FPUtil/rounding_mode.h"
16#include "src/__support/macros/config.h"
17#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
18
19namespace LIBC_NAMESPACE_DECL {
20
21namespace math {
22
23LIBC_INLINE float sinhf(float x) {
24 using FPBits = typename fputil::FPBits<float>;
25 FPBits xbits(x);
26 uint32_t x_abs = xbits.abs().uintval();
27
28 // When |x| >= 90, or x is inf or nan
29 if (LIBC_UNLIKELY(x_abs >= 0x42b4'0000U || x_abs <= 0x3da0'0000U)) {
30 // |x| <= 0.078125
31 if (x_abs <= 0x3da0'0000U) {
32#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
33 // |x| = 0.0005589424981735646724700927734375
34 if (LIBC_UNLIKELY(x_abs == 0x3a12'85ffU)) {
35#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
36 return x;
37#else
38 if (fputil::fenv_is_round_to_nearest())
39 return x;
40#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
41 }
42#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
43
44 // |x| <= 2^-26
45 if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
46 return static_cast<float>(
47 LIBC_UNLIKELY(x_abs == 0) ? x : (x + 0.25 * x * x * x));
48 }
49
50 double xdbl = x;
51 double x2 = xdbl * xdbl;
52 // Sollya: fpminimax(sinh(x),[|3,5,7|],[|D...|],[-1/16-1/64;1/16+1/64],x);
53 // Sollya output: x * (0x1p0 + x^0x1p1 * (0x1.5555555556583p-3 + x^0x1p1
54 // * (0x1.111110d239f1fp-7
55 // + x^0x1p1 * 0x1.a02b5a284013cp-13)))
56 // Therefore, output of Sollya = x * pe;
57 double pe = fputil::polyeval(x: x2, a0: 0.0, a: 0x1.5555555556583p-3,
58 a: 0x1.111110d239f1fp-7, a: 0x1.a02b5a284013cp-13);
59 return static_cast<float>(fputil::multiply_add(x: xdbl, y: pe, z: xdbl));
60 }
61
62 if (xbits.is_nan())
63 return x + 1.0f; // sNaN to qNaN + signal
64
65 if (xbits.is_inf())
66 return x;
67
68#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
69 int rounding = fputil::quick_get_round();
70 if (xbits.is_neg()) {
71 if (LIBC_UNLIKELY(rounding == FE_UPWARD || rounding == FE_TOWARDZERO))
72 return -FPBits::max_normal().get_val();
73 } else {
74 if (LIBC_UNLIKELY(rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO))
75 return FPBits::max_normal().get_val();
76 }
77#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
78
79 fputil::set_errno_if_required(ERANGE);
80 fputil::raise_except_if_required(FE_OVERFLOW);
81
82 return x + FPBits::inf(sign: xbits.sign()).get_val();
83 }
84
85 // sinh(x) = (e^x - e^(-x)) / 2.
86 return static_cast<float>(
87 math::sinhfcoshf_internal::exp_pm_eval</*is_sinh*/ true>(x));
88}
89
90} // namespace math
91
92} // namespace LIBC_NAMESPACE_DECL
93
94#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINHF_H
95