[libc-commits] [libc] [llvm] [libc][math] Add tanhbf16 function (PR #227109)

via libc-commits libc-commits at lists.llvm.org
Sat Nov 14 00:40:43 PST 2026


https://github.com/mooofin updated https://github.com/llvm/llvm-project/pull/227109

>From 109eb3147c49910041ba1f499981613ca662cbb9 Mon Sep 17 00:00:00 2001
From: mooofin <mooofin at users.noreply.github.com>
Date: Fri, 13 Nov 2026 21:23:21 +0530
Subject: [PATCH] synced

---
 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/shared/math.h                            |   1 +
 libc/shared/math/tanhbf16.h                   |  23 ++++
 libc/src/__support/math/CMakeLists.txt        |  19 ++++
 libc/src/__support/math/tanhbf16.h            | 104 ++++++++++++++++++
 libc/src/math/CMakeLists.txt                  |   1 +
 libc/src/math/generic/CMakeLists.txt          |  10 ++
 libc/src/math/generic/tanhbf16.cpp            |  19 ++++
 libc/src/math/tanhbf16.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/smoke/CMakeLists.txt       |  13 +++
 libc/test/src/math/smoke/tanhbf16_test.cpp    |  69 ++++++++++++
 libc/test/src/math/tanhbf16_test.cpp          |  33 ++++++
 .../llvm-project-overlay/libc/BUILD.bazel     |  27 +++++
 .../libc/test/src/math/BUILD.bazel            |   7 ++
 .../libc/test/src/math/smoke/BUILD.bazel      |   7 ++
 30 files changed, 382 insertions(+)
 create mode 100644 libc/shared/math/tanhbf16.h
 create mode 100644 libc/src/__support/math/tanhbf16.h
 create mode 100644 libc/src/math/generic/tanhbf16.cpp
 create mode 100644 libc/src/math/tanhbf16.h
 create mode 100644 libc/test/src/math/smoke/tanhbf16_test.cpp
 create mode 100644 libc/test/src/math/tanhbf16_test.cpp

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 1c2c46869cb3fa..37eea60f31ce32 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -904,6 +904,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 5d8de6562f9e98..e9628b0529536e 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -915,6 +915,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index c21149a19dde81..0337cba9537f71 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -907,6 +907,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index b3b033e8907939..5065a8199fd728 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -726,6 +726,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index d42cb3bb5bc623..0ea298a3323428 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -368,6 +368,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 85a18879b86fb7..c3aeb6002f6e4e 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -658,6 +658,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 13792b307c5e75..04a92e1c959d3b 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -792,6 +792,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 493eb07c8f526e..426a936555b7d3 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -808,6 +808,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index aed6852ff9b06d..2c850f3df176d4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1154,6 +1154,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 77edf1dc5d071e..3df7055358d31e 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -763,6 +763,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index dda4f01e369787..fefd55fa23d874 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1256,6 +1256,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index eb5dcd44c8fb72..b0057ac367824b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1256,6 +1256,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 6cb1e28e72280d..15cf65baee6aa8 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -469,6 +469,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.sinhbf16
   libc.src.math.sqrtbf16
   libc.src.math.tanbf16
+  libc.src.math.tanhbf16
   libc.src.math.truncbf16
   libc.src.math.totalorderbf16
   libc.src.math.totalordermagbf16
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 56654666305c52..64bceaf4b44c7a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -505,6 +505,7 @@
 #include "math/tanf.h"
 #include "math/tanf16.h"
 #include "math/tanh.h"
+#include "math/tanhbf16.h"
 #include "math/tanhf.h"
 #include "math/tanhf16.h"
 #include "math/tanpif.h"
diff --git a/libc/shared/math/tanhbf16.h b/libc/shared/math/tanhbf16.h
new file mode 100644
index 00000000000000..89d7dd788e1cbe
--- /dev/null
+++ b/libc/shared/math/tanhbf16.h
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_TANHBF16_H
+#define LLVM_LIBC_SHARED_MATH_TANHBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/tanhbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::tanhbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_TANHBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c78bab1e45b8aa..7fc375197c8776 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -6388,6 +6388,25 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  tanhbf16
+  HDRS
+    tanhbf16.h
+  DEPENDS
+    .exp10f_utils
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.nearest_integer
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.FPUtil.rounding_mode
+    libc.src.__support.macros.config
+    libc.src.__support.macros.optimization
+)
+
 add_header_library(
   tanhf
   HDRS
diff --git a/libc/src/__support/math/tanhbf16.h b/libc/src/__support/math/tanhbf16.h
new file mode 100644
index 00000000000000..5bf13f5f586788
--- /dev/null
+++ b/libc/src/__support/math/tanhbf16.h
@@ -0,0 +1,104 @@
+//===----------------------------------------------------------------------===//
+//
+// 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___SUPPORT_MATH_TANHBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_TANHBF16_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/math/exp10f_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE bfloat16 tanhbf16(bfloat16 x) {
+  using FPBits = fputil::FPBits<bfloat16>;
+  FPBits x_bits(x);
+  uint16_t x_u = x_bits.uintval();
+  uint16_t x_abs = x_u & 0x7fffU;
+
+  if (LIBC_UNLIKELY(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;
+  }
+
+  // tanh(+/-inf) = +/-1, and tanh(+/-0) = +/-0.
+  if (LIBC_UNLIKELY(x_bits.is_inf()))
+    return FPBits::one(x_bits.sign()).get_val();
+  if (LIBC_UNLIKELY(x_abs == 0))
+    return x;
+
+  // For |x| <= 0x1.7p-4, tanh(x) rounds to x in round-to-nearest.  For
+  // directed rounding, its magnitude is strictly smaller than |x|.
+  if (LIBC_UNLIKELY(x_abs <= 0x3db8U)) {
+    bool toward_zero = false;
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+    int rounding_mode = fputil::quick_get_round();
+    toward_zero = rounding_mode == FE_TOWARDZERO ||
+                  (rounding_mode == FE_DOWNWARD && x_bits.is_pos()) ||
+                  (rounding_mode == FE_UPWARD && x_bits.is_neg());
+#endif
+    uint16_t result = toward_zero ? static_cast<uint16_t>(x_u - 1U) : x_u;
+    int exceptions = FE_INEXACT;
+    if ((result & 0x7fffU) < 0x0080U)
+      exceptions |= FE_UNDERFLOW;
+    fputil::raise_except_if_required(exceptions);
+    return FPBits(result).get_val();
+  }
+
+  // 0x1.bcp+1 is the first bfloat16 input whose tanh rounds to 1 in
+  // round-to-nearest.  The result remains below 1 for every finite input.
+  if (LIBC_UNLIKELY(x_abs >= 0x405eU)) {
+    fputil::raise_except_if_required(FE_INEXACT);
+    bool round_to_one = true;
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+    int rounding_mode = fputil::quick_get_round();
+    round_to_one = rounding_mode == FE_TONEAREST ||
+                   (rounding_mode == FE_UPWARD && x_bits.is_pos()) ||
+                   (rounding_mode == FE_DOWNWARD && x_bits.is_neg());
+#endif
+    if (round_to_one)
+      return FPBits::one(x_bits.sign()).get_val();
+    return FPBits(static_cast<uint16_t>((x_u & 0x8000U) | 0x3f7fU)).get_val();
+  }
+
+  double xd = static_cast<double>(static_cast<float>(x));
+  // k = round(x * log2(e) * 2^6)
+  double k = fputil::nearest_integer(xd * (ExpBase::LOG2_B * 2.0));
+  int mk = -static_cast<int>(k);
+  int64_t exp_mhi = static_cast<int64_t>(mk >> ExpBase::MID_BITS)
+                    << fputil::FPBits<double>::FRACTION_LEN;
+  // mh = 2^(-hi - mid)
+  double mh = fputil::FPBits<double>(
+                  static_cast<uint64_t>(
+                      ExpBase::EXP_2_MID[mk & ExpBase::MID_MASK] + exp_mhi))
+                  .get_val();
+  // lo = x - k * log(2) * 2^(-6)
+  double lo = fputil::multiply_add(k, ExpBase::M_LOGB_2_HI * 0.5, xd);
+  // Degree-3 Taylor polynomial of e^(2*lo).
+  double exp_2lo =
+      fputil::polyeval(lo, 0x1p+0, 0x1p+1, 0x1p+1, 0x1.5555555555555p+0);
+  return fputil::cast<bfloat16>((exp_2lo - mh) / (exp_2lo + mh));
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANHBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 88fca3c6b572f7..69b3ebdfb2c523 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -618,6 +618,7 @@ add_math_entrypoint_object(tanf)
 add_math_entrypoint_object(tanf16)
 
 add_math_entrypoint_object(tanh)
+add_math_entrypoint_object(tanhbf16)
 add_math_entrypoint_object(tanhf)
 add_math_entrypoint_object(tanhf16)
 
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 0b3881dd00fa2f..0c4ffcc77fb747 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4001,6 +4001,16 @@ add_entrypoint_object(
     libc.src.__support.math.tanh
 )
 
+add_entrypoint_object(
+  tanhbf16
+  SRCS
+    tanhbf16.cpp
+  HDRS
+    ../tanhbf16.h
+  DEPENDS
+    libc.src.__support.math.tanhbf16
+)
+
 add_entrypoint_object(
   tanhf
   SRCS
diff --git a/libc/src/math/generic/tanhbf16.cpp b/libc/src/math/generic/tanhbf16.cpp
new file mode 100644
index 00000000000000..ae5ea481adbde0
--- /dev/null
+++ b/libc/src/math/generic/tanhbf16.cpp
@@ -0,0 +1,19 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/tanhbf16.h"
+#include "src/__support/common.h"
+#include "src/__support/math/tanhbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, tanhbf16, (bfloat16 x)) {
+  return math::tanhbf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/tanhbf16.h b/libc/src/math/tanhbf16.h
new file mode 100644
index 00000000000000..5415c7b4eabd39
--- /dev/null
+++ b/libc/src/math/tanhbf16.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_TANHBF16_H
+#define LLVM_LIBC_SRC_MATH_TANHBF16_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 tanhbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_TANHBF16_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7d368e77523611..989474b47e590d 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -529,6 +529,7 @@ else()
       libc.src.__support.math.tanf
       libc.src.__support.math.tanf16
       libc.src.__support.math.tanh
+      libc.src.__support.math.tanhbf16
       libc.src.__support.math.tanhf
       libc.src.__support.math.tanhf16
       libc.src.__support.math.tanpif
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 37244fe000f55f..40b37fdbff56f6 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -963,6 +963,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_FP_EQ(bfloat16(0.0),
                LIBC_NAMESPACE::shared::roundevenbf16(bfloat16(0.0)));
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::tanbf16(bfloat16(0.0)));
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::tanhbf16(bfloat16(0.0)));
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::truncbf16(bfloat16(0.0)));
 }
 
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 0c138178fbee93..03e685db6a5bbf 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2815,6 +2815,19 @@ add_fp_unittest(
     libc.src.__support.macros.optimization
 )
 
