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