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