1 /* 2 * Copyright (c) 1985 Regents of the University of California. 3 * 4 * Use and reproduction of this software are granted in accordance with 5 * the terms and conditions specified in the Berkeley Software License 6 * Agreement (in particular, this entails acknowledgement of the programs' 7 * source, and inclusion of this notice) with the additional understanding 8 * that all recipients should regard themselves as participants in an 9 * ongoing research project and hence should feel obligated to report 10 * their experiences (good or bad) with these elementary function codes, 11 * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 12 */ 13 14 #ifndef lint 15 static char sccsid[] = 16 "@(#)pow.c 4.5 (Berkeley) 8/21/85; 1.5 (ucb.elefunt) 07/10/87"; 17 #endif not lint 18 19 /* POW(X,Y) 20 * RETURN X**Y 21 * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 22 * CODED IN C BY K.C. NG, 1/8/85; 23 * REVISED BY K.C. NG on 7/10/85. 24 * 25 * Required system supported functions: 26 * scalb(x,n) 27 * logb(x) 28 * copysign(x,y) 29 * finite(x) 30 * drem(x,y) 31 * 32 * Required kernel functions: 33 * exp__E(a,c) ...return exp(a+c) - 1 - a*a/2 34 * log__L(x) ...return (log(1+x) - 2s)/s, s=x/(2+x) 35 * pow_p(x,y) ...return +(anything)**(finite non zero) 36 * 37 * Method 38 * 1. Compute and return log(x) in three pieces: 39 * log(x) = n*ln2 + hi + lo, 40 * where n is an integer. 41 * 2. Perform y*log(x) by simulating muti-precision arithmetic and 42 * return the answer in three pieces: 43 * y*log(x) = m*ln2 + hi + lo, 44 * where m is an integer. 45 * 3. Return x**y = exp(y*log(x)) 46 * = 2^m * ( exp(hi+lo) ). 47 * 48 * Special cases: 49 * (anything) ** 0 is 1 ; 50 * (anything) ** 1 is itself; 51 * (anything) ** NaN is NaN; 52 * NaN ** (anything except 0) is NaN; 53 * +-(anything > 1) ** +INF is +INF; 54 * +-(anything > 1) ** -INF is +0; 55 * +-(anything < 1) ** +INF is +0; 56 * +-(anything < 1) ** -INF is +INF; 57 * +-1 ** +-INF is NaN and signal INVALID; 58 * +0 ** +(anything except 0, NaN) is +0; 59 * -0 ** +(anything except 0, NaN, odd integer) is +0; 60 * +0 ** -(anything except 0, NaN) is +INF and signal DIV-BY-ZERO; 61 * -0 ** -(anything except 0, NaN, odd integer) is +INF with signal; 62 * -0 ** (odd integer) = -( +0 ** (odd integer) ); 63 * +INF ** +(anything except 0,NaN) is +INF; 64 * +INF ** -(anything except 0,NaN) is +0; 65 * -INF ** (odd integer) = -( +INF ** (odd integer) ); 66 * -INF ** (even integer) = ( +INF ** (even integer) ); 67 * -INF ** -(anything except integer,NaN) is NaN with signal; 68 * -(x=anything) ** (k=integer) is (-1)**k * (x ** k); 69 * -(anything except 0) ** (non-integer) is NaN with signal; 70 * 71 * Accuracy: 72 * pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX, 73 * and a Zilog Z8000, 74 * pow(integer,integer) 75 * always returns the correct integer provided it is representable. 76 * In a test run with 100,000 random arguments with 0 < x, y < 20.0 77 * on a VAX, the maximum observed error was 1.79 ulps (units in the 78 * last place). 79 * 80 * Constants : 81 * The hexadecimal values are the intended ones for the following constants. 82 * The decimal values may be used, provided that the compiler will convert 83 * from decimal to binary accurately enough to produce the hexadecimal values 84 * shown. 85 */ 86 87 #if (defined(VAX)||defined(TAHOE)) /* VAX D format */ 88 #include <errno.h> 89 extern double infnan(); 90 #ifdef VAX 91 #define _0x(A,B) 0x/**/A/**/B 92 #else /* VAX */ 93 #define _0x(A,B) 0x/**/B/**/A 94 #endif /* VAX */ 95 /* static double */ 96 /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 97 /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 98 /* invln2 = 1.4426950408889634148E0 , Hex 2^ 1 * .B8AA3B295C17F1 */ 99 /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 100 static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)}; 101 static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)}; 102 static long invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)}; 103 static long sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)}; 104 #define ln2hi (*(double*)ln2hix) 105 #define ln2lo (*(double*)ln2lox) 106 #define invln2 (*(double*)invln2x) 107 #define sqrt2 (*(double*)sqrt2x) 108 #else /* IEEE double */ 109 static double 110 ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 111 ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 112 invln2 = 1.4426950408889633870E0 , /*Hex 2^ 0 * 1.71547652B82FE */ 113 sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 114 #endif 115 116 static double zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0; 117 118 double pow(x,y) 119 double x,y; 120 { 121 double drem(),pow_p(),copysign(),t; 122 int finite(); 123 124 if (y==zero) return(one); 125 else if(y==one 126 #if (!defined(VAX)&&!defined(TAHOE)) 127 ||x!=x 128 #endif 129 ) return( x ); /* if x is NaN or y=1 */ 130 #if (!defined(VAX)&&!defined(TAHOE)) 131 else if(y!=y) return( y ); /* if y is NaN */ 132 #endif 133 else if(!finite(y)) /* if y is INF */ 134 if((t=copysign(x,one))==one) return(zero/zero); 135 else if(t>one) return((y>zero)?y:zero); 136 else return((y<zero)?-y:zero); 137 else if(y==two) return(x*x); 138 else if(y==negone) return(one/x); 139 140 /* sign(x) = 1 */ 141 else if(copysign(one,x)==one) return(pow_p(x,y)); 142 143 /* sign(x)= -1 */ 144 /* if y is an even integer */ 145 else if ( (t=drem(y,two)) == zero) return( pow_p(-x,y) ); 146 147 /* if y is an odd integer */ 148 else if (copysign(t,one) == one) return( -pow_p(-x,y) ); 149 150 /* Henceforth y is not an integer */ 151 else if(x==zero) /* x is -0 */ 152 return((y>zero)?-x:one/(-x)); 153 else { /* return NaN */ 154 #if (defined(VAX)||defined(TAHOE)) 155 return (infnan(EDOM)); /* NaN */ 156 #else /* IEEE double */ 157 return(zero/zero); 158 #endif 159 } 160 } 161 162 /* pow_p(x,y) return x**y for x with sign=1 and finite y */ 163 static double pow_p(x,y) 164 double x,y; 165 { 166 double logb(),scalb(),copysign(),log__L(),exp__E(); 167 double c,s,t,z,tx,ty; 168 float sx,sy; 169 long k=0; 170 int n,m; 171 172 if(x==zero||!finite(x)) { /* if x is +INF or +0 */ 173 #if (defined(VAX)||defined(TAHOE)) 174 return((y>zero)?x:infnan(ERANGE)); /* if y<zero, return +INF */ 175 #else 176 return((y>zero)?x:one/x); 177 #endif 178 } 179 if(x==1.0) return(x); /* if x=1.0, return 1 since y is finite */ 180 181 /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */ 182 z=scalb(x,-(n=logb(x))); 183 #if (!defined(VAX)&&!defined(TAHOE)) /* IEEE double; subnormal number */ 184 if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);} 185 #endif 186 if(z >= sqrt2 ) {n += 1; z *= half;} z -= one ; 187 188 /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */ 189 s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s)); 190 t= z-(c-tx); tx += (z-t)-c; 191 192 /* if y*log(x) is neither too big nor too small */ 193 if((s=logb(y)+logb(n+t)) < 12.0) 194 if(s>-60.0) { 195 196 /* compute y*log(x) ~ mlog2 + t + c */ 197 s=y*(n+invln2*t); 198 m=s+copysign(half,s); /* m := nint(y*log(x)) */ 199 k=y; 200 if((double)k==y) { /* if y is an integer */ 201 k = m-k*n; 202 sx=t; tx+=(t-sx); } 203 else { /* if y is not an integer */ 204 k =m; 205 tx+=n*ln2lo; 206 sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; } 207 /* end of checking whether k==y */ 208 209 sy=y; ty=y-sy; /* y ~ sy + ty */ 210 s=(double)sx*sy-k*ln2hi; /* (sy+ty)*(sx+tx)-kln2 */ 211 z=(tx*ty-k*ln2lo); 212 tx=tx*sy; ty=sx*ty; 213 t=ty+z; t+=tx; t+=s; 214 c= -((((t-s)-tx)-ty)-z); 215 216 /* return exp(y*log(x)) */ 217 t += exp__E(t,c); return(scalb(one+t,m)); 218 } 219 /* end of if log(y*log(x)) > -60.0 */ 220 221 else 222 /* exp(+- tiny) = 1 with inexact flag */ 223 {ln2hi+ln2lo; return(one);} 224 else if(copysign(one,y)*(n+invln2*t) <zero) 225 /* exp(-(big#)) underflows to zero */ 226 return(scalb(one,-5000)); 227 else 228 /* exp(+(big#)) overflows to INF */ 229 return(scalb(one, 5000)); 230 231 } 232