[libc-commits] [libc] [libc][math] Implement double precision tgamma (PR #210970)

Hardik Chona via libc-commits libc-commits at lists.llvm.org
Tue Jul 21 07:14:27 PDT 2026


https://github.com/un-pixelated updated https://github.com/llvm/llvm-project/pull/210970

>From 136c6a5beb3d03c5a40879ae48c7a1762cdc0e4b Mon Sep 17 00:00:00 2001
From: Hardik Chona <iamhardikchona at gmail.com>
Date: Tue, 21 Jul 2026 15:49:40 +0530
Subject: [PATCH 1/6] [libc][math] scaffolding for tgamma

---
 libc/config/linux/x86_64/entrypoints.txt |  1 +
 libc/src/__support/math/CMakeLists.txt   | 11 ++++++++
 libc/src/__support/math/tgamma.h         | 33 ++++++++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt     | 10 +++++++
 libc/src/math/generic/tgamma.cpp         | 16 ++++++++++++
 5 files changed, 71 insertions(+)
 create mode 100644 libc/src/__support/math/tgamma.h
 create mode 100644 libc/src/math/generic/tgamma.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 0a4fb747c2940..0d8492cb4df3a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -926,6 +926,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.tanf16
     libc.src.math.tanhf16
     libc.src.math.tanpif16
+    libc.src.math.tgamma
     libc.src.math.totalorderf16
     libc.src.math.totalordermagf16
     libc.src.math.truncf16
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 9f4624682eafe..5eaca01a5b096 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -5550,6 +5550,17 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  tgamma
+  HDRS
+    tgamma.h
+  DEPENDS
+    libc.src.__support.FPUtil.FEnvImpl
+    libc.src.__support.FPUtil.FPBits
+    libc.src.__support.macros.config
+    libc.src.__support.macros.optimization
+)
+
 add_header_library(
   totalorder
   HDRS
diff --git a/libc/src/__support/math/tgamma.h b/libc/src/__support/math/tgamma.h
new file mode 100644
index 0000000000000..f1db64538c052
--- /dev/null
+++ b/libc/src/__support/math/tgamma.h
@@ -0,0 +1,33 @@
+//===-- Double-precision tgamma function ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_TGAMMA_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_TGAMMA_H
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double tgamma(double x) {
+  using FPBits = fputil::FPBits<double>;
+  FPBits xbits(x);
+
+  // TODO: implement
+  return 0.0;
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_TGAMMA_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index d5661f6f55e91..84aa3f7356105 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -466,6 +466,16 @@ add_entrypoint_object(
     libc.src.__support.math.tanpif16
 )
 
+add_entrypoint_object(
+  tgamma
+  SRCS
+    tgamma.cpp
+  HDRS
+    ../tgamma.h
+  DEPENDS
+    libc.src.__support.math.tgamma
+)
+
 add_entrypoint_object(
   fabs
   SRCS
diff --git a/libc/src/math/generic/tgamma.cpp b/libc/src/math/generic/tgamma.cpp
new file mode 100644
index 0000000000000..55512f4f510da
--- /dev/null
+++ b/libc/src/math/generic/tgamma.cpp
@@ -0,0 +1,16 @@
+//===-- Double precision tgamma function ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/tgamma.h"
+#include "src/__support/math/tgamma.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, tgamma, (double x)) { return math::tgamma(x); }
+
+} // namespace LIBC_NAMESPACE_DECL

>From b2e54ade53f63f1953fcf797db021fa3b75366e0 Mon Sep 17 00:00:00 2001
From: Hardik Chona <iamhardikchona at gmail.com>
Date: Tue, 21 Jul 2026 15:57:15 +0530
Subject: [PATCH 2/6] [libc][math] add entrypoint for macOS and confirm build
 is working

---
 libc/config/darwin/aarch64/entrypoints.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 15adad72ab459..79943f155c8b7 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -391,6 +391,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.tanf
     libc.src.math.tanhf
     libc.src.math.tanpif
+    libc.src.math.tgamma
     libc.src.math.totalorder
     libc.src.math.totalorderf
     libc.src.math.totalorderl

>From e53630f826223fb91b827450bbfd5c18ae7aeece Mon Sep 17 00:00:00 2001
From: Hardik Chona <iamhardikchona at gmail.com>
Date: Tue, 21 Jul 2026 17:23:22 +0530
Subject: [PATCH 3/6] [libc][math] Write smoke tests for tgamma and wire up
 CMakeLists.txt

---
 libc/test/src/math/smoke/CMakeLists.txt  | 11 ++++++
 libc/test/src/math/smoke/tgamma_test.cpp | 49 ++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 libc/test/src/math/smoke/tgamma_test.cpp

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 81f76fa7681fd..ad1e6a648ed7a 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -180,6 +180,17 @@ add_fp_unittest(
     libc.src.math.tanpif16
 )
 
+add_fp_unittest(
+  tgamma_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    tgamma_test.cpp
+  DEPENDS
+    libc.src.math.tgamma
+    libc.hdr.errno_macros
+)
+
 add_fp_unittest(
   fabs_test
   SUITE
diff --git a/libc/test/src/math/smoke/tgamma_test.cpp b/libc/test/src/math/smoke/tgamma_test.cpp
new file mode 100644
index 0000000000000..630237facbfc6
--- /dev/null
+++ b/libc/test/src/math/smoke/tgamma_test.cpp
@@ -0,0 +1,49 @@
+//===-- Unittests for tgamma ----------------------------------------------===//
+//
+// 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/tgamma.h"
+#include "hdr/errno_macros.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcTgammaTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+using LIBC_NAMESPACE::testing::tlog;
+
+TEST_F(LlvmLibcTgammaTest, SpecialNumbers) {
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tgamma(sNaN), FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tgamma(neg_inf), FE_INVALID);
+  EXPECT_MATH_ERRNO(EDOM);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tgamma(-1.0), FE_INVALID);
+  EXPECT_MATH_ERRNO(EDOM);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::tgamma(zero), FE_DIVBYZERO);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::tgamma(neg_zero), FE_DIVBYZERO);
+  EXPECT_MATH_ERRNO(ERANGE);
+  
+  // Source: https://members.loria.fr/PZimmermann/papers/gamma.pdf
+  // Gamma(0x1.573fae561f648p+7) > DBL_MAX
+  EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::tgamma(0x1.573fae561f648p+7), FE_OVERFLOW);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+  // One ULP below 0x1.573fae561f648p+7
+  // It is the largest input our function returns a finite output for
+  EXPECT_FP_NE(inf, LIBC_NAMESPACE::tgamma(0x1.573fae561f647p+7));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::tgamma(aNaN));
+  EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::tgamma(inf));
+
+  EXPECT_FP_EQ(1.0, LIBC_NAMESPACE::tgamma(1.0));
+  EXPECT_MATH_ERRNO(0);
+}

