| 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. |
| 5 | |
| 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. |
| 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 |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 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/>. */ |
| 19 | |
| 20 | #include <math.h> |
| 21 | #include <math-narrow-eval.h> |
| 22 | #include <math_private.h> |
| 23 | #include <fenv_private.h> |
| 24 | #include <math-underflow.h> |
| 25 | #include <float.h> |
| 26 | #include <libm-alias-finite.h> |
| 27 | |
| 28 | /* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) inside exp in Stirling's |
| 29 | approximation to gamma function. */ |
| 30 | |
| 31 | static const float gamma_coeff[] = |
| 32 | { |
| 33 | 0x1.555556p-4f, |
| 34 | -0xb.60b61p-12f, |
| 35 | 0x3.403404p-12f, |
| 36 | }; |
| 37 | |
| 38 | #define NCOEFF (sizeof (gamma_coeff) / sizeof (gamma_coeff[0])) |
| 39 | |
| 40 | /* Return gamma (X), for positive X less than 42, in the form R * |
| 41 | 2^(*EXP2_ADJ), where R is the return value and *EXP2_ADJ is set to |
| 42 | avoid overflow or underflow in intermediate calculations. */ |
| 43 | |
| 44 | static float |
| 45 | gammaf_positive (float x, int *exp2_adj) |
| 46 | { |
| 47 | int local_signgam; |
| 48 | if (x < 0.5f) |
| 49 | { |
| 50 | *exp2_adj = 0; |
| 51 | return __ieee754_expf (__ieee754_lgammaf_r (x + 1, &local_signgam)) / x; |
| 52 | } |
| 53 | else if (x <= 1.5f) |
| 54 | { |
| 55 | *exp2_adj = 0; |
| 56 | return __ieee754_expf (__ieee754_lgammaf_r (x, &local_signgam)); |
| 57 | } |
| 58 | else if (x < 2.5f) |
| 59 | { |
| 60 | *exp2_adj = 0; |
| 61 | float x_adj = x - 1; |
| 62 | return (__ieee754_expf (__ieee754_lgammaf_r (x_adj, &local_signgam)) |
| 63 | * x_adj); |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | float eps = 0; |
| 68 | float x_eps = 0; |
| 69 | float x_adj = x; |
| 70 | float prod = 1; |
| 71 | if (x < 4.0f) |
| 72 | { |
| 73 | /* Adjust into the range for applying Stirling's |
| 74 | approximation. */ |
| 75 | float n = ceilf (4.0f - x); |
| 76 | x_adj = math_narrow_eval (x + n); |
| 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 | * sqrtf (2 * (float) M_PI / x_adj) |
| 99 | / prod); |
| 100 | exp_adj += x_eps * __ieee754_logf (x_adj); |
| 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 | } |
| 109 | |
| 110 | float |
| 111 | __ieee754_gammaf_r (float x, int *signgamp) |
| 112 | { |
| 113 | int32_t hx; |
| 114 | float ret; |
| 115 | |
| 116 | GET_FLOAT_WORD (hx, x); |
| 117 | |
| 118 | if (__glibc_unlikely ((hx & 0x7fffffff) == 0)) |
| 119 | { |
| 120 | /* Return value for x == 0 is Inf with divide by zero exception. */ |
| 121 | *signgamp = 0; |
| 122 | return 1.0 / x; |
| 123 | } |
| 124 | if (__builtin_expect (hx < 0, 0) |
| 125 | && (uint32_t) hx < 0xff800000 && rintf (x) == x) |
| 126 | { |
| 127 | /* Return value for integer x < 0 is NaN with invalid exception. */ |
| 128 | *signgamp = 0; |
| 129 | return (x - x) / (x - x); |
| 130 | } |
| 131 | if (__glibc_unlikely (hx == 0xff800000)) |
| 132 | { |
| 133 | /* x == -Inf. According to ISO this is NaN. */ |
| 134 | *signgamp = 0; |
| 135 | return x - x; |
| 136 | } |
| 137 | if (__glibc_unlikely ((hx & 0x7f800000) == 0x7f800000)) |
| 138 | { |
| 139 | /* Positive infinity (return positive infinity) or NaN (return |
| 140 | NaN). */ |
| 141 | *signgamp = 0; |
| 142 | return x + x; |
| 143 | } |
| 144 | |
| 145 | if (x >= 36.0f) |
| 146 | { |
| 147 | /* Overflow. */ |
| 148 | *signgamp = 0; |
| 149 | ret = math_narrow_eval (FLT_MAX * FLT_MAX); |
| 150 | return ret; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | SET_RESTORE_ROUNDF (FE_TONEAREST); |
| 155 | if (x > 0.0f) |
| 156 | { |
| 157 | *signgamp = 0; |
| 158 | int exp2_adj; |
| 159 | float tret = gammaf_positive (x, &exp2_adj); |
| 160 | ret = __scalbnf (tret, exp2_adj); |
| 161 | } |
| 162 | else if (x >= -FLT_EPSILON / 4.0f) |
| 163 | { |
| 164 | *signgamp = 0; |
| 165 | ret = 1.0f / x; |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | float tx = truncf (x); |
| 170 | *signgamp = (tx == 2.0f * truncf (tx / 2.0f)) ? -1 : 1; |
| 171 | if (x <= -42.0f) |
| 172 | /* Underflow. */ |
| 173 | ret = FLT_MIN * FLT_MIN; |
| 174 | else |
| 175 | { |
| 176 | float frac = tx - x; |
| 177 | if (frac > 0.5f) |
| 178 | frac = 1.0f - frac; |
| 179 | float sinpix = (frac <= 0.25f |
| 180 | ? __sinf ((float) M_PI * frac) |
| 181 | : __cosf ((float) M_PI * (0.5f - frac))); |
| 182 | int exp2_adj; |
| 183 | float tret = (float) M_PI / (-x * sinpix |
| 184 | * gammaf_positive (-x, &exp2_adj)); |
| 185 | ret = __scalbnf (tret, -exp2_adj); |
| 186 | math_check_force_underflow_nonneg (ret); |
| 187 | } |
| 188 | } |
| 189 | ret = math_narrow_eval (ret); |
| 190 | } |
| 191 | if (isinf (ret) && x != 0) |
| 192 | { |
| 193 | if (*signgamp < 0) |
| 194 | { |
| 195 | ret = math_narrow_eval (-copysignf (FLT_MAX, ret) * FLT_MAX); |
| 196 | ret = -ret; |
| 197 | } |
| 198 | else |
| 199 | ret = math_narrow_eval (copysignf (FLT_MAX, ret) * FLT_MAX); |
| 200 | return ret; |
| 201 | } |
| 202 | else if (ret == 0) |
| 203 | { |
| 204 | if (*signgamp < 0) |
| 205 | { |
| 206 | ret = math_narrow_eval (-copysignf (FLT_MIN, ret) * FLT_MIN); |
| 207 | ret = -ret; |
| 208 | } |
| 209 | else |
| 210 | ret = math_narrow_eval (copysignf (FLT_MIN, ret) * FLT_MIN); |
| 211 | return ret; |
| 212 | } |
| 213 | else |
| 214 | return ret; |
| 215 | } |
| 216 | libm_alias_finite (__ieee754_gammaf_r, __gammaf_r) |
| 217 | |