1*181254a7Smrg /* Quad-precision floating point e^x.
2*181254a7Smrg Copyright (C) 1999-2018 Free Software Foundation, Inc.
3*181254a7Smrg This file is part of the GNU C Library.
4*181254a7Smrg Contributed by Jakub Jelinek <jj@ultra.linux.cz>
5*181254a7Smrg Partly based on double-precision code
6*181254a7Smrg by Geoffrey Keating <geoffk@ozemail.com.au>
7*181254a7Smrg
8*181254a7Smrg The GNU C Library is free software; you can redistribute it and/or
9*181254a7Smrg modify it under the terms of the GNU Lesser General Public
10*181254a7Smrg License as published by the Free Software Foundation; either
11*181254a7Smrg version 2.1 of the License, or (at your option) any later version.
12*181254a7Smrg
13*181254a7Smrg The GNU C Library is distributed in the hope that it will be useful,
14*181254a7Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
15*181254a7Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16*181254a7Smrg Lesser General Public License for more details.
17*181254a7Smrg
18*181254a7Smrg You should have received a copy of the GNU Lesser General Public
19*181254a7Smrg License along with the GNU C Library; if not, see
20*181254a7Smrg <http://www.gnu.org/licenses/>. */
21*181254a7Smrg
22*181254a7Smrg /* The basic design here is from
23*181254a7Smrg Abraham Ziv, "Fast Evaluation of Elementary Mathematical Functions with
24*181254a7Smrg Correctly Rounded Last Bit", ACM Trans. Math. Soft., 17 (3), September 1991,
25*181254a7Smrg pp. 410-423.
26*181254a7Smrg
27*181254a7Smrg We work with number pairs where the first number is the high part and
28*181254a7Smrg the second one is the low part. Arithmetic with the high part numbers must
29*181254a7Smrg be exact, without any roundoff errors.
30*181254a7Smrg
31*181254a7Smrg The input value, X, is written as
32*181254a7Smrg X = n * ln(2)_0 + arg1[t1]_0 + arg2[t2]_0 + x
33*181254a7Smrg - n * ln(2)_1 + arg1[t1]_1 + arg2[t2]_1 + xl
34*181254a7Smrg
35*181254a7Smrg where:
36*181254a7Smrg - n is an integer, 16384 >= n >= -16495;
37*181254a7Smrg - ln(2)_0 is the first 93 bits of ln(2), and |ln(2)_0-ln(2)-ln(2)_1| < 2^-205
38*181254a7Smrg - t1 is an integer, 89 >= t1 >= -89
39*181254a7Smrg - t2 is an integer, 65 >= t2 >= -65
40*181254a7Smrg - |arg1[t1]-t1/256.0| < 2^-53
41*181254a7Smrg - |arg2[t2]-t2/32768.0| < 2^-53
42*181254a7Smrg - x + xl is whatever is left, |x + xl| < 2^-16 + 2^-53
43*181254a7Smrg
44*181254a7Smrg Then e^x is approximated as
45*181254a7Smrg
46*181254a7Smrg e^x = 2^n_1 ( 2^n_0 e^(arg1[t1]_0 + arg1[t1]_1) e^(arg2[t2]_0 + arg2[t2]_1)
47*181254a7Smrg + 2^n_0 e^(arg1[t1]_0 + arg1[t1]_1) e^(arg2[t2]_0 + arg2[t2]_1)
48*181254a7Smrg * p (x + xl + n * ln(2)_1))
49*181254a7Smrg where:
50*181254a7Smrg - p(x) is a polynomial approximating e(x)-1
51*181254a7Smrg - e^(arg1[t1]_0 + arg1[t1]_1) is obtained from a table
52*181254a7Smrg - e^(arg2[t2]_0 + arg2[t2]_1) likewise
53*181254a7Smrg - n_1 + n_0 = n, so that |n_0| < -FLT128_MIN_EXP-1.
54*181254a7Smrg
55*181254a7Smrg If it happens that n_1 == 0 (this is the usual case), that multiplication
56*181254a7Smrg is omitted.
57*181254a7Smrg */
58*181254a7Smrg
59*181254a7Smrg #ifndef _GNU_SOURCE
60*181254a7Smrg #define _GNU_SOURCE
61*181254a7Smrg #endif
62*181254a7Smrg
63*181254a7Smrg #include "quadmath-imp.h"
64*181254a7Smrg #include "expq_table.h"
65*181254a7Smrg
66*181254a7Smrg static const __float128 C[] = {
67*181254a7Smrg /* Smallest integer x for which e^x overflows. */
68*181254a7Smrg #define himark C[0]
69*181254a7Smrg 11356.523406294143949491931077970765Q,
70*181254a7Smrg
71*181254a7Smrg /* Largest integer x for which e^x underflows. */
72*181254a7Smrg #define lomark C[1]
73*181254a7Smrg -11433.4627433362978788372438434526231Q,
74*181254a7Smrg
75*181254a7Smrg /* 3x2^96 */
76*181254a7Smrg #define THREEp96 C[2]
77*181254a7Smrg 59421121885698253195157962752.0Q,
78*181254a7Smrg
79*181254a7Smrg /* 3x2^103 */
80*181254a7Smrg #define THREEp103 C[3]
81*181254a7Smrg 30423614405477505635920876929024.0Q,
82*181254a7Smrg
83*181254a7Smrg /* 3x2^111 */
84*181254a7Smrg #define THREEp111 C[4]
85*181254a7Smrg 7788445287802241442795744493830144.0Q,
86*181254a7Smrg
87*181254a7Smrg /* 1/ln(2) */
88*181254a7Smrg #define M_1_LN2 C[5]
89*181254a7Smrg 1.44269504088896340735992468100189204Q,
90*181254a7Smrg
91*181254a7Smrg /* first 93 bits of ln(2) */
92*181254a7Smrg #define M_LN2_0 C[6]
93*181254a7Smrg 0.693147180559945309417232121457981864Q,
94*181254a7Smrg
95*181254a7Smrg /* ln2_0 - ln(2) */
96*181254a7Smrg #define M_LN2_1 C[7]
97*181254a7Smrg -1.94704509238074995158795957333327386E-31Q,
98*181254a7Smrg
99*181254a7Smrg /* very small number */
100*181254a7Smrg #define TINY C[8]
101*181254a7Smrg 1.0e-4900Q,
102*181254a7Smrg
103*181254a7Smrg /* 2^16383 */
104*181254a7Smrg #define TWO16383 C[9]
105*181254a7Smrg 5.94865747678615882542879663314003565E+4931Q,
106*181254a7Smrg
107*181254a7Smrg /* 256 */
108*181254a7Smrg #define TWO8 C[10]
109*181254a7Smrg 256,
110*181254a7Smrg
111*181254a7Smrg /* 32768 */
112*181254a7Smrg #define TWO15 C[11]
113*181254a7Smrg 32768,
114*181254a7Smrg
115*181254a7Smrg /* Chebyshev polynom coefficients for (exp(x)-1)/x */
116*181254a7Smrg #define P1 C[12]
117*181254a7Smrg #define P2 C[13]
118*181254a7Smrg #define P3 C[14]
119*181254a7Smrg #define P4 C[15]
120*181254a7Smrg #define P5 C[16]
121*181254a7Smrg #define P6 C[17]
122*181254a7Smrg 0.5Q,
123*181254a7Smrg 1.66666666666666666666666666666666683E-01Q,
124*181254a7Smrg 4.16666666666666666666654902320001674E-02Q,
125*181254a7Smrg 8.33333333333333333333314659767198461E-03Q,
126*181254a7Smrg 1.38888888889899438565058018857254025E-03Q,
127*181254a7Smrg 1.98412698413981650382436541785404286E-04Q,
128*181254a7Smrg };
129*181254a7Smrg
130*181254a7Smrg __float128
expq(__float128 x)131*181254a7Smrg expq (__float128 x)
132*181254a7Smrg {
133*181254a7Smrg /* Check for usual case. */
134*181254a7Smrg if (__builtin_isless (x, himark) && __builtin_isgreater (x, lomark))
135*181254a7Smrg {
136*181254a7Smrg int tval1, tval2, unsafe, n_i;
137*181254a7Smrg __float128 x22, n, t, result, xl;
138*181254a7Smrg ieee854_float128 ex2_u, scale_u;
139*181254a7Smrg fenv_t oldenv;
140*181254a7Smrg
141*181254a7Smrg feholdexcept (&oldenv);
142*181254a7Smrg #ifdef FE_TONEAREST
143*181254a7Smrg fesetround (FE_TONEAREST);
144*181254a7Smrg #endif
145*181254a7Smrg
146*181254a7Smrg /* Calculate n. */
147*181254a7Smrg n = x * M_1_LN2 + THREEp111;
148*181254a7Smrg n -= THREEp111;
149*181254a7Smrg x = x - n * M_LN2_0;
150*181254a7Smrg xl = n * M_LN2_1;
151*181254a7Smrg
152*181254a7Smrg /* Calculate t/256. */
153*181254a7Smrg t = x + THREEp103;
154*181254a7Smrg t -= THREEp103;
155*181254a7Smrg
156*181254a7Smrg /* Compute tval1 = t. */
157*181254a7Smrg tval1 = (int) (t * TWO8);
158*181254a7Smrg
159*181254a7Smrg x -= __expq_table[T_EXPL_ARG1+2*tval1];
160*181254a7Smrg xl -= __expq_table[T_EXPL_ARG1+2*tval1+1];
161*181254a7Smrg
162*181254a7Smrg /* Calculate t/32768. */
163*181254a7Smrg t = x + THREEp96;
164*181254a7Smrg t -= THREEp96;
165*181254a7Smrg
166*181254a7Smrg /* Compute tval2 = t. */
167*181254a7Smrg tval2 = (int) (t * TWO15);
168*181254a7Smrg
169*181254a7Smrg x -= __expq_table[T_EXPL_ARG2+2*tval2];
170*181254a7Smrg xl -= __expq_table[T_EXPL_ARG2+2*tval2+1];
171*181254a7Smrg
172*181254a7Smrg x = x + xl;
173*181254a7Smrg
174*181254a7Smrg /* Compute ex2 = 2^n_0 e^(argtable[tval1]) e^(argtable[tval2]). */
175*181254a7Smrg ex2_u.value = __expq_table[T_EXPL_RES1 + tval1]
176*181254a7Smrg * __expq_table[T_EXPL_RES2 + tval2];
177*181254a7Smrg n_i = (int)n;
178*181254a7Smrg /* 'unsafe' is 1 iff n_1 != 0. */
179*181254a7Smrg unsafe = abs(n_i) >= 15000;
180*181254a7Smrg ex2_u.ieee.exponent += n_i >> unsafe;
181*181254a7Smrg
182*181254a7Smrg /* Compute scale = 2^n_1. */
183*181254a7Smrg scale_u.value = 1;
184*181254a7Smrg scale_u.ieee.exponent += n_i - (n_i >> unsafe);
185*181254a7Smrg
186*181254a7Smrg /* Approximate e^x2 - 1, using a seventh-degree polynomial,
187*181254a7Smrg with maximum error in [-2^-16-2^-53,2^-16+2^-53]
188*181254a7Smrg less than 4.8e-39. */
189*181254a7Smrg x22 = x + x*x*(P1+x*(P2+x*(P3+x*(P4+x*(P5+x*P6)))));
190*181254a7Smrg math_force_eval (x22);
191*181254a7Smrg
192*181254a7Smrg /* Return result. */
193*181254a7Smrg fesetenv (&oldenv);
194*181254a7Smrg
195*181254a7Smrg result = x22 * ex2_u.value + ex2_u.value;
196*181254a7Smrg
197*181254a7Smrg /* Now we can test whether the result is ultimate or if we are unsure.
198*181254a7Smrg In the later case we should probably call a mpn based routine to give
199*181254a7Smrg the ultimate result.
200*181254a7Smrg Empirically, this routine is already ultimate in about 99.9986% of
201*181254a7Smrg cases, the test below for the round to nearest case will be false
202*181254a7Smrg in ~ 99.9963% of cases.
203*181254a7Smrg Without proc2 routine maximum error which has been seen is
204*181254a7Smrg 0.5000262 ulp.
205*181254a7Smrg
206*181254a7Smrg ieee854_float128 ex3_u;
207*181254a7Smrg
208*181254a7Smrg #ifdef FE_TONEAREST
209*181254a7Smrg fesetround (FE_TONEAREST);
210*181254a7Smrg #endif
211*181254a7Smrg ex3_u.value = (result - ex2_u.value) - x22 * ex2_u.value;
212*181254a7Smrg ex2_u.value = result;
213*181254a7Smrg ex3_u.ieee.exponent += FLT128_MANT_DIG + 15 + IEEE854_FLOAT128_BIAS
214*181254a7Smrg - ex2_u.ieee.exponent;
215*181254a7Smrg n_i = abs (ex3_u.value);
216*181254a7Smrg n_i = (n_i + 1) / 2;
217*181254a7Smrg fesetenv (&oldenv);
218*181254a7Smrg #ifdef FE_TONEAREST
219*181254a7Smrg if (fegetround () == FE_TONEAREST)
220*181254a7Smrg n_i -= 0x4000;
221*181254a7Smrg #endif
222*181254a7Smrg if (!n_i) {
223*181254a7Smrg return __ieee754_expl_proc2 (origx);
224*181254a7Smrg }
225*181254a7Smrg */
226*181254a7Smrg if (!unsafe)
227*181254a7Smrg return result;
228*181254a7Smrg else
229*181254a7Smrg {
230*181254a7Smrg result *= scale_u.value;
231*181254a7Smrg math_check_force_underflow_nonneg (result);
232*181254a7Smrg return result;
233*181254a7Smrg }
234*181254a7Smrg }
235*181254a7Smrg /* Exceptional cases: */
236*181254a7Smrg else if (__builtin_isless (x, himark))
237*181254a7Smrg {
238*181254a7Smrg if (isinfq (x))
239*181254a7Smrg /* e^-inf == 0, with no error. */
240*181254a7Smrg return 0;
241*181254a7Smrg else
242*181254a7Smrg /* Underflow */
243*181254a7Smrg return TINY * TINY;
244*181254a7Smrg }
245*181254a7Smrg else
246*181254a7Smrg /* Return x, if x is a NaN or Inf; or overflow, otherwise. */
247*181254a7Smrg return TWO16383*x;
248*181254a7Smrg }
249