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-narrow-eval.h>
22#include <math_private.h>
23#include <math-underflow.h>
24#include <float.h>
25
26/* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) inside exp in Stirling's
27 approximation to gamma function. */
28
29static const double gamma_coeff[] =
30 {
31 0x1.5555555555555p-4,
32 -0xb.60b60b60b60b8p-12,
33 0x3.4034034034034p-12,
34 -0x2.7027027027028p-12,
35 0x3.72a3c5631fe46p-12,
36 -0x7.daac36664f1f4p-12,
37 };
38
39#define NCOEFF (sizeof (gamma_coeff) / sizeof (gamma_coeff[0]))
40
41/* Return gamma (X), for positive X less than 184, in the form R *
42 2^(*EXP2_ADJ), where R is the return value and *EXP2_ADJ is set to
43 avoid overflow or underflow in intermediate calculations. */
44
45static double
46gamma_positive (double x, int *exp2_adj)
47{
48 int local_signgam;
49 if (x < 0.5)
50 {
51 *exp2_adj = 0;
52 return __ieee754_exp (__ieee754_lgamma_r (x + 1, &local_signgam)) / x;
53 }
54 else if (x <= 1.5)
55 {
56 *exp2_adj = 0;
57 return __ieee754_exp (__ieee754_lgamma_r (x, &local_signgam));
58 }
59 else if (x < 6.5)
60 {
61 /* Adjust into the range for using exp (lgamma). */
62 *exp2_adj = 0;
63 double n = __ceil (x - 1.5);
64 double x_adj = x - n;
65 double eps;
66 double prod = __gamma_product (x_adj, 0, n, &eps);
67 return (__ieee754_exp (__ieee754_lgamma_r (x_adj, &local_signgam))
68 * prod * (1.0 + eps));
69 }
70 else
71 {
72 double eps = 0;
73 double x_eps = 0;
74 double x_adj = x;
75 double prod = 1;
76 if (x < 12.0)
77 {
78 /* Adjust into the range for applying Stirling's
79 approximation. */
80 double n = __ceil (12.0 - x);
81 x_adj = math_narrow_eval (x + n);
82 x_eps = (x - (x_adj - n));
83 prod = __gamma_product (x_adj - n, x_eps, n, &eps);
84 }
85 /* The result is now gamma (X_ADJ + X_EPS) / (PROD * (1 + EPS)).
86 Compute gamma (X_ADJ + X_EPS) using Stirling's approximation,
87 starting by computing pow (X_ADJ, X_ADJ) with a power of 2
88 factored out. */
89 double exp_adj = -eps;
90 double x_adj_int = __round (x_adj);
91 double x_adj_frac = x_adj - x_adj_int;
92 int x_adj_log2;
93 double x_adj_mant = __frexp (x_adj, &x_adj_log2);
94 if (x_adj_mant < M_SQRT1_2)
95 {
96 x_adj_log2--;
97 x_adj_mant *= 2.0;
98 }
99 *exp2_adj = x_adj_log2 * (int) x_adj_int;
100 double ret = (__ieee754_pow (x_adj_mant, x_adj)
101 * __ieee754_exp2 (x_adj_log2 * x_adj_frac)
102 * __ieee754_exp (-x_adj)
103 * sqrt (2 * M_PI / x_adj)
104 / prod);
105 exp_adj += x_eps * __ieee754_log (x_adj);
106 double bsum = gamma_coeff[NCOEFF - 1];
107 double x_adj2 = x_adj * x_adj;
108 for (size_t i = 1; i <= NCOEFF - 1; i++)
109 bsum = bsum / x_adj2 + gamma_coeff[NCOEFF - 1 - i];
110 exp_adj += bsum / x_adj;
111 return ret + ret * __expm1 (exp_adj);
112 }
113}
114
115double
116__ieee754_gamma_r (double x, int *signgamp)
117{
118 int32_t hx;
119 uint32_t lx;
120 double ret;
121
122 EXTRACT_WORDS (hx, lx, x);
123
124 if (__glibc_unlikely (((hx & 0x7fffffff) | lx) == 0))
125 {
126 /* Return value for x == 0 is Inf with divide by zero exception. */
127 *signgamp = 0;
128 return 1.0 / x;
129 }
130 if (__builtin_expect (hx < 0, 0)
131 && (uint32_t) hx < 0xfff00000 && __rint (x) == x)
132 {
133 /* Return value for integer x < 0 is NaN with invalid exception. */
134 *signgamp = 0;
135 return (x - x) / (x - x);
136 }
137 if (__glibc_unlikely ((unsigned int) hx == 0xfff00000 && lx == 0))
138 {
139 /* x == -Inf. According to ISO this is NaN. */
140 *signgamp = 0;
141 return x - x;
142 }
143 if (__glibc_unlikely ((hx & 0x7ff00000) == 0x7ff00000))
144 {
145 /* Positive infinity (return positive infinity) or NaN (return
146 NaN). */
147 *signgamp = 0;
148 return x + x;
149 }
150
151 if (x >= 172.0)
152 {
153 /* Overflow. */
154 *signgamp = 0;
155 ret = math_narrow_eval (DBL_MAX * DBL_MAX);
156 return ret;
157 }
158 else
159 {
160 SET_RESTORE_ROUND (FE_TONEAREST);
161 if (x > 0.0)
162 {
163 *signgamp = 0;
164 int exp2_adj;
165 double tret = gamma_positive (x, &exp2_adj);
166 ret = __scalbn (tret, exp2_adj);
167 }
168 else if (x >= -DBL_EPSILON / 4.0)
169 {
170 *signgamp = 0;
171 ret = 1.0 / x;
172 }
173 else
174 {
175 double tx = __trunc (x);
176 *signgamp = (tx == 2.0 * __trunc (tx / 2.0)) ? -1 : 1;
177 if (x <= -184.0)
178 /* Underflow. */
179 ret = DBL_MIN * DBL_MIN;
180 else
181 {
182 double frac = tx - x;
183 if (frac > 0.5)
184 frac = 1.0 - frac;
185 double sinpix = (frac <= 0.25
186 ? __sin (M_PI * frac)
187 : __cos (M_PI * (0.5 - frac)));
188 int exp2_adj;
189 double tret = M_PI / (-x * sinpix
190 * gamma_positive (-x, &exp2_adj));
191 ret = __scalbn (tret, -exp2_adj);
192 math_check_force_underflow_nonneg (ret);
193 }
194 }
195 ret = math_narrow_eval (ret);
196 }
197 if (isinf (ret) && x != 0)
198 {
199 if (*signgamp < 0)
200 {
201 ret = math_narrow_eval (-__copysign (DBL_MAX, ret) * DBL_MAX);
202 ret = -ret;
203 }
204 else
205 ret = math_narrow_eval (__copysign (DBL_MAX, ret) * DBL_MAX);
206 return ret;
207 }
208 else if (ret == 0)
209 {
210 if (*signgamp < 0)
211 {
212 ret = math_narrow_eval (-__copysign (DBL_MIN, ret) * DBL_MIN);
213 ret = -ret;
214 }
215 else
216 ret = math_narrow_eval (__copysign (DBL_MIN, ret) * DBL_MIN);
217 return ret;
218 }
219 else
220 return ret;
221}
222strong_alias (__ieee754_gamma_r, __gamma_r_finite)
223