xref: /titanic_50/usr/src/lib/libm/common/C/log1p.c (revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af)
125c28e83SPiotr Jasiukajtis /*
225c28e83SPiotr Jasiukajtis  * CDDL HEADER START
325c28e83SPiotr Jasiukajtis  *
425c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
525c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
625c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
725c28e83SPiotr Jasiukajtis  *
825c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
1025c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
1125c28e83SPiotr Jasiukajtis  * and limitations under the License.
1225c28e83SPiotr Jasiukajtis  *
1325c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
1425c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
1625c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
1725c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
1825c28e83SPiotr Jasiukajtis  *
1925c28e83SPiotr Jasiukajtis  * CDDL HEADER END
2025c28e83SPiotr Jasiukajtis  */
2125c28e83SPiotr Jasiukajtis /*
2225c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
2325c28e83SPiotr Jasiukajtis  */
2425c28e83SPiotr Jasiukajtis /*
2525c28e83SPiotr Jasiukajtis  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
2625c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
2725c28e83SPiotr Jasiukajtis  */
2825c28e83SPiotr Jasiukajtis 
29*ddc0e0b5SRichard Lowe #pragma weak __log1p = log1p
3025c28e83SPiotr Jasiukajtis 
3125c28e83SPiotr Jasiukajtis /* INDENT OFF */
3225c28e83SPiotr Jasiukajtis /*
3325c28e83SPiotr Jasiukajtis  * Method :
3425c28e83SPiotr Jasiukajtis  *   1. Argument Reduction: find k and f such that
3525c28e83SPiotr Jasiukajtis  *			1+x = 2^k * (1+f),
3625c28e83SPiotr Jasiukajtis  *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
3725c28e83SPiotr Jasiukajtis  *
3825c28e83SPiotr Jasiukajtis  *      Note. If k=0, then f=x is exact. However, if k != 0, then f
3925c28e83SPiotr Jasiukajtis  *	may not be representable exactly. In that case, a correction
4025c28e83SPiotr Jasiukajtis  *	term is need. Let u=1+x rounded. Let c = (1+x)-u, then
4125c28e83SPiotr Jasiukajtis  *	log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
4225c28e83SPiotr Jasiukajtis  *	and add back the correction term c/u.
4325c28e83SPiotr Jasiukajtis  *	(Note: when x > 2**53, one can simply return log(x))
4425c28e83SPiotr Jasiukajtis  *
4525c28e83SPiotr Jasiukajtis  *   2. Approximation of log1p(f).
4625c28e83SPiotr Jasiukajtis  *	Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
4725c28e83SPiotr Jasiukajtis  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
4825c28e83SPiotr Jasiukajtis  *		 = 2s + s*R
4925c28e83SPiotr Jasiukajtis  *      We use a special Reme algorithm on [0,0.1716] to generate
5025c28e83SPiotr Jasiukajtis  * 	a polynomial of degree 14 to approximate R The maximum error
5125c28e83SPiotr Jasiukajtis  *	of this polynomial approximation is bounded by 2**-58.45. In
5225c28e83SPiotr Jasiukajtis  *	other words,
5325c28e83SPiotr Jasiukajtis  *		        2      4      6      8      10      12      14
5425c28e83SPiotr Jasiukajtis  *	    R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s  +Lp6*s  +Lp7*s
5525c28e83SPiotr Jasiukajtis  *  	(the values of Lp1 to Lp7 are listed in the program)
5625c28e83SPiotr Jasiukajtis  *	and
5725c28e83SPiotr Jasiukajtis  *	    |      2          14          |     -58.45
5825c28e83SPiotr Jasiukajtis  *	    | Lp1*s +...+Lp7*s    -  R(z) | <= 2
5925c28e83SPiotr Jasiukajtis  *	    |                             |
6025c28e83SPiotr Jasiukajtis  *	Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
6125c28e83SPiotr Jasiukajtis  *	In order to guarantee error in log below 1ulp, we compute log
6225c28e83SPiotr Jasiukajtis  *	by
6325c28e83SPiotr Jasiukajtis  *		log1p(f) = f - (hfsq - s*(hfsq+R)).
6425c28e83SPiotr Jasiukajtis  *
6525c28e83SPiotr Jasiukajtis  *	3. Finally, log1p(x) = k*ln2 + log1p(f).
6625c28e83SPiotr Jasiukajtis  *			     = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
6725c28e83SPiotr Jasiukajtis  *	   Here ln2 is splitted into two floating point number:
6825c28e83SPiotr Jasiukajtis  *			ln2_hi + ln2_lo,
6925c28e83SPiotr Jasiukajtis  *	   where n*ln2_hi is always exact for |n| < 2000.
7025c28e83SPiotr Jasiukajtis  *
7125c28e83SPiotr Jasiukajtis  * Special cases:
7225c28e83SPiotr Jasiukajtis  *	log1p(x) is NaN with signal if x < -1 (including -INF) ;
7325c28e83SPiotr Jasiukajtis  *	log1p(+INF) is +INF; log1p(-1) is -INF with signal;
7425c28e83SPiotr Jasiukajtis  *	log1p(NaN) is that NaN with no signal.
7525c28e83SPiotr Jasiukajtis  *
7625c28e83SPiotr Jasiukajtis  * Accuracy:
7725c28e83SPiotr Jasiukajtis  *	according to an error analysis, the error is always less than
7825c28e83SPiotr Jasiukajtis  *	1 ulp (unit in the last place).
7925c28e83SPiotr Jasiukajtis  *
8025c28e83SPiotr Jasiukajtis  * Constants:
8125c28e83SPiotr Jasiukajtis  * The hexadecimal values are the intended ones for the following
8225c28e83SPiotr Jasiukajtis  * constants. The decimal values may be used, provided that the
8325c28e83SPiotr Jasiukajtis  * compiler will convert from decimal to binary accurately enough
8425c28e83SPiotr Jasiukajtis  * to produce the hexadecimal values shown.
8525c28e83SPiotr Jasiukajtis  *
8625c28e83SPiotr Jasiukajtis  * Note: Assuming log() return accurate answer, the following
8725c28e83SPiotr Jasiukajtis  *	 algorithm can be used to compute log1p(x) to within a few ULP:
8825c28e83SPiotr Jasiukajtis  *
8925c28e83SPiotr Jasiukajtis  *		u = 1+x;
9025c28e83SPiotr Jasiukajtis  *		if (u == 1.0) return x ; else
9125c28e83SPiotr Jasiukajtis  *			   return log(u)*(x/(u-1.0));
9225c28e83SPiotr Jasiukajtis  *
9325c28e83SPiotr Jasiukajtis  *	 See HP-15C Advanced Functions Handbook, p.193.
9425c28e83SPiotr Jasiukajtis  */
9525c28e83SPiotr Jasiukajtis /* INDENT ON */
9625c28e83SPiotr Jasiukajtis 
9725c28e83SPiotr Jasiukajtis #include "libm.h"
9825c28e83SPiotr Jasiukajtis 
9925c28e83SPiotr Jasiukajtis static const double xxx[] = {
10025c28e83SPiotr Jasiukajtis /* ln2_hi */	6.93147180369123816490e-01,	/* 3fe62e42 fee00000 */
10125c28e83SPiotr Jasiukajtis /* ln2_lo */	1.90821492927058770002e-10,	/* 3dea39ef 35793c76 */
10225c28e83SPiotr Jasiukajtis /* two54 */	1.80143985094819840000e+16,	/* 43500000 00000000 */
10325c28e83SPiotr Jasiukajtis /* Lp1 */	6.666666666666735130e-01,	/* 3FE55555 55555593 */
10425c28e83SPiotr Jasiukajtis /* Lp2 */	3.999999999940941908e-01,	/* 3FD99999 9997FA04 */
10525c28e83SPiotr Jasiukajtis /* Lp3 */	2.857142874366239149e-01,	/* 3FD24924 94229359 */
10625c28e83SPiotr Jasiukajtis /* Lp4 */	2.222219843214978396e-01,	/* 3FCC71C5 1D8E78AF */
10725c28e83SPiotr Jasiukajtis /* Lp5 */	1.818357216161805012e-01,	/* 3FC74664 96CB03DE */
10825c28e83SPiotr Jasiukajtis /* Lp6 */	1.531383769920937332e-01,	/* 3FC39A09 D078C69F */
10925c28e83SPiotr Jasiukajtis /* Lp7 */	1.479819860511658591e-01,	/* 3FC2F112 DF3E5244 */
11025c28e83SPiotr Jasiukajtis /* zero */	0.0
11125c28e83SPiotr Jasiukajtis };
11225c28e83SPiotr Jasiukajtis #define	ln2_hi	xxx[0]
11325c28e83SPiotr Jasiukajtis #define	ln2_lo	xxx[1]
11425c28e83SPiotr Jasiukajtis #define	two54	xxx[2]
11525c28e83SPiotr Jasiukajtis #define	Lp1	xxx[3]
11625c28e83SPiotr Jasiukajtis #define	Lp2	xxx[4]
11725c28e83SPiotr Jasiukajtis #define	Lp3	xxx[5]
11825c28e83SPiotr Jasiukajtis #define	Lp4	xxx[6]
11925c28e83SPiotr Jasiukajtis #define	Lp5	xxx[7]
12025c28e83SPiotr Jasiukajtis #define	Lp6	xxx[8]
12125c28e83SPiotr Jasiukajtis #define	Lp7	xxx[9]
12225c28e83SPiotr Jasiukajtis #define	zero	xxx[10]
12325c28e83SPiotr Jasiukajtis 
12425c28e83SPiotr Jasiukajtis double
log1p(double x)12525c28e83SPiotr Jasiukajtis log1p(double x) {
12625c28e83SPiotr Jasiukajtis 	double	hfsq, f, c = 0.0, s, z, R, u;
12725c28e83SPiotr Jasiukajtis 	int	k, hx, hu, ax;
12825c28e83SPiotr Jasiukajtis 
12925c28e83SPiotr Jasiukajtis 	hx = ((int *)&x)[HIWORD];		/* high word of x */
13025c28e83SPiotr Jasiukajtis 	ax = hx & 0x7fffffff;
13125c28e83SPiotr Jasiukajtis 
13225c28e83SPiotr Jasiukajtis 	if (ax >= 0x7ff00000) { /* x is inf or nan */
13325c28e83SPiotr Jasiukajtis 		if (((hx - 0xfff00000) | ((int *)&x)[LOWORD]) == 0) /* -inf */
13425c28e83SPiotr Jasiukajtis 			return (_SVID_libm_err(x, x, 44));
13525c28e83SPiotr Jasiukajtis 		return (x * x);
13625c28e83SPiotr Jasiukajtis 	}
13725c28e83SPiotr Jasiukajtis 
13825c28e83SPiotr Jasiukajtis 	k = 1;
13925c28e83SPiotr Jasiukajtis 	if (hx < 0x3FDA827A) {	/* x < 0.41422  */
14025c28e83SPiotr Jasiukajtis 		if (ax >= 0x3ff00000)	/* x <= -1.0 */
14125c28e83SPiotr Jasiukajtis 			return (_SVID_libm_err(x, x, x == -1.0 ? 43 : 44));
14225c28e83SPiotr Jasiukajtis 		if (ax < 0x3e200000) {	/* |x| < 2**-29 */
14325c28e83SPiotr Jasiukajtis 			if (two54 + x > zero &&	/* raise inexact */
14425c28e83SPiotr Jasiukajtis 			    ax < 0x3c900000)	/* |x| < 2**-54 */
14525c28e83SPiotr Jasiukajtis 				return (x);
14625c28e83SPiotr Jasiukajtis 			else
14725c28e83SPiotr Jasiukajtis 				return (x - x * x * 0.5);
14825c28e83SPiotr Jasiukajtis 		}
14925c28e83SPiotr Jasiukajtis 		if (hx > 0 || hx <= (int)0xbfd2bec3) {	/* -0.2929<x<0.41422 */
15025c28e83SPiotr Jasiukajtis 			k = 0;
15125c28e83SPiotr Jasiukajtis 			f = x;
15225c28e83SPiotr Jasiukajtis 			hu = 1;
15325c28e83SPiotr Jasiukajtis 		}
15425c28e83SPiotr Jasiukajtis 	}
15525c28e83SPiotr Jasiukajtis 	/* We will initialize 'c' here. */
15625c28e83SPiotr Jasiukajtis 	if (k != 0) {
15725c28e83SPiotr Jasiukajtis 		if (hx < 0x43400000) {
15825c28e83SPiotr Jasiukajtis 			u = 1.0 + x;
15925c28e83SPiotr Jasiukajtis 			hu = ((int *)&u)[HIWORD];	/* high word of u */
16025c28e83SPiotr Jasiukajtis 			k = (hu >> 20) - 1023;
16125c28e83SPiotr Jasiukajtis 			/*
16225c28e83SPiotr Jasiukajtis 			 * correction term
16325c28e83SPiotr Jasiukajtis 			 */
16425c28e83SPiotr Jasiukajtis 			c = k > 0 ? 1.0 - (u - x) : x - (u - 1.0);
16525c28e83SPiotr Jasiukajtis 			c /= u;
16625c28e83SPiotr Jasiukajtis 		} else {
16725c28e83SPiotr Jasiukajtis 			u = x;
16825c28e83SPiotr Jasiukajtis 			hu = ((int *)&u)[HIWORD];	/* high word of u */
16925c28e83SPiotr Jasiukajtis 			k = (hu >> 20) - 1023;
17025c28e83SPiotr Jasiukajtis 			c = 0;
17125c28e83SPiotr Jasiukajtis 		}
17225c28e83SPiotr Jasiukajtis 		hu &= 0x000fffff;
17325c28e83SPiotr Jasiukajtis 		if (hu < 0x6a09e) {	/* normalize u */
17425c28e83SPiotr Jasiukajtis 			((int *)&u)[HIWORD] = hu | 0x3ff00000;
17525c28e83SPiotr Jasiukajtis 		} else {			/* normalize u/2 */
17625c28e83SPiotr Jasiukajtis 			k += 1;
17725c28e83SPiotr Jasiukajtis 			((int *)&u)[HIWORD] = hu | 0x3fe00000;
17825c28e83SPiotr Jasiukajtis 			hu = (0x00100000 - hu) >> 2;
17925c28e83SPiotr Jasiukajtis 		}
18025c28e83SPiotr Jasiukajtis 		f = u - 1.0;
18125c28e83SPiotr Jasiukajtis 	}
18225c28e83SPiotr Jasiukajtis 	hfsq = 0.5 * f * f;
18325c28e83SPiotr Jasiukajtis 	if (hu == 0) {		/* |f| < 2**-20 */
18425c28e83SPiotr Jasiukajtis 		if (f == zero) {
18525c28e83SPiotr Jasiukajtis 			if (k == 0)
18625c28e83SPiotr Jasiukajtis 				return (zero);
18725c28e83SPiotr Jasiukajtis 			/* We already initialized 'c' before, when (k != 0) */
18825c28e83SPiotr Jasiukajtis 			c += k * ln2_lo;
18925c28e83SPiotr Jasiukajtis 			return (k * ln2_hi + c);
19025c28e83SPiotr Jasiukajtis 		}
19125c28e83SPiotr Jasiukajtis 		R = hfsq * (1.0 - 0.66666666666666666 * f);
19225c28e83SPiotr Jasiukajtis 		if (k == 0)
19325c28e83SPiotr Jasiukajtis 			return (f - R);
19425c28e83SPiotr Jasiukajtis 		return (k * ln2_hi - ((R - (k * ln2_lo + c)) - f));
19525c28e83SPiotr Jasiukajtis 	}
19625c28e83SPiotr Jasiukajtis 	s = f / (2.0 + f);
19725c28e83SPiotr Jasiukajtis 	z = s * s;
19825c28e83SPiotr Jasiukajtis 	R = z * (Lp1 + z * (Lp2 + z * (Lp3 + z * (Lp4 + z * (Lp5 +
19925c28e83SPiotr Jasiukajtis 		z * (Lp6 + z * Lp7))))));
20025c28e83SPiotr Jasiukajtis 	if (k == 0)
20125c28e83SPiotr Jasiukajtis 		return (f - (hfsq - s * (hfsq + R)));
20225c28e83SPiotr Jasiukajtis 	return (k * ln2_hi - ((hfsq - (s * (hfsq + R) +
20325c28e83SPiotr Jasiukajtis 		(k * ln2_lo + c))) - f));
20425c28e83SPiotr Jasiukajtis }
205