[libcxx-commits] [libcxx] [libc++] Avoid redeclaring lgamma_r (PR #153631)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Sep 16 11:00:58 PDT 2025
https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/153631
>From ddd93ec15649fdb3a094163fdad59c356d795b6b Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Thu, 14 Aug 2025 13:34:33 -0400
Subject: [PATCH] [libc++] Avoid redeclaring lgamma_r
This causes issues when building with modules, and redeclaring functions
provided by another library (here the C library) is bad hygiene. Instead,
use <math.h> to access the declaration provided by the system.
As a drive-by, this also starts using ::lgamma_r instead of ::lgamma when
building on top of LLVM-libc. We didn't do that previously due to a
declration conflict, but using the _r version when we can should only
make things thread safe.
---
libcxx/include/__configuration/availability.h | 13 +++++++
libcxx/include/__math/gamma.h | 25 ++++++++++++++
.../include/__random/binomial_distribution.h | 34 +++----------------
libcxx/src/CMakeLists.txt | 1 +
libcxx/src/math.cpp | 26 ++++++++++++++
5 files changed, 70 insertions(+), 29 deletions(-)
create mode 100644 libcxx/src/math.cpp
diff --git a/libcxx/include/__configuration/availability.h b/libcxx/include/__configuration/availability.h
index 2fbc34a3cf8a2..615213821e503 100644
--- a/libcxx/include/__configuration/availability.h
+++ b/libcxx/include/__configuration/availability.h
@@ -84,6 +84,9 @@
// in all versions of the library are available.
#if !_LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS
+# define _LIBCPP_INTRODUCED_IN_LLVM_22 1
+# define _LIBCPP_INTRODUCED_IN_LLVM_22_ATTRIBUTE /* nothing */
+
# define _LIBCPP_INTRODUCED_IN_LLVM_21 1
# define _LIBCPP_INTRODUCED_IN_LLVM_21_ATTRIBUTE /* nothing */
@@ -120,6 +123,11 @@
// clang-format off
+// LLVM 22
+// TODO: Fill this in
+# define _LIBCPP_INTRODUCED_IN_LLVM_22 0
+# define _LIBCPP_INTRODUCED_IN_LLVM_22_ATTRIBUTE __attribute__((unavailable))
+
// LLVM 21
// TODO: Fill this in
# define _LIBCPP_INTRODUCED_IN_LLVM_21 0
@@ -355,6 +363,11 @@
#define _LIBCPP_AVAILABILITY_HAS_BAD_FUNCTION_CALL_GOOD_WHAT_MESSAGE _LIBCPP_INTRODUCED_IN_LLVM_21
// No attribute, since we've had bad_function_call::what() in the headers before
+// This controls whether we provide an implementation of a thread safe variant of the `std::lgamma`
+// function in the dylib.
+#define _LIBCPP_AVAILABILITY_HAS_THREAD_SAFE_LGAMMA _LIBCPP_INTRODUCED_IN_LLVM_22
+// No attribute, since we call ::lgamma_r instead
+
// Define availability attributes that depend on both
// _LIBCPP_HAS_EXCEPTIONS and _LIBCPP_HAS_RTTI.
#if !_LIBCPP_HAS_EXCEPTIONS || !_LIBCPP_HAS_RTTI
diff --git a/libcxx/include/__math/gamma.h b/libcxx/include/__math/gamma.h
index 693e111a84e99..66fce3b5646de 100644
--- a/libcxx/include/__math/gamma.h
+++ b/libcxx/include/__math/gamma.h
@@ -55,6 +55,31 @@ inline _LIBCPP_HIDE_FROM_ABI double tgamma(_A1 __x) _NOEXCEPT {
return __builtin_tgamma((double)__x);
}
+// __lgamma_r
+
+struct __lgamma_result {
+ double __result;
+ int __sign;
+};
+
+#if _LIBCPP_AVAILABILITY_HAS_THREAD_SAFE_LGAMMA
+_LIBCPP_EXPORTED_FROM_ABI __lgamma_result __lgamma_thread_safe_impl(double) _NOEXCEPT;
+
+inline _LIBCPP_HIDE_FROM_ABI __lgamma_result __lgamma_thread_safe(double __d) _NOEXCEPT {
+ return __math::__lgamma_thread_safe_impl(__d);
+}
+#else
+// When deploying to older targets, call `lgamma_r` directly but avoid declaring the actual
+// function since different platforms declare the function slightly differently.
+double __lgamma_r_shim(double, int*) _NOEXCEPT __asm__("lgamma_r");
+
+inline _LIBCPP_HIDE_FROM_ABI __lgamma_result __lgamma_thread_safe(double __d) _NOEXCEPT {
+ int __sign;
+ double __res = __math::__lgamma_r_shim(__d, &__sign);
+ return __lgamma_result{__res, __sign};
+}
+#endif
+
} // namespace __math
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/__random/binomial_distribution.h b/libcxx/include/__random/binomial_distribution.h
index bada8cfdd74a3..2b74c592306b9 100644
--- a/libcxx/include/__random/binomial_distribution.h
+++ b/libcxx/include/__random/binomial_distribution.h
@@ -10,6 +10,7 @@
#define _LIBCPP___RANDOM_BINOMIAL_DISTRIBUTION_H
#include <__config>
+#include <__math/gamma.h>
#include <__random/is_valid.h>
#include <__random/uniform_real_distribution.h>
#include <cmath>
@@ -97,39 +98,14 @@ class binomial_distribution {
}
};
-// Some libc declares the math functions to be `noexcept`.
-#if defined(_LIBCPP_GLIBC_PREREQ)
-# if _LIBCPP_GLIBC_PREREQ(2, 8)
-# define _LIBCPP_LGAMMA_R_NOEXCEPT _NOEXCEPT
-# else
-# define _LIBCPP_LGAMMA_R_NOEXCEPT
-# endif
-#elif defined(__LLVM_LIBC__)
-# define _LIBCPP_LGAMMA_R_NOEXCEPT _NOEXCEPT
-#else
-# define _LIBCPP_LGAMMA_R_NOEXCEPT
-#endif
-
-#if !defined(_LIBCPP_MSVCRT_LIKE)
-extern "C" double lgamma_r(double, int*) _LIBCPP_LGAMMA_R_NOEXCEPT;
-#endif
-
-inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {
-#if defined(_LIBCPP_MSVCRT_LIKE)
- return lgamma(__d);
-#else
- int __sign;
- return lgamma_r(__d, &__sign);
-#endif
-}
-
template <class _IntType>
binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) : __t_(__t), __p_(__p) {
if (0 < __p_ && __p_ < 1) {
__r0_ = static_cast<result_type>((__t_ + 1) * __p_);
- __pr_ = std::exp(
- std::__libcpp_lgamma(__t_ + 1.) - std::__libcpp_lgamma(__r0_ + 1.) - std::__libcpp_lgamma(__t_ - __r0_ + 1.) +
- __r0_ * std::log(__p_) + (__t_ - __r0_) * std::log(1 - __p_));
+ __pr_ =
+ std::exp(__math::__lgamma_thread_safe(__t_ + 1.).__result - __math::__lgamma_thread_safe(__r0_ + 1.).__result -
+ __math::__lgamma_thread_safe(__t_ - __r0_ + 1.).__result + __r0_ * std::log(__p_) +
+ (__t_ - __r0_) * std::log(1 - __p_));
__odds_ratio_ = __p_ / (1 - __p_);
}
}
diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index f59fe0e08fccb..16e9a2e163dad 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -30,6 +30,7 @@ set(LIBCXX_SOURCES
include/ryu/ryu.h
include/to_chars_floating_point.h
include/from_chars_floating_point.h
+ math.cpp
memory.cpp
memory_resource.cpp
new_handler.cpp
diff --git a/libcxx/src/math.cpp b/libcxx/src/math.cpp
new file mode 100644
index 0000000000000..2b33f919a4d70
--- /dev/null
+++ b/libcxx/src/math.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifdef __APPLE__
+# define _REENTRANT
+#endif
+
+#include <cmath>
+#include <math.h> // for lgamma_r
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+namespace __math {
+
+_LIBCPP_EXPORTED_FROM_ABI __lgamma_result __lgamma_thread_safe_impl(double __d) noexcept {
+ int __sign;
+ double __result = ::lgamma_r(__d, &__sign);
+ return __lgamma_result{__result, __sign};
+}
+
+} // namespace __math
+_LIBCPP_END_NAMESPACE_STD
More information about the libcxx-commits
mailing list