134126Sbostic /* 224605Szliu * Copyright (c) 1985 Regents of the University of California. 334126Sbostic * All rights reserved. 434126Sbostic * 534126Sbostic * Redistribution and use in source and binary forms are permitted 6*34931Sbostic * provided that the above copyright notice and this paragraph are 7*34931Sbostic * duplicated in all such forms and that any documentation, 8*34931Sbostic * advertising materials, and other materials related to such 9*34931Sbostic * distribution and use acknowledge that the software was developed 10*34931Sbostic * by the University of California, Berkeley. The name of the 11*34931Sbostic * University may not be used to endorse or promote products derived 12*34931Sbostic * from this software without specific prior written permission. 13*34931Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34931Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34931Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634126Sbostic * 1734126Sbostic * All recipients should regard themselves as participants in an ongoing 1834126Sbostic * research project and hence should feel obligated to report their 1934126Sbostic * experiences (good or bad) with these elementary function codes, using 2034126Sbostic * the sendbug(8) program, to the authors. 2124605Szliu */ 2224605Szliu 2324605Szliu #ifndef lint 24*34931Sbostic static char sccsid[] = "@(#)pow.c 5.3 (Berkeley) 06/30/88"; 2534126Sbostic #endif /* not lint */ 2624605Szliu 2724605Szliu /* POW(X,Y) 2824605Szliu * RETURN X**Y 2924605Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 3024605Szliu * CODED IN C BY K.C. NG, 1/8/85; 3124605Szliu * REVISED BY K.C. NG on 7/10/85. 3224605Szliu * 3324605Szliu * Required system supported functions: 3424605Szliu * scalb(x,n) 3524605Szliu * logb(x) 3624605Szliu * copysign(x,y) 3724605Szliu * finite(x) 3824605Szliu * drem(x,y) 3924605Szliu * 4024605Szliu * Required kernel functions: 4124605Szliu * exp__E(a,c) ...return exp(a+c) - 1 - a*a/2 4224605Szliu * log__L(x) ...return (log(1+x) - 2s)/s, s=x/(2+x) 4324605Szliu * pow_p(x,y) ...return +(anything)**(finite non zero) 4424605Szliu * 4524605Szliu * Method 4624605Szliu * 1. Compute and return log(x) in three pieces: 4724605Szliu * log(x) = n*ln2 + hi + lo, 4824605Szliu * where n is an integer. 4924605Szliu * 2. Perform y*log(x) by simulating muti-precision arithmetic and 5024605Szliu * return the answer in three pieces: 5124605Szliu * y*log(x) = m*ln2 + hi + lo, 5224605Szliu * where m is an integer. 5324605Szliu * 3. Return x**y = exp(y*log(x)) 5424605Szliu * = 2^m * ( exp(hi+lo) ). 5524605Szliu * 5624605Szliu * Special cases: 5724605Szliu * (anything) ** 0 is 1 ; 5824605Szliu * (anything) ** 1 is itself; 5924605Szliu * (anything) ** NaN is NaN; 6024605Szliu * NaN ** (anything except 0) is NaN; 6124605Szliu * +-(anything > 1) ** +INF is +INF; 6224605Szliu * +-(anything > 1) ** -INF is +0; 6324605Szliu * +-(anything < 1) ** +INF is +0; 6424605Szliu * +-(anything < 1) ** -INF is +INF; 6524605Szliu * +-1 ** +-INF is NaN and signal INVALID; 6624605Szliu * +0 ** +(anything except 0, NaN) is +0; 6724605Szliu * -0 ** +(anything except 0, NaN, odd integer) is +0; 6824605Szliu * +0 ** -(anything except 0, NaN) is +INF and signal DIV-BY-ZERO; 6924605Szliu * -0 ** -(anything except 0, NaN, odd integer) is +INF with signal; 7024605Szliu * -0 ** (odd integer) = -( +0 ** (odd integer) ); 7124605Szliu * +INF ** +(anything except 0,NaN) is +INF; 7224605Szliu * +INF ** -(anything except 0,NaN) is +0; 7324605Szliu * -INF ** (odd integer) = -( +INF ** (odd integer) ); 7424605Szliu * -INF ** (even integer) = ( +INF ** (even integer) ); 7524605Szliu * -INF ** -(anything except integer,NaN) is NaN with signal; 7624605Szliu * -(x=anything) ** (k=integer) is (-1)**k * (x ** k); 7724605Szliu * -(anything except 0) ** (non-integer) is NaN with signal; 7824605Szliu * 7924605Szliu * Accuracy: 8024605Szliu * pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX, 8124605Szliu * and a Zilog Z8000, 8224605Szliu * pow(integer,integer) 8324605Szliu * always returns the correct integer provided it is representable. 8424605Szliu * In a test run with 100,000 random arguments with 0 < x, y < 20.0 8524605Szliu * on a VAX, the maximum observed error was 1.79 ulps (units in the 8624605Szliu * last place). 8724605Szliu * 8824605Szliu * Constants : 8924605Szliu * The hexadecimal values are the intended ones for the following constants. 9024605Szliu * The decimal values may be used, provided that the compiler will convert 9124605Szliu * from decimal to binary accurately enough to produce the hexadecimal values 9224605Szliu * shown. 9324605Szliu */ 9424605Szliu 9531853Szliu #if defined(vax)||defined(tahoe) /* VAX D format */ 9624605Szliu #include <errno.h> 9724605Szliu extern double infnan(); 9831853Szliu #ifdef vax 9931812Szliu #define _0x(A,B) 0x/**/A/**/B 10031853Szliu #else /* vax */ 10131812Szliu #define _0x(A,B) 0x/**/B/**/A 10231853Szliu #endif /* vax */ 10326893Selefunt /* static double */ 10424605Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 10524605Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 10624605Szliu /* invln2 = 1.4426950408889634148E0 , Hex 2^ 1 * .B8AA3B295C17F1 */ 10724605Szliu /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 10831812Szliu static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)}; 10931812Szliu static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)}; 11031812Szliu static long invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)}; 11131812Szliu static long sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)}; 11224605Szliu #define ln2hi (*(double*)ln2hix) 11324605Szliu #define ln2lo (*(double*)ln2lox) 11424605Szliu #define invln2 (*(double*)invln2x) 11524605Szliu #define sqrt2 (*(double*)sqrt2x) 11631853Szliu #else /* defined(vax)||defined(tahoe) */ 11726893Selefunt static double 11824605Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 11924605Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 12024605Szliu invln2 = 1.4426950408889633870E0 , /*Hex 2^ 0 * 1.71547652B82FE */ 12124605Szliu sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 12231853Szliu #endif /* defined(vax)||defined(tahoe) */ 12324605Szliu 12426893Selefunt static double zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0; 12524605Szliu 12624605Szliu double pow(x,y) 12724605Szliu double x,y; 12824605Szliu { 12924605Szliu double drem(),pow_p(),copysign(),t; 13024605Szliu int finite(); 13124605Szliu 13224605Szliu if (y==zero) return(one); 13324605Szliu else if(y==one 13431853Szliu #if !defined(vax)&&!defined(tahoe) 13524605Szliu ||x!=x 13631853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 13724605Szliu ) return( x ); /* if x is NaN or y=1 */ 13831853Szliu #if !defined(vax)&&!defined(tahoe) 13924605Szliu else if(y!=y) return( y ); /* if y is NaN */ 14031853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 14124605Szliu else if(!finite(y)) /* if y is INF */ 14224605Szliu if((t=copysign(x,one))==one) return(zero/zero); 14324605Szliu else if(t>one) return((y>zero)?y:zero); 14424605Szliu else return((y<zero)?-y:zero); 14524605Szliu else if(y==two) return(x*x); 14624605Szliu else if(y==negone) return(one/x); 14724605Szliu 14824605Szliu /* sign(x) = 1 */ 14924605Szliu else if(copysign(one,x)==one) return(pow_p(x,y)); 15024605Szliu 15124605Szliu /* sign(x)= -1 */ 15224605Szliu /* if y is an even integer */ 15324605Szliu else if ( (t=drem(y,two)) == zero) return( pow_p(-x,y) ); 15424605Szliu 15524605Szliu /* if y is an odd integer */ 15624605Szliu else if (copysign(t,one) == one) return( -pow_p(-x,y) ); 15724605Szliu 15824605Szliu /* Henceforth y is not an integer */ 15924605Szliu else if(x==zero) /* x is -0 */ 16024605Szliu return((y>zero)?-x:one/(-x)); 16124605Szliu else { /* return NaN */ 16231853Szliu #if defined(vax)||defined(tahoe) 16324605Szliu return (infnan(EDOM)); /* NaN */ 16431853Szliu #else /* defined(vax)||defined(tahoe) */ 16524605Szliu return(zero/zero); 16631853Szliu #endif /* defined(vax)||defined(tahoe) */ 16724605Szliu } 16824605Szliu } 16924605Szliu 17024605Szliu /* pow_p(x,y) return x**y for x with sign=1 and finite y */ 17124605Szliu static double pow_p(x,y) 17224605Szliu double x,y; 17324605Szliu { 17424605Szliu double logb(),scalb(),copysign(),log__L(),exp__E(); 17524605Szliu double c,s,t,z,tx,ty; 17631853Szliu #ifdef tahoe 17731826Szliu double tahoe_tmp; 17831853Szliu #endif /* tahoe */ 17924605Szliu float sx,sy; 18024605Szliu long k=0; 18124605Szliu int n,m; 18224605Szliu 18324605Szliu if(x==zero||!finite(x)) { /* if x is +INF or +0 */ 18431853Szliu #if defined(vax)||defined(tahoe) 18524605Szliu return((y>zero)?x:infnan(ERANGE)); /* if y<zero, return +INF */ 18631853Szliu #else /* defined(vax)||defined(tahoe) */ 18724605Szliu return((y>zero)?x:one/x); 18831853Szliu #endif /* defined(vax)||defined(tahoe) */ 18924605Szliu } 19024605Szliu if(x==1.0) return(x); /* if x=1.0, return 1 since y is finite */ 19124605Szliu 19224605Szliu /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */ 19324605Szliu z=scalb(x,-(n=logb(x))); 19431853Szliu #if !defined(vax)&&!defined(tahoe) /* IEEE double; subnormal number */ 19524605Szliu if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);} 19631853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 19724605Szliu if(z >= sqrt2 ) {n += 1; z *= half;} z -= one ; 19824605Szliu 19924605Szliu /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */ 20024605Szliu s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s)); 20124605Szliu t= z-(c-tx); tx += (z-t)-c; 20224605Szliu 20324605Szliu /* if y*log(x) is neither too big nor too small */ 20424605Szliu if((s=logb(y)+logb(n+t)) < 12.0) 20524605Szliu if(s>-60.0) { 20624605Szliu 20724605Szliu /* compute y*log(x) ~ mlog2 + t + c */ 20824605Szliu s=y*(n+invln2*t); 20924605Szliu m=s+copysign(half,s); /* m := nint(y*log(x)) */ 21024605Szliu k=y; 21124605Szliu if((double)k==y) { /* if y is an integer */ 21224605Szliu k = m-k*n; 21324605Szliu sx=t; tx+=(t-sx); } 21424605Szliu else { /* if y is not an integer */ 21524605Szliu k =m; 21624605Szliu tx+=n*ln2lo; 21724605Szliu sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; } 21824605Szliu /* end of checking whether k==y */ 21924605Szliu 22024605Szliu sy=y; ty=y-sy; /* y ~ sy + ty */ 22131853Szliu #ifdef tahoe 22231826Szliu s = (tahoe_tmp = sx)*sy-k*ln2hi; 22331853Szliu #else /* tahoe */ 22424605Szliu s=(double)sx*sy-k*ln2hi; /* (sy+ty)*(sx+tx)-kln2 */ 22531853Szliu #endif /* tahoe */ 22624605Szliu z=(tx*ty-k*ln2lo); 22724605Szliu tx=tx*sy; ty=sx*ty; 22824605Szliu t=ty+z; t+=tx; t+=s; 22924605Szliu c= -((((t-s)-tx)-ty)-z); 23024605Szliu 23124605Szliu /* return exp(y*log(x)) */ 23224605Szliu t += exp__E(t,c); return(scalb(one+t,m)); 23324605Szliu } 23424605Szliu /* end of if log(y*log(x)) > -60.0 */ 23524605Szliu 23624605Szliu else 23724605Szliu /* exp(+- tiny) = 1 with inexact flag */ 23824605Szliu {ln2hi+ln2lo; return(one);} 23924605Szliu else if(copysign(one,y)*(n+invln2*t) <zero) 24024605Szliu /* exp(-(big#)) underflows to zero */ 24124605Szliu return(scalb(one,-5000)); 24224605Szliu else 24324605Szliu /* exp(+(big#)) overflows to INF */ 24424605Szliu return(scalb(one, 5000)); 24524605Szliu 24624605Szliu } 247