]>
Commit | Line | Data |
---|---|---|
d705269e | 1 | /* Implementation of gamma function according to ISO C. |
d4697bc9 | 2 | Copyright (C) 1997-2014 Free Software Foundation, Inc. |
c131718c UD |
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 | |
41bdb6e2 AJ |
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. | |
c131718c UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
c131718c | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
c131718c | 19 | |
d705269e UD |
20 | #include <math.h> |
21 | #include <math_private.h> | |
d8cd06db | 22 | #include <float.h> |
d705269e | 23 | |
d8cd06db JM |
24 | /* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) inside exp in Stirling's |
25 | approximation to gamma function. */ | |
26 | ||
27 | static 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 | ||
40 | static float | |
41 | gammaf_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 | #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); | |
79 | } | |
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) | |
90 | { | |
91 | x_adj_log2--; | |
92 | x_adj_mant *= 2.0f; | |
93 | } | |
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); | |
107 | } | |
108 | } | |
d705269e UD |
109 | |
110 | float | |
111 | __ieee754_gammaf_r (float x, int *signgamp) | |
112 | { | |
d705269e UD |
113 | int32_t hx; |
114 | ||
115 | GET_FLOAT_WORD (hx, x); | |
116 | ||
a1ffb40e | 117 | if (__glibc_unlikely ((hx & 0x7fffffff) == 0)) |
b3fc5f84 | 118 | { |
52495f29 | 119 | /* Return value for x == 0 is Inf with divide by zero exception. */ |
b3fc5f84 | 120 | *signgamp = 0; |
52495f29 | 121 | return 1.0 / x; |
b3fc5f84 | 122 | } |
0ac5ae23 UD |
123 | if (__builtin_expect (hx < 0, 0) |
124 | && (u_int32_t) hx < 0xff800000 && __rintf (x) == x) | |
d705269e UD |
125 | { |
126 | /* Return value for integer x < 0 is NaN with invalid exception. */ | |
b3fc5f84 | 127 | *signgamp = 0; |
d705269e UD |
128 | return (x - x) / (x - x); |
129 | } | |
a1ffb40e | 130 | if (__glibc_unlikely (hx == 0xff800000)) |
3bde1a69 UD |
131 | { |
132 | /* x == -Inf. According to ISO this is NaN. */ | |
133 | *signgamp = 0; | |
134 | return x - x; | |
135 | } | |
a1ffb40e | 136 | if (__glibc_unlikely ((hx & 0x7f800000) == 0x7f800000)) |
d8cd06db JM |
137 | { |
138 | /* Positive infinity (return positive infinity) or NaN (return | |
139 | NaN). */ | |
140 | *signgamp = 0; | |
141 | return x + x; | |
142 | } | |
d705269e | 143 | |
d8cd06db JM |
144 | if (x >= 36.0f) |
145 | { | |
146 | /* Overflow. */ | |
147 | *signgamp = 0; | |
148 | return FLT_MAX * FLT_MAX; | |
149 | } | |
150 | else if (x > 0.0f) | |
151 | { | |
152 | *signgamp = 0; | |
153 | int exp2_adj; | |
154 | float ret = gammaf_positive (x, &exp2_adj); | |
155 | return __scalbnf (ret, exp2_adj); | |
156 | } | |
157 | else if (x >= -FLT_EPSILON / 4.0f) | |
158 | { | |
159 | *signgamp = 0; | |
160 | return 1.0f / x; | |
161 | } | |
162 | else | |
163 | { | |
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); | |
179 | } | |
d705269e | 180 | } |
0ac5ae23 | 181 | strong_alias (__ieee754_gammaf_r, __gammaf_r_finite) |