Update.
[glibc.git] / sysdeps / libm-ieee754 / e_exp.c
blob5eae12a19c4d0636351e403badec952add6e6aec
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. */
21 /* How this works:
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
31 where:
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)
44 - n*ln(2)_1
45 - n*ln(2)_1 * p(x + n * ln(2)_1) ) ) )
47 where
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
53 is omitted.
55 #ifndef _GNU_SOURCE
56 #define _GNU_SOURCE
57 #endif
58 #include <float.h>
59 #include <ieee754.h>
60 #include <math.h>
61 #include <fenv.h>
62 #include <inttypes.h>
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;
71 double
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;
81 /* 1/ln(2). */
82 static const double M_1_LN2 = 1.442695040888963387;
83 /* ln(2), part 1 */
84 static const double M_LN2_0 = .6931471805598903302;
85 /* ln(2), part 2 */
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;
91 fenv_t oldenv;
93 feholdexcept (&oldenv);
94 fesetround (FE_TONEAREST);
96 /* Calculate n. */
97 n = x * M_1_LN2 + THREEp51;
98 n -= THREEp51;
99 x = x - n*M_LN2_0;
101 /* Calculate t/512. */
102 t = x + THREEp42;
103 t -= THREEp42;
104 x -= t;
106 /* Compute tval = t. */
107 tval = (int) (t * 512.0);
109 if (t >= 0)
110 x -= __exp_deltatable[tval];
111 else
112 x += __exp_deltatable[-tval];
114 /* Now, the variable x contains x + n*ln(2)_1. */
115 dely = n*M_LN2_1;
117 /* Compute ex2 = 2^n_0 e^(t/512+delta[t]). */
118 ex2_u.d = __exp_atable[tval+177];
119 n_i = (int)n;
120 /* 'unsafe' is 1 iff n_1 != 0. */
121 unsafe = abs(n_i) >= -DBL_MIN_EXP - 1;
122 ex2_u.ieee.exponent += n_i >> unsafe;
124 /* Compute scale = 2^n_1. */
125 scale_u.d = 1.0;
126 scale_u.ieee.exponent += n_i - (n_i >> unsafe);
128 /* Approximate e^x2 - 1, using a fourth-degree polynomial,
129 with maximum error in [-2^-10-2^-28,2^-10+2^-28]
130 less than 4.9e-19. */
131 x22 = (((0.04166666898464281565
132 * x + 0.1666666766008501610)
133 * x + 0.499999999999990008)
134 * x + 0.9999999999999976685) * x;
135 /* Allow for impact of dely. */
136 x22 -= dely + dely*x22;
138 /* Return result. */
139 fesetenv (&oldenv);
141 result = x22 * ex2_u.d + ex2_u.d;
142 if (!unsafe)
143 return result;
144 else
145 return result * scale_u.d;
147 /* Exceptional cases: */
148 else if (isless (x, himark))
150 if (__isinf (x))
151 /* e^-inf == 0, with no error. */
152 return 0;
153 else
154 /* Underflow */
155 return TWOM1000 * TWOM1000;
157 else
158 /* Return x, if x is a NaN or Inf; or overflow, otherwise. */
159 return TWO1023*x;