e_pow.c revision 141296
10SN/A/* @(#)e_pow.c 1.5 04/04/22 SMI */
217247Swetmore/*
30SN/A * ====================================================
40SN/A * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
50SN/A *
60SN/A * Permission to use, copy, modify, and distribute this
72362SN/A * software is freely granted, provided that this notice
80SN/A * is preserved.
92362SN/A * ====================================================
100SN/A */
110SN/A
120SN/A#ifndef lint
130SN/Astatic char rcsid[] = "$FreeBSD: head/lib/msun/src/e_pow.c 141296 2005-02-04 18:26:06Z das $";
140SN/A#endif
150SN/A
160SN/A/* __ieee754_pow(x,y) return x**y
170SN/A *
180SN/A *		      n
190SN/A * Method:  Let x =  2   * (1+f)
200SN/A *	1. Compute and return log2(x) in two pieces:
212362SN/A *		log2(x) = w1 + w2,
222362SN/A *	   where w1 has 53-24 = 29 bit trailing zeros.
232362SN/A *	2. Perform y*log2(x) = n+y' by simulating muti-precision
240SN/A *	   arithmetic, where |y'|<=0.5.
250SN/A *	3. Return x**y = 2**n*exp(y'*log2)
260SN/A *
270SN/A * Special cases:
280SN/A *	1.  (anything) ** 0  is 1
290SN/A *	2.  (anything) ** 1  is itself
3015973Swetmore *	3.  (anything) ** NAN is NAN
310SN/A *	4.  NAN ** (anything except 0) is NAN
320SN/A *	5.  +-(|x| > 1) **  +INF is +INF
330SN/A *	6.  +-(|x| > 1) **  -INF is +0
340SN/A *	7.  +-(|x| < 1) **  +INF is +0
350SN/A *	8.  +-(|x| < 1) **  -INF is +INF
360SN/A *	9.  +-1         ** +-INF is NAN
370SN/A *	10. +0 ** (+anything except 0, NAN)               is +0
380SN/A *	11. -0 ** (+anything except 0, NAN, odd integer)  is +0
390SN/A *	12. +0 ** (-anything except 0, NAN)               is +INF
400SN/A *	13. -0 ** (-anything except 0, NAN, odd integer)  is +INF
410SN/A *	14. -0 ** (odd integer) = -( +0 ** (odd integer) )
420SN/A *	15. +INF ** (+anything except 0,NAN) is +INF
430SN/A *	16. +INF ** (-anything except 0,NAN) is +0
440SN/A *	17. -INF ** (anything)  = -0 ** (-anything)
450SN/A *	18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
460SN/A *	19. (-anything except 0 and inf) ** (non-integer) is NAN
470SN/A *
480SN/A * Accuracy:
490SN/A *	pow(x,y) returns x**y nearly rounded. In particular
500SN/A *			pow(integer,integer)
510SN/A *	always returns the correct integer provided it is
520SN/A *	representable.
530SN/A *
540SN/A * Constants :
550SN/A * The hexadecimal values are the intended ones for the following
560SN/A * constants. The decimal values may be used, provided that the
576241SN/A * compiler will convert from decimal to binary accurately enough
586241SN/A * to produce the hexadecimal values shown.
590SN/A */
606241SN/A
616241SN/A#include "math.h"
626241SN/A#include "math_private.h"
636241SN/A
640SN/Astatic const double
6512745Smartinbp[] = {1.0, 1.5,},
660SN/Adp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */
6711809Sdarcydp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */
686128SN/Azero    =  0.0,
690SN/Aone	=  1.0,
700SN/Atwo	=  2.0,
710SN/Atwo53	=  9007199254740992.0,	/* 0x43400000, 0x00000000 */
720SN/Ahuge	=  1.0e300,
730SN/Atiny    =  1.0e-300,
740SN/A	/* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
750SN/AL1  =  5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */
760SN/AL2  =  4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */
770SN/AL3  =  3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */
780SN/AL4  =  2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */
790SN/AL5  =  2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */
800SN/AL6  =  2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */
810SN/AP1   =  1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
820SN/AP2   = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
830SN/AP3   =  6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
840SN/AP4   = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
850SN/AP5   =  4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */
860SN/Alg2  =  6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
870SN/Alg2_h  =  6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */
880SN/Alg2_l  = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */
890SN/Aovt =  8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */
900SN/Acp    =  9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */
910SN/Acp_h  =  9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */
920SN/Acp_l  = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/
930SN/Aivln2    =  1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */
940SN/Aivln2_h  =  1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/
950SN/Aivln2_l  =  1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/
960SN/A
970SN/Adouble
980SN/A__ieee754_pow(double x, double y)
990SN/A{
1000SN/A	double z,ax,z_h,z_l,p_h,p_l;
1010SN/A	double y1,t1,t2,r,s,t,u,v,w;
1020SN/A	int32_t i,j,k,yisint,n;
1030SN/A	int32_t hx,hy,ix,iy;
1040SN/A	u_int32_t lx,ly;
1050SN/A
1060SN/A	EXTRACT_WORDS(hx,lx,x);
1070SN/A	EXTRACT_WORDS(hy,ly,y);
1080SN/A	ix = hx&0x7fffffff;  iy = hy&0x7fffffff;
1090SN/A
1100SN/A    /* y==zero: x**0 = 1 */
1110SN/A	if((iy|ly)==0) return one;
1120SN/A
1130SN/A    /* +-NaN return x+y */
1140SN/A	if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) ||
1150SN/A	   iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0)))
1160SN/A		return x+y;
1170SN/A
1180SN/A    /* determine if y is an odd int when x < 0
1190SN/A     * yisint = 0	... y is not an integer
12012891Sascarpino     * yisint = 1	... y is an odd int
12112891Sascarpino     * yisint = 2	... y is an even int
12214161Sascarpino     */
12314161Sascarpino	yisint  = 0;
12412891Sascarpino	if(hx<0) {
12512891Sascarpino	    if(iy>=0x43400000) yisint = 2; /* even integer y */
12612891Sascarpino	    else if(iy>=0x3ff00000) {
12712891Sascarpino		k = (iy>>20)-0x3ff;	   /* exponent */
1280SN/A		if(k>20) {
1290SN/A		    j = ly>>(52-k);
13017247Swetmore		    if((j<<(52-k))==ly) yisint = 2-(j&1);
13117247Swetmore		} else if(ly==0) {
1320SN/A		    j = iy>>(20-k);
1330SN/A		    if((j<<(20-k))==iy) yisint = 2-(j&1);
13415973Swetmore		}
1350SN/A	    }
13615973Swetmore	}
13715973Swetmore
13815973Swetmore    /* special value of y */
13915973Swetmore	if(ly==0) {
14015973Swetmore	    if (iy==0x7ff00000) {	/* y is +-inf */
1410SN/A	        if(((ix-0x3ff00000)|lx)==0)
1420SN/A		    return  y - y;	/* inf**+-1 is NaN */
1430SN/A	        else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */
1440SN/A		    return (hy>=0)? y: zero;
1450SN/A	        else			/* (|x|<1)**-,+inf = inf,0 */
14615973Swetmore		    return (hy<0)?-y: zero;
1470SN/A	    }
1480SN/A	    if(iy==0x3ff00000) {	/* y is  +-1 */
1490SN/A		if(hy<0) return one/x; else return x;
1500SN/A	    }
1510SN/A	    if(hy==0x40000000) return x*x; /* y is  2 */
1520SN/A	    if(hy==0x3fe00000) {	/* y is  0.5 */
1530SN/A		if(hx>=0)	/* x >= +0 */
1540SN/A		return sqrt(x);
1550SN/A	    }
1560SN/A	}
1570SN/A
1580SN/A	ax   = fabs(x);
1590SN/A    /* special value of x */
1600SN/A	if(lx==0) {
1610SN/A	    if(ix==0x7ff00000||ix==0||ix==0x3ff00000){
1620SN/A		z = ax;			/*x is +-0,+-inf,+-1*/
1630SN/A		if(hy<0) z = one/z;	/* z = (1/|x|) */
1640SN/A		if(hx<0) {
1650SN/A		    if(((ix-0x3ff00000)|yisint)==0) {
1660SN/A			z = (z-z)/(z-z); /* (-1)**non-int is NaN */
1670SN/A		    } else if(yisint==1)
16817247Swetmore			z = -z;		/* (x<0)**odd = -(|x|**odd) */
16917247Swetmore		}
1700SN/A		return z;
1710SN/A	    }
1720SN/A	}
1730SN/A
17415973Swetmore    /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be
1750SN/A	n = (hx>>31)+1;
17615973Swetmore       but ANSI C says a right shift of a signed negative quantity is
17715973Swetmore       implementation defined.  */
17815973Swetmore	n = ((u_int32_t)hx>>31)-1;
17915973Swetmore
18015973Swetmore    /* (x<0)**(non-int) is NaN */
18115973Swetmore	if((n|yisint)==0) return (x-x)/(x-x);
1820SN/A
1830SN/A	s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
18415973Swetmore	if((n|(yisint-1))==0) s = -one;/* (-ve)**(odd int) */
1850SN/A
18615973Swetmore    /* |y| is huge */
1870SN/A	if(iy>0x41e00000) { /* if |y| > 2**31 */
1880SN/A	    if(iy>0x43f00000){	/* if |y| > 2**64, must o/uflow */
1890SN/A		if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny;
1900SN/A		if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny;
1910SN/A	    }
1920SN/A	/* over/underflow if x is not close to one */
19315973Swetmore	    if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny;
1940SN/A	    if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny;
1950SN/A	/* now |1-x| is tiny <= 2**-20, suffice to compute
1960SN/A	   log(x) by x-x^2/2+x^3/3-x^4/4 */
1970SN/A	    t = ax-one;		/* t has 20 trailing zeros */
1980SN/A	    w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25));
1990SN/A	    u = ivln2_h*t;	/* ivln2_h has 21 sig. bits */
2000SN/A	    v = t*ivln2_l-w*ivln2;
2010SN/A	    t1 = u+v;
2020SN/A	    SET_LOW_WORD(t1,0);
2030SN/A	    t2 = v-(t1-u);
2040SN/A	} else {
2050SN/A	    double ss,s2,s_h,s_l,t_h,t_l;
2060SN/A	    n = 0;
2070SN/A	/* take care subnormal number */
2080SN/A	    if(ix<0x00100000)
2090SN/A		{ax *= two53; n -= 53; GET_HIGH_WORD(ix,ax); }
2100SN/A	    n  += ((ix)>>20)-0x3ff;
2110SN/A	    j  = ix&0x000fffff;
21217247Swetmore	/* determine interval */
21317247Swetmore	    ix = j|0x3ff00000;		/* normalize ix */
2140SN/A	    if(j<=0x3988E) k=0;		/* |x|<sqrt(3/2) */
2150SN/A	    else if(j<0xBB67A) k=1;	/* |x|<sqrt(3)   */
2160SN/A	    else {k=0;n+=1;ix -= 0x00100000;}
2170SN/A	    SET_HIGH_WORD(ax,ix);
21815973Swetmore
21915973Swetmore	/* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
22015973Swetmore	    u = ax-bp[k];		/* bp[0]=1.0, bp[1]=1.5 */
2210SN/A	    v = one/(ax+bp[k]);
22215973Swetmore	    ss = u*v;
22315973Swetmore	    s_h = ss;
22415973Swetmore	    SET_LOW_WORD(s_h,0);
2250SN/A	/* t_h=ax+bp[k] High */
22615973Swetmore	    t_h = zero;
2270SN/A	    SET_HIGH_WORD(t_h,((ix>>1)|0x20000000)+0x00080000+(k<<18));
2280SN/A	    t_l = ax - (t_h-bp[k]);
2290SN/A	    s_l = v*((u-s_h*t_h)-s_h*t_l);
2300SN/A	/* compute log(ax) */
2310SN/A	    s2 = ss*ss;
23215973Swetmore	    r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
2330SN/A	    r += s_l*(s_h+ss);
2340SN/A	    s2  = s_h*s_h;
2350SN/A	    t_h = 3.0+s2+r;
2360SN/A	    SET_LOW_WORD(t_h,0);
2370SN/A	    t_l = r-((t_h-3.0)-s2);
2380SN/A	/* u+v = ss*(1+...) */
2390SN/A	    u = s_h*t_h;
2400SN/A	    v = s_l*t_h+t_l*ss;
2410SN/A	/* 2/(3log2)*(ss+...) */
2420SN/A	    p_h = u+v;
2430SN/A	    SET_LOW_WORD(p_h,0);
2440SN/A	    p_l = v-(p_h-u);
2450SN/A	    z_h = cp_h*p_h;		/* cp_h+cp_l = 2/(3*log2) */
2460SN/A	    z_l = cp_l*p_h+p_l*cp+dp_l[k];
2470SN/A	/* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
2480SN/A	    t = (double)n;
2490SN/A	    t1 = (((z_h+z_l)+dp_h[k])+t);
2500SN/A	    SET_LOW_WORD(t1,0);
2510SN/A	    t2 = z_l-(((t1-t)-dp_h[k])-z_h);
2520SN/A	}
2530SN/A
2540SN/A    /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
2550SN/A	y1  = y;
2560SN/A	SET_LOW_WORD(y1,0);
2570SN/A	p_l = (y-y1)*t1+y*t2;
2580SN/A	p_h = y1*t1;
2590SN/A	z = p_l+p_h;
2600SN/A	EXTRACT_WORDS(j,i,z);
2610SN/A	if (j>=0x40900000) {				/* z >= 1024 */
2620SN/A	    if(((j-0x40900000)|i)!=0)			/* if z > 1024 */
2630SN/A		return s*huge*huge;			/* overflow */
2640SN/A	    else {
2650SN/A		if(p_l+ovt>z-p_h) return s*huge*huge;	/* overflow */
2660SN/A	    }
2670SN/A	} else if((j&0x7fffffff)>=0x4090cc00 ) {	/* z <= -1075 */
2680SN/A	    if(((j-0xc090cc00)|i)!=0) 		/* z < -1075 */
2690SN/A		return s*tiny*tiny;		/* underflow */
2700SN/A	    else {
2710SN/A		if(p_l<=z-p_h) return s*tiny*tiny;	/* underflow */
2720SN/A	    }
2730SN/A	}
2740SN/A    /*
2750SN/A     * compute 2**(p_h+p_l)
2760SN/A     */
2770SN/A	i = j&0x7fffffff;
2780SN/A	k = (i>>20)-0x3ff;
2790SN/A	n = 0;
2800SN/A	if(i>0x3fe00000) {		/* if |z| > 0.5, set n = [z+0.5] */
2810SN/A	    n = j+(0x00100000>>(k+1));
2820SN/A	    k = ((n&0x7fffffff)>>20)-0x3ff;	/* new k for n */
2830SN/A	    t = zero;
2840SN/A	    SET_HIGH_WORD(t,n&~(0x000fffff>>k));
2850SN/A	    n = ((n&0x000fffff)|0x00100000)>>(20-k);
2860SN/A	    if(j<0) n = -n;
2870SN/A	    p_h -= t;
2880SN/A	}
2890SN/A	t = p_l+p_h;
2900SN/A	SET_LOW_WORD(t,0);
2910SN/A	u = t*lg2_h;
2920SN/A	v = (p_l-(t-p_h))*lg2+t*lg2_l;
2930SN/A	z = u+v;
2940SN/A	w = v-(z-u);
2950SN/A	t  = z*z;
2960SN/A	t1  = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
2970SN/A	r  = (z*t1)/(t1-two)-(w+z*w);
2980SN/A	z  = one-(r-z);
2990SN/A	GET_HIGH_WORD(j,z);
3000SN/A	j += (n<<20);
3010SN/A	if((j>>20)<=0) z = scalbn(z,n);	/* subnormal output */
3020SN/A	else SET_HIGH_WORD(z,j);
3030SN/A	return s*z;
3040SN/A}
3050SN/A