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