glibc/sysdeps/ieee754/ldbl-96
Joseph Myers 050f29c188 Fix lgamma (negative) inaccuracy (bug 2542, bug 2543, bug 2558).
The existing implementations of lgamma functions (except for the ia64
versions) use the reflection formula for negative arguments.  This
suffers large inaccuracy from cancellation near zeros of lgamma (near
where the gamma function is +/- 1).

This patch fixes this inaccuracy.  For arguments above -2, there are
no zeros and no large cancellation, while for sufficiently large
negative arguments the zeros are so close to integers that even for
integers +/- 1ulp the log(gamma(1-x)) term dominates and cancellation
is not significant.  Thus, it is only necessary to take special care
about cancellation for arguments around a limited number of zeros.

Accordingly, this patch uses precomputed tables of relevant zeros,
expressed as the sum of two floating-point values.  The log of the
ratio of two sines can be computed accurately using log1p in cases
where log would lose accuracy.  The log of the ratio of two gamma(1-x)
values can be computed using Stirling's approximation (the difference
between two values of that approximation to lgamma being computable
without computing the two values and then subtracting), with
appropriate adjustments (which don't reduce accuracy too much) in
cases where 1-x is too small to use Stirling's approximation directly.

In the interval from -3 to -2, using the ratios of sines and of
gamma(1-x) can still produce too much cancellation between those two
parts of the computation (and that interval is also the worst interval
for computing the ratio between gamma(1-x) values, which computation
becomes more accurate, while being less critical for the final result,
for larger 1-x).  Because this can result in errors slightly above
those accepted in glibc, this interval is instead dealt with by
polynomial approximations.  Separate polynomial approximations to
(|gamma(x)|-1)(x-n)/(x-x0) are used for each interval of length 1/8
from -3 to -2, where n (-3 or -2) is the nearest integer to the
1/8-interval and x0 is the zero of lgamma in the relevant half-integer
interval (-3 to -2.5 or -2.5 to -2).

Together, the two approaches are intended to give sufficient accuracy
for all negative arguments in the problem range.  Outside that range,
the previous implementation continues to be used.

Tested for x86_64, x86, mips64 and powerpc.  The mips64 and powerpc
testing shows up pre-existing problems for ldbl-128 and ldbl-128ibm
with large negative arguments giving spurious "invalid" exceptions
(exposed by newly added tests for cases this patch doesn't affect the
logic for); I'll address those problems separately.

	[BZ #2542]
	[BZ #2543]
	[BZ #2558]
	* sysdeps/ieee754/dbl-64/e_lgamma_r.c (__ieee754_lgamma_r): Call
	__lgamma_neg for arguments from -28.0 to -2.0.
	* sysdeps/ieee754/flt-32/e_lgammaf_r.c (__ieee754_lgammaf_r): Call
	__lgamma_negf for arguments from -15.0 to -2.0.
	* sysdeps/ieee754/ldbl-128/e_lgammal_r.c (__ieee754_lgammal_r):
	Call __lgamma_negl for arguments from -48.0 or -50.0 to -2.0.
	* sysdeps/ieee754/ldbl-96/e_lgammal_r.c (__ieee754_lgammal_r):
	Call __lgamma_negl for arguments from -33.0 to -2.0.
	* sysdeps/ieee754/dbl-64/lgamma_neg.c: New file.
	* sysdeps/ieee754/dbl-64/lgamma_product.c: Likewise.
	* sysdeps/ieee754/flt-32/lgamma_negf.c: Likewise.
	* sysdeps/ieee754/flt-32/lgamma_productf.c: Likewise.
	* sysdeps/ieee754/ldbl-128/lgamma_negl.c: Likewise.
	* sysdeps/ieee754/ldbl-128/lgamma_productl.c: Likewise.
	* sysdeps/ieee754/ldbl-128ibm/lgamma_negl.c: Likewise.
	* sysdeps/ieee754/ldbl-128ibm/lgamma_productl.c: Likewise.
	* sysdeps/ieee754/ldbl-96/lgamma_negl.c: Likewise.
	* sysdeps/ieee754/ldbl-96/lgamma_product.c: Likewise.
	* sysdeps/ieee754/ldbl-96/lgamma_productl.c: Likewise.
	* sysdeps/generic/math_private.h (__lgamma_negf): New prototype.
	(__lgamma_neg): Likewise.
	(__lgamma_negl): Likewise.
	(__lgamma_product): Likewise.
	(__lgamma_productl): Likewise.
	* math/Makefile (libm-calls): Add lgamma_neg and lgamma_product.
	* math/auto-libm-test-in: Add more tests of lgamma.
	* math/auto-libm-test-out: Regenerated.
	* sysdeps/i386/fpu/libm-test-ulps: Update.
	* sysdeps/x86_64/fpu/libm-test-ulps: Likewise.
2015-09-10 22:27:58 +00:00
..
e_acoshl.c Fix acosh (1) in round-downward mode (bug 16927). 2014-05-14 12:35:40 +00:00
e_asinl.c Fix asin missing underflows (bug 16351). 2015-02-26 17:18:54 +00:00
e_atanhl.c Fix atanhl missing underflows (bug 16352). 2015-05-15 22:07:57 +00:00
e_coshl.c Fix cosh spurious underflows from expm1 (bug 16354), inaccurate results near 0 (bug 17061). 2014-06-23 20:20:10 +00:00
e_gammal_r.c Improve tgamma accuracy (bug 18613). 2015-06-29 23:29:35 +00:00
e_hypotl.c Fix hypot handling of subnormals (bug 16316, bug 16330). 2013-12-17 13:42:13 +00:00
e_j0l.c Avoid uninitialized warnings in Bessel functions. 2015-02-26 21:49:19 +00:00
e_j1l.c Fix j1, jn missing underflows (bug 16559). 2015-06-29 16:52:16 +00:00
e_jnl.c Fix j1, jn missing underflows (bug 16559). 2015-06-29 16:52:16 +00:00
e_lgammal_r.c Fix lgamma (negative) inaccuracy (bug 2542, bug 2543, bug 2558). 2015-09-10 22:27:58 +00:00
e_rem_pio2l.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
e_sinhl.c Fix sinh missing underflows (bug 16519). 2015-08-06 23:01:09 +00:00
gamma_product.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
gamma_productl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
k_cosl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
k_sinl.c Fix sin, sincos missing underflows (bug 16526, bug 16538). 2015-06-23 22:24:20 +00:00
k_tanl.c Disable uninitialized warning with GCC 4.8 2015-08-26 16:10:43 -07:00
ldbl2mpn.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
lgamma_negl.c Fix lgamma (negative) inaccuracy (bug 2542, bug 2543, bug 2558). 2015-09-10 22:27:58 +00:00
lgamma_product.c Fix lgamma (negative) inaccuracy (bug 2542, bug 2543, bug 2558). 2015-09-10 22:27:58 +00:00
lgamma_productl.c Fix lgamma (negative) inaccuracy (bug 2542, bug 2543, bug 2558). 2015-09-10 22:27:58 +00:00
math_ldbl.h Fix a typo. 2000-12-21 23:30:28 +00:00
mpn2ldbl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
printf_fphex.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
s_asinhl.c Fix asinh missing underflows (bug 16350). 2015-06-18 23:27:41 +00:00
s_cbrtl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
s_copysignl.c Use <> for math.h and math_private.h everywhere. 2012-03-09 16:09:10 -08:00
s_cosl.c Implement ldbl-96 sinl / cosl / sincosl (bug 13851). 2012-03-16 12:30:05 +00:00
s_erfl.c Fix erf underflow handling near 0 (bug 16516). 2014-05-14 12:34:03 +00:00
s_fma.c Replace finite with isfinite. 2015-06-03 16:35:44 +01:00
s_fmal.c Fix fma spurious underflows (bug 18824). 2015-08-14 17:15:06 +00:00
s_frexpl.c Use <> for math.h and math_private.h everywhere. 2012-03-09 16:09:10 -08:00
s_isinf_nsl.c Use <> for math.h and math_private.h everywhere. 2012-03-09 16:09:10 -08:00
s_issignalingl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
s_llrintl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
s_llroundl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
s_lrintl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
s_lroundl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
s_modfl.c Use <> for math.h and math_private.h everywhere. 2012-03-09 16:09:10 -08:00
s_nexttoward.c Fix nexttoward bugs (bugs 2550, 2570). 2012-05-01 15:37:43 +00:00
s_nexttowardf.c Fix nexttoward bugs (bugs 2550, 2570). 2012-05-01 15:37:43 +00:00
s_remquol.c Fix ldbl-96 remquol (finite, Inf) (bug 18244). 2015-05-19 23:44:28 +00:00
s_roundl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
s_scalblnl.c Fix ldbl-96 scalblnl underflowing results (bug 17803). 2015-01-12 23:02:14 +00:00
s_signbitl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
s_sincosl.c Fix sincos errno setting (bug 15467). 2015-02-11 23:17:25 +00:00
s_sinl.c Implement ldbl-96 sinl / cosl / sincosl (bug 13851). 2012-03-16 12:30:05 +00:00
s_tanhl.c Fix tanh missing underflows (bug 16520). 2015-08-13 16:40:39 +00:00
s_tanl.c Fix tan, tanl for large inputs. 2012-03-16 20:05:37 +00:00
strtold_l.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
t_sincosl.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
w_expl.c This patch renames all uses of __isinf*, __isnan*, __finite* and __signbit* to use standard C99 macros. This has no effect on generated code. 2015-06-03 15:41:36 +01:00
x2y2m1.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00
x2y2m1l.c Update copyright dates with scripts/update-copyrights. 2015-01-02 16:29:47 +00:00