From 3320e88ab4c8afe55bf9a71ded2ccc6c2220b8c9 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Thu, 4 May 2017 16:36:39 +0000 Subject: [PATCH] Use lgamma_r instead of lgamma in binomial_distribution, because freakin' POSIX took a perfectly fine call and made it not thread safe. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@302168 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/random | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/include/random b/include/random index 83fff90a4..c83db12af 100644 --- a/include/random +++ b/include/random @@ -3997,16 +3997,20 @@ public: {return !(__x == __y);} }; +extern "C" double lgamma_r(double, int *); + template -binomial_distribution<_IntType>::param_type::param_type(result_type __t, double __p) +binomial_distribution<_IntType>::param_type::param_type(const result_type __t, const double __p) : __t_(__t), __p_(__p) { if (0 < __p_ && __p_ < 1) { + int __sign; __r0_ = static_cast((__t_ + 1) * __p_); - __pr_ = _VSTD::exp(_VSTD::lgamma(__t_ + 1.) - _VSTD::lgamma(__r0_ + 1.) - - _VSTD::lgamma(__t_ - __r0_ + 1.) + __r0_ * _VSTD::log(__p_) + - (__t_ - __r0_) * _VSTD::log(1 - __p_)); + __pr_ = _VSTD::exp(lgamma_r(__t_ + 1., &__sign) - + lgamma_r(__r0_ + 1., &__sign) - + lgamma_r(__t_ - __r0_ + 1., &__sign) + __r0_ * _VSTD::log(__p_) + + (__t_ - __r0_) * _VSTD::log(1 - __p_)); __odds_ratio_ = __p_ / (1 - __p_); } }