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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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