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