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