xref: /haiku/src/system/libroot/posix/musl/math/expl.c (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1*f504f610SAugustin Cavalier /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_expl.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  *      Exponential function, long double precision
19*f504f610SAugustin Cavalier  *
20*f504f610SAugustin Cavalier  *
21*f504f610SAugustin Cavalier  * SYNOPSIS:
22*f504f610SAugustin Cavalier  *
23*f504f610SAugustin Cavalier  * long double x, y, expl();
24*f504f610SAugustin Cavalier  *
25*f504f610SAugustin Cavalier  * y = expl( x );
26*f504f610SAugustin Cavalier  *
27*f504f610SAugustin Cavalier  *
28*f504f610SAugustin Cavalier  * DESCRIPTION:
29*f504f610SAugustin Cavalier  *
30*f504f610SAugustin Cavalier  * Returns e (2.71828...) raised to the x power.
31*f504f610SAugustin Cavalier  *
32*f504f610SAugustin Cavalier  * Range reduction is accomplished by separating the argument
33*f504f610SAugustin Cavalier  * into an integer k and fraction f such that
34*f504f610SAugustin Cavalier  *
35*f504f610SAugustin Cavalier  *     x    k  f
36*f504f610SAugustin Cavalier  *    e  = 2  e.
37*f504f610SAugustin Cavalier  *
38*f504f610SAugustin Cavalier  * A Pade' form of degree 5/6 is used to approximate exp(f) - 1
39*f504f610SAugustin Cavalier  * in the basic range [-0.5 ln 2, 0.5 ln 2].
40*f504f610SAugustin Cavalier  *
41*f504f610SAugustin Cavalier  *
42*f504f610SAugustin Cavalier  * ACCURACY:
43*f504f610SAugustin Cavalier  *
44*f504f610SAugustin Cavalier  *                      Relative error:
45*f504f610SAugustin Cavalier  * arithmetic   domain     # trials      peak         rms
46*f504f610SAugustin Cavalier  *    IEEE      +-10000     50000       1.12e-19    2.81e-20
47*f504f610SAugustin Cavalier  *
48*f504f610SAugustin Cavalier  *
49*f504f610SAugustin Cavalier  * Error amplification in the exponential function can be
50*f504f610SAugustin Cavalier  * a serious matter.  The error propagation involves
51*f504f610SAugustin Cavalier  * exp( X(1+delta) ) = exp(X) ( 1 + X*delta + ... ),
52*f504f610SAugustin Cavalier  * which shows that a 1 lsb error in representing X produces
53*f504f610SAugustin Cavalier  * a relative error of X times 1 lsb in the function.
54*f504f610SAugustin Cavalier  * While the routine gives an accurate result for arguments
55*f504f610SAugustin Cavalier  * that are exactly represented by a long double precision
56*f504f610SAugustin Cavalier  * computer number, the result contains amplified roundoff
57*f504f610SAugustin Cavalier  * error for large arguments not exactly represented.
58*f504f610SAugustin Cavalier  *
59*f504f610SAugustin Cavalier  *
60*f504f610SAugustin Cavalier  * ERROR MESSAGES:
61*f504f610SAugustin Cavalier  *
62*f504f610SAugustin Cavalier  *   message         condition      value returned
63*f504f610SAugustin Cavalier  * exp underflow    x < MINLOG         0.0
64*f504f610SAugustin Cavalier  * exp overflow     x > MAXLOG         MAXNUM
65*f504f610SAugustin Cavalier  *
66*f504f610SAugustin Cavalier  */
67*f504f610SAugustin Cavalier 
68*f504f610SAugustin Cavalier #include "libm.h"
69*f504f610SAugustin Cavalier 
70*f504f610SAugustin Cavalier #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
expl(long double x)71*f504f610SAugustin Cavalier long double expl(long double x)
72*f504f610SAugustin Cavalier {
73*f504f610SAugustin Cavalier 	return exp(x);
74*f504f610SAugustin Cavalier }
75*f504f610SAugustin Cavalier #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
76*f504f610SAugustin Cavalier 
77*f504f610SAugustin Cavalier static const long double P[3] = {
78*f504f610SAugustin Cavalier  1.2617719307481059087798E-4L,
79*f504f610SAugustin Cavalier  3.0299440770744196129956E-2L,
80*f504f610SAugustin Cavalier  9.9999999999999999991025E-1L,
81*f504f610SAugustin Cavalier };
82*f504f610SAugustin Cavalier static const long double Q[4] = {
83*f504f610SAugustin Cavalier  3.0019850513866445504159E-6L,
84*f504f610SAugustin Cavalier  2.5244834034968410419224E-3L,
85*f504f610SAugustin Cavalier  2.2726554820815502876593E-1L,
86*f504f610SAugustin Cavalier  2.0000000000000000000897E0L,
87*f504f610SAugustin Cavalier };
88*f504f610SAugustin Cavalier static const long double
89*f504f610SAugustin Cavalier LN2HI = 6.9314575195312500000000E-1L,
90*f504f610SAugustin Cavalier LN2LO = 1.4286068203094172321215E-6L,
91*f504f610SAugustin Cavalier LOG2E = 1.4426950408889634073599E0L;
92*f504f610SAugustin Cavalier 
expl(long double x)93*f504f610SAugustin Cavalier long double expl(long double x)
94*f504f610SAugustin Cavalier {
95*f504f610SAugustin Cavalier 	long double px, xx;
96*f504f610SAugustin Cavalier 	int k;
97*f504f610SAugustin Cavalier 
98*f504f610SAugustin Cavalier 	if (isnan(x))
99*f504f610SAugustin Cavalier 		return x;
100*f504f610SAugustin Cavalier 	if (x > 11356.5234062941439488L) /* x > ln(2^16384 - 0.5) */
101*f504f610SAugustin Cavalier 		return x * 0x1p16383L;
102*f504f610SAugustin Cavalier 	if (x < -11399.4985314888605581L) /* x < ln(2^-16446) */
103*f504f610SAugustin Cavalier 		return -0x1p-16445L/x;
104*f504f610SAugustin Cavalier 
105*f504f610SAugustin Cavalier 	/* Express e**x = e**f 2**k
106*f504f610SAugustin Cavalier 	 *   = e**(f + k ln(2))
107*f504f610SAugustin Cavalier 	 */
108*f504f610SAugustin Cavalier 	px = floorl(LOG2E * x + 0.5);
109*f504f610SAugustin Cavalier 	k = px;
110*f504f610SAugustin Cavalier 	x -= px * LN2HI;
111*f504f610SAugustin Cavalier 	x -= px * LN2LO;
112*f504f610SAugustin Cavalier 
113*f504f610SAugustin Cavalier 	/* rational approximation of the fractional part:
114*f504f610SAugustin Cavalier 	 * e**x =  1 + 2x P(x**2)/(Q(x**2) - x P(x**2))
115*f504f610SAugustin Cavalier 	 */
116*f504f610SAugustin Cavalier 	xx = x * x;
117*f504f610SAugustin Cavalier 	px = x * __polevll(xx, P, 2);
118*f504f610SAugustin Cavalier 	x = px/(__polevll(xx, Q, 3) - px);
119*f504f610SAugustin Cavalier 	x = 1.0 + 2.0 * x;
120*f504f610SAugustin Cavalier 	return scalbnl(x, k);
121*f504f610SAugustin Cavalier }
122*f504f610SAugustin Cavalier #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
123*f504f610SAugustin Cavalier // TODO: broken implementation to make things compile
expl(long double x)124*f504f610SAugustin Cavalier long double expl(long double x)
125*f504f610SAugustin Cavalier {
126*f504f610SAugustin Cavalier 	return exp(x);
127*f504f610SAugustin Cavalier }
128*f504f610SAugustin Cavalier #endif
129