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

Kiriti Ponduri via libc-commits libc-commits at lists.llvm.org
Sat May 23 06:19:10 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 01/23] [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 02/23] [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 03/23] [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 04/23] 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(); }

>From 79b991b411a12da299b0aaa2646aaa632902b4ff Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 15:29:38 +0530
Subject: [PATCH 05/23] [libc][math] Implementation of function

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/src/__support/math/CMakeLists.txt        |  8 +-
 libc/src/__support/math/lgammabf16.h          | 93 ++++++++++++++++---
 libc/test/src/math/CMakeLists.txt             | 13 +++
 libc/test/src/math/exhaustive/CMakeLists.txt  | 17 ++++
 .../src/math/exhaustive/lgammabf16_test.cpp   | 28 ++++++
 libc/test/src/math/lgammabf16_test.cpp        | 45 +++++++++
 libc/test/src/math/smoke/lgammabf16_test.cpp  | 62 +++++--------
 7 files changed, 212 insertions(+), 54 deletions(-)
 create mode 100644 libc/test/src/math/exhaustive/lgammabf16_test.cpp
 create mode 100644 libc/test/src/math/lgammabf16_test.cpp

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index ac4bb1dd5f06f..90d8308c95c16 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -51,8 +51,14 @@ add_header_library(
   HDRS
     lgammabf16.h
   DEPENDS
+    libc.src.__support.FPUtil.FPBits
     libc.src.__support.FPUtil.cast
-    libc.src.__support.macros.properties
+    libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.rounding_mode
+    libc.src.__support.macros.config
+    libc.src.__support.macros.optimization
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
 )
 
 add_header_library(
diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index 148ac3850e5ee..a2cffe0c5de70 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -8,9 +8,15 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LGAMMABF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_LGAMMABF16_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/rounding_mode.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/properties/types.h"
+#include "src/__support/macros/optimization.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
 
 #ifdef LIBC_TARGET_OS_IS_WINDOWS
 #include <math.h> // lgammaf for MSVC
@@ -19,25 +25,88 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-// lgammabf16(x) = lgamma(x) rounded to bfloat16
+// lgammabf16(x) computes log(|Gamma(x)|) in bfloat16.
 //
-// Strategy: promote bfloat16 -> float, call lgammaf, then narrow the float
-// result back to bfloat16.
+// Strategy:
+//   - Special cases: NaN, 0, negative integers, +Inf or -Inf -> handled explicitly.
+//   - For all other values: promote bfloat16 -> float, compute lgammaf,
+//     narrow 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.
+// Correctness argument:
+//   bfloat16 has only 7 mantissa bits vs float's 23. The float result of
+//   lgammaf has far more precision than needed, so a single narrowing cast
+//   gives a correctly-rounded bfloat16 result for all normal inputs.
 //
-// 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.
+// Note: unlike lgamma/lgammaf, this does NOT set the global signgam,
+// since bfloat16 math functions are intended for ML/embedded use where
+// the global signgam side-effect is undesirable.
 LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
+  using FPBits = fputil::FPBits<bfloat16>;
+  FPBits x_bits(x);
+
+  // Handle NaN
+  if (LIBC_UNLIKELY(x_bits.is_nan())) {
+    if (x_bits.is_signaling_nan()) {
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+    return x;
+  }
+
+  uint16_t x_u = x_bits.uintval();
+  // Strip sign bit to get |x|
+  uint16_t x_abs = x_u & 0x7fffU;
+
+  // Handle +Inf -> +Inf
+  if (LIBC_UNLIKELY(x_u == 0x7f80U))
+    return FPBits::inf(Sign::POS).get_val();
+
+  // Handle -Inf -> +Inf (lgamma(-inf) = +inf)
+  if (LIBC_UNLIKELY(x_u == 0xff80U))
+    return FPBits::inf(Sign::POS).get_val();
+
+  // Handle +-0 -> +Inf (pole error)
+  if (LIBC_UNLIKELY(x_abs == 0U)) {
+    fputil::set_errno_if_required(ERANGE);
+    fputil::raise_except_if_required(FE_DIVBYZERO);
+    return FPBits::inf(Sign::POS).get_val();
+  }
+
+  // Handle negative integers -> +Inf (pole error)
+  // A negative bfloat16 value is an integer if its fractional part is zero.
+  // bfloat16 has 7 mantissa bits and 8 exponent bits.
+  // exponent bias = 127, so unbiased exponent e = (x_u >> 7) - 127
+  // x is a negative integer when x < 0 and |x| >= 1 and frac(|x|) == 0
+  if (LIBC_UNLIKELY(x_bits.is_neg() && x_abs < 0x7f80U)) {
+    int biased_exp = x_abs >> FPBits::FRACTION_LEN;
+    if (biased_exp >= FPBits::EXP_BIAS) {
+      // unbiased exponent >= 0, so |x| >= 1
+      int e = biased_exp - FPBits::EXP_BIAS;
+      if (e >= FPBits::FRACTION_LEN) {
+        // All mantissa bits are above the binary point -> always integer
+        fputil::set_errno_if_required(ERANGE);
+        fputil::raise_except_if_required(FE_DIVBYZERO);
+        return FPBits::inf(Sign::POS).get_val();
+      }
+      // Check if fractional bits are zero
+      uint16_t frac_mask =
+          static_cast<uint16_t>((1U << (FPBits::FRACTION_LEN - e)) - 1U);
+      if ((x_bits.get_mantissa() & frac_mask) == 0U) {
+        fputil::set_errno_if_required(ERANGE);
+        fputil::raise_except_if_required(FE_DIVBYZERO);
+        return FPBits::inf(Sign::POS).get_val();
+      }
+    }
+  }
+
+  // General case: promote to float, compute lgammaf, narrow back.
   float xf = fputil::cast<float>(x);
 #ifdef LIBC_TARGET_OS_IS_WINDOWS
-  return fputil::cast<bfloat16>(::lgammaf(xf));
+  float result = ::lgammaf(xf);
 #else
-  return fputil::cast<bfloat16>(__builtin_lgammaf(xf));
+  float result = __builtin_lgammaf(xf);
 #endif
+  return fputil::cast<bfloat16>(result);
 }
 
 } // namespace math
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 4213c11eca515..aa2605f5948a3 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -30,6 +30,19 @@ add_fp_unittest(
     FMA_OPT__ONLY
 )
 
