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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [language/] [c/] [libm/] [v2_0/] [src/] [double/] [ieee754-core/] [e_exp.c] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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