1 | /* lgammaf expanding around zeros. |
2 | Copyright (C) 2015-2018 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <float.h> |
20 | #include <math.h> |
21 | #include <math-narrow-eval.h> |
22 | #include <math_private.h> |
23 | |
24 | static const float lgamma_zeros[][2] = |
25 | { |
26 | { -0x2.74ff94p+0f, 0x1.3fe0f2p-24f }, |
27 | { -0x2.bf682p+0f, -0x1.437b2p-24f }, |
28 | { -0x3.24c1b8p+0f, 0x6.c34cap-28f }, |
29 | { -0x3.f48e2cp+0f, 0x1.707a04p-24f }, |
30 | { -0x4.0a13ap+0f, 0x1.e99aap-24f }, |
31 | { -0x4.fdd5ep+0f, 0x1.64454p-24f }, |
32 | { -0x5.021a98p+0f, 0x2.03d248p-24f }, |
33 | { -0x5.ffa4cp+0f, 0x2.9b82fcp-24f }, |
34 | { -0x6.005ac8p+0f, -0x1.625f24p-24f }, |
35 | { -0x6.fff3p+0f, 0x2.251e44p-24f }, |
36 | { -0x7.000dp+0f, 0x8.48078p-28f }, |
37 | { -0x7.fffe6p+0f, 0x1.fa98c4p-28f }, |
38 | { -0x8.0001ap+0f, -0x1.459fcap-28f }, |
39 | { -0x8.ffffdp+0f, -0x1.c425e8p-24f }, |
40 | { -0x9.00003p+0f, 0x1.c44b82p-24f }, |
41 | { -0xap+0f, 0x4.9f942p-24f }, |
42 | { -0xap+0f, -0x4.9f93b8p-24f }, |
43 | { -0xbp+0f, 0x6.b9916p-28f }, |
44 | { -0xbp+0f, -0x6.b9915p-28f }, |
45 | { -0xcp+0f, 0x8.f76c8p-32f }, |
46 | { -0xcp+0f, -0x8.f76c7p-32f }, |
47 | { -0xdp+0f, 0xb.09231p-36f }, |
48 | { -0xdp+0f, -0xb.09231p-36f }, |
49 | { -0xep+0f, 0xc.9cba5p-40f }, |
50 | { -0xep+0f, -0xc.9cba5p-40f }, |
51 | { -0xfp+0f, 0xd.73f9fp-44f }, |
52 | }; |
53 | |
54 | static const float e_hi = 0x2.b7e15p+0f, e_lo = 0x1.628aeep-24f; |
55 | |
56 | /* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) in Stirling's |
57 | approximation to lgamma function. */ |
58 | |
59 | static const float lgamma_coeff[] = |
60 | { |
61 | 0x1.555556p-4f, |
62 | -0xb.60b61p-12f, |
63 | 0x3.403404p-12f, |
64 | }; |
65 | |
66 | #define NCOEFF (sizeof (lgamma_coeff) / sizeof (lgamma_coeff[0])) |
67 | |
68 | /* Polynomial approximations to (|gamma(x)|-1)(x-n)/(x-x0), where n is |
69 | the integer end-point of the half-integer interval containing x and |
70 | x0 is the zero of lgamma in that half-integer interval. Each |
71 | polynomial is expressed in terms of x-xm, where xm is the midpoint |
72 | of the interval for which the polynomial applies. */ |
73 | |
74 | static const float poly_coeff[] = |
75 | { |
76 | /* Interval [-2.125, -2] (polynomial degree 5). */ |
77 | -0x1.0b71c6p+0f, |
78 | -0xc.73a1ep-4f, |
79 | -0x1.ec8462p-4f, |
80 | -0xe.37b93p-4f, |
81 | -0x1.02ed36p-4f, |
82 | -0xe.cbe26p-4f, |
83 | /* Interval [-2.25, -2.125] (polynomial degree 5). */ |
84 | -0xf.29309p-4f, |
85 | -0xc.a5cfep-4f, |
86 | 0x3.9c93fcp-4f, |
87 | -0x1.02a2fp+0f, |
88 | 0x9.896bep-4f, |
89 | -0x1.519704p+0f, |
90 | /* Interval [-2.375, -2.25] (polynomial degree 5). */ |
91 | -0xd.7d28dp-4f, |
92 | -0xe.6964cp-4f, |
93 | 0xb.0d4f1p-4f, |
94 | -0x1.9240aep+0f, |
95 | 0x1.dadabap+0f, |
96 | -0x3.1778c4p+0f, |
97 | /* Interval [-2.5, -2.375] (polynomial degree 6). */ |
98 | -0xb.74ea2p-4f, |
99 | -0x1.2a82cp+0f, |
100 | 0x1.880234p+0f, |
101 | -0x3.320c4p+0f, |
102 | 0x5.572a38p+0f, |
103 | -0x9.f92bap+0f, |
104 | 0x1.1c347ep+4f, |
105 | /* Interval [-2.625, -2.5] (polynomial degree 6). */ |
106 | -0x3.d10108p-4f, |
107 | 0x1.cd5584p+0f, |
108 | 0x3.819c24p+0f, |
109 | 0x6.84cbb8p+0f, |
110 | 0xb.bf269p+0f, |
111 | 0x1.57fb12p+4f, |
112 | 0x2.7b9854p+4f, |
113 | /* Interval [-2.75, -2.625] (polynomial degree 6). */ |
114 | -0x6.b5d25p-4f, |
115 | 0x1.28d604p+0f, |
116 | 0x1.db6526p+0f, |
117 | 0x2.e20b38p+0f, |
118 | 0x4.44c378p+0f, |
119 | 0x6.62a08p+0f, |
120 | 0x9.6db3ap+0f, |
121 | /* Interval [-2.875, -2.75] (polynomial degree 5). */ |
122 | -0x8.a41b2p-4f, |
123 | 0xc.da87fp-4f, |
124 | 0x1.147312p+0f, |
125 | 0x1.7617dap+0f, |
126 | 0x1.d6c13p+0f, |
127 | 0x2.57a358p+0f, |
128 | /* Interval [-3, -2.875] (polynomial degree 5). */ |
129 | -0xa.046d6p-4f, |
130 | 0x9.70b89p-4f, |
131 | 0xa.a89a6p-4f, |
132 | 0xd.2f2d8p-4f, |
133 | 0xd.e32b4p-4f, |
134 | 0xf.fb741p-4f, |
135 | }; |
136 | |
137 | static const size_t poly_deg[] = |
138 | { |
139 | 5, |
140 | 5, |
141 | 5, |
142 | 6, |
143 | 6, |
144 | 6, |
145 | 5, |
146 | 5, |
147 | }; |
148 | |
149 | static const size_t poly_end[] = |
150 | { |
151 | 5, |
152 | 11, |
153 | 17, |
154 | 24, |
155 | 31, |
156 | 38, |
157 | 44, |
158 | 50, |
159 | }; |
160 | |
161 | /* Compute sin (pi * X) for -0.25 <= X <= 0.5. */ |
162 | |
163 | static float |
164 | lg_sinpi (float x) |
165 | { |
166 | if (x <= 0.25f) |
167 | return __sinf ((float) M_PI * x); |
168 | else |
169 | return __cosf ((float) M_PI * (0.5f - x)); |
170 | } |
171 | |
172 | /* Compute cos (pi * X) for -0.25 <= X <= 0.5. */ |
173 | |
174 | static float |
175 | lg_cospi (float x) |
176 | { |
177 | if (x <= 0.25f) |
178 | return __cosf ((float) M_PI * x); |
179 | else |
180 | return __sinf ((float) M_PI * (0.5f - x)); |
181 | } |
182 | |
183 | /* Compute cot (pi * X) for -0.25 <= X <= 0.5. */ |
184 | |
185 | static float |
186 | lg_cotpi (float x) |
187 | { |
188 | return lg_cospi (x) / lg_sinpi (x); |
189 | } |
190 | |
191 | /* Compute lgamma of a negative argument -15 < X < -2, setting |
192 | *SIGNGAMP accordingly. */ |
193 | |
194 | float |
195 | __lgamma_negf (float x, int *signgamp) |
196 | { |
197 | /* Determine the half-integer region X lies in, handle exact |
198 | integers and determine the sign of the result. */ |
199 | int i = __floorf (-2 * x); |
200 | if ((i & 1) == 0 && i == -2 * x) |
201 | return 1.0f / 0.0f; |
202 | float xn = ((i & 1) == 0 ? -i / 2 : (-i - 1) / 2); |
203 | i -= 4; |
204 | *signgamp = ((i & 2) == 0 ? -1 : 1); |
205 | |
206 | SET_RESTORE_ROUNDF (FE_TONEAREST); |
207 | |
208 | /* Expand around the zero X0 = X0_HI + X0_LO. */ |
209 | float x0_hi = lgamma_zeros[i][0], x0_lo = lgamma_zeros[i][1]; |
210 | float xdiff = x - x0_hi - x0_lo; |
211 | |
212 | /* For arguments in the range -3 to -2, use polynomial |
213 | approximations to an adjusted version of the gamma function. */ |
214 | if (i < 2) |
215 | { |
216 | int j = __floorf (-8 * x) - 16; |
217 | float xm = (-33 - 2 * j) * 0.0625f; |
218 | float x_adj = x - xm; |
219 | size_t deg = poly_deg[j]; |
220 | size_t end = poly_end[j]; |
221 | float g = poly_coeff[end]; |
222 | for (size_t j = 1; j <= deg; j++) |
223 | g = g * x_adj + poly_coeff[end - j]; |
224 | return __log1pf (g * xdiff / (x - xn)); |
225 | } |
226 | |
227 | /* The result we want is log (sinpi (X0) / sinpi (X)) |
228 | + log (gamma (1 - X0) / gamma (1 - X)). */ |
229 | float x_idiff = fabsf (xn - x), x0_idiff = fabsf (xn - x0_hi - x0_lo); |
230 | float log_sinpi_ratio; |
231 | if (x0_idiff < x_idiff * 0.5f) |
232 | /* Use log not log1p to avoid inaccuracy from log1p of arguments |
233 | close to -1. */ |
234 | log_sinpi_ratio = __ieee754_logf (lg_sinpi (x0_idiff) |
235 | / lg_sinpi (x_idiff)); |
236 | else |
237 | { |
238 | /* Use log1p not log to avoid inaccuracy from log of arguments |
239 | close to 1. X0DIFF2 has positive sign if X0 is further from |
240 | XN than X is from XN, negative sign otherwise. */ |
241 | float x0diff2 = ((i & 1) == 0 ? xdiff : -xdiff) * 0.5f; |
242 | float sx0d2 = lg_sinpi (x0diff2); |
243 | float cx0d2 = lg_cospi (x0diff2); |
244 | log_sinpi_ratio = __log1pf (2 * sx0d2 |
245 | * (-sx0d2 + cx0d2 * lg_cotpi (x_idiff))); |
246 | } |
247 | |
248 | float log_gamma_ratio; |
249 | float y0 = math_narrow_eval (1 - x0_hi); |
250 | float y0_eps = -x0_hi + (1 - y0) - x0_lo; |
251 | float y = math_narrow_eval (1 - x); |
252 | float y_eps = -x + (1 - y); |
253 | /* We now wish to compute LOG_GAMMA_RATIO |
254 | = log (gamma (Y0 + Y0_EPS) / gamma (Y + Y_EPS)). XDIFF |
255 | accurately approximates the difference Y0 + Y0_EPS - Y - |
256 | Y_EPS. Use Stirling's approximation. */ |
257 | float log_gamma_high |
258 | = (xdiff * __log1pf ((y0 - e_hi - e_lo + y0_eps) / e_hi) |
259 | + (y - 0.5f + y_eps) * __log1pf (xdiff / y)); |
260 | /* Compute the sum of (B_2k / 2k(2k-1))(Y0^-(2k-1) - Y^-(2k-1)). */ |
261 | float y0r = 1 / y0, yr = 1 / y; |
262 | float y0r2 = y0r * y0r, yr2 = yr * yr; |
263 | float rdiff = -xdiff / (y * y0); |
264 | float bterm[NCOEFF]; |
265 | float dlast = rdiff, elast = rdiff * yr * (yr + y0r); |
266 | bterm[0] = dlast * lgamma_coeff[0]; |
267 | for (size_t j = 1; j < NCOEFF; j++) |
268 | { |
269 | float dnext = dlast * y0r2 + elast; |
270 | float enext = elast * yr2; |
271 | bterm[j] = dnext * lgamma_coeff[j]; |
272 | dlast = dnext; |
273 | elast = enext; |
274 | } |
275 | float log_gamma_low = 0; |
276 | for (size_t j = 0; j < NCOEFF; j++) |
277 | log_gamma_low += bterm[NCOEFF - 1 - j]; |
278 | log_gamma_ratio = log_gamma_high + log_gamma_low; |
279 | |
280 | return log_sinpi_ratio + log_gamma_ratio; |
281 | } |
282 | |