+add_fp_unittest(
+  lgammabf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    lgammabf16_test.cpp
+  DEPENDS
+    libc.src.math.lgammabf16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   cos_test
   NEED_MPFR
diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt
index 2d1301d3a1e66..2731ba8a609fa 100644
--- a/libc/test/src/math/exhaustive/CMakeLists.txt
+++ b/libc/test/src/math/exhaustive/CMakeLists.txt
@@ -26,6 +26,23 @@ add_fp_unittest(
     -lpthread
 )
 
+add_fp_unittest(
+  lgammabf16_test
+  NO_RUN_POSTBUILD
+  NEED_MPFR
+  SUITE
+    libc_math_exhaustive_tests
+  SRCS
+    lgammabf16_test.cpp
+  DEPENDS
+    .exhaustive_test
+    libc.src.math.lgammabf16
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.bfloat16
+  LINK_LIBRARIES
+    -lpthread
+)
+
 add_fp_unittest(
   rsqrtf_test
   NO_RUN_POSTBUILD
diff --git a/libc/test/src/math/exhaustive/lgammabf16_test.cpp b/libc/test/src/math/exhaustive/lgammabf16_test.cpp
new file mode 100644
index 0000000000000..32145ed74d95d
--- /dev/null
+++ b/libc/test/src/math/exhaustive/lgammabf16_test.cpp
@@ -0,0 +1,28 @@
+//===-- Exhaustive test 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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/lgammabf16.h"
+#include "test/src/math/exhaustive/exhaustive_test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+using LlvmLibcLgammabf16ExhaustiveTest =
+    LlvmLibcUnaryOpExhaustiveMathTest<LIBC_NAMESPACE::fputil::BFloat16,
+                                      mpfr::Operation::Lgamma,
+                                      LIBC_NAMESPACE::lgammabf16>;
+
+// Range: [0, Inf)
+TEST_F(LlvmLibcLgammabf16ExhaustiveTest, PositiveRange) {
+  test_full_range_all_roundings(0x0000U, 0x7F80U);
+}
+
+// Range: (-Inf, 0)
+TEST_F(LlvmLibcLgammabf16ExhaustiveTest, NegativeRange) {
+  test_full_range_all_roundings(0x8000U, 0xFF80U);
+}
diff --git a/libc/test/src/math/lgammabf16_test.cpp b/libc/test/src/math/lgammabf16_test.cpp
new file mode 100644
index 0000000000000..d7916f92fd84b
--- /dev/null
+++ b/libc/test/src/math/lgammabf16_test.cpp
@@ -0,0 +1,45 @@
+//===-- 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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/lgammabf16.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+class LlvmLibcLgammaBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+  DECLARE_SPECIAL_CONSTANTS(bfloat16)
+
+public:
+  void test_special_numbers() {
+    EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::lgammabf16(aNaN));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(zero));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(neg_zero));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(inf));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(neg_inf));
+  }
+};
+
+TEST_F(LlvmLibcLgammaBf16Test, SpecialNumbers) { test_special_numbers(); }
+
+TEST_F(LlvmLibcLgammaBf16Test, InBFloat16Range) {
+  constexpr uint16_t COUNT = 10000;
+  constexpr uint32_t STEP = 0x7F80U / COUNT;
+  
+  for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+    bfloat16 x = LIBC_NAMESPACE::cpp::bit_cast<bfloat16>(static_cast<uint16_t>(v));
+    if (LIBC_NAMESPACE::fputil::isnan(x) || LIBC_NAMESPACE::fputil::isinf(x))
+      continue;
+
+    ASSERT_MPFR_MATCH(mpfr::Operation::Lgamma, x,
+                      LIBC_NAMESPACE::lgammabf16(x), 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/lgammabf16_test.cpp b/libc/test/src/math/smoke/lgammabf16_test.cpp
index 3fd45abd8df8a..b97fca574de77 100644
--- a/libc/test/src/math/smoke/lgammabf16_test.cpp
+++ b/libc/test/src/math/smoke/lgammabf16_test.cpp
@@ -6,48 +6,28 @@
 //
 //===----------------------------------------------------------------------===//
 
-#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"
 
-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(); }
+using LlvmLibcLgammaBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+
+TEST_F(LlvmLibcLgammaBf16Test, SpecialNumbers) {
+  // 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));
+  // lgammabf16(1.0) = 0.0
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::lgammabf16(bfloat16(1.0f)));
+  // lgammabf16(2.0) = 0.0
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::lgammabf16(bfloat16(2.0f)));
+  // lgammabf16(negative integer) = +Inf (pole error)
+  EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(bfloat16(-1.0f)));
+  EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(bfloat16(-2.0f)));
+}

>From ced1228aded6df5da537b61a985c19bcea9d9ca1 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 15:32:11 +0530
Subject: [PATCH 06/23] code formatted

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/src/__support/math/lgammabf16.h   |  3 ++-
 libc/test/src/math/lgammabf16_test.cpp | 11 ++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index a2cffe0c5de70..9f2fb2013ae42 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -28,7 +28,8 @@ namespace math {
 // lgammabf16(x) computes log(|Gamma(x)|) in bfloat16.
 //
 // Strategy:
-//   - Special cases: NaN, 0, negative integers, +Inf or -Inf -> handled explicitly.
+//   - Special cases: NaN, 0, negative integers, +Inf or -Inf -> handled
+//   explicitly.
 //   - For all other values: promote bfloat16 -> float, compute lgammaf,
 //     narrow back to bfloat16.
 //
diff --git a/libc/test/src/math/lgammabf16_test.cpp b/libc/test/src/math/lgammabf16_test.cpp
index d7916f92fd84b..c109a8c8de28d 100644
--- a/libc/test/src/math/lgammabf16_test.cpp
+++ b/libc/test/src/math/lgammabf16_test.cpp
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/math/lgammabf16.h"
 #include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/lgammabf16.h"
 #include "test/UnitTest/FEnvSafeTest.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
@@ -33,13 +33,14 @@ TEST_F(LlvmLibcLgammaBf16Test, SpecialNumbers) { test_special_numbers(); }
 TEST_F(LlvmLibcLgammaBf16Test, InBFloat16Range) {
   constexpr uint16_t COUNT = 10000;
   constexpr uint32_t STEP = 0x7F80U / COUNT;
-  
+
   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
-    bfloat16 x = LIBC_NAMESPACE::cpp::bit_cast<bfloat16>(static_cast<uint16_t>(v));
+    bfloat16 x =
+        LIBC_NAMESPACE::cpp::bit_cast<bfloat16>(static_cast<uint16_t>(v));
     if (LIBC_NAMESPACE::fputil::isnan(x) || LIBC_NAMESPACE::fputil::isinf(x))
       continue;
 
-    ASSERT_MPFR_MATCH(mpfr::Operation::Lgamma, x,
-                      LIBC_NAMESPACE::lgammabf16(x), 0.5);
+    ASSERT_MPFR_MATCH(mpfr::Operation::Lgamma, x, LIBC_NAMESPACE::lgammabf16(x),
+                      0.5);
   }
 }

