xref: /netbsd-src/lib/libm/ld128/e_powl.c (revision cfe182f36bde4c4d81e1607954ce22a67cf35d7a)
1*cfe182f3Schristos /*-
2*cfe182f3Schristos  * ====================================================
3*cfe182f3Schristos  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4*cfe182f3Schristos  *
5*cfe182f3Schristos  * Developed at SunPro, a Sun Microsystems, Inc. business.
6*cfe182f3Schristos  * Permission to use, copy, modify, and distribute this
7*cfe182f3Schristos  * software is freely granted, provided that this notice
8*cfe182f3Schristos  * is preserved.
9*cfe182f3Schristos  * ====================================================
10*cfe182f3Schristos  */
11*cfe182f3Schristos 
12*cfe182f3Schristos /*
13*cfe182f3Schristos  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
14*cfe182f3Schristos  *
15*cfe182f3Schristos  * Permission to use, copy, modify, and distribute this software for any
16*cfe182f3Schristos  * purpose with or without fee is hereby granted, provided that the above
17*cfe182f3Schristos  * copyright notice and this permission notice appear in all copies.
18*cfe182f3Schristos  *
19*cfe182f3Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
20*cfe182f3Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
21*cfe182f3Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
22*cfe182f3Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23*cfe182f3Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
24*cfe182f3Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
25*cfe182f3Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26*cfe182f3Schristos  */
27*cfe182f3Schristos 
28*cfe182f3Schristos /* powl(x,y) return x**y
29*cfe182f3Schristos  *
30*cfe182f3Schristos  *		      n
31*cfe182f3Schristos  * Method:  Let x =  2   * (1+f)
32*cfe182f3Schristos  *	1. Compute and return log2(x) in two pieces:
33*cfe182f3Schristos  *		log2(x) = w1 + w2,
34*cfe182f3Schristos  *	   where w1 has 113-53 = 60 bit trailing zeros.
35*cfe182f3Schristos  *	2. Perform y*log2(x) = n+y' by simulating multi-precision
36*cfe182f3Schristos  *	   arithmetic, where |y'|<=0.5.
37*cfe182f3Schristos  *	3. Return x**y = 2**n*exp(y'*log2)
38*cfe182f3Schristos  *
39*cfe182f3Schristos  * Special cases:
40*cfe182f3Schristos  *	1.  (anything) ** 0  is 1
41*cfe182f3Schristos  *	2.  (anything) ** 1  is itself
42*cfe182f3Schristos  *	3.  (anything) ** NAN is NAN
43*cfe182f3Schristos  *	4.  NAN ** (anything except 0) is NAN
44*cfe182f3Schristos  *	5.  +-(|x| > 1) **  +INF is +INF
45*cfe182f3Schristos  *	6.  +-(|x| > 1) **  -INF is +0
46*cfe182f3Schristos  *	7.  +-(|x| < 1) **  +INF is +0
47*cfe182f3Schristos  *	8.  +-(|x| < 1) **  -INF is +INF
48*cfe182f3Schristos  *	9.  +-1         ** +-INF is NAN
49*cfe182f3Schristos  *	10. +0 ** (+anything except 0, NAN)               is +0
50*cfe182f3Schristos  *	11. -0 ** (+anything except 0, NAN, odd integer)  is +0
51*cfe182f3Schristos  *	12. +0 ** (-anything except 0, NAN)               is +INF
52*cfe182f3Schristos  *	13. -0 ** (-anything except 0, NAN, odd integer)  is +INF
53*cfe182f3Schristos  *	14. -0 ** (odd integer) = -( +0 ** (odd integer) )
54*cfe182f3Schristos  *	15. +INF ** (+anything except 0,NAN) is +INF
55*cfe182f3Schristos  *	16. +INF ** (-anything except 0,NAN) is +0
56*cfe182f3Schristos  *	17. -INF ** (anything)  = -0 ** (-anything)
57*cfe182f3Schristos  *	18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
58*cfe182f3Schristos  *	19. (-anything except 0 and inf) ** (non-integer) is NAN
59*cfe182f3Schristos  *
60*cfe182f3Schristos  */
61*cfe182f3Schristos 
62*cfe182f3Schristos #include <sys/cdefs.h>
63*cfe182f3Schristos #include <float.h>
64*cfe182f3Schristos #include <math.h>
65*cfe182f3Schristos 
66*cfe182f3Schristos #include "math_private.h"
67*cfe182f3Schristos 
68*cfe182f3Schristos static const long double bp[] = {
69*cfe182f3Schristos   1.0L,
70*cfe182f3Schristos   1.5L,
71*cfe182f3Schristos };
72*cfe182f3Schristos 
73*cfe182f3Schristos /* log_2(1.5) */
74*cfe182f3Schristos static const long double dp_h[] = {
75*cfe182f3Schristos   0.0,
76*cfe182f3Schristos   5.8496250072115607565592654282227158546448E-1L
77*cfe182f3Schristos };
78*cfe182f3Schristos 
79*cfe182f3Schristos /* Low part of log_2(1.5) */
80*cfe182f3Schristos static const long double dp_l[] = {
81*cfe182f3Schristos   0.0,
82*cfe182f3Schristos   1.0579781240112554492329533686862998106046E-16L
83*cfe182f3Schristos };
84*cfe182f3Schristos 
85*cfe182f3Schristos static const long double zero = 0.0L,
86*cfe182f3Schristos   one = 1.0L,
87*cfe182f3Schristos   two = 2.0L,
88*cfe182f3Schristos   two113 = 1.0384593717069655257060992658440192E34L,
89*cfe182f3Schristos   huge = 1.0e3000L,
90*cfe182f3Schristos   tiny = 1.0e-3000L;
91*cfe182f3Schristos 
92*cfe182f3Schristos /* 3/2 log x = 3 z + z^3 + z^3 (z^2 R(z^2))
93*cfe182f3Schristos    z = (x-1)/(x+1)
94*cfe182f3Schristos    1 <= x <= 1.25
95*cfe182f3Schristos    Peak relative error 2.3e-37 */
96*cfe182f3Schristos static const long double LN[] =
97*cfe182f3Schristos {
98*cfe182f3Schristos  -3.0779177200290054398792536829702930623200E1L,
99*cfe182f3Schristos   6.5135778082209159921251824580292116201640E1L,
100*cfe182f3Schristos  -4.6312921812152436921591152809994014413540E1L,
101*cfe182f3Schristos   1.2510208195629420304615674658258363295208E1L,
102*cfe182f3Schristos  -9.9266909031921425609179910128531667336670E-1L
103*cfe182f3Schristos };
104*cfe182f3Schristos static const long double LD[] =
105*cfe182f3Schristos {
106*cfe182f3Schristos  -5.129862866715009066465422805058933131960E1L,
107*cfe182f3Schristos   1.452015077564081884387441590064272782044E2L,
108*cfe182f3Schristos  -1.524043275549860505277434040464085593165E2L,
109*cfe182f3Schristos   7.236063513651544224319663428634139768808E1L,
110*cfe182f3Schristos  -1.494198912340228235853027849917095580053E1L
111*cfe182f3Schristos   /* 1.0E0 */
112*cfe182f3Schristos };
113*cfe182f3Schristos 
114*cfe182f3Schristos /* exp(x) = 1 + x - x / (1 - 2 / (x - x^2 R(x^2)))
115*cfe182f3Schristos    0 <= x <= 0.5
116*cfe182f3Schristos    Peak relative error 5.7e-38  */
117*cfe182f3Schristos static const long double PN[] =
118*cfe182f3Schristos {
119*cfe182f3Schristos   5.081801691915377692446852383385968225675E8L,
120*cfe182f3Schristos   9.360895299872484512023336636427675327355E6L,
121*cfe182f3Schristos   4.213701282274196030811629773097579432957E4L,
122*cfe182f3Schristos   5.201006511142748908655720086041570288182E1L,
123*cfe182f3Schristos   9.088368420359444263703202925095675982530E-3L,
124*cfe182f3Schristos };
125*cfe182f3Schristos static const long double PD[] =
126*cfe182f3Schristos {
127*cfe182f3Schristos   3.049081015149226615468111430031590411682E9L,
128*cfe182f3Schristos   1.069833887183886839966085436512368982758E8L,
129*cfe182f3Schristos   8.259257717868875207333991924545445705394E5L,
130*cfe182f3Schristos   1.872583833284143212651746812884298360922E3L,
131*cfe182f3Schristos   /* 1.0E0 */
132*cfe182f3Schristos };
133*cfe182f3Schristos 
134*cfe182f3Schristos static const long double
135*cfe182f3Schristos   /* ln 2 */
136*cfe182f3Schristos   lg2 = 6.9314718055994530941723212145817656807550E-1L,
137*cfe182f3Schristos   lg2_h = 6.9314718055994528622676398299518041312695E-1L,
138*cfe182f3Schristos   lg2_l = 2.3190468138462996154948554638754786504121E-17L,
139*cfe182f3Schristos   ovt = 8.0085662595372944372e-0017L,
140*cfe182f3Schristos   /* 2/(3*log(2)) */
141*cfe182f3Schristos   cp = 9.6179669392597560490661645400126142495110E-1L,
142*cfe182f3Schristos   cp_h = 9.6179669392597555432899980587535537779331E-1L,
143*cfe182f3Schristos   cp_l = 5.0577616648125906047157785230014751039424E-17L;
144*cfe182f3Schristos 
145*cfe182f3Schristos long double
powl(long double x,long double y)146*cfe182f3Schristos powl(long double x, long double y)
147*cfe182f3Schristos {
148*cfe182f3Schristos   long double z, ax, z_h, z_l, p_h, p_l;
149*cfe182f3Schristos   long double yy1, t1, t2, r, s, t, u, v, w;
150*cfe182f3Schristos   long double s2, s_h, s_l, t_h, t_l;
151*cfe182f3Schristos   int32_t i, j, k, yisint, n;
152*cfe182f3Schristos   u_int32_t ix, iy;
153*cfe182f3Schristos   int32_t hx, hy;
154*cfe182f3Schristos   ieee_quad_shape_type o, p, q;
155*cfe182f3Schristos 
156*cfe182f3Schristos   p.value = x;
157*cfe182f3Schristos   hx = p.parts32.mswhi;
158*cfe182f3Schristos   ix = hx & 0x7fffffff;
159*cfe182f3Schristos 
160*cfe182f3Schristos   q.value = y;
161*cfe182f3Schristos   hy = q.parts32.mswhi;
162*cfe182f3Schristos   iy = hy & 0x7fffffff;
163*cfe182f3Schristos 
164*cfe182f3Schristos 
165*cfe182f3Schristos   /* y==zero: x**0 = 1 */
166*cfe182f3Schristos   if ((iy | q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0)
167*cfe182f3Schristos     return one;
168*cfe182f3Schristos 
169*cfe182f3Schristos   /* 1.0**y = 1; -1.0**+-Inf = 1 */
170*cfe182f3Schristos   if (x == one)
171*cfe182f3Schristos     return one;
172*cfe182f3Schristos   if (x == -1.0L && iy == 0x7fff0000
173*cfe182f3Schristos       && (q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0)
174*cfe182f3Schristos     return one;
175*cfe182f3Schristos 
176*cfe182f3Schristos   /* +-NaN return x+y */
177*cfe182f3Schristos   if ((ix > 0x7fff0000)
178*cfe182f3Schristos       || ((ix == 0x7fff0000)
179*cfe182f3Schristos 	  && ((p.parts32.mswlo | p.parts32.lswhi | p.parts32.lswlo) != 0))
180*cfe182f3Schristos       || (iy > 0x7fff0000)
181*cfe182f3Schristos       || ((iy == 0x7fff0000)
182*cfe182f3Schristos 	  && ((q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) != 0)))
183*cfe182f3Schristos     return nan_mix(x, y);
184*cfe182f3Schristos 
185*cfe182f3Schristos   /* determine if y is an odd int when x < 0
186*cfe182f3Schristos    * yisint = 0       ... y is not an integer
187*cfe182f3Schristos    * yisint = 1       ... y is an odd int
188*cfe182f3Schristos    * yisint = 2       ... y is an even int
189*cfe182f3Schristos    */
190*cfe182f3Schristos   yisint = 0;
191*cfe182f3Schristos   if (hx < 0)
192*cfe182f3Schristos     {
193*cfe182f3Schristos       if (iy >= 0x40700000)	/* 2^113 */
194*cfe182f3Schristos 	yisint = 2;		/* even integer y */
195*cfe182f3Schristos       else if (iy >= 0x3fff0000)	/* 1.0 */
196*cfe182f3Schristos 	{
197*cfe182f3Schristos 	  if (floorl (y) == y)
198*cfe182f3Schristos 	    {
199*cfe182f3Schristos 	      z = 0.5 * y;
200*cfe182f3Schristos 	      if (floorl (z) == z)
201*cfe182f3Schristos 		yisint = 2;
202*cfe182f3Schristos 	      else
203*cfe182f3Schristos 		yisint = 1;
204*cfe182f3Schristos 	    }
205*cfe182f3Schristos 	}
206*cfe182f3Schristos     }
207*cfe182f3Schristos 
208*cfe182f3Schristos   /* special value of y */
209*cfe182f3Schristos   if ((q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0)
210*cfe182f3Schristos     {
211*cfe182f3Schristos       if (iy == 0x7fff0000)	/* y is +-inf */
212*cfe182f3Schristos 	{
213*cfe182f3Schristos 	  if (((ix - 0x3fff0000) | p.parts32.mswlo | p.parts32.lswhi |
214*cfe182f3Schristos 	    p.parts32.lswlo) == 0)
215*cfe182f3Schristos 	    return y - y;	/* +-1**inf is NaN */
216*cfe182f3Schristos 	  else if (ix >= 0x3fff0000)	/* (|x|>1)**+-inf = inf,0 */
217*cfe182f3Schristos 	    return (hy >= 0) ? y : zero;
218*cfe182f3Schristos 	  else			/* (|x|<1)**-,+inf = inf,0 */
219*cfe182f3Schristos 	    return (hy < 0) ? -y : zero;
220*cfe182f3Schristos 	}
221*cfe182f3Schristos       if (iy == 0x3fff0000)
222*cfe182f3Schristos 	{			/* y is  +-1 */
223*cfe182f3Schristos 	  if (hy < 0)
224*cfe182f3Schristos 	    return one / x;
225*cfe182f3Schristos 	  else
226*cfe182f3Schristos 	    return x;
227*cfe182f3Schristos 	}
228*cfe182f3Schristos       if (hy == 0x40000000)
229*cfe182f3Schristos 	return x * x;		/* y is  2 */
230*cfe182f3Schristos       if (hy == 0x3ffe0000)
231*cfe182f3Schristos 	{			/* y is  0.5 */
232*cfe182f3Schristos 	  if (hx >= 0)		/* x >= +0 */
233*cfe182f3Schristos 	    return sqrtl (x);
234*cfe182f3Schristos 	}
235*cfe182f3Schristos     }
236*cfe182f3Schristos 
237*cfe182f3Schristos   ax = fabsl (x);
238*cfe182f3Schristos   /* special value of x */
239*cfe182f3Schristos   if ((p.parts32.mswlo | p.parts32.lswhi | p.parts32.lswlo) == 0)
240*cfe182f3Schristos     {
241*cfe182f3Schristos       if (ix == 0x7fff0000 || ix == 0 || ix == 0x3fff0000)
242*cfe182f3Schristos 	{
243*cfe182f3Schristos 	  z = ax;		/*x is +-0,+-inf,+-1 */
244*cfe182f3Schristos 	  if (hy < 0)
245*cfe182f3Schristos 	    z = one / z;	/* z = (1/|x|) */
246*cfe182f3Schristos 	  if (hx < 0)
247*cfe182f3Schristos 	    {
248*cfe182f3Schristos 	      if (((ix - 0x3fff0000) | yisint) == 0)
249*cfe182f3Schristos 		{
250*cfe182f3Schristos 		  z = (z - z) / (z - z);	/* (-1)**non-int is NaN */
251*cfe182f3Schristos 		}
252*cfe182f3Schristos 	      else if (yisint == 1)
253*cfe182f3Schristos 		z = -z;		/* (x<0)**odd = -(|x|**odd) */
254*cfe182f3Schristos 	    }
255*cfe182f3Schristos 	  return z;
256*cfe182f3Schristos 	}
257*cfe182f3Schristos     }
258*cfe182f3Schristos 
259*cfe182f3Schristos   /* (x<0)**(non-int) is NaN */
260*cfe182f3Schristos   if (((((u_int32_t) hx >> 31) - 1) | yisint) == 0)
261*cfe182f3Schristos     return (x - x) / (x - x);
262*cfe182f3Schristos 
263*cfe182f3Schristos   /* |y| is huge.
264*cfe182f3Schristos      2^-16495 = 1/2 of smallest representable value.
265*cfe182f3Schristos      If (1 - 1/131072)^y underflows, y > 1.4986e9 */
266*cfe182f3Schristos   if (iy > 0x401d654b)
267*cfe182f3Schristos     {
268*cfe182f3Schristos       /* if (1 - 2^-113)^y underflows, y > 1.1873e38 */
269*cfe182f3Schristos       if (iy > 0x407d654b)
270*cfe182f3Schristos 	{
271*cfe182f3Schristos 	  if (ix <= 0x3ffeffff)
272*cfe182f3Schristos 	    return (hy < 0) ? huge * huge : tiny * tiny;
273*cfe182f3Schristos 	  if (ix >= 0x3fff0000)
274*cfe182f3Schristos 	    return (hy > 0) ? huge * huge : tiny * tiny;
275*cfe182f3Schristos 	}
276*cfe182f3Schristos       /* over/underflow if x is not close to one */
277*cfe182f3Schristos       if (ix < 0x3ffeffff)
278*cfe182f3Schristos 	return (hy < 0) ? huge * huge : tiny * tiny;
279*cfe182f3Schristos       if (ix > 0x3fff0000)
280*cfe182f3Schristos 	return (hy > 0) ? huge * huge : tiny * tiny;
281*cfe182f3Schristos     }
282*cfe182f3Schristos 
283*cfe182f3Schristos   n = 0;
284*cfe182f3Schristos   /* take care subnormal number */
285*cfe182f3Schristos   if (ix < 0x00010000)
286*cfe182f3Schristos     {
287*cfe182f3Schristos       ax *= two113;
288*cfe182f3Schristos       n -= 113;
289*cfe182f3Schristos       o.value = ax;
290*cfe182f3Schristos       ix = o.parts32.mswhi;
291*cfe182f3Schristos     }
292*cfe182f3Schristos   n += ((ix) >> 16) - 0x3fff;
293*cfe182f3Schristos   j = ix & 0x0000ffff;
294*cfe182f3Schristos   /* determine interval */
295*cfe182f3Schristos   ix = j | 0x3fff0000;		/* normalize ix */
296*cfe182f3Schristos   if (j <= 0x3988)
297*cfe182f3Schristos     k = 0;			/* |x|<sqrt(3/2) */
298*cfe182f3Schristos   else if (j < 0xbb67)
299*cfe182f3Schristos     k = 1;			/* |x|<sqrt(3)   */
300*cfe182f3Schristos   else
301*cfe182f3Schristos     {
302*cfe182f3Schristos       k = 0;
303*cfe182f3Schristos       n += 1;
304*cfe182f3Schristos       ix -= 0x00010000;
305*cfe182f3Schristos     }
306*cfe182f3Schristos 
307*cfe182f3Schristos   o.value = ax;
308*cfe182f3Schristos   o.parts32.mswhi = ix;
309*cfe182f3Schristos   ax = o.value;
310*cfe182f3Schristos 
311*cfe182f3Schristos   /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
312*cfe182f3Schristos   u = ax - bp[k];		/* bp[0]=1.0, bp[1]=1.5 */
313*cfe182f3Schristos   v = one / (ax + bp[k]);
314*cfe182f3Schristos   s = u * v;
315*cfe182f3Schristos   s_h = s;
316*cfe182f3Schristos 
317*cfe182f3Schristos   o.value = s_h;
318*cfe182f3Schristos   o.parts32.lswlo = 0;
319*cfe182f3Schristos   o.parts32.lswhi &= 0xf8000000;
320*cfe182f3Schristos   s_h = o.value;
321*cfe182f3Schristos   /* t_h=ax+bp[k] High */
322*cfe182f3Schristos   t_h = ax + bp[k];
323*cfe182f3Schristos   o.value = t_h;
324*cfe182f3Schristos   o.parts32.lswlo = 0;
325*cfe182f3Schristos   o.parts32.lswhi &= 0xf8000000;
326*cfe182f3Schristos   t_h = o.value;
327*cfe182f3Schristos   t_l = ax - (t_h - bp[k]);
328*cfe182f3Schristos   s_l = v * ((u - s_h * t_h) - s_h * t_l);
329*cfe182f3Schristos   /* compute log(ax) */
330*cfe182f3Schristos   s2 = s * s;
331*cfe182f3Schristos   u = LN[0] + s2 * (LN[1] + s2 * (LN[2] + s2 * (LN[3] + s2 * LN[4])));
332*cfe182f3Schristos   v = LD[0] + s2 * (LD[1] + s2 * (LD[2] + s2 * (LD[3] + s2 * (LD[4] + s2))));
333*cfe182f3Schristos   r = s2 * s2 * u / v;
334*cfe182f3Schristos   r += s_l * (s_h + s);
335*cfe182f3Schristos   s2 = s_h * s_h;
336*cfe182f3Schristos   t_h = 3.0 + s2 + r;
337*cfe182f3Schristos   o.value = t_h;
338*cfe182f3Schristos   o.parts32.lswlo = 0;
339*cfe182f3Schristos   o.parts32.lswhi &= 0xf8000000;
340*cfe182f3Schristos   t_h = o.value;
341*cfe182f3Schristos   t_l = r - ((t_h - 3.0) - s2);
342*cfe182f3Schristos   /* u+v = s*(1+...) */
343*cfe182f3Schristos   u = s_h * t_h;
344*cfe182f3Schristos   v = s_l * t_h + t_l * s;
345*cfe182f3Schristos   /* 2/(3log2)*(s+...) */
346*cfe182f3Schristos   p_h = u + v;
347*cfe182f3Schristos   o.value = p_h;
348*cfe182f3Schristos   o.parts32.lswlo = 0;
349*cfe182f3Schristos   o.parts32.lswhi &= 0xf8000000;
350*cfe182f3Schristos   p_h = o.value;
351*cfe182f3Schristos   p_l = v - (p_h - u);
352*cfe182f3Schristos   z_h = cp_h * p_h;		/* cp_h+cp_l = 2/(3*log2) */
353*cfe182f3Schristos   z_l = cp_l * p_h + p_l * cp + dp_l[k];
354*cfe182f3Schristos   /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
355*cfe182f3Schristos   t = (long double) n;
356*cfe182f3Schristos   t1 = (((z_h + z_l) + dp_h[k]) + t);
357*cfe182f3Schristos   o.value = t1;
358*cfe182f3Schristos   o.parts32.lswlo = 0;
359*cfe182f3Schristos   o.parts32.lswhi &= 0xf8000000;
360*cfe182f3Schristos   t1 = o.value;
361*cfe182f3Schristos   t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
362*cfe182f3Schristos 
363*cfe182f3Schristos   /* s (sign of result -ve**odd) = -1 else = 1 */
364*cfe182f3Schristos   s = one;
365*cfe182f3Schristos   if (((((u_int32_t) hx >> 31) - 1) | (yisint - 1)) == 0)
366*cfe182f3Schristos     s = -one;			/* (-ve)**(odd int) */
367*cfe182f3Schristos 
368*cfe182f3Schristos   /* split up y into yy1+y2 and compute (yy1+y2)*(t1+t2) */
369*cfe182f3Schristos   yy1 = y;
370*cfe182f3Schristos   o.value = yy1;
371*cfe182f3Schristos   o.parts32.lswlo = 0;
372*cfe182f3Schristos   o.parts32.lswhi &= 0xf8000000;
373*cfe182f3Schristos   yy1 = o.value;
374*cfe182f3Schristos   p_l = (y - yy1) * t1 + y * t2;
375*cfe182f3Schristos   p_h = yy1 * t1;
376*cfe182f3Schristos   z = p_l + p_h;
377*cfe182f3Schristos   o.value = z;
378*cfe182f3Schristos   j = o.parts32.mswhi;
379*cfe182f3Schristos   if (j >= 0x400d0000) /* z >= 16384 */
380*cfe182f3Schristos     {
381*cfe182f3Schristos       /* if z > 16384 */
382*cfe182f3Schristos       if (((j - 0x400d0000) | o.parts32.mswlo | o.parts32.lswhi |
383*cfe182f3Schristos 	o.parts32.lswlo) != 0)
384*cfe182f3Schristos 	return s * huge * huge;	/* overflow */
385*cfe182f3Schristos       else
386*cfe182f3Schristos 	{
387*cfe182f3Schristos 	  if (p_l + ovt > z - p_h)
388*cfe182f3Schristos 	    return s * huge * huge;	/* overflow */
389*cfe182f3Schristos 	}
390*cfe182f3Schristos     }
391*cfe182f3Schristos   else if ((j & 0x7fffffff) >= 0x400d01b9)	/* z <= -16495 */
392*cfe182f3Schristos     {
393*cfe182f3Schristos       /* z < -16495 */
394*cfe182f3Schristos       if (((j - 0xc00d01bc) | o.parts32.mswlo | o.parts32.lswhi |
395*cfe182f3Schristos 	o.parts32.lswlo)
396*cfe182f3Schristos 	  != 0)
397*cfe182f3Schristos 	return s * tiny * tiny;	/* underflow */
398*cfe182f3Schristos       else
399*cfe182f3Schristos 	{
400*cfe182f3Schristos 	  if (p_l <= z - p_h)
401*cfe182f3Schristos 	    return s * tiny * tiny;	/* underflow */
402*cfe182f3Schristos 	}
403*cfe182f3Schristos     }
404*cfe182f3Schristos   /* compute 2**(p_h+p_l) */
405*cfe182f3Schristos   i = j & 0x7fffffff;
406*cfe182f3Schristos   k = (i >> 16) - 0x3fff;
407*cfe182f3Schristos   n = 0;
408*cfe182f3Schristos   if (i > 0x3ffe0000)
409*cfe182f3Schristos     {				/* if |z| > 0.5, set n = [z+0.5] */
410*cfe182f3Schristos       n = floorl (z + 0.5L);
411*cfe182f3Schristos       t = n;
412*cfe182f3Schristos       p_h -= t;
413*cfe182f3Schristos     }
414*cfe182f3Schristos   t = p_l + p_h;
415*cfe182f3Schristos   o.value = t;
416*cfe182f3Schristos   o.parts32.lswlo = 0;
417*cfe182f3Schristos   o.parts32.lswhi &= 0xf8000000;
418*cfe182f3Schristos   t = o.value;
419*cfe182f3Schristos   u = t * lg2_h;
420*cfe182f3Schristos   v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
421*cfe182f3Schristos   z = u + v;
422*cfe182f3Schristos   w = v - (z - u);
423*cfe182f3Schristos   /*  exp(z) */
424*cfe182f3Schristos   t = z * z;
425*cfe182f3Schristos   u = PN[0] + t * (PN[1] + t * (PN[2] + t * (PN[3] + t * PN[4])));
426*cfe182f3Schristos   v = PD[0] + t * (PD[1] + t * (PD[2] + t * (PD[3] + t)));
427*cfe182f3Schristos   t1 = z - t * u / v;
428*cfe182f3Schristos   r = (z * t1) / (t1 - two) - (w + z * w);
429*cfe182f3Schristos   z = one - (r - z);
430*cfe182f3Schristos   o.value = z;
431*cfe182f3Schristos   j = o.parts32.mswhi;
432*cfe182f3Schristos   j += (n << 16);
433*cfe182f3Schristos   if ((j >> 16) <= 0)
434*cfe182f3Schristos     z = scalbnl (z, n);	/* subnormal output */
435*cfe182f3Schristos   else
436*cfe182f3Schristos     {
437*cfe182f3Schristos       o.parts32.mswhi = j;
438*cfe182f3Schristos       z = o.value;
439*cfe182f3Schristos     }
440*cfe182f3Schristos   return s * z;
441*cfe182f3Schristos }
442