1*0a6a1f1dSLionel Sambuc /* $NetBSD: n_pow.c,v 1.11 2014/10/11 07:19:27 martin Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * Copyright (c) 1985, 1993
42fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras * are met:
92fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras * without specific prior written permission.
172fe8fb19SBen Gras *
182fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras * SUCH DAMAGE.
292fe8fb19SBen Gras */
302fe8fb19SBen Gras
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras static char sccsid[] = "@(#)pow.c 8.1 (Berkeley) 6/4/93";
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras #endif /* not lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras /* POW(X,Y)
382fe8fb19SBen Gras * RETURN X**Y
392fe8fb19SBen Gras * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
402fe8fb19SBen Gras * CODED IN C BY K.C. NG, 1/8/85;
412fe8fb19SBen Gras * REVISED BY K.C. NG on 7/10/85.
422fe8fb19SBen Gras * KERNEL pow_P() REPLACED BY P. McILROY 7/22/92.
432fe8fb19SBen Gras * Required system supported functions:
442fe8fb19SBen Gras * scalb(x,n)
452fe8fb19SBen Gras * logb(x)
462fe8fb19SBen Gras * copysign(x,y)
472fe8fb19SBen Gras * finite(x)
482fe8fb19SBen Gras * drem(x,y)
492fe8fb19SBen Gras *
502fe8fb19SBen Gras * Required kernel functions:
512fe8fb19SBen Gras * exp__D(a,c) exp(a + c) for |a| << |c|
522fe8fb19SBen Gras * struct d_double dlog(x) r.a + r.b, |r.b| < |r.a|
532fe8fb19SBen Gras *
542fe8fb19SBen Gras * Method
552fe8fb19SBen Gras * 1. Compute and return log(x) in three pieces:
562fe8fb19SBen Gras * log(x) = n*ln2 + hi + lo,
572fe8fb19SBen Gras * where n is an integer.
582fe8fb19SBen Gras * 2. Perform y*log(x) by simulating muti-precision arithmetic and
592fe8fb19SBen Gras * return the answer in three pieces:
602fe8fb19SBen Gras * y*log(x) = m*ln2 + hi + lo,
612fe8fb19SBen Gras * where m is an integer.
622fe8fb19SBen Gras * 3. Return x**y = exp(y*log(x))
632fe8fb19SBen Gras * = 2^m * ( exp(hi+lo) ).
642fe8fb19SBen Gras *
652fe8fb19SBen Gras * Special cases:
662fe8fb19SBen Gras * (anything) ** 0 is 1 ;
672fe8fb19SBen Gras * (anything) ** 1 is itself;
682fe8fb19SBen Gras * (anything) ** NaN is NaN;
692fe8fb19SBen Gras * NaN ** (anything except 0) is NaN;
702fe8fb19SBen Gras * +(anything > 1) ** +INF is +INF;
712fe8fb19SBen Gras * -(anything > 1) ** +INF is NaN;
722fe8fb19SBen Gras * +-(anything > 1) ** -INF is +0;
732fe8fb19SBen Gras * +-(anything < 1) ** +INF is +0;
742fe8fb19SBen Gras * +(anything < 1) ** -INF is +INF;
752fe8fb19SBen Gras * -(anything < 1) ** -INF is NaN;
762fe8fb19SBen Gras * +-1 ** +-INF is NaN and signal INVALID;
772fe8fb19SBen Gras * +0 ** +(anything except 0, NaN) is +0;
782fe8fb19SBen Gras * -0 ** +(anything except 0, NaN, odd integer) is +0;
792fe8fb19SBen Gras * +0 ** -(anything except 0, NaN) is +INF and signal DIV-BY-ZERO;
802fe8fb19SBen Gras * -0 ** -(anything except 0, NaN, odd integer) is +INF with signal;
812fe8fb19SBen Gras * -0 ** (odd integer) = -( +0 ** (odd integer) );
822fe8fb19SBen Gras * +INF ** +(anything except 0,NaN) is +INF;
832fe8fb19SBen Gras * +INF ** -(anything except 0,NaN) is +0;
842fe8fb19SBen Gras * -INF ** (odd integer) = -( +INF ** (odd integer) );
852fe8fb19SBen Gras * -INF ** (even integer) = ( +INF ** (even integer) );
862fe8fb19SBen Gras * -INF ** -(anything except integer,NaN) is NaN with signal;
872fe8fb19SBen Gras * -(x=anything) ** (k=integer) is (-1)**k * (x ** k);
882fe8fb19SBen Gras * -(anything except 0) ** (non-integer) is NaN with signal;
892fe8fb19SBen Gras *
902fe8fb19SBen Gras * Accuracy:
912fe8fb19SBen Gras * pow(x,y) returns x**y nearly rounded. In particular, on a SUN, a VAX,
922fe8fb19SBen Gras * and a Zilog Z8000,
932fe8fb19SBen Gras * pow(integer,integer)
942fe8fb19SBen Gras * always returns the correct integer provided it is representable.
952fe8fb19SBen Gras * In a test run with 100,000 random arguments with 0 < x, y < 20.0
962fe8fb19SBen Gras * on a VAX, the maximum observed error was 1.79 ulps (units in the
972fe8fb19SBen Gras * last place).
982fe8fb19SBen Gras *
992fe8fb19SBen Gras * Constants :
1002fe8fb19SBen Gras * The hexadecimal values are the intended ones for the following constants.
1012fe8fb19SBen Gras * The decimal values may be used, provided that the compiler will convert
1022fe8fb19SBen Gras * from decimal to binary accurately enough to produce the hexadecimal values
1032fe8fb19SBen Gras * shown.
1042fe8fb19SBen Gras */
1052fe8fb19SBen Gras
1062fe8fb19SBen Gras #include <errno.h>
1072fe8fb19SBen Gras #include <math.h>
1082fe8fb19SBen Gras
1092fe8fb19SBen Gras #include "mathimpl.h"
1102fe8fb19SBen Gras
1112fe8fb19SBen Gras #if (defined(__vax__) || defined(tahoe))
1122fe8fb19SBen Gras #define TRUNC(x) x = (double) (float) x
1132fe8fb19SBen Gras #define _IEEE 0
1142fe8fb19SBen Gras #else
1152fe8fb19SBen Gras #define _IEEE 1
1162fe8fb19SBen Gras #define endian (((*(int *) &one)) ? 1 : 0)
1172fe8fb19SBen Gras #define TRUNC(x) *(((int *) &x)+endian) &= 0xf8000000
1182fe8fb19SBen Gras #define infnan(x) 0.0
1192fe8fb19SBen Gras #endif /* __vax__ or tahoe */
1202fe8fb19SBen Gras
1212fe8fb19SBen Gras static const double zero=0.0, one=1.0, two=2.0, negone= -1.0;
1222fe8fb19SBen Gras
1232fe8fb19SBen Gras static double pow_P (double, double);
1242fe8fb19SBen Gras
125*0a6a1f1dSLionel Sambuc #ifdef __weak_alias
126*0a6a1f1dSLionel Sambuc __weak_alias(_powf, powf);
127*0a6a1f1dSLionel Sambuc __weak_alias(_pow, pow);
128*0a6a1f1dSLionel Sambuc __weak_alias(_powl, pow);
129*0a6a1f1dSLionel Sambuc __weak_alias(powl, pow);
130*0a6a1f1dSLionel Sambuc #endif
131*0a6a1f1dSLionel Sambuc
1322fe8fb19SBen Gras float
powf(float x,float y)1332fe8fb19SBen Gras powf(float x, float y)
1342fe8fb19SBen Gras {
1352fe8fb19SBen Gras return pow((double) x, (double) (y));
1362fe8fb19SBen Gras }
1372fe8fb19SBen Gras
1382fe8fb19SBen Gras double
pow(double x,double y)1392fe8fb19SBen Gras pow(double x, double y)
1402fe8fb19SBen Gras {
1412fe8fb19SBen Gras double t;
1422fe8fb19SBen Gras if (y==zero)
1432fe8fb19SBen Gras return (one);
1442fe8fb19SBen Gras else if (y==one || (_IEEE && x != x))
1452fe8fb19SBen Gras return (x); /* if x is NaN or y=1 */
1462fe8fb19SBen Gras else if (_IEEE && y!=y) /* if y is NaN */
1472fe8fb19SBen Gras return (y);
1482fe8fb19SBen Gras else if (!finite(y)) /* if y is INF */
1492fe8fb19SBen Gras if ((t=fabs(x))==one) /* +-1 ** +-INF is NaN */
1502fe8fb19SBen Gras return (y - y);
1512fe8fb19SBen Gras else if (t>one)
1522fe8fb19SBen Gras return ((y<0)? zero : ((x<zero)? y-y : y));
1532fe8fb19SBen Gras else
1542fe8fb19SBen Gras return ((y>0)? zero : ((x<0)? y-y : -y));
1552fe8fb19SBen Gras else if (y==two)
1562fe8fb19SBen Gras return (x*x);
1572fe8fb19SBen Gras else if (y==negone)
1582fe8fb19SBen Gras return (one/x);
1592fe8fb19SBen Gras /* x > 0, x == +0 */
1602fe8fb19SBen Gras else if (copysign(one, x) == one)
1612fe8fb19SBen Gras return (pow_P(x, y));
1622fe8fb19SBen Gras
1632fe8fb19SBen Gras /* sign(x)= -1 */
1642fe8fb19SBen Gras /* if y is an even integer */
1652fe8fb19SBen Gras else if ( (t=drem(y,two)) == zero)
1662fe8fb19SBen Gras return (pow_P(-x, y));
1672fe8fb19SBen Gras
1682fe8fb19SBen Gras /* if y is an odd integer */
1692fe8fb19SBen Gras else if (copysign(t,one) == one)
1702fe8fb19SBen Gras return (-pow_P(-x, y));
1712fe8fb19SBen Gras
1722fe8fb19SBen Gras /* Henceforth y is not an integer */
1732fe8fb19SBen Gras else if (x==zero) /* x is -0 */
1742fe8fb19SBen Gras return ((y>zero)? -x : one/(-x));
1752fe8fb19SBen Gras else if (_IEEE)
1762fe8fb19SBen Gras return (zero/zero);
1772fe8fb19SBen Gras else
1782fe8fb19SBen Gras return (infnan(EDOM));
1792fe8fb19SBen Gras }
1802fe8fb19SBen Gras
1812fe8fb19SBen Gras /* kernel function for x >= 0 */
1822fe8fb19SBen Gras static double
pow_P(double x,double y)1832fe8fb19SBen Gras pow_P(double x, double y)
1842fe8fb19SBen Gras {
1852fe8fb19SBen Gras struct Double s, t;
18684d9c625SLionel Sambuc double huge = _HUGE, tiny = _TINY;
1872fe8fb19SBen Gras
1882fe8fb19SBen Gras if (x == zero) {
1892fe8fb19SBen Gras if (y > zero)
1902fe8fb19SBen Gras return (zero);
1912fe8fb19SBen Gras else if (_IEEE)
1922fe8fb19SBen Gras return (huge*huge);
1932fe8fb19SBen Gras else
1942fe8fb19SBen Gras return (infnan(ERANGE));
1952fe8fb19SBen Gras }
1962fe8fb19SBen Gras if (x == one)
1972fe8fb19SBen Gras return (one);
1982fe8fb19SBen Gras if (!finite(x)) {
1992fe8fb19SBen Gras if (y < zero)
2002fe8fb19SBen Gras return (zero);
2012fe8fb19SBen Gras else if (_IEEE)
2022fe8fb19SBen Gras return (huge*huge);
2032fe8fb19SBen Gras else
2042fe8fb19SBen Gras return (infnan(ERANGE));
2052fe8fb19SBen Gras }
2062fe8fb19SBen Gras if (y >= 7e18) { /* infinity */
2072fe8fb19SBen Gras if (x < 1)
2082fe8fb19SBen Gras return(tiny*tiny);
2092fe8fb19SBen Gras else if (_IEEE)
2102fe8fb19SBen Gras return (huge*huge);
2112fe8fb19SBen Gras else
2122fe8fb19SBen Gras return (infnan(ERANGE));
2132fe8fb19SBen Gras }
2142fe8fb19SBen Gras
2152fe8fb19SBen Gras /* Return exp(y*log(x)), using simulated extended */
2162fe8fb19SBen Gras /* precision for the log and the multiply. */
2172fe8fb19SBen Gras
2182fe8fb19SBen Gras s = __log__D(x);
2192fe8fb19SBen Gras t.a = y;
2202fe8fb19SBen Gras TRUNC(t.a);
2212fe8fb19SBen Gras t.b = y - t.a;
2222fe8fb19SBen Gras t.b = s.b*y + t.b*s.a;
2232fe8fb19SBen Gras t.a *= s.a;
2242fe8fb19SBen Gras s.a = t.a + t.b;
2252fe8fb19SBen Gras s.b = (t.a - s.a) + t.b;
2262fe8fb19SBen Gras return (__exp__D(s.a, s.b));
2272fe8fb19SBen Gras }
228