1570af302Sopenharmony_ci/* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_powl.c */
2570af302Sopenharmony_ci/*
3570af302Sopenharmony_ci * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
4570af302Sopenharmony_ci *
5570af302Sopenharmony_ci * Permission to use, copy, modify, and distribute this software for any
6570af302Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
7570af302Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10570af302Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11570af302Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12570af302Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13570af302Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14570af302Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15570af302Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16570af302Sopenharmony_ci */
17570af302Sopenharmony_ci/*                                                      powl.c
18570af302Sopenharmony_ci *
19570af302Sopenharmony_ci *      Power function, long double precision
20570af302Sopenharmony_ci *
21570af302Sopenharmony_ci *
22570af302Sopenharmony_ci * SYNOPSIS:
23570af302Sopenharmony_ci *
24570af302Sopenharmony_ci * long double x, y, z, powl();
25570af302Sopenharmony_ci *
26570af302Sopenharmony_ci * z = powl( x, y );
27570af302Sopenharmony_ci *
28570af302Sopenharmony_ci *
29570af302Sopenharmony_ci * DESCRIPTION:
30570af302Sopenharmony_ci *
31570af302Sopenharmony_ci * Computes x raised to the yth power.  Analytically,
32570af302Sopenharmony_ci *
33570af302Sopenharmony_ci *      x**y  =  exp( y log(x) ).
34570af302Sopenharmony_ci *
35570af302Sopenharmony_ci * Following Cody and Waite, this program uses a lookup table
36570af302Sopenharmony_ci * of 2**-i/32 and pseudo extended precision arithmetic to
37570af302Sopenharmony_ci * obtain several extra bits of accuracy in both the logarithm
38570af302Sopenharmony_ci * and the exponential.
39570af302Sopenharmony_ci *
40570af302Sopenharmony_ci *
41570af302Sopenharmony_ci * ACCURACY:
42570af302Sopenharmony_ci *
43570af302Sopenharmony_ci * The relative error of pow(x,y) can be estimated
44570af302Sopenharmony_ci * by   y dl ln(2),   where dl is the absolute error of
45570af302Sopenharmony_ci * the internally computed base 2 logarithm.  At the ends
46570af302Sopenharmony_ci * of the approximation interval the logarithm equal 1/32
47570af302Sopenharmony_ci * and its relative error is about 1 lsb = 1.1e-19.  Hence
48570af302Sopenharmony_ci * the predicted relative error in the result is 2.3e-21 y .
49570af302Sopenharmony_ci *
50570af302Sopenharmony_ci *                      Relative error:
51570af302Sopenharmony_ci * arithmetic   domain     # trials      peak         rms
52570af302Sopenharmony_ci *
53570af302Sopenharmony_ci *    IEEE     +-1000       40000      2.8e-18      3.7e-19
54570af302Sopenharmony_ci * .001 < x < 1000, with log(x) uniformly distributed.
55570af302Sopenharmony_ci * -1000 < y < 1000, y uniformly distributed.
56570af302Sopenharmony_ci *
57570af302Sopenharmony_ci *    IEEE     0,8700       60000      6.5e-18      1.0e-18
58570af302Sopenharmony_ci * 0.99 < x < 1.01, 0 < y < 8700, uniformly distributed.
59570af302Sopenharmony_ci *
60570af302Sopenharmony_ci *
61570af302Sopenharmony_ci * ERROR MESSAGES:
62570af302Sopenharmony_ci *
63570af302Sopenharmony_ci *   message         condition      value returned
64570af302Sopenharmony_ci * pow overflow     x**y > MAXNUM      INFINITY
65570af302Sopenharmony_ci * pow underflow   x**y < 1/MAXNUM       0.0
66570af302Sopenharmony_ci * pow domain      x<0 and y noninteger  0.0
67570af302Sopenharmony_ci *
68570af302Sopenharmony_ci */
69570af302Sopenharmony_ci
70570af302Sopenharmony_ci#include "libm.h"
71570af302Sopenharmony_ci
72570af302Sopenharmony_ci#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
73570af302Sopenharmony_cilong double powl(long double x, long double y)
74570af302Sopenharmony_ci{
75570af302Sopenharmony_ci	return pow(x, y);
76570af302Sopenharmony_ci}
77570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
78570af302Sopenharmony_ci
79570af302Sopenharmony_ci/* Table size */
80570af302Sopenharmony_ci#define NXT 32
81570af302Sopenharmony_ci
82570af302Sopenharmony_ci/* log(1+x) =  x - .5x^2 + x^3 *  P(z)/Q(z)
83570af302Sopenharmony_ci * on the domain  2^(-1/32) - 1  <=  x  <=  2^(1/32) - 1
84570af302Sopenharmony_ci */
85570af302Sopenharmony_cistatic const long double P[] = {
86570af302Sopenharmony_ci 8.3319510773868690346226E-4L,
87570af302Sopenharmony_ci 4.9000050881978028599627E-1L,
88570af302Sopenharmony_ci 1.7500123722550302671919E0L,
89570af302Sopenharmony_ci 1.4000100839971580279335E0L,
90570af302Sopenharmony_ci};
91570af302Sopenharmony_cistatic const long double Q[] = {
92570af302Sopenharmony_ci/* 1.0000000000000000000000E0L,*/
93570af302Sopenharmony_ci 5.2500282295834889175431E0L,
94570af302Sopenharmony_ci 8.4000598057587009834666E0L,
95570af302Sopenharmony_ci 4.2000302519914740834728E0L,
96570af302Sopenharmony_ci};
97570af302Sopenharmony_ci/* A[i] = 2^(-i/32), rounded to IEEE long double precision.
98570af302Sopenharmony_ci * If i is even, A[i] + B[i/2] gives additional accuracy.
99570af302Sopenharmony_ci */
100570af302Sopenharmony_cistatic const long double A[33] = {
101570af302Sopenharmony_ci 1.0000000000000000000000E0L,
102570af302Sopenharmony_ci 9.7857206208770013448287E-1L,
103570af302Sopenharmony_ci 9.5760328069857364691013E-1L,
104570af302Sopenharmony_ci 9.3708381705514995065011E-1L,
105570af302Sopenharmony_ci 9.1700404320467123175367E-1L,
106570af302Sopenharmony_ci 8.9735453750155359320742E-1L,
107570af302Sopenharmony_ci 8.7812608018664974155474E-1L,
108570af302Sopenharmony_ci 8.5930964906123895780165E-1L,
109570af302Sopenharmony_ci 8.4089641525371454301892E-1L,
110570af302Sopenharmony_ci 8.2287773907698242225554E-1L,
111570af302Sopenharmony_ci 8.0524516597462715409607E-1L,
112570af302Sopenharmony_ci 7.8799042255394324325455E-1L,
113570af302Sopenharmony_ci 7.7110541270397041179298E-1L,
114570af302Sopenharmony_ci 7.5458221379671136985669E-1L,
115570af302Sopenharmony_ci 7.3841307296974965571198E-1L,
116570af302Sopenharmony_ci 7.2259040348852331001267E-1L,
117570af302Sopenharmony_ci 7.0710678118654752438189E-1L,
118570af302Sopenharmony_ci 6.9195494098191597746178E-1L,
119570af302Sopenharmony_ci 6.7712777346844636413344E-1L,
120570af302Sopenharmony_ci 6.6261832157987064729696E-1L,
121570af302Sopenharmony_ci 6.4841977732550483296079E-1L,
122570af302Sopenharmony_ci 6.3452547859586661129850E-1L,
123570af302Sopenharmony_ci 6.2092890603674202431705E-1L,
124570af302Sopenharmony_ci 6.0762367999023443907803E-1L,
125570af302Sopenharmony_ci 5.9460355750136053334378E-1L,
126570af302Sopenharmony_ci 5.8186242938878875689693E-1L,
127570af302Sopenharmony_ci 5.6939431737834582684856E-1L,
128570af302Sopenharmony_ci 5.5719337129794626814472E-1L,
129570af302Sopenharmony_ci 5.4525386633262882960438E-1L,
130570af302Sopenharmony_ci 5.3357020033841180906486E-1L,
131570af302Sopenharmony_ci 5.2213689121370692017331E-1L,
132570af302Sopenharmony_ci 5.1094857432705833910408E-1L,
133570af302Sopenharmony_ci 5.0000000000000000000000E-1L,
134570af302Sopenharmony_ci};
135570af302Sopenharmony_cistatic const long double B[17] = {
136570af302Sopenharmony_ci 0.0000000000000000000000E0L,
137570af302Sopenharmony_ci 2.6176170809902549338711E-20L,
138570af302Sopenharmony_ci-1.0126791927256478897086E-20L,
139570af302Sopenharmony_ci 1.3438228172316276937655E-21L,
140570af302Sopenharmony_ci 1.2207982955417546912101E-20L,
141570af302Sopenharmony_ci-6.3084814358060867200133E-21L,
142570af302Sopenharmony_ci 1.3164426894366316434230E-20L,
143570af302Sopenharmony_ci-1.8527916071632873716786E-20L,
144570af302Sopenharmony_ci 1.8950325588932570796551E-20L,
145570af302Sopenharmony_ci 1.5564775779538780478155E-20L,
146570af302Sopenharmony_ci 6.0859793637556860974380E-21L,
147570af302Sopenharmony_ci-2.0208749253662532228949E-20L,
148570af302Sopenharmony_ci 1.4966292219224761844552E-20L,
149570af302Sopenharmony_ci 3.3540909728056476875639E-21L,
150570af302Sopenharmony_ci-8.6987564101742849540743E-22L,
151570af302Sopenharmony_ci-1.2327176863327626135542E-20L,
152570af302Sopenharmony_ci 0.0000000000000000000000E0L,
153570af302Sopenharmony_ci};
154570af302Sopenharmony_ci
155570af302Sopenharmony_ci/* 2^x = 1 + x P(x),
156570af302Sopenharmony_ci * on the interval -1/32 <= x <= 0
157570af302Sopenharmony_ci */
158570af302Sopenharmony_cistatic const long double R[] = {
159570af302Sopenharmony_ci 1.5089970579127659901157E-5L,
160570af302Sopenharmony_ci 1.5402715328927013076125E-4L,
161570af302Sopenharmony_ci 1.3333556028915671091390E-3L,
162570af302Sopenharmony_ci 9.6181291046036762031786E-3L,
163570af302Sopenharmony_ci 5.5504108664798463044015E-2L,
164570af302Sopenharmony_ci 2.4022650695910062854352E-1L,
165570af302Sopenharmony_ci 6.9314718055994530931447E-1L,
166570af302Sopenharmony_ci};
167570af302Sopenharmony_ci
168570af302Sopenharmony_ci#define MEXP (NXT*16384.0L)
169570af302Sopenharmony_ci/* The following if denormal numbers are supported, else -MEXP: */
170570af302Sopenharmony_ci#define MNEXP (-NXT*(16384.0L+64.0L))
171570af302Sopenharmony_ci/* log2(e) - 1 */
172570af302Sopenharmony_ci#define LOG2EA 0.44269504088896340735992L
173570af302Sopenharmony_ci
174570af302Sopenharmony_ci#define F W
175570af302Sopenharmony_ci#define Fa Wa
176570af302Sopenharmony_ci#define Fb Wb
177570af302Sopenharmony_ci#define G W
178570af302Sopenharmony_ci#define Ga Wa
179570af302Sopenharmony_ci#define Gb u
180570af302Sopenharmony_ci#define H W
181570af302Sopenharmony_ci#define Ha Wb
182570af302Sopenharmony_ci#define Hb Wb
183570af302Sopenharmony_ci
184570af302Sopenharmony_cistatic const long double MAXLOGL = 1.1356523406294143949492E4L;
185570af302Sopenharmony_cistatic const long double MINLOGL = -1.13994985314888605586758E4L;
186570af302Sopenharmony_cistatic const long double LOGE2L = 6.9314718055994530941723E-1L;
187570af302Sopenharmony_cistatic const long double huge = 0x1p10000L;
188570af302Sopenharmony_ci/* XXX Prevent gcc from erroneously constant folding this. */
189570af302Sopenharmony_cistatic const volatile long double twom10000 = 0x1p-10000L;
190570af302Sopenharmony_ci
191570af302Sopenharmony_cistatic long double reducl(long double);
192570af302Sopenharmony_cistatic long double powil(long double, int);
193570af302Sopenharmony_ci
194570af302Sopenharmony_cilong double powl(long double x, long double y)
195570af302Sopenharmony_ci{
196570af302Sopenharmony_ci	/* double F, Fa, Fb, G, Ga, Gb, H, Ha, Hb */
197570af302Sopenharmony_ci	int i, nflg, iyflg, yoddint;
198570af302Sopenharmony_ci	long e;
199570af302Sopenharmony_ci	volatile long double z=0;
200570af302Sopenharmony_ci	long double w=0, W=0, Wa=0, Wb=0, ya=0, yb=0, u=0;
201570af302Sopenharmony_ci
202570af302Sopenharmony_ci	/* make sure no invalid exception is raised by nan comparision */
203570af302Sopenharmony_ci	if (isnan(x)) {
204570af302Sopenharmony_ci		if (!isnan(y) && y == 0.0)
205570af302Sopenharmony_ci			return 1.0;
206570af302Sopenharmony_ci		return x;
207570af302Sopenharmony_ci	}
208570af302Sopenharmony_ci	if (isnan(y)) {
209570af302Sopenharmony_ci		if (x == 1.0)
210570af302Sopenharmony_ci			return 1.0;
211570af302Sopenharmony_ci		return y;
212570af302Sopenharmony_ci	}
213570af302Sopenharmony_ci	if (x == 1.0)
214570af302Sopenharmony_ci		return 1.0; /* 1**y = 1, even if y is nan */
215570af302Sopenharmony_ci	if (y == 0.0)
216570af302Sopenharmony_ci		return 1.0; /* x**0 = 1, even if x is nan */
217570af302Sopenharmony_ci	if (y == 1.0)
218570af302Sopenharmony_ci		return x;
219570af302Sopenharmony_ci	/* if y*log2(x) < log2(LDBL_TRUE_MIN)-1 then x^y uflows to 0
220570af302Sopenharmony_ci	   if y*log2(x) > -log2(LDBL_TRUE_MIN)+1 > LDBL_MAX_EXP then x^y oflows
221570af302Sopenharmony_ci	   if |x|!=1 then |log2(x)| > |log(x)| > LDBL_EPSILON/2 so
222570af302Sopenharmony_ci	   x^y oflows/uflows if |y|*LDBL_EPSILON/2 > -log2(LDBL_TRUE_MIN)+1 */
223570af302Sopenharmony_ci	if (fabsl(y) > 2*(-LDBL_MIN_EXP+LDBL_MANT_DIG+1)/LDBL_EPSILON) {
224570af302Sopenharmony_ci		/* y is not an odd int */
225570af302Sopenharmony_ci		if (x == -1.0)
226570af302Sopenharmony_ci			return 1.0;
227570af302Sopenharmony_ci		if (y == INFINITY) {
228570af302Sopenharmony_ci			if (x > 1.0 || x < -1.0)
229570af302Sopenharmony_ci				return INFINITY;
230570af302Sopenharmony_ci			return 0.0;
231570af302Sopenharmony_ci		}
232570af302Sopenharmony_ci		if (y == -INFINITY) {
233570af302Sopenharmony_ci			if (x > 1.0 || x < -1.0)
234570af302Sopenharmony_ci				return 0.0;
235570af302Sopenharmony_ci			return INFINITY;
236570af302Sopenharmony_ci		}
237570af302Sopenharmony_ci		if ((x > 1.0 || x < -1.0) == (y > 0))
238570af302Sopenharmony_ci			return huge * huge;
239570af302Sopenharmony_ci		return twom10000 * twom10000;
240570af302Sopenharmony_ci	}
241570af302Sopenharmony_ci	if (x == INFINITY) {
242570af302Sopenharmony_ci		if (y > 0.0)
243570af302Sopenharmony_ci			return INFINITY;
244570af302Sopenharmony_ci		return 0.0;
245570af302Sopenharmony_ci	}
246570af302Sopenharmony_ci
247570af302Sopenharmony_ci	w = floorl(y);
248570af302Sopenharmony_ci
249570af302Sopenharmony_ci	/* Set iyflg to 1 if y is an integer. */
250570af302Sopenharmony_ci	iyflg = 0;
251570af302Sopenharmony_ci	if (w == y)
252570af302Sopenharmony_ci		iyflg = 1;
253570af302Sopenharmony_ci
254570af302Sopenharmony_ci	/* Test for odd integer y. */
255570af302Sopenharmony_ci	yoddint = 0;
256570af302Sopenharmony_ci	if (iyflg) {
257570af302Sopenharmony_ci		ya = fabsl(y);
258570af302Sopenharmony_ci		ya = floorl(0.5 * ya);
259570af302Sopenharmony_ci		yb = 0.5 * fabsl(w);
260570af302Sopenharmony_ci		if( ya != yb )
261570af302Sopenharmony_ci			yoddint = 1;
262570af302Sopenharmony_ci	}
263570af302Sopenharmony_ci
264570af302Sopenharmony_ci	if (x == -INFINITY) {
265570af302Sopenharmony_ci		if (y > 0.0) {
266570af302Sopenharmony_ci			if (yoddint)
267570af302Sopenharmony_ci				return -INFINITY;
268570af302Sopenharmony_ci			return INFINITY;
269570af302Sopenharmony_ci		}
270570af302Sopenharmony_ci		if (y < 0.0) {
271570af302Sopenharmony_ci			if (yoddint)
272570af302Sopenharmony_ci				return -0.0;
273570af302Sopenharmony_ci			return 0.0;
274570af302Sopenharmony_ci		}
275570af302Sopenharmony_ci	}
276570af302Sopenharmony_ci	nflg = 0; /* (x<0)**(odd int) */
277570af302Sopenharmony_ci	if (x <= 0.0) {
278570af302Sopenharmony_ci		if (x == 0.0) {
279570af302Sopenharmony_ci			if (y < 0.0) {
280570af302Sopenharmony_ci				if (signbit(x) && yoddint)
281570af302Sopenharmony_ci					/* (-0.0)**(-odd int) = -inf, divbyzero */
282570af302Sopenharmony_ci					return -1.0/0.0;
283570af302Sopenharmony_ci				/* (+-0.0)**(negative) = inf, divbyzero */
284570af302Sopenharmony_ci				return 1.0/0.0;
285570af302Sopenharmony_ci			}
286570af302Sopenharmony_ci			if (signbit(x) && yoddint)
287570af302Sopenharmony_ci				return -0.0;
288570af302Sopenharmony_ci			return 0.0;
289570af302Sopenharmony_ci		}
290570af302Sopenharmony_ci		if (iyflg == 0)
291570af302Sopenharmony_ci			return (x - x) / (x - x); /* (x<0)**(non-int) is NaN */
292570af302Sopenharmony_ci		/* (x<0)**(integer) */
293570af302Sopenharmony_ci		if (yoddint)
294570af302Sopenharmony_ci			nflg = 1; /* negate result */
295570af302Sopenharmony_ci		x = -x;
296570af302Sopenharmony_ci	}
297570af302Sopenharmony_ci	/* (+integer)**(integer)  */
298570af302Sopenharmony_ci	if (iyflg && floorl(x) == x && fabsl(y) < 32768.0) {
299570af302Sopenharmony_ci		w = powil(x, (int)y);
300570af302Sopenharmony_ci		return nflg ? -w : w;
301570af302Sopenharmony_ci	}
302570af302Sopenharmony_ci
303570af302Sopenharmony_ci	/* separate significand from exponent */
304570af302Sopenharmony_ci	x = frexpl(x, &i);
305570af302Sopenharmony_ci	e = i;
306570af302Sopenharmony_ci
307570af302Sopenharmony_ci	/* find significand in antilog table A[] */
308570af302Sopenharmony_ci	i = 1;
309570af302Sopenharmony_ci	if (x <= A[17])
310570af302Sopenharmony_ci		i = 17;
311570af302Sopenharmony_ci	if (x <= A[i+8])
312570af302Sopenharmony_ci		i += 8;
313570af302Sopenharmony_ci	if (x <= A[i+4])
314570af302Sopenharmony_ci		i += 4;
315570af302Sopenharmony_ci	if (x <= A[i+2])
316570af302Sopenharmony_ci		i += 2;
317570af302Sopenharmony_ci	if (x >= A[1])
318570af302Sopenharmony_ci		i = -1;
319570af302Sopenharmony_ci	i += 1;
320570af302Sopenharmony_ci
321570af302Sopenharmony_ci	/* Find (x - A[i])/A[i]
322570af302Sopenharmony_ci	 * in order to compute log(x/A[i]):
323570af302Sopenharmony_ci	 *
324570af302Sopenharmony_ci	 * log(x) = log( a x/a ) = log(a) + log(x/a)
325570af302Sopenharmony_ci	 *
326570af302Sopenharmony_ci	 * log(x/a) = log(1+v),  v = x/a - 1 = (x-a)/a
327570af302Sopenharmony_ci	 */
328570af302Sopenharmony_ci	x -= A[i];
329570af302Sopenharmony_ci	x -= B[i/2];
330570af302Sopenharmony_ci	x /= A[i];
331570af302Sopenharmony_ci
332570af302Sopenharmony_ci	/* rational approximation for log(1+v):
333570af302Sopenharmony_ci	 *
334570af302Sopenharmony_ci	 * log(1+v)  =  v  -  v**2/2  +  v**3 P(v) / Q(v)
335570af302Sopenharmony_ci	 */
336570af302Sopenharmony_ci	z = x*x;
337570af302Sopenharmony_ci	w = x * (z * __polevll(x, P, 3) / __p1evll(x, Q, 3));
338570af302Sopenharmony_ci	w = w - 0.5*z;
339570af302Sopenharmony_ci
340570af302Sopenharmony_ci	/* Convert to base 2 logarithm:
341570af302Sopenharmony_ci	 * multiply by log2(e) = 1 + LOG2EA
342570af302Sopenharmony_ci	 */
343570af302Sopenharmony_ci	z = LOG2EA * w;
344570af302Sopenharmony_ci	z += w;
345570af302Sopenharmony_ci	z += LOG2EA * x;
346570af302Sopenharmony_ci	z += x;
347570af302Sopenharmony_ci
348570af302Sopenharmony_ci	/* Compute exponent term of the base 2 logarithm. */
349570af302Sopenharmony_ci	w = -i;
350570af302Sopenharmony_ci	w /= NXT;
351570af302Sopenharmony_ci	w += e;
352570af302Sopenharmony_ci	/* Now base 2 log of x is w + z. */
353570af302Sopenharmony_ci
354570af302Sopenharmony_ci	/* Multiply base 2 log by y, in extended precision. */
355570af302Sopenharmony_ci
356570af302Sopenharmony_ci	/* separate y into large part ya
357570af302Sopenharmony_ci	 * and small part yb less than 1/NXT
358570af302Sopenharmony_ci	 */
359570af302Sopenharmony_ci	ya = reducl(y);
360570af302Sopenharmony_ci	yb = y - ya;
361570af302Sopenharmony_ci
362570af302Sopenharmony_ci	/* (w+z)(ya+yb)
363570af302Sopenharmony_ci	 * = w*ya + w*yb + z*y
364570af302Sopenharmony_ci	 */
365570af302Sopenharmony_ci	F = z * y  +  w * yb;
366570af302Sopenharmony_ci	Fa = reducl(F);
367570af302Sopenharmony_ci	Fb = F - Fa;
368570af302Sopenharmony_ci
369570af302Sopenharmony_ci	G = Fa + w * ya;
370570af302Sopenharmony_ci	Ga = reducl(G);
371570af302Sopenharmony_ci	Gb = G - Ga;
372570af302Sopenharmony_ci
373570af302Sopenharmony_ci	H = Fb + Gb;
374570af302Sopenharmony_ci	Ha = reducl(H);
375570af302Sopenharmony_ci	w = (Ga + Ha) * NXT;
376570af302Sopenharmony_ci
377570af302Sopenharmony_ci	/* Test the power of 2 for overflow */
378570af302Sopenharmony_ci	if (w > MEXP)
379570af302Sopenharmony_ci		return huge * huge;  /* overflow */
380570af302Sopenharmony_ci	if (w < MNEXP)
381570af302Sopenharmony_ci		return twom10000 * twom10000;  /* underflow */
382570af302Sopenharmony_ci
383570af302Sopenharmony_ci	e = w;
384570af302Sopenharmony_ci	Hb = H - Ha;
385570af302Sopenharmony_ci
386570af302Sopenharmony_ci	if (Hb > 0.0) {
387570af302Sopenharmony_ci		e += 1;
388570af302Sopenharmony_ci		Hb -= 1.0/NXT;  /*0.0625L;*/
389570af302Sopenharmony_ci	}
390570af302Sopenharmony_ci
391570af302Sopenharmony_ci	/* Now the product y * log2(x)  =  Hb + e/NXT.
392570af302Sopenharmony_ci	 *
393570af302Sopenharmony_ci	 * Compute base 2 exponential of Hb,
394570af302Sopenharmony_ci	 * where -0.0625 <= Hb <= 0.
395570af302Sopenharmony_ci	 */
396570af302Sopenharmony_ci	z = Hb * __polevll(Hb, R, 6);  /*  z = 2**Hb - 1  */
397570af302Sopenharmony_ci
398570af302Sopenharmony_ci	/* Express e/NXT as an integer plus a negative number of (1/NXT)ths.
399570af302Sopenharmony_ci	 * Find lookup table entry for the fractional power of 2.
400570af302Sopenharmony_ci	 */
401570af302Sopenharmony_ci	if (e < 0)
402570af302Sopenharmony_ci		i = 0;
403570af302Sopenharmony_ci	else
404570af302Sopenharmony_ci		i = 1;
405570af302Sopenharmony_ci	i = e/NXT + i;
406570af302Sopenharmony_ci	e = NXT*i - e;
407570af302Sopenharmony_ci	w = A[e];
408570af302Sopenharmony_ci	z = w * z;  /*  2**-e * ( 1 + (2**Hb-1) )  */
409570af302Sopenharmony_ci	z = z + w;
410570af302Sopenharmony_ci	z = scalbnl(z, i);  /* multiply by integer power of 2 */
411570af302Sopenharmony_ci
412570af302Sopenharmony_ci	if (nflg)
413570af302Sopenharmony_ci		z = -z;
414570af302Sopenharmony_ci	return z;
415570af302Sopenharmony_ci}
416570af302Sopenharmony_ci
417570af302Sopenharmony_ci
418570af302Sopenharmony_ci/* Find a multiple of 1/NXT that is within 1/NXT of x. */
419570af302Sopenharmony_cistatic long double reducl(long double x)
420570af302Sopenharmony_ci{
421570af302Sopenharmony_ci	long double t;
422570af302Sopenharmony_ci
423570af302Sopenharmony_ci	t = x * NXT;
424570af302Sopenharmony_ci	t = floorl(t);
425570af302Sopenharmony_ci	t = t / NXT;
426570af302Sopenharmony_ci	return t;
427570af302Sopenharmony_ci}
428570af302Sopenharmony_ci
429570af302Sopenharmony_ci/*
430570af302Sopenharmony_ci *      Positive real raised to integer power, long double precision
431570af302Sopenharmony_ci *
432570af302Sopenharmony_ci *
433570af302Sopenharmony_ci * SYNOPSIS:
434570af302Sopenharmony_ci *
435570af302Sopenharmony_ci * long double x, y, powil();
436570af302Sopenharmony_ci * int n;
437570af302Sopenharmony_ci *
438570af302Sopenharmony_ci * y = powil( x, n );
439570af302Sopenharmony_ci *
440570af302Sopenharmony_ci *
441570af302Sopenharmony_ci * DESCRIPTION:
442570af302Sopenharmony_ci *
443570af302Sopenharmony_ci * Returns argument x>0 raised to the nth power.
444570af302Sopenharmony_ci * The routine efficiently decomposes n as a sum of powers of
445570af302Sopenharmony_ci * two. The desired power is a product of two-to-the-kth
446570af302Sopenharmony_ci * powers of x.  Thus to compute the 32767 power of x requires
447570af302Sopenharmony_ci * 28 multiplications instead of 32767 multiplications.
448570af302Sopenharmony_ci *
449570af302Sopenharmony_ci *
450570af302Sopenharmony_ci * ACCURACY:
451570af302Sopenharmony_ci *
452570af302Sopenharmony_ci *                      Relative error:
453570af302Sopenharmony_ci * arithmetic   x domain   n domain  # trials      peak         rms
454570af302Sopenharmony_ci *    IEEE     .001,1000  -1022,1023  50000       4.3e-17     7.8e-18
455570af302Sopenharmony_ci *    IEEE        1,2     -1022,1023  20000       3.9e-17     7.6e-18
456570af302Sopenharmony_ci *    IEEE     .99,1.01     0,8700    10000       3.6e-16     7.2e-17
457570af302Sopenharmony_ci *
458570af302Sopenharmony_ci * Returns MAXNUM on overflow, zero on underflow.
459570af302Sopenharmony_ci */
460570af302Sopenharmony_ci
461570af302Sopenharmony_cistatic long double powil(long double x, int nn)
462570af302Sopenharmony_ci{
463570af302Sopenharmony_ci	long double ww, y;
464570af302Sopenharmony_ci	long double s;
465570af302Sopenharmony_ci	int n, e, sign, lx;
466570af302Sopenharmony_ci
467570af302Sopenharmony_ci	if (nn == 0)
468570af302Sopenharmony_ci		return 1.0;
469570af302Sopenharmony_ci
470570af302Sopenharmony_ci	if (nn < 0) {
471570af302Sopenharmony_ci		sign = -1;
472570af302Sopenharmony_ci		n = -nn;
473570af302Sopenharmony_ci	} else {
474570af302Sopenharmony_ci		sign = 1;
475570af302Sopenharmony_ci		n = nn;
476570af302Sopenharmony_ci	}
477570af302Sopenharmony_ci
478570af302Sopenharmony_ci	/* Overflow detection */
479570af302Sopenharmony_ci
480570af302Sopenharmony_ci	/* Calculate approximate logarithm of answer */
481570af302Sopenharmony_ci	s = x;
482570af302Sopenharmony_ci	s = frexpl( s, &lx);
483570af302Sopenharmony_ci	e = (lx - 1)*n;
484570af302Sopenharmony_ci	if ((e == 0) || (e > 64) || (e < -64)) {
485570af302Sopenharmony_ci		s = (s - 7.0710678118654752e-1L) / (s +  7.0710678118654752e-1L);
486570af302Sopenharmony_ci		s = (2.9142135623730950L * s - 0.5 + lx) * nn * LOGE2L;
487570af302Sopenharmony_ci	} else {
488570af302Sopenharmony_ci		s = LOGE2L * e;
489570af302Sopenharmony_ci	}
490570af302Sopenharmony_ci
491570af302Sopenharmony_ci	if (s > MAXLOGL)
492570af302Sopenharmony_ci		return huge * huge;  /* overflow */
493570af302Sopenharmony_ci
494570af302Sopenharmony_ci	if (s < MINLOGL)
495570af302Sopenharmony_ci		return twom10000 * twom10000;  /* underflow */
496570af302Sopenharmony_ci	/* Handle tiny denormal answer, but with less accuracy
497570af302Sopenharmony_ci	 * since roundoff error in 1.0/x will be amplified.
498570af302Sopenharmony_ci	 * The precise demarcation should be the gradual underflow threshold.
499570af302Sopenharmony_ci	 */
500570af302Sopenharmony_ci	if (s < -MAXLOGL+2.0) {
501570af302Sopenharmony_ci		x = 1.0/x;
502570af302Sopenharmony_ci		sign = -sign;
503570af302Sopenharmony_ci	}
504570af302Sopenharmony_ci
505570af302Sopenharmony_ci	/* First bit of the power */
506570af302Sopenharmony_ci	if (n & 1)
507570af302Sopenharmony_ci		y = x;
508570af302Sopenharmony_ci	else
509570af302Sopenharmony_ci		y = 1.0;
510570af302Sopenharmony_ci
511570af302Sopenharmony_ci	ww = x;
512570af302Sopenharmony_ci	n >>= 1;
513570af302Sopenharmony_ci	while (n) {
514570af302Sopenharmony_ci		ww = ww * ww;   /* arg to the 2-to-the-kth power */
515570af302Sopenharmony_ci		if (n & 1)     /* if that bit is set, then include in product */
516570af302Sopenharmony_ci			y *= ww;
517570af302Sopenharmony_ci		n >>= 1;
518570af302Sopenharmony_ci	}
519570af302Sopenharmony_ci
520570af302Sopenharmony_ci	if (sign < 0)
521570af302Sopenharmony_ci		y = 1.0/y;
522570af302Sopenharmony_ci	return y;
523570af302Sopenharmony_ci}
524570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
525570af302Sopenharmony_ci// TODO: broken implementation to make things compile
526570af302Sopenharmony_cilong double powl(long double x, long double y)
527570af302Sopenharmony_ci{
528570af302Sopenharmony_ci	return pow(x, y);
529570af302Sopenharmony_ci}
530570af302Sopenharmony_ci#endif
531