1 /* Quad-precision floating point e^x.
2 Copyright (C) 1999 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jj@ultra.linux.cz>
5 Partly based on double-precision code
6 by Geoffrey Keating <geoffk@ozemail.com.au>
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 /* The basic design here is from
24 Abraham Ziv, "Fast Evaluation of Elementary Mathematical Functions with
25 Correctly Rounded Last Bit", ACM Trans. Math. Soft., 17 (3), September 1991,
28 We work with number pairs where the first number is the high part and
29 the second one is the low part. Arithmetic with the high part numbers must
30 be exact, without any roundoff errors.
32 The input value, X, is written as
33 X = n * ln(2)_0 + arg1[t1]_0 + arg2[t2]_0 + x
34 - n * ln(2)_1 + arg1[t1]_1 + arg2[t2]_1 + xl
37 - n is an integer, 16384 >= n >= -16495;
38 - ln(2)_0 is the first 93 bits of ln(2), and |ln(2)_0-ln(2)-ln(2)_1| < 2^-205
39 - t1 is an integer, 89 >= t1 >= -89
40 - t2 is an integer, 65 >= t2 >= -65
41 - |arg1[t1]-t1/256.0| < 2^-53
42 - |arg2[t2]-t2/32768.0| < 2^-53
43 - x + xl is whatever is left, |x + xl| < 2^-16 + 2^-53
45 Then e^x is approximated as
47 e^x = 2^n_1 ( 2^n_0 e^(arg1[t1]_0 + arg1[t1]_1) e^(arg2[t2]_0 + arg2[t2]_1)
48 + 2^n_0 e^(arg1[t1]_0 + arg1[t1]_1) e^(arg2[t2]_0 + arg2[t2]_1)
49 * p (x + xl + n * ln(2)_1))
51 - p(x) is a polynomial approximating e(x)-1
52 - e^(arg1[t1]_0 + arg1[t1]_1) is obtained from a table
53 - e^(arg2[t2]_0 + arg2[t2]_1) likewise
54 - n_1 + n_0 = n, so that |n_0| < -LDBL_MIN_EXP-1.
56 If it happens that n_1 == 0 (this is the usual case), that multiplication
68 #include <math_private.h>
72 static const long double C
[] = {
73 /* Smallest integer x for which e^x overflows. */
75 11356.523406294143949491931077970765L,
77 /* Largest integer x for which e^x underflows. */
79 -11433.4627433362978788372438434526231L,
83 59421121885698253195157962752.0L,
86 #define THREEp103 C[3]
87 30423614405477505635920876929024.0L,
90 #define THREEp111 C[4]
91 7788445287802241442795744493830144.0L,
95 1.44269504088896340735992468100189204L,
97 /* first 93 bits of ln(2) */
99 0.693147180559945309417232121457981864L,
103 -1.94704509238074995158795957333327386E-31L,
105 /* very small number */
110 #define TWO16383 C[9]
111 5.94865747678615882542879663314003565E+4931L,
121 /* Chebyshev polynom coeficients for (exp(x)-1)/x */
129 1.66666666666666666666666666666666683E-01L,
130 4.16666666666666666666654902320001674E-02L,
131 8.33333333333333333333314659767198461E-03L,
132 1.38888888889899438565058018857254025E-03L,
133 1.98412698413981650382436541785404286E-04L,
137 __ieee754_expl (long double x
)
139 /* Check for usual case. */
140 if (isless (x
, himark
) && isgreater (x
, lomark
))
142 int tval1
, tval2
, unsafe
, n_i
;
143 long double x22
, n
, t
, result
, xl
;
144 union ieee854_long_double ex2_u
, scale_u
;
147 feholdexcept (&oldenv
);
149 fesetround (FE_TONEAREST
);
153 n
= x
* M_1_LN2
+ THREEp111
;
158 /* Calculate t/256. */
162 /* Compute tval1 = t. */
163 tval1
= (int) (t
* TWO8
);
165 x
-= __expl_table
[T_EXPL_ARG1
+2*tval1
];
166 xl
-= __expl_table
[T_EXPL_ARG1
+2*tval1
+1];
168 /* Calculate t/32768. */
172 /* Compute tval2 = t. */
173 tval2
= (int) (t
* TWO15
);
175 x
-= __expl_table
[T_EXPL_ARG2
+2*tval2
];
176 xl
-= __expl_table
[T_EXPL_ARG2
+2*tval2
+1];
180 /* Compute ex2 = 2^n_0 e^(argtable[tval1]) e^(argtable[tval2]). */
181 ex2_u
.d
= __expl_table
[T_EXPL_RES1
+ tval1
]
182 * __expl_table
[T_EXPL_RES2
+ tval2
];
184 /* 'unsafe' is 1 iff n_1 != 0. */
185 unsafe
= abs(n_i
) >= -LDBL_MIN_EXP
- 1;
186 ex2_u
.ieee
.exponent
+= n_i
>> unsafe
;
188 /* Compute scale = 2^n_1. */
190 scale_u
.ieee
.exponent
+= n_i
- (n_i
>> unsafe
);
192 /* Approximate e^x2 - 1, using a seventh-degree polynomial,
193 with maximum error in [-2^-16-2^-53,2^-16+2^-53]
194 less than 4.8e-39. */
195 x22
= x
+ x
*x
*(P1
+x
*(P2
+x
*(P3
+x
*(P4
+x
*(P5
+x
*P6
)))));
200 result
= x22
* ex2_u
.d
+ ex2_u
.d
;
202 /* Now we can test whether the result is ultimate or if we are unsure.
203 In the later case we should probably call a mpn based routine to give
205 Empirically, this routine is already ultimate in about 99.9986% of
206 cases, the test below for the round to nearest case will be false
207 in ~ 99.9963% of cases.
208 Without proc2 routine maximum error which has been seen is
211 union ieee854_long_double ex3_u;
214 fesetround (FE_TONEAREST);
216 ex3_u.d = (result - ex2_u.d) - x22 * ex2_u.d;
218 ex3_u.ieee.exponent += LDBL_MANT_DIG + 15 + IEEE854_LONG_DOUBLE_BIAS
219 - ex2_u.ieee.exponent;
224 if (fegetround () == FE_TONEAREST)
228 return __ieee754_expl_proc2 (origx);
234 return result
* scale_u
.d
;
236 /* Exceptional cases: */
237 else if (isless (x
, himark
))
240 /* e^-inf == 0, with no error. */
247 /* Return x, if x is a NaN or Inf; or overflow, otherwise. */