clean-ups.
[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 15static char sccsid[] =
859dc438
ZAL
16"@(#)pow.c 4.5 (Berkeley) 8/21/85; 1.7 (ucb.elefunt) %G%";
17#endif /* not lint */
9f4a7cc1
ZAL
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
859dc438 87#if defined(vax)||defined(tahoe) /* VAX D format */
9f4a7cc1
ZAL
88#include <errno.h>
89extern double infnan();
859dc438 90#ifdef vax
0e01cbea 91#define _0x(A,B) 0x/**/A/**/B
859dc438 92#else /* vax */
0e01cbea 93#define _0x(A,B) 0x/**/B/**/A
859dc438 94#endif /* vax */
62b65e15 95/* static double */
9f4a7cc1
ZAL
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 */
0e01cbea
ZAL
100static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
101static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
102static long invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)};
103static long sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
9f4a7cc1
ZAL
104#define ln2hi (*(double*)ln2hix)
105#define ln2lo (*(double*)ln2lox)
106#define invln2 (*(double*)invln2x)
107#define sqrt2 (*(double*)sqrt2x)
859dc438 108#else /* defined(vax)||defined(tahoe) */
62b65e15 109static double
9f4a7cc1
ZAL
110ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */
111ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */
112invln2 = 1.4426950408889633870E0 , /*Hex 2^ 0 * 1.71547652B82FE */
113sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */
859dc438 114#endif /* defined(vax)||defined(tahoe) */
9f4a7cc1 115
62b65e15 116static double zero=0.0, half=1.0/2.0, one=1.0, two=2.0, negone= -1.0;
9f4a7cc1
ZAL
117
118double pow(x,y)
119double 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
859dc438 126#if !defined(vax)&&!defined(tahoe)
9f4a7cc1 127 ||x!=x
859dc438 128#endif /* !defined(vax)&&!defined(tahoe) */
9f4a7cc1 129 ) return( x ); /* if x is NaN or y=1 */
859dc438 130#if !defined(vax)&&!defined(tahoe)
9f4a7cc1 131 else if(y!=y) return( y ); /* if y is NaN */
859dc438 132#endif /* !defined(vax)&&!defined(tahoe) */
9f4a7cc1
ZAL
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 */
859dc438 154#if defined(vax)||defined(tahoe)
9f4a7cc1 155 return (infnan(EDOM)); /* NaN */
859dc438 156#else /* defined(vax)||defined(tahoe) */
9f4a7cc1 157 return(zero/zero);
859dc438 158#endif /* defined(vax)||defined(tahoe) */
9f4a7cc1
ZAL
159 }
160}
161
162/* pow_p(x,y) return x**y for x with sign=1 and finite y */
163static double pow_p(x,y)
164double x,y;
165{
166 double logb(),scalb(),copysign(),log__L(),exp__E();
167 double c,s,t,z,tx,ty;
859dc438 168#ifdef tahoe
471c7555 169 double tahoe_tmp;
859dc438 170#endif /* tahoe */
9f4a7cc1
ZAL
171 float sx,sy;
172 long k=0;
173 int n,m;
174
175 if(x==zero||!finite(x)) { /* if x is +INF or +0 */
859dc438 176#if defined(vax)||defined(tahoe)
9f4a7cc1 177 return((y>zero)?x:infnan(ERANGE)); /* if y<zero, return +INF */
859dc438 178#else /* defined(vax)||defined(tahoe) */
9f4a7cc1 179 return((y>zero)?x:one/x);
859dc438 180#endif /* defined(vax)||defined(tahoe) */
9f4a7cc1
ZAL
181 }
182 if(x==1.0) return(x); /* if x=1.0, return 1 since y is finite */
183
184 /* reduce x to z in [sqrt(1/2)-1, sqrt(2)-1] */
185 z=scalb(x,-(n=logb(x)));
859dc438 186#if !defined(vax)&&!defined(tahoe) /* IEEE double; subnormal number */
9f4a7cc1 187 if(n <= -1022) {n += (m=logb(z)); z=scalb(z,-m);}
859dc438 188#endif /* !defined(vax)&&!defined(tahoe) */
9f4a7cc1
ZAL
189 if(z >= sqrt2 ) {n += 1; z *= half;} z -= one ;
190
191 /* log(x) = nlog2+log(1+z) ~ nlog2 + t + tx */
192 s=z/(two+z); c=z*z*half; tx=s*(c+log__L(s*s));
193 t= z-(c-tx); tx += (z-t)-c;
194
195 /* if y*log(x) is neither too big nor too small */
196 if((s=logb(y)+logb(n+t)) < 12.0)
197 if(s>-60.0) {
198
199 /* compute y*log(x) ~ mlog2 + t + c */
200 s=y*(n+invln2*t);
201 m=s+copysign(half,s); /* m := nint(y*log(x)) */
202 k=y;
203 if((double)k==y) { /* if y is an integer */
204 k = m-k*n;
205 sx=t; tx+=(t-sx); }
206 else { /* if y is not an integer */
207 k =m;
208 tx+=n*ln2lo;
209 sx=(c=n*ln2hi)+t; tx+=(c-sx)+t; }
210 /* end of checking whether k==y */
211
212 sy=y; ty=y-sy; /* y ~ sy + ty */
859dc438 213#ifdef tahoe
471c7555 214 s = (tahoe_tmp = sx)*sy-k*ln2hi;
859dc438 215#else /* tahoe */
9f4a7cc1 216 s=(double)sx*sy-k*ln2hi; /* (sy+ty)*(sx+tx)-kln2 */
859dc438 217#endif /* tahoe */
9f4a7cc1
ZAL
218 z=(tx*ty-k*ln2lo);
219 tx=tx*sy; ty=sx*ty;
220 t=ty+z; t+=tx; t+=s;
221 c= -((((t-s)-tx)-ty)-z);
222
223 /* return exp(y*log(x)) */
224 t += exp__E(t,c); return(scalb(one+t,m));
225 }
226 /* end of if log(y*log(x)) > -60.0 */
227
228 else
229 /* exp(+- tiny) = 1 with inexact flag */
230 {ln2hi+ln2lo; return(one);}
231 else if(copysign(one,y)*(n+invln2*t) <zero)
232 /* exp(-(big#)) underflows to zero */
233 return(scalb(one,-5000));
234 else
235 /* exp(+(big#)) overflows to INF */
236 return(scalb(one, 5000));
237
238}