Skip to content

Commit f5377fd

Browse files
committed
Merge pull request #75 from NuxiNL/signgam
Clean up handling of signgam
2 parents 8b38bd1 + 55ac462 commit f5377fd

8 files changed

Lines changed: 46 additions & 71 deletions

File tree

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,26 @@
1616
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1717
*/
1818

19-
/* lgammal
19+
/* lgammal_r
2020
*
2121
* Natural logarithm of gamma function
2222
*
2323
*
2424
*
2525
* SYNOPSIS:
2626
*
27-
* long double x, y, lgammal();
28-
* extern int signgam;
27+
* long double x, y, lgammal_r();
28+
* int signgam;
2929
*
30-
* y = lgammal(x);
30+
* y = lgammal_r(x, &signgam);
3131
*
3232
*
3333
*
3434
* DESCRIPTION:
3535
*
3636
* Returns the base e (2.718...) logarithm of the absolute
3737
* value of the gamma function of the argument.
38-
* The sign (+1 or -1) of the gamma function is returned in a
39-
* global (extern) variable named signgam.
38+
* The sign (+1 or -1) of the gamma function is returned through signgamp.
4039
*
4140
* The positive domain is partitioned into numerous segments for approximation.
4241
* For x > 10,
@@ -757,20 +756,20 @@ deval (long double x, const long double *p, int n)
757756

758757

759758
long double
760-
lgammal(long double x)
759+
lgammal_r(long double x, int *signgamp)
761760
{
762761
long double p, q, w, z, nx;
763762
int i, nn;
764763

765-
signgam = 1;
764+
*signgamp = 1;
766765

767766
if (! finite (x))
768767
return x * x;
769768

770769
if (x == 0.0L)
771770
{
772771
if (signbit (x))
773-
signgam = -1;
772+
*signgamp = -1;
774773
return one / fabsl (x);
775774
}
776775

@@ -782,9 +781,9 @@ lgammal(long double x)
782781
return (one / (p - p));
783782
i = p;
784783
if ((i & 1) == 0)
785-
signgam = -1;
784+
*signgamp = -1;
786785
else
787-
signgam = 1;
786+
*signgamp = 1;
788787
z = q - p;
789788
if (z > 0.5L)
790789
{
@@ -793,7 +792,7 @@ lgammal(long double x)
793792
}
794793
z = q * sinl (PIL * z);
795794
if (z == 0.0L)
796-
return (signgam * huge * huge);
795+
return (*signgamp * huge * huge);
797796
w = lgammal (q);
798797
z = logl (PIL / z) - w;
799798
return (z);
@@ -1025,7 +1024,7 @@ lgammal(long double x)
10251024
}
10261025

10271026
if (x > MAXLGM)
1028-
return (signgam * huge * huge);
1027+
return (*signgamp * huge * huge);
10291028

10301029
q = ls2pi - x;
10311030
q = (x - 0.5L) * logl (x) + q;

ld128/e_tgammal.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,14 @@ tgammal(long double x)
2626
int64_t i0,i1;
2727

2828
GET_LDOUBLE_WORDS64(i0,i1,x);
29-
if (((i0&0x7fffffffffffffffLL)|i1) == 0) {
30-
signgam = 0;
29+
if (((i0&0x7fffffffffffffffLL)|i1) == 0)
3130
return (1.0/x);
32-
}
3331

34-
if (i0<0 && (u_int64_t)i0<0xffff000000000000ULL && rintl(x)==x) {
35-
signgam = 0;
32+
if (i0<0 && (u_int64_t)i0<0xffff000000000000ULL && rintl(x)==x)
3633
return (x-x)/(x-x);
37-
}
3834

39-
if (i0==0xffff000000000000ULL && i1==0) {
40-
signgam = 0;
35+
if (i0==0xffff000000000000ULL && i1==0)
4136
return (x-x);
42-
}
4337

4438
return expl(lgammal(x));
4539
}

ld80/Make.files

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$(CUR_SRCS) += invtrig.c \
22
e_acoshl.c e_powl.c k_tanl.c s_exp2l.c \
3-
e_atanhl.c e_lgammal.c e_sinhl.c s_asinhl.c s_expm1l.c \
3+
e_atanhl.c e_lgammal_r.c e_sinhl.c s_asinhl.c s_expm1l.c \
44
e_coshl.c e_log10l.c e_tgammal.c \
55
e_expl.c e_log2l.c k_cosl.c s_log1pl.c s_tanhl.c \
66
e_logl.c k_sinl.c s_erfl.c

ld80/e_lgammal.c renamed to ld80/e_lgammal_r.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2626
*/
2727

