[libc-commits] [libc] [llvm] [libc][math] Add tgammabf16 math support for bfloat16. (PR #208689)
via libc-commits
libc-commits at lists.llvm.org
Wed Jul 15 13:51:24 PDT 2026
================
@@ -0,0 +1,187 @@
+//===-- Implementation header for tgammabf16 --------------------*- 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___SUPPORT_MATH_TGAMMABF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_TGAMMABF16_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.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/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/math/exp.h"
+#include "src/__support/math/log.h"
+#include "src/__support/math/sin.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+namespace tgammabf16_internal {
+
+LIBC_INLINE_VAR constexpr double PI = 0x1.921fb54442d18p+1;
+LIBC_INLINE_VAR constexpr double LOG_SQRT_2_PI = 0x1.d67f1c864beb5p-1;
+
+// Paul Godfrey's exact Lanczos approximation coefficients (g=7, n=9)
+// Reference: "A note on the computation of the convergent Lanczos complex Gamma
+// approximation" by Paul Godfrey (2001).
+// Original:
+// https://web.archive.org/web/20060915161115/http://my.fit.edu/~gabdo/gamma.txt
+// Mirror: http://www.mrob.com/pub/ries/lanczos-gamma.html
+LIBC_INLINE_VAR constexpr double LANCZOS_COEFFS[9] = {
+ 0x1.ffffffffff950p-1, 0x1.52429b6c30b05p+9, -0x1.3ac8e8ed4171bp+10,
+ 0x1.81a9661d3b4d8p+9, -0x1.613ae51a32f5dp+7, 0x1.903c27f8b9c81p+3,
+ -0x1.1bcb2992b2855p-3, 0x1.4f0514e4e324fp-17, 0x1.435508f3faeefp-23};
+
+} // namespace tgammabf16_internal
+
+LIBC_INLINE bfloat16 tgammabf16(bfloat16 x) {
+ using FPBits = fputil::FPBits<bfloat16>;
+ using namespace tgammabf16_internal;
+
+ FPBits xbits(x);
+
+ if (LIBC_UNLIKELY(xbits.is_nan())) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x;
+ }
+
+ if (LIBC_UNLIKELY(xbits.is_inf())) {
+ if (xbits.is_pos())
+ return x;
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
----------------
Sukumarsawant wrote:
can have something similar to
`if (LIBC_UNLIKELY(x_abs >= 0x7F80))`
and be nested (i.e nan +inf )
let tue confirm this .
https://github.com/llvm/llvm-project/pull/208689
More information about the libc-commits
mailing list