1*f504f610SAugustin Cavalier /* origin: FreeBSD /usr/src/lib/msun/src/e_log10f.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 * See comments in log10.c.
14*f504f610SAugustin Cavalier */
15*f504f610SAugustin Cavalier
16*f504f610SAugustin Cavalier #include <math.h>
17*f504f610SAugustin Cavalier #include <stdint.h>
18*f504f610SAugustin Cavalier
19*f504f610SAugustin Cavalier static const float
20*f504f610SAugustin Cavalier ivln10hi = 4.3432617188e-01, /* 0x3ede6000 */
21*f504f610SAugustin Cavalier ivln10lo = -3.1689971365e-05, /* 0xb804ead9 */
22*f504f610SAugustin Cavalier log10_2hi = 3.0102920532e-01, /* 0x3e9a2080 */
23*f504f610SAugustin Cavalier log10_2lo = 7.9034151668e-07, /* 0x355427db */
24*f504f610SAugustin Cavalier /* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
25*f504f610SAugustin Cavalier Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */
26*f504f610SAugustin Cavalier Lg2 = 0xccce13.0p-25, /* 0.40000972152 */
27*f504f610SAugustin Cavalier Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */
28*f504f610SAugustin Cavalier Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */
29*f504f610SAugustin Cavalier
log10f(float x)30*f504f610SAugustin Cavalier float log10f(float x)
31*f504f610SAugustin Cavalier {
32*f504f610SAugustin Cavalier union {float f; uint32_t i;} u = {x};
33*f504f610SAugustin Cavalier float_t hfsq,f,s,z,R,w,t1,t2,dk,hi,lo;
34*f504f610SAugustin Cavalier uint32_t ix;
35*f504f610SAugustin Cavalier int k;
36*f504f610SAugustin Cavalier
37*f504f610SAugustin Cavalier ix = u.i;
38*f504f610SAugustin Cavalier k = 0;
39*f504f610SAugustin Cavalier if (ix < 0x00800000 || ix>>31) { /* x < 2**-126 */
40*f504f610SAugustin Cavalier if (ix<<1 == 0)
41*f504f610SAugustin Cavalier return -1/(x*x); /* log(+-0)=-inf */
42*f504f610SAugustin Cavalier if (ix>>31)
43*f504f610SAugustin Cavalier return (x-x)/0.0f; /* log(-#) = NaN */
44*f504f610SAugustin Cavalier /* subnormal number, scale up x */
45*f504f610SAugustin Cavalier k -= 25;
46*f504f610SAugustin Cavalier x *= 0x1p25f;
47*f504f610SAugustin Cavalier u.f = x;
48*f504f610SAugustin Cavalier ix = u.i;
49*f504f610SAugustin Cavalier } else if (ix >= 0x7f800000) {
50*f504f610SAugustin Cavalier return x;
51*f504f610SAugustin Cavalier } else if (ix == 0x3f800000)
52*f504f610SAugustin Cavalier return 0;
53*f504f610SAugustin Cavalier
54*f504f610SAugustin Cavalier /* reduce x into [sqrt(2)/2, sqrt(2)] */
55*f504f610SAugustin Cavalier ix += 0x3f800000 - 0x3f3504f3;
56*f504f610SAugustin Cavalier k += (int)(ix>>23) - 0x7f;
57*f504f610SAugustin Cavalier ix = (ix&0x007fffff) + 0x3f3504f3;
58*f504f610SAugustin Cavalier u.i = ix;
59*f504f610SAugustin Cavalier x = u.f;
60*f504f610SAugustin Cavalier
61*f504f610SAugustin Cavalier f = x - 1.0f;
62*f504f610SAugustin Cavalier s = f/(2.0f + f);
63*f504f610SAugustin Cavalier z = s*s;
64*f504f610SAugustin Cavalier w = z*z;
65*f504f610SAugustin Cavalier t1= w*(Lg2+w*Lg4);
66*f504f610SAugustin Cavalier t2= z*(Lg1+w*Lg3);
67*f504f610SAugustin Cavalier R = t2 + t1;
68*f504f610SAugustin Cavalier hfsq = 0.5f*f*f;
69*f504f610SAugustin Cavalier
70*f504f610SAugustin Cavalier hi = f - hfsq;
71*f504f610SAugustin Cavalier u.f = hi;
72*f504f610SAugustin Cavalier u.i &= 0xfffff000;
73*f504f610SAugustin Cavalier hi = u.f;
74*f504f610SAugustin Cavalier lo = f - hi - hfsq + s*(hfsq+R);
75*f504f610SAugustin Cavalier dk = k;
76*f504f610SAugustin Cavalier return dk*log10_2lo + (lo+hi)*ivln10lo + lo*ivln10hi + hi*ivln10hi + dk*log10_2hi;
77*f504f610SAugustin Cavalier }
78