1 /*	$OpenBSD: e_powl.c,v 1.5 2013/11/12 20:35:19 martynas Exp $	*/
2 
3 /*
4  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*							powl.c
20  *
21  *	Power function, long double precision
22  *
23  *
24  *
25  * SYNOPSIS:
26  *
27  * long double x, y, z, powl();
28  *
29  * z = powl( x, y );
30  *
31  *
32  *
33  * DESCRIPTION:
34  *
35  * Computes x raised to the yth power.  Analytically,
36  *
37  *      x**y  =  exp( y log(x) ).
38  *
39  * Following Cody and Waite, this program uses a lookup table
40  * of 2**-i/32 and pseudo extended precision arithmetic to
41  * obtain several extra bits of accuracy in both the logarithm
42  * and the exponential.
43  *
44  *
45  *
46  * ACCURACY:
47  *
48  * The relative error of pow(x,y) can be estimated
49  * by   y dl ln(2),   where dl is the absolute error of
50  * the internally computed base 2 logarithm.  At the ends
51  * of the approximation interval the logarithm equal 1/32
52  * and its relative error is about 1 lsb = 1.1e-19.  Hence
53  * the predicted relative error in the result is 2.3e-21 y .
54  *
55  *                      Relative error:
56  * arithmetic   domain     # trials      peak         rms
57  *
58  *    IEEE     +-1000       40000      2.8e-18      3.7e-19
59  * .001 < x < 1000, with log(x) uniformly distributed.
60  * -1000 < y < 1000, y uniformly distributed.
61  *
62  *    IEEE     0,8700       60000      6.5e-18      1.0e-18
63  * 0.99 < x < 1.01, 0 < y < 8700, uniformly distributed.
64  *
65  *
66  * ERROR MESSAGES:
67  *
68  *   message         condition      value returned
69  * pow overflow     x**y > MAXNUM      INFINITY
70  * pow underflow   x**y < 1/MAXNUM       0.0
71  * pow domain      x<0 and y noninteger  0.0
72  *
73  */
74 
75 
76 
77 /* Table size */
78 #define NXT 32
79 /* log2(Table size) */
80 #define LNXT 5
81 
82 /* log(1+x) =  x - .5x^2 + x^3 *  P(z)/Q(z)
83  * on the domain  2^(-1/32) - 1  <=  x  <=  2^(1/32) - 1
84  */
85 static const long double P[] = {
86  8.3319510773868690346226E-4L,
87  4.9000050881978028599627E-1L,
88  1.7500123722550302671919E0L,
89  1.4000100839971580279335E0L,
90 };
91 static const long double Q[] = {
92 /* 1.0000000000000000000000E0L,*/
93  5.2500282295834889175431E0L,
94  8.4000598057587009834666E0L,
95  4.2000302519914740834728E0L,
96 };
97 /* A[i] = 2^(-i/32), rounded to IEEE long double precision.
98  * If i is even, A[i] + B[i/2] gives additional accuracy.
99  */
100 static const long double A[33] = {
101  1.0000000000000000000000E0L,
102  9.7857206208770013448287E-1L,
103  9.5760328069857364691013E-1L,
104  9.3708381705514995065011E-1L,
105  9.1700404320467123175367E-1L,
106  8.9735453750155359320742E-1L,
107  8.7812608018664974155474E-1L,
108  8.5930964906123895780165E-1L,
109  8.4089641525371454301892E-1L,
110  8.2287773907698242225554E-1L,
111  8.0524516597462715409607E-1L,
112  7.8799042255394324325455E-1L,
113  7.7110541270397041179298E-1L,
114  7.5458221379671136985669E-1L,
115  7.3841307296974965571198E-1L,
116  7.2259040348852331001267E-1L,
117  7.0710678118654752438189E-1L,
118  6.9195494098191597746178E-1L,
119  6.7712777346844636413344E-1L,
120  6.6261832157987064729696E-1L,
121  6.4841977732550483296079E-1L,
122  6.3452547859586661129850E-1L,
123  6.2092890603674202431705E-1L,
124  6.0762367999023443907803E-1L,
125  5.9460355750136053334378E-1L,
126  5.8186242938878875689693E-1L,
127  5.6939431737834582684856E-1L,
128  5.5719337129794626814472E-1L,
129  5.4525386633262882960438E-1L,
130  5.3357020033841180906486E-1L,
131  5.2213689121370692017331E-1L,
132  5.1094857432705833910408E-1L,
133  5.0000000000000000000000E-1L,
134 };
135 static const long double B[17] = {
136  0.0000000000000000000000E0L,
137  2.6176170809902549338711E-20L,
138 -1.0126791927256478897086E-20L,
139  1.3438228172316276937655E-21L,
140  1.2207982955417546912101E-20L,
141 -6.3084814358060867200133E-21L,
142  1.3164426894366316434230E-20L,
143 -1.8527916071632873716786E-20L,
144  1.8950325588932570796551E-20L,
145  1.5564775779538780478155E-20L,
146  6.0859793637556860974380E-21L,
147 -2.0208749253662532228949E-20L,
148  1.4966292219224761844552E-20L,
149  3.3540909728056476875639E-21L,
150 -8.6987564101742849540743E-22L,
151 -1.2327176863327626135542E-20L,
152  0.0000000000000000000000E0L,
153 };
154 
155 /* 2^x = 1 + x P(x),
156  * on the interval -1/32 <= x <= 0
157  */
158 static const long double R[] = {
159  1.5089970579127659901157E-5L,
160  1.5402715328927013076125E-4L,
161  1.3333556028915671091390E-3L,
162  9.6181291046036762031786E-3L,
163  5.5504108664798463044015E-2L,
164  2.4022650695910062854352E-1L,
165  6.9314718055994530931447E-1L,
166 };
167 
168 #define douba(k) A[k]
169 #define doubb(k) B[k]
170 #define MEXP (NXT*16384.0L)
171 /* The following if denormal numbers are supported, else -MEXP: */
172 #define MNEXP (-NXT*(16384.0L+64.0L))
173 /* log2(e) - 1 */
174 #define LOG2EA 0.44269504088896340735992L
175 
176 static const long double MAXLOGL = 1.1356523406294143949492E4L;
177 static const long double MINLOGL = -1.13994985314888605586758E4L;
178 static const long double LOGE2L = 6.9314718055994530941723E-1L;
179 static const long double huge = 0x1p10000L;
180 static const long double twom10000 = 0x1p-10000L;
181 
182 static long double reducl( long double );
183 static long double powil ( long double, int );
184 
185 long double
powl(long double x,long double y)186 powl(long double x, long double y)
187 {
188 long double w, ya, yb, z;
189 long double F, Fa, Fb, G, Ga, Gb, H, Ha, Hb;
190 int i, nflg, iyflg, yoddint;
191 long e;
192 
193 if( !isnanl_inline(y) && y == 0.0L ) {
194         if (issignalingl_inline(x))
195                 return x + y;
196 	return( 1.0L );
197 }
198 
199 if( !isnanl_inline(x) && x == 1.0L ) {
200         if (issignalingl_inline(y))
201                 return x + y;
202 	return( 1.0L );
203 }
204 
205 if( isnanl_inline(x) )
206 	return( x + y );
207 if( isnanl_inline(y) )
208 	return( x + y );
209 
210 w = floorl(y);
211 /* Set iyflg to 1 if y is an integer.  */
212 iyflg = (w == y);
213 
214 /* flag = 1 if x is negative */
215 nflg = signbit(x);
216 
217 /* Test for odd integer y and negative x (including negative zero)  */
218 yoddint = 0;
219 if( iyflg && nflg )
220 	{
221         ya = ldexpl(y, -1);
222         yoddint = (ya != floorl(ya));
223 	}
224 
225 if( x == 0.0L) {
226         if( y < 0 )
227                 return __math_divzerol(yoddint);
228 }
229 
230 if( y == 1.0L )
231 	return( x );
232 
233 if( !isfinite(y) ) {
234         if (x == -1.0L)
235                 return( 1.0L );
236         if (y < 0) {
237                 if (fabsl(x) < 1)
238                         return( (long double)INFINITY );
239                 return( 0.0L );
240         } else {
241                 if (fabsl(x) < 1)
242                         return( 0.0L );
243                 return( (long double)INFINITY );
244         }
245 }
246 
247 /* y >= 2**80, infinity (x > 1) or zero (x < 1) */
248 if( y >= 0x1p80L)
249 	{
250 	if( x > 1.0L )
251 		return __math_oflowl(0);
252 	if( x > 0.0L && x < 1.0L )
253 		return( 0.0L );
254 	if( x < -1.0L )
255 		return __math_oflowl(1);
256 	if( x > -1.0L && x < 0.0L )
257 		return( -0.0L );
258 	}
259 /* y <= -2**80, zero (x > 1) or infinity (x < 1) */
260 if( y <= -0x1p80L)
261 	{
262 	if( x > 1.0L )
263 		return __math_uflowl(0);
264 	if( x > 0.0L && x < 1.0L )
265 		return( (long double)INFINITY );
266 	if( x < -1.0L )
267 		return( 0.0L );
268 	if( x > -1.0L && x < 0.0L )
269 		return( (long double)INFINITY );
270 	}
271 if( x >= LDBL_MAX )
272 	{
273 	if( y > 0.0L )
274 		return( (long double)INFINITY );
275 	return( 0.0L );
276 	}
277 
278 
279 
280 if( x <= -LDBL_MAX )
281 	{
282 	if( y > 0.0L )
283 		{
284 		if( yoddint )
285 			return( -(long double)INFINITY );
286 		return( (long double)INFINITY );
287 		}
288 	if( y < 0.0L )
289 		{
290 		if( yoddint )
291 			return( -0.0L );
292 		return( 0.0L );
293 		}
294 	}
295 
296 
297 if( x <= 0.0L )
298 	{
299 	if( x == 0.0L )
300 		{
301 		if( y < 0.0L )
302 			{
303                         if( yoddint )
304 				return( -(long double)INFINITY );
305 			return( (long double)INFINITY );
306 			}
307 		if( y > 0.0L )
308 			{
309 			if( yoddint )
310 				return( -0.0L );
311 			return( 0.0L );
312 			}
313 		if( y == 0.0L )
314 			return( 1.0L );  /*   0**0   */
315 		else
316 			return( 0.0L );  /*   0**y   */
317 		}
318 	else
319 		{
320 		if( iyflg == 0 )
321                         return __math_invalidl(x); /* (x<0)**(non-int) is NaN */
322 		}
323 	}
324 
325 /* Integer power of an integer.  */
326 
327 if( iyflg )
328 	{
329 	w = floorl(x);
330 	if( (w == x) && (fabsl(y) < 32768.0L) )
331 		{
332 		w = powil( x, (int) y );
333 		return( w );
334 		}
335 	}
336 
337 
338 if( nflg )
339 	x = fabsl(x);
340 
341 /* separate significand from exponent */
342 x = frexpl( x, &i );
343 e = i;
344 
345 /* find significand in antilog table A[] */
346 i = 1;
347 if( x <= douba(17) )
348 	i = 17;
349 if( x <= douba(i+8) )
350 	i += 8;
351 if( x <= douba(i+4) )
352 	i += 4;
353 if( x <= douba(i+2) )
354 	i += 2;
355 if( x >= douba(1) )
356 	i = -1;
357 i += 1;
358 
359 
360 /* Find (x - A[i])/A[i]
361  * in order to compute log(x/A[i]):
362  *
363  * log(x) = log( a x/a ) = log(a) + log(x/a)
364  *
365  * log(x/a) = log(1+v),  v = x/a - 1 = (x-a)/a
366  */
367 x -= douba(i);
368 x -= doubb(i/2);
369 x /= douba(i);
370 
371 
372 /* rational approximation for log(1+v):
373  *
374  * log(1+v)  =  v  -  v**2/2  +  v**3 P(v) / Q(v)
375  */
376 z = x*x;
377 w = x * ( z * __polevll( x, P, 3 ) / __p1evll( x, Q, 3 ) );
378 w = w - ldexpl( z, -1 );   /*  w - 0.5 * z  */
379 
380 /* Convert to base 2 logarithm:
381  * multiply by log2(e) = 1 + LOG2EA
382  */
383 z = LOG2EA * w;
384 z += w;
385 z += LOG2EA * x;
386 z += x;
387 
388 /* Compute exponent term of the base 2 logarithm. */
389 w = -i;
390 w = ldexpl( w, -LNXT );	/* divide by NXT */
391 w += e;
392 /* Now base 2 log of x is w + z. */
393 
394 /* Multiply base 2 log by y, in extended precision. */
395 
396 /* separate y into large part ya
397  * and small part yb less than 1/NXT
398  */
399 ya = reducl(y);
400 yb = y - ya;
401 
402 /* (w+z)(ya+yb)
403  * = w*ya + w*yb + z*y
404  */
405 F = z * y  +  w * yb;
406 Fa = reducl(F);
407 Fb = F - Fa;
408 
409 G = Fa + w * ya;
410 Ga = reducl(G);
411 Gb = G - Ga;
412 
413 H = Fb + Gb;
414 Ha = reducl(H);
415 w = ldexpl( Ga+Ha, LNXT );
416 
417 /* Test the power of 2 for overflow */
418 if( w > MEXP )
419 	return __math_oflowl(yoddint);		/* overflow */
420 
421 if( w < MNEXP )
422 	return __math_uflowl(yoddint);	        /* underflow */
423 
424 e = w;
425 Hb = H - Ha;
426 
427 if( Hb > 0.0L )
428 	{
429 	e += 1;
430 	Hb -= (1.0L/NXT);  /*0.0625L;*/
431 	}
432 
433 /* Now the product y * log2(x)  =  Hb + e/NXT.
434  *
435  * Compute base 2 exponential of Hb,
436  * where -0.0625 <= Hb <= 0.
437  */
438 z = Hb * __polevll( Hb, R, 6 );  /*    z  =  2**Hb - 1    */
439 
440 /* Express e/NXT as an integer plus a negative number of (1/NXT)ths.
441  * Find lookup table entry for the fractional power of 2.
442  */
443 if( e < 0 )
444 	i = 0;
445 else
446 	i = 1;
447 i = e/NXT + i;
448 e = NXT*i - e;
449 w = douba( e );
450 z = w * z;      /*    2**-e * ( 1 + (2**Hb-1) )    */
451 z = z + w;
452 z = ldexpl( z, i );  /* multiply by integer power of 2 */
453 
454 if( nflg )
455 	{
456 /* For negative x,
457  * find out if the integer exponent
458  * is odd or even.
459  */
460 	w = ldexpl( y, -1 );
461 	w = floorl(w);
462 	w = ldexpl( w, 1 );
463 	if( w != y )
464 		z = -z; /* odd exponent */
465 	}
466 
467 return( z );
468 }
469 
470 
471 /* Find a multiple of 1/NXT that is within 1/NXT of x. */
472 static long double
reducl(long double x)473 reducl(long double x)
474 {
475 long double t = x;
476 
477 if (t < LDBL_MAX / NXT) {
478         t = ldexpl( t, LNXT );
479         t = floorl( t );
480         t = ldexpl( t, -LNXT );
481 }
482 return(t);
483 }
484 
485 /*							powil.c
486  *
487  *	Real raised to integer power, long double precision
488  *
489  *
490  *
491  * SYNOPSIS:
492  *
493  * long double x, y, powil();
494  * int n;
495  *
496  * y = powil( x, n );
497  *
498  *
499  *
500  * DESCRIPTION:
501  *
502  * Returns argument x raised to the nth power.
503  * The routine efficiently decomposes n as a sum of powers of
504  * two. The desired power is a product of two-to-the-kth
505  * powers of x.  Thus to compute the 32767 power of x requires
506  * 28 multiplications instead of 32767 multiplications.
507  *
508  *
509  *
510  * ACCURACY:
511  *
512  *
513  *                      Relative error:
514  * arithmetic   x domain   n domain  # trials      peak         rms
515  *    IEEE     .001,1000  -1022,1023  50000       4.3e-17     7.8e-18
516  *    IEEE        1,2     -1022,1023  20000       3.9e-17     7.6e-18
517  *    IEEE     .99,1.01     0,8700    10000       3.6e-16     7.2e-17
518  *
519  * Returns MAXNUM on overflow, zero on underflow.
520  *
521  */
522 
523 static long double
powil(long double x,int nn)524 powil(long double x, int nn)
525 {
526 long double ww, y;
527 long double s;
528 int n, e, sign, asign, lx;
529 
530 if( x == 0.0L )
531 	{
532 	if( nn == 0 )
533 		return( 1.0L );
534 	else if( nn < 0 )
535 		return( LDBL_MAX );
536 	else
537 		return( 0.0L );
538 	}
539 
540 if( nn == 0 )
541 	return( 1.0L );
542 
543 
544 if( x < 0.0L )
545 	{
546 	asign = -1;
547 	x = -x;
548 	}
549 else
550 	asign = 0;
551 
552 
553 if( nn < 0 )
554 	{
555 	sign = -1;
556 	n = -nn;
557 	}
558 else
559 	{
560 	sign = 1;
561 	n = nn;
562 	}
563 
564 /* Overflow detection */
565 
566 /* Calculate approximate logarithm of answer */
567 s = x;
568 s = frexpl( s, &lx );
569 e = (lx - 1)*n;
570 if( (e == 0) || (e > 64) || (e < -64) )
571 	{
572 	s = (s - 7.0710678118654752e-1L) / (s +  7.0710678118654752e-1L);
573 	s = (2.9142135623730950L * s - 0.5L + lx) * nn * LOGE2L;
574 	}
575 else
576 	{
577 	s = LOGE2L * e;
578 	}
579 
580 if( s > MAXLOGL )
581 	return __math_oflowl(asign < 0 && (nn & 1));		/* overflow */
582 
583 if( s < MINLOGL )
584 	return __math_uflowl(asign < 0 && (nn & 1));	/* underflow */
585 
586 /* Handle tiny denormal answer, but with less accuracy
587  * since roundoff error in 1.0/x will be amplified.
588  * The precise demarcation should be the gradual underflow threshold.
589  */
590 if( s < (-MAXLOGL+2.0L) )
591 	{
592 	x = 1.0L/x;
593 	sign = -sign;
594 	}
595 
596 /* First bit of the power */
597 if( n & 1 )
598 	y = x;
599 
600 else
601 	{
602 	y = 1.0L;
603 	asign = 0;
604 	}
605 
606 ww = x;
607 n >>= 1;
608 while( n )
609 	{
610 	ww = ww * ww;	/* arg to the 2-to-the-kth power */
611 	if( n & 1 )	/* if that bit is set, then include in product */
612 		y *= ww;
613 	n >>= 1;
614 	}
615 
616 if( asign )
617 	y = -y; /* odd power of negative number */
618 if( sign < 0 )
619 	y = 1.0L/y;
620 return(y);
621 }
622 
623 #if defined(_HAVE_ALIAS_ATTRIBUTE)
624 #ifndef __clang__
625 #pragma GCC diagnostic ignored "-Wmissing-attributes"
626 #endif
627 __strong_reference(powl, _powl);
628 #endif
629