s_log1p.c revision 251292
1219820Sjeff/* @(#)s_log1p.c 5.1 93/09/24 */
2219820Sjeff/*
3219820Sjeff * ====================================================
4219820Sjeff * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5219820Sjeff *
6219820Sjeff * Developed at SunPro, a Sun Microsystems, Inc. business.
7219820Sjeff * Permission to use, copy, modify, and distribute this
8219820Sjeff * software is freely granted, provided that this notice
9219820Sjeff * is preserved.
10219820Sjeff * ====================================================
11219820Sjeff */
12219820Sjeff
13219820Sjeff#include <sys/cdefs.h>
14219820Sjeff__FBSDID("$FreeBSD: head/lib/msun/src/s_log1p.c 251292 2013-06-03 09:14:31Z das $");
15219820Sjeff
16219820Sjeff/* double log1p(double x)
17219820Sjeff *
18219820Sjeff * Method :
19219820Sjeff *   1. Argument Reduction: find k and f such that
20219820Sjeff *			1+x = 2^k * (1+f),
21219820Sjeff *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
22219820Sjeff *
23219820Sjeff *      Note. If k=0, then f=x is exact. However, if k!=0, then f
24219820Sjeff *	may not be representable exactly. In that case, a correction
25219820Sjeff *	term is need. Let u=1+x rounded. Let c = (1+x)-u, then
26219820Sjeff *	log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
27219820Sjeff *	and add back the correction term c/u.
28219820Sjeff *	(Note: when x > 2**53, one can simply return log(x))
29219820Sjeff *
30219820Sjeff *   2. Approximation of log1p(f).
31219820Sjeff *	Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
32219820Sjeff *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
33219820Sjeff *	     	 = 2s + s*R
34219820Sjeff *      We use a special Reme algorithm on [0,0.1716] to generate
35219820Sjeff * 	a polynomial of degree 14 to approximate R The maximum error
36219820Sjeff *	of this polynomial approximation is bounded by 2**-58.45. In
37219820Sjeff *	other words,
38219820Sjeff *		        2      4      6      8      10      12      14
39219820Sjeff *	    R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s  +Lp6*s  +Lp7*s
40219820Sjeff *  	(the values of Lp1 to Lp7 are listed in the program)
41219820Sjeff *	and
42219820Sjeff *	    |      2          14          |     -58.45
43219820Sjeff *	    | Lp1*s +...+Lp7*s    -  R(z) | <= 2
44219820Sjeff *	    |                             |
45219820Sjeff *	Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
46219820Sjeff *	In order to guarantee error in log below 1ulp, we compute log
47219820Sjeff *	by
48219820Sjeff *		log1p(f) = f - (hfsq - s*(hfsq+R)).
49219820Sjeff *
50219820Sjeff *	3. Finally, log1p(x) = k*ln2 + log1p(f).
51219820Sjeff *		 	     = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
52219820Sjeff *	   Here ln2 is split into two floating point number:
53219820Sjeff *			ln2_hi + ln2_lo,
54219820Sjeff *	   where n*ln2_hi is always exact for |n| < 2000.
55219820Sjeff *
56219820Sjeff * Special cases:
57219820Sjeff *	log1p(x) is NaN with signal if x < -1 (including -INF) ;
58219820Sjeff *	log1p(+INF) is +INF; log1p(-1) is -INF with signal;
59219820Sjeff *	log1p(NaN) is that NaN with no signal.
60219820Sjeff *
61219820Sjeff * Accuracy:
62219820Sjeff *	according to an error analysis, the error is always less than
63219820Sjeff *	1 ulp (unit in the last place).
64219820Sjeff *
65219820Sjeff * Constants:
66219820Sjeff * The hexadecimal values are the intended ones for the following
67219820Sjeff * constants. The decimal values may be used, provided that the
68219820Sjeff * compiler will convert from decimal to binary accurately enough
69219820Sjeff * to produce the hexadecimal values shown.
70219820Sjeff *
71219820Sjeff * Note: Assuming log() return accurate answer, the following
72219820Sjeff * 	 algorithm can be used to compute log1p(x) to within a few ULP:
73219820Sjeff *
74219820Sjeff *		u = 1+x;
75219820Sjeff *		if(u==1.0) return x ; else
76219820Sjeff *			   return log(u)*(x/(u-1.0));
77219820Sjeff *
78219820Sjeff *	 See HP-15C Advanced Functions Handbook, p.193.
79219820Sjeff */
80219820Sjeff
81219820Sjeff#include <float.h>
82219820Sjeff
83219820Sjeff#include "math.h"
84219820Sjeff#include "math_private.h"
85219820Sjeff
86219820Sjeffstatic const double
87219820Sjeffln2_hi  =  6.93147180369123816490e-01,	/* 3fe62e42 fee00000 */
88219820Sjeffln2_lo  =  1.90821492927058770002e-10,	/* 3dea39ef 35793c76 */
89219820Sjefftwo54   =  1.80143985094819840000e+16,  /* 43500000 00000000 */
90219820SjeffLp1 = 6.666666666666735130e-01,  /* 3FE55555 55555593 */
91219820SjeffLp2 = 3.999999999940941908e-01,  /* 3FD99999 9997FA04 */
92219820SjeffLp3 = 2.857142874366239149e-01,  /* 3FD24924 94229359 */
93219820SjeffLp4 = 2.222219843214978396e-01,  /* 3FCC71C5 1D8E78AF */
94219820SjeffLp5 = 1.818357216161805012e-01,  /* 3FC74664 96CB03DE */
95219820SjeffLp6 = 1.531383769920937332e-01,  /* 3FC39A09 D078C69F */
96219820SjeffLp7 = 1.479819860511658591e-01;  /* 3FC2F112 DF3E5244 */
97219820Sjeff
98219820Sjeffstatic const double zero = 0.0;
99219820Sjeffstatic volatile double vzero = 0.0;
100219820Sjeff
101219820Sjeffdouble
102219820Sjefflog1p(double x)
103219820Sjeff{
104219820Sjeff	double hfsq,f,c,s,z,R,u;
105219820Sjeff	int32_t k,hx,hu,ax;
106219820Sjeff
107219820Sjeff	GET_HIGH_WORD(hx,x);
108219820Sjeff	ax = hx&0x7fffffff;
109219820Sjeff
110219820Sjeff	k = 1;
111219820Sjeff	if (hx < 0x3FDA827A) {			/* 1+x < sqrt(2)+ */
112219820Sjeff	    if(ax>=0x3ff00000) {		/* x <= -1.0 */
113219820Sjeff		if(x==-1.0) return -two54/vzero; /* log1p(-1)=+inf */
114219820Sjeff		else return (x-x)/(x-x);	/* log1p(x<-1)=NaN */
115219820Sjeff	    }
116219820Sjeff	    if(ax<0x3e200000) {			/* |x| < 2**-29 */
117219820Sjeff		if(two54+x>zero			/* raise inexact */
118219820Sjeff	            &&ax<0x3c900000) 		/* |x| < 2**-54 */
119219820Sjeff		    return x;
120219820Sjeff		else
121219820Sjeff		    return x - x*x*0.5;
122219820Sjeff	    }
123219820Sjeff	    if(hx>0||hx<=((int32_t)0xbfd2bec4)) {
124219820Sjeff		k=0;f=x;hu=1;}		/* sqrt(2)/2- <= 1+x < sqrt(2)+ */
125219820Sjeff	}
126219820Sjeff	if (hx >= 0x7ff00000) return x+x;
127219820Sjeff	if(k!=0) {
128219820Sjeff	    if(hx<0x43400000) {
129219820Sjeff		STRICT_ASSIGN(double,u,1.0+x);
130219820Sjeff		GET_HIGH_WORD(hu,u);
131219820Sjeff	        k  = (hu>>20)-1023;
132219820Sjeff	        c  = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */
133219820Sjeff		c /= u;
134219820Sjeff	    } else {
135219820Sjeff		u  = x;
136219820Sjeff		GET_HIGH_WORD(hu,u);
137219820Sjeff	        k  = (hu>>20)-1023;
138219820Sjeff		c  = 0;
139219820Sjeff	    }
140219820Sjeff	    hu &= 0x000fffff;
141219820Sjeff	    /*
142219820Sjeff	     * The approximation to sqrt(2) used in thresholds is not
143	     * critical.  However, the ones used above must give less
144	     * strict bounds than the one here so that the k==0 case is
145	     * never reached from here, since here we have committed to
146	     * using the correction term but don't use it if k==0.
147	     */
148	    if(hu<0x6a09e) {			/* u ~< sqrt(2) */
149	        SET_HIGH_WORD(u,hu|0x3ff00000);	/* normalize u */
150	    } else {
151	        k += 1;
152		SET_HIGH_WORD(u,hu|0x3fe00000);	/* normalize u/2 */
153	        hu = (0x00100000-hu)>>2;
154	    }
155	    f = u-1.0;
156	}
157	hfsq=0.5*f*f;
158	if(hu==0) {	/* |f| < 2**-20 */
159	    if(f==zero) {
160		if(k==0) {
161		    return zero;
162		} else {
163		    c += k*ln2_lo;
164		    return k*ln2_hi+c;
165		}
166	    }
167	    R = hfsq*(1.0-0.66666666666666666*f);
168	    if(k==0) return f-R; else
169	    	     return k*ln2_hi-((R-(k*ln2_lo+c))-f);
170	}
171 	s = f/(2.0+f);
172	z = s*s;
173	R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
174	if(k==0) return f-(hfsq-s*(hfsq+R)); else
175		 return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
176}
177
178#if (LDBL_MANT_DIG == 53)
179__weak_reference(log1p, log1pl);
180#endif
181