blob: 9ca2f017da666e2a4da75df66f1ab846eb0537e9 [file] [log] [blame]
Rich Felkerb69f6952012-03-13 01:17:53 -04001/* origin: FreeBSD /usr/src/lib/msun/src/e_log10f.c */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12/*
13 * See comments in log10.c.
14 */
15
Szabolcs Nagy71d23b32013-10-28 01:16:14 +000016#include <math.h>
17#include <stdint.h>
Rich Felkerb69f6952012-03-13 01:17:53 -040018
19static const float
Rich Felkerb69f6952012-03-13 01:17:53 -040020ivln10hi = 4.3432617188e-01, /* 0x3ede6000 */
21ivln10lo = -3.1689971365e-05, /* 0xb804ead9 */
22log10_2hi = 3.0102920532e-01, /* 0x3e9a2080 */
Szabolcs Nagy71d23b32013-10-28 01:16:14 +000023log10_2lo = 7.9034151668e-07, /* 0x355427db */
24/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
25Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */
26Lg2 = 0xccce13.0p-25, /* 0.40000972152 */
27Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */
28Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */
Rich Felkerb69f6952012-03-13 01:17:53 -040029
Rich Felkerb69f6952012-03-13 01:17:53 -040030float log10f(float x)
31{
Szabolcs Nagy71d23b32013-10-28 01:16:14 +000032 union {float f; uint32_t i;} u = {x};
33 float_t hfsq,f,s,z,R,w,t1,t2,dk,hi,lo;
34 uint32_t ix;
35 int k;
Rich Felkerb69f6952012-03-13 01:17:53 -040036
Szabolcs Nagy71d23b32013-10-28 01:16:14 +000037 ix = u.i;
Rich Felkerb69f6952012-03-13 01:17:53 -040038 k = 0;
Szabolcs Nagy71d23b32013-10-28 01:16:14 +000039 if (ix < 0x00800000 || ix>>31) { /* x < 2**-126 */
40 if (ix<<1 == 0)
41 return -1/(x*x); /* log(+-0)=-inf */
42 if (ix>>31)
43 return (x-x)/0.0f; /* log(-#) = NaN */
Rich Felkerb69f6952012-03-13 01:17:53 -040044 /* subnormal number, scale up x */
45 k -= 25;
Szabolcs Nagy71d23b32013-10-28 01:16:14 +000046 x *= 0x1p25f;
47 u.f = x;
48 ix = u.i;
49 } else if (ix >= 0x7f800000) {
50 return x;
51 } else if (ix == 0x3f800000)
52 return 0;
Rich Felkerb69f6952012-03-13 01:17:53 -040053
Szabolcs Nagy71d23b32013-10-28 01:16:14 +000054 /* reduce x into [sqrt(2)/2, sqrt(2)] */
55 ix += 0x3f800000 - 0x3f3504f3;
56 k += (int)(ix>>23) - 0x7f;
57 ix = (ix&0x007fffff) + 0x3f3504f3;
58 u.i = ix;
59 x = u.f;
60
61 f = x - 1.0f;
62 s = f/(2.0f + f);
63 z = s*s;
64 w = z*z;
65 t1= w*(Lg2+w*Lg4);
66 t2= z*(Lg1+w*Lg3);
67 R = t2 + t1;
68 hfsq = 0.5f*f*f;
69
Rich Felkerb69f6952012-03-13 01:17:53 -040070 hi = f - hfsq;
Szabolcs Nagy71d23b32013-10-28 01:16:14 +000071 u.f = hi;
72 u.i &= 0xfffff000;
73 hi = u.f;
74 lo = f - hi - hfsq + s*(hfsq+R);
75 dk = k;
76 return dk*log10_2lo + (lo+hi)*ivln10lo + lo*ivln10hi + hi*ivln10hi + dk*log10_2hi;
Rich Felkerb69f6952012-03-13 01:17:53 -040077}