[libc-commits] [libc] [llvm] [libc][math][c23] Add sinhbf16 function (PR #224645)
via libc-commits
libc-commits at lists.llvm.org
Wed Sep 23 06:29:50 PDT 2026
https://github.com/dil12321 updated https://github.com/llvm/llvm-project/pull/224645
>From 81a9a4f6fa5f9c15799ac4a0191098697b047749 Mon Sep 17 00:00:00 2001
From: Dylan Fernando <dylanf123 at gmail.com>
Date: Fri, 18 Sep 2026 23:50:55 +1000
Subject: [PATCH 1/4] feat: implementation and tests for sinhbf16
---
libc/config/baremetal/aarch64/entrypoints.txt | 1 +
libc/config/baremetal/arm/entrypoints.txt | 1 +
libc/config/baremetal/riscv/entrypoints.txt | 1 +
libc/config/darwin/aarch64/entrypoints.txt | 1 +
libc/config/darwin/x86_64/entrypoints.txt | 1 +
libc/config/freebsd/x86_64/entrypoints.txt | 1 +
libc/config/gpu/amdgpu/entrypoints.txt | 1 +
libc/config/gpu/nvptx/entrypoints.txt | 1 +
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/arm/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/config/windows/entrypoints.txt | 1 +
libc/docs/headers/math/index.rst | 2 +-
libc/include/math.yaml | 6 ++
libc/shared/math.h | 1 +
libc/shared/math/sinhbf16.h | 23 +++++
libc/src/__support/math/CMakeLists.txt | 13 +++
libc/src/__support/math/sinhbf16.h | 97 +++++++++++++++++++
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 10 ++
libc/src/math/generic/sinhbf16.cpp | 18 ++++
libc/src/math/sinhbf16.h | 21 ++++
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
libc/test/src/math/CMakeLists.txt | 13 +++
libc/test/src/math/sinhbf16_test.cpp | 47 +++++++++
libc/test/src/math/smoke/CMakeLists.txt | 13 +++
libc/test/src/math/smoke/sinhbf16_test.cpp | 89 +++++++++++++++++
.../llvm-project-overlay/libc/BUILD.bazel | 20 ++++
30 files changed, 388 insertions(+), 1 deletion(-)
create mode 100644 libc/shared/math/sinhbf16.h
create mode 100644 libc/src/__support/math/sinhbf16.h
create mode 100644 libc/src/math/generic/sinhbf16.cpp
create mode 100644 libc/src/math/sinhbf16.h
create mode 100644 libc/test/src/math/sinhbf16_test.cpp
create mode 100644 libc/test/src/math/smoke/sinhbf16_test.cpp
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 114534ce61ae6e..cdc0eb219fb8a5 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -875,6 +875,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index c8148815c34e34..24cd54e88376c0 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -886,6 +886,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 622d6156cff11d..7d67b314707a3f 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -878,6 +878,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index d702b2c5a65497..75561bd049c8b4 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -697,6 +697,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 716ab572f8d3cd..57b48280a12e44 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -333,6 +333,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 8f6c178301c2f2..33987b80a71508 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -630,6 +630,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 5c5e5891da8227..8fbdbd897342d5 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -746,6 +746,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index afae725484a891..0c65b328bdb8fb 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -762,6 +762,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f87ba3437fb6c4..f9ea212ab13d46 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1064,6 +1064,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index f4347331d0b310..8cb0d594bd82ed 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -688,6 +688,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 51587056fb02d6..911003ed26322d 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1148,6 +1148,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 8b77efa648c43d..0cb7d7fa68b79e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1159,6 +1159,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 3a48b49e169b3a..603717a159c001 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -423,6 +423,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.scalbnbf16
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
+ libc.src.math.sinhbf16
libc.src.math.sqrtbf16
libc.src.math.tanbf16
libc.src.math.truncbf16
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 2d5e188e804d4c..60dc3fb152f59c 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -347,7 +347,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| sincos | |check| | |check| | | | | | | |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| sinh | |check| | | | |check| | | | 7.12.5.5 | F.10.2.5 |
+| sinh | |check| | | | |check| | | |check| | 7.12.5.5 | F.10.2.5 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| sinpi | |check| | | | |check| | | | 7.12.4.13 | F.10.1.13 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/include/math.yaml b/libc/include/math.yaml
index 006233e579f86c..cdd0c6fbe1b3a2 100644
--- a/libc/include/math.yaml
+++ b/libc/include/math.yaml
@@ -2659,6 +2659,12 @@ functions:
return_type: float
arguments:
- type: float
+ - name: sinhbf16
+ standards:
+ - stdc
+ return_type: _Float16
+ arguments:
+ - type: _Float16
- name: sinhf16
standards:
- stdc
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 7b52cedf99c245..17e8b38d64a738 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -467,6 +467,7 @@
#include "math/sinf.h"
#include "math/sinf16.h"
#include "math/sinhf.h"
+#include "math/sinhbf16.h"
#include "math/sinhf16.h"
#include "math/sinpif.h"
#include "math/sinpif16.h"
diff --git a/libc/shared/math/sinhbf16.h b/libc/shared/math/sinhbf16.h
new file mode 100644
index 00000000000000..a4255838a5d917
--- /dev/null
+++ b/libc/shared/math/sinhbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared sinhbf16 function --------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_SINHBF16_H
+#define LLVM_LIBC_SHARED_MATH_SINHBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/sinhbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::sinhbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_SINHBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 0871a5bf229b63..58339ad1264011 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5370,6 +5370,19 @@ add_header_library(
libc.src.__support.macros.properties.types
)
+add_header_library(
+ sinhbf16
+ HDRS
+ sinhbf16.h
+ DEPENDS
+ .sinhfcoshf_utils
+ libc.src.__support.FPUtil.bfloat16
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.macros.config
+ libc.src.__support.macros.attributes
+ libc.src.__support.macros.optimization
+)
+
add_header_library(
sinhf
HDRS
diff --git a/libc/src/__support/math/sinhbf16.h b/libc/src/__support/math/sinhbf16.h
new file mode 100644
index 00000000000000..6720bd4a265bd2
--- /dev/null
+++ b/libc/src/__support/math/sinhbf16.h
@@ -0,0 +1,97 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the implementation of sinhbf16.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16_H
+
+#include "sinhfcoshf_utils.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE bfloat16 sinhbf16(bfloat16 x) {
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits x_bits(x);
+ uint16_t x_u = x_bits.uintval();
+ uint16_t x_abs = x_u & 0x7fff;
+
+ // |x| <= 0.11328125
+ if (x_abs <= 0x3de8) {
+ if (x_abs == 0U)
+ return x;
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ if (x_u & 0x8000) {
+ if (fputil::fenv_is_round_down())
+ return FPBits(static_cast<uint16_t>(x_u + 1)).get_val();
+ } else {
+ if (fputil::fenv_is_round_up())
+ return FPBits(static_cast<uint16_t>(x_u + 1)).get_val();
+ }
+#endif
+ return FPBits(static_cast<uint16_t>(x_u)).get_val();
+ }
+
+ // |x| >= 89.5
+ if (LIBC_UNLIKELY(x_abs >= 0x42b3)) {
+
+ // sinh(inf) = inf
+ if (x_bits.is_inf())
+ return x;
+
+ // sinh(NaN) = NaN
+ if (x_bits.is_nan()) {
+ if (x_bits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ int rounding = fputil::quick_get_round();
+ if (x_bits.is_neg()) {
+ if (LIBC_UNLIKELY(rounding == FE_UPWARD || rounding == FE_TOWARDZERO))
+ return -FPBits::max_normal().get_val();
+ } else {
+ if (LIBC_UNLIKELY(rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO))
+ return FPBits::max_normal().get_val();
+ }
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_OVERFLOW);
+
+ uint16_t inf_bits = (x_u & 0x8000) | 0x7f80;
+ return fputil::FPBits<bfloat16>(inf_bits).get_val();
+ }
+
+ float xf = static_cast<float>(x);
+ float result = static_cast<float>(
+ math::sinhfcoshf_internal::exp_pm_eval</*is_sinh*/ true>(xf));
+
+ return fputil::cast<bfloat16>(result);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 2c7568f0b63618..3fb9e8b2d0cc5f 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -580,6 +580,7 @@ add_math_entrypoint_object(sinpif)
add_math_entrypoint_object(sinpif16)
add_math_entrypoint_object(sinh)
+add_math_entrypoint_object(sinhbf16)
add_math_entrypoint_object(sinhf)
add_math_entrypoint_object(sinhf16)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 28b8e954530ddf..e243130c997e32 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3755,6 +3755,16 @@ add_entrypoint_object(
libc.src.__support.math.sinhf
)
+add_entrypoint_object(
+ sinhbf16
+ SRCS
+ sinhbf16.cpp
+ HDRS
+ ../sinhbf16.h
+ DEPENDS
+ libc.src.__support.math.sinhbf16
+)
+
add_entrypoint_object(
sinhf16
SRCS
diff --git a/libc/src/math/generic/sinhbf16.cpp b/libc/src/math/generic/sinhbf16.cpp
new file mode 100644
index 00000000000000..eaab609360752f
--- /dev/null
+++ b/libc/src/math/generic/sinhbf16.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation for sinhbf16(x) function ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/sinhbf16.h"
+#include "src/__support/math/sinhbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, sinhbf16, (bfloat16 x)) {
+ return math::sinhbf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/sinhbf16.h b/libc/src/math/sinhbf16.h
new file mode 100644
index 00000000000000..91f30cb68f2469
--- /dev/null
+++ b/libc/src/math/sinhbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for sinhbf16 ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_SINHBF16_H
+#define LLVM_LIBC_SRC_MATH_SINHBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 sinhbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SINHBF16_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 74eda7cdaf065e..927b99faa8e6ff 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -471,6 +471,7 @@ else()
libc.src.__support.math.sincos
libc.src.__support.math.sincosf
libc.src.__support.math.sinf
+ libc.src.__support.math.sinbf16
libc.src.__support.math.sinf16
libc.src.__support.math.sinhf
libc.src.__support.math.sinhf16
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 7b785331f36c6a..147772b30c6f7b 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -811,6 +811,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::expbf16(bfloat16(0.0)));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::log_bf16(bfloat16(1.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::sinhbf16(bfloat16(0.0)));
bfloat16 neg_min_denormal = FPBits::min_subnormal(Sign::NEG).get_val();
EXPECT_FP_EQ(neg_min_denormal,
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 8f1a543de0a5e7..72dd7350ca286d 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2612,6 +2612,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ sinhbf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ sinhbf16_test.cpp
+ DEPENDS
+ libc.src.math.sinhbf16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
sinhf16_test
NEED_MPFR
diff --git a/libc/test/src/math/sinhbf16_test.cpp b/libc/test/src/math/sinhbf16_test.cpp
new file mode 100644
index 00000000000000..f5115d48547f46
--- /dev/null
+++ b/libc/test/src/math/sinhbf16_test.cpp
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains exhaustive tests for bfloat16 sinh.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/expbf16.h"
+#include "src/math/sinhbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcSinhBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// range: [0, inf]
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7f80U;
+
+// range: [-0, -inf]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcSinhBf16Test, PositiveRange) {
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinh, x,
+ LIBC_NAMESPACE::sinhbf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcSinhBf16Test, NegativeRange) {
+ for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinh, x,
+ LIBC_NAMESPACE::sinhbf16(x), 0.5);
+ }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index a61b854a85fb2b..6e45b79c26a4d2 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -4763,6 +4763,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ sinhbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ sinhbf16_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.sinhbf16
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
sinhf16_test
SUITE
diff --git a/libc/test/src/math/smoke/sinhbf16_test.cpp b/libc/test/src/math/smoke/sinhbf16_test.cpp
new file mode 100644
index 00000000000000..69d6959ef8a7c3
--- /dev/null
+++ b/libc/test/src/math/smoke/sinhbf16_test.cpp
@@ -0,0 +1,89 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains unit tests for bfloat16 sinh.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/sinhbf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+class LlvmLibcSinhBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+ DECLARE_SPECIAL_CONSTANTS(bfloat16)
+public:
+ void test_special_numbers() {
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::sinhbf16(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::sinhbf16(sNaN),
+ FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::sinhbf16(inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, LIBC_NAMESPACE::sinhbf16(neg_inf));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::sinhbf16(zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::sinhbf16(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+ }
+
+ // For small values, sinh(x) is x.
+ void test_small_values() {
+ bfloat16 x = FPBits(uint16_t(0x3de8)).get_val();
+ bfloat16 result = LIBC_NAMESPACE::sinhbf16(x);
+ EXPECT_FP_EQ(x, result);
+
+ x = FPBits(uint16_t(0xbde8)).get_val();
+ result = LIBC_NAMESPACE::sinhbf16(x);
+ EXPECT_FP_EQ(x, result);
+
+ x = FPBits(uint16_t(0x0001)).get_val();
+ result = LIBC_NAMESPACE::sinhbf16(x);
+ EXPECT_FP_EQ(x, result);
+
+ x = FPBits(uint16_t(0x8001)).get_val();
+ result = LIBC_NAMESPACE::sinhbf16(x);
+ EXPECT_FP_EQ(x, result);
+ }
+
+ void test_overflow() {
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ inf, LIBC_NAMESPACE::sinhbf16(FPBits(uint16_t(0x42b3)).get_val()),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::sinhbf16(max_normal),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ neg_inf, LIBC_NAMESPACE::sinhbf16(FPBits(uint16_t(0xc2b3)).get_val()),
+ FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ neg_inf, LIBC_NAMESPACE::sinhbf16(neg_max_normal), FE_OVERFLOW);
+ EXPECT_MATH_ERRNO(ERANGE);
+ }
+};
+
+TEST_F(LlvmLibcSinhBf16Test, SpecialNumbers) { test_special_numbers(); }
+TEST_F(LlvmLibcSinhBf16Test, SmallValues) { test_small_values(); }
+TEST_F(LlvmLibcSinhBf16Test, Overflow) { test_overflow(); }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index cdbf40b3e81a24..66832c972a978d 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -10610,6 +10610,19 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_sinhbf16",
+ hdrs = ["src/__support/math/sinhbf16.h"],
+ deps = [
+ ":__support_fputil_bfloat16",
+ ":__support_fputil_cast",
+ ":__support_fputil_fp_bits",
+ ":__support_macros_config",
+ ":__support_macros_optimization",
+ ":__support_sincosf_utils",
+ ],
+)
+
libc_support_library(
name = "__support_math_sinf16",
hdrs = ["src/__support/math/sinf16.h"],
@@ -14621,6 +14634,13 @@ libc_math_function(
additional_deps = [":__support_math_sinhf"],
)
+libc_math_function(
+ name = "sinhbf16",
+ additional_deps = [
+ ":__support_math_sinhbf16",
+ ],
+)
+
libc_math_function(
name = "sinhf16",
additional_deps = [
>From c0567c7fe905c459df5ea9cb1d2e44cf60561f3f Mon Sep 17 00:00:00 2001
From: Dylan Fernando <dylanf123 at gmail.com>
Date: Sun, 20 Sep 2026 23:02:49 +1000
Subject: [PATCH 2/4] new header template
---
libc/shared/math/sinhbf16.h | 7 ++++++-
libc/src/math/generic/sinhbf16.cpp | 9 +++++++--
libc/src/math/sinhbf16.h | 7 ++++++-
3 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/libc/shared/math/sinhbf16.h b/libc/shared/math/sinhbf16.h
index a4255838a5d917..0c31f307ba789e 100644
--- a/libc/shared/math/sinhbf16.h
+++ b/libc/shared/math/sinhbf16.h
@@ -1,10 +1,15 @@
-//===-- Shared sinhbf16 function --------------------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the shared sinhbf16(x) function.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SHARED_MATH_SINHBF16_H
#define LLVM_LIBC_SHARED_MATH_SINHBF16_H
diff --git a/libc/src/math/generic/sinhbf16.cpp b/libc/src/math/generic/sinhbf16.cpp
index eaab609360752f..2a09a0dc152821 100644
--- a/libc/src/math/generic/sinhbf16.cpp
+++ b/libc/src/math/generic/sinhbf16.cpp
@@ -1,10 +1,15 @@
-//===-- Implementation for sinhbf16(x) function ---------------------------===//
+//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation for sinhbf16(x) function.
+///
+//===----------------------------------------------------------------------===//
#include "src/math/sinhbf16.h"
#include "src/__support/math/sinhbf16.h"
diff --git a/libc/src/math/sinhbf16.h b/libc/src/math/sinhbf16.h
index 91f30cb68f2469..11c7f659984869 100644
--- a/libc/src/math/sinhbf16.h
+++ b/libc/src/math/sinhbf16.h
@@ -1,10 +1,15 @@
-//===-- Implementation header for sinhbf16 ----------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for sinhbf16.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_MATH_SINHBF16_H
#define LLVM_LIBC_SRC_MATH_SINHBF16_H
>From 960a5713a0dd577fe0a4d77c4eb204b4cb562648 Mon Sep 17 00:00:00 2001
From: Dylan Fernando <dylanf123 at gmail.com>
Date: Sun, 20 Sep 2026 23:17:30 +1000
Subject: [PATCH 3/4] add test files to bazel
---
.../llvm-project-overlay/libc/test/src/math/BUILD.bazel | 7 +++++++
.../libc/test/src/math/smoke/BUILD.bazel | 7 +++++++
2 files changed, 14 insertions(+)
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
index 8e5706c085f39f..cf48d85df48479 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
@@ -773,6 +773,13 @@ math_mpfr_test(
],
)
+math_mpfr_test(
+ name = "sinhbf16",
+ deps = [
+ "//libc:__support_fputil_bfloat16",
+ ],
+)
+
math_mpfr_test(
name = "sinpif",
hdrs = ["sdcomp26094.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
index e1d9305617303e..f9a75a5c3252d5 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/math/smoke/BUILD.bazel
@@ -2099,6 +2099,13 @@ math_test(name = "sincos")
math_test(name = "sincosf")
+math_test(
+ name = "sinhbf16",
+ deps = [
+ "//libc:__support_fputil_bfloat16",
+ ],
+)
+
math_test(
name = "sinhf",
deps = [
>From 1fbfe996f271a76921c904db35d145542911dece Mon Sep 17 00:00:00 2001
From: Dylan Fernando <dylanf123 at gmail.com>
Date: Wed, 23 Sep 2026 23:29:26 +1000
Subject: [PATCH 4/4] make sinhbf16 constexpr, use float for intermediate
calculation
---
libc/src/__support/math/CMakeLists.txt | 13 +-
libc/src/__support/math/sinhbf16.h | 21 ++-
.../__support/math/sinhbf16coshbf16_utils.h | 170 ++++++++++++++++++
.../llvm-project-overlay/libc/BUILD.bazel | 14 ++
4 files changed, 213 insertions(+), 5 deletions(-)
create mode 100644 libc/src/__support/math/sinhbf16coshbf16_utils.h
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 58339ad1264011..ea74945e597c1a 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -4932,6 +4932,17 @@ add_header_library(
libc.src.__support.common
)
+add_header_library(
+ sinhbf16coshbf16_utils
+ HDRS
+ sinhbf16coshbf16_utils.h
+ DEPENDS
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.nearest_integer
+ libc.src.__support.FPUtil.multiply_add
+)
+
add_header_library(
sinhfcoshf_utils
HDRS
@@ -5375,7 +5386,7 @@ add_header_library(
HDRS
sinhbf16.h
DEPENDS
- .sinhfcoshf_utils
+ .sinhbf16coshbf16_utils
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.cast
libc.src.__support.macros.config
diff --git a/libc/src/__support/math/sinhbf16.h b/libc/src/__support/math/sinhbf16.h
index 6720bd4a265bd2..300d3a4685423d 100644
--- a/libc/src/__support/math/sinhbf16.h
+++ b/libc/src/__support/math/sinhbf16.h
@@ -14,7 +14,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16_H
-#include "sinhfcoshf_utils.h"
+#include "sinhbf16coshbf16_utils.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/FPUtil/cast.h"
@@ -22,12 +22,11 @@
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
-
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 sinhbf16(bfloat16 x) {
+LIBC_INLINE constexpr bfloat16 sinhbf16(bfloat16 x) {
using FPBits = fputil::FPBits<bfloat16>;
FPBits x_bits(x);
uint16_t x_u = x_bits.uintval();
@@ -85,8 +84,22 @@ LIBC_INLINE bfloat16 sinhbf16(bfloat16 x) {
}
float xf = static_cast<float>(x);
+
+ // |x| >= 6.875
+ // return e^x / 2
+ if (x_abs >= 0x40dc) {
+ uint32_t x_abs_bits = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
+ float x_abs_f = fputil::FPBits<float>(x_abs_bits).get_val();
+ float abs_result = math::sinhbf16coshbf16_internal::exp_half(x_abs_f);
+ if (x_u & 0x8000) {
+ abs_result = -abs_result;
+ }
+ return fputil::cast<bfloat16>(abs_result);
+ }
+
+ // sinh(x) = (e^x - e^(-x)) / 2.
float result = static_cast<float>(
- math::sinhfcoshf_internal::exp_pm_eval</*is_sinh*/ true>(xf));
+ math::sinhbf16coshbf16_internal::eval_sinh_or_cosh</*is_sinh*/ true>(xf));
return fputil::cast<bfloat16>(result);
}
diff --git a/libc/src/__support/math/sinhbf16coshbf16_utils.h b/libc/src/__support/math/sinhbf16coshbf16_utils.h
new file mode 100644
index 00000000000000..4e51813d7e5c74
--- /dev/null
+++ b/libc/src/__support/math/sinhbf16coshbf16_utils.h
@@ -0,0 +1,170 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// General sinh/cosh functions for bfloat16.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16COSHBF16_UTILS_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16COSHBF16_UTILS_H
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace sinhbf16coshbf16_internal {
+
+LIBC_INLINE_VAR constexpr float LOG2F_E = 0x1.715476p+0f;
+
+LIBC_INLINE_VAR constexpr float LOGF_2 = 0x1.62e43p-1f;
+
+LIBC_INLINE_VAR constexpr cpp::array<uint32_t, 32> EXP2_MID_5_BITS = {
+ 0x3f80'0000U, 0x3f82'cd87U, 0x3f85'aac3U, 0x3f88'980fU, 0x3f8b'95c2U,
+ 0x3f8e'a43aU, 0x3f91'c3d3U, 0x3f94'f4f0U, 0x3f98'37f0U, 0x3f9b'8d3aU,
+ 0x3f9e'f532U, 0x3fa2'7043U, 0x3fa5'fed7U, 0x3fa9'a15bU, 0x3fad'583fU,
+ 0x3fb1'23f6U, 0x3fb5'04f3U, 0x3fb8'fbafU, 0x3fbd'08a4U, 0x3fc1'2c4dU,
+ 0x3fc5'672aU, 0x3fc9'b9beU, 0x3fce'248cU, 0x3fd2'a81eU, 0x3fd7'44fdU,
+ 0x3fdb'fbb8U, 0x3fe0'ccdfU, 0x3fe5'b907U, 0x3fea'c0c7U, 0x3fef'e4baU,
+ 0x3ff5'257dU, 0x3ffa'83b3U,
+};
+
+// This function correctly calculates sinh(x) and cosh(x) by calculating exp(x)
+// and exp(-x) simultaneously.
+// To compute e^x, we perform the following range reduction:
+// find hi, mid, lo such that:
+// x = (hi + mid) * log(2) + lo, in which
+// hi is an integer,
+// 0 <= mid * 2^5 < 32 is an integer
+// -2^(-5) <= lo * log2(e) <= 2^-5.
+// In particular,
+// hi + mid = round(x * log2(e) * 2^5) * 2^(-5).
+// Then,
+// e^x = 2^(hi + mid) * e^lo = 2^hi * 2^mid * e^lo.
+// We store 2^mid in the lookup table EXP2_MID_5_BITS, and compute 2^hi * 2^mid
+// by adding hi to the exponent field of 2^mid.
+// e^lo is computed using a degree-3 minimax polynomial generated by Sollya:
+// e^lo ~ P(lo)
+// = 1 + lo + c2 * lo^2 + ... + c5 * lo^5
+// = (1 + c2*lo^2 + c4*lo^4) + lo * (1 + c3*lo^2 + c5*lo^4)
+// = P_even + lo * P_odd
+// To compute e^(-x), notice that:
+// e^(-x) = 2^(-(hi + mid)) * e^(-lo)
+// ~ 2^(-(hi + mid)) * P(-lo)
+// = 2^(-(hi + mid)) * (P_even - lo * P_odd)
+// So:
+// sinh(x) = (e^x - e^(-x)) / 2
+// ~ 0.5 * (2^(hi + mid) * (P_even + lo * P_odd) -
+// 2^(-(hi + mid)) * (P_even - lo * P_odd))
+// = 0.5 * (P_even * (2^(hi + mid) - 2^(-(hi + mid))) +
+// lo * P_odd * (2^(hi + mid) + 2^(-(hi + mid))))
+// And similarly:
+// cosh(x) = (e^x + e^(-x)) / 2
+// ~ 0.5 * (P_even * (2^(hi + mid) + 2^(-(hi + mid))) +
+// lo * P_odd * (2^(hi + mid) - 2^(-(hi + mid))))
+// The main point of these formulas is that the expensive part of calculating
+// the polynomials approximating lower parts of e^x and e^(-x) is shared and
+// only done once.
+template <bool IsSinh> LIBC_INLINE constexpr float eval_sinh_or_cosh(float xf) {
+ float kf = fputil::nearest_integer(xf * (LOG2F_E * 0x1.0p+5f));
+ int x_hi_mid_p = static_cast<int>(kf);
+ int x_hi_mid_m = -x_hi_mid_p;
+
+ unsigned x_hi_p = static_cast<unsigned>(x_hi_mid_p) >> 5;
+ unsigned x_hi_m = static_cast<unsigned>(x_hi_mid_m) >> 5;
+ unsigned x_mid_p = static_cast<unsigned>(x_hi_mid_p) & 0x1f;
+ unsigned x_mid_m = static_cast<unsigned>(x_hi_mid_m) & 0x1f;
+
+ uint32_t exp2_hi_mid_bits_p =
+ EXP2_MID_5_BITS[x_mid_p] +
+ static_cast<uint32_t>(x_hi_p << fputil::FPBits<float>::FRACTION_LEN);
+ uint32_t exp2_hi_mid_bits_m =
+ EXP2_MID_5_BITS[x_mid_m] +
+ static_cast<uint32_t>(x_hi_m << fputil::FPBits<float>::FRACTION_LEN);
+ // exp2_hi_mid_p = 2^(hi + mid)
+ float exp2_hi_mid_p = fputil::FPBits<float>(exp2_hi_mid_bits_p).get_val();
+ // exp2_hi_mid_m = 2^(-(hi + mid))
+ float exp2_hi_mid_m = fputil::FPBits<float>(exp2_hi_mid_bits_m).get_val();
+
+ // exp2_hi_mid_sum = 2^(hi + mid) + 2^(-(hi + mid))
+ float exp2_hi_mid_sum = exp2_hi_mid_p + exp2_hi_mid_m;
+ // exp2_hi_mid_diff = 2^(hi + mid) - 2^(-(hi + mid))
+ float exp2_hi_mid_diff = exp2_hi_mid_p - exp2_hi_mid_m;
+
+ // lo = x - (hi + mid) = round(x * log2(e) * 2^5) * log(2) * (-2^(-5)) + x
+ float lo = fputil::multiply_add(kf, LOGF_2 * -0x1.0p-5f, xf);
+ float lo_sq = lo * lo;
+
+ // Degree-3 minimax polynomial generated by Sollya with the following
+ // commands:
+ // > display = hexadecimal;
+ // > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
+ // > 1 + x * P;
+ constexpr cpp::array<float, 4> COEFFS = {0x1p+0f, 0x1p+0f, 0x1.0004p-1f,
+ 0x1.555778p-3f};
+ float half_p_odd =
+ fputil::polyeval(lo_sq, COEFFS[1] * 0.5f, COEFFS[3] * 0.5f);
+ float half_p_even =
+ fputil::polyeval(lo_sq, COEFFS[0] * 0.5f, COEFFS[2] * 0.5f);
+
+ // sinh(x) = lo * (0.5 * P_odd * (2^(hi + mid) + 2^(-(hi + mid)))) +
+ // (0.5 * P_even * (2^(hi + mid) - 2^(-(hi + mid))))
+ if constexpr (IsSinh)
+ return fputil::multiply_add(lo, half_p_odd * exp2_hi_mid_sum,
+ half_p_even * exp2_hi_mid_diff);
+ // cosh(x) = lo * (0.5 * P_odd * (2^(hi + mid) - 2^(-(hi + mid)))) +
+ // (0.5 * P_even * (2^(hi + mid) + 2^(-(hi + mid))))
+ return fputil::multiply_add(lo, half_p_odd * exp2_hi_mid_diff,
+ half_p_even * exp2_hi_mid_sum);
+}
+
+// Calculate e^x / 2
+LIBC_INLINE constexpr float exp_half(float xf) {
+ float kf = fputil::nearest_integer(xf * (LOG2F_E * 0x1.0p+5f));
+ int x_hi_mid_p = static_cast<int>(kf);
+
+ unsigned x_hi_p = (static_cast<unsigned>(x_hi_mid_p) >> 5) - 1;
+ unsigned x_mid_p = static_cast<unsigned>(x_hi_mid_p) & 0x1f;
+
+ uint32_t exp2_hi_mid_bits_p =
+ EXP2_MID_5_BITS[x_mid_p] +
+ static_cast<uint32_t>(x_hi_p << fputil::FPBits<float>::FRACTION_LEN);
+
+ // exp2_hi_mid_p = 2^(hi + mid - 1)
+ float exp2_hi_mid_p = fputil::FPBits<float>(exp2_hi_mid_bits_p).get_val();
+
+ // lo = x - (hi + mid) = round(x * log2(e) * 2^5) * log(2) * (-2^(-5)) + x
+ float lo = fputil::multiply_add(kf, LOGF_2 * -0x1.0p-5f, xf);
+ float lo_sq = lo * lo;
+
+ constexpr cpp::array<float, 4> COEFFS = {0x1p+0f, 0x1p+0f, 0x1.0004p-1f,
+ 0x1.555778p-3f};
+
+ float p_odd = fputil::polyeval(lo_sq, COEFFS[1], COEFFS[3]);
+ float p_even = fputil::polyeval(lo_sq, COEFFS[0], COEFFS[2]);
+
+ float exp_lo = p_even + lo * p_odd;
+
+ // exp(x) / 2 = 2^(hi + mid - 1) * exp(lo).
+ float result = exp2_hi_mid_p * exp_lo;
+ return result;
+}
+
+} // namespace sinhbf16coshbf16_internal
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINHBF16COSHBF16_UTILS_H
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 66832c972a978d..94aa7d1f077450 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -10620,6 +10620,7 @@ libc_support_library(
":__support_macros_config",
":__support_macros_optimization",
":__support_sincosf_utils",
+ ":__support_math_sinhbf16coshbf16_utils",
],
)
@@ -10669,6 +10670,19 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_sinhbf16coshbf16_utils",
+ hdrs = ["src/__support/math/sinhbf16coshbf16_utils.h"],
+ deps = [
+ ":__support_fputil_cast",
+ ":__support_fputil_fp_bits",
+ ":__support_fputil_multiply_add",
+ ":__support_fputil_nearest_integer",
+ ":__support_macros_attributes",
+ ":__support_macros_config",
+ ],
+)
+
libc_support_library(
name = "__support_math_sqrt",
hdrs = ["src/__support/math/sqrt.h"],
More information about the libc-commits
mailing list