xref: /csrg-svn/lib/libm/common_source/log.c (revision 34931)
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
6*34931Sbostic  * provided that the above copyright notice and this paragraph are
7*34931Sbostic  * duplicated in all such forms and that any documentation,
8*34931Sbostic  * advertising materials, and other materials related to such
9*34931Sbostic  * distribution and use acknowledge that the software was developed
10*34931Sbostic  * by the University of California, Berkeley.  The name of the
11*34931Sbostic  * University may not be used to endorse or promote products derived
12*34931Sbostic  * from this software without specific prior written permission.
13*34931Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34931Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34931Sbostic  * 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*34931Sbostic static char sccsid[] = "@(#)log.c	5.3 (Berkeley) 06/30/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 
7831853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
7924601Szliu #include <errno.h>
8031853Szliu #ifdef vax
8131812Szliu #define _0x(A,B)	0x/**/A/**/B
8231853Szliu #else	/* vax */
8331812Szliu #define _0x(A,B)	0x/**/B/**/A
8431853Szliu #endif	/* vax */
8526893Selefunt /* static double */
8624601Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
8724601Szliu /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
8824601Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
8931812Szliu static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
9031812Szliu static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
9131812Szliu static long     sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
9224601Szliu #define    ln2hi    (*(double*)ln2hix)
9324601Szliu #define    ln2lo    (*(double*)ln2lox)
9424601Szliu #define    sqrt2    (*(double*)sqrt2x)
9531853Szliu #else	/* defined(vax)||defined(tahoe)	*/
9626893Selefunt static double
9724601Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
9824601Szliu ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
9924601Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
10031853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
10124601Szliu 
10224601Szliu double log(x)
10324601Szliu double x;
10424601Szliu {
10524601Szliu 	static double zero=0.0, negone= -1.0, half=1.0/2.0;
10624601Szliu 	double logb(),scalb(),copysign(),log__L(),s,z,t;
10724601Szliu 	int k,n,finite();
10824601Szliu 
10931853Szliu #if !defined(vax)&&!defined(tahoe)
11024601Szliu 	if(x!=x) return(x);	/* x is NaN */
11131853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
11224601Szliu 	if(finite(x)) {
11324601Szliu 	   if( x > zero ) {
11424601Szliu 
11524601Szliu 	   /* argument reduction */
11624601Szliu 	      k=logb(x);   x=scalb(x,-k);
11724601Szliu 	      if(k == -1022) /* subnormal no. */
11824601Szliu 		   {n=logb(x); x=scalb(x,-n); k+=n;}
11924601Szliu 	      if(x >= sqrt2 ) {k += 1; x *= half;}
12024601Szliu 	      x += negone ;
12124601Szliu 
12224601Szliu 	   /* compute log(1+x)  */
12324601Szliu               s=x/(2+x); t=x*x*half;
12424601Szliu 	      z=k*ln2lo+s*(t+log__L(s*s));
12524601Szliu 	      x += (z - t) ;
12624601Szliu 
12724601Szliu 	      return(k*ln2hi+x);
12824601Szliu 	   }
12924601Szliu 	/* end of if (x > zero) */
13024601Szliu 
13124601Szliu 	   else {
13231853Szliu #if defined(vax)||defined(tahoe)
13324601Szliu 		extern double infnan();
13424601Szliu 		if ( x == zero )
13524601Szliu 		    return (infnan(-ERANGE));	/* -INF */
13624601Szliu 		else
13724601Szliu 		    return (infnan(EDOM));	/* NaN */
13831853Szliu #else	/* defined(vax)||defined(tahoe) */
13924601Szliu 		/* zero argument, return -INF with signal */
14024601Szliu 		if ( x == zero )
14124601Szliu 		    return( negone/zero );
14224601Szliu 
14324601Szliu 		/* negative argument, return NaN with signal */
14424601Szliu 		else
14524601Szliu 		    return ( zero / zero );
14631853Szliu #endif	/* defined(vax)||defined(tahoe) */
14724601Szliu 	    }
14824601Szliu 	}
14924601Szliu     /* end of if (finite(x)) */
15031853Szliu     /* NOTREACHED if defined(vax)||defined(tahoe) */
15124601Szliu 
15224601Szliu     /* log(-INF) is NaN with signal */
15324601Szliu 	else if (x<0)
15424601Szliu 	    return(zero/zero);
15524601Szliu 
15624601Szliu     /* log(+INF) is +INF */
15724601Szliu 	else return(x);
15824601Szliu 
15924601Szliu }
160