[libc-commits] [libc] [llvm] [libc][math][c23] Add tanbf16 math function (PR #185100)
via libc-commits
libc-commits at lists.llvm.org
Thu Jun 25 07:03:38 PDT 2026
https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/185100
>From 53494c2fb6dfc470d4f240ca683fb19bef2fd8b2 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 5 Mar 2026 23:47:04 +0530
Subject: [PATCH 01/15] feat: implementation for tanbf16
t1
initial commit
entrypoints
t1
t2
t3
t3
tanbf16_add
---
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/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/tanbf16.h | 23 +++++
libc/src/__support/math/CMakeLists.txt | 18 ++++
libc/src/__support/math/tanbf16.h | 82 ++++++++++++++++++
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/generic/CMakeLists.txt | 11 +++
libc/src/math/generic/tanbf16.cpp | 17 ++++
libc/src/math/tanbf16.h | 21 +++++
libc/test/shared/CMakeLists.txt | 1 +
libc/test/shared/shared_math_test.cpp | 83 +------------------
libc/test/src/math/CMakeLists.txt | 13 +++
libc/test/src/math/smoke/CMakeLists.txt | 13 +++
libc/test/src/math/smoke/tanbf16_test.cpp | 41 +++++++++
libc/test/src/math/tanbf16_test.cpp | 76 +++++++++++++++++
.../llvm-project-overlay/libc/BUILD.bazel | 26 ++++++
27 files changed, 357 insertions(+), 82 deletions(-)
create mode 100644 libc/shared/math/tanbf16.h
create mode 100644 libc/src/__support/math/tanbf16.h
create mode 100644 libc/src/math/generic/tanbf16.cpp
create mode 100644 libc/src/math/tanbf16.h
create mode 100644 libc/test/src/math/smoke/tanbf16_test.cpp
create mode 100644 libc/test/src/math/tanbf16_test.cpp
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index f64eb55b40acd..73f4c1f937388 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -865,6 +865,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 55a175923faa1..a5c1b20a7a434 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -876,6 +876,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 201d2e9cbf97b..60bc924993fc8 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -873,6 +873,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 413534a14f34e..6f4916b203de5 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -686,6 +686,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 d35700d341d1a..c09ccadbb8c4c 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -304,6 +304,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 7fcab53bd99f3..60a98cf5c773f 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -711,6 +711,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 be1467e9cbceb..70bdbde97e868 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -713,6 +713,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 599ae32a35073..14c6e56d13c19 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -986,6 +986,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 d610e663229ba..e6aa6091ed550 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -557,6 +557,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 ef2d13e6d6f9e..e184941910710 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1007,6 +1007,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 8046679785e68..54727a8cdf31e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1078,6 +1078,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 d7f66dc58e4db..3c68290675b11 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -393,6 +393,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
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 f4b28e3aa139d..2e8a574e4d042 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -474,6 +474,7 @@
#include "math/sqrtf16.h"
#include "math/sqrtl.h"
#include "math/tan.h"
+#include "math/tanbf16.h"
#include "math/tanf.h"
#include "math/tanf16.h"
#include "math/tanhf.h"
diff --git a/libc/shared/math/tanbf16.h b/libc/shared/math/tanbf16.h
new file mode 100644
index 0000000000000..24006da13b4dd
--- /dev/null
+++ b/libc/shared/math/tanbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared tanbf16 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_TANBF16_H
+#define LLVM_LIBC_SHARED_MATH_TANBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/tanbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::tanbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_TANBF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index b57e82a036665..307f35d743bd0 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5399,6 +5399,24 @@ add_header_library(
libc.src.__support.macros.optimization
)
+
+add_header_library(
+ tanbf16
+ HDRS
+ tanbf16.h
+ DEPENDS
+ .sincosf16_utils
+ libc.hdr.errno_macros
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.cast
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.except_value_utils
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.macros.optimization
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_header_library(
tanf
HDRS
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
new file mode 100644
index 0000000000000..99d0369bc3dd5
--- /dev/null
+++ b/libc/src/__support/math/tanbf16.h
@@ -0,0 +1,82 @@
+//===-- Single-precision tanbf16 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_TANBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_TANBF16_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "sincosf16_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.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/macros/optimization.h"
+#include "src/__support/FPUtil/bfloat16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
+ using namespace sincosf16_internal;
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits xbits(x);
+
+ uint16_t x_u = xbits.uintval();
+ uint16_t x_abs = x_u & 0x7fff;
+
+ //NaN or -/+ INF
+ if(x_abs >= 0x7F80){
+ //NaN
+ if(xbits.is_nan()){
+ if(xbits.is_signaling_nan()){
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+ //|x| is +/- INF
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return x + FPBits::quiet_nan().get_val();
+ }
+
+ // |x| = {0}
+ if(LIBC_UNLIKELY(x_abs== 0)){
+ return x;
+ }
+ float xf = x;
+ // Through Exhaustive testing
+ // The last value where tan(x) ~ x is 0x3db8
+ if (LIBC_UNLIKELY(x_abs <= 0x3db8)) {
+ int rounding = fputil::quick_get_round();
+
+ // tan(x) is always slightly larger in magnitude than x
+ // so when rounding away from zero, correct answer is x - 1 ULP
+ if ((xbits.is_pos() && rounding == FE_UPWARD) ||
+ (xbits.is_neg() && rounding == FE_DOWNWARD))
+ return fputil::cast<bfloat16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
+ return x;
+ }
+
+ float sin_k, cos_k, sin_y, cosm1_y;
+ sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
+
+ using fputil::multiply_add;
+ return fputil::cast<bfloat16>(
+ multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /
+ multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k)));
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index dcfc8183d716a..4823ceefd7475 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -587,6 +587,7 @@ add_math_entrypoint_object(sqrtf128)
add_math_entrypoint_object(sqrtbf16)
add_math_entrypoint_object(tan)
+add_math_entrypoint_object(tanbf16)
add_math_entrypoint_object(tanf)
add_math_entrypoint_object(tanf16)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 770da1404a95b..bd96ea4ed2dea 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -415,6 +415,17 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ tanbf16
+ SRCS
+ tanbf16.cpp
+ HDRS
+ ../tanbf16.h
+ DEPENDS
+ libc.src.__support.math.tanbf16
+)
+
+
add_entrypoint_object(
tanf
SRCS
diff --git a/libc/src/math/generic/tanbf16.cpp b/libc/src/math/generic/tanbf16.cpp
new file mode 100644
index 0000000000000..40d0443a75fb1
--- /dev/null
+++ b/libc/src/math/generic/tanbf16.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation for tanbf16(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/tanbf16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/math/tanbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, tanbf16, (bfloat16 x)) { return math::tanbf16(x); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/tanbf16.h b/libc/src/math/tanbf16.h
new file mode 100644
index 0000000000000..034167d3bb9c2
--- /dev/null
+++ b/libc/src/math/tanbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for tanbf16 -----------------------*- 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_TANBF16_H
+#define LLVM_LIBC_SRC_MATH_TANBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 tanbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_TANBF16_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 17e1570f1b19a..9b990dd5a5c84 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -479,6 +479,7 @@ add_fp_unittest(
libc.src.__support.math.sqrtf
libc.src.__support.math.sqrtl
libc.src.__support.math.tan
+ libc.src.__support.math.tanbf16
libc.src.__support.math.tanf
libc.src.__support.math.tanf16
libc.src.__support.math.tanhf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index e26532773c69b..a000991027ac6 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -812,86 +812,5 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::nextafterbf16(
bfloat16(0.0), bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::sqrtbf16(bfloat16(1.0)));
- // TODO: This test case just failed only when building with gcc-13 and only
- // for `ninja libc.test.shared.shared_math_test.__unit__.__NO_FMA_OPT
- // Other gcc versions or other sub-targets work fine.
- // https://github.com/llvm/llvm-project/issues/199332
- // #ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
- // EXPECT_FP_EQ(bfloat16(0.0),
- // LIBC_NAMESPACE::shared::nexttowardbf16(bfloat16(0.0),
- // 0.0L));
- // #endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
-
- EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbbf16(bfloat16(1.0)));
- EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogbbf16(bfloat16(1.0)));
-
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fmaximumbf16(
- bfloat16(0.0), bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fminimumbf16(
- bfloat16(0.0), bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::fminbf16(bfloat16(0.0), bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fmaximum_numbf16(
- bfloat16(0.0), bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fminimum_numbf16(
- bfloat16(0.0), bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::fromfpbf16(bfloat16(0.0), 0, 32));
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::fromfpxbf16(bfloat16(0.0), 0, 32));
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::ufromfpbf16(bfloat16(0.0), 0, 32));
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::ufromfpxbf16(bfloat16(0.0), 0, 32));
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fmaximum_magbf16(
- bfloat16(0.0), bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fminimum_magbf16(
- bfloat16(0.0), bfloat16(0.0)));
- bfloat16 totalorderbf16_x = bfloat16(0.0);
- bfloat16 totalorderbf16_y = bfloat16(0.0);
- EXPECT_EQ(1, LIBC_NAMESPACE::shared::totalorderbf16(&totalorderbf16_x,
- &totalorderbf16_y));
- bfloat16 totalordermagbf16_x = bfloat16(0.0);
- bfloat16 totalordermagbf16_y = bfloat16(0.0);
- EXPECT_EQ(1, LIBC_NAMESPACE::shared::totalordermagbf16(&totalordermagbf16_x,
- &totalordermagbf16_y));
- LIBC_NAMESPACE::shared::fmodbf16(bfloat16(1.0), bfloat16(1.0));
- bfloat16 modfbf16_iptr = bfloat16(0.0);
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::modfbf16(bfloat16(0.0), &modfbf16_iptr));
- EXPECT_FP_EQ(bfloat16(0.0), modfbf16_iptr);
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fminimum_mag_numbf16(
- bfloat16(0.0), bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::remainderbf16(
- bfloat16(1.0), bfloat16(1.0)));
- int remquobf16_exp = 0;
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::remquobf16(bfloat16(1.0), bfloat16(1.0),
- &remquobf16_exp));
- EXPECT_EQ(1, remquobf16_exp);
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::ldexpbf16(bfloat16(0.0), 0));
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::scalblnbf16(bfloat16(0.0), 0L));
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::scalbnbf16(bfloat16(0.0), 0));
- int frexpbf16_exp = 0;
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::frexpbf16(
- bfloat16(0.0), &frexpbf16_exp));
- EXPECT_EQ(0, frexpbf16_exp);
- EXPECT_EQ(0L, LIBC_NAMESPACE::shared::lrintbf16(bfloat16(0.0)));
- EXPECT_EQ(0L, LIBC_NAMESPACE::shared::lroundbf16(bfloat16(0.0)));
- EXPECT_EQ(0LL, LIBC_NAMESPACE::shared::llrintbf16(bfloat16(0.0)));
- EXPECT_EQ(0LL, LIBC_NAMESPACE::shared::llroundbf16(bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::nearbyintbf16(bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::rintbf16(bfloat16(0.0)));
- EXPECT_EQ(1, LIBC_NAMESPACE::shared::iscanonicalbf16(bfloat16(0.0)));
- EXPECT_EQ(0, LIBC_NAMESPACE::shared::issignalingbf16(bfloat16(0.0)));
- EXPECT_TRUE(FPBits(LIBC_NAMESPACE::shared::nanbf16("")).is_nan());
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::roundbf16(bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0),
- LIBC_NAMESPACE::shared::roundevenbf16(bfloat16(0.0)));
- EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::truncbf16(bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::tanbf16(bfloat16(0.0)));
}
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 1e27724fe0a60..31f6468e01110 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -201,6 +201,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ tanbf16_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ tanbf16_test.cpp
+ DEPENDS
+ libc.src.math.tanbf16
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
tanf_test
NEED_MPFR
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index b763cdb43b31c..73d2265322c4d 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -110,6 +110,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ tanbf16_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ tanbf16_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.src.math.tanbf16
+ libc.hdr.fenv_macros
+ libc.src.__support.FPUtil.bfloat16
+)
+
add_fp_unittest(
tanf_test
SUITE
diff --git a/libc/test/src/math/smoke/tanbf16_test.cpp b/libc/test/src/math/smoke/tanbf16_test.cpp
new file mode 100644
index 0000000000000..4e760b4a747e4
--- /dev/null
+++ b/libc/test/src/math/smoke/tanbf16_test.cpp
@@ -0,0 +1,41 @@
+//===-- Unittests for tanbf16 ---------------------------------------------===//
+//
+//
+// 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 "src/__support/FPUtil/bfloat16.h"
+#include "src/math/tanbf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+class LlvmLibcTanBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+ DECLARE_SPECIAL_CONSTANTS(bfloat16)
+public:
+ void test_special_numbers() {
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::tanbf16(aNaN));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(
+ aNaN, LIBC_NAMESPACE::tanbf16(sNaN), FE_INVALID);
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::tanbf16(zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::tanbf16(neg_zero));
+ EXPECT_MATH_ERRNO(0);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::tanbf16(inf));
+ EXPECT_MATH_ERRNO(EDOM);
+
+ EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::tanbf16(neg_inf));
+ EXPECT_MATH_ERRNO(EDOM);
+ }
+};
+TEST_F(LlvmLibcTanBf16Test, SpecialNumbers) { test_special_numbers(); }
diff --git a/libc/test/src/math/tanbf16_test.cpp b/libc/test/src/math/tanbf16_test.cpp
new file mode 100644
index 0000000000000..b4e5f5a2fb12b
--- /dev/null
+++ b/libc/test/src/math/tanbf16_test.cpp
@@ -0,0 +1,76 @@
+//===-- Exhaustive test for tanbf16 ---------------------------------------===//
+//
+// 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/tanbf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+#include <iostream>
+
+using LlvmLibcTanBf16Test = 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: [-Inf, 0]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xff80U;
+
+TEST_F(LlvmLibcTanBf16Test, PositiveRange) {
+ uint16_t last_equal = 0; // remove
+ for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+
+ bfloat16 result = LIBC_NAMESPACE::tanbf16(x);
+ // Check if tan(x) rounds to x itself
+ if (FPBits(result).uintval() == v) {
+ last_equal = v;
+ } else {
+ // First x where tan(x) != x — print and stop
+ bfloat16 prev = FPBits(last_equal).get_val();
+ std::cout << "Last x where tan(x)==x: "
+ << "hex=0x" << std::hex << last_equal << " value=" << std::dec
+ << (float)prev << "\n";
+ std::cout << "First x where tan(x)!=x: "
+ << "hex=0x" << std::hex << v << " value=" << std::dec
+ << (float)x << "\n";
+ std::cout << "tan(first_diff_x)=" << (float)result << "\n";
+ std::cout << "\n=== USE IN CODE ===\n"
+ << "if (x_abs <= 0x" << std::hex << last_equal
+ << "U) // tan(x)==x for bfloat16\n";
+ break;
+ }
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tan, x,
+ LIBC_NAMESPACE::tanbf16(x), 0.5);
+ }
+}
+
+TEST_F(LlvmLibcTanBf16Test, NegativeRange) {
+ uint16_t last_equal = 0x8000; // remove
+ for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+ bfloat16 x = FPBits(v).get_val();
+
+ bfloat16 result = LIBC_NAMESPACE::tanbf16(x);
+ if (FPBits(result).uintval() == v) {
+ last_equal = v;
+ } else {
+ std::cout << "Negative — last equal: "
+ << "hex=0x" << std::hex << last_equal << " value=" << std::dec
+ << (float)FPBits(last_equal).get_val() << "\n";
+ break;
+ }
+
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tan, x,
+ LIBC_NAMESPACE::tanbf16(x), 0.5);
+ }
+}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e0f981d35b965..7ed1f1cc9cc4a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -9175,6 +9175,24 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_math_tanbf16",
+ hdrs = ["src/__support/math/tanbf16.h"],
+ deps = [
+ ":__support_fputil_cast",
+ ":__support_fputil_except_value_utils",
+ ":__support_fputil_fenv_impl",
+ ":__support_fputil_fp_bits",
+ ":__support_fputil_multiply_add",
+ ":__support_macros_optimization",
+ ":__support_macros_properties_types",
+ ":__support_math_sincosf16_utils",
+ ":hdr_errno_macros",
+ ":hdr_fenv_macros",
+ ":__support_fputil_bfloat16",
+ ],
+)
+
libc_support_library(
name = "__support_math_tanf",
hdrs = ["src/__support/math/tanf.h"],
@@ -12882,6 +12900,14 @@ libc_math_function(
additional_deps = [":__support_math_tan"],
)
+libc_math_function(
+ name = "tanbf16",
+ additional_deps = [
+ ":__support_math_tanbf16",
+ ],
+)
+
+
libc_math_function(
name = "tanf",
additional_deps = [":__support_math_tanf"],
>From 11b6ce38086ff14d3c5da6679adfbac5bbb36482 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 01:42:34 +0530
Subject: [PATCH 02/15] feat: added tests and corrections
---
libc/src/__support/math/CMakeLists.txt | 1 -
libc/src/__support/math/tanbf16.h | 24 +++++++++----------
.../llvm-project-overlay/libc/BUILD.bazel | 1 -
3 files changed, 11 insertions(+), 15 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 307f35d743bd0..a39933f81cb71 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5411,7 +5411,6 @@ add_header_library(
libc.src.__support.FPUtil.cast
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
- libc.src.__support.FPUtil.except_value_utils
libc.src.__support.FPUtil.multiply_add
libc.src.__support.macros.optimization
libc.src.__support.FPUtil.bfloat16
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
index 99d0369bc3dd5..57d3614e9de59 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -1,4 +1,4 @@
-//===-- Single-precision tanbf16 function ---------------------------------===//
+//===-- Implementation of tanbf16 function --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -14,11 +14,10 @@
#include "sincosf16_utils.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.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/macros/optimization.h"
-#include "src/__support/FPUtil/bfloat16.h"
namespace LIBC_NAMESPACE_DECL {
@@ -31,12 +30,13 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
uint16_t x_u = xbits.uintval();
uint16_t x_abs = x_u & 0x7fff;
+ float xf = x;
- //NaN or -/+ INF
- if(x_abs >= 0x7F80){
- //NaN
- if(xbits.is_nan()){
- if(xbits.is_signaling_nan()){
+ // NaN or -/+ INF
+ if (x_abs >= 0x7F80) {
+ // NaN
+ if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
fputil::raise_except_if_required(FE_INVALID);
return FPBits::quiet_nan().get_val();
}
@@ -49,17 +49,15 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
}
// |x| = {0}
- if(LIBC_UNLIKELY(x_abs== 0)){
+ if (LIBC_UNLIKELY(x_abs == 0)) {
return x;
}
- float xf = x;
+
// Through Exhaustive testing
// The last value where tan(x) ~ x is 0x3db8
if (LIBC_UNLIKELY(x_abs <= 0x3db8)) {
int rounding = fputil::quick_get_round();
-
- // tan(x) is always slightly larger in magnitude than x
- // so when rounding away from zero, correct answer is x - 1 ULP
+ // separate case handles it with magnitude of 2^-13
if ((xbits.is_pos() && rounding == FE_UPWARD) ||
(xbits.is_neg() && rounding == FE_DOWNWARD))
return fputil::cast<bfloat16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 7ed1f1cc9cc4a..80bf90e078f43 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -9180,7 +9180,6 @@ libc_support_library(
hdrs = ["src/__support/math/tanbf16.h"],
deps = [
":__support_fputil_cast",
- ":__support_fputil_except_value_utils",
":__support_fputil_fenv_impl",
":__support_fputil_fp_bits",
":__support_fputil_multiply_add",
>From 91ba66bb9015c95e55064733c619e390391883f7 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 01:44:14 +0530
Subject: [PATCH 03/15] chore: bazel
---
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 80bf90e078f43..3321c6add97a9 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -9179,6 +9179,7 @@ libc_support_library(
name = "__support_math_tanbf16",
hdrs = ["src/__support/math/tanbf16.h"],
deps = [
+ ":__support_fputil_bfloat16",
":__support_fputil_cast",
":__support_fputil_fenv_impl",
":__support_fputil_fp_bits",
@@ -9188,7 +9189,6 @@ libc_support_library(
":__support_math_sincosf16_utils",
":hdr_errno_macros",
":hdr_fenv_macros",
- ":__support_fputil_bfloat16",
],
)
@@ -12906,7 +12906,6 @@ libc_math_function(
],
)
-
libc_math_function(
name = "tanf",
additional_deps = [":__support_math_tanf"],
>From 7631599a31ec25528ff1701e36ad797500a23661 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 01:48:19 +0530
Subject: [PATCH 04/15] chore: nit
---
libc/src/__support/math/tanbf16.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
index 57d3614e9de59..ba182433c6932 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -60,7 +60,7 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
// separate case handles it with magnitude of 2^-13
if ((xbits.is_pos() && rounding == FE_UPWARD) ||
(xbits.is_neg() && rounding == FE_DOWNWARD))
- return fputil::cast<bfloat16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
+ return fputil::cast<bfloat16>(fputil::multiply_add(xf, 0x1.0p-13f, xf));
return x;
}
>From 3894c0dfa50e77228e0f5349d8d76d2695fdb9d1 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 01:56:08 +0530
Subject: [PATCH 05/15] chore: added in index.rst after exhaustive passes
---
libc/docs/headers/math/index.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index c2ebc5031cef5..8f8f381054002 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -353,7 +353,7 @@ Higher Math Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| sqrt | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.7.10 | F.10.4.10 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| tan | |check| | |check| | | |check| | | | 7.12.4.7 | F.10.1.7 |
+| tan | |check| | |check| | | |check| | | |check| | 7.12.4.7 | F.10.1.7 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| tanh | |check| | | | |check| | | | 7.12.5.6 | F.10.2.6 |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
>From 343f0bceb68ed019d374a05e383411a91f12bbac Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 01:58:54 +0530
Subject: [PATCH 06/15] chore: removed debug statements
---
libc/test/src/math/tanbf16_test.cpp | 33 -----------------------------
1 file changed, 33 deletions(-)
diff --git a/libc/test/src/math/tanbf16_test.cpp b/libc/test/src/math/tanbf16_test.cpp
index b4e5f5a2fb12b..848e07debe18d 100644
--- a/libc/test/src/math/tanbf16_test.cpp
+++ b/libc/test/src/math/tanbf16_test.cpp
@@ -11,7 +11,6 @@
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
-#include <iostream>
using LlvmLibcTanBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
@@ -26,50 +25,18 @@ static constexpr uint16_t NEG_START = 0x8000U;
static constexpr uint16_t NEG_STOP = 0xff80U;
TEST_F(LlvmLibcTanBf16Test, PositiveRange) {
- uint16_t last_equal = 0; // remove
for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
bfloat16 x = FPBits(v).get_val();
- bfloat16 result = LIBC_NAMESPACE::tanbf16(x);
- // Check if tan(x) rounds to x itself
- if (FPBits(result).uintval() == v) {
- last_equal = v;
- } else {
- // First x where tan(x) != x — print and stop
- bfloat16 prev = FPBits(last_equal).get_val();
- std::cout << "Last x where tan(x)==x: "
- << "hex=0x" << std::hex << last_equal << " value=" << std::dec
- << (float)prev << "\n";
- std::cout << "First x where tan(x)!=x: "
- << "hex=0x" << std::hex << v << " value=" << std::dec
- << (float)x << "\n";
- std::cout << "tan(first_diff_x)=" << (float)result << "\n";
- std::cout << "\n=== USE IN CODE ===\n"
- << "if (x_abs <= 0x" << std::hex << last_equal
- << "U) // tan(x)==x for bfloat16\n";
- break;
- }
-
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tan, x,
LIBC_NAMESPACE::tanbf16(x), 0.5);
}
}
TEST_F(LlvmLibcTanBf16Test, NegativeRange) {
- uint16_t last_equal = 0x8000; // remove
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
bfloat16 x = FPBits(v).get_val();
- bfloat16 result = LIBC_NAMESPACE::tanbf16(x);
- if (FPBits(result).uintval() == v) {
- last_equal = v;
- } else {
- std::cout << "Negative — last equal: "
- << "hex=0x" << std::hex << last_equal << " value=" << std::dec
- << (float)FPBits(last_equal).get_val() << "\n";
- break;
- }
-
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tan, x,
LIBC_NAMESPACE::tanbf16(x), 0.5);
}
>From 3e378cbcedbeb89a5773a1c3f7885981642b70c8 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 02:14:10 +0530
Subject: [PATCH 07/15] fix: reverted magnitude
---
libc/src/__support/math/tanbf16.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
index ba182433c6932..762feb1884190 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -57,10 +57,10 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
// The last value where tan(x) ~ x is 0x3db8
if (LIBC_UNLIKELY(x_abs <= 0x3db8)) {
int rounding = fputil::quick_get_round();
- // separate case handles it with magnitude of 2^-13
+ // separate case handles it with magnitude of 2^-11
if ((xbits.is_pos() && rounding == FE_UPWARD) ||
(xbits.is_neg() && rounding == FE_DOWNWARD))
- return fputil::cast<bfloat16>(fputil::multiply_add(xf, 0x1.0p-13f, xf));
+ return fputil::cast<bfloat16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
return x;
}
>From e0141e797d79d6ddaae4e04f3f73c85a39d51981 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 10:59:03 +0530
Subject: [PATCH 08/15] fix: added double intermediate for sincoseval
test
test-handling larger separately
testing with double
chore: format and corrected cmake dependencies
chore: err
---
libc/src/__support/math/CMakeLists.txt | 2 +-
libc/src/__support/math/tanbf16.h | 20 +++++++++++--------
.../llvm-project-overlay/libc/BUILD.bazel | 2 +-
3 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a39933f81cb71..ed5e04d6a90e4 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5405,7 +5405,7 @@ add_header_library(
HDRS
tanbf16.h
DEPENDS
- .sincosf16_utils
+ .sincosf_utils
libc.hdr.errno_macros
libc.hdr.fenv_macros
libc.src.__support.FPUtil.cast
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
index 762feb1884190..69b275c4a9eb5 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -11,7 +11,7 @@
#include "hdr/errno_macros.h"
#include "hdr/fenv_macros.h"
-#include "sincosf16_utils.h"
+#include "sincosf_utils.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/bfloat16.h"
@@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
- using namespace sincosf16_internal;
+ using namespace sincosf_utils_internal;
using FPBits = fputil::FPBits<bfloat16>;
FPBits xbits(x);
@@ -53,7 +53,7 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
return x;
}
- // Through Exhaustive testing
+ // Through Exhaustive testing -
// The last value where tan(x) ~ x is 0x3db8
if (LIBC_UNLIKELY(x_abs <= 0x3db8)) {
int rounding = fputil::quick_get_round();
@@ -64,13 +64,17 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
return x;
}
- float sin_k, cos_k, sin_y, cosm1_y;
- sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
+ double xd = static_cast<double>(xf);
+ uint32_t x_abs_d = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
+ double sin_k, cos_k, sin_y, cosm1_y;
+
+ sincosf_eval(xd, x_abs_d, sin_k, cos_k, sin_y, cosm1_y);
- using fputil::multiply_add;
return fputil::cast<bfloat16>(
- multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /
- multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k)));
+ fputil::multiply_add(sin_y, cos_k,
+ fputil::multiply_add(cosm1_y, sin_k, sin_k)) /
+ fputil::multiply_add(sin_y, -sin_k,
+ fputil::multiply_add(cosm1_y, cos_k, cos_k)));
}
} // namespace math
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 3321c6add97a9..246345c0e2a89 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -9186,7 +9186,7 @@ libc_support_library(
":__support_fputil_multiply_add",
":__support_macros_optimization",
":__support_macros_properties_types",
- ":__support_math_sincosf16_utils",
+ ":__support_math_sincosf_utils",
":hdr_errno_macros",
":hdr_fenv_macros",
],
>From 6638ad5de329fce05d313a624c7713617ad101ba Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 14:13:54 +0530
Subject: [PATCH 09/15] nit
---
libc/src/math/generic/CMakeLists.txt | 1 -
1 file changed, 1 deletion(-)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index bd96ea4ed2dea..5a5bd84213a55 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -425,7 +425,6 @@ add_entrypoint_object(
libc.src.__support.math.tanbf16
)
-
add_entrypoint_object(
tanf
SRCS
>From f3be1934637346cd3c09839e6348d68403e2d7dc Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 7 Jun 2026 16:04:58 +0530
Subject: [PATCH 10/15] add freebsd entrypoint
---
libc/config/freebsd/x86_64/entrypoints.txt | 1 +
1 file changed, 1 insertion(+)
diff --git a/libc/config/freebsd/x86_64/entrypoints.txt b/libc/config/freebsd/x86_64/entrypoints.txt
index 394b83130f067..7b8b8384e6dd8 100644
--- a/libc/config/freebsd/x86_64/entrypoints.txt
+++ b/libc/config/freebsd/x86_64/entrypoints.txt
@@ -627,6 +627,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.setpayloadbf16
libc.src.math.setpayloadsigbf16
libc.src.math.sqrtbf16
+ libc.src.math.tanbf16
libc.src.math.truncbf16
libc.src.math.totalorderbf16
libc.src.math.totalordermagbf16
>From 9e0e2df21d7655ec30295f80f97a6d32bc4d3cd6 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sun, 7 Jun 2026 16:13:30 +0530
Subject: [PATCH 11/15] nits
---
libc/test/shared/shared_math_test.cpp | 82 +++++++++++++++++++++++++++
1 file changed, 82 insertions(+)
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index a000991027ac6..5877729ae7931 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -812,5 +812,87 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::nextafterbf16(
bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::sqrtbf16(bfloat16(1.0)));
+ // TODO: This test case just failed only when building with gcc-13 and only
+ // for `ninja libc.test.shared.shared_math_test.__unit__.__NO_FMA_OPT
+ // Other gcc versions or other sub-targets work fine.
+ // https://github.com/llvm/llvm-project/issues/199332
+ // #ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+ // EXPECT_FP_EQ(bfloat16(0.0),
+ // LIBC_NAMESPACE::shared::nexttowardbf16(bfloat16(0.0),
+ // 0.0L));
+ // #endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
+
+ EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbbf16(bfloat16(1.0)));
+ EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogbbf16(bfloat16(1.0)));
+
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fmaximumbf16(
+ bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fminimumbf16(
+ bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::fminbf16(bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fmaximum_numbf16(
+ bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fminimum_numbf16(
+ bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::fromfpbf16(bfloat16(0.0), 0, 32));
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::fromfpxbf16(bfloat16(0.0), 0, 32));
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::ufromfpbf16(bfloat16(0.0), 0, 32));
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::ufromfpxbf16(bfloat16(0.0), 0, 32));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fmaximum_magbf16(
+ bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fminimum_magbf16(
+ bfloat16(0.0), bfloat16(0.0)));
+ bfloat16 totalorderbf16_x = bfloat16(0.0);
+ bfloat16 totalorderbf16_y = bfloat16(0.0);
+ EXPECT_EQ(1, LIBC_NAMESPACE::shared::totalorderbf16(&totalorderbf16_x,
+ &totalorderbf16_y));
+ bfloat16 totalordermagbf16_x = bfloat16(0.0);
+ bfloat16 totalordermagbf16_y = bfloat16(0.0);
+ EXPECT_EQ(1, LIBC_NAMESPACE::shared::totalordermagbf16(&totalordermagbf16_x,
+ &totalordermagbf16_y));
+ LIBC_NAMESPACE::shared::fmodbf16(bfloat16(1.0), bfloat16(1.0));
+ bfloat16 modfbf16_iptr = bfloat16(0.0);
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::modfbf16(bfloat16(0.0), &modfbf16_iptr));
+ EXPECT_FP_EQ(bfloat16(0.0), modfbf16_iptr);
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fminimum_mag_numbf16(
+ bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::remainderbf16(
+ bfloat16(1.0), bfloat16(1.0)));
+ int remquobf16_exp = 0;
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::remquobf16(bfloat16(1.0), bfloat16(1.0),
+ &remquobf16_exp));
+ EXPECT_EQ(1, remquobf16_exp);
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::ldexpbf16(bfloat16(0.0), 0));
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::scalblnbf16(bfloat16(0.0), 0L));
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::scalbnbf16(bfloat16(0.0), 0));
+ int frexpbf16_exp = 0;
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::frexpbf16(
+ bfloat16(0.0), &frexpbf16_exp));
+ EXPECT_EQ(0, frexpbf16_exp);
+ EXPECT_EQ(0L, LIBC_NAMESPACE::shared::lrintbf16(bfloat16(0.0)));
+ EXPECT_EQ(0L, LIBC_NAMESPACE::shared::lroundbf16(bfloat16(0.0)));
+ EXPECT_EQ(0LL, LIBC_NAMESPACE::shared::llrintbf16(bfloat16(0.0)));
+ EXPECT_EQ(0LL, LIBC_NAMESPACE::shared::llroundbf16(bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::nearbyintbf16(bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::rintbf16(bfloat16(0.0)));
+ EXPECT_EQ(1, LIBC_NAMESPACE::shared::iscanonicalbf16(bfloat16(0.0)));
+ EXPECT_EQ(0, LIBC_NAMESPACE::shared::issignalingbf16(bfloat16(0.0)));
+ EXPECT_TRUE(FPBits(LIBC_NAMESPACE::shared::nanbf16("")).is_nan());
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::roundbf16(bfloat16(0.0)));
+ 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::truncbf16(bfloat16(0.0)));
}
>From cfec02ee2da47ba833677a33177c506d19e27745 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Mon, 8 Jun 2026 00:27:53 +0530
Subject: [PATCH 12/15] nits
---
libc/src/__support/math/tanbf16.h | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
index 69b275c4a9eb5..d84a9653ed29d 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
- using namespace sincosf_utils_internal;
+
using FPBits = fputil::FPBits<bfloat16>;
FPBits xbits(x);
@@ -68,7 +68,11 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
uint32_t x_abs_d = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
double sin_k, cos_k, sin_y, cosm1_y;
- sincosf_eval(xd, x_abs_d, sin_k, cos_k, sin_y, cosm1_y);
+ // TODO: Use bfloat16 version for inv_trigf_utils_internals after they are
+ // available
+ // Tracking issue :
+ // https://github.com/llvm/llvm-project/issues/202079
+ sincosf_utils_internal::sincosf_eval(xd, x_abs_d, sin_k, cos_k, sin_y, cosm1_y);
return fputil::cast<bfloat16>(
fputil::multiply_add(sin_y, cos_k,
>From 9ae3a67b2df2fdae36425f12b9a3ac48f30088f8 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 25 Jun 2026 03:08:15 +0530
Subject: [PATCH 13/15] chore: remove sep case for rounding modes + nits
---
libc/src/__support/math/tanbf16.h | 21 ++++++++-------------
1 file changed, 8 insertions(+), 13 deletions(-)
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
index d84a9653ed29d..3dae149a4a643 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -48,31 +48,26 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
return x + FPBits::quiet_nan().get_val();
}
- // |x| = {0}
- if (LIBC_UNLIKELY(x_abs == 0)) {
- return x;
- }
-
// Through Exhaustive testing -
// The last value where tan(x) ~ x is 0x3db8
if (LIBC_UNLIKELY(x_abs <= 0x3db8)) {
- int rounding = fputil::quick_get_round();
- // separate case handles it with magnitude of 2^-11
- if ((xbits.is_pos() && rounding == FE_UPWARD) ||
- (xbits.is_neg() && rounding == FE_DOWNWARD))
- return fputil::cast<bfloat16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
- return x;
+ // |x| = {0}
+ if (LIBC_UNLIKELY(x_abs == 0)) {
+ return x;
+ }
+ return fputil::cast<bfloat16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
}
double xd = static_cast<double>(xf);
- uint32_t x_abs_d = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
+ uint32_t x_abs_f = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
double sin_k, cos_k, sin_y, cosm1_y;
// TODO: Use bfloat16 version for inv_trigf_utils_internals after they are
// available
// Tracking issue :
// https://github.com/llvm/llvm-project/issues/202079
- sincosf_utils_internal::sincosf_eval(xd, x_abs_d, sin_k, cos_k, sin_y, cosm1_y);
+ sincosf_utils_internal::sincosf_eval(xd, x_abs_f, sin_k, cos_k, sin_y,
+ cosm1_y);
return fputil::cast<bfloat16>(
fputil::multiply_add(sin_y, cos_k,
>From 034de179156a595d5757ea12e88d1290b9725b5e Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 25 Jun 2026 15:14:48 +0530
Subject: [PATCH 14/15] nits
---
libc/src/__support/math/CMakeLists.txt | 1 -
libc/src/math/generic/tanbf16.cpp | 1 -
2 files changed, 2 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index ed5e04d6a90e4..b1c07f2e4c6cc 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5399,7 +5399,6 @@ add_header_library(
libc.src.__support.macros.optimization
)
-
add_header_library(
tanbf16
HDRS
diff --git a/libc/src/math/generic/tanbf16.cpp b/libc/src/math/generic/tanbf16.cpp
index 40d0443a75fb1..990272fdcd677 100644
--- a/libc/src/math/generic/tanbf16.cpp
+++ b/libc/src/math/generic/tanbf16.cpp
@@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//
#include "src/math/tanbf16.h"
-#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/math/tanbf16.h"
namespace LIBC_NAMESPACE_DECL {
>From 96066f9343403cac920c437bcc9b19bbd1c56b20 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 25 Jun 2026 15:15:44 +0530
Subject: [PATCH 15/15] nits
---
libc/src/__support/math/CMakeLists.txt | 1 +
1 file changed, 1 insertion(+)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index b1c07f2e4c6cc..a57086a31dc03 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5374,6 +5374,7 @@ add_header_library(
libc.src.__support.uint128
libc.include.llvm-libc-types.float128
)
+
add_header_library(
sqrtl
HDRS
More information about the libc-commits
mailing list