]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/ieee754/dbl-64/e_gamma_r.c
b2fec30f622745cb818d4eb01ab96766aaa69bad
1 /* Implementation of gamma function according to ISO C.
2 Copyright (C) 1997-2020 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 <https://www.gnu.org/licenses/>. */
21 #include <math-narrow-eval.h>
22 #include <math_private.h>
23 #include <fenv_private.h>
24 #include <math-underflow.h>
26 #include <libm-alias-finite.h>
28 /* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) inside exp in Stirling's
29 approximation to gamma function. */
31 static const double gamma_coeff
[] =
34 -0xb.60b60b60b60b8p
-12,
35 0x3.4034034034034p
-12,
36 -0x2.7027027027028p
-12,
37 0x3.72a3c5631fe46p
-12,
38 -0x7.daac36664f1f4p
-12,
41 #define NCOEFF (sizeof (gamma_coeff) / sizeof (gamma_coeff[0]))
43 /* Return gamma (X), for positive X less than 184, in the form R *
44 2^(*EXP2_ADJ), where R is the return value and *EXP2_ADJ is set to
45 avoid overflow or underflow in intermediate calculations. */
48 gamma_positive (double x
, int *exp2_adj
)
54 return __ieee754_exp (__ieee754_lgamma_r (x
+ 1, &local_signgam
)) / x
;
59 return __ieee754_exp (__ieee754_lgamma_r (x
, &local_signgam
));
63 /* Adjust into the range for using exp (lgamma). */
65 double n
= ceil (x
- 1.5);
68 double prod
= __gamma_product (x_adj
, 0, n
, &eps
);
69 return (__ieee754_exp (__ieee754_lgamma_r (x_adj
, &local_signgam
))
70 * prod
* (1.0 + eps
));
80 /* Adjust into the range for applying Stirling's
82 double n
= ceil (12.0 - x
);
83 x_adj
= math_narrow_eval (x
+ n
);
84 x_eps
= (x
- (x_adj
- n
));
85 prod
= __gamma_product (x_adj
- n
, x_eps
, n
, &eps
);
87 /* The result is now gamma (X_ADJ + X_EPS) / (PROD * (1 + EPS)).
88 Compute gamma (X_ADJ + X_EPS) using Stirling's approximation,
89 starting by computing pow (X_ADJ, X_ADJ) with a power of 2
91 double exp_adj
= -eps
;
92 double x_adj_int
= round (x_adj
);
93 double x_adj_frac
= x_adj
- x_adj_int
;
95 double x_adj_mant
= __frexp (x_adj
, &x_adj_log2
);
96 if (x_adj_mant
< M_SQRT1_2
)
101 *exp2_adj
= x_adj_log2
* (int) x_adj_int
;
102 double ret
= (__ieee754_pow (x_adj_mant
, x_adj
)
103 * __ieee754_exp2 (x_adj_log2
* x_adj_frac
)
104 * __ieee754_exp (-x_adj
)
105 * sqrt (2 * M_PI
/ x_adj
)
107 exp_adj
+= x_eps
* __ieee754_log (x_adj
);
108 double bsum
= gamma_coeff
[NCOEFF
- 1];
109 double x_adj2
= x_adj
* x_adj
;
110 for (size_t i
= 1; i
<= NCOEFF
- 1; i
++)
111 bsum
= bsum
/ x_adj2
+ gamma_coeff
[NCOEFF
- 1 - i
];
112 exp_adj
+= bsum
/ x_adj
;
113 return ret
+ ret
* __expm1 (exp_adj
);
118 __ieee754_gamma_r (double x
, int *signgamp
)
124 EXTRACT_WORDS (hx
, lx
, x
);
126 if (__glibc_unlikely (((hx
& 0x7fffffff) | lx
) == 0))
128 /* Return value for x == 0 is Inf with divide by zero exception. */
132 if (__builtin_expect (hx
< 0, 0)
133 && (uint32_t) hx
< 0xfff00000 && rint (x
) == x
)
135 /* Return value for integer x < 0 is NaN with invalid exception. */
137 return (x
- x
) / (x
- x
);
139 if (__glibc_unlikely ((unsigned int) hx
== 0xfff00000 && lx
== 0))
141 /* x == -Inf. According to ISO this is NaN. */
145 if (__glibc_unlikely ((hx
& 0x7ff00000) == 0x7ff00000))
147 /* Positive infinity (return positive infinity) or NaN (return
157 ret
= math_narrow_eval (DBL_MAX
* DBL_MAX
);
162 SET_RESTORE_ROUND (FE_TONEAREST
);
167 double tret
= gamma_positive (x
, &exp2_adj
);
168 ret
= __scalbn (tret
, exp2_adj
);
170 else if (x
>= -DBL_EPSILON
/ 4.0)
177 double tx
= trunc (x
);
178 *signgamp
= (tx
== 2.0 * trunc (tx
/ 2.0)) ? -1 : 1;
181 ret
= DBL_MIN
* DBL_MIN
;
184 double frac
= tx
- x
;
187 double sinpix
= (frac
<= 0.25
188 ? __sin (M_PI
* frac
)
189 : __cos (M_PI
* (0.5 - frac
)));
191 double tret
= M_PI
/ (-x
* sinpix
192 * gamma_positive (-x
, &exp2_adj
));
193 ret
= __scalbn (tret
, -exp2_adj
);
194 math_check_force_underflow_nonneg (ret
);
197 ret
= math_narrow_eval (ret
);
199 if (isinf (ret
) && x
!= 0)
203 ret
= math_narrow_eval (-copysign (DBL_MAX
, ret
) * DBL_MAX
);
207 ret
= math_narrow_eval (copysign (DBL_MAX
, ret
) * DBL_MAX
);
214 ret
= math_narrow_eval (-copysign (DBL_MIN
, ret
) * DBL_MIN
);
218 ret
= math_narrow_eval (copysign (DBL_MIN
, ret
) * DBL_MIN
);
224 libm_alias_finite (__ieee754_gamma_r
, __gamma_r
)