[libc-commits] [libc] [llvm] [libc][math] Refactor tanhf16 implementation to header-only in src/__support/math folder. (PR #178645)
Xinlong Chen via libc-commits
libc-commits at lists.llvm.org
Sat Feb 14 03:49:48 PST 2026
https://github.com/Xinlong-Chen updated https://github.com/llvm/llvm-project/pull/178645
>From 788ea179131148b8dca6927a39d20b3135410ee8 Mon Sep 17 00:00:00 2001
From: xinlongchen <xinlongchen at tencent.com>
Date: Thu, 29 Jan 2026 20:05:30 +0800
Subject: [PATCH 1/5] [libc][math] Refactor tanhf16 implementation to
header-only in src/__support/math folder.
---
libc/shared/math.h | 1 +
libc/shared/math/tanhf16.h | 23 +++
libc/src/__support/math/CMakeLists.txt | 19 ++
libc/src/__support/math/tanhf16.h | 163 ++++++++++++++++++
libc/src/math/generic/CMakeLists.txt | 12 +-
libc/src/math/generic/tanhf16.cpp | 140 +--------------
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 1 +
.../llvm-project-overlay/libc/BUILD.bazel | 10 +-
9 files changed, 220 insertions(+), 150 deletions(-)
create mode 100644 libc/shared/math/tanhf16.h
create mode 100644 libc/src/__support/math/tanhf16.h
diff --git a/libc/shared/math.h b/libc/shared/math.h
index b58d29d1ee480..8d10627c35ea5 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -105,5 +105,6 @@
#include "math/sqrtf16.h"
#include "math/tan.h"
#include "math/tanf.h"
+#include "math/tanhf16.h"
#endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/tanhf16.h b/libc/shared/math/tanhf16.h
new file mode 100644
index 0000000000000..47f3531d94ee1
--- /dev/null
+++ b/libc/shared/math/tanhf16.h
@@ -0,0 +1,23 @@
+//===-- Shared tanhf16 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_TANHF16_H
+#define LLVM_LIBC_SHARED_MATH_TANHF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/tanhf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::tanhf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_TANHF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 58e4040911f8e..a04b096f08f4f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1521,3 +1521,22 @@ add_header_library(
libc.src.__support.FPUtil.polyeval
libc.src.__support.macros.optimization
)
+
+add_header_library(
+ tanhf16
+ HDRS
+ tanhf16.h
+ DEPENDS
+ libc.hdr.fenv_macros
+ libc.src.__support.CPP.array
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.except_value_utils
+ 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.optimization
+ libc.src.__support.math.expxf16_utils
+)
diff --git a/libc/src/__support/math/tanhf16.h b/libc/src/__support/math/tanhf16.h
new file mode 100644
index 0000000000000..4761431443cfc
--- /dev/null
+++ b/libc/src/__support/math/tanhf16.h
@@ -0,0 +1,163 @@
+//===-- Single-precision tanhf16 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_TANHF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_TANHF16_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/CPP/array.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.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/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/math/expxf16_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace tanhf16_internal {
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, 2> TANHF16_EXCEPTS = {{
+ // x = 0x1.f54p+0, tanhf16(x) = 0x1.ecp-1 (RZ)
+ {0x3fd5U, 0x3bb0U, 1U, 0U, 0U},
+ // x = -0x1.f54p+0, tanhf16(x) = -0x1.ecp-1 (RZ)
+ {0xbfd5U, 0xbbb0U, 0U, 1U, 0U},
+}};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+} // namespace tanhf16_internal
+
+LIBC_INLINE constexpr float16 tanhf16(float16 x) {
+ using namespace tanhf16_internal;
+ using namespace math::expxf16_internal;
+ using FPBits = fputil::FPBits<float16>;
+ FPBits x_bits(x);
+
+ uint16_t x_u = x_bits.uintval();
+ uint16_t x_abs = x_u & 0x7fffU;
+
+ // When -2^(-14) <= x <= -2^(-9), or |x| <= 0x1.d2p-4,
+ // or |x| >= atanh(1 - 2^(-11)), or x is NaN.
+ if (LIBC_UNLIKELY(x_abs <= 0x2f48U || x_abs >= 0x4429U)) {
+ // tanh(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;
+ }
+
+ // When -2^(-14) <= x <= -2^(-9).
+ if (x_u >= 0x8400U && x_u <= 0x9800U) {
+ switch (fputil::quick_get_round()) {
+ case FE_TONEAREST:
+ case FE_DOWNWARD:
+ return x;
+ default:
+ return FPBits(static_cast<uint16_t>(x_u - 1U)).get_val();
+ }
+ }
+
+ // When |x| <= 0x1.d2p-4.
+ if (x_abs <= 0x2f48U) {
+ if (LIBC_UNLIKELY(x_abs == 0))
+ return x;
+
+ float xf = x;
+ float xf_sq = xf * xf;
+ // Degree-7 Taylor expansion generated by Sollya with the following
+ // commands:
+ // > taylor(tanh(x), 7, 0);
+ // > display = hexadecimal;
+ // > // For each coefficient:
+ // > round(/* put coefficient here */, SG, RN);
+ return fputil::cast<float16>(
+ xf * fputil::polyeval(xf_sq, 0x1p+0f, -0x1.555556p-2f, 0x1.111112p-3f,
+ -0x1.ba1ba2p-5f));
+ }
+
+ // tanh(+/-inf) = +/-1
+ if (x_bits.is_inf())
+ return FPBits::one(x_bits.sign()).get_val();
+
+ // When |x| >= atanh(1 - 2^(-11)).
+ fputil::raise_except_if_required(FE_INEXACT);
+
+ int rounding_mode = fputil::quick_get_round();
+ if ((rounding_mode == FE_TONEAREST && x_abs >= 0x4482U) ||
+ (rounding_mode == FE_UPWARD && x_bits.is_pos()) ||
+ (rounding_mode == FE_DOWNWARD && x_bits.is_neg())) {
+ return FPBits::one(x_bits.sign()).get_val();
+ }
+ if (x_bits.is_pos())
+ return fputil::cast<float16>(0x1.ffcp-1);
+ return fputil::cast<float16>(-0x1.ffcp-1);
+ }
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ if (auto r = TANHF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+ // For atanh(-1 + 2^(-11)) < x < atanh(1 - 2^(-11)), to compute tanh(x), we
+ // perform the following range reduction: find hi, mid, lo, such that:
+ // x = (hi + mid) * log(2) * 0.5 + lo, in which
+ // hi is an integer,
+ // mid * 2^5 is an integer,
+ // -2^(-5) <= lo < 2^(-5).
+ // In particular,
+ // hi + mid = round(x * log2(e) * 2 * 2^5) * 2^(-5).
+ // Then,
+ // tanh(x) = sinh(x)/cosh(x)
+ // = (e^x - e^(-x)) / (e^x + e^(-x))
+ // = (e^(2x) - 1) / (e^(2x) + 1)
+ // = (2^(hi + mid) * e^(2*lo) - 1) / (2^(hi + mid) * e^(2*lo) + 1)
+ // = (e^(2*lo) - 2^(-hi - mid)) / (e^(2*lo) + 2^(-hi - mid))
+ // We store 2^(-mid) in the lookup table EXP2_MID_5_BITS, and compute
+ // 2^(-hi - mid) by adding -hi to the exponent field of 2^(-mid).
+ // e^lo is computed using a degree-3 minimax polynomial generated by Sollya.
+
+ float xf = x;
+ float kf = fputil::nearest_integer(xf * (LOG2F_E * 2.0f * 0x1.0p+5f));
+ int x_hi_mid = -static_cast<int>(kf);
+ unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 5;
+ unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x1f;
+ // lo = x - (hi + mid)
+ // = round(x * log2(e) * 2 * 2^5) * log(2) * 0.5 * (-2^(-5)) + x
+ float lo = fputil::multiply_add(kf, LOGF_2 * 0.5f * -0x1.0p-5f, xf);
+
+ uint32_t exp2_hi_mid_bits =
+ EXP2_MID_5_BITS[x_mid] +
+ static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
+ // exp2_hi_mid = 2^(-hi - mid)
+ float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
+ // Degree-3 minimax polynomial generated by Sollya with the following
+ // commands:
+ // > display = hexadecimal;
+ // > P = fpminimax(expm1(2*x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
+ // > 1 + x * P;
+ float exp_2lo =
+ fputil::polyeval(lo, 0x1p+0f, 0x1p+1f, 0x1.001p+1f, 0x1.555ddep+0f);
+ return fputil::cast<float16>((exp_2lo - exp2_hi_mid) /
+ (exp_2lo + exp2_hi_mid));
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANHF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 1b18388ed60f8..fdde21d82b488 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4132,17 +4132,7 @@ add_entrypoint_object(
../tanhf16.h
DEPENDS
libc.hdr.fenv_macros
- libc.src.__support.CPP.array
- libc.src.__support.FPUtil.cast
- libc.src.__support.FPUtil.except_value_utils
- 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.optimization
- libc.src.__support.math.expxf16_utils
+ libc.src.__support.math.tanhf16
)
add_entrypoint_object(
diff --git a/libc/src/math/generic/tanhf16.cpp b/libc/src/math/generic/tanhf16.cpp
index fc0e28b95a834..564f37c79d308 100644
--- a/libc/src/math/generic/tanhf16.cpp
+++ b/libc/src/math/generic/tanhf16.cpp
@@ -7,146 +7,10 @@
//===----------------------------------------------------------------------===//
#include "src/math/tanhf16.h"
-#include "hdr/fenv_macros.h"
-#include "src/__support/CPP/array.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
-#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/PolyEval.h"
-#include "src/__support/FPUtil/cast.h"
-#include "src/__support/FPUtil/except_value_utils.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/common.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/optimization.h"
-#include "src/__support/math/expxf16_utils.h"
+#include "src/__support/math/tanhf16.h"
namespace LIBC_NAMESPACE_DECL {
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-static constexpr fputil::ExceptValues<float16, 2> TANHF16_EXCEPTS = {{
- // x = 0x1.f54p+0, tanhf16(x) = 0x1.ecp-1 (RZ)
- {0x3fd5U, 0x3bb0U, 1U, 0U, 0U},
- // x = -0x1.f54p+0, tanhf16(x) = -0x1.ecp-1 (RZ)
- {0xbfd5U, 0xbbb0U, 0U, 1U, 0U},
-}};
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-
-LLVM_LIBC_FUNCTION(float16, tanhf16, (float16 x)) {
- using namespace math::expxf16_internal;
- using FPBits = fputil::FPBits<float16>;
- FPBits x_bits(x);
-
- uint16_t x_u = x_bits.uintval();
- uint16_t x_abs = x_u & 0x7fffU;
-
- // When -2^(-14) <= x <= -2^(-9), or |x| <= 0x1.d2p-4,
- // or |x| >= atanh(1 - 2^(-11)), or x is NaN.
- if (LIBC_UNLIKELY(x_abs <= 0x2f48U || x_abs >= 0x4429U)) {
- // tanh(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;
- }
-
- // When -2^(-14) <= x <= -2^(-9).
- if (x_u >= 0x8400U && x_u <= 0x9800U) {
- switch (fputil::quick_get_round()) {
- case FE_TONEAREST:
- case FE_DOWNWARD:
- return x;
- default:
- return FPBits(static_cast<uint16_t>(x_u - 1U)).get_val();
- }
- }
-
- // When |x| <= 0x1.d2p-4.
- if (x_abs <= 0x2f48U) {
- if (LIBC_UNLIKELY(x_abs == 0))
- return x;
-
- float xf = x;
- float xf_sq = xf * xf;
- // Degree-7 Taylor expansion generated by Sollya with the following
- // commands:
- // > taylor(tanh(x), 7, 0);
- // > display = hexadecimal;
- // > // For each coefficient:
- // > round(/* put coefficient here */, SG, RN);
- return fputil::cast<float16>(
- xf * fputil::polyeval(xf_sq, 0x1p+0f, -0x1.555556p-2f, 0x1.111112p-3f,
- -0x1.ba1ba2p-5f));
- }
-
- // tanh(+/-inf) = +/-1
- if (x_bits.is_inf())
- return FPBits::one(x_bits.sign()).get_val();
-
- // When |x| >= atanh(1 - 2^(-11)).
- fputil::raise_except_if_required(FE_INEXACT);
-
- int rounding_mode = fputil::quick_get_round();
- if ((rounding_mode == FE_TONEAREST && x_abs >= 0x4482U) ||
- (rounding_mode == FE_UPWARD && x_bits.is_pos()) ||
- (rounding_mode == FE_DOWNWARD && x_bits.is_neg())) {
- return FPBits::one(x_bits.sign()).get_val();
- }
- if (x_bits.is_pos())
- return fputil::cast<float16>(0x1.ffcp-1);
- return fputil::cast<float16>(-0x1.ffcp-1);
- }
-
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
- if (auto r = TANHF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
- return r.value();
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-
- // For atanh(-1 + 2^(-11)) < x < atanh(1 - 2^(-11)), to compute tanh(x), we
- // perform the following range reduction: find hi, mid, lo, such that:
- // x = (hi + mid) * log(2) * 0.5 + lo, in which
- // hi is an integer,
- // mid * 2^5 is an integer,
- // -2^(-5) <= lo < 2^(-5).
- // In particular,
- // hi + mid = round(x * log2(e) * 2 * 2^5) * 2^(-5).
- // Then,
- // tanh(x) = sinh(x)/cosh(x)
- // = (e^x - e^(-x)) / (e^x + e^(-x))
- // = (e^(2x) - 1) / (e^(2x) + 1)
- // = (2^(hi + mid) * e^(2*lo) - 1) / (2^(hi + mid) * e^(2*lo) + 1)
- // = (e^(2*lo) - 2^(-hi - mid)) / (e^(2*lo) + 2^(-hi - mid))
- // We store 2^(-mid) in the lookup table EXP2_MID_5_BITS, and compute
- // 2^(-hi - mid) by adding -hi to the exponent field of 2^(-mid).
- // e^lo is computed using a degree-3 minimax polynomial generated by Sollya.
-
- float xf = x;
- float kf = fputil::nearest_integer(xf * (LOG2F_E * 2.0f * 0x1.0p+5f));
- int x_hi_mid = -static_cast<int>(kf);
- unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 5;
- unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x1f;
- // lo = x - (hi + mid)
- // = round(x * log2(e) * 2 * 2^5) * log(2) * 0.5 * (-2^(-5)) + x
- float lo = fputil::multiply_add(kf, LOGF_2 * 0.5f * -0x1.0p-5f, xf);
-
- uint32_t exp2_hi_mid_bits =
- EXP2_MID_5_BITS[x_mid] +
- static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
- // exp2_hi_mid = 2^(-hi - mid)
- float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
- // Degree-3 minimax polynomial generated by Sollya with the following
- // commands:
- // > display = hexadecimal;
- // > P = fpminimax(expm1(2*x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
- // > 1 + x * P;
- float exp_2lo =
- fputil::polyeval(lo, 0x1p+0f, 0x1p+1f, 0x1.001p+1f, 0x1.555ddep+0f);
- return fputil::cast<float16>((exp_2lo - exp2_hi_mid) /
- (exp_2lo + exp2_hi_mid));
-}
+LLVM_LIBC_FUNCTION(float16, tanhf16, (float16 x)) { return math::tanhf16(x);}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 07ccd0ca1bc4b..47c3bba275e20 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -102,4 +102,5 @@ add_fp_unittest(
libc.src.__support.math.sqrtf
libc.src.__support.math.tan
libc.src.__support.math.tanf
+ libc.src.__support.math.tanhf16
)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index ec70025f0f4ea..cd9bb9486b4db 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -108,6 +108,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sinf(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sqrtf(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanf(0.0f));
+ EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanhf16(0.0f16));
}
TEST(LlvmLibcSharedMathTest, AllDouble) {
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 83b752ce1214b..ad07ab48c1c94 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3748,6 +3748,14 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_tanhf16",
+ hdrs = ["src/__support/math/tanhf16.h"],
+ deps = [
+ ":__support_math_expxf16_utils",
+ ],
+)
+
############################### complex targets ################################
libc_function(
@@ -5453,7 +5461,7 @@ libc_math_function(
libc_math_function(
name = "tanhf16",
additional_deps = [
- ":__support_math_expxf16_utils",
+ ":__support_math_tanhf16",
],
)
>From 0d19fcce7795f3b4f82978fb09b092ea88994688 Mon Sep 17 00:00:00 2001
From: xinlongchen <xinlongchen at tencent.com>
Date: Wed, 11 Feb 2026 23:00:42 +0800
Subject: [PATCH 2/5] fix cr comment
---
libc/shared/math/tanhf16.h | 8 ++++-
libc/src/__support/math/CMakeLists.txt | 11 ++++---
libc/src/__support/math/tanhf16.h | 31 ++++++++++---------
libc/src/math/generic/CMakeLists.txt | 1 -
libc/src/math/generic/tanhf16.cpp | 4 +--
libc/test/shared/shared_math_test.cpp | 2 +-
.../llvm-project-overlay/libc/BUILD.bazel | 13 ++++++++
.../libc/libc_configure_options.bzl | 4 +++
8 files changed, 51 insertions(+), 23 deletions(-)
diff --git a/libc/shared/math/tanhf16.h b/libc/shared/math/tanhf16.h
index 47f3531d94ee1..27a1944c3c4d7 100644
--- a/libc/shared/math/tanhf16.h
+++ b/libc/shared/math/tanhf16.h
@@ -1,4 +1,4 @@
-//===-- Shared tanhf16 function ------------------------------------*- C++ -*-===//
+//===-- Shared tanhf16 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.
@@ -9,6 +9,10 @@
#ifndef LLVM_LIBC_SHARED_MATH_TANHF16_H
#define LLVM_LIBC_SHARED_MATH_TANHF16_H
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
#include "shared/libc_common.h"
#include "src/__support/math/tanhf16.h"
@@ -20,4 +24,6 @@ using math::tanhf16;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
+#endif // LIBC_TYPES_HAS_FLOAT16
+
#endif // LLVM_LIBC_SHARED_MATH_TANHF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index ca96ae64f007a..f35f3a018c3e1 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1574,16 +1574,19 @@ add_header_library(
HDRS
tanhf16.h
DEPENDS
+ .expxf16_utils
libc.hdr.fenv_macros
libc.src.__support.CPP.array
- libc.src.__support.FPUtil.cast
- libc.src.__support.FPUtil.except_value_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.except_value_utils
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.common
+ libc.src.__support.macros.config
libc.src.__support.macros.optimization
- libc.src.__support.math.expxf16_utils
+ libc.include.llvm-libc-macros.float16_macros
)
diff --git a/libc/src/__support/math/tanhf16.h b/libc/src/__support/math/tanhf16.h
index 4761431443cfc..7a95e5f51b668 100644
--- a/libc/src/__support/math/tanhf16.h
+++ b/libc/src/__support/math/tanhf16.h
@@ -9,6 +9,11 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_TANHF16_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_TANHF16_H
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "expxf16_utils.h"
#include "hdr/fenv_macros.h"
#include "src/__support/CPP/array.h"
#include "src/__support/FPUtil/FEnvImpl.h"
@@ -22,29 +27,25 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
-#include "src/__support/math/expxf16_utils.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-namespace tanhf16_internal {
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, 2> TANHF16_EXCEPTS = {{
- // x = 0x1.f54p+0, tanhf16(x) = 0x1.ecp-1 (RZ)
- {0x3fd5U, 0x3bb0U, 1U, 0U, 0U},
- // x = -0x1.f54p+0, tanhf16(x) = -0x1.ecp-1 (RZ)
- {0xbfd5U, 0xbbb0U, 0U, 1U, 0U},
-}};
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-} // namespace tanhf16_internal
-
-LIBC_INLINE constexpr float16 tanhf16(float16 x) {
- using namespace tanhf16_internal;
+LIBC_INLINE float16 tanhf16(float16 x) {
using namespace math::expxf16_internal;
using FPBits = fputil::FPBits<float16>;
FPBits x_bits(x);
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+ constexpr fputil::ExceptValues<float16, 2> TANHF16_EXCEPTS = {{
+ // x = 0x1.f54p+0, tanhf16(x) = 0x1.ecp-1 (RZ)
+ {0x3fd5U, 0x3bb0U, 1U, 0U, 0U},
+ // x = -0x1.f54p+0, tanhf16(x) = -0x1.ecp-1 (RZ)
+ {0xbfd5U, 0xbbb0U, 0U, 1U, 0U},
+ }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
uint16_t x_u = x_bits.uintval();
uint16_t x_abs = x_u & 0x7fffU;
@@ -160,4 +161,6 @@ LIBC_INLINE constexpr float16 tanhf16(float16 x) {
} // namespace LIBC_NAMESPACE_DECL
+#endif // LIBC_TYPES_HAS_FLOAT16
+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANHF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index f3e5a6bc5607a..ccd6a543a8e65 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4125,7 +4125,6 @@ add_entrypoint_object(
HDRS
../tanhf16.h
DEPENDS
- libc.hdr.fenv_macros
libc.src.__support.math.tanhf16
)
diff --git a/libc/src/math/generic/tanhf16.cpp b/libc/src/math/generic/tanhf16.cpp
index 564f37c79d308..45e168291b07b 100644
--- a/libc/src/math/generic/tanhf16.cpp
+++ b/libc/src/math/generic/tanhf16.cpp
@@ -1,4 +1,4 @@
-//===-- Half-precision tanh(x) function -----------------------------------===//
+//===-- Half-precision tanhf(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.
@@ -11,6 +11,6 @@
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(float16, tanhf16, (float16 x)) { return math::tanhf16(x);}
+LLVM_LIBC_FUNCTION(float16, tanhf16, (float16 x)) { return math::tanhf16(x); }
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index e28c3103d2e39..4079f5a23a3a0 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -68,6 +68,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::sinf16(0.0f16));
+ EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::tanhf16(0.0f16));
}
#endif // LIBC_TYPES_HAS_FLOAT16
@@ -118,7 +119,6 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sinf(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sqrtf(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanf(0.0f));
- EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanhf16(0.0f16));
}
TEST(LlvmLibcSharedMathTest, AllDouble) {
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 592ca8aa600be..f8d229a7ca3df 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3803,6 +3803,19 @@ libc_support_library(
hdrs = ["src/__support/math/tanhf16.h"],
deps = [
":__support_math_expxf16_utils",
+ ":hdr_fenv_macros",
+ ":__support_fputil_fenv_impl",
+ ":__support_fputil_fp_bits",
+ ":__support_fputil_polyeval",
+ ":__support_fputil_cast",
+ ":__support_fputil_except_value_utils",
+ ":__support_fputil_multiply_add",
+ ":__support_fputil_nearest_integer",
+ ":__support_fputil_rounding_mode",
+ ":__support_common",
+ ":__support_macros_config",
+ ":__support_macros_optimization",
+ ":llvm_libc_macros_float16_macros",
],
)
diff --git a/utils/bazel/llvm-project-overlay/libc/libc_configure_options.bzl b/utils/bazel/llvm-project-overlay/libc/libc_configure_options.bzl
index 1c2b65ea20113..16add7c131f22 100644
--- a/utils/bazel/llvm-project-overlay/libc/libc_configure_options.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/libc_configure_options.bzl
@@ -53,4 +53,8 @@ LIBC_CONFIGURE_OPTIONS = [
# Documentation in libc/src/__support/libc_errno.h
"LIBC_ERRNO_MODE=LIBC_ERRNO_MODE_SYSTEM_INLINE",
+
+ # Enable float16 for tanhf16, sinhf16, etc. Bazel's compiler often does not
+ # predefine __FLT16_MANT_DIG__; this forces the type when _Float16 is supported.
+ "LIBC_TYPES_HAS_FLOAT16",
]
>From e6d72baa0ab128a857d04e8bdced8a719cd9c40c Mon Sep 17 00:00:00 2001
From: xinlongchen <xinlongchen at tencent.com>
Date: Thu, 12 Feb 2026 17:19:14 +0800
Subject: [PATCH 3/5] delete useless config
---
libc/shared/math/tanhf16.h | 2 +-
libc/src/__support/math/tanhf16.h | 2 +-
.../llvm-project-overlay/libc/libc_configure_options.bzl | 4 ----
3 files changed, 2 insertions(+), 6 deletions(-)
diff --git a/libc/shared/math/tanhf16.h b/libc/shared/math/tanhf16.h
index 27a1944c3c4d7..11ac4e04f0355 100644
--- a/libc/shared/math/tanhf16.h
+++ b/libc/shared/math/tanhf16.h
@@ -1,4 +1,4 @@
-//===-- Shared tanhf16 function ------------------------------*- C++ -*-===//
+//===-- Shared tanhf16 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.
diff --git a/libc/src/__support/math/tanhf16.h b/libc/src/__support/math/tanhf16.h
index 7a95e5f51b668..80671691203c9 100644
--- a/libc/src/__support/math/tanhf16.h
+++ b/libc/src/__support/math/tanhf16.h
@@ -1,4 +1,4 @@
-//===-- Single-precision tanhf16 function --------------------------------===//
+//===-- Single-precision tanhf16 function ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/utils/bazel/llvm-project-overlay/libc/libc_configure_options.bzl b/utils/bazel/llvm-project-overlay/libc/libc_configure_options.bzl
index 16add7c131f22..1c2b65ea20113 100644
--- a/utils/bazel/llvm-project-overlay/libc/libc_configure_options.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/libc_configure_options.bzl
@@ -53,8 +53,4 @@ LIBC_CONFIGURE_OPTIONS = [
# Documentation in libc/src/__support/libc_errno.h
"LIBC_ERRNO_MODE=LIBC_ERRNO_MODE_SYSTEM_INLINE",
-
- # Enable float16 for tanhf16, sinhf16, etc. Bazel's compiler often does not
- # predefine __FLT16_MANT_DIG__; this forces the type when _Float16 is supported.
- "LIBC_TYPES_HAS_FLOAT16",
]
>From 578d4464d8c3cd9875dba46d4ac3b85847be37fd Mon Sep 17 00:00:00 2001
From: xinlongchen <xinlongchen at tencent.com>
Date: Fri, 13 Feb 2026 22:38:13 +0800
Subject: [PATCH 4/5] fix with code review
---
libc/src/__support/math/CMakeLists.txt | 2 +-
libc/src/__support/math/tanhf16.h | 2 +-
libc/src/math/generic/tanhf16.cpp | 2 +-
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 12 ++++++------
4 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a55f684b5f7c7..87a87895bcc51 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1714,6 +1714,7 @@ add_header_library(
.expxf16_utils
libc.hdr.fenv_macros
libc.src.__support.CPP.array
+ libc.src.__support.common
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.polyeval
@@ -1722,7 +1723,6 @@ add_header_library(
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.rounding_mode
- libc.src.__support.common
libc.src.__support.macros.config
libc.src.__support.macros.optimization
libc.include.llvm-libc-macros.float16_macros
diff --git a/libc/src/__support/math/tanhf16.h b/libc/src/__support/math/tanhf16.h
index 80671691203c9..477593b1368b5 100644
--- a/libc/src/__support/math/tanhf16.h
+++ b/libc/src/__support/math/tanhf16.h
@@ -16,6 +16,7 @@
#include "expxf16_utils.h"
#include "hdr/fenv_macros.h"
#include "src/__support/CPP/array.h"
+#include "src/__support/common.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/PolyEval.h"
@@ -24,7 +25,6 @@
#include "src/__support/FPUtil/multiply_add.h"
#include "src/__support/FPUtil/nearest_integer.h"
#include "src/__support/FPUtil/rounding_mode.h"
-#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
diff --git a/libc/src/math/generic/tanhf16.cpp b/libc/src/math/generic/tanhf16.cpp
index 45e168291b07b..0d1b9c6062640 100644
--- a/libc/src/math/generic/tanhf16.cpp
+++ b/libc/src/math/generic/tanhf16.cpp
@@ -1,4 +1,4 @@
-//===-- Half-precision tanhf(x) function ----------------------------------===//
+//===-- Half-precision tanh(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.
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index a08488c3b7053..b0ced3fc3fcd6 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3908,19 +3908,19 @@ libc_support_library(
name = "__support_math_tanhf16",
hdrs = ["src/__support/math/tanhf16.h"],
deps = [
- ":__support_math_expxf16_utils",
- ":hdr_fenv_macros",
- ":__support_fputil_fenv_impl",
- ":__support_fputil_fp_bits",
- ":__support_fputil_polyeval",
+ ":__support_common",
":__support_fputil_cast",
":__support_fputil_except_value_utils",
+ ":__support_fputil_fenv_impl",
+ ":__support_fputil_fp_bits",
":__support_fputil_multiply_add",
":__support_fputil_nearest_integer",
+ ":__support_fputil_polyeval",
":__support_fputil_rounding_mode",
- ":__support_common",
":__support_macros_config",
":__support_macros_optimization",
+ ":__support_math_expxf16_utils",
+ ":hdr_fenv_macros",
":llvm_libc_macros_float16_macros",
],
)
>From 9de1310d166f30e5e9d5d0449ec200d8bb4c4dee Mon Sep 17 00:00:00 2001
From: xinlongchen <xinlongchen at tencent.com>
Date: Sat, 14 Feb 2026 19:49:30 +0800
Subject: [PATCH 5/5] fix format
---
libc/src/__support/math/CMakeLists.txt | 2 +-
libc/src/__support/math/tanhf16.h | 2 +-
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 4d2de7a8a297c..4564e77d408af 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1763,7 +1763,6 @@ add_header_library(
.expxf16_utils
libc.hdr.fenv_macros
libc.src.__support.CPP.array
- libc.src.__support.common
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.polyeval
@@ -1772,6 +1771,7 @@ add_header_library(
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.common
libc.src.__support.macros.config
libc.src.__support.macros.optimization
libc.include.llvm-libc-macros.float16_macros
diff --git a/libc/src/__support/math/tanhf16.h b/libc/src/__support/math/tanhf16.h
index 477593b1368b5..80671691203c9 100644
--- a/libc/src/__support/math/tanhf16.h
+++ b/libc/src/__support/math/tanhf16.h
@@ -16,7 +16,6 @@
#include "expxf16_utils.h"
#include "hdr/fenv_macros.h"
#include "src/__support/CPP/array.h"
-#include "src/__support/common.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/PolyEval.h"
@@ -25,6 +24,7 @@
#include "src/__support/FPUtil/multiply_add.h"
#include "src/__support/FPUtil/nearest_integer.h"
#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index ab4086112c210..99a903cfd68b6 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3971,7 +3971,7 @@ libc_support_library(
":llvm_libc_macros_float16_macros",
],
)
-
+
############################### complex targets ################################
libc_function(
More information about the libc-commits
mailing list