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