msvcrt: Use the lgamma()/lgammaf() implementation from the bundled musl library.

This commit is contained in:
Alexandre Julliard 2023-04-03 17:20:19 +02:00
parent f27f2d4e66
commit 442052fe18
3 changed files with 8 additions and 382 deletions

View file

@ -576,6 +576,7 @@ static float __expm1f(float x)
return y;
}
#ifndef __i386__
/* Copied from musl: src/math/__sindf.c */
static float __sindf(double x)
{
@ -611,6 +612,7 @@ static float __cosdf(double x)
return 1 + C0 * z;
return 1.0 + z * (C0 + z * (C1 + z * (C2 + z * (C3 + z * C4))));
}
#endif
static const UINT64 exp2f_T[] = {
0x3ff0000000000000ULL, 0x3fefd9b0d3158574ULL, 0x3fefb5586cf9890fULL, 0x3fef9301d0125b51ULL,
@ -9016,386 +9018,6 @@ static double sin_pi(double x)
}
}
/*********************************************************************
* lgamma (MSVCR120.@)
*
* Copied from musl: src/math/lgamma_r.c
*/
double CDECL lgamma(double x)
{
static const double pi = 3.14159265358979311600e+00,
a0 = 7.72156649015328655494e-02,
a1 = 3.22467033424113591611e-01,
a2 = 6.73523010531292681824e-02,
a3 = 2.05808084325167332806e-02,
a4 = 7.38555086081402883957e-03,
a5 = 2.89051383673415629091e-03,
a6 = 1.19270763183362067845e-03,
a7 = 5.10069792153511336608e-04,
a8 = 2.20862790713908385557e-04,
a9 = 1.08011567247583939954e-04,
a10 = 2.52144565451257326939e-05,
a11 = 4.48640949618915160150e-05,
tc = 1.46163214496836224576e+00,
tf = -1.21486290535849611461e-01,
tt = -3.63867699703950536541e-18,
t0 = 4.83836122723810047042e-01,
t1 = -1.47587722994593911752e-01,
t2 = 6.46249402391333854778e-02,
t3 = -3.27885410759859649565e-02,
t4 = 1.79706750811820387126e-02,
t5 = -1.03142241298341437450e-02,
t6 = 6.10053870246291332635e-03,
t7 = -3.68452016781138256760e-03,
t8 = 2.25964780900612472250e-03,
t9 = -1.40346469989232843813e-03,
t10 = 8.81081882437654011382e-04,
t11 = -5.38595305356740546715e-04,
t12 = 3.15632070903625950361e-04,
t13 = -3.12754168375120860518e-04,
t14 = 3.35529192635519073543e-04,
u0 = -7.72156649015328655494e-02,
u1 = 6.32827064025093366517e-01,
u2 = 1.45492250137234768737e+00,
u3 = 9.77717527963372745603e-01,
u4 = 2.28963728064692451092e-01,
u5 = 1.33810918536787660377e-02,
v1 = 2.45597793713041134822e+00,
v2 = 2.12848976379893395361e+00,
v3 = 7.69285150456672783825e-01,
v4 = 1.04222645593369134254e-01,
v5 = 3.21709242282423911810e-03,
s0 = -7.72156649015328655494e-02,
s1 = 2.14982415960608852501e-01,
s2 = 3.25778796408930981787e-01,
s3 = 1.46350472652464452805e-01,
s4 = 2.66422703033638609560e-02,
s5 = 1.84028451407337715652e-03,
s6 = 3.19475326584100867617e-05,
r1 = 1.39200533467621045958e+00,
r2 = 7.21935547567138069525e-01,
r3 = 1.71933865632803078993e-01,
r4 = 1.86459191715652901344e-02,
r5 = 7.77942496381893596434e-04,
r6 = 7.32668430744625636189e-06,
w0 = 4.18938533204672725052e-01,
w1 = 8.33333333333329678849e-02,
w2 = -2.77777777728775536470e-03,
w3 = 7.93650558643019558500e-04,
w4 = -5.95187557450339963135e-04,
w5 = 8.36339918996282139126e-04,
w6 = -1.63092934096575273989e-03;
union {double f; UINT64 i;} u = {x};
double t, y, z, nadj, p, p1, p2, p3, q, r, w;
UINT32 ix;
int sign,i;
/* purge off +-inf, NaN, +-0, tiny and negative arguments */
sign = u.i >> 63;
ix = u.i >> 32 & 0x7fffffff;
if (ix >= 0x7ff00000)
return x * x;
if (ix < (0x3ff - 70) << 20) { /* |x|<2**-70, return -log(|x|) */
if(sign)
x = -x;
return -log(x);
}
if (sign) {
x = -x;
t = sin_pi(x);
if (t == 0.0) { /* -integer */
*_errno() = ERANGE;
return 1.0 / (x - x);
}
if (t <= 0.0)
t = -t;
nadj = log(pi / (t * x));
}
/* purge off 1 and 2 */
if ((ix == 0x3ff00000 || ix == 0x40000000) && (UINT32)u.i == 0)
r = 0;
/* for x < 2.0 */
else if (ix < 0x40000000) {
if (ix <= 0x3feccccc) { /* lgamma(x) = lgamma(x+1)-log(x) */
r = -log(x);
if (ix >= 0x3FE76944) {
y = 1.0 - x;
i = 0;
} else if (ix >= 0x3FCDA661) {
y = x - (tc - 1.0);
i = 1;
} else {
y = x;
i = 2;
}
} else {
r = 0.0;
if (ix >= 0x3FFBB4C3) { /* [1.7316,2] */
y = 2.0 - x;
i = 0;
} else if(ix >= 0x3FF3B4C4) { /* [1.23,1.73] */
y = x - tc;
i = 1;
} else {
y = x - 1.0;
i = 2;
}
}
switch (i) {
case 0:
z = y * y;
p1 = a0 + z * (a2 + z * (a4 + z * (a6 + z * (a8 + z * a10))));
p2 = z * (a1 + z * (a3 + z * (a5 + z * (a7 + z * (a9 + z * a11)))));
p = y * p1 + p2;
r += (p - 0.5 * y);
break;
case 1:
z = y * y;
w = z * y;
p1 = t0 + w * (t3 + w * (t6 + w * (t9 + w * t12))); /* parallel comp */
p2 = t1 + w * (t4 + w * (t7 + w * (t10 + w * t13)));
p3 = t2 + w * (t5 + w * (t8 + w * (t11 + w * t14)));
p = z * p1 - (tt - w * (p2 + y * p3));
r += tf + p;
break;
case 2:
p1 = y * (u0 + y * (u1 + y * (u2 + y * (u3 + y * (u4 + y * u5)))));
p2 = 1.0 + y * (v1 + y * (v2 + y * (v3 + y * (v4 + y * v5))));
r += -0.5 * y + p1 / p2;
}
} else if (ix < 0x40200000) { /* x < 8.0 */
i = (int)x;
y = x - (double)i;
p = y * (s0 + y * (s1 + y * (s2 + y * (s3 + y * (s4 + y * (s5 + y * s6))))));
q = 1.0 + y * (r1 + y * (r2 + y * (r3 + y * (r4 + y * (r5 + y * r6)))));
r = 0.5 * y + p / q;
z = 1.0; /* lgamma(1+s) = log(s) + lgamma(s) */
switch (i) {
case 7: z *= y + 6.0; /* fall through */
case 6: z *= y + 5.0; /* fall through */
case 5: z *= y + 4.0; /* fall through */
case 4: z *= y + 3.0; /* fall through */
case 3:
z *= y + 2.0;
r += log(z);
break;
}
} else if (ix < 0x43900000) { /* 8.0 <= x < 2**58 */
t = log(x);
z = 1.0 / x;
y = z * z;
w = w0 + z * (w1 + y * (w2 + y * (w3 + y * (w4 + y * (w5 + y * w6)))));
r = (x - 0.5) * (t - 1.0) + w;
} else /* 2**58 <= x <= inf */
r = x * (log(x) - 1.0);
if (sign)
r = nadj - r;
return r;
}
/* sin(pi*x) assuming x > 2^-100, if sin(pi*x)==0 the sign is arbitrary */
static float sinf_pi(float x)
{
double y;
int n;
/* spurious inexact if odd int */
x = 2 * (x * 0.5f - floorf(x * 0.5f)); /* x mod 2.0 */
n = (int)(x * 4);
n = (n + 1) / 2;
y = x - n * 0.5f;
y *= M_PI;
switch (n) {
default: /* case 4: */
case 0: return __sindf(y);
case 1: return __cosdf(y);
case 2: return __sindf(-y);
case 3: return -__cosdf(y);
}
}
/*********************************************************************
* lgammaf (MSVCR120.@)
*
* Copied from musl: src/math/lgammaf_r.c
*/
float CDECL lgammaf(float x)
{
static const float pi = 3.1415927410e+00,
a0 = 7.7215664089e-02,
a1 = 3.2246702909e-01,
a2 = 6.7352302372e-02,
a3 = 2.0580807701e-02,
a4 = 7.3855509982e-03,
a5 = 2.8905137442e-03,
a6 = 1.1927076848e-03,
a7 = 5.1006977446e-04,
a8 = 2.2086278477e-04,
a9 = 1.0801156895e-04,
a10 = 2.5214456400e-05,
a11 = 4.4864096708e-05,
tc = 1.4616321325e+00,
tf = -1.2148628384e-01,
tt = 6.6971006518e-09,
t0 = 4.8383611441e-01,
t1 = -1.4758771658e-01,
t2 = 6.4624942839e-02,
t3 = -3.2788541168e-02,
t4 = 1.7970675603e-02,
t5 = -1.0314224288e-02,
t6 = 6.1005386524e-03,
t7 = -3.6845202558e-03,
t8 = 2.2596477065e-03,
t9 = -1.4034647029e-03,
t10 = 8.8108185446e-04,
t11 = -5.3859531181e-04,
t12 = 3.1563205994e-04,
t13 = -3.1275415677e-04,
t14 = 3.3552918467e-04,
u0 = -7.7215664089e-02,
u1 = 6.3282704353e-01,
u2 = 1.4549225569e+00,
u3 = 9.7771751881e-01,
u4 = 2.2896373272e-01,
u5 = 1.3381091878e-02,
v1 = 2.4559779167e+00,
v2 = 2.1284897327e+00,
v3 = 7.6928514242e-01,
v4 = 1.0422264785e-01,
v5 = 3.2170924824e-03,
s0 = -7.7215664089e-02,
s1 = 2.1498242021e-01,
s2 = 3.2577878237e-01,
s3 = 1.4635047317e-01,
s4 = 2.6642270386e-02,
s5 = 1.8402845599e-03,
s6 = 3.1947532989e-05,
r1 = 1.3920053244e+00,
r2 = 7.2193557024e-01,
r3 = 1.7193385959e-01,
r4 = 1.8645919859e-02,
r5 = 7.7794247773e-04,
r6 = 7.3266842264e-06,
w0 = 4.1893854737e-01,
w1 = 8.3333335817e-02,
w2 = -2.7777778450e-03,
w3 = 7.9365057172e-04,
w4 = -5.9518753551e-04,
w5 = 8.3633989561e-04,
w6 = -1.6309292987e-03;
union {float f; UINT32 i;} u = {x};
float t, y, z, nadj, p, p1, p2, p3, q, r, w;
UINT32 ix;
int i, sign;
/* purge off +-inf, NaN, +-0, tiny and negative arguments */
sign = u.i >> 31;
ix = u.i & 0x7fffffff;
if (ix >= 0x7f800000)
return x * x;
if (ix < 0x35000000) { /* |x| < 2**-21, return -log(|x|) */
if (sign)
x = -x;
return -logf(x);
}
if (sign) {
x = -x;
t = sinf_pi(x);
if (t == 0.0f) { /* -integer */
*_errno() = ERANGE;
return 1.0f / (x - x);
}
if (t <= 0.0f)
t = -t;
nadj = logf(pi / (t * x));
}
/* purge off 1 and 2 */
if (ix == 0x3f800000 || ix == 0x40000000)
r = 0;
/* for x < 2.0 */
else if (ix < 0x40000000) {
if (ix <= 0x3f666666) { /* lgamma(x) = lgamma(x+1)-log(x) */
r = -logf(x);
if (ix >= 0x3f3b4a20) {
y = 1.0f - x;
i = 0;
} else if (ix >= 0x3e6d3308) {
y = x - (tc - 1.0f);
i = 1;
} else {
y = x;
i = 2;
}
} else {
r = 0.0f;
if (ix >= 0x3fdda618) { /* [1.7316,2] */
y = 2.0f - x;
i = 0;
} else if (ix >= 0x3F9da620) { /* [1.23,1.73] */
y = x - tc;
i = 1;
} else {
y = x - 1.0f;
i = 2;
}
}
switch(i) {
case 0:
z = y * y;
p1 = a0 + z * (a2 + z * (a4 + z * (a6 + z * (a8 + z * a10))));
p2 = z * (a1 + z * (a3 + z * (a5 + z * (a7 + z * (a9 + z * a11)))));
p = y * p1 + p2;
r += p - 0.5f * y;
break;
case 1:
z = y * y;
w = z * y;
p1 = t0 + w * (t3 + w * (t6 + w * (t9 + w * t12))); /* parallel comp */
p2 = t1 + w * (t4 + w * (t7 + w * (t10 + w * t13)));
p3 = t2 + w * (t5 + w * (t8 + w * (t11 + w * t14)));
p = z * p1 - (tt - w * (p2 + y * p3));
r += (tf + p);
break;
case 2:
p1 = y * (u0 + y * (u1 + y * (u2 + y * (u3 + y * (u4 + y * u5)))));
p2 = 1.0f + y * (v1 + y * (v2 + y * (v3 + y * (v4 + y * v5))));
r += -0.5f * y + p1 / p2;
}
} else if (ix < 0x41000000) { /* x < 8.0 */
i = (int)x;
y = x - (float)i;
p = y * (s0 + y * (s1 + y * (s2 + y * (s3 + y * (s4 + y * (s5 + y * s6))))));
q = 1.0f + y * (r1 + y * (r2 + y * (r3 + y * (r4 + y * (r5 + y * r6)))));
r = 0.5f * y + p / q;
z = 1.0f; /* lgamma(1+s) = log(s) + lgamma(s) */
switch (i) {
case 7: z *= y + 6.0f; /* fall through */
case 6: z *= y + 5.0f; /* fall through */
case 5: z *= y + 4.0f; /* fall through */
case 4: z *= y + 3.0f; /* fall through */
case 3:
z *= y + 2.0f;
r += logf(z);
break;
}
} else if (ix < 0x5c800000) { /* 8.0 <= x < 2**58 */
t = logf(x);
z = 1.0f / x;
y = z * z;
w = w0 + z * (w1 + y * (w2 + y * (w3 + y * (w4 + y * (w5 + y * w6)))));
r = (x - 0.5f) * (t - 1.0f) + w;
} else /* 2**58 <= x <= inf */
r = x * (logf(x) - 1.0f);
if (sign)
r = nadj - r;
return r;
}
static double tgamma_S(double x)
{
static const double Snum[] = {

View file

@ -190,8 +190,10 @@ double __lgamma_r(double x, int *signgamp)
if (sign) {
x = -x;
t = sin_pi(x);
if (t == 0.0) /* -integer */
if (t == 0.0) { /* -integer */
errno = ERANGE;
return 1.0/(x-x);
}
if (t > 0.0)
*signgamp = -1;
else

View file

@ -125,8 +125,10 @@ float __lgammaf_r(float x, int *signgamp)
if (sign) {
x = -x;
t = sin_pi(x);
if (t == 0.0f) /* -integer */
if (t == 0.0f) { /* -integer */
errno = ERANGE;
return 1.0f/(x-x);
}
if (t > 0.0f)
*signgamp = -1;
else