>From 3692a6714a521d0b4943d63f607a823c01ae74cc Mon Sep 17 00:00:00 2001
From: Hardik Chona <iamhardikchona at gmail.com>
Date: Tue, 21 Jul 2026 17:25:03 +0530
Subject: [PATCH 4/6] [libc][math] nits fixed

---
 libc/test/src/math/smoke/tgamma_test.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libc/test/src/math/smoke/tgamma_test.cpp b/libc/test/src/math/smoke/tgamma_test.cpp
index 630237facbfc6..261598b361773 100644
--- a/libc/test/src/math/smoke/tgamma_test.cpp
+++ b/libc/test/src/math/smoke/tgamma_test.cpp
@@ -6,15 +6,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/math/tgamma.h"
 #include "hdr/errno_macros.h"
+#include "src/math/tgamma.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
 using LlvmLibcTgammaTest = LIBC_NAMESPACE::testing::FPTest<double>;
 
-using LIBC_NAMESPACE::testing::tlog;
-
 TEST_F(LlvmLibcTgammaTest, SpecialNumbers) {
   EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tgamma(sNaN), FE_INVALID);
   EXPECT_MATH_ERRNO(0);

>From fd6202f7eaeaee4899e8a92c3b68bb1ab472c4f3 Mon Sep 17 00:00:00 2001
From: Hardik Chona <iamhardikchona at gmail.com>
Date: Tue, 21 Jul 2026 18:00:19 +0530
Subject: [PATCH 5/6] [libc][math] add to entrypoints for linux arm and linux
 aarch64

---
 libc/config/linux/aarch64/entrypoints.txt | 1 +
 libc/config/linux/arm/entrypoints.txt     | 1 +
 2 files changed, 2 insertions(+)

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 2e7b9276ab068..d258492d6e4b2 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -711,6 +711,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.tanf
     libc.src.math.tanhf
     libc.src.math.tanpif
+    libc.src.math.tgamma
     libc.src.math.totalorder
     libc.src.math.totalorderf
     libc.src.math.totalorderl
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 1986d6a5347dc..539aa64cfb4c1 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -489,6 +489,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.tanf
     libc.src.math.tanhf
     libc.src.math.tanpif
+    libc.src.math.tgamma
     libc.src.math.totalorder
     libc.src.math.totalorderf
     libc.src.math.totalorderl

>From f1937ef0cf6e051fc65a87abfab638d26b62b053 Mon Sep 17 00:00:00 2001
From: Hardik Chona <iamhardikchona at gmail.com>
Date: Tue, 21 Jul 2026 19:44:13 +0530
Subject: [PATCH 6/6] clang-format

---
 libc/test/src/math/smoke/tgamma_test.cpp | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/libc/test/src/math/smoke/tgamma_test.cpp b/libc/test/src/math/smoke/tgamma_test.cpp
index 261598b361773..458aeb6b801b9 100644
--- a/libc/test/src/math/smoke/tgamma_test.cpp
+++ b/libc/test/src/math/smoke/tgamma_test.cpp
@@ -17,7 +17,8 @@ TEST_F(LlvmLibcTgammaTest, SpecialNumbers) {
   EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tgamma(sNaN), FE_INVALID);
   EXPECT_MATH_ERRNO(0);
 
-  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tgamma(neg_inf), FE_INVALID);
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tgamma(neg_inf),
+                              FE_INVALID);
   EXPECT_MATH_ERRNO(EDOM);
 
   EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tgamma(-1.0), FE_INVALID);
@@ -26,12 +27,14 @@ TEST_F(LlvmLibcTgammaTest, SpecialNumbers) {
   EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::tgamma(zero), FE_DIVBYZERO);
   EXPECT_MATH_ERRNO(ERANGE);
 
-  EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::tgamma(neg_zero), FE_DIVBYZERO);
+  EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, LIBC_NAMESPACE::tgamma(neg_zero),
+                              FE_DIVBYZERO);
   EXPECT_MATH_ERRNO(ERANGE);
-  
+
   // Source: https://members.loria.fr/PZimmermann/papers/gamma.pdf
   // Gamma(0x1.573fae561f648p+7) > DBL_MAX
-  EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::tgamma(0x1.573fae561f648p+7), FE_OVERFLOW);
+  EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::tgamma(0x1.573fae561f648p+7),
+                              FE_OVERFLOW);
   EXPECT_MATH_ERRNO(ERANGE);
 
   // One ULP below 0x1.573fae561f648p+7



More information about the libc-commits mailing list