Update.
[glibc.git] / sysdeps / libm-ieee754 / e_exp.c
blobee0b22f6ae01ae9ad3d909a8c80e7e44863c44b7
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 #ifdef FE_TONEAREST
95 fesetround (FE_TONEAREST);
96 #endif
98 /* Calculate n. */
99 n = x * M_1_LN2 + THREEp51;
100 n -= THREEp51;
101 x = x - n*M_LN2_0;
103 /* Calculate t/512. */
104 t = x + THREEp42;
105 t -= THREEp42;
106 x -= t;
108 /* Compute tval = t. */
109 tval = (int) (t * 512.0);
111 if (t >= 0)
112 x -= __exp_deltatable[tval];
113 else
114 x += __exp_deltatable[-tval];
116 /* Now, the variable x contains x + n*ln(2)_1. */
117 dely = n*M_LN2_1;
119 /* Compute ex2 = 2^n_0 e^(t/512+delta[t]). */
120 ex2_u.d = __exp_atable[tval+177];
121 n_i = (int)n;
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. */
127 scale_u.d = 1.0;
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;
140 /* Return result. */
141 fesetenv (&oldenv);
143 result = x22 * ex2_u.d + ex2_u.d;
144 if (!unsafe)
145 return result;
146 else
147 return result * scale_u.d;
149 /* Exceptional cases: */
150 else if (isless (x, himark))
152 if (__isinf (x))
153 /* e^-inf == 0, with no error. */
154 return 0;
155 else
156 /* Underflow */
157 return TWOM1000 * TWOM1000;
159 else
160 /* Return x, if x is a NaN or Inf; or overflow, otherwise. */
161 return TWO1023*x;