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