[libc-commits] [libc] [llvm] [libc][math][c23] Add tanbf16 math function (PR #185100)
via libc-commits
libc-commits at lists.llvm.org
Tue Jun 23 20:59:24 PDT 2026
================
@@ -0,0 +1,84 @@
+//===-- 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.
+// 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 "sincosf_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/multiply_add.h"
+#include "src/__support/macros/optimization.h"
+
+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);
+
+ 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()) {
+ 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;
+ }
+
+ // 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));
----------------
lntue wrote:
Why don't we just do `fputil::multiply_add(xf, 0x1.0p-13f, xf)` for all the rounding modes, and don't need to do `quick_get_round()`.
https://github.com/llvm/llvm-project/pull/185100
More information about the libc-commits
mailing list