[libc-commits] [libc] [libc][math] Impl bfloat16 lgamma function. (PR #199312)

Kiriti Ponduri via libc-commits libc-commits at lists.llvm.org
Sat May 23 01:59:26 PDT 2026


https://github.com/udaykiriti updated https://github.com/llvm/llvm-project/pull/199312

>From 813062edac0e87f4c3a70021aa01d30e1522231e Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 07:56:37 +0530
Subject: [PATCH 1/4] [libc][math] Impl bfloat16 lgamma function.

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/config/linux/x86_64/entrypoints.txt     |  1 +
 libc/src/__support/math/lgammabf16.h         | 27 ++++++++++++++++++++
 libc/src/math/CMakeLists.txt                 |  1 +
 libc/src/math/generic/CMakeLists.txt         |  9 +++++++
 libc/src/math/generic/lgammabf16.cpp         | 18 +++++++++++++
 libc/src/math/lgammabf16.h                   | 21 +++++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt      | 11 ++++++++
 libc/test/src/math/smoke/lgammabf16_test.cpp | 19 ++++++++++++++
 8 files changed, 107 insertions(+)
 create mode 100644 libc/src/__support/math/lgammabf16.h
 create mode 100644 libc/src/math/generic/lgammabf16.cpp
 create mode 100644 libc/src/math/lgammabf16.h
 create mode 100644 libc/test/src/math/smoke/lgammabf16_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5545790fecd85..0723e58d0dd57 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -666,6 +666,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.ldexp
     libc.src.math.ldexpf
     libc.src.math.ldexpl
+    libc.src.math.lgammabf16
     libc.src.math.llogb
     libc.src.math.llogbf
     libc.src.math.llogbl
diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
new file mode 100644
index 0000000000000..cc8da2dee77f3
--- /dev/null
+++ b/libc/src/__support/math/lgammabf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for lgammabf16 --------------------*- 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_LGAMMABF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LGAMMABF16_H
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
+  return fputil::cast<bfloat16>(__builtin_lgammaf(fputil::cast<float>(x)));
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LGAMMABF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index b53817e2a1729..b50b848df999e 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -596,6 +596,7 @@ add_math_entrypoint_object(tanpif16)
 add_math_entrypoint_object(tgamma)
 add_math_entrypoint_object(tgammaf)
 add_math_entrypoint_object(lgamma)
+add_math_entrypoint_object(lgammabf16)
 add_math_entrypoint_object(lgamma_r)
 
 add_math_entrypoint_object(totalorder)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 7ccbddba07b8d..375d03da28e7b 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4868,6 +4868,15 @@ add_entrypoint_object(
   DEPENDS
     libc.src.__support.math.bf16fmal
 )
+add_entrypoint_object(
+  lgammabf16
+  SRCS
+    lgammabf16.cpp
+  HDRS
+    ../lgammabf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.cast
+)
 
 add_entrypoint_object(
   bf16fmaf128
diff --git a/libc/src/math/generic/lgammabf16.cpp b/libc/src/math/generic/lgammabf16.cpp
new file mode 100644
index 0000000000000..6ae7e69134a1c
--- /dev/null
+++ b/libc/src/math/generic/lgammabf16.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of lgammabf16 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/lgammabf16.h"
+#include "src/__support/math/lgammabf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(bfloat16, lgammabf16, (bfloat16 x)) {
+  return math::lgammabf16(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/lgammabf16.h b/libc/src/math/lgammabf16.h
new file mode 100644
index 0000000000000..965c8c881eb03
--- /dev/null
+++ b/libc/src/math/lgammabf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for lgammabf16 --------------------*- 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_LGAMMABF16_H
+#define LLVM_LIBC_SRC_MATH_LGAMMABF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+bfloat16 lgammabf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_LGAMMABF16_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 28b85b1a25bbd..78161561387f1 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -36,6 +36,17 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  lgammabf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    lgammabf16_test.cpp
+  DEPENDS
+    libc.src.math.lgammabf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   cospif16_test
   SUITE
diff --git a/libc/test/src/math/smoke/lgammabf16_test.cpp b/libc/test/src/math/smoke/lgammabf16_test.cpp
new file mode 100644
index 0000000000000..5bb0ae4e81bca
--- /dev/null
+++ b/libc/test/src/math/smoke/lgammabf16_test.cpp
@@ -0,0 +1,19 @@
+//===-- Smoke tests for lgammabf16 -----------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/lgammabf16.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcLgammabf16Test, SpecialNumbers) {
+  LIBC_NAMESPACE::fputil::BFloat16 one(1.0f);
+  LIBC_NAMESPACE::fputil::BFloat16 two(2.0f);
+  // lgamma(1) = 0, lgamma(2) = 0
+  (void)LIBC_NAMESPACE::lgammabf16(one);
+  (void)LIBC_NAMESPACE::lgammabf16(two);
+}

>From 85e7216b9195b47db04ad7ca671b14662d0b2a45 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 09:03:14 +0530
Subject: [PATCH 2/4] [libc][math] lgammabf16 cmake dependencies

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/src/math/generic/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 375d03da28e7b..6a2615f92a53e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4875,7 +4875,7 @@ add_entrypoint_object(
   HDRS
     ../lgammabf16.h
   DEPENDS
-    libc.src.__support.FPUtil.cast
+    libc.src.__support.math.lgammabf16
 )
 
 add_entrypoint_object(

>From 371e5674fea210ab1e5a0c44f5bcf26d86f27e28 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 09:07:58 +0530
Subject: [PATCH 3/4] [libc][math] Define lgammabf16 in
 __support/math/CMakeLists.txt

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/src/__support/math/CMakeLists.txt | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 9e3ec26cdc881..a389a37e37a0f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -46,6 +46,17 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
+add_header_library(
+  lgammabf16
+  HDRS
+    lgammabf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.math.logf
+    libc.src.math.sinf
+)
+
 add_header_library(
   acosh_float_constants
   HDRS

>From 87af769bb592027d35f22550890f439ed7ed18a4 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 13:45:20 +0530
Subject: [PATCH 4/4] implemented lgammabf16

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 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      |  2 +-
 libc/shared/math.h                            |  1 +
 libc/shared/math/lgammabf16.h                 | 23 +++++++++
 libc/src/__support/math/CMakeLists.txt        |  4 +-
 libc/src/__support/math/lgammabf16.h          | 29 +++++++++--
 libc/src/math/lgammabf16.h                    |  2 +-
 libc/test/shared/CMakeLists.txt               |  1 +
 libc/test/shared/shared_math_test.cpp         |  3 +-
 libc/test/src/math/smoke/CMakeLists.txt       |  1 +
 libc/test/src/math/smoke/lgammabf16_test.cpp  | 50 ++++++++++++++++---
 20 files changed, 107 insertions(+), 19 deletions(-)
 create mode 100644 libc/shared/math/lgammabf16.h

diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index dcb50135232e2..2c27769fe0786 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -835,6 +835,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index fac62bac939cc..78b0ee578f472 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.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index a3b96225ff09d..9893ee44d4e18 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -843,6 +843,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 914d2b7918da1..56abccb09dce7 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -656,6 +656,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index b941c968255e8..bc164e4fcd3f6 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -278,6 +278,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index c76900a8371f7..913db74f49f12 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -684,6 +684,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 6538732f785f5..1fdcd32ff10ee 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -686,6 +686,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index a50923fddec54..e7d42623cd56f 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -948,6 +948,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 49b30ef1830f3..8bac9164441d6 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.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 51c769d853a52..5374674401b65 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -969,6 +969,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 0723e58d0dd57..c3d284b758254 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -666,7 +666,6 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.ldexp
     libc.src.math.ldexpf
     libc.src.math.ldexpl
-    libc.src.math.lgammabf16
     libc.src.math.llogb
     libc.src.math.llogbf
     libc.src.math.llogbl
@@ -1035,6 +1034,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.iscanonicalbf16
   libc.src.math.issignalingbf16
   libc.src.math.ldexpbf16
+  libc.src.math.lgammabf16
   libc.src.math.llogbbf16
   libc.src.math.llrintbf16
   libc.src.math.llroundbf16
diff --git a/libc/shared/math.h b/libc/shared/math.h
index 2baeb07294a3b..9482efb1dc1b7 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -305,6 +305,7 @@
 #include "math/ldexpf128.h"
 #include "math/ldexpf16.h"
 #include "math/ldexpl.h"
+#include "math/lgammabf16.h"
 #include "math/llogb.h"
 #include "math/llogbbf16.h"
 #include "math/llogbf.h"
diff --git a/libc/shared/math/lgammabf16.h b/libc/shared/math/lgammabf16.h
new file mode 100644
index 0000000000000..0317926a46c03
--- /dev/null
+++ b/libc/shared/math/lgammabf16.h
@@ -0,0 +1,23 @@
+//===-- Shared header for lgammabf16 ----------------------------*- 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_LGAMMABF16_H
+#define LLVM_LIBC_SHARED_MATH_LGAMMABF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/math/lgammabf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using ::LIBC_NAMESPACE::math::lgammabf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_LGAMMABF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a389a37e37a0f..ac4bb1dd5f06f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -52,9 +52,7 @@ add_header_library(
     lgammabf16.h
   DEPENDS
     libc.src.__support.FPUtil.cast
-    libc.src.__support.FPUtil.fp_bits
-    libc.src.math.logf
-    libc.src.math.sinf
+    libc.src.__support.macros.properties
 )
 
 add_header_library(
diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index cc8da2dee77f3..148ac3850e5ee 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -1,24 +1,43 @@
-//===-- Implementation header for lgammabf16 --------------------*- C++ -*-===//
+//===-- Implementation of lgammabf16 ----------------------------*- 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_LGAMMABF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_LGAMMABF16_H
 
-#include "src/__support/FPUtil/bfloat16.h"
 #include "src/__support/FPUtil/cast.h"
-#include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+#ifdef LIBC_TARGET_OS_IS_WINDOWS
+#include <math.h> // lgammaf for MSVC
+#endif
 
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
+// lgammabf16(x) = lgamma(x) rounded to bfloat16
+//
+// Strategy: promote bfloat16 -> float, call lgammaf, then narrow the float
+// result back to bfloat16.
+//
+// This is correctly rounded because bfloat16 has only 7 mantissa bits vs
+// float's 23, so the float result has far more precision than needed; a
+// single narrowing cast gives a correctly-rounded bfloat16 result.
+//
+// Special-case behaviour (NaN, ±0 → +Inf, negative integers → +Inf,
+// ±Inf → +Inf) is inherited transparently from the underlying lgammaf.
+// The global signgam is set as a side effect.
 LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
-  return fputil::cast<bfloat16>(__builtin_lgammaf(fputil::cast<float>(x)));
+  float xf = fputil::cast<float>(x);
+#ifdef LIBC_TARGET_OS_IS_WINDOWS
+  return fputil::cast<bfloat16>(::lgammaf(xf));
+#else
+  return fputil::cast<bfloat16>(__builtin_lgammaf(xf));
+#endif
 }
 
 } // namespace math
diff --git a/libc/src/math/lgammabf16.h b/libc/src/math/lgammabf16.h
index 965c8c881eb03..4c63945c6a6a8 100644
--- a/libc/src/math/lgammabf16.h
+++ b/libc/src/math/lgammabf16.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_MATH_LGAMMABF16_H
 #define LLVM_LIBC_SRC_MATH_LGAMMABF16_H
 
+#include "src/__support/FPUtil/bfloat16.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/properties/types.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 84a8d4bf79b3d..237a7b1e209f1 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -297,6 +297,7 @@ add_fp_unittest(
     libc.src.__support.math.ldexpbf16
     libc.src.__support.math.ldexpf
     libc.src.__support.math.ldexpl
+    libc.src.__support.math.lgammabf16
     libc.src.__support.math.llogb
     libc.src.__support.math.llogbbf16
     libc.src.__support.math.llrint
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 634778380dc8e..c42c2ffd12f53 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -785,7 +785,8 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
 
   EXPECT_FP_EQ(bfloat16(5.0),
                LIBC_NAMESPACE::shared::hypotbf16(bfloat16(4.0), bfloat16(3.0)));
-
+  EXPECT_FP_EQ(bfloat16(0.0f),
+               LIBC_NAMESPACE::shared::lgammabf16(bfloat16(1.0f)));
   EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::logbbf16(bfloat16(1.0f)));
 
   bfloat16 setpayloadbf16_res = bfloat16(0.0);
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 78161561387f1..ad7387a52d41c 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -38,6 +38,7 @@ add_fp_unittest(
 
 add_fp_unittest(
   lgammabf16_test
+  UNIT_TEST_ONLY
   SUITE
     libc-math-smoke-tests
   SRCS
diff --git a/libc/test/src/math/smoke/lgammabf16_test.cpp b/libc/test/src/math/smoke/lgammabf16_test.cpp
index 5bb0ae4e81bca..3fd45abd8df8a 100644
--- a/libc/test/src/math/smoke/lgammabf16_test.cpp
+++ b/libc/test/src/math/smoke/lgammabf16_test.cpp
@@ -1,4 +1,4 @@
-//===-- Smoke tests for lgammabf16 -----------------------------*- C++ -*-===//
+//===-- Unittests for lgammabf16 ------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,13 +7,47 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.h"
 #include "src/math/lgammabf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
-TEST(LlvmLibcLgammabf16Test, SpecialNumbers) {
-  LIBC_NAMESPACE::fputil::BFloat16 one(1.0f);
-  LIBC_NAMESPACE::fputil::BFloat16 two(2.0f);
-  // lgamma(1) = 0, lgamma(2) = 0
-  (void)LIBC_NAMESPACE::lgammabf16(one);
-  (void)LIBC_NAMESPACE::lgammabf16(two);
-}
+class LlvmLibcLgammaBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+  DECLARE_SPECIAL_CONSTANTS(bfloat16)
+
+public:
+  void test_special_numbers() {
+    // lgammabf16(NaN) = NaN
+    EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::lgammabf16(aNaN));
+
+    // lgammabf16(+0) = +Inf (pole error)
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(zero));
+
+    // lgammabf16(-0) = +Inf (pole error)
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(neg_zero));
+
+    // lgammabf16(+Inf) = +Inf
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(inf));
+
+    // lgammabf16(-Inf) = +Inf
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(neg_inf));
+
+    // lgamma(1.0) = 0.0
+    EXPECT_FP_EQ(zero, LIBC_NAMESPACE::lgammabf16(
+                           LIBC_NAMESPACE::fputil::cast<bfloat16>(1.0f)));
+
+    // lgamma(2.0) = 0.0
+    EXPECT_FP_EQ(zero, LIBC_NAMESPACE::lgammabf16(
+                           LIBC_NAMESPACE::fputil::cast<bfloat16>(2.0f)));
+
+    // lgamma(negative integer) = +Inf (pole error)
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(
+                          LIBC_NAMESPACE::fputil::cast<bfloat16>(-1.0f)));
+
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(
+                          LIBC_NAMESPACE::fputil::cast<bfloat16>(-2.0f)));
+  }
+};
+
+TEST_F(LlvmLibcLgammaBf16Test, SpecialNumbers) { test_special_numbers(); }



More information about the libc-commits mailing list