xref: /csrg-svn/lib/libm/common_source/log.c (revision 34126)
1*34126Sbostic /*
224601Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34126Sbostic  * All rights reserved.
4*34126Sbostic  *
5*34126Sbostic  * Redistribution and use in source and binary forms are permitted
6*34126Sbostic  * provided that this notice is preserved and that due credit is given
7*34126Sbostic  * to the University of California at Berkeley. The name of the University
8*34126Sbostic  * may not be used to endorse or promote products derived from this
9*34126Sbostic  * software without specific prior written permission. This software
10*34126Sbostic  * is provided ``as is'' without express or implied warranty.
11*34126Sbostic  *
12*34126Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34126Sbostic  * research project and hence should feel obligated to report their
14*34126Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34126Sbostic  * the sendbug(8) program, to the authors.
1624601Szliu  */
1724601Szliu 
1824601Szliu #ifndef lint
19*34126Sbostic static char sccsid[] = "@(#)log.c	5.2 (Berkeley) 04/29/88";
20*34126Sbostic #endif /* not lint */
2124601Szliu 
2224601Szliu /* LOG(X)
2324601Szliu  * RETURN THE LOGARITHM OF x
2424601Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 bits or IEEE DOUBLE 53 BITS)
2524601Szliu  * CODED IN C BY K.C. NG, 1/19/85;
2624601Szliu  * REVISED BY K.C. NG on 2/7/85, 3/7/85, 3/24/85, 4/16/85.
2724601Szliu  *
2824601Szliu  * Required system supported functions:
2924601Szliu  *	scalb(x,n)
3024601Szliu  *	copysign(x,y)
3124601Szliu  *	logb(x)
3224601Szliu  *	finite(x)
3324601Szliu  *
3424601Szliu  * Required kernel function:
3524601Szliu  *	log__L(z)
3624601Szliu  *
3724601Szliu  * Method :
3824601Szliu  *	1. Argument Reduction: find k and f such that
3924601Szliu  *			x = 2^k * (1+f),
4024601Szliu  *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
4124601Szliu  *
4224601Szliu  *	2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
4324601Szliu  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
4424601Szliu  *	   log(1+f) is computed by
4524601Szliu  *
4624601Szliu  *	     		log(1+f) = 2s + s*log__L(s*s)
4724601Szliu  *	   where
4824601Szliu  *		log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...)))
4924601Szliu  *
5024601Szliu  *	   See log__L() for the values of the coefficients.
5124601Szliu  *
5224601Szliu  *	3. Finally,  log(x) = k*ln2 + log(1+f).  (Here n*ln2 will be stored
5324601Szliu  *	   in two floating point number: n*ln2hi + n*ln2lo, n*ln2hi is exact
5424601Szliu  *	   since the last 20 bits of ln2hi is 0.)
5524601Szliu  *
5624601Szliu  * Special cases:
5724601Szliu  *	log(x) is NaN with signal if x < 0 (including -INF) ;
5824601Szliu  *	log(+INF) is +INF; log(0) is -INF with signal;
5924601Szliu  *	log(NaN) is that NaN with no signal.
6024601Szliu  *
6124601Szliu  * Accuracy:
6224601Szliu  *	log(x) returns the exact log(x) nearly rounded. In a test run with
6324601Szliu  *	1,536,000 random arguments on a VAX, the maximum observed error was
6424601Szliu  *	.826 ulps (units in the last place).
6524601Szliu  *
6624601Szliu  * Constants:
6724601Szliu  * The hexadecimal values are the intended ones for the following constants.
6824601Szliu  * The decimal values may be used, provided that the compiler will convert
6924601Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
7024601Szliu  * shown.
7124601Szliu  */
7224601Szliu 
7331853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
7424601Szliu #include <errno.h>
7531853Szliu #ifdef vax
7631812Szliu #define _0x(A,B)	0x/**/A/**/B
7731853Szliu #else	/* vax */
7831812Szliu #define _0x(A,B)	0x/**/B/**/A
7931853Szliu #endif	/* vax */
8026893Selefunt /* static double */
8124601Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
8224601Szliu /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
8324601Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
8431812Szliu static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
8531812Szliu static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
8631812Szliu static long     sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
8724601Szliu #define    ln2hi    (*(double*)ln2hix)
8824601Szliu #define    ln2lo    (*(double*)ln2lox)
8924601Szliu #define    sqrt2    (*(double*)sqrt2x)
9031853Szliu #else	/* defined(vax)||defined(tahoe)	*/
9126893Selefunt static double
9224601Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
9324601Szliu ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
9424601Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
9531853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
9624601Szliu 
9724601Szliu double log(x)
9824601Szliu double x;
9924601Szliu {
10024601Szliu 	static double zero=0.0, negone= -1.0, half=1.0/2.0;
10124601Szliu 	double logb(),scalb(),copysign(),log__L(),s,z,t;
10224601Szliu 	int k,n,finite();
10324601Szliu 
10431853Szliu #if !defined(vax)&&!defined(tahoe)
10524601Szliu 	if(x!=x) return(x);	/* x is NaN */
10631853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
10724601Szliu 	if(finite(x)) {
10824601Szliu 	   if( x > zero ) {
10924601Szliu 
11024601Szliu 	   /* argument reduction */
11124601Szliu 	      k=logb(x);   x=scalb(x,-k);
11224601Szliu 	      if(k == -1022) /* subnormal no. */
11324601Szliu 		   {n=logb(x); x=scalb(x,-n); k+=n;}
11424601Szliu 	      if(x >= sqrt2 ) {k += 1; x *= half;}
11524601Szliu 	      x += negone ;
11624601Szliu 
11724601Szliu 	   /* compute log(1+x)  */
11824601Szliu               s=x/(2+x); t=x*x*half;
11924601Szliu 	      z=k*ln2lo+s*(t+log__L(s*s));
12024601Szliu 	      x += (z - t) ;
12124601Szliu 
12224601Szliu 	      return(k*ln2hi+x);
12324601Szliu 	   }
12424601Szliu 	/* end of if (x > zero) */
12524601Szliu 
12624601Szliu 	   else {
12731853Szliu #if defined(vax)||defined(tahoe)
12824601Szliu 		extern double infnan();
12924601Szliu 		if ( x == zero )
13024601Szliu 		    return (infnan(-ERANGE));	/* -INF */
13124601Szliu 		else
13224601Szliu 		    return (infnan(EDOM));	/* NaN */
13331853Szliu #else	/* defined(vax)||defined(tahoe) */
13424601Szliu 		/* zero argument, return -INF with signal */
13524601Szliu 		if ( x == zero )
13624601Szliu 		    return( negone/zero );
13724601Szliu 
13824601Szliu 		/* negative argument, return NaN with signal */
13924601Szliu 		else
14024601Szliu 		    return ( zero / zero );
14131853Szliu #endif	/* defined(vax)||defined(tahoe) */
14224601Szliu 	    }
14324601Szliu 	}
14424601Szliu     /* end of if (finite(x)) */
14531853Szliu     /* NOTREACHED if defined(vax)||defined(tahoe) */
14624601Szliu 
14724601Szliu     /* log(-INF) is NaN with signal */
14824601Szliu 	else if (x<0)
14924601Szliu 	    return(zero/zero);
15024601Szliu 
15124601Szliu     /* log(+INF) is +INF */
15224601Szliu 	else return(x);
15324601Szliu 
15424601Szliu }
155