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