28-
/* lgammal(x)
28+
/* lgammal_r(x, signgamp)
2929
* Reentrant version of the logarithm of the Gamma function
3030
* with user provide pointer for the sign of Gamma(x).
3131
*
@@ -89,7 +89,6 @@
8989
#include <openlibm.h>
9090

9191
#include "math_private.h"
92-
extern int signgam;
9392

9493
static const long double
9594
half = 0.5L,
@@ -267,20 +266,20 @@ sin_pi(long double x)
267266

268267

269268
long double
270-
lgammal(long double x)
269+
lgammal_r(long double x, int *signgamp)
271270
{
272271
long double t, y, z, nadj, p, p1, p2, q, r, w;
273272
int i, ix;
274273
u_int32_t se, i0, i1;
275274

276-
signgam = 1;
275+
*signgamp = 1;
277276
GET_LDOUBLE_WORDS (se, i0, i1, x);
278277
ix = se & 0x7fff;
279278

280279
if ((ix | i0 | i1) == 0)
281280
{
282281
if (se & 0x8000)
283-
signgam = -1;
282+
*signgamp = -1;
284283
return one / fabsl (x);
285284
}
286285

@@ -294,7 +293,7 @@ lgammal(long double x)
294293
{ /* |x|<2**-63, return -log(|x|) */
295294
if (se & 0x8000)
296295
{
297-
signgam = -1;
296+
*signgamp = -1;
298297
return -logl (-x);
299298
}
300299
else
@@ -307,7 +306,7 @@ lgammal(long double x)
307306
return one / fabsl (t); /* -integer */
308307
nadj = logl (pi / fabsl (t * x));
309308
if (t < zero)
310-
signgam = -1;
309+
*signgamp = -1;
311310
x = -x;
312311
}
313312

ld80/e_tgammal.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,15 @@
2525
* SYNOPSIS:
2626
*
2727
* long double x, y, tgammal();
28-
* extern int signgam;
2928
*
3029
* y = tgammal( x );
3130
*
3231
*
3332
*
3433
* DESCRIPTION:
3534
*
36-
* Returns gamma function of the argument. The result is
37-
* correctly signed, and the sign (+1 or -1) is also
38-
* returned in a global (extern) variable named signgam.
39-
* This variable is also filled in by the logarithmic gamma
35+
* Returns gamma function of the argument. The result is correctly
36+
* signed. This variable is also filled in by the logarithmic gamma
4037
* function lgamma().
4138
*
4239
* Arguments |x| <= 13 are reduced by recurrence and the function
@@ -61,7 +58,6 @@
6158
#include <openlibm.h>
6259

6360
#include "math_private.h"
64-
extern int signgam;
6561

