s_log1p.c revision 8870
12116Sjkh/* @(#)s_log1p.c 5.1 93/09/24 */
22116Sjkh/*
32116Sjkh * ====================================================
42116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
52116Sjkh *
62116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business.
72116Sjkh * Permission to use, copy, modify, and distribute this
88870Srgrimes * software is freely granted, provided that this notice
92116Sjkh * is preserved.
102116Sjkh * ====================================================
112116Sjkh */
122116Sjkh
132116Sjkh#ifndef lint
148870Srgrimesstatic char rcsid[] = "$Id: s_log1p.c,v 1.1.1.1 1994/08/19 09:39:52 jkh Exp $";
152116Sjkh#endif
162116Sjkh
172116Sjkh/* double log1p(double x)
182116Sjkh *
198870Srgrimes * Method :
208870Srgrimes *   1. Argument Reduction: find k and f such that
218870Srgrimes *			1+x = 2^k * (1+f),
222116Sjkh *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
232116Sjkh *
242116Sjkh *      Note. If k=0, then f=x is exact. However, if k!=0, then f
252116Sjkh *	may not be representable exactly. In that case, a correction
262116Sjkh *	term is need. Let u=1+x rounded. Let c = (1+x)-u, then
272116Sjkh *	log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
282116Sjkh *	and add back the correction term c/u.
292116Sjkh *	(Note: when x > 2**53, one can simply return log(x))
302116Sjkh *
312116Sjkh *   2. Approximation of log1p(f).
322116Sjkh *	Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
332116Sjkh *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
342116Sjkh *	     	 = 2s + s*R
358870Srgrimes *      We use a special Reme algorithm on [0,0.1716] to generate
368870Srgrimes * 	a polynomial of degree 14 to approximate R The maximum error
372116Sjkh *	of this polynomial approximation is bounded by 2**-58.45. In
382116Sjkh *	other words,
392116Sjkh *		        2      4      6      8      10      12      14
402116Sjkh *	    R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s  +Lp6*s  +Lp7*s
412116Sjkh *  	(the values of Lp1 to Lp7 are listed in the program)
422116Sjkh *	and
432116Sjkh *	    |      2          14          |     -58.45
448870Srgrimes *	    | Lp1*s +...+Lp7*s    -  R(z) | <= 2
452116Sjkh *	    |                             |
462116Sjkh *	Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
472116Sjkh *	In order to guarantee error in log below 1ulp, we compute log
482116Sjkh *	by
492116Sjkh *		log1p(f) = f - (hfsq - s*(hfsq+R)).
508870Srgrimes *
518870Srgrimes *	3. Finally, log1p(x) = k*ln2 + log1p(f).
522116Sjkh *		 	     = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
538870Srgrimes *	   Here ln2 is split into two floating point number:
542116Sjkh *			ln2_hi + ln2_lo,
552116Sjkh *	   where n*ln2_hi is always exact for |n| < 2000.
562116Sjkh *
572116Sjkh * Special cases:
588870Srgrimes *	log1p(x) is NaN with signal if x < -1 (including -INF) ;
592116Sjkh *	log1p(+INF) is +INF; log1p(-1) is -INF with signal;
602116Sjkh *	log1p(NaN) is that NaN with no signal.
612116Sjkh *
622116Sjkh * Accuracy:
632116Sjkh *	according to an error analysis, the error is always less than
642116Sjkh *	1 ulp (unit in the last place).
652116Sjkh *
662116Sjkh * Constants:
678870Srgrimes * The hexadecimal values are the intended ones for the following
688870Srgrimes * constants. The decimal values may be used, provided that the
698870Srgrimes * compiler will convert from decimal to binary accurately enough
702116Sjkh * to produce the hexadecimal values shown.
712116Sjkh *
722116Sjkh * Note: Assuming log() return accurate answer, the following
732116Sjkh * 	 algorithm can be used to compute log1p(x) to within a few ULP:
748870Srgrimes *
752116Sjkh *		u = 1+x;
762116Sjkh *		if(u==1.0) return x ; else
772116Sjkh *			   return log(u)*(x/(u-1.0));
782116Sjkh *
792116Sjkh *	 See HP-15C Advanced Functions Handbook, p.193.
802116Sjkh */
812116Sjkh
822116Sjkh#include "math.h"
832116Sjkh#include "math_private.h"
842116Sjkh
852116Sjkh#ifdef __STDC__
862116Sjkhstatic const double
872116Sjkh#else
882116Sjkhstatic double
892116Sjkh#endif
902116Sjkhln2_hi  =  6.93147180369123816490e-01,	/* 3fe62e42 fee00000 */
912116Sjkhln2_lo  =  1.90821492927058770002e-10,	/* 3dea39ef 35793c76 */
922116Sjkhtwo54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
932116SjkhLp1 = 6.666666666666735130e-01,  /* 3FE55555 55555593 */
942116SjkhLp2 = 3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
952116SjkhLp3 = 2.857142874366239149e-01,  /* 3FD24924 94229359 */
962116SjkhLp4 = 2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
972116SjkhLp5 = 1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
982116SjkhLp6 = 1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
992116SjkhLp7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
1002116Sjkh
1012116Sjkh#ifdef __STDC__
1022116Sjkhstatic const double zero = 0.0;
1032116Sjkh#else
1042116Sjkhstatic double zero = 0.0;
1052116Sjkh#endif
1062116Sjkh
1072116Sjkh#ifdef __STDC__
1082116Sjkh	double log1p(double x)
1092116Sjkh#else
1102116Sjkh	double log1p(x)
1112116Sjkh	double x;
1122116Sjkh#endif
1132116Sjkh{
1142116Sjkh	double hfsq,f,c,s,z,R,u;
1152116Sjkh	int32_t k,hx,hu,ax;
1162116Sjkh
1172116Sjkh	GET_HIGH_WORD(hx,x);
1182116Sjkh	ax = hx&0x7fffffff;
1192116Sjkh
1202116Sjkh	k = 1;
1212116Sjkh	if (hx < 0x3FDA827A) {			/* x < 0.41422  */
1222116Sjkh	    if(ax>=0x3ff00000) {		/* x <= -1.0 */
1232116Sjkh		if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */
1242116Sjkh		else return (x-x)/(x-x);	/* log1p(x<-1)=NaN */
1252116Sjkh	    }
1262116Sjkh	    if(ax<0x3e200000) {			/* |x| < 2**-29 */
1272116Sjkh		if(two54+x>zero			/* raise inexact */
1282116Sjkh	            &&ax<0x3c900000) 		/* |x| < 2**-54 */
1292116Sjkh		    return x;
1302116Sjkh		else
1312116Sjkh		    return x - x*x*0.5;
1322116Sjkh	    }
1332116Sjkh	    if(hx>0||hx<=((int32_t)0xbfd2bec3)) {
1342116Sjkh		k=0;f=x;hu=1;}	/* -0.2929<x<0.41422 */
1358870Srgrimes	}
1362116Sjkh	if (hx >= 0x7ff00000) return x+x;
1372116Sjkh	if(k!=0) {
1382116Sjkh	    if(hx<0x43400000) {
1398870Srgrimes		u  = 1.0+x;
1402116Sjkh		GET_HIGH_WORD(hu,u);
1412116Sjkh	        k  = (hu>>20)-1023;
1422116Sjkh	        c  = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */
1432116Sjkh		c /= u;
1442116Sjkh	    } else {
1452116Sjkh		u  = x;
1462116Sjkh		GET_HIGH_WORD(hu,u);
1472116Sjkh	        k  = (hu>>20)-1023;
1482116Sjkh		c  = 0;
1492116Sjkh	    }
1502116Sjkh	    hu &= 0x000fffff;
1512116Sjkh	    if(hu<0x6a09e) {
1522116Sjkh	        SET_HIGH_WORD(u,hu|0x3ff00000);	/* normalize u */
1532116Sjkh	    } else {
1548870Srgrimes	        k += 1;
1552116Sjkh		SET_HIGH_WORD(u,hu|0x3fe00000);	/* normalize u/2 */
1562116Sjkh	        hu = (0x00100000-hu)>>2;
1572116Sjkh	    }
1582116Sjkh	    f = u-1.0;
1592116Sjkh	}
1602116Sjkh	hfsq=0.5*f*f;
1612116Sjkh	if(hu==0) {	/* |f| < 2**-20 */
1628870Srgrimes	    if(f==zero) if(k==0) return zero;
1632116Sjkh			else {c += k*ln2_lo; return k*ln2_hi+c;}
1642116Sjkh	    R = hfsq*(1.0-0.66666666666666666*f);
1652116Sjkh	    if(k==0) return f-R; else
1662116Sjkh	    	     return k*ln2_hi-((R-(k*ln2_lo+c))-f);
1672116Sjkh	}
1688870Srgrimes 	s = f/(2.0+f);
1692116Sjkh	z = s*s;
1702116Sjkh	R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
1712116Sjkh	if(k==0) return f-(hfsq-s*(hfsq+R)); else
1722116Sjkh		 return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
1732116Sjkh}
174