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