b_tgamma.c revision 92917
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1992, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 3. All advertising materials mentioning features or use of this software
141573Srgrimes *    must display the following acknowledgement:
151573Srgrimes *	This product includes software developed by the University of
161573Srgrimes *	California, Berkeley and its contributors.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes */
331573Srgrimes
341573Srgrimes#ifndef lint
351573Srgrimesstatic char sccsid[] = "@(#)gamma.c	8.1 (Berkeley) 6/4/93";
361573Srgrimes#endif /* not lint */
3792887Sobrieninclude <sys/cdefs.h>
3892887Sobrien__FBSDID("$FreeBSD: head/lib/msun/bsdsrc/b_tgamma.c 92917 2002-03-21 23:54:04Z obrien $");
391573Srgrimes
401573Srgrimes/*
411573Srgrimes * This code by P. McIlroy, Oct 1992;
421573Srgrimes *
431573Srgrimes * The financial support of UUNET Communications Services is greatfully
441573Srgrimes * acknowledged.
451573Srgrimes */
461573Srgrimes
471573Srgrimes#include <math.h>
481573Srgrimes#include "mathimpl.h"
491573Srgrimes#include <errno.h>
501573Srgrimes
511573Srgrimes/* METHOD:
521573Srgrimes * x < 0: Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x))
531573Srgrimes * 	At negative integers, return +Inf, and set errno.
541573Srgrimes *
551573Srgrimes * x < 6.5:
561573Srgrimes *	Use argument reduction G(x+1) = xG(x) to reach the
571573Srgrimes *	range [1.066124,2.066124].  Use a rational
581573Srgrimes *	approximation centered at the minimum (x0+1) to
591573Srgrimes *	ensure monotonicity.
601573Srgrimes *
611573Srgrimes * x >= 6.5: Use the asymptotic approximation (Stirling's formula)
621573Srgrimes *	adjusted for equal-ripples:
631573Srgrimes *
641573Srgrimes *	log(G(x)) ~= (x-.5)*(log(x)-1) + .5(log(2*pi)-1) + 1/x*P(1/(x*x))
651573Srgrimes *
661573Srgrimes *	Keep extra precision in multiplying (x-.5)(log(x)-1), to
671573Srgrimes *	avoid premature round-off.
681573Srgrimes *
691573Srgrimes * Special values:
701573Srgrimes *	non-positive integer:	Set overflow trap; return +Inf;
711573Srgrimes *	x > 171.63:		Set overflow trap; return +Inf;
721573Srgrimes *	NaN: 			Set invalid trap;  return NaN
731573Srgrimes *
741573Srgrimes * Accuracy: Gamma(x) is accurate to within
751573Srgrimes *	x > 0:  error provably < 0.9ulp.
761573Srgrimes *	Maximum observed in 1,000,000 trials was .87ulp.
771573Srgrimes *	x < 0:
781573Srgrimes *	Maximum observed error < 4ulp in 1,000,000 trials.
791573Srgrimes */
801573Srgrimes
8192917Sobrienstatic double neg_gam(double);
8292917Sobrienstatic double small_gam(double);
8392917Sobrienstatic double smaller_gam(double);
8492917Sobrienstatic struct Double large_gam(double);
8592917Sobrienstatic struct Double ratfun_gam(double, double);
861573Srgrimes
871573Srgrimes/*
881573Srgrimes * Rational approximation, A0 + x*x*P(x)/Q(x), on the interval
891573Srgrimes * [1.066.., 2.066..] accurate to 4.25e-19.
901573Srgrimes */
911573Srgrimes#define LEFT -.3955078125	/* left boundary for rat. approx */
921573Srgrimes#define x0 .461632144968362356785	/* xmin - 1 */
931573Srgrimes
941573Srgrimes#define a0_hi 0.88560319441088874992
951573Srgrimes#define a0_lo -.00000000000000004996427036469019695
961573Srgrimes#define P0	 6.21389571821820863029017800727e-01
971573Srgrimes#define P1	 2.65757198651533466104979197553e-01
981573Srgrimes#define P2	 5.53859446429917461063308081748e-03
991573Srgrimes#define P3	 1.38456698304096573887145282811e-03
1001573Srgrimes#define P4	 2.40659950032711365819348969808e-03
1011573Srgrimes#define Q0	 1.45019531250000000000000000000e+00
1021573Srgrimes#define Q1	 1.06258521948016171343454061571e+00
1031573Srgrimes#define Q2	-2.07474561943859936441469926649e-01
1041573Srgrimes#define Q3	-1.46734131782005422506287573015e-01
1051573Srgrimes#define Q4	 3.07878176156175520361557573779e-02
1061573Srgrimes#define Q5	 5.12449347980666221336054633184e-03
1071573Srgrimes#define Q6	-1.76012741431666995019222898833e-03
1081573Srgrimes#define Q7	 9.35021023573788935372153030556e-05
1091573Srgrimes#define Q8	 6.13275507472443958924745652239e-06
1101573Srgrimes/*
1111573Srgrimes * Constants for large x approximation (x in [6, Inf])
1121573Srgrimes * (Accurate to 2.8*10^-19 absolute)
1131573Srgrimes */
1141573Srgrimes#define lns2pi_hi 0.418945312500000
1151573Srgrimes#define lns2pi_lo -.000006779295327258219670263595
1161573Srgrimes#define Pa0	 8.33333333333333148296162562474e-02
1171573Srgrimes#define Pa1	-2.77777777774548123579378966497e-03
1181573Srgrimes#define Pa2	 7.93650778754435631476282786423e-04
1191573Srgrimes#define Pa3	-5.95235082566672847950717262222e-04
1201573Srgrimes#define Pa4	 8.41428560346653702135821806252e-04
1211573Srgrimes#define Pa5	-1.89773526463879200348872089421e-03
1221573Srgrimes#define Pa6	 5.69394463439411649408050664078e-03
1231573Srgrimes#define Pa7	-1.44705562421428915453880392761e-02
1241573Srgrimes
1251573Srgrimesstatic const double zero = 0., one = 1.0, tiny = 1e-300;
1261573Srgrimesstatic int endian;
1271573Srgrimes/*
1281573Srgrimes * TRUNC sets trailing bits in a floating-point number to zero.
1291573Srgrimes * is a temporary variable.
1301573Srgrimes */
1311573Srgrimes#if defined(vax) || defined(tahoe)
1321573Srgrimes#define _IEEE		0
1331573Srgrimes#define TRUNC(x)	x = (double) (float) (x)
1341573Srgrimes#else
1351573Srgrimes#define _IEEE		1
1361573Srgrimes#define TRUNC(x)	*(((int *) &x) + endian) &= 0xf8000000
1371573Srgrimes#define infnan(x)	0.0
1381573Srgrimes#endif
1391573Srgrimes
1401573Srgrimesdouble
1411573Srgrimesgamma(x)
1421573Srgrimes	double x;
1431573Srgrimes{
1441573Srgrimes	struct Double u;
1451573Srgrimes	endian = (*(int *) &one) ? 1 : 0;
1461573Srgrimes
1471573Srgrimes	if (x >= 6) {
1481573Srgrimes		if(x > 171.63)
1491573Srgrimes			return(one/zero);
1501573Srgrimes		u = large_gam(x);
1511573Srgrimes		return(__exp__D(u.a, u.b));
1521573Srgrimes	} else if (x >= 1.0 + LEFT + x0)
1531573Srgrimes		return (small_gam(x));
1541573Srgrimes	else if (x > 1.e-17)
1551573Srgrimes		return (smaller_gam(x));
1561573Srgrimes	else if (x > -1.e-17) {
1571573Srgrimes		if (x == 0.0)
1581573Srgrimes			if (!_IEEE) return (infnan(ERANGE));
1591573Srgrimes			else return (one/x);
1601573Srgrimes		one+1e-20;		/* Raise inexact flag. */
1611573Srgrimes		return (one/x);
1621573Srgrimes	} else if (!finite(x)) {
1631573Srgrimes		if (_IEEE)		/* x = NaN, -Inf */
1641573Srgrimes			return (x*x);
1651573Srgrimes		else
1661573Srgrimes			return (infnan(EDOM));
1671573Srgrimes	 } else
1681573Srgrimes		return (neg_gam(x));
1691573Srgrimes}
1701573Srgrimes/*
1711573Srgrimes * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
1721573Srgrimes */
1731573Srgrimesstatic struct Double
1741573Srgrimeslarge_gam(x)
1751573Srgrimes	double x;
1761573Srgrimes{
1771573Srgrimes	double z, p;
1781573Srgrimes	int i;
1791573Srgrimes	struct Double t, u, v;
1801573Srgrimes
1811573Srgrimes	z = one/(x*x);
1821573Srgrimes	p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7))))));
1831573Srgrimes	p = p/x;
1841573Srgrimes
1851573Srgrimes	u = __log__D(x);
1861573Srgrimes	u.a -= one;
1871573Srgrimes	v.a = (x -= .5);
1881573Srgrimes	TRUNC(v.a);
1891573Srgrimes	v.b = x - v.a;
1901573Srgrimes	t.a = v.a*u.a;			/* t = (x-.5)*(log(x)-1) */
1911573Srgrimes	t.b = v.b*u.a + x*u.b;
1921573Srgrimes	/* return t.a + t.b + lns2pi_hi + lns2pi_lo + p */
1931573Srgrimes	t.b += lns2pi_lo; t.b += p;
1941573Srgrimes	u.a = lns2pi_hi + t.b; u.a += t.a;
1951573Srgrimes	u.b = t.a - u.a;
1961573Srgrimes	u.b += lns2pi_hi; u.b += t.b;
1971573Srgrimes	return (u);
1981573Srgrimes}
1991573Srgrimes/*
2001573Srgrimes * Good to < 1 ulp.  (provably .90 ulp; .87 ulp on 1,000,000 runs.)
2011573Srgrimes * It also has correct monotonicity.
2021573Srgrimes */
2031573Srgrimesstatic double
2041573Srgrimessmall_gam(x)
2051573Srgrimes	double x;
2061573Srgrimes{
2071573Srgrimes	double y, ym1, t, x1;
2081573Srgrimes	struct Double yy, r;
2091573Srgrimes	y = x - one;
2101573Srgrimes	ym1 = y - one;
2111573Srgrimes	if (y <= 1.0 + (LEFT + x0)) {
2121573Srgrimes		yy = ratfun_gam(y - x0, 0);
2131573Srgrimes		return (yy.a + yy.b);
2141573Srgrimes	}
2151573Srgrimes	r.a = y;
2161573Srgrimes	TRUNC(r.a);
2171573Srgrimes	yy.a = r.a - one;
2181573Srgrimes	y = ym1;
2191573Srgrimes	yy.b = r.b = y - yy.a;
2201573Srgrimes	/* Argument reduction: G(x+1) = x*G(x) */
2211573Srgrimes	for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) {
2221573Srgrimes		t = r.a*yy.a;
2231573Srgrimes		r.b = r.a*yy.b + y*r.b;
2241573Srgrimes		r.a = t;
2251573Srgrimes		TRUNC(r.a);
2261573Srgrimes		r.b += (t - r.a);
2271573Srgrimes	}
2281573Srgrimes	/* Return r*gamma(y). */
2291573Srgrimes	yy = ratfun_gam(y - x0, 0);
2301573Srgrimes	y = r.b*(yy.a + yy.b) + r.a*yy.b;
2311573Srgrimes	y += yy.a*r.a;
2321573Srgrimes	return (y);
2331573Srgrimes}
2341573Srgrimes/*
2351573Srgrimes * Good on (0, 1+x0+LEFT].  Accurate to 1ulp.
2361573Srgrimes */
2371573Srgrimesstatic double
2381573Srgrimessmaller_gam(x)
2391573Srgrimes	double x;
2401573Srgrimes{
2411573Srgrimes	double t, d;
2421573Srgrimes	struct Double r, xx;
2431573Srgrimes	if (x < x0 + LEFT) {
2441573Srgrimes		t = x, TRUNC(t);
2451573Srgrimes		d = (t+x)*(x-t);
2461573Srgrimes		t *= t;
2471573Srgrimes		xx.a = (t + x), TRUNC(xx.a);
2481573Srgrimes		xx.b = x - xx.a; xx.b += t; xx.b += d;
2491573Srgrimes		t = (one-x0); t += x;
2501573Srgrimes		d = (one-x0); d -= t; d += x;
2511573Srgrimes		x = xx.a + xx.b;
2521573Srgrimes	} else {
2531573Srgrimes		xx.a =  x, TRUNC(xx.a);
2541573Srgrimes		xx.b = x - xx.a;
2551573Srgrimes		t = x - x0;
2561573Srgrimes		d = (-x0 -t); d += x;
2571573Srgrimes	}
2581573Srgrimes	r = ratfun_gam(t, d);
2591573Srgrimes	d = r.a/x, TRUNC(d);
2601573Srgrimes	r.a -= d*xx.a; r.a -= d*xx.b; r.a += r.b;
2611573Srgrimes	return (d + r.a/x);
2621573Srgrimes}
2631573Srgrimes/*
2641573Srgrimes * returns (z+c)^2 * P(z)/Q(z) + a0
2651573Srgrimes */
2661573Srgrimesstatic struct Double
2671573Srgrimesratfun_gam(z, c)
2681573Srgrimes	double z, c;
2691573Srgrimes{
2701573Srgrimes	int i;
2711573Srgrimes	double p, q;
2721573Srgrimes	struct Double r, t;
2731573Srgrimes
2741573Srgrimes	q = Q0 +z*(Q1+z*(Q2+z*(Q3+z*(Q4+z*(Q5+z*(Q6+z*(Q7+z*Q8)))))));
2751573Srgrimes	p = P0 + z*(P1 + z*(P2 + z*(P3 + z*P4)));
2761573Srgrimes
2771573Srgrimes	/* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */
2781573Srgrimes	p = p/q;
2791573Srgrimes	t.a = z, TRUNC(t.a);		/* t ~= z + c */
2801573Srgrimes	t.b = (z - t.a) + c;
2811573Srgrimes	t.b *= (t.a + z);
2821573Srgrimes	q = (t.a *= t.a);		/* t = (z+c)^2 */
2831573Srgrimes	TRUNC(t.a);
2841573Srgrimes	t.b += (q - t.a);
2851573Srgrimes	r.a = p, TRUNC(r.a);		/* r = P/Q */
2861573Srgrimes	r.b = p - r.a;
2871573Srgrimes	t.b = t.b*p + t.a*r.b + a0_lo;
2881573Srgrimes	t.a *= r.a;			/* t = (z+c)^2*(P/Q) */
2891573Srgrimes	r.a = t.a + a0_hi, TRUNC(r.a);
2901573Srgrimes	r.b = ((a0_hi-r.a) + t.a) + t.b;
2911573Srgrimes	return (r);			/* r = a0 + t */
2921573Srgrimes}
2931573Srgrimes
2941573Srgrimesstatic double
2951573Srgrimesneg_gam(x)
2961573Srgrimes	double x;
2971573Srgrimes{
2981573Srgrimes	int sgn = 1;
2991573Srgrimes	struct Double lg, lsine;
3001573Srgrimes	double y, z;
3011573Srgrimes
3021573Srgrimes	y = floor(x + .5);
3031573Srgrimes	if (y == x)		/* Negative integer. */
3041573Srgrimes		if(!_IEEE)
3051573Srgrimes			return (infnan(ERANGE));
3061573Srgrimes		else
3071573Srgrimes			return (one/zero);
3081573Srgrimes	z = fabs(x - y);
3091573Srgrimes	y = .5*ceil(x);
3101573Srgrimes	if (y == ceil(y))
3111573Srgrimes		sgn = -1;
3121573Srgrimes	if (z < .25)
3131573Srgrimes		z = sin(M_PI*z);
3141573Srgrimes	else
3151573Srgrimes		z = cos(M_PI*(0.5-z));
3161573Srgrimes	/* Special case: G(1-x) = Inf; G(x) may be nonzero. */
3171573Srgrimes	if (x < -170) {
3181573Srgrimes		if (x < -190)
3191573Srgrimes			return ((double)sgn*tiny*tiny);
3201573Srgrimes		y = one - x;		/* exact: 128 < |x| < 255 */
3211573Srgrimes		lg = large_gam(y);
3221573Srgrimes		lsine = __log__D(M_PI/z);	/* = TRUNC(log(u)) + small */
3231573Srgrimes		lg.a -= lsine.a;		/* exact (opposite signs) */
3241573Srgrimes		lg.b -= lsine.b;
3251573Srgrimes		y = -(lg.a + lg.b);
3261573Srgrimes		z = (y + lg.a) + lg.b;
3271573Srgrimes		y = __exp__D(y, z);
3281573Srgrimes		if (sgn < 0) y = -y;
3291573Srgrimes		return (y);
3301573Srgrimes	}
3311573Srgrimes	y = one-x;
3321573Srgrimes	if (one-y == x)
3331573Srgrimes		y = gamma(y);
3341573Srgrimes	else		/* 1-x is inexact */
3351573Srgrimes		y = -x*gamma(-x);
3361573Srgrimes	if (sgn < 0) y = -y;
3371573Srgrimes	return (M_PI / (y*z));
3381573Srgrimes}
339