math: fix tgamma to raise underflow for large negative values
[oweals/musl.git] / src / math / log1pf.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_log1pf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4  */
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15
16 #include "libm.h"
17
18 static const float
19 ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
20 ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
21 two25  = 3.355443200e+07,  /* 0x4c000000 */
22 Lp1 = 6.6666668653e-01, /* 3F2AAAAB */
23 Lp2 = 4.0000000596e-01, /* 3ECCCCCD */
24 Lp3 = 2.8571429849e-01, /* 3E924925 */
25 Lp4 = 2.2222198546e-01, /* 3E638E29 */
26 Lp5 = 1.8183572590e-01, /* 3E3A3325 */
27 Lp6 = 1.5313838422e-01, /* 3E1CD04F */
28 Lp7 = 1.4798198640e-01; /* 3E178897 */
29
30 float log1pf(float x)
31 {
32         float hfsq,f,c,s,z,R,u;
33         int32_t k,hx,hu,ax;
34
35         GET_FLOAT_WORD(hx, x);
36         ax = hx & 0x7fffffff;
37
38         k = 1;
39         if (hx < 0x3ed413d0) {  /* 1+x < sqrt(2)+  */
40                 if (ax >= 0x3f800000) {  /* x <= -1.0 */
41                         if (x == -1.0f)
42                                 return -two25/0.0f; /* log1p(-1)=+inf */
43                         return (x-x)/(x-x);         /* log1p(x<-1)=NaN */
44                 }
45                 if (ax < 0x38000000) {   /* |x| < 2**-15 */
46                         /* raise inexact */
47                         if (two25 + x > 0.0f && ax < 0x33800000)  /* |x| < 2**-24 */
48                                 return x;
49                         return x - x*x*0.5f;
50                 }
51                 if (hx > 0 || hx <= (int32_t)0xbe95f619) { /* sqrt(2)/2- <= 1+x < sqrt(2)+ */
52                         k = 0;
53                         f = x;
54                         hu = 1;
55                 }
56         }
57         if (hx >= 0x7f800000)
58                 return x+x;
59         if (k != 0) {
60                 if (hx < 0x5a000000) {
61                         STRICT_ASSIGN(float, u, 1.0f + x);
62                         GET_FLOAT_WORD(hu, u);
63                         k = (hu>>23) - 127;
64                         /* correction term */
65                         c = k > 0 ? 1.0f-(u-x) : x-(u-1.0f);
66                         c /= u;
67                 } else {
68                         u = x;
69                         GET_FLOAT_WORD(hu,u);
70                         k = (hu>>23) - 127;
71                         c = 0;
72                 }
73                 hu &= 0x007fffff;
74                 /*
75                  * The approximation to sqrt(2) used in thresholds is not
76                  * critical.  However, the ones used above must give less
77                  * strict bounds than the one here so that the k==0 case is
78                  * never reached from here, since here we have committed to
79                  * using the correction term but don't use it if k==0.
80                  */
81                 if (hu < 0x3504f4) {  /* u < sqrt(2) */
82                         SET_FLOAT_WORD(u, hu|0x3f800000);  /* normalize u */
83                 } else {
84                         k += 1;
85                         SET_FLOAT_WORD(u, hu|0x3f000000);  /* normalize u/2 */
86                         hu = (0x00800000-hu)>>2;
87                 }
88                 f = u - 1.0f;
89         }
90         hfsq = 0.5f * f * f;
91         if (hu == 0) {  /* |f| < 2**-20 */
92                 if (f == 0.0f) {
93                         if (k == 0)
94                                 return 0.0f;
95                         c += k*ln2_lo;
96                         return k*ln2_hi+c;
97                 }
98                 R = hfsq*(1.0f - 0.66666666666666666f * f);
99                 if (k == 0)
100                         return f - R;
101                 return k*ln2_hi - ((R-(k*ln2_lo+c))-f);
102         }
103         s = f/(2.0f + f);
104         z = s*s;
105         R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
106         if (k == 0)
107                 return f - (hfsq-s*(hfsq+R));
108         return k*ln2_hi - ((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
109 }