59cd6c378ad7eab05b927aa4078380d164b7fba0
[gnulib.git] / lib / expl.c
1 /* Emulation for expl.
2    Contributed by Paolo Bonzini
3
4    Copyright 2002-2003, 2007, 2009-2012 Free Software Foundation, Inc.
5
6    This file is part of gnulib.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program 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
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include <config.h>
22
23 /* Specification.  */
24 #include <math.h>
25
26 #if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
27
28 long double
29 expl (long double x)
30 {
31   return exp (x);
32 }
33
34 #else
35
36 /* Code based on glibc/sysdeps/ieee754/ldbl-128/e_expl.c.  */
37
38 # include <float.h>
39
40 static const long double C[] = {
41 /* Chebyshev polynomial coefficients for (exp(x)-1)/x */
42 # define P1 C[0]
43 # define P2 C[1]
44 # define P3 C[2]
45 # define P4 C[3]
46 # define P5 C[4]
47 # define P6 C[5]
48  0.5L,
49  1.66666666666666666666666666666666683E-01L,
50  4.16666666666666666666654902320001674E-02L,
51  8.33333333333333333333314659767198461E-03L,
52  1.38888888889899438565058018857254025E-03L,
53  1.98412698413981650382436541785404286E-04L,
54
55 /* Smallest integer x for which e^x overflows.  */
56 # define himark C[6]
57  11356.523406294143949491931077970765L,
58
59 /* Largest integer x for which e^x underflows.  */
60 # define lomark C[7]
61 -11433.4627433362978788372438434526231L,
62
63 /* very small number */
64 # define TINY C[8]
65  1.0e-4900L,
66
67 /* 2^16383 */
68 # define TWO16383 C[9]
69  5.94865747678615882542879663314003565E+4931L};
70
71 long double
72 expl (long double x)
73 {
74   /* Check for usual case.  */
75   if (x < himark && x > lomark)
76     {
77       int exponent;
78       long double t, x22;
79       int k = 1;
80       long double result = 1.0;
81
82       /* Compute an integer power of e with a granularity of 0.125.  */
83       exponent = (int) floorl (x * 8.0L);
84       x -= exponent / 8.0L;
85
86       if (x > 0.0625)
87         {
88           exponent++;
89           x -= 0.125L;
90         }
91
92       if (exponent < 0)
93         {
94           t = 0.8824969025845954028648921432290507362220L; /* e^-0.25 */
95           exponent = -exponent;
96         }
97       else
98         t = 1.1331484530668263168290072278117938725655L; /* e^0.25 */
99
100       while (exponent)
101         {
102           if (exponent & k)
103             {
104               result *= t;
105               exponent ^= k;
106             }
107           t *= t;
108           k <<= 1;
109         }
110
111       /* Approximate (e^x - 1)/x, using a seventh-degree polynomial,
112          with maximum error in [-2^-16-2^-53,2^-16+2^-53]
113          less than 4.8e-39.  */
114       x22 = x + x*x*(P1+x*(P2+x*(P3+x*(P4+x*(P5+x*P6)))));
115
116       return result + result * x22;
117     }
118   /* Exceptional cases:  */
119   else if (x < himark)
120     {
121       if (x + x == x)
122         /* e^-inf == 0, with no error.  */
123         return 0;
124       else
125         /* Underflow */
126         return TINY * TINY;
127     }
128   else
129     /* Return x, if x is a NaN or Inf; or overflow, otherwise.  */
130     return TWO16383*x;
131 }
132
133 #endif
134
135 #if 0
136 int
137 main (void)
138 {
139   printf ("%.16Lg\n", expl (1.0L));
140   printf ("%.16Lg\n", expl (-1.0L));
141   printf ("%.16Lg\n", expl (2.0L));
142   printf ("%.16Lg\n", expl (4.0L));
143   printf ("%.16Lg\n", expl (-2.0L));
144   printf ("%.16Lg\n", expl (-4.0L));
145   printf ("%.16Lg\n", expl (0.0625L));
146   printf ("%.16Lg\n", expl (0.3L));
147   printf ("%.16Lg\n", expl (0.6L));
148 }
149 #endif