[libc] [llvm] [libc][math][c23] Add tanbf16 math function (PR #185100)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 6 23:46:41 PST 2026
https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/185100
>From 5b7051d8c71e846ba08b327e79ca354f941fb2ac 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/10] 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/__support/math/tanf16.h | 2 +-
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 | 1 +
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 ++++++
28 files changed, 358 insertions(+), 1 deletion(-)
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 278e1f5afde70..f6d4b278d450c 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -843,6 +843,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 7bc92e84b7fa4..c64391b2b1fa3 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -846,6 +846,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 29be47dfbfd53..7cf7c91757789 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -841,6 +841,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 b4a1ee70764cb..d7ed493478138 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -657,6 +657,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 e899bf97ea3f6..11aa2e175ce04 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -297,6 +297,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 a65b6f0274fd8..2e44b1b7a1f4c 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -682,6 +682,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 fee0038c88cc0..407c1ebf7569f 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -684,6 +684,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 960958b7cf90a..eef57eb0ce9e9 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -913,6 +913,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 e76bf81670ce5..d3de94da2dbfe 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -526,6 +526,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 a69d1a1b0a642..9860bc2e187ff 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -934,6 +934,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 4103ee0331a23..614b1459b78b6 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -984,6 +984,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 f777fc6c94bc2..5dec00b2dfe15 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -372,6 +372,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 a7d735ffa1746..9092cdf29a2cb 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -233,6 +233,7 @@
#include "math/sqrtf128.h"
#include "math/sqrtf16.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 79278b6e77a3b..eeccb8c0f167d 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -2887,6 +2887,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/__support/math/tanf16.h b/libc/src/__support/math/tanf16.h
index 6b9b9224fb84d..eda872ee7ab0a 100644
--- a/libc/src/__support/math/tanf16.h
+++ b/libc/src/__support/math/tanf16.h
@@ -76,7 +76,7 @@ LIBC_INLINE float16 tanf16(float16 x) {
// tan(x) = x * 2^-11 + x
if ((xbits.is_pos() && rounding == FE_UPWARD) ||
(xbits.is_neg() && rounding == FE_DOWNWARD))
- return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
+ return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-13f, xf));
return x;
}
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index e617950368994..f1559fc49ade1 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -571,6 +571,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 f8ec25be61d12..1f4abf5373ad5 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -419,6 +419,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 a04a15cdabcb7..91402ecdf1127 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -231,6 +231,7 @@ add_fp_unittest(
libc.src.__support.math.sqrt
libc.src.__support.math.sqrtf
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 460449e4fcb2e..941a94df604fe 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -450,4 +450,5 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
LIBC_NAMESPACE::shared::nexttowardbf16(bfloat16(0.0), 0.0L));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::nextafterbf16(
bfloat16(0.0), 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 73b5ebf5a856e..f01b8b7a7dacf 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -202,6 +202,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 3d52873c40bb8..4d06afea8baca 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 87d1d88e971bf..2e061d221fc49 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5527,6 +5527,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"],
@@ -7918,6 +7936,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 d5a7dbfa54700883095c4f8af275e893ecb4e31a 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/10] 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 eeccb8c0f167d..0c0ccaef4a0ae 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -2899,7 +2899,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 2e061d221fc49..8741309f2919c 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5532,7 +5532,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 4d1c9f368e5bdc19abe98980fa51e30f9d5e3309 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/10] 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 8741309f2919c..989ee85cec9b0 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5531,6 +5531,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",
@@ -5540,7 +5541,6 @@ libc_support_library(
":__support_math_sincosf16_utils",
":hdr_errno_macros",
":hdr_fenv_macros",
- ":__support_fputil_bfloat16",
],
)
@@ -7942,7 +7942,6 @@ libc_math_function(
],
)
-
libc_math_function(
name = "tanf",
additional_deps = [":__support_math_tanf"],
>From b3715e113b25d2a176455a90479eca6572410205 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/10] chore: nit
---
libc/src/__support/math/tanbf16.h | 2 +-
libc/src/__support/math/tanf16.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
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;
}
diff --git a/libc/src/__support/math/tanf16.h b/libc/src/__support/math/tanf16.h
index eda872ee7ab0a..6b9b9224fb84d 100644
--- a/libc/src/__support/math/tanf16.h
+++ b/libc/src/__support/math/tanf16.h
@@ -76,7 +76,7 @@ LIBC_INLINE float16 tanf16(float16 x) {
// tan(x) = x * 2^-11 + x
if ((xbits.is_pos() && rounding == FE_UPWARD) ||
(xbits.is_neg() && rounding == FE_DOWNWARD))
- return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-13f, xf));
+ return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
return x;
}
>From 570fc17b1628a6b01836846e70a3625cbf7188e5 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/10] 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 7ea54fb4d8263..ce57fb959978e 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -359,7 +359,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 cd5091c5055058e63410d245d82cb77d96ea11d1 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/10] 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 7d30cff472f93112cb6c28e433bf0847873055ac 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/10] 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 7d41d231d5684d174ae3708843410c4889956ea6 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/10] test
---
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 762feb1884190..f452067054ef0 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -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();
>From 4708618ea3cb3b475f1f76c146231037f8874902 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 12:50:27 +0530
Subject: [PATCH 09/10] test-handling larger separately
---
libc/src/__support/math/tanbf16.h | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
index f452067054ef0..144e8b6f9bc49 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -11,6 +11,7 @@
#include "hdr/errno_macros.h"
#include "hdr/fenv_macros.h"
+#include "range_reduction.h"
#include "sincosf16_utils.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
@@ -65,7 +66,17 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
}
float sin_k, cos_k, sin_y, cosm1_y;
- sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
+
+ if (LIBC_UNLIKELY(x_abs >= 0x4580)) {
+ double y;
+ int x_exp = fputil::FPBits<float>(xf).get_exponent();
+ int64_t k = trigonometric_func_utils_internal::large_range_reduction(
+ static_cast<double>(xf), x_exp, y);
+ sincosf16_poly_eval(static_cast<int32_t>(k & 63), static_cast<float>(y),
+ sin_k, cos_k, sin_y, cosm1_y);
+ } else {
+ sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
+ }
using fputil::multiply_add;
return fputil::cast<bfloat16>(
>From 6b5384d8ff88d7034d261423d506db90f4bae9d3 Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Sat, 7 Mar 2026 13:16:04 +0530
Subject: [PATCH 10/10] testing with double
---
libc/src/__support/math/tanbf16.h | 27 ++++++++++-----------------
1 file changed, 10 insertions(+), 17 deletions(-)
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
index 144e8b6f9bc49..8eabbd02f97d3 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -11,8 +11,7 @@
#include "hdr/errno_macros.h"
#include "hdr/fenv_macros.h"
-#include "range_reduction.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"
@@ -25,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);
@@ -65,23 +64,17 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
return x;
}
- float 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;
- if (LIBC_UNLIKELY(x_abs >= 0x4580)) {
- double y;
- int x_exp = fputil::FPBits<float>(xf).get_exponent();
- int64_t k = trigonometric_func_utils_internal::large_range_reduction(
- static_cast<double>(xf), x_exp, y);
- sincosf16_poly_eval(static_cast<int32_t>(k & 63), static_cast<float>(y),
- sin_k, cos_k, sin_y, cosm1_y);
- } else {
- sincosf16_eval(xf, 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
More information about the llvm-commits
mailing list