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/>. */
21 #include <math_private.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
[] =
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. */
41 gammaf_positive (float x
, int *exp2_adj
)
47 return __ieee754_expf (__ieee754_lgammaf_r (x
+ 1, &local_signgam
)) / x
;
52 return __ieee754_expf (__ieee754_lgammaf_r (x
, &local_signgam
));
58 return (__ieee754_expf (__ieee754_lgammaf_r (x_adj
, &local_signgam
))
69 /* Adjust into the range for applying Stirling's
71 float n
= __ceilf (4.0f
- x
);
72 #if FLT_EVAL_METHOD != 0
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
85 float x_adj_int
= __roundf (x_adj
);
86 float x_adj_frac
= x_adj
- x_adj_int
;
88 float x_adj_mant
= __frexpf (x_adj
, &x_adj_log2
);
89 if (x_adj_mant
< (float) M_SQRT1_2
)
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
)
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
);
111 __ieee754_gammaf_r (float x
, int *signgamp
)
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. */
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. */
128 return (x
- x
) / (x
- x
);
130 if (__builtin_expect (hx
== 0xff800000, 0))
132 /* x == -Inf. According to ISO this is NaN. */
136 if (__builtin_expect ((hx
& 0x7f800000) == 0x7f800000, 0))
138 /* Positive infinity (return positive infinity) or NaN (return
148 return FLT_MAX
* FLT_MAX
;
154 float ret
= gammaf_positive (x
, &exp2_adj
);
155 return __scalbnf (ret
, exp2_adj
);
157 else if (x
>= -FLT_EPSILON
/ 4.0f
)
164 float tx
= __truncf (x
);
165 *signgamp
= (tx
== 2.0f
* __truncf (tx
/ 2.0f
)) ? -1 : 1;
168 return FLT_MIN
* FLT_MIN
;
172 float sinpix
= (frac
<= 0.25f
173 ? __sinf ((float) M_PI
* frac
)
174 : __cosf ((float) M_PI
* (0.5f
- frac
)));
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
)