>From c7fdfb30e191970f4dfa1243d32a9a4488f724ce Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 16:15:33 +0530
Subject: [PATCH 07/23] windows entrypoint

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/config/windows/entrypoints.txt  | 1 +
 libc/src/math/generic/CMakeLists.txt | 1 +
 2 files changed, 2 insertions(+)

diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 94d1d00e676d9..5f14967e3c8a7 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -368,6 +368,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/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 6a2615f92a53e..c6d85ed090371 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4868,6 +4868,7 @@ add_entrypoint_object(
   DEPENDS
     libc.src.__support.math.bf16fmal
 )
+
 add_entrypoint_object(
   lgammabf16
   SRCS

>From d964842a7bae02121c1a97f61ad8f5b63c422168 Mon Sep 17 00:00:00 2001
From: Kiriti Ponduri <123718855+udaykiriti at users.noreply.github.com>
Date: Sat, 23 May 2026 16:35:54 +0530
Subject: [PATCH 08/23] Update libc/shared/math/lgammabf16.h

Co-authored-by: Zorojuro <sawantsukumar at gmail.com>
---
 libc/shared/math/lgammabf16.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/shared/math/lgammabf16.h b/libc/shared/math/lgammabf16.h
index 0317926a46c03..85fdd03ff60ab 100644
--- a/libc/shared/math/lgammabf16.h
+++ b/libc/shared/math/lgammabf16.h
@@ -15,7 +15,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace shared {
 
-using ::LIBC_NAMESPACE::math::lgammabf16;
+using math::lgammabf16;
 
 } // namespace shared
 } // namespace LIBC_NAMESPACE_DECL

>From 1b9400fe49d72ce80959350d9d78e77d95f12a30 Mon Sep 17 00:00:00 2001
From: Kiriti Ponduri <123718855+udaykiriti at users.noreply.github.com>
Date: Sat, 23 May 2026 16:36:15 +0530
Subject: [PATCH 09/23] Update libc/src/math/generic/lgammabf16.cpp

Co-authored-by: Zorojuro <sawantsukumar at gmail.com>
---
 libc/src/math/generic/lgammabf16.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/math/generic/lgammabf16.cpp b/libc/src/math/generic/lgammabf16.cpp
index 6ae7e69134a1c..a26e5fe437a90 100644
--- a/libc/src/math/generic/lgammabf16.cpp
+++ b/libc/src/math/generic/lgammabf16.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation of lgammabf16 function ----------------------------===//
+//===-- 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.

>From b8bc57230965c41b3e844cd9efe796a99bc052f2 Mon Sep 17 00:00:00 2001
From: Kiriti Ponduri <123718855+udaykiriti at users.noreply.github.com>
Date: Sat, 23 May 2026 16:36:27 +0530
Subject: [PATCH 10/23] Update libc/src/__support/math/lgammabf16.h

Co-authored-by: Zorojuro <sawantsukumar at gmail.com>
---
 libc/src/__support/math/lgammabf16.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index 9f2fb2013ae42..172ba24e17fd2 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -14,7 +14,6 @@
 #include "src/__support/FPUtil/rounding_mode.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h"
-
 #include "hdr/errno_macros.h"
 #include "hdr/fenv_macros.h"
 

>From 4d6c4d8206397812557d34601516b84e76e81231 Mon Sep 17 00:00:00 2001
From: Kiriti Ponduri <123718855+udaykiriti at users.noreply.github.com>
Date: Sat, 23 May 2026 16:36:38 +0530
Subject: [PATCH 11/23] Update libc/src/__support/math/lgammabf16.h

Co-authored-by: Zorojuro <sawantsukumar at gmail.com>
---
 libc/src/__support/math/lgammabf16.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index 172ba24e17fd2..0bdb6f360739f 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -5,6 +5,7 @@
 // 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
 

>From f47e7e22e03b3f6ac67009b26b38317d65cd2101 Mon Sep 17 00:00:00 2001
From: Kiriti Ponduri <123718855+udaykiriti at users.noreply.github.com>
Date: Sat, 23 May 2026 16:36:58 +0530
Subject: [PATCH 12/23] Update libc/src/__support/math/CMakeLists.txt

