OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [c/] [libm/] [current/] [src/] [double/] [ieee754-core/] [e_exp.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//===========================================================================
2
//
3
//      e_exp.c
4
//
5
//      Part of the standard mathematical function library
6
//
7
//===========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//===========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):   jlarmour
43
// Contributors:  jlarmour
44
// Date:        1998-02-13
45
// Purpose:     
46
// Description: 
47
// Usage:       
48
//
49
//####DESCRIPTIONEND####
50
//
51
//===========================================================================
52
 
53
// CONFIGURATION
54
 
55
#include <pkgconf/libm.h>   // Configuration header
56
 
57
// Include the Math library?
58
#ifdef CYGPKG_LIBM     
59
 
60
// Derived from code with the following copyright
61
 
62
 
63
/* @(#)e_exp.c 1.3 95/01/18 */
64
/*
65
 * ====================================================
66
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
67
 *
68
 * Developed at SunSoft, a Sun Microsystems, Inc. business.
69
 * Permission to use, copy, modify, and distribute this
70
 * software is freely granted, provided that this notice
71
 * is preserved.
72
 * ====================================================
73
 */
74
 
75
/* __ieee754_exp(x)
76
 * Returns the exponential of x.
77
 *
78
 * Method
79
 *   1. Argument reduction:
80
 *      Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
81
 *      Given x, find r and integer k such that
82
 *
83
 *               x = k*ln2 + r,  |r| <= 0.5*ln2.
84
 *
85
 *      Here r will be represented as r = hi-lo for better
86
 *      accuracy.
87
 *
88
 *   2. Approximation of exp(r) by a special rational function on
89
 *      the interval [0,0.34658]:
90
 *      Write
91
 *          R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
92
 *      We use a special Reme algorithm on [0,0.34658] to generate
93
 *      a polynomial of degree 5 to approximate R. The maximum error
94
 *      of this polynomial approximation is bounded by 2**-59. In
95
 *      other words,
96
 *          R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
97
 *      (where z=r*r, and the values of P1 to P5 are listed below)
98
 *      and
99
 *          |                  5          |     -59
100
 *          | 2.0+P1*z+...+P5*z   -  R(z) | <= 2
101
 *          |                             |
102
 *      The computation of exp(r) thus becomes
103
 *                             2*r
104
 *              exp(r) = 1 + -------
105
 *                            R - r
106
 *                                 r*R1(r)
107
 *                     = 1 + r + ----------- (for better accuracy)
108
 *                                2 - R1(r)
109
 *      where
110
 *                               2       4             10
111
 *              R1(r) = r - (P1*r  + P2*r  + ... + P5*r   ).
112
 *
113
 *   3. Scale back to obtain exp(x):
114
 *      From step 1, we have
115
 *         exp(x) = 2^k * exp(r)
116
 *
117
 * Special cases:
118
 *      exp(INF) is INF, exp(NaN) is NaN;
119
 *      exp(-INF) is 0, and
120
 *      for finite argument, only exp(0)=1 is exact.
121
 *
122
 * Accuracy:
123
 *      according to an error analysis, the error is always less than
124
 *      1 ulp (unit in the last place).
125
 *
126
 * Misc. info.
127
 *      For IEEE double
128
 *          if x >  7.09782712893383973096e+02 then exp(x) overflow
129
 *          if x < -7.45133219101941108420e+02 then exp(x) underflow
130
 *
131
 * Constants:
132
 * The hexadecimal values are the intended ones for the following
133
 * constants. The decimal values may be used, provided that the
134
 * compiler will convert from decimal to binary accurately enough
135
 * to produce the hexadecimal values shown.
136
 */
137
 
138
#include "mathincl/fdlibm.h"
139
 
140
static const double
141
one     = 1.0,
142
halF[2] = {0.5,-0.5,},
143
huge    = 1.0e+300,
144
twom1000= 9.33263618503218878990e-302,     /* 2**-1000=0x01700000,0*/
145
o_threshold=  7.09782712893383973096e+02,  /* 0x40862E42, 0xFEFA39EF */
146
u_threshold= -7.45133219101941108420e+02,  /* 0xc0874910, 0xD52D3051 */
147
ln2HI[2]   ={ 6.93147180369123816490e-01,  /* 0x3fe62e42, 0xfee00000 */
148
             -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */
149
ln2LO[2]   ={ 1.90821492927058770002e-10,  /* 0x3dea39ef, 0x35793c76 */
150
             -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */
151
invln2 =  1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */
152
P1   =  1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
153
P2   = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
154
P3   =  6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
155
P4   = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
156
P5   =  4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
157
 
158
 
159
        double __ieee754_exp(double x)  /* default IEEE double exp */
160
{
161
        double y,hi,lo,c,t;
162
        int k,xsb;
163
        unsigned hx;
164
 
165
        hi=lo=0.0;                      /* to placate compiler */
166
        hx  = CYG_LIBM_HI(x);           /* high word of x */
167
        xsb = (hx>>31)&1;               /* sign bit of x */
168
        hx &= 0x7fffffff;               /* high word of |x| */
169
 
170
    /* filter out non-finite argument */
171
        if(hx >= 0x40862E42) {                  /* if |x|>=709.78... */
172
            if(hx>=0x7ff00000) {
173
                if(((hx&0xfffff)|CYG_LIBM_LO(x))!=0)
174
                     return x+x;                /* NaN */
175
                else return (xsb==0)? x:0.0;    /* exp(+-inf)={inf,0} */
176
            }
177
            if(x > o_threshold) return huge*huge; /* overflow */
178
            if(x < u_threshold) return twom1000*twom1000; /* underflow */
179
        }
180
 
181
    /* argument reduction */
182
        if(hx > 0x3fd62e42) {           /* if  |x| > 0.5 ln2 */
183
            if(hx < 0x3FF0A2B2) {       /* and |x| < 1.5 ln2 */
184
                hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
185
            } else {
186
                k  = invln2*x+halF[xsb];
187
                t  = k;
188
                hi = x - t*ln2HI[0];    /* t*ln2HI is exact here */
189
                lo = t*ln2LO[0];
190
            }
191
            x  = hi - lo;
192
        }
193
        else if(hx < 0x3e300000)  {     /* when |x|<2**-28 */
194
            if(huge+x>one)
195
                return one+x;/* trigger inexact */
196
            else
197
                k = 0;
198
        }
199
        else k = 0;
200
 
201
    /* x is now in primary range */
202
        t  = x*x;
203
        c  = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
204
        if(k==0)        return one-((x*c)/(c-2.0)-x);
205
        else            y = one-((lo-(x*c)/(2.0-c))-hi);
206
        if(k >= -1021) {
207
            CYG_LIBM_HI(y) += (k<<20);  /* add k to y's exponent */
208
            return y;
209
        } else {
210
            CYG_LIBM_HI(y) += ((k+1000)<<20);/* add k to y's exponent */
211
            return y*twom1000;
212
        }
213
}
214
 
215
#endif // ifdef CYGPKG_LIBM     
216
 
217
// EOF e_exp.c

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.