[flang] Fix crash in HLFIR generation (#118399)
[llvm-project.git] / libc / src / math / generic / sinhf.cpp
blob371dd6e67e66e9f6388a59da49af49132dd59a2a
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 //===----------------------------------------------------------------------===//
9 #include "src/math/sinhf.h"
10 #include "src/__support/FPUtil/FPBits.h"
11 #include "src/__support/FPUtil/rounding_mode.h"
12 #include "src/__support/macros/config.h"
13 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
14 #include "src/math/generic/explogxf.h"
16 namespace LIBC_NAMESPACE_DECL {
18 LLVM_LIBC_FUNCTION(float, sinhf, (float x)) {
19 using FPBits = typename fputil::FPBits<float>;
20 FPBits xbits(x);
21 uint32_t x_abs = xbits.abs().uintval();
23 // When |x| >= 90, or x is inf or nan
24 if (LIBC_UNLIKELY(x_abs >= 0x42b4'0000U || x_abs <= 0x3da0'0000U)) {
25 // |x| <= 0.078125
26 if (x_abs <= 0x3da0'0000U) {
27 // |x| = 0.0005589424981735646724700927734375
28 if (LIBC_UNLIKELY(x_abs == 0x3a12'85ffU)) {
29 if (fputil::fenv_is_round_to_nearest())
30 return x;
33 // |x| <= 2^-26
34 if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
35 return static_cast<float>(
36 LIBC_UNLIKELY(x_abs == 0) ? x : (x + 0.25 * x * x * x));
39 double xdbl = x;
40 double x2 = xdbl * xdbl;
41 // Sollya: fpminimax(sinh(x),[|3,5,7|],[|D...|],[-1/16-1/64;1/16+1/64],x);
42 // Sollya output: x * (0x1p0 + x^0x1p1 * (0x1.5555555556583p-3 + x^0x1p1
43 // * (0x1.111110d239f1fp-7
44 // + x^0x1p1 * 0x1.a02b5a284013cp-13)))
45 // Therefore, output of Sollya = x * pe;
46 double pe = fputil::polyeval(x2, 0.0, 0x1.5555555556583p-3,
47 0x1.111110d239f1fp-7, 0x1.a02b5a284013cp-13);
48 return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl));
51 if (xbits.is_nan())
52 return x + 1.0f; // sNaN to qNaN + signal
54 if (xbits.is_inf())
55 return x;
57 int rounding = fputil::quick_get_round();
58 if (xbits.is_neg()) {
59 if (LIBC_UNLIKELY(rounding == FE_UPWARD || rounding == FE_TOWARDZERO))
60 return -FPBits::max_normal().get_val();
61 } else {
62 if (LIBC_UNLIKELY(rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO))
63 return FPBits::max_normal().get_val();
66 fputil::set_errno_if_required(ERANGE);
67 fputil::raise_except_if_required(FE_OVERFLOW);
69 return x + FPBits::inf(xbits.sign()).get_val();
72 // sinh(x) = (e^x - e^(-x)) / 2.
73 return static_cast<float>(exp_pm_eval</*is_sinh*/ true>(x));
76 } // namespace LIBC_NAMESPACE_DECL