xref: /netbsd-src/lib/libm/noieee_src/n_pow.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*      $NetBSD: n_pow.c,v 1.1 1995/10/10 23:37:02 ragge Exp $ */
2 /*
3  * Copyright (c) 1985, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 static char sccsid[] = "@(#)pow.c	8.1 (Berkeley) 6/4/93";
37 #endif /* not lint */
38 
39 /* POW(X,Y)
40  * RETURN X**Y
41  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
42  * CODED IN C BY K.C. NG, 1/8/85;
43  * REVISED BY K.C. NG on 7/10/85.
44  * KERNEL pow_P() REPLACED BY P. McILROY 7/22/92.
45  * Required system supported functions:
46  *      scalb(x,n)
47  *      logb(x)
48  *	copysign(x,y)
49  *	finite(x)
50  *	drem(x,y)
51  *
52  * Required kernel functions:
53  *	exp__D(a,c)			exp(a + c) for |a| << |c|
54  *	struct d_double dlog(x)		r.a + r.b, |r.b| < |r.a|
55  *
56  * Method
57  *	1. Compute and return log(x) in three pieces:
58  *		log(x) = n*ln2 + hi + lo,
59  *	   where n is an integer.
60  *	2. Perform y*log(x) by simulating muti-precision arithmetic and
61  *	   return the answer in three pieces:
62  *		y*log(x) = m*ln2 + hi + lo,
63  *	   where m is an integer.
64  *	3. Return x**y = exp(y*log(x))
65  *		= 2^m * ( exp(hi+lo) ).
66  *
67  * Special cases:
68  *	(anything) ** 0  is 1 ;
69  *	(anything) ** 1  is itself;
70  *	(anything) ** NaN is NaN;
71  *	NaN ** (anything except 0) is NaN;
72  *	+(anything > 1) ** +INF is +INF;
73  *	-(anything > 1) ** +INF is NaN;
74  *	+-(anything > 1) ** -INF is +0;
75  *	+-(anything < 1) ** +INF is +0;
76  *	+(anything < 1) ** -INF is +INF;
77  *	-(anything < 1) ** -INF is NaN;
78  *	+-1 ** +-INF is NaN and signal INVALID;
79  *	+0 ** +(anything except 0, NaN)  is +0;
80  *	-0 ** +(anything except 0, NaN, odd integer)  is +0;
81  *	+0 ** -(anything except 0, NaN)  is +INF and signal DIV-BY-ZERO;
82  *	-0 ** -(anything except 0, NaN, odd integer)  is +INF with signal;
83  *	-0 ** (odd integer) = -( +0 ** (odd integer) );
84  *	+INF ** +(anything except 0,NaN) is +INF;
85  *	+INF ** -(anything except 0,NaN) is +0;
86  *	-INF ** (odd integer) = -( +INF ** (odd integer) );
87  *	-INF ** (even integer) = ( +INF ** (even integer) );
88  *	-INF ** -(anything except integer,NaN) is NaN with signal;
89  *	-(x=anything) ** (k=integer) is (-1)**k * (x ** k);
90  *	-(anything except 0) ** (non-integer) is NaN with signal;
91  *
92  * Accuracy:
93  *	pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX,
94  *	and a Zilog Z8000,
95  *			pow(integer,integer)
96  *	always returns the correct integer provided it is representable.
97  *	In a test run with 100,000 random arguments with 0 < x, y < 20.0
98  *	on a VAX, the maximum observed error was 1.79 ulps (units in the
99  *	last place).
100  *
101  * Constants :
102  * The hexadecimal values are the intended ones for the following constants.
103  * The decimal values may be used, provided that the compiler will convert
104  * from decimal to binary accurately enough to produce the hexadecimal values
105  * shown.
106  */
107 
108 #include <errno.h>
109 #include <math.h>
110 
111 #include "mathimpl.h"
112 
113 #if (defined(vax) || defined(tahoe))
114 #define TRUNC(x)	x = (double) (float) x
115 #define _IEEE		0
116 #else
117 #define _IEEE		1
118 #define endian		(((*(int *) &one)) ? 1 : 0)
119 #define TRUNC(x) 	*(((int *) &x)+endian) &= 0xf8000000
120 #define infnan(x)	0.0
121 #endif		/* vax or tahoe */
122 
123 const static double zero=0.0, one=1.0, two=2.0, negone= -1.0;
124 
125 static double pow_P __P((double, double));
126 
127 double pow(x,y)
128 double x,y;
129 {
130 	double t;
131 	if (y==zero)
132 		return (one);
133 	else if (y==one || (_IEEE && x != x))
134 		return (x);		/* if x is NaN or y=1 */
135 	else if (_IEEE && y!=y)		/* if y is NaN */
136 		return (y);
137 	else if (!finite(y))		/* if y is INF */
138 		if ((t=fabs(x))==one)	/* +-1 ** +-INF is NaN */
139 			return (y - y);
140 		else if (t>one)
141 			return ((y<0)? zero : ((x<zero)? y-y : y));
142 		else
143 			return ((y>0)? zero : ((x<0)? y-y : -y));
144 	else if (y==two)
145 		return (x*x);
146 	else if (y==negone)
147 		return (one/x);
148     /* x > 0, x == +0 */
149 	else if (copysign(one, x) == one)
150 		return (pow_P(x, y));
151 
152     /* sign(x)= -1 */
153 	/* if y is an even integer */
154 	else if ( (t=drem(y,two)) == zero)
155 		return (pow_P(-x, y));
156 
157 	/* if y is an odd integer */
158 	else if (copysign(t,one) == one)
159 		return (-pow_P(-x, y));
160 
161 	/* Henceforth y is not an integer */
162 	else if (x==zero)	/* x is -0 */
163 		return ((y>zero)? -x : one/(-x));
164 	else if (_IEEE)
165 		return (zero/zero);
166 	else
167 		return (infnan(EDOM));
168 }
169 /* kernel function for x >= 0 */
170 static double
171 #ifdef _ANSI_SOURCE
172 pow_P(double x, double y)
173 #else
174 pow_P(x, y) double x, y;
175 #endif
176 {
177 	struct Double s, t, __log__D();
178 	double  __exp__D(), huge = 1e300, tiny = 1e-300;
179 
180 	if (x == zero)
181 		if (y > zero)
182 			return (zero);
183 		else if (_IEEE)
184 			return (huge*huge);
185 		else
186 			return (infnan(ERANGE));
187 	if (x == one)
188 		return (one);
189 	if (!finite(x))
190 		if (y < zero)
191 			return (zero);
192 		else if (_IEEE)
193 			return (huge*huge);
194 		else
195 			return (infnan(ERANGE));
196 	if (y >= 7e18)		/* infinity */
197 		if (x < 1)
198 			return(tiny*tiny);
199 		else if (_IEEE)
200 			return (huge*huge);
201 		else
202 			return (infnan(ERANGE));
203 
204 	/* Return exp(y*log(x)), using simulated extended */
205 	/* precision for the log and the multiply.	  */
206 
207 	s = __log__D(x);
208 	t.a = y;
209 	TRUNC(t.a);
210 	t.b = y - t.a;
211 	t.b = s.b*y + t.b*s.a;
212 	t.a *= s.a;
213 	s.a = t.a + t.b;
214 	s.b = (t.a - s.a) + t.b;
215 	return (__exp__D(s.a, s.b));
216 }
217