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_log1p.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//===========================================================================
2
//
3
//      s_log1p.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
/* @(#)s_log1p.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
/* double log1p(double x)
76
 *
77
 * Method :
78
 *   1. Argument Reduction: find k and f such that
79
 *                      1+x = 2^k * (1+f),
80
 *         where  sqrt(2)/2 < 1+f < sqrt(2) .
81
 *
82
 *      Note. If k=0, then f=x is exact. However, if k!=0, then f
83
 *      may not be representable exactly. In that case, a correction
84
 *      term is need. Let u=1+x rounded. Let c = (1+x)-u, then
85
 *      log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
86
 *      and add back the correction term c/u.
87
 *      (Note: when x > 2**53, one can simply return log(x))
88
 *
89
 *   2. Approximation of log1p(f).
90
 *      Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
91
 *               = 2s + 2/3 s**3 + 2/5 s**5 + .....,
92
 *               = 2s + s*R
93
 *      We use a special Reme algorithm on [0,0.1716] to generate
94
 *      a polynomial of degree 14 to approximate R The maximum error
95
 *      of this polynomial approximation is bounded by 2**-58.45. In
96
 *      other words,
97
 *                      2      4      6      8      10      12      14
98
 *          R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s  +Lp6*s  +Lp7*s
99
 *      (the values of Lp1 to Lp7 are listed in the program)
100
 *      and
101
 *          |      2          14          |     -58.45
102
 *          | Lp1*s +...+Lp7*s    -  R(z) | <= 2
103
 *          |                             |
104
 *      Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
105
 *      In order to guarantee error in log below 1ulp, we compute log
106
 *      by
107
 *              log1p(f) = f - (hfsq - s*(hfsq+R)).
108
 *
109
 *      3. Finally, log1p(x) = k*ln2 + log1p(f).
110
 *                           = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
111
 *         Here ln2 is split into two floating point number:
112
 *                      ln2_hi + ln2_lo,
113
 *         where n*ln2_hi is always exact for |n| < 2000.
114
 *
115
 * Special cases:
116
 *      log1p(x) is NaN with signal if x < -1 (including -INF) ;
117
 *      log1p(+INF) is +INF; log1p(-1) is -INF with signal;
118
 *      log1p(NaN) is that NaN with no signal.
119
 *
120
 * Accuracy:
121
 *      according to an error analysis, the error is always less than
122
 *      1 ulp (unit in the last place).
123
 *
124
 * Constants:
125
 * The hexadecimal values are the intended ones for the following
126
 * constants. The decimal values may be used, provided that the
127
 * compiler will convert from decimal to binary accurately enough
128
 * to produce the hexadecimal values shown.
129
 *
130
 * Note: Assuming log() return accurate answer, the following
131
 *       algorithm can be used to compute log1p(x) to within a few ULP:
132
 *
133
 *              u = 1+x;
134
 *              if(u==1.0) return x ; else
135
 *                         return log(u)*(x/(u-1.0));
136
 *
137
 *       See HP-15C Advanced Functions Handbook, p.193.
138
 */
139
 
140
#include "mathincl/fdlibm.h"
141
 
142
static const double
143
ln2_hi  =  6.93147180369123816490e-01,  /* 3fe62e42 fee00000 */
144
ln2_lo  =  1.90821492927058770002e-10,  /* 3dea39ef 35793c76 */
145
two54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
146
Lp1 = 6.666666666666735130e-01,  /* 3FE55555 55555593 */
147
Lp2 = 3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
148
Lp3 = 2.857142874366239149e-01,  /* 3FD24924 94229359 */
149
Lp4 = 2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
150
Lp5 = 1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
151
Lp6 = 1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
152
Lp7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
153
 
154
static double zero = 0.0;
155
 
156
        double log1p(double x)
157
{
158
        double hfsq,f,c,s,z,R,u;
159
        int k,hx,hu,ax;
160
 
161
        c=f=hu=0.0; /* to placate compiler */
162
        hx = CYG_LIBM_HI(x);            /* high word of x */
163
        ax = hx&0x7fffffff;
164
 
165
        k = 1;
166
        if (hx < 0x3FDA827A) {                  /* x < 0.41422  */
167
            if(ax>=0x3ff00000) {                /* x <= -1.0 */
168
                if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */
169
                else return (x-x)/(x-x);        /* log1p(x<-1)=NaN */
170
            }
171
            if(ax<0x3e200000) {                 /* |x| < 2**-29 */
172
                if(two54+x>zero                 /* raise inexact */
173
                    &&ax<0x3c900000)            /* |x| < 2**-54 */
174
                    return x;
175
                else
176
                    return x - x*x*0.5;
177
            }
178
            if(hx>0||hx<=((int)0xbfd2bec3)) {
179
                k=0;f=x;hu=1;}  /* -0.2929<x<0.41422 */
180
        }
181
        if (hx >= 0x7ff00000) return x+x;
182
        if(k!=0) {
183
            if(hx<0x43400000) {
184
                u  = 1.0+x;
185
                hu = CYG_LIBM_HI(u);            /* high word of u */
186
                k  = (hu>>20)-1023;
187
                c  = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */
188
                c /= u;
189
            } else {
190
                u  = x;
191
                hu = CYG_LIBM_HI(u);            /* high word of u */
192
                k  = (hu>>20)-1023;
193
                c  = 0;
194
            }
195
            hu &= 0x000fffff;
196
            if(hu<0x6a09e) {
197
                CYG_LIBM_HI(u) = hu|0x3ff00000; /* normalize u */
198
            } else {
199
                k += 1;
200
                CYG_LIBM_HI(u) = hu|0x3fe00000; /* normalize u/2 */
201
                hu = (0x00100000-hu)>>2;
202
            }
203
            f = u-1.0;
204
        }
205
        hfsq=0.5*f*f;
206
        if(hu==0) {     /* |f| < 2**-20 */
207
            if(f==zero) {
208
                if(k==0) return zero;
209
                else {
210
                    c += k*ln2_lo; return k*ln2_hi+c;
211
                }
212
            }
213
            R = hfsq*(1.0-0.66666666666666666*f);
214
            if(k==0) return f-R;
215
            else return k*ln2_hi-((R-(k*ln2_lo+c))-f);
216
        }
217
        s = f/(2.0+f);
218
        z = s*s;
219
        R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
220
        if(k==0) return f-(hfsq-s*(hfsq+R));
221
        else return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
222
}
223
 
224
#endif // ifdef CYGPKG_LIBM     
225
 
226
// EOF s_log1p.c

powered by: WebSVN 2.1.0

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