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