]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/ieee754/flt-32/e_gammaf_r.c
19f51b0c8b3d864e69422599f31558427147ae40
1 /* Implementation of gamma function according to ISO C.
2 Copyright (C) 1997-2016 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 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
);
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
81 float x_adj_int
= __roundf (x_adj
);
82 float x_adj_frac
= x_adj
- x_adj_int
;
84 float x_adj_mant
= __frexpf (x_adj
, &x_adj_log2
);
85 if (x_adj_mant
< (float) M_SQRT1_2
)
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
)
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
);
107 __ieee754_gammaf_r (float x
, int *signgamp
)
112 GET_FLOAT_WORD (hx
, x
);
114 if (__glibc_unlikely ((hx
& 0x7fffffff) == 0))
116 /* Return value for x == 0 is Inf with divide by zero exception. */
120 if (__builtin_expect (hx
< 0, 0)
121 && (u_int32_t
) hx
< 0xff800000 && __rintf (x
) == x
)
123 /* Return value for integer x < 0 is NaN with invalid exception. */
125 return (x
- x
) / (x
- x
);
127 if (__glibc_unlikely (hx
== 0xff800000))
129 /* x == -Inf. According to ISO this is NaN. */
133 if (__glibc_unlikely ((hx
& 0x7f800000) == 0x7f800000))
135 /* Positive infinity (return positive infinity) or NaN (return
145 ret
= math_narrow_eval (FLT_MAX
* FLT_MAX
);
150 SET_RESTORE_ROUNDF (FE_TONEAREST
);
155 float tret
= gammaf_positive (x
, &exp2_adj
);
156 ret
= __scalbnf (tret
, exp2_adj
);
158 else if (x
>= -FLT_EPSILON
/ 4.0f
)
165 float tx
= __truncf (x
);
166 *signgamp
= (tx
== 2.0f
* __truncf (tx
/ 2.0f
)) ? -1 : 1;
169 ret
= FLT_MIN
* FLT_MIN
;
175 float sinpix
= (frac
<= 0.25f
176 ? __sinf ((float) M_PI
* frac
)
177 : __cosf ((float) M_PI
* (0.5f
- frac
)));
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
);
185 ret
= math_narrow_eval (ret
);
187 if (isinf (ret
) && x
!= 0)
191 ret
= math_narrow_eval (-__copysignf (FLT_MAX
, ret
) * FLT_MAX
);
195 ret
= math_narrow_eval (__copysignf (FLT_MAX
, ret
) * FLT_MAX
);
202 ret
= math_narrow_eval (-__copysignf (FLT_MIN
, ret
) * FLT_MIN
);
206 ret
= math_narrow_eval (__copysignf (FLT_MIN
, ret
) * FLT_MIN
);
212 strong_alias (__ieee754_gammaf_r
, __gammaf_r_finite
)