xref: /titanic_44/usr/src/lib/libast/common/uwin/log.c (revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968)
1*da2e3ebdSchin #include "FEATURE/uwin"
2*da2e3ebdSchin 
3*da2e3ebdSchin #if !_UWIN
4*da2e3ebdSchin 
_STUB_log()5*da2e3ebdSchin void _STUB_log(){}
6*da2e3ebdSchin 
7*da2e3ebdSchin #else
8*da2e3ebdSchin 
9*da2e3ebdSchin /*
10*da2e3ebdSchin  * Copyright (c) 1992, 1993
11*da2e3ebdSchin  *	The Regents of the University of California.  All rights reserved.
12*da2e3ebdSchin  *
13*da2e3ebdSchin  * Redistribution and use in source and binary forms, with or without
14*da2e3ebdSchin  * modification, are permitted provided that the following conditions
15*da2e3ebdSchin  * are met:
16*da2e3ebdSchin  * 1. Redistributions of source code must retain the above copyright
17*da2e3ebdSchin  *    notice, this list of conditions and the following disclaimer.
18*da2e3ebdSchin  * 2. Redistributions in binary form must reproduce the above copyright
19*da2e3ebdSchin  *    notice, this list of conditions and the following disclaimer in the
20*da2e3ebdSchin  *    documentation and/or other materials provided with the distribution.
21*da2e3ebdSchin  * 3. Neither the name of the University nor the names of its contributors
22*da2e3ebdSchin  *    may be used to endorse or promote products derived from this software
23*da2e3ebdSchin  *    without specific prior written permission.
24*da2e3ebdSchin  *
25*da2e3ebdSchin  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*da2e3ebdSchin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*da2e3ebdSchin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*da2e3ebdSchin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*da2e3ebdSchin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*da2e3ebdSchin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*da2e3ebdSchin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*da2e3ebdSchin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*da2e3ebdSchin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*da2e3ebdSchin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*da2e3ebdSchin  * SUCH DAMAGE.
36*da2e3ebdSchin  */
37*da2e3ebdSchin 
38*da2e3ebdSchin #ifndef lint
39*da2e3ebdSchin static char sccsid[] = "@(#)log.c	8.2 (Berkeley) 11/30/93";
40*da2e3ebdSchin #endif /* not lint */
41*da2e3ebdSchin 
42*da2e3ebdSchin #include <math.h>
43*da2e3ebdSchin #include <errno.h>
44*da2e3ebdSchin 
45*da2e3ebdSchin #include "mathimpl.h"
46*da2e3ebdSchin 
47*da2e3ebdSchin /* Table-driven natural logarithm.
48*da2e3ebdSchin  *
49*da2e3ebdSchin  * This code was derived, with minor modifications, from:
50*da2e3ebdSchin  *	Peter Tang, "Table-Driven Implementation of the
51*da2e3ebdSchin  *	Logarithm in IEEE Floating-Point arithmetic." ACM Trans.
52*da2e3ebdSchin  *	Math Software, vol 16. no 4, pp 378-400, Dec 1990).
53*da2e3ebdSchin  *
54*da2e3ebdSchin  * Calculates log(2^m*F*(1+f/F)), |f/j| <= 1/256,
55*da2e3ebdSchin  * where F = j/128 for j an integer in [0, 128].
56*da2e3ebdSchin  *
57*da2e3ebdSchin  * log(2^m) = log2_hi*m + log2_tail*m
58*da2e3ebdSchin  * since m is an integer, the dominant term is exact.
59*da2e3ebdSchin  * m has at most 10 digits (for subnormal numbers),
60*da2e3ebdSchin  * and log2_hi has 11 trailing zero bits.
61*da2e3ebdSchin  *
62*da2e3ebdSchin  * log(F) = logF_hi[j] + logF_lo[j] is in tabular form in log_table.h
63*da2e3ebdSchin  * logF_hi[] + 512 is exact.
64*da2e3ebdSchin  *
65*da2e3ebdSchin  * log(1+f/F) = 2*f/(2*F + f) + 1/12 * (2*f/(2*F + f))**3 + ...
66*da2e3ebdSchin  * the leading term is calculated to extra precision in two
67*da2e3ebdSchin  * parts, the larger of which adds exactly to the dominant
68*da2e3ebdSchin  * m and F terms.
69*da2e3ebdSchin  * There are two cases:
70*da2e3ebdSchin  *	1. when m, j are non-zero (m | j), use absolute
71*da2e3ebdSchin  *	   precision for the leading term.
72*da2e3ebdSchin  *	2. when m = j = 0, |1-x| < 1/256, and log(x) ~= (x-1).
73*da2e3ebdSchin  *	   In this case, use a relative precision of 24 bits.
74*da2e3ebdSchin  * (This is done differently in the original paper)
75*da2e3ebdSchin  *
76*da2e3ebdSchin  * Special cases:
77*da2e3ebdSchin  *	0	return signalling -Inf
78*da2e3ebdSchin  *	neg	return signalling NaN
79*da2e3ebdSchin  *	+Inf	return +Inf
80*da2e3ebdSchin */
81*da2e3ebdSchin 
82*da2e3ebdSchin #if defined(vax) || defined(tahoe)
83*da2e3ebdSchin #define _IEEE		0
84*da2e3ebdSchin #define TRUNC(x)	x = (double) (float) (x)
85*da2e3ebdSchin #else
86*da2e3ebdSchin #define _IEEE		1
87*da2e3ebdSchin #define endian		(((*(int *) &one)) ? 1 : 0)
88*da2e3ebdSchin #define TRUNC(x)	*(((int *) &x) + endian) &= 0xf8000000
89*da2e3ebdSchin #define infnan(x)	0.0
90*da2e3ebdSchin #endif
91*da2e3ebdSchin 
92*da2e3ebdSchin #define N 128
93*da2e3ebdSchin 
94*da2e3ebdSchin /* Table of log(Fj) = logF_head[j] + logF_tail[j], for Fj = 1+j/128.
95*da2e3ebdSchin  * Used for generation of extend precision logarithms.
96*da2e3ebdSchin  * The constant 35184372088832 is 2^45, so the divide is exact.
97*da2e3ebdSchin  * It ensures correct reading of logF_head, even for inaccurate
98*da2e3ebdSchin  * decimal-to-binary conversion routines.  (Everybody gets the
99*da2e3ebdSchin  * right answer for integers less than 2^53.)
100*da2e3ebdSchin  * Values for log(F) were generated using error < 10^-57 absolute
101*da2e3ebdSchin  * with the bc -l package.
102*da2e3ebdSchin */
103*da2e3ebdSchin static double	A1 = 	  .08333333333333178827;
104*da2e3ebdSchin static double	A2 = 	  .01250000000377174923;
105*da2e3ebdSchin static double	A3 =	 .002232139987919447809;
106*da2e3ebdSchin static double	A4 =	.0004348877777076145742;
107*da2e3ebdSchin 
108*da2e3ebdSchin static double logF_head[N+1] = {
109*da2e3ebdSchin 	0.,
110*da2e3ebdSchin 	.007782140442060381246,
111*da2e3ebdSchin 	.015504186535963526694,
112*da2e3ebdSchin 	.023167059281547608406,
113*da2e3ebdSchin 	.030771658666765233647,
114*da2e3ebdSchin 	.038318864302141264488,
115*da2e3ebdSchin 	.045809536031242714670,
116*da2e3ebdSchin 	.053244514518837604555,
117*da2e3ebdSchin 	.060624621816486978786,
118*da2e3ebdSchin 	.067950661908525944454,
119*da2e3ebdSchin 	.075223421237524235039,
120*da2e3ebdSchin 	.082443669210988446138,
121*da2e3ebdSchin 	.089612158689760690322,
122*da2e3ebdSchin 	.096729626458454731618,
123*da2e3ebdSchin 	.103796793681567578460,
124*da2e3ebdSchin 	.110814366340264314203,
125*da2e3ebdSchin 	.117783035656430001836,
126*da2e3ebdSchin 	.124703478501032805070,
127*da2e3ebdSchin 	.131576357788617315236,
128*da2e3ebdSchin 	.138402322859292326029,
129*da2e3ebdSchin 	.145182009844575077295,
130*da2e3ebdSchin 	.151916042025732167530,
131*da2e3ebdSchin 	.158605030176659056451,
132*da2e3ebdSchin 	.165249572895390883786,
133*da2e3ebdSchin 	.171850256926518341060,
134*da2e3ebdSchin 	.178407657472689606947,
135*da2e3ebdSchin 	.184922338493834104156,
136*da2e3ebdSchin 	.191394852999565046047,
137*da2e3ebdSchin 	.197825743329758552135,
138*da2e3ebdSchin 	.204215541428766300668,
139*da2e3ebdSchin 	.210564769107350002741,
140*da2e3ebdSchin 	.216873938300523150246,
141*da2e3ebdSchin 	.223143551314024080056,
142*da2e3ebdSchin 	.229374101064877322642,
143*da2e3ebdSchin 	.235566071312860003672,
144*da2e3ebdSchin 	.241719936886966024758,
145*da2e3ebdSchin 	.247836163904594286577,
146*da2e3ebdSchin 	.253915209980732470285,
147*da2e3ebdSchin 	.259957524436686071567,
148*da2e3ebdSchin 	.265963548496984003577,
149*da2e3ebdSchin 	.271933715484010463114,
150*da2e3ebdSchin 	.277868451003087102435,
151*da2e3ebdSchin 	.283768173130738432519,
152*da2e3ebdSchin 	.289633292582948342896,
153*da2e3ebdSchin 	.295464212893421063199,
154*da2e3ebdSchin 	.301261330578199704177,
155*da2e3ebdSchin 	.307025035294827830512,
156*da2e3ebdSchin 	.312755710004239517729,
157*da2e3ebdSchin 	.318453731118097493890,
158*da2e3ebdSchin 	.324119468654316733591,
159*da2e3ebdSchin 	.329753286372579168528,
160*da2e3ebdSchin 	.335355541920762334484,
161*da2e3ebdSchin 	.340926586970454081892,
162*da2e3ebdSchin 	.346466767346100823488,
163*da2e3ebdSchin 	.351976423156884266063,
164*da2e3ebdSchin 	.357455888922231679316,
165*da2e3ebdSchin 	.362905493689140712376,
166*da2e3ebdSchin 	.368325561158599157352,
167*da2e3ebdSchin 	.373716409793814818840,
168*da2e3ebdSchin 	.379078352934811846353,
169*da2e3ebdSchin 	.384411698910298582632,
170*da2e3ebdSchin 	.389716751140440464951,
171*da2e3ebdSchin 	.394993808240542421117,
172*da2e3ebdSchin 	.400243164127459749579,
173*da2e3ebdSchin 	.405465108107819105498,
174*da2e3ebdSchin 	.410659924985338875558,
175*da2e3ebdSchin 	.415827895143593195825,
176*da2e3ebdSchin 	.420969294644237379543,
177*da2e3ebdSchin 	.426084395310681429691,
178*da2e3ebdSchin 	.431173464818130014464,
179*da2e3ebdSchin 	.436236766774527495726,
180*da2e3ebdSchin 	.441274560805140936281,
181*da2e3ebdSchin 	.446287102628048160113,
182*da2e3ebdSchin 	.451274644139630254358,
183*da2e3ebdSchin 	.456237433481874177232,
184*da2e3ebdSchin 	.461175715122408291790,
185*da2e3ebdSchin 	.466089729924533457960,
186*da2e3ebdSchin 	.470979715219073113985,
187*da2e3ebdSchin 	.475845904869856894947,
188*da2e3ebdSchin 	.480688529345570714212,
189*da2e3ebdSchin 	.485507815781602403149,
190*da2e3ebdSchin 	.490303988045525329653,
191*da2e3ebdSchin 	.495077266798034543171,
192*da2e3ebdSchin 	.499827869556611403822,
193*da2e3ebdSchin 	.504556010751912253908,
194*da2e3ebdSchin 	.509261901790523552335,
195*da2e3ebdSchin 	.513945751101346104405,
196*da2e3ebdSchin 	.518607764208354637958,
197*da2e3ebdSchin 	.523248143765158602036,
198*da2e3ebdSchin 	.527867089620485785417,
199*da2e3ebdSchin 	.532464798869114019908,
200*da2e3ebdSchin 	.537041465897345915436,
201*da2e3ebdSchin 	.541597282432121573947,
202*da2e3ebdSchin 	.546132437597407260909,
203*da2e3ebdSchin 	.550647117952394182793,
204*da2e3ebdSchin 	.555141507540611200965,
205*da2e3ebdSchin 	.559615787935399566777,
206*da2e3ebdSchin 	.564070138285387656651,
207*da2e3ebdSchin 	.568504735352689749561,
208*da2e3ebdSchin 	.572919753562018740922,
209*da2e3ebdSchin 	.577315365035246941260,
210*da2e3ebdSchin 	.581691739635061821900,
211*da2e3ebdSchin 	.586049045003164792433,
212*da2e3ebdSchin 	.590387446602107957005,
213*da2e3ebdSchin 	.594707107746216934174,
214*da2e3ebdSchin 	.599008189645246602594,
215*da2e3ebdSchin 	.603290851438941899687,
216*da2e3ebdSchin 	.607555250224322662688,
217*da2e3ebdSchin 	.611801541106615331955,
218*da2e3ebdSchin 	.616029877215623855590,
219*da2e3ebdSchin 	.620240409751204424537,
220*da2e3ebdSchin 	.624433288012369303032,
221*da2e3ebdSchin 	.628608659422752680256,
222*da2e3ebdSchin 	.632766669570628437213,
223*da2e3ebdSchin 	.636907462236194987781,
224*da2e3ebdSchin 	.641031179420679109171,
225*da2e3ebdSchin 	.645137961373620782978,
226*da2e3ebdSchin 	.649227946625615004450,
227*da2e3ebdSchin 	.653301272011958644725,
228*da2e3ebdSchin 	.657358072709030238911,
229*da2e3ebdSchin 	.661398482245203922502,
230*da2e3ebdSchin 	.665422632544505177065,
231*da2e3ebdSchin 	.669430653942981734871,
232*da2e3ebdSchin 	.673422675212350441142,
233*da2e3ebdSchin 	.677398823590920073911,
234*da2e3ebdSchin 	.681359224807238206267,
235*da2e3ebdSchin 	.685304003098281100392,
236*da2e3ebdSchin 	.689233281238557538017,
237*da2e3ebdSchin 	.693147180560117703862
238*da2e3ebdSchin };
239*da2e3ebdSchin 
240*da2e3ebdSchin static double logF_tail[N+1] = {
241*da2e3ebdSchin 	0.,
242*da2e3ebdSchin 	-.00000000000000543229938420049,
243*da2e3ebdSchin 	 .00000000000000172745674997061,
244*da2e3ebdSchin 	-.00000000000001323017818229233,
245*da2e3ebdSchin 	-.00000000000001154527628289872,
246*da2e3ebdSchin 	-.00000000000000466529469958300,
247*da2e3ebdSchin 	 .00000000000005148849572685810,
248*da2e3ebdSchin 	-.00000000000002532168943117445,
249*da2e3ebdSchin 	-.00000000000005213620639136504,
250*da2e3ebdSchin 	-.00000000000001819506003016881,
251*da2e3ebdSchin 	 .00000000000006329065958724544,
252*da2e3ebdSchin 	 .00000000000008614512936087814,
253*da2e3ebdSchin 	-.00000000000007355770219435028,
254*da2e3ebdSchin 	 .00000000000009638067658552277,
255*da2e3ebdSchin 	 .00000000000007598636597194141,
256*da2e3ebdSchin 	 .00000000000002579999128306990,
257*da2e3ebdSchin 	-.00000000000004654729747598444,
258*da2e3ebdSchin 	-.00000000000007556920687451336,
259*da2e3ebdSchin 	 .00000000000010195735223708472,
260*da2e3ebdSchin 	-.00000000000017319034406422306,
261*da2e3ebdSchin 	-.00000000000007718001336828098,
262*da2e3ebdSchin 	 .00000000000010980754099855238,
263*da2e3ebdSchin 	-.00000000000002047235780046195,
264*da2e3ebdSchin 	-.00000000000008372091099235912,
265*da2e3ebdSchin 	 .00000000000014088127937111135,
266*da2e3ebdSchin 	 .00000000000012869017157588257,
267*da2e3ebdSchin 	 .00000000000017788850778198106,
268*da2e3ebdSchin 	 .00000000000006440856150696891,
269*da2e3ebdSchin 	 .00000000000016132822667240822,
270*da2e3ebdSchin 	-.00000000000007540916511956188,
271*da2e3ebdSchin 	-.00000000000000036507188831790,
272*da2e3ebdSchin 	 .00000000000009120937249914984,
273*da2e3ebdSchin 	 .00000000000018567570959796010,
274*da2e3ebdSchin 	-.00000000000003149265065191483,
275*da2e3ebdSchin 	-.00000000000009309459495196889,
276*da2e3ebdSchin 	 .00000000000017914338601329117,
277*da2e3ebdSchin 	-.00000000000001302979717330866,
278*da2e3ebdSchin 	 .00000000000023097385217586939,
279*da2e3ebdSchin 	 .00000000000023999540484211737,
280*da2e3ebdSchin 	 .00000000000015393776174455408,
281*da2e3ebdSchin 	-.00000000000036870428315837678,
282*da2e3ebdSchin 	 .00000000000036920375082080089,
283*da2e3ebdSchin 	-.00000000000009383417223663699,
284*da2e3ebdSchin 	 .00000000000009433398189512690,
285*da2e3ebdSchin 	 .00000000000041481318704258568,
286*da2e3ebdSchin 	-.00000000000003792316480209314,
287*da2e3ebdSchin 	 .00000000000008403156304792424,
288*da2e3ebdSchin 	-.00000000000034262934348285429,
289*da2e3ebdSchin 	 .00000000000043712191957429145,
290*da2e3ebdSchin 	-.00000000000010475750058776541,
291*da2e3ebdSchin 	-.00000000000011118671389559323,
292*da2e3ebdSchin 	 .00000000000037549577257259853,
293*da2e3ebdSchin 	 .00000000000013912841212197565,
294*da2e3ebdSchin 	 .00000000000010775743037572640,
295*da2e3ebdSchin 	 .00000000000029391859187648000,
296*da2e3ebdSchin 	-.00000000000042790509060060774,
297*da2e3ebdSchin 	 .00000000000022774076114039555,
298*da2e3ebdSchin 	 .00000000000010849569622967912,
299*da2e3ebdSchin 	-.00000000000023073801945705758,
300*da2e3ebdSchin 	 .00000000000015761203773969435,
301*da2e3ebdSchin 	 .00000000000003345710269544082,
302*da2e3ebdSchin 	-.00000000000041525158063436123,
303*da2e3ebdSchin 	 .00000000000032655698896907146,
304*da2e3ebdSchin 	-.00000000000044704265010452446,
305*da2e3ebdSchin 	 .00000000000034527647952039772,
306*da2e3ebdSchin 	-.00000000000007048962392109746,
307*da2e3ebdSchin 	 .00000000000011776978751369214,
308*da2e3ebdSchin 	-.00000000000010774341461609578,
309*da2e3ebdSchin 	 .00000000000021863343293215910,
310*da2e3ebdSchin 	 .00000000000024132639491333131,
311*da2e3ebdSchin 	 .00000000000039057462209830700,
312*da2e3ebdSchin 	-.00000000000026570679203560751,
313*da2e3ebdSchin 	 .00000000000037135141919592021,
314*da2e3ebdSchin 	-.00000000000017166921336082431,
315*da2e3ebdSchin 	-.00000000000028658285157914353,
316*da2e3ebdSchin 	-.00000000000023812542263446809,
317*da2e3ebdSchin 	 .00000000000006576659768580062,
318*da2e3ebdSchin 	-.00000000000028210143846181267,
319*da2e3ebdSchin 	 .00000000000010701931762114254,
320*da2e3ebdSchin 	 .00000000000018119346366441110,
321*da2e3ebdSchin 	 .00000000000009840465278232627,
322*da2e3ebdSchin 	-.00000000000033149150282752542,
323*da2e3ebdSchin 	-.00000000000018302857356041668,
324*da2e3ebdSchin 	-.00000000000016207400156744949,
325*da2e3ebdSchin 	 .00000000000048303314949553201,
326*da2e3ebdSchin 	-.00000000000071560553172382115,
327*da2e3ebdSchin 	 .00000000000088821239518571855,
328*da2e3ebdSchin 	-.00000000000030900580513238244,
329*da2e3ebdSchin 	-.00000000000061076551972851496,
330*da2e3ebdSchin 	 .00000000000035659969663347830,
331*da2e3ebdSchin 	 .00000000000035782396591276383,
332*da2e3ebdSchin 	-.00000000000046226087001544578,
333*da2e3ebdSchin 	 .00000000000062279762917225156,
334*da2e3ebdSchin 	 .00000000000072838947272065741,
335*da2e3ebdSchin 	 .00000000000026809646615211673,
336*da2e3ebdSchin 	-.00000000000010960825046059278,
337*da2e3ebdSchin 	 .00000000000002311949383800537,
338*da2e3ebdSchin 	-.00000000000058469058005299247,
339*da2e3ebdSchin 	-.00000000000002103748251144494,
340*da2e3ebdSchin 	-.00000000000023323182945587408,
341*da2e3ebdSchin 	-.00000000000042333694288141916,
342*da2e3ebdSchin 	-.00000000000043933937969737844,
343*da2e3ebdSchin 	 .00000000000041341647073835565,
344*da2e3ebdSchin 	 .00000000000006841763641591466,
345*da2e3ebdSchin 	 .00000000000047585534004430641,
346*da2e3ebdSchin 	 .00000000000083679678674757695,
347*da2e3ebdSchin 	-.00000000000085763734646658640,
348*da2e3ebdSchin 	 .00000000000021913281229340092,
349*da2e3ebdSchin 	-.00000000000062242842536431148,
350*da2e3ebdSchin 	-.00000000000010983594325438430,
351*da2e3ebdSchin 	 .00000000000065310431377633651,
352*da2e3ebdSchin 	-.00000000000047580199021710769,
353*da2e3ebdSchin 	-.00000000000037854251265457040,
354*da2e3ebdSchin 	 .00000000000040939233218678664,
355*da2e3ebdSchin 	 .00000000000087424383914858291,
356*da2e3ebdSchin 	 .00000000000025218188456842882,
357*da2e3ebdSchin 	-.00000000000003608131360422557,
358*da2e3ebdSchin 	-.00000000000050518555924280902,
359*da2e3ebdSchin 	 .00000000000078699403323355317,
360*da2e3ebdSchin 	-.00000000000067020876961949060,
361*da2e3ebdSchin 	 .00000000000016108575753932458,
362*da2e3ebdSchin 	 .00000000000058527188436251509,
363*da2e3ebdSchin 	-.00000000000035246757297904791,
364*da2e3ebdSchin 	-.00000000000018372084495629058,
365*da2e3ebdSchin 	 .00000000000088606689813494916,
366*da2e3ebdSchin 	 .00000000000066486268071468700,
367*da2e3ebdSchin 	 .00000000000063831615170646519,
368*da2e3ebdSchin 	 .00000000000025144230728376072,
369*da2e3ebdSchin 	-.00000000000017239444525614834
370*da2e3ebdSchin };
371*da2e3ebdSchin 
372*da2e3ebdSchin #if !_lib_log
373*da2e3ebdSchin 
374*da2e3ebdSchin extern double
375*da2e3ebdSchin #ifdef _ANSI_SOURCE
log(double x)376*da2e3ebdSchin log(double x)
377*da2e3ebdSchin #else
378*da2e3ebdSchin log(x) double x;
379*da2e3ebdSchin #endif
380*da2e3ebdSchin {
381*da2e3ebdSchin 	int m, j;
382*da2e3ebdSchin 	double F, f, g, q, u, u2, v, zero = 0.0, one = 1.0;
383*da2e3ebdSchin 	volatile double u1;
384*da2e3ebdSchin 
385*da2e3ebdSchin 	/* Catch special cases */
386*da2e3ebdSchin 	if (x <= 0)
387*da2e3ebdSchin 		if (_IEEE && x == zero)	/* log(0) = -Inf */
388*da2e3ebdSchin 			return (-one/zero);
389*da2e3ebdSchin 		else if (_IEEE)		/* log(neg) = NaN */
390*da2e3ebdSchin 			return (zero/zero);
391*da2e3ebdSchin 		else if (x == zero)	/* NOT REACHED IF _IEEE */
392*da2e3ebdSchin 			return (infnan(-ERANGE));
393*da2e3ebdSchin 		else
394*da2e3ebdSchin 			return (infnan(EDOM));
395*da2e3ebdSchin 	else if (!finite(x))
396*da2e3ebdSchin 		if (_IEEE)		/* x = NaN, Inf */
397*da2e3ebdSchin 			return (x+x);
398*da2e3ebdSchin 		else
399*da2e3ebdSchin 			return (infnan(ERANGE));
400*da2e3ebdSchin 
401*da2e3ebdSchin 	/* Argument reduction: 1 <= g < 2; x/2^m = g;	*/
402*da2e3ebdSchin 	/* y = F*(1 + f/F) for |f| <= 2^-8		*/
403*da2e3ebdSchin 
404*da2e3ebdSchin 	m = logb(x);
405*da2e3ebdSchin 	g = ldexp(x, -m);
406*da2e3ebdSchin 	if (_IEEE && m == -1022) {
407*da2e3ebdSchin 		j = logb(g), m += j;
408*da2e3ebdSchin 		g = ldexp(g, -j);
409*da2e3ebdSchin 	}
410*da2e3ebdSchin 	j = N*(g-1) + .5;
411*da2e3ebdSchin 	F = (1.0/N) * j + 1;	/* F*128 is an integer in [128, 512] */
412*da2e3ebdSchin 	f = g - F;
413*da2e3ebdSchin 
414*da2e3ebdSchin 	/* Approximate expansion for log(1+f/F) ~= u + q */
415*da2e3ebdSchin 	g = 1/(2*F+f);
416*da2e3ebdSchin 	u = 2*f*g;
417*da2e3ebdSchin 	v = u*u;
418*da2e3ebdSchin 	q = u*v*(A1 + v*(A2 + v*(A3 + v*A4)));
419*da2e3ebdSchin 
420*da2e3ebdSchin     /* case 1: u1 = u rounded to 2^-43 absolute.  Since u < 2^-8,
421*da2e3ebdSchin      * 	       u1 has at most 35 bits, and F*u1 is exact, as F has < 8 bits.
422*da2e3ebdSchin      *         It also adds exactly to |m*log2_hi + log_F_head[j] | < 750
423*da2e3ebdSchin     */
424*da2e3ebdSchin 	if (m | j)
425*da2e3ebdSchin 		u1 = u + 513, u1 -= 513;
426*da2e3ebdSchin 
427*da2e3ebdSchin     /* case 2:	|1-x| < 1/256. The m- and j- dependent terms are zero;
428*da2e3ebdSchin      * 		u1 = u to 24 bits.
429*da2e3ebdSchin     */
430*da2e3ebdSchin 	else
431*da2e3ebdSchin 		u1 = u, TRUNC(u1);
432*da2e3ebdSchin 	u2 = (2.0*(f - F*u1) - u1*f) * g;
433*da2e3ebdSchin 			/* u1 + u2 = 2f/(2F+f) to extra precision.	*/
434*da2e3ebdSchin 
435*da2e3ebdSchin 	/* log(x) = log(2^m*F*(1+f/F)) =				*/
436*da2e3ebdSchin 	/* (m*log2_hi+logF_head[j]+u1) + (m*log2_lo+logF_tail[j]+q);	*/
437*da2e3ebdSchin 	/* (exact) + (tiny)						*/
438*da2e3ebdSchin 
439*da2e3ebdSchin 	u1 += m*logF_head[N] + logF_head[j];		/* exact */
440*da2e3ebdSchin 	u2 = (u2 + logF_tail[j]) + q;			/* tiny */
441*da2e3ebdSchin 	u2 += logF_tail[N]*m;
442*da2e3ebdSchin 	return (u1 + u2);
443*da2e3ebdSchin }
444*da2e3ebdSchin 
445*da2e3ebdSchin #endif
446*da2e3ebdSchin 
447*da2e3ebdSchin /*
448*da2e3ebdSchin  * Extra precision variant, returning struct {double a, b;};
449*da2e3ebdSchin  * log(x) = a+b to 63 bits, with a is rounded to 26 bits.
450*da2e3ebdSchin  */
451*da2e3ebdSchin struct Double
452*da2e3ebdSchin #ifdef _ANSI_SOURCE
__log__D(double x)453*da2e3ebdSchin __log__D(double x)
454*da2e3ebdSchin #else
455*da2e3ebdSchin __log__D(x) double x;
456*da2e3ebdSchin #endif
457*da2e3ebdSchin {
458*da2e3ebdSchin 	int m, j;
459*da2e3ebdSchin 	double F, f, g, q, u, v, u2, one = 1.0;
460*da2e3ebdSchin 	volatile double u1;
461*da2e3ebdSchin 	struct Double r;
462*da2e3ebdSchin 
463*da2e3ebdSchin 	/* Argument reduction: 1 <= g < 2; x/2^m = g;	*/
464*da2e3ebdSchin 	/* y = F*(1 + f/F) for |f| <= 2^-8		*/
465*da2e3ebdSchin 
466*da2e3ebdSchin 	m = (int)logb(x);
467*da2e3ebdSchin 	g = ldexp(x, -m);
468*da2e3ebdSchin 	if (_IEEE && m == -1022) {
469*da2e3ebdSchin 		j = (int)logb(g), m += j;
470*da2e3ebdSchin 		g = ldexp(g, -j);
471*da2e3ebdSchin 	}
472*da2e3ebdSchin 	j = (int)(N*(g-1) + .5);
473*da2e3ebdSchin 	F = (1.0/N) * j + 1;
474*da2e3ebdSchin 	f = g - F;
475*da2e3ebdSchin 
476*da2e3ebdSchin 	g = 1/(2*F+f);
477*da2e3ebdSchin 	u = 2*f*g;
478*da2e3ebdSchin 	v = u*u;
479*da2e3ebdSchin 	q = u*v*(A1 + v*(A2 + v*(A3 + v*A4)));
480*da2e3ebdSchin 	if (m | j)
481*da2e3ebdSchin 		u1 = u + 513, u1 -= 513;
482*da2e3ebdSchin 	else
483*da2e3ebdSchin 		u1 = u, TRUNC(u1);
484*da2e3ebdSchin 	u2 = (2.0*(f - F*u1) - u1*f) * g;
485*da2e3ebdSchin 
486*da2e3ebdSchin 	u1 += m*logF_head[N] + logF_head[j];
487*da2e3ebdSchin 
488*da2e3ebdSchin 	u2 +=  logF_tail[j]; u2 += q;
489*da2e3ebdSchin 	u2 += logF_tail[N]*m;
490*da2e3ebdSchin 	r.a = u1 + u2;			/* Only difference is here */
491*da2e3ebdSchin 	TRUNC(r.a);
492*da2e3ebdSchin 	r.b = (u1 - r.a) + u2;
493*da2e3ebdSchin 	return (r);
494*da2e3ebdSchin }
495*da2e3ebdSchin 
496*da2e3ebdSchin #endif
497