1*34126Sbostic /* 224605Szliu * Copyright (c) 1985 Regents of the University of California. 3*34126Sbostic * All rights reserved. 4*34126Sbostic * 5*34126Sbostic * Redistribution and use in source and binary forms are permitted 6*34126Sbostic * provided that this notice is preserved and that due credit is given 7*34126Sbostic * to the University of California at Berkeley. The name of the University 8*34126Sbostic * may not be used to endorse or promote products derived from this 9*34126Sbostic * software without specific prior written permission. This software 10*34126Sbostic * is provided ``as is'' without express or implied warranty. 11*34126Sbostic * 12*34126Sbostic * All recipients should regard themselves as participants in an ongoing 13*34126Sbostic * research project and hence should feel obligated to report their 14*34126Sbostic * experiences (good or bad) with these elementary function codes, using 15*34126Sbostic * the sendbug(8) program, to the authors. 1624605Szliu */ 1724605Szliu 1824605Szliu #ifndef lint 19*34126Sbostic static char sccsid[] = "@(#)pow.c 5.2 (Berkeley) 04/29/88"; 20*34126Sbostic #endif /* not lint */ 2124605Szliu 2224605Szliu /* POW(X,Y) 2324605Szliu * RETURN X**Y 2424605Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 2524605Szliu * CODED IN C BY K.C. NG, 1/8/85; 2624605Szliu * REVISED BY K.C. NG on 7/10/85. 2724605Szliu * 2824605Szliu * Required system supported functions: 2924605Szliu * scalb(x,n) 3024605Szliu * logb(x) 3124605Szliu * copysign(x,y) 3224605Szliu * finite(x) 3324605Szliu * drem(x,y) 3424605Szliu * 3524605Szliu * Required kernel functions: 3624605Szliu * exp__E(a,c) ...return exp(a+c) - 1 - a*a/2 3724605Szliu * log__L(x) ...return (log(1+x) - 2s)/s, s=x/(2+x) 3824605Szliu * pow_p(x,y) ...return +(anything)**(finite non zero) 3924605Szliu * 4024605Szliu * Method 4124605Szliu * 1. Compute and return log(x) in three pieces: 4224605Szliu * log(x) = n*ln2 + hi + lo, 4324605Szliu * where n is an integer. 4424605Szliu * 2. Perform y*log(x) by simulating muti-precision arithmetic and 4524605Szliu * return the answer in three pieces: 4624605Szliu * y*log(x) = m*ln2 + hi + lo, 4724605Szliu * where m is an integer. 4824605Szliu * 3. Return x**y = exp(y*log(x)) 4924605Szliu * = 2^m * ( exp(hi+lo) ). 5024605Szliu * 5124605Szliu * Special cases: 5224605Szliu * (anything) ** 0 is 1 ; 5324605Szliu * (anything) ** 1 is itself; 5424605Szliu * (anything) ** NaN is NaN; 5524605Szliu * NaN ** (anything except 0) is NaN; 5624605Szliu * +-(anything > 1) ** +INF is +INF; 5724605Szliu * +-(anything > 1) ** -INF is +0; 5824605Szliu * +-(anything < 1) ** +INF is +0; 5924605Szliu * +-(anything < 1) ** -INF is +INF; 6024605Szliu * +-1 ** +-INF is NaN and signal INVALID; 6124605Szliu * +0 ** +(anything except 0, NaN) is +0; 6224605Szliu * -0 ** +(anything except 0, NaN, odd integer) is +0; 6324605Szliu * +0 ** -(anything except 0, NaN) is +INF and signal DIV-BY-ZERO; 6424605Szliu * -0 ** -(anything except 0, NaN, odd integer) is +INF with signal; 6524605Szliu * -0 ** (odd integer) = -( +0 ** (odd integer) ); 6624605Szliu * +INF ** +(anything except 0,NaN) is +INF; 6724605Szliu * +INF ** -(anything except 0,NaN) is +0; 6824605Szliu * -INF ** (odd integer) = -( +INF ** (odd integer) ); 6924605Szliu * -INF ** (even integer) = ( +INF ** (even integer) ); 7024605Szliu * -INF ** -(anything except integer,NaN) is NaN with signal; 7124605Szliu * -(x=anything) ** (k=integer) is (-1)**k * (x ** k); 7224605Szliu * -(anything except 0) ** (non-integer) is NaN with signal; 7324605Szliu * 7424605Szliu * Accuracy: 7524605Szliu * pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX, 7624605Szliu * and a Zilog Z8000, 7724605Szliu * pow(integer,integer) 7824605Szliu * always returns the correct integer provided it is representable. 7924605Szliu * In a test run with 100,000 random arguments with 0 < x, y < 20.0 8024605Szliu * on a VAX, the maximum observed error was 1.79 ulps (units in the 8124605Szliu * last place). 8224605Szliu * 8324605Szliu * Constants : 8424605Szliu * The hexadecimal values are the intended ones for the following constants. 8524605Szliu * The decimal values may be used, provided that the compiler will convert 8624605Szliu * from decimal to binary accurately enough to produce the hexadecimal values 8724605Szliu * shown. 8824605Szliu */ 8924605Szliu 9031853Szliu #if defined(vax)||defined(tahoe) /* VAX D format */ 9124605Szliu #include <errno.h> 9224605Szliu extern double infnan(); 9331853Szliu #ifdef vax 9431812Szliu #define _0x(A,B) 0x/**/A/**/B 9531853Szliu #else /* vax */ 9631812Szliu #define _0x(A,B) 0x/**/B/**/A 9731853Szliu #endif /* vax */ 9826893Selefunt /* static double */ 9924605Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 10024605Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 10124605Szliu /* invln2 = 1.4426950408889634148E0 , Hex 2^ 1 * .B8AA3B295C17F1 */ 10224605Szliu /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 10331812Szliu static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)}; 10431812Szliu static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)}; 10531812Szliu static long invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)}; 10631812Szliu static long sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)}; 10724605Szliu #define ln2hi (*(double*)ln2hix) 10824605Szliu #define ln2lo (*(double*)ln2lox) 10924605Szliu #define invln2 (*(double*)invln2x) 11024605Szliu #define sqrt2 (*(double*)sqrt2x) 11131853Szliu #else /* defined(vax)||defined(tahoe) */ 11226893Selefunt static double 11324605Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 11424605Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 11524605Szliu invln2 = 1.4426950408889633870E0 , /*Hex 2^ 0 * 1.71547652B82FE */ 11624605Szliu sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 11731853Szliu #endif /* defined(vax)||defined(tahoe) */ 11824605Szliu 11926893Selefunt static double zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0; 12024605Szliu 12124605Szliu double pow(x,y) 12224605Szliu double x,y; 12324605Szliu { 12424605Szliu double drem(),pow_p(),copysign(),t; 12524605Szliu int finite(); 12624605Szliu 12724605Szliu if (y==zero) return(one); 12824605Szliu else if(y==one 12931853Szliu #if !defined(vax)&&!defined(tahoe) 13024605Szliu ||x!=x 13131853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 13224605Szliu ) return( x ); /* if x is NaN or y=1 */ 13331853Szliu #if !defined(vax)&&!defined(tahoe) 13424605Szliu else if(y!=y) return( y ); /* if y is NaN */ 13531853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 13624605Szliu else if(!finite(y)) /* if y is INF */ 13724605Szliu if((t=copysign(x,one))==one) return(zero/zero); 13824605Szliu else if(t>one) return((y>zero)?y:zero); 13924605Szliu else return((y<zero)?-y:zero); 14024605Szliu else if(y==two) return(x*x); 14124605Szliu else if(y==negone) return(one/x); 14224605Szliu 14324605Szliu /* sign(x) = 1 */ 14424605Szliu else if(copysign(one,x)==one) return(pow_p(x,y)); 14524605Szliu 14624605Szliu /* sign(x)= -1 */ 14724605Szliu /* if y is an even integer */ 14824605Szliu else if ( (t=drem(y,two)) == zero) return( pow_p(-x,y) ); 14924605Szliu 15024605Szliu /* if y is an odd integer */ 15124605Szliu else if (copysign(t,one) == one) return( -pow_p(-x,y) ); 15224605Szliu 15324605Szliu /* Henceforth y is not an integer */ 15424605Szliu else if(x==zero) /* x is -0 */ 15524605Szliu return((y>zero)?-x:one/(-x)); 15624605Szliu else { /* return NaN */ 15731853Szliu #if defined(vax)||defined(tahoe) 15824605Szliu return (infnan(EDOM)); /* NaN */ 15931853Szliu #else /* defined(vax)||defined(tahoe) */ 16024605Szliu return(zero/zero); 16131853Szliu #endif /* defined(vax)||defined(tahoe) */ 16224605Szliu } 16324605Szliu } 16424605Szliu 16524605Szliu /* pow_p(x,y) return x**y for x with sign=1 and finite y */ 16624605Szliu static double pow_p(x,y) 16724605Szliu double x,y; 16824605Szliu { 16924605Szliu double logb(),scalb(),copysign(),log__L(),exp__E(); 17024605Szliu double c,s,t,z,tx,ty; 17131853Szliu #ifdef tahoe 17231826Szliu double tahoe_tmp; 17331853Szliu #endif /* tahoe */ 17424605Szliu float sx,sy; 17524605Szliu long k=0; 17624605Szliu int n,m; 17724605Szliu 17824605Szliu if(x==zero||!finite(x)) { /* if x is +INF or +0 */ 17931853Szliu #if defined(vax)||defined(tahoe) 18024605Szliu return((y>zero)?x:infnan(ERANGE)); /* if y<zero, return +INF */ 18131853Szliu #else /* defined(vax)||defined(tahoe) */ 18224605Szliu return((y>zero)?x:one/x); 18331853Szliu #endif /* defined(vax)||defined(tahoe) */ 18424605Szliu } 18524605Szliu if(x==1.0) return(x); /* if x=1.0, return 1 since y is finite */ 18624605Szliu 18724605Szliu /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */ 18824605Szliu z=scalb(x,-(n=logb(x))); 18931853Szliu #if !defined(vax)&&!defined(tahoe) /* IEEE double; subnormal number */ 19024605Szliu if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);} 19131853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 19224605Szliu if(z >= sqrt2 ) {n += 1; z *= half;} z -= one ; 19324605Szliu 19424605Szliu /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */ 19524605Szliu s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s)); 19624605Szliu t= z-(c-tx); tx += (z-t)-c; 19724605Szliu 19824605Szliu /* if y*log(x) is neither too big nor too small */ 19924605Szliu if((s=logb(y)+logb(n+t)) < 12.0) 20024605Szliu if(s>-60.0) { 20124605Szliu 20224605Szliu /* compute y*log(x) ~ mlog2 + t + c */ 20324605Szliu s=y*(n+invln2*t); 20424605Szliu m=s+copysign(half,s); /* m := nint(y*log(x)) */ 20524605Szliu k=y; 20624605Szliu if((double)k==y) { /* if y is an integer */ 20724605Szliu k = m-k*n; 20824605Szliu sx=t; tx+=(t-sx); } 20924605Szliu else { /* if y is not an integer */ 21024605Szliu k =m; 21124605Szliu tx+=n*ln2lo; 21224605Szliu sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; } 21324605Szliu /* end of checking whether k==y */ 21424605Szliu 21524605Szliu sy=y; ty=y-sy; /* y ~ sy + ty */ 21631853Szliu #ifdef tahoe 21731826Szliu s = (tahoe_tmp = sx)*sy-k*ln2hi; 21831853Szliu #else /* tahoe */ 21924605Szliu s=(double)sx*sy-k*ln2hi; /* (sy+ty)*(sx+tx)-kln2 */ 22031853Szliu #endif /* tahoe */ 22124605Szliu z=(tx*ty-k*ln2lo); 22224605Szliu tx=tx*sy; ty=sx*ty; 22324605Szliu t=ty+z; t+=tx; t+=s; 22424605Szliu c= -((((t-s)-tx)-ty)-z); 22524605Szliu 22624605Szliu /* return exp(y*log(x)) */ 22724605Szliu t += exp__E(t,c); return(scalb(one+t,m)); 22824605Szliu } 22924605Szliu /* end of if log(y*log(x)) > -60.0 */ 23024605Szliu 23124605Szliu else 23224605Szliu /* exp(+- tiny) = 1 with inexact flag */ 23324605Szliu {ln2hi+ln2lo; return(one);} 23424605Szliu else if(copysign(one,y)*(n+invln2*t) <zero) 23524605Szliu /* exp(-(big#)) underflows to zero */ 23624605Szliu return(scalb(one,-5000)); 23724605Szliu else 23824605Szliu /* exp(+(big#)) overflows to INF */ 23924605Szliu return(scalb(one, 5000)); 24024605Szliu 24124605Szliu } 242