1 /* Double-precision floating point e^x.
2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Geoffrey Keating <geoffk@ozemail.com.au>
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 The basic design here is from
23 Shmuel Gal and Boris Bachelis, "An Accurate Elementary Mathematical
24 Library for the IEEE Floating Point Standard", ACM Trans. Math. Soft.,
25 17 (1), March 1991, pp. 26-45.
27 The input value, x, is written as
29 x = n * ln(2)_0 + t/512 + delta[t] + x + n * ln(2)_1
32 - n is an integer, 1024 >= n >= -1075;
33 - ln(2)_0 is the first 43 bits of ln(2), and ln(2)_1 is the remainder, so
34 that |ln(2)_1| < 2^-32;
35 - t is an integer, 177 >= t >= -177
36 - delta is based on a table entry, delta[t] < 2^-28
37 - x is whatever is left, |x| < 2^-10
39 Then e^x is approximated as
41 e^x = 2^n_1 ( 2^n_0 e^(t/512 + delta[t])
42 + ( 2^n_0 e^(t/512 + delta[t])
43 * ( p(x + n * ln(2)_1)
45 - n*ln(2)_1 * p(x + n * ln(2)_1) ) ) )
48 - p(x) is a polynomial approximating e(x)-1;
49 - e^(t/512 + delta[t]) is obtained from a table;
50 - n_1 + n_0 = n, so that |n_0| < DBL_MIN_EXP-1.
52 If it happens that n_1 == 0 (this is the usual case), that multiplication
63 #include <math_private.h>
65 extern const float __exp_deltatable
[178];
66 extern const double __exp_atable
[355] /* __attribute__((mode(DF))) */;
68 static const volatile double TWO1023
= 8.988465674311579539e+307;
69 static const volatile double TWOM1000
= 9.3326361850321887899e-302;
72 __ieee754_exp (double x
)
74 static const double himark
= 709.7827128933840868;
75 static const double lomark
= -745.1332191019412221;
76 /* Check for usual case. */
77 if (isless (x
, himark
) && isgreater (x
, lomark
))
79 static const double THREEp42
= 13194139533312.0;
80 static const double THREEp51
= 6755399441055744.0;
82 static const double M_1_LN2
= 1.442695040888963387;
84 static const double M_LN2_0
= .6931471805598903302;
86 static const double M_LN2_1
= 5.497923018708371155e-14;
88 int tval
, unsafe
, n_i
;
89 double x22
, n
, t
, dely
, result
;
90 union ieee754_double ex2_u
, scale_u
;
93 feholdexcept (&oldenv
);
95 fesetround (FE_TONEAREST
);
99 n
= x
* M_1_LN2
+ THREEp51
;
103 /* Calculate t/512. */
108 /* Compute tval = t. */
109 tval
= (int) (t
* 512.0);
112 x
-= __exp_deltatable
[tval
];
114 x
+= __exp_deltatable
[-tval
];
116 /* Now, the variable x contains x + n*ln(2)_1. */
119 /* Compute ex2 = 2^n_0 e^(t/512+delta[t]). */
120 ex2_u
.d
= __exp_atable
[tval
+177];
122 /* 'unsafe' is 1 iff n_1 != 0. */
123 unsafe
= abs(n_i
) >= -DBL_MIN_EXP
- 1;
124 ex2_u
.ieee
.exponent
+= n_i
>> unsafe
;
126 /* Compute scale = 2^n_1. */
128 scale_u
.ieee
.exponent
+= n_i
- (n_i
>> unsafe
);
130 /* Approximate e^x2 - 1, using a fourth-degree polynomial,
131 with maximum error in [-2^-10-2^-28,2^-10+2^-28]
132 less than 4.9e-19. */
133 x22
= (((0.04166666898464281565
134 * x
+ 0.1666666766008501610)
135 * x
+ 0.499999999999990008)
136 * x
+ 0.9999999999999976685) * x
;
137 /* Allow for impact of dely. */
138 x22
-= dely
+ dely
*x22
;
143 result
= x22
* ex2_u
.d
+ ex2_u
.d
;
147 return result
* scale_u
.d
;
149 /* Exceptional cases: */
150 else if (isless (x
, himark
))
153 /* e^-inf == 0, with no error. */
157 return TWOM1000
* TWOM1000
;
160 /* Return x, if x is a NaN or Inf; or overflow, otherwise. */