xref: /csrg-svn/lib/libm/common_source/log1p.c (revision 34126)
1*34126Sbostic /*
224603Szliu  * 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.
1624603Szliu  */
1724603Szliu 
1824603Szliu #ifndef lint
19*34126Sbostic static char sccsid[] = "@(#)log1p.c	5.2 (Berkeley) 04/29/88";
20*34126Sbostic #endif /* not lint */
2124603Szliu 
2224603Szliu /* LOG1P(x)
2324603Szliu  * RETURN THE LOGARITHM OF 1+x
2424603Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS)
2524603Szliu  * CODED IN C BY K.C. NG, 1/19/85;
2624603Szliu  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85.
2724603Szliu  *
2824603Szliu  * Required system supported functions:
2924603Szliu  *	scalb(x,n)
3024603Szliu  *	copysign(x,y)
3124603Szliu  *	logb(x)
3224603Szliu  *	finite(x)
3324603Szliu  *
3424603Szliu  * Required kernel function:
3524603Szliu  *	log__L(z)
3624603Szliu  *
3724603Szliu  * Method :
3824603Szliu  *	1. Argument Reduction: find k and f such that
3924603Szliu  *			1+x  = 2^k * (1+f),
4024603Szliu  *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
4124603Szliu  *
4224603Szliu  *	2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
4324603Szliu  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
4424603Szliu  *	   log(1+f) is computed by
4524603Szliu  *
4624603Szliu  *	     		log(1+f) = 2s + s*log__L(s*s)
4724603Szliu  *	   where
4824603Szliu  *		log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...)))
4924603Szliu  *
5024603Szliu  *	   See log__L() for the values of the coefficients.
5124603Szliu  *
5224603Szliu  *	3. Finally,  log(1+x) = k*ln2 + log(1+f).
5324603Szliu  *
5424603Szliu  *	Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers
5524603Szliu  *		   n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last
5624603Szliu  *		   20 bits (for VAX D format), or the last 21 bits ( for IEEE
5724603Szliu  *		   double) is 0. This ensures n*ln2hi is exactly representable.
5824603Szliu  *		2. In step 1, f may not be representable. A correction term c
5924603Szliu  *	 	   for f is computed. It follows that the correction term for
6024603Szliu  *		   f - t (the leading term of log(1+f) in step 2) is c-c*x. We
6124603Szliu  *		   add this correction term to n*ln2lo to attenuate the error.
6224603Szliu  *
6324603Szliu  *
6424603Szliu  * Special cases:
6524603Szliu  *	log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal;
6624603Szliu  *	log1p(INF) is +INF; log1p(-1) is -INF with signal;
6724603Szliu  *	only log1p(0)=0 is exact for finite argument.
6824603Szliu  *
6924603Szliu  * Accuracy:
7024603Szliu  *	log1p(x) returns the exact log(1+x) nearly rounded. In a test run
7124603Szliu  *	with 1,536,000 random arguments on a VAX, the maximum observed
7224603Szliu  *	error was .846 ulps (units in the last place).
7324603Szliu  *
7424603Szliu  * Constants:
7524603Szliu  * The hexadecimal values are the intended ones for the following constants.
7624603Szliu  * The decimal values may be used, provided that the compiler will convert
7724603Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
7824603Szliu  * shown.
7924603Szliu  */
8024603Szliu 
8131853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
8224603Szliu #include <errno.h>
8331853Szliu #ifdef vax
8431812Szliu #define _0x(A,B)	0x/**/A/**/B
8531853Szliu #else	/* vax */
8631812Szliu #define _0x(A,B)	0x/**/B/**/A
8731853Szliu #endif	/* vax */
8826893Selefunt /* static double */
8924603Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
9024603Szliu /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
9124603Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
9231812Szliu static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
9331812Szliu static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
9431812Szliu static long     sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
9524603Szliu #define    ln2hi    (*(double*)ln2hix)
9624603Szliu #define    ln2lo    (*(double*)ln2lox)
9724603Szliu #define    sqrt2    (*(double*)sqrt2x)
9831853Szliu #else	/* defined(vax)||defined(tahoe)	*/
9926893Selefunt static double
10024603Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
10124603Szliu ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
10224603Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
10331853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
10424603Szliu 
10524603Szliu double log1p(x)
10624603Szliu double x;
10724603Szliu {
10824603Szliu 	static double zero=0.0, negone= -1.0, one=1.0,
10924603Szliu 		      half=1.0/2.0, small=1.0E-20;   /* 1+small == 1 */
11024603Szliu 	double logb(),copysign(),scalb(),log__L(),z,s,t,c;
11124603Szliu 	int k,finite();
11224603Szliu 
11331853Szliu #if !defined(vax)&&!defined(tahoe)
11424603Szliu 	if(x!=x) return(x);	/* x is NaN */
11531853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
11624603Szliu 
11724603Szliu 	if(finite(x)) {
11824603Szliu 	   if( x > negone ) {
11924603Szliu 
12024603Szliu 	   /* argument reduction */
12124603Szliu 	      if(copysign(x,one)<small) return(x);
12224603Szliu 	      k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k);
12324603Szliu 	      if(z+t >= sqrt2 )
12424603Szliu 		  { k += 1 ; z *= half; t *= half; }
12524603Szliu 	      t += negone; x = z + t;
12624603Szliu 	      c = (t-x)+z ;		/* correction term for x */
12724603Szliu 
12824603Szliu  	   /* compute log(1+x)  */
12924603Szliu               s = x/(2+x); t = x*x*half;
13024603Szliu 	      c += (k*ln2lo-c*x);
13124603Szliu 	      z = c+s*(t+log__L(s*s));
13224603Szliu 	      x += (z - t) ;
13324603Szliu 
13424603Szliu 	      return(k*ln2hi+x);
13524603Szliu 	   }
13624603Szliu 	/* end of if (x > negone) */
13724603Szliu 
13824603Szliu 	    else {
13931853Szliu #if defined(vax)||defined(tahoe)
14024603Szliu 		extern double infnan();
14124603Szliu 		if ( x == negone )
14224603Szliu 		    return (infnan(-ERANGE));	/* -INF */
14324603Szliu 		else
14424603Szliu 		    return (infnan(EDOM));	/* NaN */
14531853Szliu #else	/* defined(vax)||defined(tahoe) */
14624603Szliu 		/* x = -1, return -INF with signal */
14724603Szliu 		if ( x == negone ) return( negone/zero );
14824603Szliu 
14924603Szliu 		/* negative argument for log, return NaN with signal */
15024603Szliu 	        else return ( zero / zero );
15131853Szliu #endif	/* defined(vax)||defined(tahoe) */
15224603Szliu 	    }
15324603Szliu 	}
15424603Szliu     /* end of if (finite(x)) */
15524603Szliu 
15624603Szliu     /* log(-INF) is NaN */
15724603Szliu 	else if(x<0)
15824603Szliu 	     return(zero/zero);
15924603Szliu 
16024603Szliu     /* log(+INF) is INF */
16124603Szliu 	else return(x);
16224603Szliu }
163