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