4239f144ce
libquadmath sources are mostly based on glibc sources at present, but derived from them by a manual editing / substitution process and with subsequent manual merges. The manual effort involved in merges means they are sometimes incomplete and long-delayed. Since libquadmath was first created, glibc's support for this format has undergone significant changes so that it can also be used in glibc to provide *f128 functions for the _Float128 type from TS 18661-3. This makes it significantly easier to use it for libquadmath in a more automated fashion, since glibc has a float128_private.h header that redefines many identifiers as macros as needed for building *f128 functions. Simply using float128_private.h directly in libquadmath, with unmodified glibc sources except for changing function names in that one header to be *q instead of *f128, would be tricky, given its dependence on lots of other glibc-internal headers (whereas libquadmath supports non-glibc systems), and also given how some libm functions in glibc are built from type-generic templates using a further set of macros rather than from separate function implementations for each type. So instead this patch adds a script update-quadmath.py to convert glibc sources into libquadmath ones, and the script reads float128_private.h to identify many of the substitutions it should make. quadmath-imp.h is updated with various new internal definitions, taken from glibc as needed; this is the main place expected to need updating manually when subsequent merges from glibc are done using the script. No attempt is made to make the script output match the details of existing formatting, although the differences are of a size that makes a rough comparison (ignoring whitespace) possible. Two new public interfaces are added to libquadmath, exp2q and issignalingq, at a new QUADMATH_1.2 symbol version, since those interfaces are used internally by some of the glibc sources being merged into libquadmath; although there is a new symbol version, no change however is made to the libtool version in the libtool-version file. Although there are various other interfaces now in glibc libm but not in libquadmath, this patch does nothing to add such interfaces (although adding many of them would in fact be easy to do, given the script). One internal file (not providing any public interfaces), math/isinf_nsq.c, is removed, as no longer used by anything in libquadmath after the merge. Conditionals in individual source files on <fenv.h> availability or features are moved into quadmath-imp.h (providing trivial macro versions of the functions if real implementations aren't available), to simplify the substitutions in individual source files. Note however that I haven't tested for any configurations lacking <fenv.h>, so further changes could well be needed there. Two files in libquadmath/math/ are based on glibc sources but not updated in this patch: fmaq.c and rem_pio2q.c. Both could be updated after further changes to the script (and quadmath-imp.h as needed); in the case of rem_pio2q.c, based on two separate glibc source files, those separate files would naturally be split out into separate libquadmath source files in the process (as done in this patch with expq_table.h and tanq_kernel.c, where previously two glibc source files had been merged into one libquadmath source file). complex.c, nanq.c and sqrtq.c are not based on glibc sources (though four of the (trivial) functions in complex.c could readily be replaced by instead using the four corresponding files from glibc, if desired). libquadmath also has printf/ and strtod/ sources based on glibc, also mostly not updated for a long time. Again the script could no doubt be made to generate those automatically, although that would be a larger change (effectively some completely separate logic in the script, not sharing much if anything with the existing code). Bootstrapped with no regressions on x86_64-pc-linux-gnu. PR libquadmath/68686 * Makefile.am: (libquadmath_la_SOURCES): Remove math/isinf_nsq.c. Add math/exp2q.c math/issignalingq.c math/lgammaq_neg.c math/lgammaq_product.c math/tanq_kernel.c math/tgammaq_product.c math/casinhq_kernel.c. * Makefile.in: Regenerate. * libquadmath.texi (exp2q, issignalingq): Document. * quadmath-imp.h: Include <errno.h>, <limits.h>, <stdbool.h> and <fenv.h>. (HIGH_ORDER_BIT_IS_SET_FOR_SNAN, FIX_FLT128_LONG_CONVERT_OVERFLOW) (FIX_FLT128_LLONG_CONVERT_OVERFLOW, __quadmath_kernel_tanq) (__quadmath_gamma_productq, __quadmath_gammaq_r) (__quadmath_lgamma_negq, __quadmath_lgamma_productq) (__quadmath_lgammaq_r, __quadmath_kernel_casinhq, mul_splitq) (math_check_force_underflow_complex, __glibc_likely) (__glibc_unlikely, struct rm_ctx, SET_RESTORE_ROUNDF128) (libc_feholdsetround_ctx, libc_feresetround_ctx): New. (feraiseexcept, fenv_t, feholdexcept, fesetround, feupdateenv) (fesetenv, fetestexcept, feclearexcept): Define if not supported through <fenv.h>. (__quadmath_isinf_nsq): Remove. * quadmath.h (exp2q, issignalingq): New. * quadmath.map (QUADMATH_1.2): New. * quadmath_weak.h (exp2q, issignalingq): New. * update-quadmath.py: New file. * math/isinf_nsq.c: Remove file. * math/casinhq_kernel.c, math/exp2q.c, math/expq_table.h, math/issignalingq.c, math/lgammaq_neg.c, math/lgammaq_product.c, math/tanq_kernel.c, math/tgammaq_product.c: New files. Generated from glibc sources with update-quadmath.py. * math/acoshq.c, math/acosq.c, math/asinhq.c, math/asinq.c, math/atan2q.c, math/atanhq.c, math/atanq.c, math/cacoshq.c, math/cacosq.c, math/casinhq.c, math/casinq.c, math/catanhq.c, math/catanq.c, math/cbrtq.c, math/ccoshq.c, math/ceilq.c, math/cexpq.c, math/cimagq.c, math/clog10q.c, math/clogq.c, math/conjq.c, math/copysignq.c, math/coshq.c, math/cosq.c, math/cosq_kernel.c, math/cprojq.c, math/crealq.c, math/csinhq.c, math/csinq.c, math/csqrtq.c, math/ctanhq.c, math/ctanq.c, math/erfq.c, math/expm1q.c, math/expq.c, math/fabsq.c, math/fdimq.c, math/finiteq.c, math/floorq.c, math/fmaxq.c, math/fminq.c, math/fmodq.c, math/frexpq.c, math/hypotq.c, math/ilogbq.c, math/isinfq.c, math/isnanq.c, math/j0q.c, math/j1q.c, math/jnq.c, math/ldexpq.c, math/lgammaq.c, math/llrintq.c, math/llroundq.c, math/log10q.c, math/log1pq.c, math/log2q.c, math/logbq.c, math/logq.c, math/lrintq.c, math/lroundq.c, math/modfq.c, math/nearbyintq.c, math/nextafterq.c, math/powq.c, math/remainderq.c, math/remquoq.c, math/rintq.c, math/roundq.c, math/scalblnq.c, math/scalbnq.c, math/signbitq.c, math/sincos_table.c, math/sincosq.c, math/sincosq_kernel.c, math/sinhq.c, math/sinq.c, math/sinq_kernel.c, math/tanhq.c, math/tanq.c, math/tgammaq.c, math/truncq.c, math/x2y2m1q.c: Regenerate from glibc sources with update-quadmath.py. From-SVN: r265822
224 lines
6.3 KiB
C
224 lines
6.3 KiB
C
/* Implementation of gamma function according to ISO C.
|
|
Copyright (C) 1997-2018 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997 and
|
|
Jakub Jelinek <jj@ultra.linux.cz, 1999.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
#include "quadmath-imp.h"
|
|
__float128
|
|
tgammaq (__float128 x)
|
|
{
|
|
int sign;
|
|
__float128 ret;
|
|
ret = __quadmath_gammaq_r (x, &sign);
|
|
return sign < 0 ? -ret : ret;
|
|
}
|
|
|
|
/* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) inside exp in Stirling's
|
|
approximation to gamma function. */
|
|
|
|
static const __float128 gamma_coeff[] =
|
|
{
|
|
0x1.5555555555555555555555555555p-4Q,
|
|
-0xb.60b60b60b60b60b60b60b60b60b8p-12Q,
|
|
0x3.4034034034034034034034034034p-12Q,
|
|
-0x2.7027027027027027027027027028p-12Q,
|
|
0x3.72a3c5631fe46ae1d4e700dca8f2p-12Q,
|
|
-0x7.daac36664f1f207daac36664f1f4p-12Q,
|
|
0x1.a41a41a41a41a41a41a41a41a41ap-8Q,
|
|
-0x7.90a1b2c3d4e5f708192a3b4c5d7p-8Q,
|
|
0x2.dfd2c703c0cfff430edfd2c703cp-4Q,
|
|
-0x1.6476701181f39edbdb9ce625987dp+0Q,
|
|
0xd.672219167002d3a7a9c886459cp+0Q,
|
|
-0x9.cd9292e6660d55b3f712eb9e07c8p+4Q,
|
|
0x8.911a740da740da740da740da741p+8Q,
|
|
-0x8.d0cc570e255bf59ff6eec24b49p+12Q,
|
|
};
|
|
|
|
#define NCOEFF (sizeof (gamma_coeff) / sizeof (gamma_coeff[0]))
|
|
|
|
/* Return gamma (X), for positive X less than 1775, in the form R *
|
|
2^(*EXP2_ADJ), where R is the return value and *EXP2_ADJ is set to
|
|
avoid overflow or underflow in intermediate calculations. */
|
|
|
|
static __float128
|
|
gammal_positive (__float128 x, int *exp2_adj)
|
|
{
|
|
int local_signgam;
|
|
if (x < 0.5Q)
|
|
{
|
|
*exp2_adj = 0;
|
|
return expq (__quadmath_lgammaq_r (x + 1, &local_signgam)) / x;
|
|
}
|
|
else if (x <= 1.5Q)
|
|
{
|
|
*exp2_adj = 0;
|
|
return expq (__quadmath_lgammaq_r (x, &local_signgam));
|
|
}
|
|
else if (x < 12.5Q)
|
|
{
|
|
/* Adjust into the range for using exp (lgamma). */
|
|
*exp2_adj = 0;
|
|
__float128 n = ceilq (x - 1.5Q);
|
|
__float128 x_adj = x - n;
|
|
__float128 eps;
|
|
__float128 prod = __quadmath_gamma_productq (x_adj, 0, n, &eps);
|
|
return (expq (__quadmath_lgammaq_r (x_adj, &local_signgam))
|
|
* prod * (1 + eps));
|
|
}
|
|
else
|
|
{
|
|
__float128 eps = 0;
|
|
__float128 x_eps = 0;
|
|
__float128 x_adj = x;
|
|
__float128 prod = 1;
|
|
if (x < 24)
|
|
{
|
|
/* Adjust into the range for applying Stirling's
|
|
approximation. */
|
|
__float128 n = ceilq (24 - x);
|
|
x_adj = x + n;
|
|
x_eps = (x - (x_adj - n));
|
|
prod = __quadmath_gamma_productq (x_adj - n, x_eps, n, &eps);
|
|
}
|
|
/* The result is now gamma (X_ADJ + X_EPS) / (PROD * (1 + EPS)).
|
|
Compute gamma (X_ADJ + X_EPS) using Stirling's approximation,
|
|
starting by computing pow (X_ADJ, X_ADJ) with a power of 2
|
|
factored out. */
|
|
__float128 exp_adj = -eps;
|
|
__float128 x_adj_int = roundq (x_adj);
|
|
__float128 x_adj_frac = x_adj - x_adj_int;
|
|
int x_adj_log2;
|
|
__float128 x_adj_mant = frexpq (x_adj, &x_adj_log2);
|
|
if (x_adj_mant < M_SQRT1_2q)
|
|
{
|
|
x_adj_log2--;
|
|
x_adj_mant *= 2;
|
|
}
|
|
*exp2_adj = x_adj_log2 * (int) x_adj_int;
|
|
__float128 ret = (powq (x_adj_mant, x_adj)
|
|
* exp2q (x_adj_log2 * x_adj_frac)
|
|
* expq (-x_adj)
|
|
* sqrtq (2 * M_PIq / x_adj)
|
|
/ prod);
|
|
exp_adj += x_eps * logq (x_adj);
|
|
__float128 bsum = gamma_coeff[NCOEFF - 1];
|
|
__float128 x_adj2 = x_adj * x_adj;
|
|
for (size_t i = 1; i <= NCOEFF - 1; i++)
|
|
bsum = bsum / x_adj2 + gamma_coeff[NCOEFF - 1 - i];
|
|
exp_adj += bsum / x_adj;
|
|
return ret + ret * expm1q (exp_adj);
|
|
}
|
|
}
|
|
|
|
__float128
|
|
__quadmath_gammaq_r (__float128 x, int *signgamp)
|
|
{
|
|
int64_t hx;
|
|
uint64_t lx;
|
|
__float128 ret;
|
|
|
|
GET_FLT128_WORDS64 (hx, lx, x);
|
|
|
|
if (((hx & 0x7fffffffffffffffLL) | lx) == 0)
|
|
{
|
|
/* Return value for x == 0 is Inf with divide by zero exception. */
|
|
*signgamp = 0;
|
|
return 1.0 / x;
|
|
}
|
|
if (hx < 0 && (uint64_t) hx < 0xffff000000000000ULL && rintq (x) == x)
|
|
{
|
|
/* Return value for integer x < 0 is NaN with invalid exception. */
|
|
*signgamp = 0;
|
|
return (x - x) / (x - x);
|
|
}
|
|
if (hx == 0xffff000000000000ULL && lx == 0)
|
|
{
|
|
/* x == -Inf. According to ISO this is NaN. */
|
|
*signgamp = 0;
|
|
return x - x;
|
|
}
|
|
if ((hx & 0x7fff000000000000ULL) == 0x7fff000000000000ULL)
|
|
{
|
|
/* Positive infinity (return positive infinity) or NaN (return
|
|
NaN). */
|
|
*signgamp = 0;
|
|
return x + x;
|
|
}
|
|
|
|
if (x >= 1756)
|
|
{
|
|
/* Overflow. */
|
|
*signgamp = 0;
|
|
return FLT128_MAX * FLT128_MAX;
|
|
}
|
|
else
|
|
{
|
|
SET_RESTORE_ROUNDF128 (FE_TONEAREST);
|
|
if (x > 0)
|
|
{
|
|
*signgamp = 0;
|
|
int exp2_adj;
|
|
ret = gammal_positive (x, &exp2_adj);
|
|
ret = scalbnq (ret, exp2_adj);
|
|
}
|
|
else if (x >= -FLT128_EPSILON / 4)
|
|
{
|
|
*signgamp = 0;
|
|
ret = 1 / x;
|
|
}
|
|
else
|
|
{
|
|
__float128 tx = truncq (x);
|
|
*signgamp = (tx == 2 * truncq (tx / 2)) ? -1 : 1;
|
|
if (x <= -1775)
|
|
/* Underflow. */
|
|
ret = FLT128_MIN * FLT128_MIN;
|
|
else
|
|
{
|
|
__float128 frac = tx - x;
|
|
if (frac > 0.5Q)
|
|
frac = 1 - frac;
|
|
__float128 sinpix = (frac <= 0.25Q
|
|
? sinq (M_PIq * frac)
|
|
: cosq (M_PIq * (0.5Q - frac)));
|
|
int exp2_adj;
|
|
ret = M_PIq / (-x * sinpix
|
|
* gammal_positive (-x, &exp2_adj));
|
|
ret = scalbnq (ret, -exp2_adj);
|
|
math_check_force_underflow_nonneg (ret);
|
|
}
|
|
}
|
|
}
|
|
if (isinfq (ret) && x != 0)
|
|
{
|
|
if (*signgamp < 0)
|
|
return -(-copysignq (FLT128_MAX, ret) * FLT128_MAX);
|
|
else
|
|
return copysignq (FLT128_MAX, ret) * FLT128_MAX;
|
|
}
|
|
else if (ret == 0)
|
|
{
|
|
if (*signgamp < 0)
|
|
return -(-copysignq (FLT128_MIN, ret) * FLT128_MIN);
|
|
else
|
|
return copysignq (FLT128_MIN, ret) * FLT128_MIN;
|
|
}
|
|
else
|
|
return ret;
|
|
}
|