1*f504f610SAugustin Cavalier /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_log2l.c */
2*f504f610SAugustin Cavalier /*
3*f504f610SAugustin Cavalier * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
4*f504f610SAugustin Cavalier *
5*f504f610SAugustin Cavalier * Permission to use, copy, modify, and distribute this software for any
6*f504f610SAugustin Cavalier * purpose with or without fee is hereby granted, provided that the above
7*f504f610SAugustin Cavalier * copyright notice and this permission notice appear in all copies.
8*f504f610SAugustin Cavalier *
9*f504f610SAugustin Cavalier * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*f504f610SAugustin Cavalier * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*f504f610SAugustin Cavalier * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*f504f610SAugustin Cavalier * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*f504f610SAugustin Cavalier * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*f504f610SAugustin Cavalier * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*f504f610SAugustin Cavalier * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*f504f610SAugustin Cavalier */
17*f504f610SAugustin Cavalier /*
18*f504f610SAugustin Cavalier * Base 2 logarithm, long double precision
19*f504f610SAugustin Cavalier *
20*f504f610SAugustin Cavalier *
21*f504f610SAugustin Cavalier * SYNOPSIS:
22*f504f610SAugustin Cavalier *
23*f504f610SAugustin Cavalier * long double x, y, log2l();
24*f504f610SAugustin Cavalier *
25*f504f610SAugustin Cavalier * y = log2l( x );
26*f504f610SAugustin Cavalier *
27*f504f610SAugustin Cavalier *
28*f504f610SAugustin Cavalier * DESCRIPTION:
29*f504f610SAugustin Cavalier *
30*f504f610SAugustin Cavalier * Returns the base 2 logarithm of x.
31*f504f610SAugustin Cavalier *
32*f504f610SAugustin Cavalier * The argument is separated into its exponent and fractional
33*f504f610SAugustin Cavalier * parts. If the exponent is between -1 and +1, the (natural)
34*f504f610SAugustin Cavalier * logarithm of the fraction is approximated by
35*f504f610SAugustin Cavalier *
36*f504f610SAugustin Cavalier * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x).
37*f504f610SAugustin Cavalier *
38*f504f610SAugustin Cavalier * Otherwise, setting z = 2(x-1)/x+1),
39*f504f610SAugustin Cavalier *
40*f504f610SAugustin Cavalier * log(x) = z + z**3 P(z)/Q(z).
41*f504f610SAugustin Cavalier *
42*f504f610SAugustin Cavalier *
43*f504f610SAugustin Cavalier * ACCURACY:
44*f504f610SAugustin Cavalier *
45*f504f610SAugustin Cavalier * Relative error:
46*f504f610SAugustin Cavalier * arithmetic domain # trials peak rms
47*f504f610SAugustin Cavalier * IEEE 0.5, 2.0 30000 9.8e-20 2.7e-20
48*f504f610SAugustin Cavalier * IEEE exp(+-10000) 70000 5.4e-20 2.3e-20
49*f504f610SAugustin Cavalier *
50*f504f610SAugustin Cavalier * In the tests over the interval exp(+-10000), the logarithms
51*f504f610SAugustin Cavalier * of the random arguments were uniformly distributed over
52*f504f610SAugustin Cavalier * [-10000, +10000].
53*f504f610SAugustin Cavalier */
54*f504f610SAugustin Cavalier
55*f504f610SAugustin Cavalier #include "libm.h"
56*f504f610SAugustin Cavalier
57*f504f610SAugustin Cavalier #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
log2l(long double x)58*f504f610SAugustin Cavalier long double log2l(long double x)
59*f504f610SAugustin Cavalier {
60*f504f610SAugustin Cavalier return log2(x);
61*f504f610SAugustin Cavalier }
62*f504f610SAugustin Cavalier #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
63*f504f610SAugustin Cavalier /* Coefficients for ln(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
64*f504f610SAugustin Cavalier * 1/sqrt(2) <= x < sqrt(2)
65*f504f610SAugustin Cavalier * Theoretical peak relative error = 6.2e-22
66*f504f610SAugustin Cavalier */
67*f504f610SAugustin Cavalier static const long double P[] = {
68*f504f610SAugustin Cavalier 4.9962495940332550844739E-1L,
69*f504f610SAugustin Cavalier 1.0767376367209449010438E1L,
70*f504f610SAugustin Cavalier 7.7671073698359539859595E1L,
71*f504f610SAugustin Cavalier 2.5620629828144409632571E2L,
72*f504f610SAugustin Cavalier 4.2401812743503691187826E2L,
73*f504f610SAugustin Cavalier 3.4258224542413922935104E2L,
74*f504f610SAugustin Cavalier 1.0747524399916215149070E2L,
75*f504f610SAugustin Cavalier };
76*f504f610SAugustin Cavalier static const long double Q[] = {
77*f504f610SAugustin Cavalier /* 1.0000000000000000000000E0,*/
78*f504f610SAugustin Cavalier 2.3479774160285863271658E1L,
79*f504f610SAugustin Cavalier 1.9444210022760132894510E2L,
80*f504f610SAugustin Cavalier 7.7952888181207260646090E2L,
81*f504f610SAugustin Cavalier 1.6911722418503949084863E3L,
82*f504f610SAugustin Cavalier 2.0307734695595183428202E3L,
83*f504f610SAugustin Cavalier 1.2695660352705325274404E3L,
84*f504f610SAugustin Cavalier 3.2242573199748645407652E2L,
85*f504f610SAugustin Cavalier };
86*f504f610SAugustin Cavalier
87*f504f610SAugustin Cavalier /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
88*f504f610SAugustin Cavalier * where z = 2(x-1)/(x+1)
89*f504f610SAugustin Cavalier * 1/sqrt(2) <= x < sqrt(2)
90*f504f610SAugustin Cavalier * Theoretical peak relative error = 6.16e-22
91*f504f610SAugustin Cavalier */
92*f504f610SAugustin Cavalier static const long double R[4] = {
93*f504f610SAugustin Cavalier 1.9757429581415468984296E-3L,
94*f504f610SAugustin Cavalier -7.1990767473014147232598E-1L,
95*f504f610SAugustin Cavalier 1.0777257190312272158094E1L,
96*f504f610SAugustin Cavalier -3.5717684488096787370998E1L,
97*f504f610SAugustin Cavalier };
98*f504f610SAugustin Cavalier static const long double S[4] = {
99*f504f610SAugustin Cavalier /* 1.00000000000000000000E0L,*/
100*f504f610SAugustin Cavalier -2.6201045551331104417768E1L,
101*f504f610SAugustin Cavalier 1.9361891836232102174846E2L,
102*f504f610SAugustin Cavalier -4.2861221385716144629696E2L,
103*f504f610SAugustin Cavalier };
104*f504f610SAugustin Cavalier /* log2(e) - 1 */
105*f504f610SAugustin Cavalier #define LOG2EA 4.4269504088896340735992e-1L
106*f504f610SAugustin Cavalier
107*f504f610SAugustin Cavalier #define SQRTH 0.70710678118654752440L
108*f504f610SAugustin Cavalier
log2l(long double x)109*f504f610SAugustin Cavalier long double log2l(long double x)
110*f504f610SAugustin Cavalier {
111*f504f610SAugustin Cavalier long double y, z;
112*f504f610SAugustin Cavalier int e;
113*f504f610SAugustin Cavalier
114*f504f610SAugustin Cavalier if (isnan(x))
115*f504f610SAugustin Cavalier return x;
116*f504f610SAugustin Cavalier if (x == INFINITY)
117*f504f610SAugustin Cavalier return x;
118*f504f610SAugustin Cavalier if (x <= 0.0) {
119*f504f610SAugustin Cavalier if (x == 0.0)
120*f504f610SAugustin Cavalier return -1/(x*x); /* -inf with divbyzero */
121*f504f610SAugustin Cavalier return 0/0.0f; /* nan with invalid */
122*f504f610SAugustin Cavalier }
123*f504f610SAugustin Cavalier
124*f504f610SAugustin Cavalier /* separate mantissa from exponent */
125*f504f610SAugustin Cavalier /* Note, frexp is used so that denormal numbers
126*f504f610SAugustin Cavalier * will be handled properly.
127*f504f610SAugustin Cavalier */
128*f504f610SAugustin Cavalier x = frexpl(x, &e);
129*f504f610SAugustin Cavalier
130*f504f610SAugustin Cavalier /* logarithm using log(x) = z + z**3 P(z)/Q(z),
131*f504f610SAugustin Cavalier * where z = 2(x-1)/x+1)
132*f504f610SAugustin Cavalier */
133*f504f610SAugustin Cavalier if (e > 2 || e < -2) {
134*f504f610SAugustin Cavalier if (x < SQRTH) { /* 2(2x-1)/(2x+1) */
135*f504f610SAugustin Cavalier e -= 1;
136*f504f610SAugustin Cavalier z = x - 0.5;
137*f504f610SAugustin Cavalier y = 0.5 * z + 0.5;
138*f504f610SAugustin Cavalier } else { /* 2 (x-1)/(x+1) */
139*f504f610SAugustin Cavalier z = x - 0.5;
140*f504f610SAugustin Cavalier z -= 0.5;
141*f504f610SAugustin Cavalier y = 0.5 * x + 0.5;
142*f504f610SAugustin Cavalier }
143*f504f610SAugustin Cavalier x = z / y;
144*f504f610SAugustin Cavalier z = x*x;
145*f504f610SAugustin Cavalier y = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3));
146*f504f610SAugustin Cavalier goto done;
147*f504f610SAugustin Cavalier }
148*f504f610SAugustin Cavalier
149*f504f610SAugustin Cavalier /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
150*f504f610SAugustin Cavalier if (x < SQRTH) {
151*f504f610SAugustin Cavalier e -= 1;
152*f504f610SAugustin Cavalier x = 2.0*x - 1.0;
153*f504f610SAugustin Cavalier } else {
154*f504f610SAugustin Cavalier x = x - 1.0;
155*f504f610SAugustin Cavalier }
156*f504f610SAugustin Cavalier z = x*x;
157*f504f610SAugustin Cavalier y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 7));
158*f504f610SAugustin Cavalier y = y - 0.5*z;
159*f504f610SAugustin Cavalier
160*f504f610SAugustin Cavalier done:
161*f504f610SAugustin Cavalier /* Multiply log of fraction by log2(e)
162*f504f610SAugustin Cavalier * and base 2 exponent by 1
163*f504f610SAugustin Cavalier *
164*f504f610SAugustin Cavalier * ***CAUTION***
165*f504f610SAugustin Cavalier *
166*f504f610SAugustin Cavalier * This sequence of operations is critical and it may
167*f504f610SAugustin Cavalier * be horribly defeated by some compiler optimizers.
168*f504f610SAugustin Cavalier */
169*f504f610SAugustin Cavalier z = y * LOG2EA;
170*f504f610SAugustin Cavalier z += x * LOG2EA;
171*f504f610SAugustin Cavalier z += y;
172*f504f610SAugustin Cavalier z += x;
173*f504f610SAugustin Cavalier z += e;
174*f504f610SAugustin Cavalier return z;
175*f504f610SAugustin Cavalier }
176*f504f610SAugustin Cavalier #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
177*f504f610SAugustin Cavalier // TODO: broken implementation to make things compile
log2l(long double x)178*f504f610SAugustin Cavalier long double log2l(long double x)
179*f504f610SAugustin Cavalier {
180*f504f610SAugustin Cavalier return log2(x);
181*f504f610SAugustin Cavalier }
182*f504f610SAugustin Cavalier #endif
183