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