xref: /csrg-svn/lib/libm/common_source/pow.c (revision 31826)
124605Szliu /*
224605Szliu  * Copyright (c) 1985 Regents of the University of California.
324605Szliu  *
424605Szliu  * Use and reproduction of this software are granted  in  accordance  with
524605Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
624605Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
724605Szliu  * source, and inclusion of this notice) with the additional understanding
824605Szliu  * that  all  recipients  should regard themselves as participants  in  an
924605Szliu  * ongoing  research  project and hence should  feel  obligated  to report
1024605Szliu  * their  experiences (good or bad) with these elementary function  codes,
1124605Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
1224605Szliu  */
1324605Szliu 
1424605Szliu #ifndef lint
1524706Selefunt static char sccsid[] =
16*31826Szliu "@(#)pow.c	4.5 (Berkeley) 8/21/85; 1.6 (ucb.elefunt) 07/10/87";
1724605Szliu #endif not lint
1824605Szliu 
1924605Szliu /* POW(X,Y)
2024605Szliu  * RETURN X**Y
2124605Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2224605Szliu  * CODED IN C BY K.C. NG, 1/8/85;
2324605Szliu  * REVISED BY K.C. NG on 7/10/85.
2424605Szliu  *
2524605Szliu  * Required system supported functions:
2624605Szliu  *      scalb(x,n)
2724605Szliu  *      logb(x)
2824605Szliu  *	copysign(x,y)
2924605Szliu  *	finite(x)
3024605Szliu  *	drem(x,y)
3124605Szliu  *
3224605Szliu  * Required kernel functions:
3324605Szliu  *	exp__E(a,c)	...return  exp(a+c) - 1 - a*a/2
3424605Szliu  *	log__L(x)	...return  (log(1+x) - 2s)/s, s=x/(2+x)
3524605Szliu  *	pow_p(x,y)	...return  +(anything)**(finite non zero)
3624605Szliu  *
3724605Szliu  * Method
3824605Szliu  *	1. Compute and return log(x) in three pieces:
3924605Szliu  *		log(x) = n*ln2 + hi + lo,
4024605Szliu  *	   where n is an integer.
4124605Szliu  *	2. Perform y*log(x) by simulating muti-precision arithmetic and
4224605Szliu  *	   return the answer in three pieces:
4324605Szliu  *		y*log(x) = m*ln2 + hi + lo,
4424605Szliu  *	   where m is an integer.
4524605Szliu  *	3. Return x**y = exp(y*log(x))
4624605Szliu  *		= 2^m * ( exp(hi+lo) ).
4724605Szliu  *
4824605Szliu  * Special cases:
4924605Szliu  *	(anything) ** 0  is 1 ;
5024605Szliu  *	(anything) ** 1  is itself;
5124605Szliu  *	(anything) ** NaN is NaN;
5224605Szliu  *	NaN ** (anything except 0) is NaN;
5324605Szliu  *	+-(anything > 1) ** +INF is +INF;
5424605Szliu  *	+-(anything > 1) ** -INF is +0;
5524605Szliu  *	+-(anything < 1) ** +INF is +0;
5624605Szliu  *	+-(anything < 1) ** -INF is +INF;
5724605Szliu  *	+-1 ** +-INF is NaN and signal INVALID;
5824605Szliu  *	+0 ** +(anything except 0, NaN)  is +0;
5924605Szliu  *	-0 ** +(anything except 0, NaN, odd integer)  is +0;
6024605Szliu  *	+0 ** -(anything except 0, NaN)  is +INF and signal DIV-BY-ZERO;
6124605Szliu  *	-0 ** -(anything except 0, NaN, odd integer)  is +INF with signal;
6224605Szliu  *	-0 ** (odd integer) = -( +0 ** (odd integer) );
6324605Szliu  *	+INF ** +(anything except 0,NaN) is +INF;
6424605Szliu  *	+INF ** -(anything except 0,NaN) is +0;
6524605Szliu  *	-INF ** (odd integer) = -( +INF ** (odd integer) );
6624605Szliu  *	-INF ** (even integer) = ( +INF ** (even integer) );
6724605Szliu  *	-INF ** -(anything except integer,NaN) is NaN with signal;
6824605Szliu  *	-(x=anything) ** (k=integer) is (-1)**k * (x ** k);
6924605Szliu  *	-(anything except 0) ** (non-integer) is NaN with signal;
7024605Szliu  *
7124605Szliu  * Accuracy:
7224605Szliu  *	pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX,
7324605Szliu  *	and a Zilog Z8000,
7424605Szliu  *			pow(integer,integer)
7524605Szliu  *	always returns the correct integer provided it is representable.
7624605Szliu  *	In a test run with 100,000 random arguments with 0 < x, y < 20.0
7724605Szliu  *	on a VAX, the maximum observed error was 1.79 ulps (units in the
7824605Szliu  *	last place).
7924605Szliu  *
8024605Szliu  * Constants :
8124605Szliu  * The hexadecimal values are the intended ones for the following constants.
8224605Szliu  * The decimal values may be used, provided that the compiler will convert
8324605Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
8424605Szliu  * shown.
8524605Szliu  */
8624605Szliu 
8731790Szliu #if (defined(VAX)||defined(TAHOE))	/* VAX D format */
8824605Szliu #include <errno.h>
8924605Szliu extern double infnan();
9031812Szliu #ifdef VAX
9131812Szliu #define _0x(A,B)	0x/**/A/**/B
9231812Szliu #else	/* VAX */
9331812Szliu #define _0x(A,B)	0x/**/B/**/A
9431812Szliu #endif	/* VAX */
9526893Selefunt /* static double */
9624605Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
9724605Szliu /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
9824605Szliu /* invln2 =  1.4426950408889634148E0     , Hex  2^  1   *  .B8AA3B295C17F1 */
9924605Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
10031812Szliu static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
10131812Szliu static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
10231812Szliu static long    invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)};
10331812Szliu static long     sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
10424605Szliu #define    ln2hi    (*(double*)ln2hix)
10524605Szliu #define    ln2lo    (*(double*)ln2lox)
10624605Szliu #define   invln2    (*(double*)invln2x)
10724605Szliu #define    sqrt2    (*(double*)sqrt2x)
10824605Szliu #else	/* IEEE double */
10926893Selefunt static double
11024605Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
11124605Szliu ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
11224605Szliu invln2 =  1.4426950408889633870E0     , /*Hex  2^  0   *  1.71547652B82FE */
11324605Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
11424605Szliu #endif
11524605Szliu 
11626893Selefunt static double zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0;
11724605Szliu 
11824605Szliu double pow(x,y)
11924605Szliu double x,y;
12024605Szliu {
12124605Szliu 	double drem(),pow_p(),copysign(),t;
12224605Szliu 	int finite();
12324605Szliu 
12424605Szliu 	if     (y==zero)      return(one);
12524605Szliu 	else if(y==one
12631790Szliu #if (!defined(VAX)&&!defined(TAHOE))
12724605Szliu 		||x!=x
12824605Szliu #endif
12924605Szliu 		) return( x );      /* if x is NaN or y=1 */
13031790Szliu #if (!defined(VAX)&&!defined(TAHOE))
13124605Szliu 	else if(y!=y)         return( y );      /* if y is NaN */
13224605Szliu #endif
13324605Szliu 	else if(!finite(y))                     /* if y is INF */
13424605Szliu 	     if((t=copysign(x,one))==one) return(zero/zero);
13524605Szliu 	     else if(t>one) return((y>zero)?y:zero);
13624605Szliu 	     else return((y<zero)?-y:zero);
13724605Szliu 	else if(y==two)       return(x*x);
13824605Szliu 	else if(y==negone)    return(one/x);
13924605Szliu 
14024605Szliu     /* sign(x) = 1 */
14124605Szliu 	else if(copysign(one,x)==one) return(pow_p(x,y));
14224605Szliu 
14324605Szliu     /* sign(x)= -1 */
14424605Szliu 	/* if y is an even integer */
14524605Szliu 	else if ( (t=drem(y,two)) == zero)	return( pow_p(-x,y) );
14624605Szliu 
14724605Szliu 	/* if y is an odd integer */
14824605Szliu 	else if (copysign(t,one) == one) return( -pow_p(-x,y) );
14924605Szliu 
15024605Szliu 	/* Henceforth y is not an integer */
15124605Szliu 	else if(x==zero)	/* x is -0 */
15224605Szliu 	    return((y>zero)?-x:one/(-x));
15324605Szliu 	else {			/* return NaN */
15431790Szliu #if (defined(VAX)||defined(TAHOE))
15524605Szliu 	    return (infnan(EDOM));	/* NaN */
15624605Szliu #else	/* IEEE double */
15724605Szliu 	    return(zero/zero);
15824605Szliu #endif
15924605Szliu 	}
16024605Szliu }
16124605Szliu 
16224605Szliu /* pow_p(x,y) return x**y for x with sign=1 and finite y */
16324605Szliu static double pow_p(x,y)
16424605Szliu double x,y;
16524605Szliu {
16624605Szliu         double logb(),scalb(),copysign(),log__L(),exp__E();
16724605Szliu         double c,s,t,z,tx,ty;
168*31826Szliu #ifdef TAHOE
169*31826Szliu 	double tahoe_tmp;
170*31826Szliu #endif
17124605Szliu         float sx,sy;
17224605Szliu 	long k=0;
17324605Szliu         int n,m;
17424605Szliu 
17524605Szliu 	if(x==zero||!finite(x)) {           /* if x is +INF or +0 */
17631790Szliu #if (defined(VAX)||defined(TAHOE))
17724605Szliu 	     return((y>zero)?x:infnan(ERANGE));	/* if y<zero, return +INF */
17824605Szliu #else
17924605Szliu 	     return((y>zero)?x:one/x);
18024605Szliu #endif
18124605Szliu 	}
18224605Szliu 	if(x==1.0) return(x);	/* if x=1.0, return 1 since y is finite */
18324605Szliu 
18424605Szliu     /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */
18524605Szliu         z=scalb(x,-(n=logb(x)));
18631790Szliu #if (!defined(VAX)&&!defined(TAHOE))	/* IEEE double; subnormal number */
18724605Szliu         if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);}
18824605Szliu #endif
18924605Szliu         if(z >= sqrt2 ) {n += 1; z *= half;}  z -= one ;
19024605Szliu 
19124605Szliu     /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */
19224605Szliu 	s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s));
19324605Szliu 	t= z-(c-tx); tx += (z-t)-c;
19424605Szliu 
19524605Szliu    /* if y*log(x) is neither too big nor too small */
19624605Szliu 	if((s=logb(y)+logb(n+t)) < 12.0)
19724605Szliu 	    if(s>-60.0) {
19824605Szliu 
19924605Szliu 	/* compute y*log(x) ~ mlog2 + t + c */
20024605Szliu         	s=y*(n+invln2*t);
20124605Szliu                 m=s+copysign(half,s);   /* m := nint(y*log(x)) */
20224605Szliu 		k=y;
20324605Szliu 		if((double)k==y) {	/* if y is an integer */
20424605Szliu 		    k = m-k*n;
20524605Szliu 		    sx=t; tx+=(t-sx); }
20624605Szliu 		else	{		/* if y is not an integer */
20724605Szliu 		    k =m;
20824605Szliu 	 	    tx+=n*ln2lo;
20924605Szliu 		    sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; }
21024605Szliu 	   /* end of checking whether k==y */
21124605Szliu 
21224605Szliu                 sy=y; ty=y-sy;          /* y ~ sy + ty */
213*31826Szliu #ifdef TAHOE
214*31826Szliu 		s = (tahoe_tmp = sx)*sy-k*ln2hi;
215*31826Szliu #else
21624605Szliu 		s=(double)sx*sy-k*ln2hi;        /* (sy+ty)*(sx+tx)-kln2 */
217*31826Szliu #endif
21824605Szliu 		z=(tx*ty-k*ln2lo);
21924605Szliu 		tx=tx*sy; ty=sx*ty;
22024605Szliu 		t=ty+z; t+=tx; t+=s;
22124605Szliu 		c= -((((t-s)-tx)-ty)-z);
22224605Szliu 
22324605Szliu 	    /* return exp(y*log(x)) */
22424605Szliu 		t += exp__E(t,c); return(scalb(one+t,m));
22524605Szliu 	     }
22624605Szliu 	/* end of if log(y*log(x)) > -60.0 */
22724605Szliu 
22824605Szliu 	    else
22924605Szliu 		/* exp(+- tiny) = 1 with inexact flag */
23024605Szliu 			{ln2hi+ln2lo; return(one);}
23124605Szliu 	    else if(copysign(one,y)*(n+invln2*t) <zero)
23224605Szliu 		/* exp(-(big#)) underflows to zero */
23324605Szliu 	        	return(scalb(one,-5000));
23424605Szliu 	    else
23524605Szliu 	        /* exp(+(big#)) overflows to INF */
23624605Szliu 	    		return(scalb(one, 5000));
23724605Szliu 
23824605Szliu }
239