1*f504f610SAugustin Cavalier /* origin: FreeBSD /usr/src/lib/msun/src/s_log1pf.c */
2*f504f610SAugustin Cavalier /*
3*f504f610SAugustin Cavalier * ====================================================
4*f504f610SAugustin Cavalier * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*f504f610SAugustin Cavalier *
6*f504f610SAugustin Cavalier * Developed at SunPro, a Sun Microsystems, Inc. business.
7*f504f610SAugustin Cavalier * Permission to use, copy, modify, and distribute this
8*f504f610SAugustin Cavalier * software is freely granted, provided that this notice
9*f504f610SAugustin Cavalier * is preserved.
10*f504f610SAugustin Cavalier * ====================================================
11*f504f610SAugustin Cavalier */
12*f504f610SAugustin Cavalier
13*f504f610SAugustin Cavalier #include "libm.h"
14*f504f610SAugustin Cavalier
15*f504f610SAugustin Cavalier static const float
16*f504f610SAugustin Cavalier ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
17*f504f610SAugustin Cavalier ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
18*f504f610SAugustin Cavalier /* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
19*f504f610SAugustin Cavalier Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */
20*f504f610SAugustin Cavalier Lg2 = 0xccce13.0p-25, /* 0.40000972152 */
21*f504f610SAugustin Cavalier Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */
22*f504f610SAugustin Cavalier Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */
23*f504f610SAugustin Cavalier
log1pf(float x)24*f504f610SAugustin Cavalier float log1pf(float x)
25*f504f610SAugustin Cavalier {
26*f504f610SAugustin Cavalier union {float f; uint32_t i;} u = {x};
27*f504f610SAugustin Cavalier float_t hfsq,f,c,s,z,R,w,t1,t2,dk;
28*f504f610SAugustin Cavalier uint32_t ix,iu;
29*f504f610SAugustin Cavalier int k;
30*f504f610SAugustin Cavalier
31*f504f610SAugustin Cavalier ix = u.i;
32*f504f610SAugustin Cavalier k = 1;
33*f504f610SAugustin Cavalier if (ix < 0x3ed413d0 || ix>>31) { /* 1+x < sqrt(2)+ */
34*f504f610SAugustin Cavalier if (ix >= 0xbf800000) { /* x <= -1.0 */
35*f504f610SAugustin Cavalier if (x == -1)
36*f504f610SAugustin Cavalier return x/0.0f; /* log1p(-1)=+inf */
37*f504f610SAugustin Cavalier return (x-x)/0.0f; /* log1p(x<-1)=NaN */
38*f504f610SAugustin Cavalier }
39*f504f610SAugustin Cavalier if (ix<<1 < 0x33800000<<1) { /* |x| < 2**-24 */
40*f504f610SAugustin Cavalier /* underflow if subnormal */
41*f504f610SAugustin Cavalier if ((ix&0x7f800000) == 0)
42*f504f610SAugustin Cavalier FORCE_EVAL(x*x);
43*f504f610SAugustin Cavalier return x;
44*f504f610SAugustin Cavalier }
45*f504f610SAugustin Cavalier if (ix <= 0xbe95f619) { /* sqrt(2)/2- <= 1+x < sqrt(2)+ */
46*f504f610SAugustin Cavalier k = 0;
47*f504f610SAugustin Cavalier c = 0;
48*f504f610SAugustin Cavalier f = x;
49*f504f610SAugustin Cavalier }
50*f504f610SAugustin Cavalier } else if (ix >= 0x7f800000)
51*f504f610SAugustin Cavalier return x;
52*f504f610SAugustin Cavalier if (k) {
53*f504f610SAugustin Cavalier u.f = 1 + x;
54*f504f610SAugustin Cavalier iu = u.i;
55*f504f610SAugustin Cavalier iu += 0x3f800000 - 0x3f3504f3;
56*f504f610SAugustin Cavalier k = (int)(iu>>23) - 0x7f;
57*f504f610SAugustin Cavalier /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */
58*f504f610SAugustin Cavalier if (k < 25) {
59*f504f610SAugustin Cavalier c = k >= 2 ? 1-(u.f-x) : x-(u.f-1);
60*f504f610SAugustin Cavalier c /= u.f;
61*f504f610SAugustin Cavalier } else
62*f504f610SAugustin Cavalier c = 0;
63*f504f610SAugustin Cavalier /* reduce u into [sqrt(2)/2, sqrt(2)] */
64*f504f610SAugustin Cavalier iu = (iu&0x007fffff) + 0x3f3504f3;
65*f504f610SAugustin Cavalier u.i = iu;
66*f504f610SAugustin Cavalier f = u.f - 1;
67*f504f610SAugustin Cavalier }
68*f504f610SAugustin Cavalier s = f/(2.0f + f);
69*f504f610SAugustin Cavalier z = s*s;
70*f504f610SAugustin Cavalier w = z*z;
71*f504f610SAugustin Cavalier t1= w*(Lg2+w*Lg4);
72*f504f610SAugustin Cavalier t2= z*(Lg1+w*Lg3);
73*f504f610SAugustin Cavalier R = t2 + t1;
74*f504f610SAugustin Cavalier hfsq = 0.5f*f*f;
75*f504f610SAugustin Cavalier dk = k;
76*f504f610SAugustin Cavalier return s*(hfsq+R) + (dk*ln2_lo+c) - hfsq + f + dk*ln2_hi;
77*f504f610SAugustin Cavalier }
78