Co-authored-by: Zorojuro <sawantsukumar at gmail.com>
---
 libc/src/__support/math/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 90d8308c95c16..f72155e34889e 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -51,7 +51,7 @@ add_header_library(
   HDRS
     lgammabf16.h
   DEPENDS
-    libc.src.__support.FPUtil.FPBits
+    libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.cast
     libc.src.__support.FPUtil.except_value_utils
     libc.src.__support.FPUtil.rounding_mode

>From b4d0b6efff232a57b7b38c5fd36a84f73ad38225 Mon Sep 17 00:00:00 2001
From: Kiriti Ponduri <123718855+udaykiriti at users.noreply.github.com>
Date: Sat, 23 May 2026 16:37:28 +0530
Subject: [PATCH 13/23] Update libc/src/math/generic/CMakeLists.txt

Co-authored-by: Zorojuro <sawantsukumar at gmail.com>
---
 libc/src/math/generic/CMakeLists.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index c6d85ed090371..16bdc0ae30dd0 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4869,6 +4869,7 @@ add_entrypoint_object(
     libc.src.__support.math.bf16fmal
 )
 
+
 add_entrypoint_object(
   lgammabf16
   SRCS

>From 0f68a9c2eeffbca244cf37e8851f3d69484a010d Mon Sep 17 00:00:00 2001
From: Kiriti Ponduri <123718855+udaykiriti at users.noreply.github.com>
Date: Sat, 23 May 2026 16:38:03 +0530
Subject: [PATCH 14/23] Update libc/src/math/lgammabf16.h

Co-authored-by: Zorojuro <sawantsukumar at gmail.com>
---
 libc/src/math/lgammabf16.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/src/math/lgammabf16.h b/libc/src/math/lgammabf16.h
index 4c63945c6a6a8..3b17c0b10a74a 100644
--- a/libc/src/math/lgammabf16.h
+++ b/libc/src/math/lgammabf16.h
@@ -9,7 +9,6 @@
 #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"
 
 namespace LIBC_NAMESPACE_DECL {

>From 656f13fbbbc2fa684ff80cf8f37a7b491ae01ed4 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 16:47:09 +0530
Subject: [PATCH 15/23] fixed some dependencies and nits

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

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index f72155e34889e..acd21dbce646c 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -55,6 +55,7 @@ add_header_library(
     libc.src.__support.FPUtil.cast
     libc.src.__support.FPUtil.except_value_utils
     libc.src.__support.FPUtil.rounding_mode
+    libc.src.__support.FPUtil.bfloat16
     libc.src.__support.macros.config
     libc.src.__support.macros.optimization
     libc.hdr.errno_macros
diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index 0bdb6f360739f..349d7e89c3d8e 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -9,6 +9,7 @@
 #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/FPBits.h"
 #include "src/__support/FPUtil/cast.h"
 #include "src/__support/FPUtil/except_value_utils.h"
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 16bdc0ae30dd0..c03843f2fcfb3 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4878,6 +4878,7 @@ add_entrypoint_object(
     ../lgammabf16.h
   DEPENDS
     libc.src.__support.math.lgammabf16
+    libc.src.__support.FPUtil.bfloat16
 )
 
 add_entrypoint_object(

>From 446e2843d914f5a984762b22a5af02c87f7c4b8d Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 17:00:30 +0530
Subject: [PATCH 16/23] [libc][math] fix lgammabf16 deps, remove Windows ifdef,
 cleanup

---
 libc/src/__support/math/CMakeLists.txt        |  1 -
 libc/src/__support/math/lgammabf16.h          | 67 ++++---------------
 .../llvm-project-overlay/libc/BUILD.bazel     | 22 ++++++
 3 files changed, 34 insertions(+), 56 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index acd21dbce646c..e15e5db37004f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -54,7 +54,6 @@ add_header_library(
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.cast
     libc.src.__support.FPUtil.except_value_utils
-    libc.src.__support.FPUtil.rounding_mode
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.macros.config
     libc.src.__support.macros.optimization
diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index 349d7e89c3d8e..ba3e6e2b4ebc6 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -9,44 +9,22 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LGAMMABF16_H
 #define LLVM_LIBC_SRC___SUPPORT_MATH_LGAMMABF16_H
 
-#include "src/__support/FPUtil/bfloat16.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.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/rounding_mode.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h"
-#include "hdr/errno_macros.h"
-#include "hdr/fenv_macros.h"
-
-#ifdef LIBC_TARGET_OS_IS_WINDOWS
-#include <math.h> // lgammaf for MSVC
-#endif
 
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
-// lgammabf16(x) computes log(|Gamma(x)|) in bfloat16.
-//
-// Strategy:
-//   - Special cases: NaN, 0, negative integers, +Inf or -Inf -> handled
-//   explicitly.
-//   - For all other values: promote bfloat16 -> float, compute lgammaf,
-//     narrow back to bfloat16.
-//
-// Correctness argument:
-//   bfloat16 has only 7 mantissa bits vs float's 23. The float result of
-//   lgammaf has far more precision than needed, so a single narrowing cast
-//   gives a correctly-rounded bfloat16 result for all normal inputs.
-//
-// Note: unlike lgamma/lgammaf, this does NOT set the global signgam,
-// since bfloat16 math functions are intended for ML/embedded use where
-// the global signgam side-effect is undesirable.
 LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
   using FPBits = fputil::FPBits<bfloat16>;
   FPBits x_bits(x);
 
-  // Handle NaN
   if (LIBC_UNLIKELY(x_bits.is_nan())) {
     if (x_bits.is_signaling_nan()) {
       fputil::raise_except_if_required(FE_INVALID);
@@ -56,44 +34,28 @@ LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
   }
 
   uint16_t x_u = x_bits.uintval();
-  // Strip sign bit to get |x|
   uint16_t x_abs = x_u & 0x7fffU;
 
-  // Handle +Inf -> +Inf
-  if (LIBC_UNLIKELY(x_u == 0x7f80U))
-    return FPBits::inf(Sign::POS).get_val();
-
-  // Handle -Inf -> +Inf (lgamma(-inf) = +inf)
-  if (LIBC_UNLIKELY(x_u == 0xff80U))
+  // +Inf or -Inf -> +Inf
+  if (LIBC_UNLIKELY(x_abs == 0x7f80U))
     return FPBits::inf(Sign::POS).get_val();
 
-  // Handle +-0 -> +Inf (pole error)
+  // +-0 -> +Inf (pole error)
   if (LIBC_UNLIKELY(x_abs == 0U)) {
     fputil::set_errno_if_required(ERANGE);
     fputil::raise_except_if_required(FE_DIVBYZERO);
     return FPBits::inf(Sign::POS).get_val();
   }
 
-  // Handle negative integers -> +Inf (pole error)
-  // A negative bfloat16 value is an integer if its fractional part is zero.
-  // bfloat16 has 7 mantissa bits and 8 exponent bits.
-  // exponent bias = 127, so unbiased exponent e = (x_u >> 7) - 127
-  // x is a negative integer when x < 0 and |x| >= 1 and frac(|x|) == 0
-  if (LIBC_UNLIKELY(x_bits.is_neg() && x_abs < 0x7f80U)) {
+  // Negative integers -> +Inf (pole error)
+  if (LIBC_UNLIKELY(x_bits.is_neg())) {
     int biased_exp = x_abs >> FPBits::FRACTION_LEN;
     if (biased_exp >= FPBits::EXP_BIAS) {
-      // unbiased exponent >= 0, so |x| >= 1
       int e = biased_exp - FPBits::EXP_BIAS;
-      if (e >= FPBits::FRACTION_LEN) {
-        // All mantissa bits are above the binary point -> always integer
-        fputil::set_errno_if_required(ERANGE);
-        fputil::raise_except_if_required(FE_DIVBYZERO);
-        return FPBits::inf(Sign::POS).get_val();
-      }
-      // Check if fractional bits are zero
-      uint16_t frac_mask =
-          static_cast<uint16_t>((1U << (FPBits::FRACTION_LEN - e)) - 1U);
-      if ((x_bits.get_mantissa() & frac_mask) == 0U) {
+      if (e >= FPBits::FRACTION_LEN ||
+          (x_bits.get_mantissa() &
+           static_cast<uint16_t>((1U << (FPBits::FRACTION_LEN - e)) - 1U)) ==
+              0U) {
         fputil::set_errno_if_required(ERANGE);
         fputil::raise_except_if_required(FE_DIVBYZERO);
         return FPBits::inf(Sign::POS).get_val();
@@ -101,13 +63,8 @@ LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
     }
   }
 
-  // General case: promote to float, compute lgammaf, narrow back.
   float xf = fputil::cast<float>(x);
-#ifdef LIBC_TARGET_OS_IS_WINDOWS
-  float result = ::lgammaf(xf);
-#else
   float result = __builtin_lgammaf(xf);
-#endif
   return fputil::cast<bfloat16>(result);
 }
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 87963e4524b39..2bc976bf35766 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6233,6 +6233,21 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_lgammabf16",
+    hdrs = ["src/__support/math/lgammabf16.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_cast",
+        ":__support_fputil_except_value_utils",
+        ":__support_macros_config",
+        ":__support_macros_optimization",
+        ":hdr_errno_macros",
+        ":hdr_fenv_macros",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_llogbbf16",
     hdrs = ["src/__support/math/llogbbf16.h"],
@@ -11397,6 +11412,13 @@ libc_math_function(
     ],
 )
 
+libc_math_function(
+    name = "lgammabf16",
+    additional_deps = [
+        ":__support_math_lgammabf16",
+    ],
+)
+
 libc_math_function(
     name = "llogb",
     additional_deps = [

>From a1f705238ed1e01ced073f1b7d6d74537ef0c883 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 17:18:52 +0530
Subject: [PATCH 17/23] back as same

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/src/__support/math/lgammabf16.h              | 8 ++++++++
 libc/src/math/lgammabf16.h                        | 1 +
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 +-
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index ba3e6e2b4ebc6..77359c8dfc944 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -18,6 +18,10 @@
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h"
 
+#ifdef LIBC_TARGET_OS_IS_WINDOWS
+#include <math.h>
+#endif
+
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
@@ -64,7 +68,11 @@ LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
   }
 
   float xf = fputil::cast<float>(x);
+#ifdef LIBC_TARGET_OS_IS_WINDOWS
+  float result = ::lgammaf(xf);
+#else
   float result = __builtin_lgammaf(xf);
+#endif
   return fputil::cast<bfloat16>(result);
 }
 
diff --git a/libc/src/math/lgammabf16.h b/libc/src/math/lgammabf16.h
index 3b17c0b10a74a..965c8c881eb03 100644
--- a/libc/src/math/lgammabf16.h
+++ b/libc/src/math/lgammabf16.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC_MATH_LGAMMABF16_H
 
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 2bc976bf35766..aa9d62f7bcca8 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6238,9 +6238,9 @@ libc_support_library(
     hdrs = ["src/__support/math/lgammabf16.h"],
     deps = [
         ":__support_fputil_bfloat16",
-        ":__support_fputil_fp_bits",
         ":__support_fputil_cast",
         ":__support_fputil_except_value_utils",
+        ":__support_fputil_fp_bits",
         ":__support_macros_config",
         ":__support_macros_optimization",
         ":hdr_errno_macros",

>From 0e9d24592de3d42d0a0c00f14699fd38b2b9af80 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 17:22:32 +0530
Subject: [PATCH 18/23] .....

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/test/src/math/smoke/lgammabf16_test.cpp | 41 ++++++++++----------
 1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/libc/test/src/math/smoke/lgammabf16_test.cpp b/libc/test/src/math/smoke/lgammabf16_test.cpp
index b97fca574de77..393cae08fd10c 100644
--- a/libc/test/src/math/smoke/lgammabf16_test.cpp
+++ b/libc/test/src/math/smoke/lgammabf16_test.cpp
@@ -5,29 +5,28 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/bfloat16.h"
 #include "src/math/lgammabf16.h"
+#include "test/UnitTest/FEnvSafeTest.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
-using LlvmLibcLgammaBf16Test = LIBC_NAMESPACE::testing::FPTest<bfloat16>;
+class LlvmLibcLgammaBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+  DECLARE_SPECIAL_CONSTANTS(bfloat16)
+public:
+  void test_special_numbers() {
+    EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::lgammabf16(aNaN));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(zero));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(neg_zero));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(inf));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(neg_inf));
+    EXPECT_FP_EQ(zero, LIBC_NAMESPACE::lgammabf16(bfloat16(1.0f)));
+    EXPECT_FP_EQ(zero, LIBC_NAMESPACE::lgammabf16(bfloat16(2.0f)));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(bfloat16(-1.0f)));
+    EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(bfloat16(-2.0f)));
+  }
+};
 
-TEST_F(LlvmLibcLgammaBf16Test, SpecialNumbers) {
-  // 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));
-  // lgammabf16(1.0) = 0.0
-  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::lgammabf16(bfloat16(1.0f)));
-  // lgammabf16(2.0) = 0.0
-  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::lgammabf16(bfloat16(2.0f)));
-  // lgammabf16(negative integer) = +Inf (pole error)
-  EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(bfloat16(-1.0f)));
-  EXPECT_FP_EQ(inf, LIBC_NAMESPACE::lgammabf16(bfloat16(-2.0f)));
-}
+TEST_F(LlvmLibcLgammaBf16Test, SpecialNumbers) { test_special_numbers(); }

