1/* Implementation of gamma function according to ISO C.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <math.h>
21#include <math_private.h>
22#include <float.h>
23
24/* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) inside exp in Stirling's
25 approximation to gamma function. */
26
27static const double gamma_coeff[] =
28 {
29 0x1.5555555555555p-4,
30 -0xb.60b60b60b60b8p-12,
31 0x3.4034034034034p-12,
32 -0x2.7027027027028p-12,
33 0x3.72a3c5631fe46p-12,
34 -0x7.daac36664f1f4p-12,
35 };
36
37#define NCOEFF (sizeof (gamma_coeff) / sizeof (gamma_coeff[0]))
38
39/* Return gamma (X), for positive X less than 184, in the form R *
40 2^(*EXP2_ADJ), where R is the return value and *EXP2_ADJ is set to
41 avoid overflow or underflow in intermediate calculations. */
42
43static double
44gamma_positive (double x, int *exp2_adj)
45{
46 int local_signgam;
47 if (x < 0.5)
48 {
49 *exp2_adj = 0;
50 return __ieee754_exp (__ieee754_lgamma_r (x + 1, &local_signgam)) / x;
51 }
52 else if (x <= 1.5)
53 {
54 *exp2_adj = 0;
55 return __ieee754_exp (__ieee754_lgamma_r (x, &local_signgam));
56 }
57 else if (x < 6.5)
58 {
59 /* Adjust into the range for using exp (lgamma). */
60 *exp2_adj = 0;
61 double n = __ceil (x - 1.5);
62 double x_adj = x - n;
63 double eps;
64 double prod = __gamma_product (x_adj, 0, n, &eps);
65 return (__ieee754_exp (__ieee754_lgamma_r (x_adj, &local_signgam))
66 * prod * (1.0 + eps));
67 }
68 else
69 {
70 double eps = 0;
71 double x_eps = 0;
72 double x_adj = x;
73 double prod = 1;
74 if (x < 12.0)
75 {
76 /* Adjust into the range for applying Stirling's
77 approximation. */
78 double n = __ceil (12.0 - x);
79 x_adj = math_narrow_eval (x + n);
80 x_eps = (x - (x_adj - n));
81 prod = __gamma_product (x_adj - n, x_eps, n, &eps);
82 }
83 /* The result is now gamma (X_ADJ + X_EPS) / (PROD * (1 + EPS)).
84 Compute gamma (X_ADJ + X_EPS) using Stirling's approximation,
85 starting by computing pow (X_ADJ, X_ADJ) with a power of 2
86 factored out. */
87 double exp_adj = -eps;
88 double x_adj_int = __round (x_adj);
89 double x_adj_frac = x_adj - x_adj_int;
90 int x_adj_log2;
91 double x_adj_mant = __frexp (x_adj, &x_adj_log2);
92 if (x_adj_mant < M_SQRT1_2)
93 {
94 x_adj_log2--;
95 x_adj_mant *= 2.0;
96 }
97 *exp2_adj = x_adj_log2 * (int) x_adj_int;
98 double ret = (__ieee754_pow (x_adj_mant, x_adj)
99 * __ieee754_exp2 (x_adj_log2 * x_adj_frac)
100 * __ieee754_exp (-x_adj)
101 * __ieee754_sqrt (2 * M_PI / x_adj)
102 / prod);
103 exp_adj += x_eps * __ieee754_log (x_adj);
104 double bsum = gamma_coeff[NCOEFF - 1];
105 double x_adj2 = x_adj * x_adj;
106 for (size_t i = 1; i <= NCOEFF - 1; i++)
107 bsum = bsum / x_adj2 + gamma_coeff[NCOEFF - 1 - i];
108 exp_adj += bsum / x_adj;
109 return ret + ret * __expm1 (exp_adj);
110 }
111}
112
113double
114__ieee754_gamma_r (double x, int *signgamp)
115{
116 int32_t hx;
117 uint32_t lx;
118 double ret;
119
120 EXTRACT_WORDS (hx, lx, x);
121
122 if (__glibc_unlikely (((hx & 0x7fffffff) | lx) == 0))
123 {
124 /* Return value for x == 0 is Inf with divide by zero exception. */
125 *signgamp = 0;
126 return 1.0 / x;
127 }
128 if (__builtin_expect (hx < 0, 0)
129 && (uint32_t) hx < 0xfff00000 && __rint (x) == x)
130 {
131 /* Return value for integer x < 0 is NaN with invalid exception. */
132 *signgamp = 0;
133 return (x - x) / (x - x);
134 }
135 if (__glibc_unlikely ((unsigned int) hx == 0xfff00000 && lx == 0))
136 {
137 /* x == -Inf. According to ISO this is NaN. */
138 *signgamp = 0;
139 return x - x;
140 }
141 if (__glibc_unlikely ((hx & 0x7ff00000) == 0x7ff00000))
142 {
143 /* Positive infinity (return positive infinity) or NaN (return
144 NaN). */
145 *signgamp = 0;
146 return x + x;
147 }
148
149 if (x >= 172.0)
150 {
151 /* Overflow. */
152 *signgamp = 0;
153 ret = math_narrow_eval (DBL_MAX * DBL_MAX);
154 return ret;
155 }
156 else
157 {
158 SET_RESTORE_ROUND (FE_TONEAREST);
159 if (x > 0.0)
160 {
161 *signgamp = 0;
162 int exp2_adj;
163 double tret = gamma_positive (x, &exp2_adj);
164 ret = __scalbn (tret, exp2_adj);
165 }
166 else if (x >= -DBL_EPSILON / 4.0)
167 {
168 *signgamp = 0;
169 ret = 1.0 / x;
170 }
171 else
172 {
173 double tx = __trunc (x);
174 *signgamp = (tx == 2.0 * __trunc (tx / 2.0)) ? -1 : 1;
175 if (x <= -184.0)
176 /* Underflow. */
177 ret = DBL_MIN * DBL_MIN;
178 else
179 {
180 double frac = tx - x;
181 if (frac > 0.5)
182 frac = 1.0 - frac;
183 double sinpix = (frac <= 0.25
184 ? __sin (M_PI * frac)
185 : __cos (M_PI * (0.5 - frac)));
186 int exp2_adj;
187 double tret = M_PI / (-x * sinpix
188 * gamma_positive (-x, &exp2_adj));
189 ret = __scalbn (tret, -exp2_adj);
190 math_check_force_underflow_nonneg (ret);
191 }
192 }
193 ret = math_narrow_eval (ret);
194 }
195 if (isinf (ret) && x != 0)
196 {
197 if (*signgamp < 0)
198 {
199 ret = math_narrow_eval (-__copysign (DBL_MAX, ret) * DBL_MAX);
200 ret = -ret;
201 }
202 else
203 ret = math_narrow_eval (__copysign (DBL_MAX, ret) * DBL_MAX);
204 return ret;
205 }
206 else if (ret == 0)
207 {
208 if (*signgamp < 0)
209 {
210 ret = math_narrow_eval (-__copysign (DBL_MIN, ret) * DBL_MIN);
211 ret = -ret;
212 }
213 else
214 ret = math_narrow_eval (__copysign (DBL_MIN, ret) * DBL_MIN);
215 return ret;
216 }
217 else
218 return ret;
219}
220strong_alias (__ieee754_gamma_r, __gamma_r_finite)
221