xref: /csrg-svn/lib/libm/common_source/pow.c (revision 42657)
134126Sbostic /*
224605Szliu  * Copyright (c) 1985 Regents of the University of California.
334126Sbostic  * All rights reserved.
434126Sbostic  *
5*42657Sbostic  * %sccs.include.redist.c%
634126Sbostic  *
734126Sbostic  * All recipients should regard themselves as participants in an ongoing
834126Sbostic  * research project and hence should feel obligated to report their
934126Sbostic  * experiences (good or bad) with these elementary function codes, using
1034126Sbostic  * the sendbug(8) program, to the authors.
1124605Szliu  */
1224605Szliu 
1324605Szliu #ifndef lint
14*42657Sbostic static char sccsid[] = "@(#)pow.c	5.6 (Berkeley) 06/01/90";
1534126Sbostic #endif /* not lint */
1624605Szliu 
1724605Szliu /* POW(X,Y)
1824605Szliu  * RETURN X**Y
1924605Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2024605Szliu  * CODED IN C BY K.C. NG, 1/8/85;
2124605Szliu  * REVISED BY K.C. NG on 7/10/85.
2224605Szliu  *
2324605Szliu  * Required system supported functions:
2424605Szliu  *      scalb(x,n)
2524605Szliu  *      logb(x)
2624605Szliu  *	copysign(x,y)
2724605Szliu  *	finite(x)
2824605Szliu  *	drem(x,y)
2924605Szliu  *
3024605Szliu  * Required kernel functions:
3124605Szliu  *	exp__E(a,c)	...return  exp(a+c) - 1 - a*a/2
3224605Szliu  *	log__L(x)	...return  (log(1+x) - 2s)/s, s=x/(2+x)
3324605Szliu  *	pow_p(x,y)	...return  +(anything)**(finite non zero)
3424605Szliu  *
3524605Szliu  * Method
3624605Szliu  *	1. Compute and return log(x) in three pieces:
3724605Szliu  *		log(x) = n*ln2 + hi + lo,
3824605Szliu  *	   where n is an integer.
3924605Szliu  *	2. Perform y*log(x) by simulating muti-precision arithmetic and
4024605Szliu  *	   return the answer in three pieces:
4124605Szliu  *		y*log(x) = m*ln2 + hi + lo,
4224605Szliu  *	   where m is an integer.
4324605Szliu  *	3. Return x**y = exp(y*log(x))
4424605Szliu  *		= 2^m * ( exp(hi+lo) ).
4524605Szliu  *
4624605Szliu  * Special cases:
4724605Szliu  *	(anything) ** 0  is 1 ;
4824605Szliu  *	(anything) ** 1  is itself;
4924605Szliu  *	(anything) ** NaN is NaN;
5024605Szliu  *	NaN ** (anything except 0) is NaN;
5124605Szliu  *	+-(anything > 1) ** +INF is +INF;
5224605Szliu  *	+-(anything > 1) ** -INF is +0;
5324605Szliu  *	+-(anything < 1) ** +INF is +0;
5424605Szliu  *	+-(anything < 1) ** -INF is +INF;
5524605Szliu  *	+-1 ** +-INF is NaN and signal INVALID;
5624605Szliu  *	+0 ** +(anything except 0, NaN)  is +0;
5724605Szliu  *	-0 ** +(anything except 0, NaN, odd integer)  is +0;
5824605Szliu  *	+0 ** -(anything except 0, NaN)  is +INF and signal DIV-BY-ZERO;
5924605Szliu  *	-0 ** -(anything except 0, NaN, odd integer)  is +INF with signal;
6024605Szliu  *	-0 ** (odd integer) = -( +0 ** (odd integer) );
6124605Szliu  *	+INF ** +(anything except 0,NaN) is +INF;
6224605Szliu  *	+INF ** -(anything except 0,NaN) is +0;
6324605Szliu  *	-INF ** (odd integer) = -( +INF ** (odd integer) );
6424605Szliu  *	-INF ** (even integer) = ( +INF ** (even integer) );
6524605Szliu  *	-INF ** -(anything except integer,NaN) is NaN with signal;
6624605Szliu  *	-(x=anything) ** (k=integer) is (-1)**k * (x ** k);
6724605Szliu  *	-(anything except 0) ** (non-integer) is NaN with signal;
6824605Szliu  *
6924605Szliu  * Accuracy:
7024605Szliu  *	pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX,
7124605Szliu  *	and a Zilog Z8000,
7224605Szliu  *			pow(integer,integer)
7324605Szliu  *	always returns the correct integer provided it is representable.
7424605Szliu  *	In a test run with 100,000 random arguments with 0 < x, y < 20.0
7524605Szliu  *	on a VAX, the maximum observed error was 1.79 ulps (units in the
7624605Szliu  *	last place).
7724605Szliu  *
7824605Szliu  * Constants :
7924605Szliu  * The hexadecimal values are the intended ones for the following constants.
8024605Szliu  * The decimal values may be used, provided that the compiler will convert
8124605Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
8224605Szliu  * shown.
8324605Szliu  */
8424605Szliu 
8524605Szliu #include <errno.h>
8635679Sbostic #include "mathimpl.h"
8724605Szliu 
8835679Sbostic vc(ln2hi,  6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
8935679Sbostic vc(ln2lo,  1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
9035679Sbostic vc(invln2, 1.4426950408889634148E0   ,aa3b,40b8,17f1,295c,   1, .B8AA3B295C17F1)
9135679Sbostic vc(sqrt2,  1.4142135623730950622E0   ,04f3,40b5,de65,33f9,   1, .B504F333F9DE65)
9224605Szliu 
9335679Sbostic ic(ln2hi,  6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
9435679Sbostic ic(ln2lo,  1.9082149292705877000E-10, -33, 1.A39EF35793C76)
9535679Sbostic ic(invln2, 1.4426950408889633870E0,     0, 1.71547652B82FE)
9635679Sbostic ic(sqrt2,  1.4142135623730951455E0,     0, 1.6A09E667F3BCD)
9735679Sbostic 
9835679Sbostic #ifdef vccast
9935679Sbostic #define	ln2hi	vccast(ln2hi)
10035679Sbostic #define	ln2lo	vccast(ln2lo)
10135679Sbostic #define	invln2	vccast(invln2)
10235679Sbostic #define	sqrt2	vccast(sqrt2)
10335679Sbostic #endif
10435679Sbostic 
10535679Sbostic const static double zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0;
10635679Sbostic 
10735679Sbostic static double pow_p();
10835679Sbostic 
10924605Szliu double pow(x,y)
11024605Szliu double x,y;
11124605Szliu {
11235679Sbostic 	double t;
11324605Szliu 
11424605Szliu 	if     (y==zero)      return(one);
11524605Szliu 	else if(y==one
11631853Szliu #if !defined(vax)&&!defined(tahoe)
11724605Szliu 		||x!=x
11831853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
11924605Szliu 		) return( x );      /* if x is NaN or y=1 */
12031853Szliu #if !defined(vax)&&!defined(tahoe)
12124605Szliu 	else if(y!=y)         return( y );      /* if y is NaN */
12231853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
12324605Szliu 	else if(!finite(y))                     /* if y is INF */
12424605Szliu 	     if((t=copysign(x,one))==one) return(zero/zero);
12524605Szliu 	     else if(t>one) return((y>zero)?y:zero);
12624605Szliu 	     else return((y<zero)?-y:zero);
12724605Szliu 	else if(y==two)       return(x*x);
12824605Szliu 	else if(y==negone)    return(one/x);
12924605Szliu 
13024605Szliu     /* sign(x) = 1 */
13124605Szliu 	else if(copysign(one,x)==one) return(pow_p(x,y));
13224605Szliu 
13324605Szliu     /* sign(x)= -1 */
13424605Szliu 	/* if y is an even integer */
13524605Szliu 	else if ( (t=drem(y,two)) == zero)	return( pow_p(-x,y) );
13624605Szliu 
13724605Szliu 	/* if y is an odd integer */
13824605Szliu 	else if (copysign(t,one) == one) return( -pow_p(-x,y) );
13924605Szliu 
14024605Szliu 	/* Henceforth y is not an integer */
14124605Szliu 	else if(x==zero)	/* x is -0 */
14224605Szliu 	    return((y>zero)?-x:one/(-x));
14324605Szliu 	else {			/* return NaN */
14431853Szliu #if defined(vax)||defined(tahoe)
14524605Szliu 	    return (infnan(EDOM));	/* NaN */
14631853Szliu #else	/* defined(vax)||defined(tahoe) */
14724605Szliu 	    return(zero/zero);
14831853Szliu #endif	/* defined(vax)||defined(tahoe) */
14924605Szliu 	}
15024605Szliu }
15124605Szliu 
15242208Sbostic #ifndef mc68881
15324605Szliu /* pow_p(x,y) return x**y for x with sign=1 and finite y */
15424605Szliu static double pow_p(x,y)
15524605Szliu double x,y;
15624605Szliu {
15724605Szliu         double c,s,t,z,tx,ty;
15831853Szliu #ifdef tahoe
15931826Szliu 	double tahoe_tmp;
16031853Szliu #endif	/* tahoe */
16124605Szliu         float sx,sy;
16224605Szliu 	long k=0;
16324605Szliu         int n,m;
16424605Szliu 
16524605Szliu 	if(x==zero||!finite(x)) {           /* if x is +INF or +0 */
16631853Szliu #if defined(vax)||defined(tahoe)
16724605Szliu 	     return((y>zero)?x:infnan(ERANGE));	/* if y<zero, return +INF */
16831853Szliu #else	/* defined(vax)||defined(tahoe) */
16924605Szliu 	     return((y>zero)?x:one/x);
17031853Szliu #endif	/* defined(vax)||defined(tahoe) */
17124605Szliu 	}
17224605Szliu 	if(x==1.0) return(x);	/* if x=1.0, return 1 since y is finite */
17324605Szliu 
17424605Szliu     /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */
17524605Szliu         z=scalb(x,-(n=logb(x)));
17631853Szliu #if !defined(vax)&&!defined(tahoe)	/* IEEE double; subnormal number */
17724605Szliu         if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);}
17831853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
17924605Szliu         if(z >= sqrt2 ) {n += 1; z *= half;}  z -= one ;
18024605Szliu 
18124605Szliu     /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */
18224605Szliu 	s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s));
18324605Szliu 	t= z-(c-tx); tx += (z-t)-c;
18424605Szliu 
18524605Szliu    /* if y*log(x) is neither too big nor too small */
18624605Szliu 	if((s=logb(y)+logb(n+t)) < 12.0)
18724605Szliu 	    if(s>-60.0) {
18824605Szliu 
18924605Szliu 	/* compute y*log(x) ~ mlog2 + t + c */
19024605Szliu         	s=y*(n+invln2*t);
19124605Szliu                 m=s+copysign(half,s);   /* m := nint(y*log(x)) */
19224605Szliu 		k=y;
19324605Szliu 		if((double)k==y) {	/* if y is an integer */
19424605Szliu 		    k = m-k*n;
19524605Szliu 		    sx=t; tx+=(t-sx); }
19624605Szliu 		else	{		/* if y is not an integer */
19724605Szliu 		    k =m;
19824605Szliu 	 	    tx+=n*ln2lo;
19924605Szliu 		    sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; }
20024605Szliu 	   /* end of checking whether k==y */
20124605Szliu 
20224605Szliu                 sy=y; ty=y-sy;          /* y ~ sy + ty */
20331853Szliu #ifdef tahoe
20431826Szliu 		s = (tahoe_tmp = sx)*sy-k*ln2hi;
20531853Szliu #else	/* tahoe */
20624605Szliu 		s=(double)sx*sy-k*ln2hi;        /* (sy+ty)*(sx+tx)-kln2 */
20731853Szliu #endif	/* tahoe */
20824605Szliu 		z=(tx*ty-k*ln2lo);
20924605Szliu 		tx=tx*sy; ty=sx*ty;
21024605Szliu 		t=ty+z; t+=tx; t+=s;
21124605Szliu 		c= -((((t-s)-tx)-ty)-z);
21224605Szliu 
21324605Szliu 	    /* return exp(y*log(x)) */
21424605Szliu 		t += exp__E(t,c); return(scalb(one+t,m));
21524605Szliu 	     }
21624605Szliu 	/* end of if log(y*log(x)) > -60.0 */
21724605Szliu 
21824605Szliu 	    else
21924605Szliu 		/* exp(+- tiny) = 1 with inexact flag */
22024605Szliu 			{ln2hi+ln2lo; return(one);}
22124605Szliu 	    else if(copysign(one,y)*(n+invln2*t) <zero)
22224605Szliu 		/* exp(-(big#)) underflows to zero */
22324605Szliu 	        	return(scalb(one,-5000));
22424605Szliu 	    else
22524605Szliu 	        /* exp(+(big#)) overflows to INF */
22624605Szliu 	    		return(scalb(one, 5000));
22724605Szliu 
22824605Szliu }
22942208Sbostic #endif /* mc68881 */
230