134126Sbostic /* 224601Szliu * Copyright (c) 1985 Regents of the University of California. 334126Sbostic * All rights reserved. 434126Sbostic * 534126Sbostic * Redistribution and use in source and binary forms are permitted 634931Sbostic * provided that the above copyright notice and this paragraph are 734931Sbostic * duplicated in all such forms and that any documentation, 834931Sbostic * advertising materials, and other materials related to such 934931Sbostic * distribution and use acknowledge that the software was developed 1034931Sbostic * by the University of California, Berkeley. The name of the 1134931Sbostic * University may not be used to endorse or promote products derived 1234931Sbostic * from this software without specific prior written permission. 1334931Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434931Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534931Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634126Sbostic * 1734126Sbostic * All recipients should regard themselves as participants in an ongoing 1834126Sbostic * research project and hence should feel obligated to report their 1934126Sbostic * experiences (good or bad) with these elementary function codes, using 2034126Sbostic * the sendbug(8) program, to the authors. 2124601Szliu */ 2224601Szliu 2324601Szliu #ifndef lint 24*35679Sbostic static char sccsid[] = "@(#)log.c 5.4 (Berkeley) 09/22/88"; 2534126Sbostic #endif /* not lint */ 2624601Szliu 2724601Szliu /* LOG(X) 2824601Szliu * RETURN THE LOGARITHM OF x 2924601Szliu * DOUBLE PRECISION (VAX D FORMAT 56 bits or IEEE DOUBLE 53 BITS) 3024601Szliu * CODED IN C BY K.C. NG, 1/19/85; 3124601Szliu * REVISED BY K.C. NG on 2/7/85, 3/7/85, 3/24/85, 4/16/85. 3224601Szliu * 3324601Szliu * Required system supported functions: 3424601Szliu * scalb(x,n) 3524601Szliu * copysign(x,y) 3624601Szliu * logb(x) 3724601Szliu * finite(x) 3824601Szliu * 3924601Szliu * Required kernel function: 4024601Szliu * log__L(z) 4124601Szliu * 4224601Szliu * Method : 4324601Szliu * 1. Argument Reduction: find k and f such that 4424601Szliu * x = 2^k * (1+f), 4524601Szliu * where sqrt(2)/2 < 1+f < sqrt(2) . 4624601Szliu * 4724601Szliu * 2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 4824601Szliu * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 4924601Szliu * log(1+f) is computed by 5024601Szliu * 5124601Szliu * log(1+f) = 2s + s*log__L(s*s) 5224601Szliu * where 5324601Szliu * log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...))) 5424601Szliu * 5524601Szliu * See log__L() for the values of the coefficients. 5624601Szliu * 5724601Szliu * 3. Finally, log(x) = k*ln2 + log(1+f). (Here n*ln2 will be stored 5824601Szliu * in two floating point number: n*ln2hi + n*ln2lo, n*ln2hi is exact 5924601Szliu * since the last 20 bits of ln2hi is 0.) 6024601Szliu * 6124601Szliu * Special cases: 6224601Szliu * log(x) is NaN with signal if x < 0 (including -INF) ; 6324601Szliu * log(+INF) is +INF; log(0) is -INF with signal; 6424601Szliu * log(NaN) is that NaN with no signal. 6524601Szliu * 6624601Szliu * Accuracy: 6724601Szliu * log(x) returns the exact log(x) nearly rounded. In a test run with 6824601Szliu * 1,536,000 random arguments on a VAX, the maximum observed error was 6924601Szliu * .826 ulps (units in the last place). 7024601Szliu * 7124601Szliu * Constants: 7224601Szliu * The hexadecimal values are the intended ones for the following constants. 7324601Szliu * The decimal values may be used, provided that the compiler will convert 7424601Szliu * from decimal to binary accurately enough to produce the hexadecimal values 7524601Szliu * shown. 7624601Szliu */ 7724601Szliu 7824601Szliu #include <errno.h> 79*35679Sbostic #include "mathimpl.h" 8024601Szliu 81*35679Sbostic vc(ln2hi, 6.9314718055829871446E-1 ,7217,4031,0000,f7d0, 0, .B17217F7D00000) 82*35679Sbostic vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC) 83*35679Sbostic vc(sqrt2, 1.4142135623730950622E0 ,04f3,40b5,de65,33f9, 1, .B504F333F9DE65) 84*35679Sbostic 85*35679Sbostic ic(ln2hi, 6.9314718036912381649E-1, -1, 1.62E42FEE00000) 86*35679Sbostic ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76) 87*35679Sbostic ic(sqrt2, 1.4142135623730951455E0, 0, 1.6A09E667F3BCD) 88*35679Sbostic 89*35679Sbostic #ifdef vccast 90*35679Sbostic #define ln2hi vccast(ln2hi) 91*35679Sbostic #define ln2lo vccast(ln2lo) 92*35679Sbostic #define sqrt2 vccast(sqrt2) 93*35679Sbostic #endif 94*35679Sbostic 95*35679Sbostic 9624601Szliu double log(x) 9724601Szliu double x; 9824601Szliu { 99*35679Sbostic const static double zero=0.0, negone= -1.0, half=1.0/2.0; 100*35679Sbostic double s,z,t; 101*35679Sbostic int k,n; 10224601Szliu 10331853Szliu #if !defined(vax)&&!defined(tahoe) 10424601Szliu if(x!=x) return(x); /* x is NaN */ 10531853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 10624601Szliu if(finite(x)) { 10724601Szliu if( x > zero ) { 10824601Szliu 10924601Szliu /* argument reduction */ 11024601Szliu k=logb(x); x=scalb(x,-k); 11124601Szliu if(k == -1022) /* subnormal no. */ 11224601Szliu {n=logb(x); x=scalb(x,-n); k+=n;} 11324601Szliu if(x >= sqrt2 ) {k += 1; x *= half;} 11424601Szliu x += negone ; 11524601Szliu 11624601Szliu /* compute log(1+x) */ 11724601Szliu s=x/(2+x); t=x*x*half; 11824601Szliu z=k*ln2lo+s*(t+log__L(s*s)); 11924601Szliu x += (z - t) ; 12024601Szliu 12124601Szliu return(k*ln2hi+x); 12224601Szliu } 12324601Szliu /* end of if (x > zero) */ 12424601Szliu 12524601Szliu else { 12631853Szliu #if defined(vax)||defined(tahoe) 12724601Szliu if ( x == zero ) 12824601Szliu return (infnan(-ERANGE)); /* -INF */ 12924601Szliu else 13024601Szliu return (infnan(EDOM)); /* NaN */ 13131853Szliu #else /* defined(vax)||defined(tahoe) */ 13224601Szliu /* zero argument, return -INF with signal */ 13324601Szliu if ( x == zero ) 13424601Szliu return( negone/zero ); 13524601Szliu 13624601Szliu /* negative argument, return NaN with signal */ 13724601Szliu else 13824601Szliu return ( zero / zero ); 13931853Szliu #endif /* defined(vax)||defined(tahoe) */ 14024601Szliu } 14124601Szliu } 14224601Szliu /* end of if (finite(x)) */ 14331853Szliu /* NOTREACHED if defined(vax)||defined(tahoe) */ 14424601Szliu 14524601Szliu /* log(-INF) is NaN with signal */ 14624601Szliu else if (x<0) 14724601Szliu return(zero/zero); 14824601Szliu 14924601Szliu /* log(+INF) is +INF */ 15024601Szliu else return(x); 15124601Szliu 15224601Szliu } 153