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