1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Sun Microsystems, Inc.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
40 /* @(#)s_log1p.c 1.3 95/01/18 */
41 /*
42 * ====================================================
43 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
44 *
45 * Developed at SunSoft, a Sun Microsystems, Inc. business.
46 * Permission to use, copy, modify, and distribute this
47 * software is freely granted, provided that this notice
48 * is preserved.
49 * ====================================================
50 */
52 /* double log1p(double x)
53 *
54 * Method :
55 * 1. Argument Reduction: find k and f such that
56 * 1+x = 2^k * (1+f),
57 * where sqrt(2)/2 < 1+f < sqrt(2) .
58 *
59 * Note. If k=0, then f=x is exact. However, if k!=0, then f
60 * may not be representable exactly. In that case, a correction
61 * term is need. Let u=1+x rounded. Let c = (1+x)-u, then
62 * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
63 * and add back the correction term c/u.
64 * (Note: when x > 2**53, one can simply return log(x))
65 *
66 * 2. Approximation of log1p(f).
67 * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
68 * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
69 * = 2s + s*R
70 * We use a special Reme algorithm on [0,0.1716] to generate
71 * a polynomial of degree 14 to approximate R The maximum error
72 * of this polynomial approximation is bounded by 2**-58.45. In
73 * other words,
74 * 2 4 6 8 10 12 14
75 * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
76 * (the values of Lp1 to Lp7 are listed in the program)
77 * and
78 * | 2 14 | -58.45
79 * | Lp1*s +...+Lp7*s - R(z) | <= 2
80 * | |
81 * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
82 * In order to guarantee error in log below 1ulp, we compute log
83 * by
84 * log1p(f) = f - (hfsq - s*(hfsq+R)).
85 *
86 * 3. Finally, log1p(x) = k*ln2 + log1p(f).
87 * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
88 * Here ln2 is split into two floating point number:
89 * ln2_hi + ln2_lo,
90 * where n*ln2_hi is always exact for |n| < 2000.
91 *
92 * Special cases:
93 * log1p(x) is NaN with signal if x < -1 (including -INF) ;
94 * log1p(+INF) is +INF; log1p(-1) is -INF with signal;
95 * log1p(NaN) is that NaN with no signal.
96 *
97 * Accuracy:
98 * according to an error analysis, the error is always less than
99 * 1 ulp (unit in the last place).
100 *
101 * Constants:
102 * The hexadecimal values are the intended ones for the following
103 * constants. The decimal values may be used, provided that the
104 * compiler will convert from decimal to binary accurately enough
105 * to produce the hexadecimal values shown.
106 *
107 * Note: Assuming log() return accurate answer, the following
108 * algorithm can be used to compute log1p(x) to within a few ULP:
109 *
110 * u = 1+x;
111 * if(u==1.0) return x ; else
112 * return log(u)*(x/(u-1.0));
113 *
114 * See HP-15C Advanced Functions Handbook, p.193.
115 */
117 #include "fdlibm.h"
119 #ifdef __STDC__
120 static const double
121 #else
122 static double
123 #endif
124 ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
125 ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
126 two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
127 Lp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
128 Lp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
129 Lp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
130 Lp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
131 Lp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
132 Lp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
133 Lp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
135 static double zero = 0.0;
137 #ifdef __STDC__
138 double fd_log1p(double x)
139 #else
140 double fd_log1p(x)
141 double x;
142 #endif
143 {
144 double hfsq,f,c,s,z,R,u;
145 int k,hx,hu,ax;
146 fd_twoints un;
148 un.d = x;
149 hx = __HI(un); /* high word of x */
150 ax = hx&0x7fffffff;
152 k = 1;
153 if (hx < 0x3FDA827A) { /* x < 0.41422 */
154 if(ax>=0x3ff00000) { /* x <= -1.0 */
155 if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */
156 else return (x-x)/(x-x); /* log1p(x<-1)=NaN */
157 }
158 if(ax<0x3e200000) { /* |x| < 2**-29 */
159 if(two54+x>zero /* raise inexact */
160 &&ax<0x3c900000) /* |x| < 2**-54 */
161 return x;
162 else
163 return x - x*x*0.5;
164 }
165 if(hx>0||hx<=((int)0xbfd2bec3)) {
166 k=0;f=x;hu=1;} /* -0.2929<x<0.41422 */
167 }
168 if (hx >= 0x7ff00000) return x+x;
169 if(k!=0) {
170 if(hx<0x43400000) {
171 u = 1.0+x;
172 un.d = u;
173 hu = __HI(un); /* high word of u */
174 k = (hu>>20)-1023;
175 c = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */
176 c /= u;
177 } else {
178 u = x;
179 un.d = u;
180 hu = __HI(un); /* high word of u */
181 k = (hu>>20)-1023;
182 c = 0;
183 }
184 hu &= 0x000fffff;
185 if(hu<0x6a09e) {
186 un.d = u;
187 __HI(un) = hu|0x3ff00000; /* normalize u */
188 u = un.d;
189 } else {
190 k += 1;
191 un.d = u;
192 __HI(un) = hu|0x3fe00000; /* normalize u/2 */
193 u = un.d;
194 hu = (0x00100000-hu)>>2;
195 }
196 f = u-1.0;
197 }
198 hfsq=0.5*f*f;
199 if(hu==0) { /* |f| < 2**-20 */
200 if(f==zero) if(k==0) return zero;
201 else {c += k*ln2_lo; return k*ln2_hi+c;}
202 R = hfsq*(1.0-0.66666666666666666*f);
203 if(k==0) return f-R; else
204 return k*ln2_hi-((R-(k*ln2_lo+c))-f);
205 }
206 s = f/(2.0+f);
207 z = s*s;
208 R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
209 if(k==0) return f-(hfsq-s*(hfsq+R)); else
210 return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
211 }