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/] [portable-api/] [s_expm1.c] - Blame information for rev 794

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//===========================================================================
2
//
3
//      s_expm1.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: 
44
// Date:         2001-07-20
45
// Purpose:     
46
// Description: 
47
// Usage:       
48
//
49
//####DESCRIPTIONEND####
50
//
51
//===========================================================================
52
 
53
// CONFIGURATION
54
 
55
#include <pkgconf/libm.h>   // Configuration header
56
 
57
 
58
/* @(#)s_expm1.c 5.1 93/09/24 */
59
/*
60
 * ====================================================
61
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
62
 *
63
 * Developed at SunPro, a Sun Microsystems, Inc. business.
64
 * Permission to use, copy, modify, and distribute this
65
 * software is freely granted, provided that this notice
66
 * is preserved.
67
 * ====================================================
68
 */
69
 
70
/*
71
FUNCTION
72
        <<expm1>>, <<expm1f>>---exponential minus 1
73
INDEX
74
        expm1
75
INDEX
76
        expm1f
77
 
78
ANSI_SYNOPSIS
79
        #include <math.h>
80
        double expm1(double <[x]>);
81
        float expm1f(float <[x]>);
82
 
83
TRAD_SYNOPSIS
84
        #include <math.h>
85
        double expm1(<[x]>);
86
        double <[x]>;
87
 
88
        float expm1f(<[x]>);
89
        float <[x]>;
90
 
91
DESCRIPTION
92
        <<expm1>> and <<expm1f>> calculate the exponential of <[x]>
93
        and subtract 1, that is,
94
        @ifinfo
95
        e raised to the power <[x]> minus 1 (where e
96
        @end ifinfo
97
        @tex
98
        $e^x - 1$ (where $e$
99
        @end tex
100
        is the base of the natural system of logarithms, approximately
101
        2.71828).  The result is accurate even for small values of
102
        <[x]>, where using <<exp(<[x]>)-1>> would lose many
103
        significant digits.
104
 
105
RETURNS
106
        e raised to the power <[x]>, minus 1.
107
 
108
PORTABILITY
109
        Neither <<expm1>> nor <<expm1f>> is required by ANSI C or by
110
        the System V Interface Definition (Issue 2).
111
*/
112
 
