1 /* $OpenBSD: e_expl.c,v 1.3 2013/11/12 20:35:18 martynas Exp $ */
2
3 /*
4 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /* expl.c
20 *
21 * Exponential function, 128-bit long double precision
22 *
23 *
24 *
25 * SYNOPSIS:
26 *
27 * long double x, y, expl();
28 *
29 * y = expl( x );
30 *
31 *
32 *
33 * DESCRIPTION:
34 *
35 * Returns e (2.71828...) raised to the x power.
36 *
37 * Range reduction is accomplished by separating the argument
38 * into an integer k and fraction f such that
39 *
40 * x k f
41 * e = 2 e.
42 *
43 * A Pade' form of degree 2/3 is used to approximate exp(f) - 1
44 * in the basic range [-0.5 ln 2, 0.5 ln 2].
45 *
46 *
47 * ACCURACY:
48 *
49 * Relative error:
50 * arithmetic domain # trials peak rms
51 * IEEE +-MAXLOG 100,000 2.6e-34 8.6e-35
52 *
53 *
54 * Error amplification in the exponential function can be
55 * a serious matter. The error propagation involves
56 * exp( X(1+delta) ) = exp(X) ( 1 + X*delta + ... ),
57 * which shows that a 1 lsb error in representing X produces
58 * a relative error of X times 1 lsb in the function.
59 * While the routine gives an accurate result for arguments
60 * that are exactly represented by a long double precision
61 * computer number, the result contains amplified roundoff
62 * error for large arguments not exactly represented.
63 *
64 *
65 * ERROR MESSAGES:
66 *
67 * message condition value returned
68 * exp underflow x < MINLOG 0.0
69 * exp overflow x > MAXLOG MAXNUM
70 *
71 */
72
73 /* Exponential function */
74
75
76
77 /* Pade' coefficients for exp(x) - 1
78 Theoretical peak relative error = 2.2e-37,
79 relative peak error spread = 9.2e-38
80 */
81 static long double P[5] = {
82 3.279723985560247033712687707263393506266E-10L,
83 6.141506007208645008909088812338454698548E-7L,
84 2.708775201978218837374512615596512792224E-4L,
85 3.508710990737834361215404761139478627390E-2L,
86 9.999999999999999999999999999999999998502E-1L
87 };
88 static long double Q[6] = {
89 2.980756652081995192255342779918052538681E-12L,
90 1.771372078166251484503904874657985291164E-8L,
91 1.504792651814944826817779302637284053660E-5L,
92 3.611828913847589925056132680618007270344E-3L,
93 2.368408864814233538909747618894558968880E-1L,
94 2.000000000000000000000000000000000000150E0L
95 };
96 /* C1 + C2 = ln 2 */
97 static const long double C1 = -6.93145751953125E-1L;
98 static const long double C2 = -1.428606820309417232121458176568075500134E-6L;
99
100 static const long double LOG2EL = 1.442695040888963407359924681001892137426646L;
101 static const long double MAXLOGL = 1.1356523406294143949491931077970764891253E4L;
102 static const long double MINLOGL = -1.143276959615573793352782661133116431383730e4L;
103 static const long double huge = 0x1p10000L;
104 #if 0 /* XXX Prevent gcc from erroneously constant folding this. */
105 static const long double twom10000 = 0x1p-10000L;
106 #else
107 static volatile long double twom10000 = 0x1p-10000L;
108 #endif
109
110 long double
expl(long double x)111 expl(long double x)
112 {
113 long double px, xx;
114 int n;
115
116 if( isnan(x) )
117 return(x + x);
118 if( x > MAXLOGL) {
119 if (isinf(x))
120 return x;
121 return __math_oflowl(0);
122 }
123
124 if( x < MINLOGL ) {
125 if (isinf(x))
126 return 0.0L;
127 return __math_uflowl(0);
128 }
129
130 /* Express e**x = e**g 2**n
131 * = e**g e**( n loge(2) )
132 * = e**( g + n loge(2) )
133 */
134 px = floorl( LOG2EL * x + 0.5L ); /* floor() truncates toward -infinity. */
135 n = px;
136 x += px * C1;
137 x += px * C2;
138 /* rational approximation for exponential
139 * of the fractional part:
140 * e**x = 1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
141 */
142 xx = x * x;
143 px = x * __polevll( xx, P, 4 );
144 xx = __polevll( xx, Q, 5 );
145 x = px/( xx - px );
146 x = 1.0L + x + x;
147
148 x = ldexpl( x, n );
149 return(x);
150 }
151