|
Packit |
6c4009 |
/* Implementation of gamma function according to ISO C.
|
|
Packit |
6c4009 |
Copyright (C) 1997-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997 and
|
|
Packit |
6c4009 |
Jakub Jelinek
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <math.h>
|
|
Packit |
6c4009 |
#include <math_private.h>
|
|
Packit |
6c4009 |
#include <math-underflow.h>
|
|
Packit |
6c4009 |
#include <float.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) inside exp in Stirling's
|
|
Packit |
6c4009 |
approximation to gamma function. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static const _Float128 gamma_coeff[] =
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
L(0x1.5555555555555555555555555555p-4),
|
|
Packit |
6c4009 |
L(-0xb.60b60b60b60b60b60b60b60b60b8p-12),
|
|
Packit |
6c4009 |
L(0x3.4034034034034034034034034034p-12),
|
|
Packit |
6c4009 |
L(-0x2.7027027027027027027027027028p-12),
|
|
Packit |
6c4009 |
L(0x3.72a3c5631fe46ae1d4e700dca8f2p-12),
|
|
Packit |
6c4009 |
L(-0x7.daac36664f1f207daac36664f1f4p-12),
|
|
Packit |
6c4009 |
L(0x1.a41a41a41a41a41a41a41a41a41ap-8),
|
|
Packit |
6c4009 |
L(-0x7.90a1b2c3d4e5f708192a3b4c5d7p-8),
|
|
Packit |
6c4009 |
L(0x2.dfd2c703c0cfff430edfd2c703cp-4),
|
|
Packit |
6c4009 |
L(-0x1.6476701181f39edbdb9ce625987dp+0),
|
|
Packit |
6c4009 |
L(0xd.672219167002d3a7a9c886459cp+0),
|
|
Packit |
6c4009 |
L(-0x9.cd9292e6660d55b3f712eb9e07c8p+4),
|
|
Packit |
6c4009 |
L(0x8.911a740da740da740da740da741p+8),
|
|
Packit |
6c4009 |
L(-0x8.d0cc570e255bf59ff6eec24b49p+12),
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define NCOEFF (sizeof (gamma_coeff) / sizeof (gamma_coeff[0]))
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Return gamma (X), for positive X less than 1775, in the form R *
|
|
Packit |
6c4009 |
2^(*EXP2_ADJ), where R is the return value and *EXP2_ADJ is set to
|
|
Packit |
6c4009 |
avoid overflow or underflow in intermediate calculations. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static _Float128
|
|
Packit |
6c4009 |
gammal_positive (_Float128 x, int *exp2_adj)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int local_signgam;
|
|
Packit |
6c4009 |
if (x < L(0.5))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*exp2_adj = 0;
|
|
Packit |
6c4009 |
return __ieee754_expl (__ieee754_lgammal_r (x + 1, &local_signgam)) / x;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (x <= L(1.5))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*exp2_adj = 0;
|
|
Packit |
6c4009 |
return __ieee754_expl (__ieee754_lgammal_r (x, &local_signgam));
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (x < L(12.5))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Adjust into the range for using exp (lgamma). */
|
|
Packit |
6c4009 |
*exp2_adj = 0;
|
|
Packit |
6c4009 |
_Float128 n = __ceill (x - L(1.5));
|
|
Packit |
6c4009 |
_Float128 x_adj = x - n;
|
|
Packit |
6c4009 |
_Float128 eps;
|
|
Packit |
6c4009 |
_Float128 prod = __gamma_productl (x_adj, 0, n, &eps);
|
|
Packit |
6c4009 |
return (__ieee754_expl (__ieee754_lgammal_r (x_adj, &local_signgam))
|
|
Packit |
6c4009 |
* prod * (1 + eps));
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
_Float128 eps = 0;
|
|
Packit |
6c4009 |
_Float128 x_eps = 0;
|
|
Packit |
6c4009 |
_Float128 x_adj = x;
|
|
Packit |
6c4009 |
_Float128 prod = 1;
|
|
Packit |
6c4009 |
if (x < 24)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Adjust into the range for applying Stirling's
|
|
Packit |
6c4009 |
approximation. */
|
|
Packit |
6c4009 |
_Float128 n = __ceill (24 - x);
|
|
Packit |
6c4009 |
x_adj = x + n;
|
|
Packit |
6c4009 |
x_eps = (x - (x_adj - n));
|
|
Packit |
6c4009 |
prod = __gamma_productl (x_adj - n, x_eps, n, &eps);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
/* The result is now gamma (X_ADJ + X_EPS) / (PROD * (1 + EPS)).
|
|
Packit |
6c4009 |
Compute gamma (X_ADJ + X_EPS) using Stirling's approximation,
|
|
Packit |
6c4009 |
starting by computing pow (X_ADJ, X_ADJ) with a power of 2
|
|
Packit |
6c4009 |
factored out. */
|
|
Packit |
6c4009 |
_Float128 exp_adj = -eps;
|
|
Packit |
6c4009 |
_Float128 x_adj_int = __roundl (x_adj);
|
|
Packit |
6c4009 |
_Float128 x_adj_frac = x_adj - x_adj_int;
|
|
Packit |
6c4009 |
int x_adj_log2;
|
|
Packit |
6c4009 |
_Float128 x_adj_mant = __frexpl (x_adj, &x_adj_log2);
|
|
Packit |
6c4009 |
if (x_adj_mant < M_SQRT1_2l)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
x_adj_log2--;
|
|
Packit |
6c4009 |
x_adj_mant *= 2;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
*exp2_adj = x_adj_log2 * (int) x_adj_int;
|
|
Packit |
6c4009 |
_Float128 ret = (__ieee754_powl (x_adj_mant, x_adj)
|
|
Packit |
6c4009 |
* __ieee754_exp2l (x_adj_log2 * x_adj_frac)
|
|
Packit |
6c4009 |
* __ieee754_expl (-x_adj)
|
|
Packit |
6c4009 |
* sqrtl (2 * M_PIl / x_adj)
|
|
Packit |
6c4009 |
/ prod);
|
|
Packit |
6c4009 |
exp_adj += x_eps * __ieee754_logl (x_adj);
|
|
Packit |
6c4009 |
_Float128 bsum = gamma_coeff[NCOEFF - 1];
|
|
Packit |
6c4009 |
_Float128 x_adj2 = x_adj * x_adj;
|
|
Packit |
6c4009 |
for (size_t i = 1; i <= NCOEFF - 1; i++)
|
|
Packit |
6c4009 |
bsum = bsum / x_adj2 + gamma_coeff[NCOEFF - 1 - i];
|
|
Packit |
6c4009 |
exp_adj += bsum / x_adj;
|
|
Packit |
6c4009 |
return ret + ret * __expm1l (exp_adj);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
_Float128
|
|
Packit |
6c4009 |
__ieee754_gammal_r (_Float128 x, int *signgamp)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int64_t hx;
|
|
Packit |
6c4009 |
uint64_t lx;
|
|
Packit |
6c4009 |
_Float128 ret;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
GET_LDOUBLE_WORDS64 (hx, lx, x);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (((hx & 0x7fffffffffffffffLL) | lx) == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Return value for x == 0 is Inf with divide by zero exception. */
|
|
Packit |
6c4009 |
*signgamp = 0;
|
|
Packit |
6c4009 |
return 1.0 / x;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (hx < 0 && (uint64_t) hx < 0xffff000000000000ULL && __rintl (x) == x)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Return value for integer x < 0 is NaN with invalid exception. */
|
|
Packit |
6c4009 |
*signgamp = 0;
|
|
Packit |
6c4009 |
return (x - x) / (x - x);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (hx == 0xffff000000000000ULL && lx == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* x == -Inf. According to ISO this is NaN. */
|
|
Packit |
6c4009 |
*signgamp = 0;
|
|
Packit |
6c4009 |
return x - x;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if ((hx & 0x7fff000000000000ULL) == 0x7fff000000000000ULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Positive infinity (return positive infinity) or NaN (return
|
|
Packit |
6c4009 |
NaN). */
|
|
Packit |
6c4009 |
*signgamp = 0;
|
|
Packit |
6c4009 |
return x + x;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (x >= 1756)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Overflow. */
|
|
Packit |
6c4009 |
*signgamp = 0;
|
|
Packit |
6c4009 |
return LDBL_MAX * LDBL_MAX;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
SET_RESTORE_ROUNDL (FE_TONEAREST);
|
|
Packit |
6c4009 |
if (x > 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*signgamp = 0;
|
|
Packit |
6c4009 |
int exp2_adj;
|
|
Packit |
6c4009 |
ret = gammal_positive (x, &exp2_adj);
|
|
Packit |
6c4009 |
ret = __scalbnl (ret, exp2_adj);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (x >= -LDBL_EPSILON / 4)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*signgamp = 0;
|
|
Packit |
6c4009 |
ret = 1 / x;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
_Float128 tx = __truncl (x);
|
|
Packit |
6c4009 |
*signgamp = (tx == 2 * __truncl (tx / 2)) ? -1 : 1;
|
|
Packit |
6c4009 |
if (x <= -1775)
|
|
Packit |
6c4009 |
/* Underflow. */
|
|
Packit |
6c4009 |
ret = LDBL_MIN * LDBL_MIN;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
_Float128 frac = tx - x;
|
|
Packit |
6c4009 |
if (frac > L(0.5))
|
|
Packit |
6c4009 |
frac = 1 - frac;
|
|
Packit |
6c4009 |
_Float128 sinpix = (frac <= L(0.25)
|
|
Packit |
6c4009 |
? __sinl (M_PIl * frac)
|
|
Packit |
6c4009 |
: __cosl (M_PIl * (L(0.5) - frac)));
|
|
Packit |
6c4009 |
int exp2_adj;
|
|
Packit |
6c4009 |
ret = M_PIl / (-x * sinpix
|
|
Packit |
6c4009 |
* gammal_positive (-x, &exp2_adj));
|
|
Packit |
6c4009 |
ret = __scalbnl (ret, -exp2_adj);
|
|
Packit |
6c4009 |
math_check_force_underflow_nonneg (ret);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (isinf (ret) && x != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (*signgamp < 0)
|
|
Packit |
6c4009 |
return -(-__copysignl (LDBL_MAX, ret) * LDBL_MAX);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
return __copysignl (LDBL_MAX, ret) * LDBL_MAX;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (ret == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (*signgamp < 0)
|
|
Packit |
6c4009 |
return -(-__copysignl (LDBL_MIN, ret) * LDBL_MIN);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
return __copysignl (LDBL_MIN, ret) * LDBL_MIN;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
return ret;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
strong_alias (__ieee754_gammal_r, __gammal_r_finite)
|