113
/* expm1(x)
114
 * Returns exp(x)-1, the exponential of x minus 1.
115
 *
116
 * Method
117
 *   1. Argument reduction:
118
 *      Given x, find r and integer k such that
119
 *
120
 *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
121
 *
122
 *      Here a correction term c will be computed to compensate
123
 *      the error in r when rounded to a floating-point number.
124
 *
125
 *   2. Approximating expm1(r) by a special rational function on
126
 *      the interval [0,0.34658]:
127
 *      Since
128
 *          r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
129
 *      we define R1(r*r) by
130
 *          r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
131
 *      That is,
132
 *          R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
133
 *                   = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
134
 *                   = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
135
 *      We use a special Reme algorithm on [0,0.347] to generate
136
 *      a polynomial of degree 5 in r*r to approximate R1. The
137
 *      maximum error of this polynomial approximation is bounded
138
 *      by 2**-61. In other words,
139
 *          R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
140
 *      where   Q1  =  -1.6666666666666567384E-2,
141
 *              Q2  =   3.9682539681370365873E-4,
142
 *              Q3  =  -9.9206344733435987357E-6,
143
 *              Q4  =   2.5051361420808517002E-7,
144
 *              Q5  =  -6.2843505682382617102E-9;
145
 *      (where z=r*r, and the values of Q1 to Q5 are listed below)
146
 *      with error bounded by
147
 *          |                  5           |     -61
148
 *          | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2
149
 *          |                              |
150
 *
151
 *      expm1(r) = exp(r)-1 is then computed by the following
152
 *      specific way which minimize the accumulation rounding error:
153
 *                             2     3
154
 *                            r     r    [ 3 - (R1 + R1*r/2)  ]
155
 *            expm1(r) = r + --- + --- * [--------------------]
156
 *                            2     2    [ 6 - r*(3 - R1*r/2) ]
157
 *
158
 *      To compensate the error in the argument reduction, we use
159
 *              expm1(r+c) = expm1(r) + c + expm1(r)*c
160
 *                         ~ expm1(r) + c + r*c
161
 *      Thus c+r*c will be added in as the correction terms for
162
 *      expm1(r+c). Now rearrange the term to avoid optimization
163
 *      screw up:
164
 *                      (      2                                    2 )
165
 *                      ({  ( r    [ R1 -  (3 - R1*r/2) ]  )  }    r  )
166
 *       expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
167
 *                      ({  ( 2    [ 6 - r*(3 - R1*r/2) ]  )  }    2  )
168
 *                      (                                             )
169
 *
170
 *                 = r - E
171
 *   3. Scale back to obtain expm1(x):
172
 *      From step 1, we have
173
 *         expm1(x) = either 2^k*[expm1(r)+1] - 1
174
 *                  = or     2^k*[expm1(r) + (1-2^-k)]
175
 *   4. Implementation notes:
176
 *      (A). To save one multiplication, we scale the coefficient Qi
177
 *           to Qi*2^i, and replace z by (x^2)/2.
178
 *      (B). To achieve maximum accuracy, we compute expm1(x) by
179
 *        (i)   if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
180
 *        (ii)  if k=0, return r-E
181
 *        (iii) if k=-1, return 0.5*(r-E)-0.5
182
 *        (iv)  if k=1 if r < -0.25, return 2*((r+0.5)- E)
183
 *                     else          return  1.0+2.0*(r-E);
184
 *        (v)   if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
185
 *        (vi)  if k <= 20, return 2^k((1-2^-k)-(E-r)), else
186
 *        (vii) return 2^k(1-((E+2^-k)-r))
187
 *
188
 * Special cases:
189
 *      expm1(INF) is INF, expm1(NaN) is NaN;
190
 *      expm1(-INF) is -1, and
191
 *      for finite argument, only expm1(0)=0 is exact.
192
 *
193
 * Accuracy:
194
 *      according to an error analysis, the error is always less than
195
 *      1 ulp (unit in the last place).
196
 *
197
 * Misc. info.
198
 *      For IEEE double
199
 *          if x >  7.09782712893383973096e+02 then expm1(x) overflow
200
 *
201
 * Constants:
202
 * The hexadecimal values are the intended ones for the following
203
 * constants. The decimal values may be used, provided that the
204
 * compiler will convert from decimal to binary accurately enough
205
 * to produce the hexadecimal values shown.
206
 */
207
 
208
#include "mathincl/fdlibm.h"
209
 
210
#ifndef _DOUBLE_IS_32BITS
211
 
212
#ifdef __STDC__
213
static const double
214
#else
215
static double
216
#endif
217
one             = 1.0,
218
huge            = 1.0e+300,
219
tiny            = 1.0e-300,
220
o_threshold     = 7.09782712893383973096e+02,/* 0x40862E42, 0xFEFA39EF */
221
ln2_hi          = 6.93147180369123816490e-01,/* 0x3fe62e42, 0xfee00000 */
222
ln2_lo          = 1.90821492927058770002e-10,/* 0x3dea39ef, 0x35793c76 */
223
invln2          = 1.44269504088896338700e+00,/* 0x3ff71547, 0x652b82fe */
224
        /* scaled coefficients related to expm1 */
225
Q1  =  -3.33333333333331316428e-02, /* BFA11111 111110F4 */
226
Q2  =   1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
227
Q3  =  -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
228
Q4  =   4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
229
Q5  =  -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */
230
 
231
#ifdef __STDC__
232
        double expm1(double x)
233
#else
234
        double expm1(x)
235
        double x;
