Merge C++ from gomp-20050608-branch.
[official-gcc.git] / libgcc-math / flt-32 / e_expf.c
blobfeaac183af5b6424316807184a6c064ce51e6dc2
1 /* Single-precision floating point e^x.
2 Copyright (C) 1997, 1998, 2005, 2006 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 /* How this works:
23 The input value, x, is written as
25 x = n * ln(2) + t/512 + delta[t] + x;
27 where:
28 - n is an integer, 127 >= n >= -150;
29 - t is an integer, 177 >= t >= -177
30 - delta is based on a table entry, delta[t] < 2^-28
31 - x is whatever is left, |x| < 2^-10
33 Then e^x is approximated as
35 e^x = 2^n ( e^(t/512 + delta[t])
36 + ( e^(t/512 + delta[t])
37 * ( p(x + delta[t] + n * ln(2)) - delta ) ) )
39 where
40 - p(x) is a polynomial approximating e(x)-1;
41 - e^(t/512 + delta[t]) is obtained from a table.
43 The table used is the same one as for the double precision version;
44 since we have the table, we might as well use it.
46 It turns out to be faster to do calculations in double precision than
47 to perform an 'accurate table method' expf, because of the range reduction
48 overhead (compare exp2f).
50 #ifndef _GNU_SOURCE
51 #define _GNU_SOURCE
52 #endif
53 #include <float.h>
54 #include <ieee754.h>
55 #include <math_private.h>
57 extern const float __exp_deltatable[178];
58 extern const double __exp_atable[355] /* __attribute__((mode(DF))) */;
60 static const volatile float TWOM100 = 7.88860905e-31;
61 static const volatile float TWO127 = 1.7014118346e+38;
63 float
64 __ieee754_expf (float x)
66 static const float himark = 88.72283935546875;
67 static const float lomark = -103.972084045410;
68 /* Check for usual case. */
69 if (__builtin_isless (x, himark) && __builtin_isgreater (x, lomark))
71 static const float THREEp42 = 13194139533312.0;
72 static const float THREEp22 = 12582912.0;
73 /* 1/ln(2). */
74 #undef M_1_LN2
75 static const float M_1_LN2 = 1.44269502163f;
76 /* ln(2) */
77 #undef M_LN2
78 static const double M_LN2 = .6931471805599452862;
80 int tval;
81 double x22, t, result, dx;
82 float n, delta;
83 union ieee754_double ex2_u;
85 /* Calculate n. */
86 n = x * M_1_LN2 + THREEp22;
87 n -= THREEp22;
88 dx = x - n*M_LN2;
90 /* Calculate t/512. */
91 t = dx + THREEp42;
92 t -= THREEp42;
93 dx -= t;
95 /* Compute tval = t. */
96 tval = (int) (t * 512.0);
98 if (t >= 0)
99 delta = - __exp_deltatable[tval];
100 else
101 delta = __exp_deltatable[-tval];
103 /* Compute ex2 = 2^n e^(t/512+delta[t]). */
104 ex2_u.d = __exp_atable[tval+177];
105 ex2_u.ieee.exponent += (int) n;
107 /* Approximate e^(dx+delta) - 1, using a second-degree polynomial,
108 with maximum error in [-2^-10-2^-28,2^-10+2^-28]
109 less than 5e-11. */
110 x22 = (0.5000000496709180453 * dx + 1.0000001192102037084) * dx + delta;
112 /* Return result. */
113 result = x22 * ex2_u.d + ex2_u.d;
114 return (float) result;
116 /* Exceptional cases: */
117 else if (__builtin_isless (x, himark))
119 if (__isinff (x))
120 /* e^-inf == 0, with no error. */
121 return 0;
122 else
123 /* Underflow */
124 return TWOM100 * TWOM100;
126 else
127 /* Return x, if x is a NaN or Inf; or overflow, otherwise. */
128 return TWO127*x;