>From e11be37afe3359f581b42f5bfa095e5c13ed35ff Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 18:06:40 +0530
Subject: [PATCH 19/23] implemented lgammabf16 from very beginning so need to
 check twice

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/src/__support/math/CMakeLists.txt        |   2 +-
 libc/src/__support/math/lgammabf16.h          | 139 ++++++++++++++++--
 .../llvm-project-overlay/libc/BUILD.bazel     |   2 +-
 3 files changed, 128 insertions(+), 15 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e15e5db37004f..5ee2e8b0f43ac 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -53,7 +53,7 @@ add_header_library(
   DEPENDS
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.cast
-    libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.macros.config
     libc.src.__support.macros.optimization
diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index 77359c8dfc944..a91310fbe1d38 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -5,7 +5,6 @@
 // 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
 
@@ -14,21 +13,109 @@
 #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/FEnvImpl.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h"
 
-#ifdef LIBC_TARGET_OS_IS_WINDOWS
-#include <math.h>
-#endif
-
 namespace LIBC_NAMESPACE_DECL {
 namespace math {
 
+// Evaluate degree-4 polynomial using Horner's method:
+// p(t) = c0 + t*(c1 + t*(c2 + t*(c3 + t*c4)))
+// coeffs = {c4, c3, c2, c1, c0}
+static inline float poly4(float t, const float c[5]) {
+  return c[4] + t * (c[3] + t * (c[2] + t * (c[1] + t * c[0])));
+}
+
+// Compute natural log of positive float x using range reduction.
+// x = 2^e * m, m in [1, 2)
+// ln(x) = e * ln(2) + ln(m)
+// ln(m) approximated by degree-5 poly centered at 1.5
+static inline float lgamma_logf(float x) {
+  // Coefficients for ln(m) on [1,2), centered at 1.5
+  // Generated by numpy.polyfit, max_err = 1.71e-05 (well within bf16 precision)
+  constexpr float LN_COEFFS[6] = {
+      0x1.ef26300000000p-6f,  // c5
+      -0x1.c2e22a0000000p-5f, // c4
+      0x1.929d980000000p-4f,  // c3
+      -0x1.c615ec0000000p-3f, // c2
+      0x1.5557200000000p-1f,  // c1
+      0x1.9f309c0000000p-2f,  // c0
+  };
+  constexpr float LN2 = 0x1.62e430p-1f;
+
+  // Extract exponent and mantissa
+  uint32_t bits;
+  __builtin_memcpy(&bits, &x, sizeof(bits));
+  int e = static_cast<int>((bits >> 23) & 0xFFU) - 127;
+  // Set exponent to 127 (i.e. value in [1,2))
+  bits = (bits & 0x807FFFFFU) | (127U << 23);
+  float m;
+  __builtin_memcpy(&m, &bits, sizeof(m));
+
+  float t = m - 1.5f;
+  float lnm = LN_COEFFS[5] +
+              t * (LN_COEFFS[4] +
+                   t * (LN_COEFFS[3] +
+                        t * (LN_COEFFS[2] +
+                             t * (LN_COEFFS[1] + t * LN_COEFFS[0]))));
+  return static_cast<float>(e) * LN2 + lnm;
+}
+
+// Coefficients for lgamma on [n, n+1), centered at n+0.5
+// Each row: {c4, c3, c2, c1, c0} for poly in (x - (n+0.5))
+// Generated by numpy.polyfit with degree 4
+// Intervals: n = 1..7
+static constexpr float LGAMMA_POLY[7][5] = {
+    // [1,2), center=1.5, max_err=1.29e-04
+    {0x1.08d8ae0000000p-4f, -0x1.2d373c0000000p-3f, 0x1.de14880000000p-2f,
+     0x1.2f128a0000000p-5f, -0x1.eeb2800000000p-4f},
+    // [2,3), center=2.5, max_err=9.98e-06
+    {0x1.3b3ccc0000000p-7f, -0x1.48c82a0000000p-5f, 0x1.f613220000000p-3f,
+     0x1.6809be0000000p-1f, 0x1.2383fc0000000p-2f},
+    // [3,4), center=3.5, max_err=2.06e-06
+    {0x1.859be80000000p-9f, -0x1.2a28760000000p-6f, 0x1.52475c0000000p-3f,
+     0x1.1a69120000000p+0f, 0x1.3373020000000p+0f},
+    // [4,5), center=4.5, max_err=6.61e-07
+    {0x1.4e023c0000000p-10f, -0x1.51efee0000000p-7f, 0x1.fd62a00000000p-4f,
+     0x1.638d3e0000000p+0f, 0x1.3a140a0000000p+1f},
+    // [5,6), center=5.5, max_err=2.72e-07
+    {0x1.58a5b20000000p-11f, -0x1.b218380000000p-8f, 0x1.9840800000000p-4f,
+     0x1.9c70ae0000000p+0f, 0x1.fa99a60000000p+1f},
+    // [6,7), center=6.5, max_err=1.32e-07
+    {0x1.9096420000000p-12f, -0x1.2e0b0c0000000p-8f, 0x1.548cda0000000p-4f,
+     0x1.cafc460000000p+0f, 0x1.6a676a0000000p+2f},
+    // [7,8), center=7.5, max_err=7.10e-08
+    {0x1.f9d4be0000000p-13f, -0x1.bc58500000000p-9f, 0x1.2413be0000000p-4f,
+     0x1.f25eb80000000p+0f, 0x1.e233060000000p+2f},
+};
+
+// Compute lgamma for x >= 1 using polynomial or Stirling
+static inline float lgamma_positive(float x) {
+  if(x == 1.0f || x == 2.0f) return 0.0f;
+  // For x >= 8: Stirling approximation
+  // lgamma(x) ~ (x-0.5)*ln(x) - x + 0.5*ln(2*pi)
+  // 0.5*ln(2*pi) = 0x1.6840e60000000p-1f
+  if (x >= 8.0f) {
+    constexpr float HALF_LN_2PI = 0x1.6840e60000000p-1f;
+    float lx = lgamma_logf(x);
+    return (x - 0.5f) * lx - x + HALF_LN_2PI;
+  }
+
+  // For x in [1, 8): use piecewise polynomial
+  int n = static_cast<int>(x); // floor for x >= 1
+  if (n >= 7)
+    n = 7;
+  float mid = static_cast<float>(n) + 0.5f;
+  float t = x - mid;
+  return poly4(t, LGAMMA_POLY[n - 1]);
+}
+
 LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
   using FPBits = fputil::FPBits<bfloat16>;
   FPBits x_bits(x);
 
+  // Handle NaN
   if (LIBC_UNLIKELY(x_bits.is_nan())) {
     if (x_bits.is_signaling_nan()) {
       fputil::raise_except_if_required(FE_INVALID);
@@ -51,6 +138,8 @@ LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
     return FPBits::inf(Sign::POS).get_val();
   }
 
+  float xf = fputil::cast<float>(x);
+
   // Negative integers -> +Inf (pole error)
   if (LIBC_UNLIKELY(x_bits.is_neg())) {
     int biased_exp = x_abs >> FPBits::FRACTION_LEN;
@@ -65,15 +154,39 @@ LIBC_INLINE bfloat16 lgammabf16(bfloat16 x) {
         return FPBits::inf(Sign::POS).get_val();
       }
     }
+
+    // Negative non-integer: reflection formula
+    // lgamma(x) = ln(pi / |sin(pi*x)|) - lgamma(1-x)
+    constexpr float LN_PI = 0x1.250d060000000p+0f; // ln(pi)
+    float ax = -xf;
+    // sin(pi*x) for negative x: sin(pi*x) = -sin(pi*ax)
+    // Use: |sin(pi*x)| = |sin(pi*(ax - floor(ax)))|
+    float frac = ax - static_cast<float>(static_cast<int>(ax));
+    // sin(pi*frac) via polynomial for frac in (0,1)
+    // sin(pi*t) ~ pi*t*(1 - t)*(a + b*t*(1-t)) Bhaskara-like approx
+    // Better: use small polynomial
+    // sin(pi*t) for t in (0,1):
+    constexpr float PI = 0x1.921fb60000000p+1f;
+    float pf = PI * frac;
+    // Taylor: sin(t) ~ t - t^3/6 + t^5/120
+    float pf2 = pf * pf;
+    float sinpif = pf * (1.0f - pf2 * (1.0f / 6.0f - pf2 * (1.0f / 120.0f)));
+    if (sinpif < 0.0f)
+      sinpif = -sinpif;
+
+    float log_sin = lgamma_logf(sinpif);
+    float result = LN_PI - log_sin - lgamma_positive(1.0f + ax);
+    return fputil::cast<bfloat16>(result);
   }
 
-  float xf = fputil::cast<float>(x);
-#ifdef LIBC_TARGET_OS_IS_WINDOWS
-  float result = ::lgammaf(xf);
-#else
-  float result = __builtin_lgammaf(xf);
-#endif
-  return fputil::cast<bfloat16>(result);
+  // Positive x in (0, 1): use recurrence lgamma(x) = lgamma(x+1) - ln(x)
+  if (xf < 1.0f) {
+    float lnx = lgamma_logf(xf);
+    return fputil::cast<bfloat16>(lgamma_positive(xf + 1.0f) - lnx);
+  }
+
+  // Positive x >= 1
+  return fputil::cast<bfloat16>(lgamma_positive(xf));
 }
 
 } // namespace math
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index aa9d62f7bcca8..ce7dfe539ce90 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6239,7 +6239,7 @@ libc_support_library(
     deps = [
         ":__support_fputil_bfloat16",
         ":__support_fputil_cast",
-        ":__support_fputil_except_value_utils",
+        ":__support_fputil_fenv_impl",
         ":__support_fputil_fp_bits",
         ":__support_macros_config",
         ":__support_macros_optimization",

>From 9217d8e3917e787aaac5e5830220095bd532d141 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 18:17:36 +0530
Subject: [PATCH 20/23] implementation with base consitons and used bit casting

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/src/__support/math/CMakeLists.txt        |  3 ++-
 libc/src/__support/math/lgammabf16.h          | 22 +++++++++----------
 .../llvm-project-overlay/libc/BUILD.bazel     |  1 +
 3 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 5ee2e8b0f43ac..189d1d004abac 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -51,10 +51,11 @@ add_header_library(
   HDRS
     lgammabf16.h
   DEPENDS
+    libc.src.__support.CPP.bit
     libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.cast
     libc.src.__support.FPUtil.fenv_impl
-    libc.src.__support.FPUtil.bfloat16
     libc.src.__support.macros.config
     libc.src.__support.macros.optimization
     libc.hdr.errno_macros
diff --git a/libc/src/__support/math/lgammabf16.h b/libc/src/__support/math/lgammabf16.h
index a91310fbe1d38..06570c35cb779 100644
--- a/libc/src/__support/math/lgammabf16.h
+++ b/libc/src/__support/math/lgammabf16.h
@@ -10,10 +10,11 @@
 
 #include "hdr/errno_macros.h"
 #include "hdr/fenv_macros.h"
+#include "src/__support/CPP/bit.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/FEnvImpl.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h"
 
@@ -45,20 +46,18 @@ static inline float lgamma_logf(float x) {
   constexpr float LN2 = 0x1.62e430p-1f;
 
   // Extract exponent and mantissa
-  uint32_t bits;
-  __builtin_memcpy(&bits, &x, sizeof(bits));
+  uint32_t bits = cpp::bit_cast<uint32_t>(x);
   int e = static_cast<int>((bits >> 23) & 0xFFU) - 127;
   // Set exponent to 127 (i.e. value in [1,2))
   bits = (bits & 0x807FFFFFU) | (127U << 23);
-  float m;
-  __builtin_memcpy(&m, &bits, sizeof(m));
+  float m = cpp::bit_cast<float>(bits);
 
   float t = m - 1.5f;
-  float lnm = LN_COEFFS[5] +
-              t * (LN_COEFFS[4] +
-                   t * (LN_COEFFS[3] +
-                        t * (LN_COEFFS[2] +
-                             t * (LN_COEFFS[1] + t * LN_COEFFS[0]))));
+  float lnm =
+      LN_COEFFS[5] +
+      t * (LN_COEFFS[4] +
+           t * (LN_COEFFS[3] +
+                t * (LN_COEFFS[2] + t * (LN_COEFFS[1] + t * LN_COEFFS[0]))));
   return static_cast<float>(e) * LN2 + lnm;
 }
 
@@ -92,7 +91,8 @@ static constexpr float LGAMMA_POLY[7][5] = {
 
 // Compute lgamma for x >= 1 using polynomial or Stirling
 static inline float lgamma_positive(float x) {
-  if(x == 1.0f || x == 2.0f) return 0.0f;
+  if (x == 1.0f || x == 2.0f)
+    return 0.0f;
   // For x >= 8: Stirling approximation
   // lgamma(x) ~ (x-0.5)*ln(x) - x + 0.5*ln(2*pi)
   // 0.5*ln(2*pi) = 0x1.6840e60000000p-1f
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index ce7dfe539ce90..297f0fa2e4720 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6237,6 +6237,7 @@ libc_support_library(
     name = "__support_math_lgammabf16",
     hdrs = ["src/__support/math/lgammabf16.h"],
     deps = [
+        ":__support_cpp_bit",
         ":__support_fputil_bfloat16",
         ":__support_fputil_cast",
         ":__support_fputil_fenv_impl",

>From 31ecf83948d50e382330bb63274527310a0bf922 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 18:36:08 +0530
Subject: [PATCH 21/23] build issues

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/test/src/math/lgammabf16_test.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/math/lgammabf16_test.cpp b/libc/test/src/math/lgammabf16_test.cpp
index c109a8c8de28d..2707a05c04692 100644
--- a/libc/test/src/math/lgammabf16_test.cpp
+++ b/libc/test/src/math/lgammabf16_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/bfloat16.h"
 #include "src/math/lgammabf16.h"
 #include "test/UnitTest/FEnvSafeTest.h"
@@ -37,7 +38,8 @@ TEST_F(LlvmLibcLgammaBf16Test, InBFloat16Range) {
   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
     bfloat16 x =
         LIBC_NAMESPACE::cpp::bit_cast<bfloat16>(static_cast<uint16_t>(v));
-    if (LIBC_NAMESPACE::fputil::isnan(x) || LIBC_NAMESPACE::fputil::isinf(x))
+    LIBC_NAMESPACE::fputil::FPBits<bfloat16> x_bits(x);
+    if (x_bits.isnan(x) || x_bits.isinf(x))
       continue;
 
     ASSERT_MPFR_MATCH(mpfr::Operation::Lgamma, x, LIBC_NAMESPACE::lgammabf16(x),

>From 6db65d73db185cb7ca3bd6e98d1d588f54593d44 Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 18:40:14 +0530
Subject: [PATCH 22/23] [libc][math] Fix MSVC build, clean up dependencies, and
 wire MPFR for lgammabf16

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/test/src/math/lgammabf16_test.cpp | 2 +-
 libc/utils/MPFRWrapper/MPFRUtils.cpp   | 2 ++
 libc/utils/MPFRWrapper/MPFRUtils.h     | 1 +
 3 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/libc/test/src/math/lgammabf16_test.cpp b/libc/test/src/math/lgammabf16_test.cpp
index 2707a05c04692..25bc5a6e545f0 100644
--- a/libc/test/src/math/lgammabf16_test.cpp
+++ b/libc/test/src/math/lgammabf16_test.cpp
@@ -39,7 +39,7 @@ TEST_F(LlvmLibcLgammaBf16Test, InBFloat16Range) {
     bfloat16 x =
         LIBC_NAMESPACE::cpp::bit_cast<bfloat16>(static_cast<uint16_t>(v));
     LIBC_NAMESPACE::fputil::FPBits<bfloat16> x_bits(x);
-    if (x_bits.isnan(x) || x_bits.isinf(x))
+    if (x_bits.isnan() || x_bits.isinf())
       continue;
 
     ASSERT_MPFR_MATCH(mpfr::Operation::Lgamma, x, LIBC_NAMESPACE::lgammabf16(x),
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 356764302bda8..2330ec05003b6 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -87,6 +87,8 @@ unary_operation(Operation op, InputType input, unsigned int precision,
     return mpfrInput.log10p1();
   case Operation::Log1p:
     return mpfrInput.log1p();
+  case Operation::Lgamma:
+    return mpfrInput.lgamma();
   case Operation::Mod2PI:
     return mpfrInput.mod_2pi();
   case Operation::ModPIOver2:
diff --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index 6c1b15598b0f2..a9029b83c1e2f 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -54,6 +54,7 @@ enum class Operation : int {
   Log10,
   Log10p1,
   Log1p,
+  Lgamma,
   Mod2PI,
   ModPIOver2,
   ModPIOver4,

>From e5dec96bdaaf4eb8529c27b23e35a1529bac341d Mon Sep 17 00:00:00 2001
From: udaykiriti <udaykiriti9 at gmail.com>
Date: Sat, 23 May 2026 18:48:34 +0530
Subject: [PATCH 23/23] [libc][math] Fix MSVC build and wire up MPFR for
 lgammabf16

Signed-off-by: udaykiriti <udaykiriti9 at gmail.com>
---
 libc/utils/MPFRWrapper/MPCommon.cpp | 7 +++++++
 libc/utils/MPFRWrapper/MPCommon.h   | 1 +
 2 files changed, 8 insertions(+)

diff --git a/libc/utils/MPFRWrapper/MPCommon.cpp b/libc/utils/MPFRWrapper/MPCommon.cpp
index bdd8286148641..2d95148cc2a0b 100644
--- a/libc/utils/MPFRWrapper/MPCommon.cpp
+++ b/libc/utils/MPFRWrapper/MPCommon.cpp
@@ -379,6 +379,13 @@ MPFRNumber MPFRNumber::log1p() const {
   return result;
 }
 
+MPFRNumber MPFRNumber::lgamma() const {
+  MPFRNumber result(*this);
+  int signp;
+  mpfr_lgamma(result.value, &signp, value, mpfr_rounding);
+  return result;
+}
+
 MPFRNumber MPFRNumber::pow(const MPFRNumber &b) {
   MPFRNumber result(*this);
   mpfr_pow(result.value, value, b.value, mpfr_rounding);
diff --git a/libc/utils/MPFRWrapper/MPCommon.h b/libc/utils/MPFRWrapper/MPCommon.h
index 935a2614968a2..47bebffef9fec 100644
--- a/libc/utils/MPFRWrapper/MPCommon.h
+++ b/libc/utils/MPFRWrapper/MPCommon.h
@@ -218,6 +218,7 @@ class MPFRNumber {
   MPFRNumber log10() const;
   MPFRNumber log10p1() const;
   MPFRNumber log1p() const;
+  MPFRNumber lgamma() const;
   MPFRNumber pow(const MPFRNumber &b);
   MPFRNumber remquo(const MPFRNumber &divisor, int &quotient);
   MPFRNumber round() const;



More information about the libc-commits mailing list