1 |
786 |
skrzyp |
//===========================================================================
|
2 |
|
|
//
|
3 |
|
|
// e_log.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 |
|
|
/* @(#)e_log.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 |
|
|
/* __ieee754_log(x)
|
76 |
|
|
* Return the logrithm of x
|
77 |
|
|
*
|
78 |
|
|
* Method :
|
79 |
|
|
* 1. Argument Reduction: find k and f such that
|
80 |
|
|
* x = 2^k * (1+f),
|
81 |
|
|
* where sqrt(2)/2 < 1+f < sqrt(2) .
|
82 |
|
|
*
|
83 |
|
|
* 2. Approximation of log(1+f).
|
84 |
|
|
* Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
|
85 |
|
|
* = 2s + 2/3 s**3 + 2/5 s**5 + .....,
|
86 |
|
|
* = 2s + s*R
|
87 |
|
|
* We use a special Reme algorithm on [0,0.1716] to generate
|
88 |
|
|
* a polynomial of degree 14 to approximate R The maximum error
|
89 |
|
|
* of this polynomial approximation is bounded by 2**-58.45. In
|
90 |
|
|
* other words,
|
91 |
|
|
* 2 4 6 8 10 12 14
|
92 |
|
|
* R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
|
93 |
|
|
* (the values of Lg1 to Lg7 are listed in the program)
|
94 |
|
|
* and
|
95 |
|
|
* | 2 14 | -58.45
|
96 |
|
|
* | Lg1*s +...+Lg7*s - R(z) | <= 2
|
97 |
|
|
* | |
|
98 |
|
|
* Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
|
99 |
|
|
* In order to guarantee error in log below 1ulp, we compute log
|
100 |
|
|
* by
|
101 |
|
|
* log(1+f) = f - s*(f - R) (if f is not too large)
|
102 |
|
|
* log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
|
103 |
|
|
*
|
104 |
|
|
* 3. Finally, log(x) = k*ln2 + log(1+f).
|
105 |
|
|
* = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
|
106 |
|
|
* Here ln2 is split into two floating point number:
|
107 |
|
|
* ln2_hi + ln2_lo,
|
108 |
|
|
* where n*ln2_hi is always exact for |n| < 2000.
|
109 |
|
|
*
|
110 |
|
|
* Special cases:
|
111 |
|
|
* log(x) is NaN with signal if x < 0 (including -INF) ;
|
112 |
|
|
* log(+INF) is +INF; log(0) is -INF with signal;
|
113 |
|
|
* log(NaN) is that NaN with no signal.
|
114 |
|
|
*
|
115 |
|
|
* Accuracy:
|
116 |
|
|
* according to an error analysis, the error is always less than
|
117 |
|
|
* 1 ulp (unit in the last place).
|
118 |
|
|
*
|
119 |
|
|
* Constants:
|
120 |
|
|
* The hexadecimal values are the intended ones for the following
|
121 |
|
|
* constants. The decimal values may be used, provided that the
|
122 |
|
|
* compiler will convert from decimal to binary accurately enough
|
123 |
|
|
* to produce the hexadecimal values shown.
|
124 |
|
|
*/
|
125 |
|
|
|
126 |
|
|
#include "mathincl/fdlibm.h"
|
127 |
|
|
|
128 |
|
|
static const double
|
129 |
|
|
ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
|
130 |
|
|
ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
|
131 |
|
|
two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
|
132 |
|
|
Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
|
133 |
|
|
Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
|
134 |
|
|
Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
|
135 |
|
|
Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
|
136 |
|
|
Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
|
137 |
|
|
Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
|
138 |
|
|
Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
|
139 |
|
|
|
140 |
|
|
static double zero = 0.0;
|
141 |
|
|
|
142 |
|
|
double __ieee754_log(double x)
|
143 |
|
|
{
|
144 |
|
|
double hfsq,f,s,z,R,w,t1,t2,dk;
|
145 |
|
|
int k,hx,i,j;
|
146 |
|
|
unsigned lx;
|
147 |
|
|
|
148 |
|
|
hx = CYG_LIBM_HI(x); /* high word of x */
|
149 |
|
|
lx = CYG_LIBM_LO(x); /* low word of x */
|
150 |
|
|
|
151 |
|
|
k=0;
|
152 |
|
|
if (hx < 0x00100000) { /* x < 2**-1022 */
|
153 |
|
|
if (((hx&0x7fffffff)|lx)==0)
|
154 |
|
|
return -two54/zero; /* log(+-0)=-inf */
|
155 |
|
|
if (hx<0) return (x-x)/zero; /* log(-#) = NaN */
|
156 |
|
|
k -= 54; x *= two54; /* subnormal number, scale up x */
|
157 |
|
|
hx = CYG_LIBM_HI(x); /* high word of x */
|
158 |
|
|
}
|
159 |
|
|
if (hx >= 0x7ff00000) return x+x;
|
160 |
|
|
k += (hx>>20)-1023;
|
161 |
|
|
hx &= 0x000fffff;
|
162 |
|
|
i = (hx+0x95f64)&0x100000;
|
163 |
|
|
CYG_LIBM_HI(x) = hx|(i^0x3ff00000); /* normalize x or x/2 */
|
164 |
|
|
k += (i>>20);
|
165 |
|
|
f = x-1.0;
|
166 |
|
|
if((0x000fffff&(2+hx))<3) { /* |f| < 2**-20 */
|
167 |
|
|
if(f==zero) {
|
168 |
|
|
if(k==0) return zero;
|
169 |
|
|
else {
|
170 |
|
|
dk=(double)k;
|
171 |
|
|
return dk*ln2_hi+dk*ln2_lo;
|
172 |
|
|
}
|
173 |
|
|
}
|
174 |
|
|
R = f*f*(0.5-0.33333333333333333*f);
|
175 |
|
|
if(k==0) return f-R;
|
176 |
|
|
else {
|
177 |
|
|
dk=(double)k;
|
178 |
|
|
return dk*ln2_hi-((R-dk*ln2_lo)-f);
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
s = f/(2.0+f);
|
182 |
|
|
dk = (double)k;
|
183 |
|
|
z = s*s;
|
184 |
|
|
i = hx-0x6147a;
|
185 |
|
|
w = z*z;
|
186 |
|
|
j = 0x6b851-hx;
|
187 |
|
|
t1= w*(Lg2+w*(Lg4+w*Lg6));
|
188 |
|
|
t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
|
189 |
|
|
i |= j;
|
190 |
|
|
R = t2+t1;
|
191 |
|
|
if(i>0) {
|
192 |
|
|
hfsq=0.5*f*f;
|
193 |
|
|
if(k==0) return f-(hfsq-s*(hfsq+R)); else
|
194 |
|
|
return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);
|
195 |
|
|
} else {
|
196 |
|
|
if(k==0) return f-s*(f-R); else
|
197 |
|
|
return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);
|
198 |
|
|
}
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
#endif // ifdef CYGPKG_LIBM
|
202 |
|
|
|
203 |
|
|
// EOF e_log.c
|