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 float gamma_coeff[] =
30 {
31 0x1.555556p-4f,
32 -0xb.60b61p-12f,
33 0x3.403404p-12f,
34 };
35
36#define NCOEFF (sizeof (gamma_coeff) / sizeof (gamma_coeff[0]))
37
38/* Return gamma (X), for positive X less than 42, in the form R *
39 2^(*EXP2_ADJ), where R is the return value and *EXP2_ADJ is set to
40 avoid overflow or underflow in intermediate calculations. */
41
42static float
43gammaf_positive (float x, int *exp2_adj)
44{
45 int local_signgam;
46 if (x < 0.5f)
47 {
48 *exp2_adj = 0;
49 return __ieee754_expf (__ieee754_lgammaf_r (x + 1, &local_signgam)) / x;
50 }
51 else if (x <= 1.5f)
52 {
53 *exp2_adj = 0;
54 return __ieee754_expf (__ieee754_lgammaf_r (x, &local_signgam));
55 }
56 else if (x < 2.5f)
57 {
58 *exp2_adj = 0;
59 float x_adj = x - 1;
60 return (__ieee754_expf (__ieee754_lgammaf_r (x_adj, &local_signgam))
61 * x_adj);
62 }
63 else
64 {
65 float eps = 0;
66 float x_eps = 0;
67 float x_adj = x;
68 float prod = 1;
69 if (x < 4.0f)
70 {
71 /* Adjust into the range for applying Stirling's
72 approximation. */
73 float n = __ceilf (4.0f - x);
74 x_adj = math_narrow_eval (x + n);
75 x_eps = (x - (x_adj - n));
76 prod = __gamma_productf (x_adj - n, x_eps, n, &eps);
77 }
78 /* The result is now gamma (X_ADJ + X_EPS) / (PROD * (1 + EPS)).
79 Compute gamma (X_ADJ + X_EPS) using Stirling's approximation,
80 starting by computing pow (X_ADJ, X_ADJ) with a power of 2
81 factored out. */
82 float exp_adj = -eps;
83 float x_adj_int = __roundf (x_adj);
84 float x_adj_frac = x_adj - x_adj_int;
85 int x_adj_log2;
86 float x_adj_mant = __frexpf (x_adj, &x_adj_log2);
87 if (x_adj_mant < (float) M_SQRT1_2)
88 {
89 x_adj_log2--;
90 x_adj_mant *= 2.0f;
91 }
92 *exp2_adj = x_adj_log2 * (int) x_adj_int;
93 float ret = (__ieee754_powf (x_adj_mant, x_adj)
94 * __ieee754_exp2f (x_adj_log2 * x_adj_frac)
95 * __ieee754_expf (-x_adj)
96 * sqrtf (2 * (float) M_PI / x_adj)
97 / prod);
98 exp_adj += x_eps * __ieee754_logf (x_adj);
99 float bsum = gamma_coeff[NCOEFF - 1];
100 float x_adj2 = x_adj * x_adj;
101 for (size_t i = 1; i <= NCOEFF - 1; i++)
102 bsum = bsum / x_adj2 + gamma_coeff[NCOEFF - 1 - i];
103 exp_adj += bsum / x_adj;
104 return ret + ret * __expm1f (exp_adj);
105 }
106}
107
108float
109__ieee754_gammaf_r (float x, int *signgamp)
110{
111 int32_t hx;
112 float ret;
113
114 GET_FLOAT_WORD (hx, x);
115
116 if (__glibc_unlikely ((hx & 0x7fffffff) == 0))
117 {
118 /* Return value for x == 0 is Inf with divide by zero exception. */
119 *signgamp = 0;
120 return 1.0 / x;
121 }
122 if (__builtin_expect (hx < 0, 0)
123 && (uint32_t) hx < 0xff800000 && __rintf (x) == x)
124 {
125 /* Return value for integer x < 0 is NaN with invalid exception. */
126 *signgamp = 0;
127 return (x - x) / (x - x);
128 }
129 if (__glibc_unlikely (hx == 0xff800000))
130 {
131 /* x == -Inf. According to ISO this is NaN. */
132 *signgamp = 0;
133 return x - x;
134 }
135 if (__glibc_unlikely ((hx & 0x7f800000) == 0x7f800000))
136 {
137 /* Positive infinity (return positive infinity) or NaN (return
138 NaN). */
139 *signgamp = 0;
140 return x + x;
141 }
142
143 if (x >= 36.0f)
144 {
145 /* Overflow. */
146 *signgamp = 0;
147 ret = math_narrow_eval (FLT_MAX * FLT_MAX);
148 return ret;
149 }
150 else
151 {
152 SET_RESTORE_ROUNDF (FE_TONEAREST);
153 if (x > 0.0f)
154 {
155 *signgamp = 0;
156 int exp2_adj;
157 float tret = gammaf_positive (x, &exp2_adj);
158 ret = __scalbnf (tret, exp2_adj);
159 }
160 else if (x >= -FLT_EPSILON / 4.0f)
161 {
162 *signgamp = 0;
163 ret = 1.0f / x;
164 }
165 else
166 {
167 float tx = __truncf (x);
168 *signgamp = (tx == 2.0f * __truncf (tx / 2.0f)) ? -1 : 1;
169 if (x <= -42.0f)
170 /* Underflow. */
171 ret = FLT_MIN * FLT_MIN;
172 else
173 {
174 float frac = tx - x;
175 if (frac > 0.5f)
176 frac = 1.0f - frac;
177 float sinpix = (frac <= 0.25f
178 ? __sinf ((float) M_PI * frac)
179 : __cosf ((float) M_PI * (0.5f - frac)));
180 int exp2_adj;
181 float tret = (float) M_PI / (-x * sinpix
182 * gammaf_positive (-x, &exp2_adj));
183 ret = __scalbnf (tret, -exp2_adj);
184 math_check_force_underflow_nonneg (ret);
185 }
186 }
187 ret = math_narrow_eval (ret);
188 }
189 if (isinf (ret) && x != 0)
190 {
191 if (*signgamp < 0)
192 {
193 ret = math_narrow_eval (-__copysignf (FLT_MAX, ret) * FLT_MAX);
194 ret = -ret;
195 }
196 else
197 ret = math_narrow_eval (__copysignf (FLT_MAX, ret) * FLT_MAX);
198 return ret;
199 }
200 else if (ret == 0)
201 {
202 if (*signgamp < 0)
203 {
204 ret = math_narrow_eval (-__copysignf (FLT_MIN, ret) * FLT_MIN);
205 ret = -ret;
206 }
207 else
208 ret = math_narrow_eval (__copysignf (FLT_MIN, ret) * FLT_MIN);
209 return ret;
210 }
211 else
212 return ret;
213}
214strong_alias (__ieee754_gammaf_r, __gammaf_r_finite)
215