236
#endif
237
{
238
        double y,hi,lo,c,t,e,hxs,hfx,r1;
239
        __int32_t k,xsb;
240
        __uint32_t hx;
241
 
242
        GET_HIGH_WORD(hx,x);
243
        xsb = hx&0x80000000;            /* sign bit of x */
244
        if(xsb==0) y=x; else y= -x;      /* y = |x| */
245
        hx &= 0x7fffffff;               /* high word of |x| */
246
 
247
    /* filter out huge and non-finite argument */
248
        if(hx >= 0x4043687A) {                  /* if |x|>=56*ln2 */
249
            if(hx >= 0x40862E42) {              /* if |x|>=709.78... */
250
                if(hx>=0x7ff00000) {
251
                    __uint32_t low;
252
                    GET_LOW_WORD(low,x);
253
                    if(((hx&0xfffff)|low)!=0)
254
                         return x+x;     /* NaN */
255
                    else return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */
256
                }
257
                if(x > o_threshold) return huge*huge; /* overflow */
258
            }
259
            if(xsb!=0) { /* x < -56*ln2, return -1.0 with inexact */
260
                if(x+tiny<0.0)          /* raise inexact */
261
                return tiny-one;        /* return -1 */
262
            }
263
        }
264
 
265
    /* argument reduction */
266
        if(hx > 0x3fd62e42) {           /* if  |x| > 0.5 ln2 */
267
            if(hx < 0x3FF0A2B2) {       /* and |x| < 1.5 ln2 */
268
                if(xsb==0)
269
                    {hi = x - ln2_hi; lo =  ln2_lo;  k =  1;}
270
                else
271
                    {hi = x + ln2_hi; lo = -ln2_lo;  k = -1;}
272
            } else {
273
                k  = invln2*x+((xsb==0)?0.5:-0.5);
274
                t  = k;
275
                hi = x - t*ln2_hi;      /* t*ln2_hi is exact here */
276
                lo = t*ln2_lo;
277
            }
278
            x  = hi - lo;
279
            c  = (hi-x)-lo;
280
        }
281
        else if(hx < 0x3c900000) {      /* when |x|<2**-54, return x */
282
            t = huge+x; /* return x with inexact flags when x!=0 */
283
            return x - (t-(huge+x));
284
        }
285
        else k = 0, c = 0;
286
 
287
    /* x is now in primary range */
288
        hfx = 0.5*x;
289
        hxs = x*hfx;
290
        r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))));
291
        t  = 3.0-r1*hfx;
292
        e  = hxs*((r1-t)/(6.0 - x*t));
293
        if(k==0) return x - (x*e-hxs);           /* c is 0 */
294
        else {
295
            e  = (x*(e-c)-c);
296
            e -= hxs;
297
            if(k== -1) return 0.5*(x-e)-0.5;
298
          if(k==1) {
299
                if(x < -0.25) return -2.0*(e-(x+0.5));
300
                else          return  one+2.0*(x-e);
301
          }
302
            if (k <= -2 || k>56) {   /* suffice to return exp(x)-1 */
303
                __uint32_t high;
304
                y = one-(e-x);
305
                GET_HIGH_WORD(high,y);
306
                SET_HIGH_WORD(y,high+(k<<20));  /* add k to y's exponent */
307
                return y-one;
308
            }
309
            t = one;
310
            if(k<20) {
311
                __uint32_t high;
312
                SET_HIGH_WORD(t,0x3ff00000 - (0x200000>>k));  /* t=1-2^-k */
313
                y = t-(e-x);
314
                GET_HIGH_WORD(high,y);
315
                SET_HIGH_WORD(y,high+(k<<20));  /* add k to y's exponent */
316
           } else {
317
                __uint32_t high;
318
                SET_HIGH_WORD(t,((0x3ff-k)<<20));       /* 2^-k */
319
                y = x-(e+t);
320
                y += one;
321
                GET_HIGH_WORD(high,y);
322
                SET_HIGH_WORD(y,high+(k<<20));  /* add k to y's exponent */
323
            }
324
        }
325
        return y;
326
}
327
 
328
#endif /* _DOUBLE_IS_32BITS */
329
 
330
// EOF s_expm1.c

powered by: WebSVN 2.1.0

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