s_log1p.c revision 2116
118316Swollman/* @(#)s_log1p.c 5.1 93/09/24 */
218316Swollman/*
318316Swollman * ====================================================
418316Swollman * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
518316Swollman *
618316Swollman * Developed at SunPro, a Sun Microsystems, Inc. business.
718316Swollman * Permission to use, copy, modify, and distribute this
818316Swollman * software is freely granted, provided that this notice
918316Swollman * is preserved.
1018316Swollman * ====================================================
1118316Swollman */
1218316Swollman
1318316Swollman#ifndef lint
1418316Swollmanstatic char rcsid[] = "$Id: s_log1p.c,v 1.6 1994/08/18 23:06:59 jtc Exp $";
1518316Swollman#endif
1618316Swollman
1718316Swollman/* double log1p(double x)
1818316Swollman *
1918316Swollman * Method :
2018316Swollman *   1. Argument Reduction: find k and f such that
2118316Swollman *			1+x = 2^k * (1+f),
2218316Swollman *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
2318316Swollman *
2418316Swollman *      Note. If k=0, then f=x is exact. However, if k!=0, then f
2518316Swollman *	may not be representable exactly. In that case, a correction
2618316Swollman *	term is need. Let u=1+x rounded. Let c = (1+x)-u, then
2718316Swollman *	log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
2818316Swollman *	and add back the correction term c/u.
2918316Swollman *	(Note: when x > 2**53, one can simply return log(x))
3018316Swollman *
3118316Swollman *   2. Approximation of log1p(f).
3218316Swollman *	Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
3318316Swollman *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
3418316Swollman *	     	 = 2s + s*R
3518316Swollman *      We use a special Reme algorithm on [0,0.1716] to generate
3618316Swollman * 	a polynomial of degree 14 to approximate R The maximum error
3718316Swollman *	of this polynomial approximation is bounded by 2**-58.45. In
3818316Swollman *	other words,
3918316Swollman *		        2      4      6      8      10      12      14
4018316Swollman *	    R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s  +Lp6*s  +Lp7*s
4118316Swollman *  	(the values of Lp1 to Lp7 are listed in the program)
4218316Swollman *	and
4318316Swollman *	    |      2          14          |     -58.45
4418316Swollman *	    | Lp1*s +...+Lp7*s    -  R(z) | <= 2
4518316Swollman *	    |                             |
4618316Swollman *	Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
4718316Swollman *	In order to guarantee error in log below 1ulp, we compute log
4818316Swollman *	by
4918316Swollman *		log1p(f) = f - (hfsq - s*(hfsq+R)).
5018316Swollman *
5118316Swollman *	3. Finally, log1p(x) = k*ln2 + log1p(f).
5218316Swollman *		 	     = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
5318316Swollman *	   Here ln2 is split into two floating point number:
5418316Swollman *			ln2_hi + ln2_lo,
5518316Swollman *	   where n*ln2_hi is always exact for |n| < 2000.
5618316Swollman *
5718316Swollman * Special cases:
5818316Swollman *	log1p(x) is NaN with signal if x < -1 (including -INF) ;
5918316Swollman *	log1p(+INF) is +INF; log1p(-1) is -INF with signal;
6018316Swollman *	log1p(NaN) is that NaN with no signal.
6118316Swollman *
6218316Swollman * Accuracy:
6318316Swollman *	according to an error analysis, the error is always less than
6418316Swollman *	1 ulp (unit in the last place).
6518316Swollman *
6618316Swollman * Constants:
6718316Swollman * The hexadecimal values are the intended ones for the following
6818316Swollman * constants. The decimal values may be used, provided that the
6918316Swollman * compiler will convert from decimal to binary accurately enough
7018316Swollman * to produce the hexadecimal values shown.
7118316Swollman *
7218316Swollman * Note: Assuming log() return accurate answer, the following
7318316Swollman * 	 algorithm can be used to compute log1p(x) to within a few ULP:
7418316Swollman *
7518316Swollman *		u = 1+x;
7618316Swollman *		if(u==1.0) return x ; else
7718316Swollman *			   return log(u)*(x/(u-1.0));
7818316Swollman *
7918316Swollman *	 See HP-15C Advanced Functions Handbook, p.193.
8018316Swollman */
8118316Swollman
8218316Swollman#include "math.h"
8318316Swollman#include "math_private.h"
8418316Swollman
8518316Swollman#ifdef __STDC__
8618316Swollmanstatic const double
8718316Swollman#else
8818316Swollmanstatic double
8918316Swollman#endif
9018316Swollmanln2_hi  =  6.93147180369123816490e-01,	/* 3fe62e42 fee00000 */
9118316Swollmanln2_lo  =  1.90821492927058770002e-10,	/* 3dea39ef 35793c76 */
9218316Swollmantwo54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
9318316SwollmanLp1 = 6.666666666666735130e-01,  /* 3FE55555 55555593 */
9418316SwollmanLp2 = 3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
9518316SwollmanLp3 = 2.857142874366239149e-01,  /* 3FD24924 94229359 */
9618316SwollmanLp4 = 2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
9718316SwollmanLp5 = 1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
9818316SwollmanLp6 = 1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
9918316SwollmanLp7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
10018316Swollman
10118316Swollman#ifdef __STDC__
10218316Swollmanstatic const double zero = 0.0;
10318316Swollman#else
10418316Swollmanstatic double zero = 0.0;
10518316Swollman#endif
10618316Swollman
10718316Swollman#ifdef __STDC__
10818316Swollman	double log1p(double x)
10918316Swollman#else
11018316Swollman	double log1p(x)
11118316Swollman	double x;
11218316Swollman#endif
11318316Swollman{
11418316Swollman	double hfsq,f,c,s,z,R,u;
11518316Swollman	int32_t k,hx,hu,ax;
11618316Swollman
11718316Swollman	GET_HIGH_WORD(hx,x);
11818316Swollman	ax = hx&0x7fffffff;
11918316Swollman
12018316Swollman	k = 1;
12118316Swollman	if (hx < 0x3FDA827A) {			/* x < 0.41422  */
12218316Swollman	    if(ax>=0x3ff00000) {		/* x <= -1.0 */
12318316Swollman		if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */
12418316Swollman		else return (x-x)/(x-x);	/* log1p(x<-1)=NaN */
12518316Swollman	    }
12618316Swollman	    if(ax<0x3e200000) {			/* |x| < 2**-29 */
12718316Swollman		if(two54+x>zero			/* raise inexact */
12818316Swollman	            &&ax<0x3c900000) 		/* |x| < 2**-54 */
12918316Swollman		    return x;
13018316Swollman		else
13118316Swollman		    return x - x*x*0.5;
13218316Swollman	    }
13318316Swollman	    if(hx>0||hx<=((int32_t)0xbfd2bec3)) {
13418316Swollman		k=0;f=x;hu=1;}	/* -0.2929<x<0.41422 */
13518316Swollman	}
13618316Swollman	if (hx >= 0x7ff00000) return x+x;
13718316Swollman	if(k!=0) {
13818316Swollman	    if(hx<0x43400000) {
13918316Swollman		u  = 1.0+x;
14018316Swollman		GET_HIGH_WORD(hu,u);
14118316Swollman	        k  = (hu>>20)-1023;
14218316Swollman	        c  = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */
14318316Swollman		c /= u;
14418316Swollman	    } else {
14518316Swollman		u  = x;
14618316Swollman		GET_HIGH_WORD(hu,u);
14718316Swollman	        k  = (hu>>20)-1023;
14818316Swollman		c  = 0;
14918316Swollman	    }
15018316Swollman	    hu &= 0x000fffff;
15118316Swollman	    if(hu<0x6a09e) {
15218316Swollman	        SET_HIGH_WORD(u,hu|0x3ff00000);	/* normalize u */
15318316Swollman	    } else {
15418316Swollman	        k += 1;
15518316Swollman		SET_HIGH_WORD(u,hu|0x3fe00000);	/* normalize u/2 */
15618316Swollman	        hu = (0x00100000-hu)>>2;
15718316Swollman	    }
15818316Swollman	    f = u-1.0;
15918316Swollman	}
16018316Swollman	hfsq=0.5*f*f;
16118316Swollman	if(hu==0) {	/* |f| < 2**-20 */
16218316Swollman	    if(f==zero) if(k==0) return zero;
16318316Swollman			else {c += k*ln2_lo; return k*ln2_hi+c;}
16418316Swollman	    R = hfsq*(1.0-0.66666666666666666*f);
16518316Swollman	    if(k==0) return f-R; else
16618316Swollman	    	     return k*ln2_hi-((R-(k*ln2_lo+c))-f);
16718316Swollman	}
16818316Swollman 	s = f/(2.0+f);
16918316Swollman	z = s*s;
17018316Swollman	R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
17118316Swollman	if(k==0) return f-(hfsq-s*(hfsq+R)); else
17218316Swollman		 return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
17318316Swollman}
17418316Swollman