From 389bedc347b533dda32ad44645ae4a0bc83f051f Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Thu, 20 Nov 2025 14:47:23 -0800 Subject: [PATCH] ``: Replace `_XLgamma()` with UCRT `lgamma()` In `poisson_distribution` and `binomial_distribution`. --- stl/inc/random | 12 +++------ stl/src/xlgamma.cpp | 60 +++++++-------------------------------------- 2 files changed, 13 insertions(+), 59 deletions(-) diff --git a/stl/inc/random b/stl/inc/random index 7f0a0e1ca5d..d2948988865 100644 --- a/stl/inc/random +++ b/stl/inc/random @@ -65,10 +65,6 @@ _INLINE_VAR constexpr long double _Exp1 = 2.7182818284590452353602874713526625 _INLINE_VAR constexpr long double _Two32 = 4294967296.0L; _INLINE_VAR constexpr long double _Two31 = 2147483648.0L; -extern "C++" _CRTIMP2_PURE float __CLRCALL_PURE_OR_CDECL _XLgamma(float) noexcept; -extern "C++" _CRTIMP2_PURE double __CLRCALL_PURE_OR_CDECL _XLgamma(double) noexcept; -extern "C++" _CRTIMP2_PURE long double __CLRCALL_PURE_OR_CDECL _XLgamma(long double) noexcept; - _INLINE_VAR constexpr int _Nwords = 4; template @@ -1926,7 +1922,7 @@ public: } _Sqrt = _CSTD sqrt(2.0 * _Mean0); _Logm = _CSTD log(_Mean0); - _Gx1 = _Mean0 * _Logm - _XLgamma(_Mean0 + 1.0); + _Gx1 = _Mean0 * _Logm - _STD lgamma(_Mean0 + 1.0); } _Ty1 _Mean = 0.0; @@ -2030,7 +2026,7 @@ private: } if (_Nrand_impl<_Ty1>(_Eng) - <= 0.9 * (1.0 + _Yx * _Yx) * _CSTD exp(_Res * _Par0._Logm - _XLgamma(_Res + 1.0) - _Par0._Gx1)) { + <= 0.9 * (1.0 + _Yx * _Yx) * _CSTD exp(_Res * _Par0._Logm - _STD lgamma(_Res + 1.0) - _Par0._Gx1)) { return _Res; } } @@ -2086,7 +2082,7 @@ public: _Px = _Px0; _Pp = _Px < 0.5 ? _Px : (1.0 - _Px); _Mean = _Tx * _Pp; - _Gx1 = _XLgamma(_Tx + 1.0); + _Gx1 = _STD lgamma(_Tx + 1.0); _Sqrt = _CSTD sqrt(2 * _Mean * (1 - _Pp)); _Logp = _CSTD log(_Pp); _Logp1 = _CSTD log(1.0 - _Pp); @@ -2232,7 +2228,7 @@ private: } if (_Nrand_impl<_Ty1>(_Eng) <= 1.2 * _Par0._Sqrt * (1.0 + _Yx * _Yx) - * _CSTD exp(_Par0._Gx1 - _XLgamma(_Res + 1.0) - _XLgamma(_Par0._Tx - _Res + 1.0) + * _CSTD exp(_Par0._Gx1 - _STD lgamma(_Res + 1.0) - _STD lgamma(_Par0._Tx - _Res + 1.0) + _Res * _Par0._Logp + (_Par0._Tx - _Res) * _Par0._Logp1)) { break; } diff --git a/stl/src/xlgamma.cpp b/stl/src/xlgamma.cpp index 1eff9756c12..4367cf6f500 100644 --- a/stl/src/xlgamma.cpp +++ b/stl/src/xlgamma.cpp @@ -1,63 +1,21 @@ // Copyright (c) Microsoft Corporation. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// moderately accurate lgamma function for random - -// TRANSITION, ABI: This should be superseded by the CRT's lgamma(). - #include -// #include _STD_BEGIN -_CRTIMP2_PURE float __CLRCALL_PURE_OR_CDECL _XLgamma(float) noexcept; -_CRTIMP2_PURE double __CLRCALL_PURE_OR_CDECL _XLgamma(double) noexcept; -_CRTIMP2_PURE long double __CLRCALL_PURE_OR_CDECL _XLgamma(long double) noexcept; - -float __CLRCALL_PURE_OR_CDECL _XLgamma(float x) noexcept { // moderately accurate log gamma - static const float coeff[6] = {76.18009172947146F, -86.50532032941677F, 24.01409824083091F, -1.23173972450155F, - 0.1208650973866179E-2F, -0.5395239384953E-5F}; - - float val0 = x + 5.5F; - val0 -= (x + 0.5F) * _STD log(val0); - float val1 = 1.000000000190015F; - float y = x + 1.0F; - - for (int i = 0; i < 6; ++i, y += 1.0) { - val1 += coeff[i] / y; - } - - return -val0 + _STD log(2.5066282746310005F * val1 / x); +// TRANSITION, ABI: preserved for binary compatibility +_CRTIMP2_PURE float __CLRCALL_PURE_OR_CDECL _XLgamma(float x) noexcept { + return _STD lgamma(x); } -double __CLRCALL_PURE_OR_CDECL _XLgamma(double x) noexcept { // moderately accurate log gamma - static const double coeff[6] = {76.18009172947146, -86.50532032941677, 24.01409824083091, -1.23173972450155, - 0.1208650973866179E-2, -0.5395239384953E-5}; - - double val0 = x + 5.5; - val0 -= (x + 0.5) * _STD log(val0); - double val1 = 1.000000000190015; - double y = x + 1; - - for (int i = 0; i < 6; ++i, y += 1.0) { - val1 += coeff[i] / y; - } - - return -val0 + _STD log(2.5066282746310005 * val1 / x); +// TRANSITION, ABI: preserved for binary compatibility +_CRTIMP2_PURE double __CLRCALL_PURE_OR_CDECL _XLgamma(double x) noexcept { + return _STD lgamma(x); } -long double __CLRCALL_PURE_OR_CDECL _XLgamma(long double x) noexcept { // moderately accurate log gamma - static const long double coeff[6] = {76.18009172947146, -86.50532032941677, 24.01409824083091, -1.23173972450155, - 0.1208650973866179E-2, -0.5395239384953E-5}; - - long double val0 = x + 5.5; - val0 -= (x + 0.5) * _STD log(val0); - long double val1 = 1.000000000190015; - long double y = x + 1; - - for (int i = 0; i < 6; ++i, y += 1.0) { - val1 += coeff[i] / y; - } - - return -val0 + _STD log(2.5066282746310005 * val1 / x); +// TRANSITION, ABI: preserved for binary compatibility +_CRTIMP2_PURE long double __CLRCALL_PURE_OR_CDECL _XLgamma(long double x) noexcept { + return _STD lgamma(x); } _STD_END