Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / ieee754 / flt-32 / e_gammaf_r.c
blobe8da51a42cb142442b909df665f3a74a2f9988af
1 /* Implementation of gamma function according to ISO C.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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.
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.
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/>. */
20 #include <math.h>
21 #include <math_private.h>
22 #include <float.h>
24 /* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) inside exp in Stirling's
25 approximation to gamma function. */
27 static const float gamma_coeff[] =
29 0x1.555556p-4f,
30 -0xb.60b61p-12f,
31 0x3.403404p-12f,
34 #define NCOEFF (sizeof (gamma_coeff) / sizeof (gamma_coeff[0]))
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. */
40 static float
41 gammaf_positive (float x, int *exp2_adj)
43 int local_signgam;
44 if (x < 0.5f)
46 *exp2_adj = 0;
47 return __ieee754_expf (__ieee754_lgammaf_r (x + 1, &local_signgam)) / x;
49 else if (x <= 1.5f)
51 *exp2_adj = 0;
52 return __ieee754_expf (__ieee754_lgammaf_r (x, &local_signgam));
54 else if (x < 2.5f)
56 *exp2_adj = 0;
57 float x_adj = x - 1;
58 return (__ieee754_expf (__ieee754_lgammaf_r (x_adj, &local_signgam))
59 * x_adj);
61 else
63 float eps = 0;
64 float x_eps = 0;
65 float x_adj = x;
66 float prod = 1;
67 if (x < 4.0f)
69 /* Adjust into the range for applying Stirling's
70 approximation. */
71 float n = __ceilf (4.0f - x);
72 #if FLT_EVAL_METHOD != 0
73 volatile
74 #endif
75 float x_tmp = x + n;
76 x_adj = x_tmp;
77 x_eps = (x - (x_adj - n));
78 prod = __gamma_productf (x_adj - n, x_eps, n, &eps);
80 /* The result is now gamma (X_ADJ + X_EPS) / (PROD * (1 + EPS)).
81 Compute gamma (X_ADJ + X_EPS) using Stirling's approximation,
82 starting by computing pow (X_ADJ, X_ADJ) with a power of 2
83 factored out. */
84 float exp_adj = -eps;
85 float x_adj_int = __roundf (x_adj);
86 float x_adj_frac = x_adj - x_adj_int;
87 int x_adj_log2;
88 float x_adj_mant = __frexpf (x_adj, &x_adj_log2);
89 if (x_adj_mant < (float) M_SQRT1_2)
91 x_adj_log2--;
92 x_adj_mant *= 2.0f;
94 *exp2_adj = x_adj_log2 * (int) x_adj_int;
95 float ret = (__ieee754_powf (x_adj_mant, x_adj)
96 * __ieee754_exp2f (x_adj_log2 * x_adj_frac)
97 * __ieee754_expf (-x_adj)
98 * __ieee754_sqrtf (2 * (float) M_PI / x_adj)
99 / prod);
100 exp_adj += x_eps * __ieee754_logf (x);
101 float bsum = gamma_coeff[NCOEFF - 1];
102 float x_adj2 = x_adj * x_adj;
103 for (size_t i = 1; i <= NCOEFF - 1; i++)
104 bsum = bsum / x_adj2 + gamma_coeff[NCOEFF - 1 - i];
105 exp_adj += bsum / x_adj;
106 return ret + ret * __expm1f (exp_adj);
110 float
111 __ieee754_gammaf_r (float x, int *signgamp)
113 int32_t hx;
115 GET_FLOAT_WORD (hx, x);
117 if (__builtin_expect ((hx & 0x7fffffff) == 0, 0))
119 /* Return value for x == 0 is Inf with divide by zero exception. */
120 *signgamp = 0;
121 return 1.0 / x;
123 if (__builtin_expect (hx < 0, 0)
124 && (u_int32_t) hx < 0xff800000 && __rintf (x) == x)
126 /* Return value for integer x < 0 is NaN with invalid exception. */
127 *signgamp = 0;
128 return (x - x) / (x - x);
130 if (__builtin_expect (hx == 0xff800000, 0))
132 /* x == -Inf. According to ISO this is NaN. */
133 *signgamp = 0;
134 return x - x;
136 if (__builtin_expect ((hx & 0x7f800000) == 0x7f800000, 0))
138 /* Positive infinity (return positive infinity) or NaN (return
139 NaN). */
140 *signgamp = 0;
141 return x + x;
144 if (x >= 36.0f)
146 /* Overflow. */
147 *signgamp = 0;
148 return FLT_MAX * FLT_MAX;
150 else if (x > 0.0f)
152 *signgamp = 0;
153 int exp2_adj;
154 float ret = gammaf_positive (x, &exp2_adj);
155 return __scalbnf (ret, exp2_adj);
157 else if (x >= -FLT_EPSILON / 4.0f)
159 *signgamp = 0;
160 return 1.0f / x;
162 else
164 float tx = __truncf (x);
165 *signgamp = (tx == 2.0f * __truncf (tx / 2.0f)) ? -1 : 1;
166 if (x <= -42.0f)
167 /* Underflow. */
168 return FLT_MIN * FLT_MIN;
169 float frac = tx - x;
170 if (frac > 0.5f)
171 frac = 1.0f - frac;
172 float sinpix = (frac <= 0.25f
173 ? __sinf ((float) M_PI * frac)
174 : __cosf ((float) M_PI * (0.5f - frac)));
175 int exp2_adj;
176 float ret = (float) M_PI / (-x * sinpix
177 * gammaf_positive (-x, &exp2_adj));
178 return __scalbnf (ret, -exp2_adj);
181 strong_alias (__ieee754_gammaf_r, __gammaf_r_finite)