1*49393c00Smartynas /* $OpenBSD: e_log2l.c,v 1.1 2011/07/06 00:02:42 martynas Exp $ */
2*49393c00Smartynas
3*49393c00Smartynas /*
4*49393c00Smartynas * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5*49393c00Smartynas *
6*49393c00Smartynas * Permission to use, copy, modify, and distribute this software for any
7*49393c00Smartynas * purpose with or without fee is hereby granted, provided that the above
8*49393c00Smartynas * copyright notice and this permission notice appear in all copies.
9*49393c00Smartynas *
10*49393c00Smartynas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*49393c00Smartynas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*49393c00Smartynas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*49393c00Smartynas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*49393c00Smartynas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*49393c00Smartynas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*49393c00Smartynas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*49393c00Smartynas */
18*49393c00Smartynas
19*49393c00Smartynas /* log2l.c
20*49393c00Smartynas * Base 2 logarithm, 128-bit long double precision
21*49393c00Smartynas *
22*49393c00Smartynas *
23*49393c00Smartynas *
24*49393c00Smartynas * SYNOPSIS:
25*49393c00Smartynas *
26*49393c00Smartynas * long double x, y, log2l();
27*49393c00Smartynas *
28*49393c00Smartynas * y = log2l( x );
29*49393c00Smartynas *
30*49393c00Smartynas *
31*49393c00Smartynas *
32*49393c00Smartynas * DESCRIPTION:
33*49393c00Smartynas *
34*49393c00Smartynas * Returns the base 2 logarithm of x.
35*49393c00Smartynas *
36*49393c00Smartynas * The argument is separated into its exponent and fractional
37*49393c00Smartynas * parts. If the exponent is between -1 and +1, the (natural)
38*49393c00Smartynas * logarithm of the fraction is approximated by
39*49393c00Smartynas *
40*49393c00Smartynas * log(1+x) = x - 0.5 x^2 + x^3 P(x)/Q(x).
41*49393c00Smartynas *
42*49393c00Smartynas * Otherwise, setting z = 2(x-1)/x+1),
43*49393c00Smartynas *
44*49393c00Smartynas * log(x) = z + z^3 P(z)/Q(z).
45*49393c00Smartynas *
46*49393c00Smartynas *
47*49393c00Smartynas *
48*49393c00Smartynas * ACCURACY:
49*49393c00Smartynas *
50*49393c00Smartynas * Relative error:
51*49393c00Smartynas * arithmetic domain # trials peak rms
52*49393c00Smartynas * IEEE 0.5, 2.0 100,000 2.6e-34 4.9e-35
53*49393c00Smartynas * IEEE exp(+-10000) 100,000 9.6e-35 4.0e-35
54*49393c00Smartynas *
55*49393c00Smartynas * In the tests over the interval exp(+-10000), the logarithms
56*49393c00Smartynas * of the random arguments were uniformly distributed over
57*49393c00Smartynas * [-10000, +10000].
58*49393c00Smartynas *
59*49393c00Smartynas */
60*49393c00Smartynas
61*49393c00Smartynas #include <math.h>
62*49393c00Smartynas
63*49393c00Smartynas #include "math_private.h"
64*49393c00Smartynas
65*49393c00Smartynas /* Coefficients for ln(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
66*49393c00Smartynas * 1/sqrt(2) <= x < sqrt(2)
67*49393c00Smartynas * Theoretical peak relative error = 5.3e-37,
68*49393c00Smartynas * relative peak error spread = 2.3e-14
69*49393c00Smartynas */
70*49393c00Smartynas static const long double P[13] =
71*49393c00Smartynas {
72*49393c00Smartynas 1.313572404063446165910279910527789794488E4L,
73*49393c00Smartynas 7.771154681358524243729929227226708890930E4L,
74*49393c00Smartynas 2.014652742082537582487669938141683759923E5L,
75*49393c00Smartynas 3.007007295140399532324943111654767187848E5L,
76*49393c00Smartynas 2.854829159639697837788887080758954924001E5L,
77*49393c00Smartynas 1.797628303815655343403735250238293741397E5L,
78*49393c00Smartynas 7.594356839258970405033155585486712125861E4L,
79*49393c00Smartynas 2.128857716871515081352991964243375186031E4L,
80*49393c00Smartynas 3.824952356185897735160588078446136783779E3L,
81*49393c00Smartynas 4.114517881637811823002128927449878962058E2L,
82*49393c00Smartynas 2.321125933898420063925789532045674660756E1L,
83*49393c00Smartynas 4.998469661968096229986658302195402690910E-1L,
84*49393c00Smartynas 1.538612243596254322971797716843006400388E-6L
85*49393c00Smartynas };
86*49393c00Smartynas static const long double Q[12] =
87*49393c00Smartynas {
88*49393c00Smartynas 3.940717212190338497730839731583397586124E4L,
89*49393c00Smartynas 2.626900195321832660448791748036714883242E5L,
90*49393c00Smartynas 7.777690340007566932935753241556479363645E5L,
91*49393c00Smartynas 1.347518538384329112529391120390701166528E6L,
92*49393c00Smartynas 1.514882452993549494932585972882995548426E6L,
93*49393c00Smartynas 1.158019977462989115839826904108208787040E6L,
94*49393c00Smartynas 6.132189329546557743179177159925690841200E5L,
95*49393c00Smartynas 2.248234257620569139969141618556349415120E5L,
96*49393c00Smartynas 5.605842085972455027590989944010492125825E4L,
97*49393c00Smartynas 9.147150349299596453976674231612674085381E3L,
98*49393c00Smartynas 9.104928120962988414618126155557301584078E2L,
99*49393c00Smartynas 4.839208193348159620282142911143429644326E1L
100*49393c00Smartynas /* 1.000000000000000000000000000000000000000E0L, */
101*49393c00Smartynas };
102*49393c00Smartynas
103*49393c00Smartynas /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
104*49393c00Smartynas * where z = 2(x-1)/(x+1)
105*49393c00Smartynas * 1/sqrt(2) <= x < sqrt(2)
106*49393c00Smartynas * Theoretical peak relative error = 1.1e-35,
107*49393c00Smartynas * relative peak error spread 1.1e-9
108*49393c00Smartynas */
109*49393c00Smartynas static const long double R[6] =
110*49393c00Smartynas {
111*49393c00Smartynas 1.418134209872192732479751274970992665513E5L,
112*49393c00Smartynas -8.977257995689735303686582344659576526998E4L,
113*49393c00Smartynas 2.048819892795278657810231591630928516206E4L,
114*49393c00Smartynas -2.024301798136027039250415126250455056397E3L,
115*49393c00Smartynas 8.057002716646055371965756206836056074715E1L,
116*49393c00Smartynas -8.828896441624934385266096344596648080902E-1L
117*49393c00Smartynas };
118*49393c00Smartynas static const long double S[6] =
119*49393c00Smartynas {
120*49393c00Smartynas 1.701761051846631278975701529965589676574E6L,
121*49393c00Smartynas -1.332535117259762928288745111081235577029E6L,
122*49393c00Smartynas 4.001557694070773974936904547424676279307E5L,
123*49393c00Smartynas -5.748542087379434595104154610899551484314E4L,
124*49393c00Smartynas 3.998526750980007367835804959888064681098E3L,
125*49393c00Smartynas -1.186359407982897997337150403816839480438E2L
126*49393c00Smartynas /* 1.000000000000000000000000000000000000000E0L, */
127*49393c00Smartynas };
128*49393c00Smartynas
129*49393c00Smartynas static const long double
130*49393c00Smartynas /* log2(e) - 1 */
131*49393c00Smartynas LOG2EA = 4.4269504088896340735992468100189213742664595E-1L,
132*49393c00Smartynas /* sqrt(2)/2 */
133*49393c00Smartynas SQRTH = 7.071067811865475244008443621048490392848359E-1L;
134*49393c00Smartynas
135*49393c00Smartynas
136*49393c00Smartynas /* Evaluate P[n] x^n + P[n-1] x^(n-1) + ... + P[0] */
137*49393c00Smartynas
138*49393c00Smartynas static long double
neval(long double x,const long double * p,int n)139*49393c00Smartynas neval (long double x, const long double *p, int n)
140*49393c00Smartynas {
141*49393c00Smartynas long double y;
142*49393c00Smartynas
143*49393c00Smartynas p += n;
144*49393c00Smartynas y = *p--;
145*49393c00Smartynas do
146*49393c00Smartynas {
147*49393c00Smartynas y = y * x + *p--;
148*49393c00Smartynas }
149*49393c00Smartynas while (--n > 0);
150*49393c00Smartynas return y;
151*49393c00Smartynas }
152*49393c00Smartynas
153*49393c00Smartynas
154*49393c00Smartynas /* Evaluate x^n+1 + P[n] x^(n) + P[n-1] x^(n-1) + ... + P[0] */
155*49393c00Smartynas
156*49393c00Smartynas static long double
deval(long double x,const long double * p,int n)157*49393c00Smartynas deval (long double x, const long double *p, int n)
158*49393c00Smartynas {
159*49393c00Smartynas long double y;
160*49393c00Smartynas
161*49393c00Smartynas p += n;
162*49393c00Smartynas y = x + *p--;
163*49393c00Smartynas do
164*49393c00Smartynas {
165*49393c00Smartynas y = y * x + *p--;
166*49393c00Smartynas }
167*49393c00Smartynas while (--n > 0);
168*49393c00Smartynas return y;
169*49393c00Smartynas }
170*49393c00Smartynas
171*49393c00Smartynas
172*49393c00Smartynas
173*49393c00Smartynas long double
log2l(long double x)174*49393c00Smartynas log2l(long double x)
175*49393c00Smartynas {
176*49393c00Smartynas long double z;
177*49393c00Smartynas long double y;
178*49393c00Smartynas int e;
179*49393c00Smartynas int64_t hx, lx;
180*49393c00Smartynas
181*49393c00Smartynas /* Test for domain */
182*49393c00Smartynas GET_LDOUBLE_WORDS64 (hx, lx, x);
183*49393c00Smartynas if (((hx & 0x7fffffffffffffffLL) | lx) == 0)
184*49393c00Smartynas return (-1.0L / (x - x));
185*49393c00Smartynas if (hx < 0)
186*49393c00Smartynas return (x - x) / (x - x);
187*49393c00Smartynas if (hx >= 0x7fff000000000000LL)
188*49393c00Smartynas return (x + x);
189*49393c00Smartynas
190*49393c00Smartynas /* separate mantissa from exponent */
191*49393c00Smartynas
192*49393c00Smartynas /* Note, frexp is used so that denormal numbers
193*49393c00Smartynas * will be handled properly.
194*49393c00Smartynas */
195*49393c00Smartynas x = frexpl (x, &e);
196*49393c00Smartynas
197*49393c00Smartynas
198*49393c00Smartynas /* logarithm using log(x) = z + z**3 P(z)/Q(z),
199*49393c00Smartynas * where z = 2(x-1)/x+1)
200*49393c00Smartynas */
201*49393c00Smartynas if ((e > 2) || (e < -2))
202*49393c00Smartynas {
203*49393c00Smartynas if (x < SQRTH)
204*49393c00Smartynas { /* 2( 2x-1 )/( 2x+1 ) */
205*49393c00Smartynas e -= 1;
206*49393c00Smartynas z = x - 0.5L;
207*49393c00Smartynas y = 0.5L * z + 0.5L;
208*49393c00Smartynas }
209*49393c00Smartynas else
210*49393c00Smartynas { /* 2 (x-1)/(x+1) */
211*49393c00Smartynas z = x - 0.5L;
212*49393c00Smartynas z -= 0.5L;
213*49393c00Smartynas y = 0.5L * x + 0.5L;
214*49393c00Smartynas }
215*49393c00Smartynas x = z / y;
216*49393c00Smartynas z = x * x;
217*49393c00Smartynas y = x * (z * neval (z, R, 5) / deval (z, S, 5));
218*49393c00Smartynas goto done;
219*49393c00Smartynas }
220*49393c00Smartynas
221*49393c00Smartynas
222*49393c00Smartynas /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
223*49393c00Smartynas
224*49393c00Smartynas if (x < SQRTH)
225*49393c00Smartynas {
226*49393c00Smartynas e -= 1;
227*49393c00Smartynas x = 2.0 * x - 1.0L; /* 2x - 1 */
228*49393c00Smartynas }
229*49393c00Smartynas else
230*49393c00Smartynas {
231*49393c00Smartynas x = x - 1.0L;
232*49393c00Smartynas }
233*49393c00Smartynas z = x * x;
234*49393c00Smartynas y = x * (z * neval (x, P, 12) / deval (x, Q, 11));
235*49393c00Smartynas y = y - 0.5 * z;
236*49393c00Smartynas
237*49393c00Smartynas done:
238*49393c00Smartynas
239*49393c00Smartynas /* Multiply log of fraction by log2(e)
240*49393c00Smartynas * and base 2 exponent by 1
241*49393c00Smartynas */
242*49393c00Smartynas z = y * LOG2EA;
243*49393c00Smartynas z += x * LOG2EA;
244*49393c00Smartynas z += y;
245*49393c00Smartynas z += x;
246*49393c00Smartynas z += e;
247*49393c00Smartynas return (z);
248*49393c00Smartynas }
249