4.3BSD version dated 09/12/85.
[unix-history] / usr / src / lib / libm / common_source / pow.c
CommitLineData
9f4a7cc1
ZAL
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
a62df508
GK
15static char sccsid[] =
16"@(#)pow.c 4.5 (Berkeley) 8/21/85; 1.2 (ucb.elefunt) %G%";
9f4a7cc1
ZAL
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#ifdef VAX /* VAX D format */
88#include <errno.h>
89extern double infnan();
90
91/* double static */
92/* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */
93/* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */
94/* invln2 = 1.4426950408889634148E0 , Hex 2^ 1 * .B8AA3B295C17F1 */
95/* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */
96static long ln2hix[] = { 0x72174031, 0x0000f7d0};
97static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1};
98static long invln2x[] = { 0xaa3b40b8, 0x17f1295c};
99static long sqrt2x[] = { 0x04f340b5, 0xde6533f9};
100#define ln2hi (*(double*)ln2hix)
101#define ln2lo (*(double*)ln2lox)
102#define invln2 (*(double*)invln2x)
103#define sqrt2 (*(double*)sqrt2x)
104#else /* IEEE double */
105double static
106ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */
107ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */
108invln2 = 1.4426950408889633870E0 , /*Hex 2^ 0 * 1.71547652B82FE */
109sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */
110#endif
111
112double static zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0;
113
114double pow(x,y)
115double x,y;
116{
117 double drem(),pow_p(),copysign(),t;
118 int finite();
119
120 if (y==zero) return(one);
121 else if(y==one
122#ifndef VAX
123 ||x!=x
124#endif
125 ) return( x ); /* if x is NaN or y=1 */
126#ifndef VAX
127 else if(y!=y) return( y ); /* if y is NaN */
128#endif
129 else if(!finite(y)) /* if y is INF */
130 if((t=copysign(x,one))==one) return(zero/zero);
131 else if(t>one) return((y>zero)?y:zero);
132 else return((y<zero)?-y:zero);
133 else if(y==two) return(x*x);
134 else if(y==negone) return(one/x);
135
136 /* sign(x) = 1 */
137 else if(copysign(one,x)==one) return(pow_p(x,y));
138
139 /* sign(x)= -1 */
140 /* if y is an even integer */
141 else if ( (t=drem(y,two)) == zero) return( pow_p(-x,y) );
142
143 /* if y is an odd integer */
144 else if (copysign(t,one) == one) return( -pow_p(-x,y) );
145
146 /* Henceforth y is not an integer */
147 else if(x==zero) /* x is -0 */
148 return((y>zero)?-x:one/(-x));
149 else { /* return NaN */
150#ifdef VAX
151 return (infnan(EDOM)); /* NaN */
152#else /* IEEE double */
153 return(zero/zero);
154#endif
155 }
156}
157
158/* pow_p(x,y) return x**y for x with sign=1 and finite y */
159static double pow_p(x,y)
160double x,y;
161{
162 double logb(),scalb(),copysign(),log__L(),exp__E();
163 double c,s,t,z,tx,ty;
164 float sx,sy;
165 long k=0;
166 int n,m;
167
168 if(x==zero||!finite(x)) { /* if x is +INF or +0 */
169#ifdef VAX
170 return((y>zero)?x:infnan(ERANGE)); /* if y<zero, return +INF */
171#else
172 return((y>zero)?x:one/x);
173#endif
174 }
175 if(x==1.0) return(x); /* if x=1.0, return 1 since y is finite */
176
177 /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */
178 z=scalb(x,-(n=logb(x)));
179#ifndef VAX /* IEEE double */ /* subnormal number */
180 if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);}
181#endif
182 if(z >= sqrt2 ) {n += 1; z *= half;} z -= one ;
183
184 /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */
185 s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s));
186 t= z-(c-tx); tx += (z-t)-c;
187
188 /* if y*log(x) is neither too big nor too small */
189 if((s=logb(y)+logb(n+t)) < 12.0)
190 if(s>-60.0) {
191
192 /* compute y*log(x) ~ mlog2 + t + c */
193 s=y*(n+invln2*t);
194 m=s+copysign(half,s); /* m := nint(y*log(x)) */
195 k=y;
196 if((double)k==y) { /* if y is an integer */
197 k = m-k*n;
198 sx=t; tx+=(t-sx); }
199 else { /* if y is not an integer */
200 k =m;
201 tx+=n*ln2lo;
202 sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; }
203 /* end of checking whether k==y */
204
205 sy=y; ty=y-sy; /* y ~ sy + ty */
206 s=(double)sx*sy-k*ln2hi; /* (sy+ty)*(sx+tx)-kln2 */
207 z=(tx*ty-k*ln2lo);
208 tx=tx*sy; ty=sx*ty;
209 t=ty+z; t+=tx; t+=s;
210 c= -((((t-s)-tx)-ty)-z);
211
212 /* return exp(y*log(x)) */
213 t += exp__E(t,c); return(scalb(one+t,m));
214 }
215 /* end of if log(y*log(x)) > -60.0 */
216
217 else
218 /* exp(+- tiny) = 1 with inexact flag */
219 {ln2hi+ln2lo; return(one);}
220 else if(copysign(one,y)*(n+invln2*t) <zero)
221 /* exp(-(big#)) underflows to zero */
222 return(scalb(one,-5000));
223 else
224 /* exp(+(big#)) overflows to INF */
225 return(scalb(one, 5000));
226
227}