+add_fp_unittest(
+  tanhbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-tests
+  SRCS
+    tanhbf16_test.cpp
+  DEPENDS
+    libc.src.math.tanhbf16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   tanhf_test
   NEED_MPFR
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 76afa547166d6e..dad6e3f68d6086 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -5117,6 +5117,19 @@ add_fp_unittest(
     libc.src.__support.macros.optimization
 )
 
+add_fp_unittest(
+  tanhbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    tanhbf16_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.math.tanhbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   tanhf_test
   SUITE
diff --git a/libc/test/src/math/smoke/tanhbf16_test.cpp b/libc/test/src/math/smoke/tanhbf16_test.cpp
new file mode 100644
index 00000000000000..38490e7e59c072
--- /dev/null
+++ b/libc/test/src/math/smoke/tanhbf16_test.cpp
@@ -0,0 +1,69 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/tanhbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcTanhBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+TEST_F(LlvmLibcTanhBf16Test, SpecialNumbers) {
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::tanhbf16(aNaN));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::tanhbf16(sNaN),
+                                           FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(1.0f), LIBC_NAMESPACE::tanhbf16(inf));
+  EXPECT_FP_EQ_ALL_ROUNDING(bfloat16(-1.0f), LIBC_NAMESPACE::tanhbf16(neg_inf));
+  EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::tanhbf16(zero));
+  EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::tanhbf16(neg_zero));
+  EXPECT_MATH_ERRNO(0);
+}
+
+TEST_F(LlvmLibcTanhBf16Test, SmallAndLargeValues) {
+  bfloat16 x = FPBits(uint16_t(0x3d80U)).get_val(); // 0.0625
+  bfloat16 below_x = FPBits(uint16_t(0x3d7fU)).get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(x, LIBC_NAMESPACE::tanhbf16(x),
+                                               FE_INEXACT);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      below_x, LIBC_NAMESPACE::tanhbf16(x), FE_INEXACT);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      bfloat16(1.0f), LIBC_NAMESPACE::tanhbf16(max_normal), FE_INEXACT);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      FPBits(uint16_t(0x3f7fU)).get_val(), LIBC_NAMESPACE::tanhbf16(max_normal),
+      FE_INEXACT);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      FPBits(uint16_t(0xbf7fU)).get_val(),
+      LIBC_NAMESPACE::tanhbf16(neg_max_normal), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+}
+
+TEST_F(LlvmLibcTanhBf16Test, TinyValues) {
+  bfloat16 smallest = FPBits(uint16_t(0x0001U)).get_val();
+  bfloat16 neg_smallest = FPBits(uint16_t(0x8001U)).get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      smallest, LIBC_NAMESPACE::tanhbf16(smallest), FE_UNDERFLOW | FE_INEXACT);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      zero, LIBC_NAMESPACE::tanhbf16(smallest), FE_UNDERFLOW | FE_INEXACT);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      neg_zero, LIBC_NAMESPACE::tanhbf16(neg_smallest),
+      FE_UNDERFLOW | FE_INEXACT);
+
+  bfloat16 min_normal = FPBits(uint16_t(0x0080U)).get_val();
+  bfloat16 max_subnormal = FPBits(uint16_t(0x007fU)).get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      max_subnormal, LIBC_NAMESPACE::tanhbf16(min_normal),
+      FE_UNDERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+}
diff --git a/libc/test/src/math/tanhbf16_test.cpp b/libc/test/src/math/tanhbf16_test.cpp
new file mode 100644
index 00000000000000..de734dbf1b62c5
--- /dev/null
+++ b/libc/test/src/math/tanhbf16_test.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/__support/FPUtil/bfloat16.h"
+#include "src/math/tanhbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcTanhBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcTanhBf16Test, PositiveRange) {
+  for (uint32_t v = 0; v <= 0x7f80U; ++v) {
+    bfloat16 x = FPBits(static_cast<uint16_t>(v)).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, x,
+                                   LIBC_NAMESPACE::tanhbf16(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcTanhBf16Test, NegativeRange) {
+  for (uint32_t v = 0x8000U; v <= 0xff80U; ++v) {
+    bfloat16 x = FPBits(static_cast<uint16_t>(v)).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, x,
+                                   LIBC_NAMESPACE::tanhbf16(x), 0.5);
+  }
+}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 9f5fc451f9ac8c..c2f18bdf1be473 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -13333,6 +13333,25 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_tanhbf16",
+    hdrs = ["src/__support/math/tanhbf16.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_cast",
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_multiply_add",
+        ":__support_fputil_nearest_integer",
+        ":__support_fputil_polyeval",
+        ":__support_fputil_rounding_mode",
+        ":__support_macros_config",
+        ":__support_macros_optimization",
+        ":__support_math_exp10f_utils",
+        ":hdr_fenv_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_tanhf",
     hdrs = ["src/__support/math/tanhf.h"],
@@ -17086,6 +17105,14 @@ libc_math_function(
     additional_deps = [":__support_math_tanh"],
 )
 
+libc_math_function(
+    name = "tanhbf16",
+    additional_deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_math_tanhbf16",
+    ],
+)
+
 libc_math_function(
     name = "tanhf",
     additional_deps = [
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 eb2281762a7072..6660ce02e459f5 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
@@ -1235,6 +1235,13 @@ math_mpfr_test(
     ],
 )
 
+math_mpfr_test(
+    name = "tanhbf16",
+    deps = [
+        "//libc:__support_fputil_bfloat16",
+    ],
+)
+
 math_mpfr_test(name = "tanhf")
 
 math_mpfr_test(
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 2add50bddd902e..7e0c0ae527a193 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
@@ -2458,6 +2458,13 @@ math_test(
     ],
 )
 
+math_test(
+    name = "tanhbf16",
+    deps = [
+        "//libc:__support_fputil_bfloat16",
+    ],
+)
+
 math_test(name = "tanhf")
 
 math_test(name = "tanpif")



More information about the libc-commits mailing list