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 634931Sbostic * provided that the above copyright notice and this paragraph are 734931Sbostic * duplicated in all such forms and that any documentation, 834931Sbostic * advertising materials, and other materials related to such 934931Sbostic * distribution and use acknowledge that the software was developed 1034931Sbostic * by the University of California, Berkeley. The name of the 1134931Sbostic * University may not be used to endorse or promote products derived 1234931Sbostic * from this software without specific prior written permission. 1334931Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434931Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534931Sbostic * 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*35679Sbostic static char sccsid[] = "@(#)pow.c 5.4 (Berkeley) 09/22/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 9524605Szliu #include <errno.h> 96*35679Sbostic #include "mathimpl.h" 9724605Szliu 98*35679Sbostic vc(ln2hi, 6.9314718055829871446E-1 ,7217,4031,0000,f7d0, 0, .B17217F7D00000) 99*35679Sbostic vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC) 100*35679Sbostic vc(invln2, 1.4426950408889634148E0 ,aa3b,40b8,17f1,295c, 1, .B8AA3B295C17F1) 101*35679Sbostic vc(sqrt2, 1.4142135623730950622E0 ,04f3,40b5,de65,33f9, 1, .B504F333F9DE65) 10224605Szliu 103*35679Sbostic ic(ln2hi, 6.9314718036912381649E-1, -1, 1.62E42FEE00000) 104*35679Sbostic ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76) 105*35679Sbostic ic(invln2, 1.4426950408889633870E0, 0, 1.71547652B82FE) 106*35679Sbostic ic(sqrt2, 1.4142135623730951455E0, 0, 1.6A09E667F3BCD) 107*35679Sbostic 108*35679Sbostic #ifdef vccast 109*35679Sbostic #define ln2hi vccast(ln2hi) 110*35679Sbostic #define ln2lo vccast(ln2lo) 111*35679Sbostic #define invln2 vccast(invln2) 112*35679Sbostic #define sqrt2 vccast(sqrt2) 113*35679Sbostic #endif 114*35679Sbostic 115*35679Sbostic const static double zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0; 116*35679Sbostic 117*35679Sbostic static double pow_p(); 118*35679Sbostic 11924605Szliu double pow(x,y) 12024605Szliu double x,y; 12124605Szliu { 122*35679Sbostic double t; 12324605Szliu 12424605Szliu if (y==zero) return(one); 12524605Szliu else if(y==one 12631853Szliu #if !defined(vax)&&!defined(tahoe) 12724605Szliu ||x!=x 12831853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 12924605Szliu ) return( x ); /* if x is NaN or y=1 */ 13031853Szliu #if !defined(vax)&&!defined(tahoe) 13124605Szliu else if(y!=y) return( y ); /* if y is NaN */ 13231853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 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 */ 15431853Szliu #if defined(vax)||defined(tahoe) 15524605Szliu return (infnan(EDOM)); /* NaN */ 15631853Szliu #else /* defined(vax)||defined(tahoe) */ 15724605Szliu return(zero/zero); 15831853Szliu #endif /* defined(vax)||defined(tahoe) */ 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 c,s,t,z,tx,ty; 16731853Szliu #ifdef tahoe 16831826Szliu double tahoe_tmp; 16931853Szliu #endif /* tahoe */ 17024605Szliu float sx,sy; 17124605Szliu long k=0; 17224605Szliu int n,m; 17324605Szliu 17424605Szliu if(x==zero||!finite(x)) { /* if x is +INF or +0 */ 17531853Szliu #if defined(vax)||defined(tahoe) 17624605Szliu return((y>zero)?x:infnan(ERANGE)); /* if y<zero, return +INF */ 17731853Szliu #else /* defined(vax)||defined(tahoe) */ 17824605Szliu return((y>zero)?x:one/x); 17931853Szliu #endif /* defined(vax)||defined(tahoe) */ 18024605Szliu } 18124605Szliu if(x==1.0) return(x); /* if x=1.0, return 1 since y is finite */ 18224605Szliu 18324605Szliu /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */ 18424605Szliu z=scalb(x,-(n=logb(x))); 18531853Szliu #if !defined(vax)&&!defined(tahoe) /* IEEE double; subnormal number */ 18624605Szliu if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);} 18731853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 18824605Szliu if(z >= sqrt2 ) {n += 1; z *= half;} z -= one ; 18924605Szliu 19024605Szliu /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */ 19124605Szliu s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s)); 19224605Szliu t= z-(c-tx); tx += (z-t)-c; 19324605Szliu 19424605Szliu /* if y*log(x) is neither too big nor too small */ 19524605Szliu if((s=logb(y)+logb(n+t)) < 12.0) 19624605Szliu if(s>-60.0) { 19724605Szliu 19824605Szliu /* compute y*log(x) ~ mlog2 + t + c */ 19924605Szliu s=y*(n+invln2*t); 20024605Szliu m=s+copysign(half,s); /* m := nint(y*log(x)) */ 20124605Szliu k=y; 20224605Szliu if((double)k==y) { /* if y is an integer */ 20324605Szliu k = m-k*n; 20424605Szliu sx=t; tx+=(t-sx); } 20524605Szliu else { /* if y is not an integer */ 20624605Szliu k =m; 20724605Szliu tx+=n*ln2lo; 20824605Szliu sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; } 20924605Szliu /* end of checking whether k==y */ 21024605Szliu 21124605Szliu sy=y; ty=y-sy; /* y ~ sy + ty */ 21231853Szliu #ifdef tahoe 21331826Szliu s = (tahoe_tmp = sx)*sy-k*ln2hi; 21431853Szliu #else /* tahoe */ 21524605Szliu s=(double)sx*sy-k*ln2hi; /* (sy+ty)*(sx+tx)-kln2 */ 21631853Szliu #endif /* tahoe */ 21724605Szliu z=(tx*ty-k*ln2lo); 21824605Szliu tx=tx*sy; ty=sx*ty; 21924605Szliu t=ty+z; t+=tx; t+=s; 22024605Szliu c= -((((t-s)-tx)-ty)-z); 22124605Szliu 22224605Szliu /* return exp(y*log(x)) */ 22324605Szliu t += exp__E(t,c); return(scalb(one+t,m)); 22424605Szliu } 22524605Szliu /* end of if log(y*log(x)) > -60.0 */ 22624605Szliu 22724605Szliu else 22824605Szliu /* exp(+- tiny) = 1 with inexact flag */ 22924605Szliu {ln2hi+ln2lo; return(one);} 23024605Szliu else if(copysign(one,y)*(n+invln2*t) <zero) 23124605Szliu /* exp(-(big#)) underflows to zero */ 23224605Szliu return(scalb(one,-5000)); 23324605Szliu else 23424605Szliu /* exp(+(big#)) overflows to INF */ 23524605Szliu return(scalb(one, 5000)); 23624605Szliu 23724605Szliu } 238