expl: Simplify for platforms where 'long double' == 'double'.
[gnulib.git] / lib / expl.c
1 /* Emulation for expl.
2    Contributed by Paolo Bonzini
3
4    Copyright 2002-2003, 2007, 2009-2011 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 # include <float.h>
37
38 static const long double C[] = {
39 /* Chebyshev polynom coeficients for (exp(x)-1)/x */
40 # define P1 C[0]
41 # define P2 C[1]
42 # define P3 C[2]
43 # define P4 C[3]
44 # define P5 C[4]
45 # define P6 C[5]
46  0.5L,
47  1.66666666666666666666666666666666683E-01L,
48  4.16666666666666666666654902320001674E-02L,
49  8.33333333333333333333314659767198461E-03L,
50  1.38888888889899438565058018857254025E-03L,
51  1.98412698413981650382436541785404286E-04L,
52
53 /* Smallest integer x for which e^x overflows.  */
54 # define himark C[6]
55  11356.523406294143949491931077970765L,
56
57 /* Largest integer x for which e^x underflows.  */
58 # define lomark C[7]
59 -11433.4627433362978788372438434526231L,
60
61 /* very small number */
62 # define TINY C[8]
63  1.0e-4900L,
64
65 /* 2^16383 */
66 # define TWO16383 C[9]
67  5.94865747678615882542879663314003565E+4931L};
68
69 long double
70 expl (long double x)
71 {
72   /* Check for usual case.  */
73   if (x < himark && x > lomark)
74     {
75       int exponent;
76       long double t, x22;
77       int k = 1;
78       long double result = 1.0;
79
80       /* Compute an integer power of e with a granularity of 0.125.  */
81       exponent = (int) floorl (x * 8.0L);
82       x -= exponent / 8.0L;
83
84       if (x > 0.0625)
85         {
86           exponent++;
87           x -= 0.125L;
88         }
89
90       if (exponent < 0)
91         {
92           t = 0.8824969025845954028648921432290507362220L; /* e^-0.25 */
93           exponent = -exponent;
94         }
95       else
96         t = 1.1331484530668263168290072278117938725655L; /* e^0.25 */
97
98       while (exponent)
99         {
100           if (exponent & k)
101             {
102               result *= t;
103               exponent ^= k;
104             }
105           t *= t;
106           k <<= 1;
107         }
108
109       /* Approximate (e^x - 1)/x, using a seventh-degree polynomial,
110          with maximum error in [-2^-16-2^-53,2^-16+2^-53]
111          less than 4.8e-39.  */
112       x22 = x + x*x*(P1+x*(P2+x*(P3+x*(P4+x*(P5+x*P6)))));
113
114       return result + result * x22;
115     }
116   /* Exceptional cases:  */
117   else if (x < himark)
118     {
119       if (x + x == x)
120         /* e^-inf == 0, with no error.  */
121         return 0;
122       else
123         /* Underflow */
124         return TINY * TINY;
125     }
126   else
127     /* Return x, if x is a NaN or Inf; or overflow, otherwise.  */
128     return TWO16383*x;
129 }
130
131 #endif
132
133 #if 0
134 int
135 main (void)
136 {
137   printf ("%.16Lg\n", expl (1.0L));
138   printf ("%.16Lg\n", expl (-1.0L));
139   printf ("%.16Lg\n", expl (2.0L));
140   printf ("%.16Lg\n", expl (4.0L));
141   printf ("%.16Lg\n", expl (-2.0L));
142   printf ("%.16Lg\n", expl (-4.0L));
143   printf ("%.16Lg\n", expl (0.0625L));
144   printf ("%.16Lg\n", expl (0.3L));
145   printf ("%.16Lg\n", expl (0.6L));
146 }
147 #endif