6662
/*
6763
tgamma(x+2) = tgamma(x+2) P(x)/Q(x)
@@ -224,7 +220,6 @@ tgammal(long double x)
224220
long double p, q, z;
225221
int i;
226222

227-
signgam = 1;
228223
if( isnan(x) )
229224
return(NAN);
230225
if(x == INFINITY)
@@ -237,6 +232,7 @@ q = fabsl(x);
237232

238233
if( q > 13.0L )
239234
{
235+
int sign = 1;
240236
if( q > MAXGAML )
241237
goto goverf;
242238
if( x < 0.0L )
@@ -246,7 +242,7 @@ if( q > 13.0L )
246242
return (x - x) / (x - x);
247243
i = p;
248244
if( (i & 1) == 0 )
249-
signgam = -1;
245+
sign = -1;
250246
z = q - p;
251247
if( z > 0.5L )
252248
{
@@ -258,15 +254,15 @@ if( q > 13.0L )
258254
if( z <= PIL/LDBL_MAX )
259255
{
260256
goverf:
261-
return( signgam * INFINITY);
257+
return( sign * INFINITY);
262258
}
263259
z = PIL/z;
264260
}
265261
else
266262
{
267263
z = stirf(x);
268264
}
269-
return( signgam * z );
265+
return( sign * z );
270266
}
271267

272268
z = 1.0L;
@@ -298,8 +294,6 @@ x -= 2.0L;
298294
p = __polevll( x, P, 7 );
299295
q = __polevll( x, Q, 8 );
300296
z = z * p / q;
301-
if( z < 0 )
302-
signgam = -1;
303297
return z;
304298

305299
small:
@@ -311,7 +305,6 @@ else
311305
{
312306
x = -x;
313307
q = z / (x * __polevll( x, SN, 8 ));
314-
signgam = -1;
315308
}
316309
else
317310
q = z / (x * __polevll( x, S, 8 ));

src/Make.files

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $(CUR_SRCS) = common.c \
44
e_expf.c e_fmod.c e_fmodf.c e_gamma.c e_gamma_r.c e_gammaf.c \
55
e_gammaf_r.c e_hypot.c e_hypotf.c e_j0.c e_j0f.c e_j1.c e_j1f.c \
66
e_jn.c e_jnf.c e_lgamma.c e_lgamma_r.c e_lgammaf.c e_lgammaf_r.c \
7-
e_log.c e_log10.c e_log10f.c e_log2.c e_log2f.c e_logf.c \
7+
e_lgammal.c e_log.c e_log10.c e_log10f.c e_log2.c e_log2f.c e_logf.c \
88
e_pow.c e_powf.c e_remainder.c e_remainderf.c e_scalb.c e_scalbf.c \
99
e_rem_pio2.c e_rem_pio2f.c \
1010
e_sinh.c e_sinhf.c e_sqrt.c e_sqrtf.c \

src/e_lgammal.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "cdefs-compat.h"
2+
#include "openlibm.h"
3+
#include "math_private.h"
4+
5+
extern int signgam;
6+
7+
DLLEXPORT long double
8+
lgammal(long double x)
9+
{
10+
11+
return (lgammal_r(x, &signgam));
12+
}

src/openlibm.h

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -515,35 +515,23 @@ float significandf(float);
515515
* long double versions of ISO/POSIX math functions
516516
*/
517517
#if __ISO_C_VISIBLE >= 1999
518-
#if _DECLARE_C99_LDBL_MATH
519518
long double acoshl(long double);
520-
#endif
521519
long double acosl(long double);
522-
#if _DECLARE_C99_LDBL_MATH
523520
long double asinhl(long double);
524-
#endif
525521
long double asinl(long double);
526522
long double atan2l(long double, long double);
527-
#if _DECLARE_C99_LDBL_MATH
528523
long double atanhl(long double);
529-
#endif
530524
long double atanl(long double);
531525
long double cbrtl(long double);
532526
long double ceill(long double);
533527
long double copysignl(long double, long double) __pure2;
534-
#if _DECLARE_C99_LDBL_MATH
535528
long double coshl(long double);
536-
#endif
537529
long double cosl(long double);
538-
#if _DECLARE_C99_LDBL_MATH
539530
long double erfcl(long double);
540531
long double erfl(long double);
541-
#endif
542532
long double exp2l(long double);
543-
#if _DECLARE_C99_LDBL_MATH
544533
long double expl(long double);
545534
long double expm1l(long double);
546-
#endif
547535
long double fabsl(long double) __pure2;
548536
long double fdiml(long double, long double);
549537
long double floorl(long double);
@@ -555,20 +543,14 @@ long double frexpl(long double value, int *); /* fundamentally !__pure2 */
555543
long double hypotl(long double, long double);
556544
int ilogbl(long double) __pure2;
557545
long double ldexpl(long double, int);
558-
#if _DECLARE_C99_LDBL_MATH
559546
long double lgammal(long double);
560-
#endif
561547
long long llrintl(long double);
562548
long long llroundl(long double);
563-
#if _DECLARE_C99_LDBL_MATH
564549
long double log10l(long double);
565550
long double log1pl(long double);
566551
long double log2l(long double);
567-
#endif
568552
long double logbl(long double);
569-
#if _DECLARE_C99_LDBL_MATH
570553
long double logl(long double);
571-
#endif
572554
long lrintl(long double);
573555
long lroundl(long double);
574556
long double modfl(long double, long double *); /* fundamentally !__pure2 */
@@ -578,31 +560,27 @@ long double nextafterl(long double, long double);
578560
double nexttoward(double, long double);
579561
float nexttowardf(float, long double);
580562
long double nexttowardl(long double, long double);
581-
#if _DECLARE_C99_LDBL_MATH
582563
long double powl(long double, long double);
583-
#endif
584564
long double remainderl(long double, long double);
585565
long double remquol(long double, long double, int *);
586566
long double rintl(long double);
587567
long double roundl(long double);
588568
long double scalblnl(long double, long);
589569
long double scalbnl(long double, int);
590-
#if _DECLARE_C99_LDBL_MATH
591570
long double sinhl(long double);
592-
#endif
593571
long double sinl(long double);
594572
long double sqrtl(long double);
595-
#if _DECLARE_C99_LDBL_MATH
596573
long double tanhl(long double);
597-
#endif
598574
long double tanl(long double);
599-
#if _DECLARE_C99_LDBL_MATH
600575
long double tgammal(long double);
601-
#endif
602576
long double truncl(long double);
603-
604577
#endif /* __ISO_C_VISIBLE >= 1999 */
605578

579+
/* Reentrant version of lgammal. */
580+
#if __BSD_VISIBLE
581+
long double lgammal_r(long double, int *);
582+
#endif /* __BSD_VISIBLE */
583+
606584
#include "openlibm_complex.h"
607585

608586
#if defined(__cplusplus)

0 commit comments

Comments
 (0)