xref: /freebsd/lib/msun/src/s_log1p.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
13a8617a8SJordan K. Hubbard /*
23a8617a8SJordan K. Hubbard  * ====================================================
33a8617a8SJordan K. Hubbard  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
43a8617a8SJordan K. Hubbard  *
53a8617a8SJordan K. Hubbard  * Developed at SunPro, a Sun Microsystems, Inc. business.
63a8617a8SJordan K. Hubbard  * Permission to use, copy, modify, and distribute this
73a8617a8SJordan K. Hubbard  * software is freely granted, provided that this notice
83a8617a8SJordan K. Hubbard  * is preserved.
93a8617a8SJordan K. Hubbard  * ====================================================
103a8617a8SJordan K. Hubbard  */
113a8617a8SJordan K. Hubbard 
123a8617a8SJordan K. Hubbard /* double log1p(double x)
133a8617a8SJordan K. Hubbard  *
143a8617a8SJordan K. Hubbard  * Method :
153a8617a8SJordan K. Hubbard  *   1. Argument Reduction: find k and f such that
163a8617a8SJordan K. Hubbard  *			1+x = 2^k * (1+f),
173a8617a8SJordan K. Hubbard  *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
183a8617a8SJordan K. Hubbard  *
193a8617a8SJordan K. Hubbard  *      Note. If k=0, then f=x is exact. However, if k!=0, then f
203a8617a8SJordan K. Hubbard  *	may not be representable exactly. In that case, a correction
213a8617a8SJordan K. Hubbard  *	term is need. Let u=1+x rounded. Let c = (1+x)-u, then
223a8617a8SJordan K. Hubbard  *	log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
233a8617a8SJordan K. Hubbard  *	and add back the correction term c/u.
243a8617a8SJordan K. Hubbard  *	(Note: when x > 2**53, one can simply return log(x))
253a8617a8SJordan K. Hubbard  *
263a8617a8SJordan K. Hubbard  *   2. Approximation of log1p(f).
273a8617a8SJordan K. Hubbard  *	Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
283a8617a8SJordan K. Hubbard  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
293a8617a8SJordan K. Hubbard  *	     	 = 2s + s*R
303a8617a8SJordan K. Hubbard  *      We use a special Reme algorithm on [0,0.1716] to generate
313a8617a8SJordan K. Hubbard  * 	a polynomial of degree 14 to approximate R The maximum error
323a8617a8SJordan K. Hubbard  *	of this polynomial approximation is bounded by 2**-58.45. In
333a8617a8SJordan K. Hubbard  *	other words,
343a8617a8SJordan K. Hubbard  *		        2      4      6      8      10      12      14
353a8617a8SJordan K. Hubbard  *	    R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s  +Lp6*s  +Lp7*s
363a8617a8SJordan K. Hubbard  *  	(the values of Lp1 to Lp7 are listed in the program)
373a8617a8SJordan K. Hubbard  *	and
383a8617a8SJordan K. Hubbard  *	    |      2          14          |     -58.45
393a8617a8SJordan K. Hubbard  *	    | Lp1*s +...+Lp7*s    -  R(z) | <= 2
403a8617a8SJordan K. Hubbard  *	    |                             |
413a8617a8SJordan K. Hubbard  *	Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
423a8617a8SJordan K. Hubbard  *	In order to guarantee error in log below 1ulp, we compute log
433a8617a8SJordan K. Hubbard  *	by
443a8617a8SJordan K. Hubbard  *		log1p(f) = f - (hfsq - s*(hfsq+R)).
453a8617a8SJordan K. Hubbard  *
463a8617a8SJordan K. Hubbard  *	3. Finally, log1p(x) = k*ln2 + log1p(f).
473a8617a8SJordan K. Hubbard  *		 	     = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
483a8617a8SJordan K. Hubbard  *	   Here ln2 is split into two floating point number:
493a8617a8SJordan K. Hubbard  *			ln2_hi + ln2_lo,
503a8617a8SJordan K. Hubbard  *	   where n*ln2_hi is always exact for |n| < 2000.
513a8617a8SJordan K. Hubbard  *
523a8617a8SJordan K. Hubbard  * Special cases:
533a8617a8SJordan K. Hubbard  *	log1p(x) is NaN with signal if x < -1 (including -INF) ;
543a8617a8SJordan K. Hubbard  *	log1p(+INF) is +INF; log1p(-1) is -INF with signal;
553a8617a8SJordan K. Hubbard  *	log1p(NaN) is that NaN with no signal.
563a8617a8SJordan K. Hubbard  *
573a8617a8SJordan K. Hubbard  * Accuracy:
583a8617a8SJordan K. Hubbard  *	according to an error analysis, the error is always less than
593a8617a8SJordan K. Hubbard  *	1 ulp (unit in the last place).
603a8617a8SJordan K. Hubbard  *
613a8617a8SJordan K. Hubbard  * Constants:
623a8617a8SJordan K. Hubbard  * The hexadecimal values are the intended ones for the following
633a8617a8SJordan K. Hubbard  * constants. The decimal values may be used, provided that the
643a8617a8SJordan K. Hubbard  * compiler will convert from decimal to binary accurately enough
653a8617a8SJordan K. Hubbard  * to produce the hexadecimal values shown.
663a8617a8SJordan K. Hubbard  *
673a8617a8SJordan K. Hubbard  * Note: Assuming log() return accurate answer, the following
683a8617a8SJordan K. Hubbard  * 	 algorithm can be used to compute log1p(x) to within a few ULP:
693a8617a8SJordan K. Hubbard  *
703a8617a8SJordan K. Hubbard  *		u = 1+x;
713a8617a8SJordan K. Hubbard  *		if(u==1.0) return x ; else
723a8617a8SJordan K. Hubbard  *			   return log(u)*(x/(u-1.0));
733a8617a8SJordan K. Hubbard  *
743a8617a8SJordan K. Hubbard  *	 See HP-15C Advanced Functions Handbook, p.193.
753a8617a8SJordan K. Hubbard  */
763a8617a8SJordan K. Hubbard 
770814af48SBruce Evans #include <float.h>
780814af48SBruce Evans 
793a8617a8SJordan K. Hubbard #include "math.h"
803a8617a8SJordan K. Hubbard #include "math_private.h"
813a8617a8SJordan K. Hubbard 
823a8617a8SJordan K. Hubbard static const double
833a8617a8SJordan K. Hubbard ln2_hi  =  6.93147180369123816490e-01,	/* 3fe62e42 fee00000 */
843a8617a8SJordan K. Hubbard ln2_lo  =  1.90821492927058770002e-10,	/* 3dea39ef 35793c76 */
853a8617a8SJordan K. Hubbard two54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
863a8617a8SJordan K. Hubbard Lp1 = 6.666666666666735130e-01,  /* 3FE55555 55555593 */
873a8617a8SJordan K. Hubbard Lp2 = 3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
883a8617a8SJordan K. Hubbard Lp3 = 2.857142874366239149e-01,  /* 3FD24924 94229359 */
893a8617a8SJordan K. Hubbard Lp4 = 2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
903a8617a8SJordan K. Hubbard Lp5 = 1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
913a8617a8SJordan K. Hubbard Lp6 = 1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
923a8617a8SJordan K. Hubbard Lp7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
933a8617a8SJordan K. Hubbard 
943a8617a8SJordan K. Hubbard static const double zero = 0.0;
957dbbb6ddSDavid Schultz static volatile double vzero = 0.0;
963a8617a8SJordan K. Hubbard 
9759b19ff1SAlfred Perlstein double
log1p(double x)9859b19ff1SAlfred Perlstein log1p(double x)
993a8617a8SJordan K. Hubbard {
1003a8617a8SJordan K. Hubbard 	double hfsq,f,c,s,z,R,u;
1013a8617a8SJordan K. Hubbard 	int32_t k,hx,hu,ax;
1023a8617a8SJordan K. Hubbard 
1033a8617a8SJordan K. Hubbard 	GET_HIGH_WORD(hx,x);
1043a8617a8SJordan K. Hubbard 	ax = hx&0x7fffffff;
1053a8617a8SJordan K. Hubbard 
1063a8617a8SJordan K. Hubbard 	k = 1;
107d48ea975SBruce Evans 	if (hx < 0x3FDA827A) {			/* 1+x < sqrt(2)+ */
1083a8617a8SJordan K. Hubbard 	    if(ax>=0x3ff00000) {		/* x <= -1.0 */
1097dbbb6ddSDavid Schultz 		if(x==-1.0) return -two54/vzero; /* log1p(-1)=+inf */
1103a8617a8SJordan K. Hubbard 		else return (x-x)/(x-x);	/* log1p(x<-1)=NaN */
1113a8617a8SJordan K. Hubbard 	    }
1123a8617a8SJordan K. Hubbard 	    if(ax<0x3e200000) {			/* |x| < 2**-29 */
1133a8617a8SJordan K. Hubbard 		if(two54+x>zero			/* raise inexact */
1143a8617a8SJordan K. Hubbard 	            &&ax<0x3c900000) 		/* |x| < 2**-54 */
1153a8617a8SJordan K. Hubbard 		    return x;
1163a8617a8SJordan K. Hubbard 		else
1173a8617a8SJordan K. Hubbard 		    return x - x*x*0.5;
1183a8617a8SJordan K. Hubbard 	    }
119d48ea975SBruce Evans 	    if(hx>0||hx<=((int32_t)0xbfd2bec4)) {
120d48ea975SBruce Evans 		k=0;f=x;hu=1;}		/* sqrt(2)/2- <= 1+x < sqrt(2)+ */
1213a8617a8SJordan K. Hubbard 	}
1223a8617a8SJordan K. Hubbard 	if (hx >= 0x7ff00000) return x+x;
1233a8617a8SJordan K. Hubbard 	if(k!=0) {
1243a8617a8SJordan K. Hubbard 	    if(hx<0x43400000) {
1250814af48SBruce Evans 		STRICT_ASSIGN(double,u,1.0+x);
1263a8617a8SJordan K. Hubbard 		GET_HIGH_WORD(hu,u);
1273a8617a8SJordan K. Hubbard 	        k  = (hu>>20)-1023;
1283a8617a8SJordan K. Hubbard 	        c  = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */
1293a8617a8SJordan K. Hubbard 		c /= u;
1303a8617a8SJordan K. Hubbard 	    } else {
1313a8617a8SJordan K. Hubbard 		u  = x;
1323a8617a8SJordan K. Hubbard 		GET_HIGH_WORD(hu,u);
1333a8617a8SJordan K. Hubbard 	        k  = (hu>>20)-1023;
1343a8617a8SJordan K. Hubbard 		c  = 0;
1353a8617a8SJordan K. Hubbard 	    }
1363a8617a8SJordan K. Hubbard 	    hu &= 0x000fffff;
137d48ea975SBruce Evans 	    /*
138d48ea975SBruce Evans 	     * The approximation to sqrt(2) used in thresholds is not
139d48ea975SBruce Evans 	     * critical.  However, the ones used above must give less
140d48ea975SBruce Evans 	     * strict bounds than the one here so that the k==0 case is
141d48ea975SBruce Evans 	     * never reached from here, since here we have committed to
142d48ea975SBruce Evans 	     * using the correction term but don't use it if k==0.
143d48ea975SBruce Evans 	     */
144d48ea975SBruce Evans 	    if(hu<0x6a09e) {			/* u ~< sqrt(2) */
1453a8617a8SJordan K. Hubbard 	        SET_HIGH_WORD(u,hu|0x3ff00000);	/* normalize u */
1463a8617a8SJordan K. Hubbard 	    } else {
1473a8617a8SJordan K. Hubbard 	        k += 1;
1483a8617a8SJordan K. Hubbard 		SET_HIGH_WORD(u,hu|0x3fe00000);	/* normalize u/2 */
1493a8617a8SJordan K. Hubbard 	        hu = (0x00100000-hu)>>2;
1503a8617a8SJordan K. Hubbard 	    }
1513a8617a8SJordan K. Hubbard 	    f = u-1.0;
1523a8617a8SJordan K. Hubbard 	}
1533a8617a8SJordan K. Hubbard 	hfsq=0.5*f*f;
1543a8617a8SJordan K. Hubbard 	if(hu==0) {	/* |f| < 2**-20 */
155ee0730e6SDavid Schultz 	    if(f==zero) {
156ee0730e6SDavid Schultz 		if(k==0) {
157ee0730e6SDavid Schultz 		    return zero;
158ee0730e6SDavid Schultz 		} else {
159ee0730e6SDavid Schultz 		    c += k*ln2_lo;
160ee0730e6SDavid Schultz 		    return k*ln2_hi+c;
161ee0730e6SDavid Schultz 		}
162ee0730e6SDavid Schultz 	    }
1633a8617a8SJordan K. Hubbard 	    R = hfsq*(1.0-0.66666666666666666*f);
1643a8617a8SJordan K. Hubbard 	    if(k==0) return f-R; else
1653a8617a8SJordan K. Hubbard 	    	     return k*ln2_hi-((R-(k*ln2_lo+c))-f);
1663a8617a8SJordan K. Hubbard 	}
1673a8617a8SJordan K. Hubbard  	s = f/(2.0+f);
1683a8617a8SJordan K. Hubbard 	z = s*s;
1693a8617a8SJordan K. Hubbard 	R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
1703a8617a8SJordan K. Hubbard 	if(k==0) return f-(hfsq-s*(hfsq+R)); else
1713a8617a8SJordan K. Hubbard 		 return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
1723a8617a8SJordan K. Hubbard }
173*25a4d6bfSDavid Schultz 
174*25a4d6bfSDavid Schultz #if (LDBL_MANT_DIG == 53)
175*25a4d6bfSDavid Schultz __weak_reference(log1p, log1pl);
176*25a4d6bfSDavid Schultz #endif
177