1/*
2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001-2016 Free Software Foundation, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19/***************************************************************************/
20/* MODULE_NAME: upow.c */
21/* */
22/* FUNCTIONS: upow */
23/* power1 */
24/* my_log2 */
25/* log1 */
26/* checkint */
27/* FILES NEEDED: dla.h endian.h mpa.h mydefs.h */
28/* halfulp.c mpexp.c mplog.c slowexp.c slowpow.c mpa.c */
29/* uexp.c upow.c */
30/* root.tbl uexp.tbl upow.tbl */
31/* An ultimate power routine. Given two IEEE double machine numbers y,x */
32/* it computes the correctly rounded (to nearest) value of x^y. */
33/* Assumption: Machine arithmetic operations are performed in */
34/* round to nearest mode of IEEE 754 standard. */
35/* */
36/***************************************************************************/
37#include <math.h>
38#include "endian.h"
39#include "upow.h"
40#include <dla.h>
41#include "mydefs.h"
42#include "MathLib.h"
43#include "upow.tbl"
44#include <math_private.h>
45#include <fenv.h>
46
47#ifndef SECTION
48# define SECTION
49#endif
50
51static const double huge = 1.0e300, tiny = 1.0e-300;
52
53double __exp1 (double x, double xx, double error);
54static double log1 (double x, double *delta, double *error);
55static double my_log2 (double x, double *delta, double *error);
56double __slowpow (double x, double y, double z);
57static double power1 (double x, double y);
58static int checkint (double x);
59
60/* An ultimate power routine. Given two IEEE double machine numbers y, x it
61 computes the correctly rounded (to nearest) value of X^y. */
62double
63SECTION
64__ieee754_pow (double x, double y)
65{
66 double z, a, aa, error, t, a1, a2, y1, y2;
67 mynumber u, v;
68 int k;
69 int4 qx, qy;
70 v.x = y;
71 u.x = x;
72 if (v.i[LOW_HALF] == 0)
73 { /* of y */
74 qx = u.i[HIGH_HALF] & 0x7fffffff;
75 /* Is x a NaN? */
76 if (((qx == 0x7ff00000) && (u.i[LOW_HALF] != 0)) || (qx > 0x7ff00000))
77 return x;
78 if (y == 1.0)
79 return x;
80 if (y == 2.0)
81 return x * x;
82 if (y == -1.0)
83 return 1.0 / x;
84 if (y == 0)
85 return 1.0;
86 }
87 /* else */
88 if (((u.i[HIGH_HALF] > 0 && u.i[HIGH_HALF] < 0x7ff00000) || /* x>0 and not x->0 */
89 (u.i[HIGH_HALF] == 0 && u.i[LOW_HALF] != 0)) &&
90 /* 2^-1023< x<= 2^-1023 * 0x1.0000ffffffff */
91 (v.i[HIGH_HALF] & 0x7fffffff) < 0x4ff00000)
92 { /* if y<-1 or y>1 */
93 double retval;
94
95 {
96 SET_RESTORE_ROUND (FE_TONEAREST);
97
98 /* Avoid internal underflow for tiny y. The exact value of y does
99 not matter if |y| <= 2**-64. */
100 if (fabs (y) < 0x1p-64)
101 y = y < 0 ? -0x1p-64 : 0x1p-64;
102 z = log1 (x, &aa, &error); /* x^y =e^(y log (X)) */
103 t = y * CN;
104 y1 = t - (t - y);
105 y2 = y - y1;
106 t = z * CN;
107 a1 = t - (t - z);
108 a2 = (z - a1) + aa;
109 a = y1 * a1;
110 aa = y2 * a1 + y * a2;
111 a1 = a + aa;
112 a2 = (a - a1) + aa;
113 error = error * fabs (y);
114 t = __exp1 (a1, a2, 1.9e16 * error); /* return -10 or 0 if wasn't computed exactly */
115 retval = (t > 0) ? t : power1 (x, y);
116 }
117
118 if (isinf (retval))
119 retval = huge * huge;
120 else if (retval == 0)
121 retval = tiny * tiny;
122 else
123 math_check_force_underflow_nonneg (retval);
124 return retval;
125 }
126
127 if (x == 0)
128 {
129 if (((v.i[HIGH_HALF] & 0x7fffffff) == 0x7ff00000 && v.i[LOW_HALF] != 0)
130 || (v.i[HIGH_HALF] & 0x7fffffff) > 0x7ff00000) /* NaN */
131 return y;
132 if (fabs (y) > 1.0e20)
133 return (y > 0) ? 0 : 1.0 / 0.0;
134 k = checkint (y);
135 if (k == -1)
136 return y < 0 ? 1.0 / x : x;
137 else
138 return y < 0 ? 1.0 / 0.0 : 0.0; /* return 0 */
139 }
140
141 qx = u.i[HIGH_HALF] & 0x7fffffff; /* no sign */
142 qy = v.i[HIGH_HALF] & 0x7fffffff; /* no sign */
143
144 if (qx >= 0x7ff00000 && (qx > 0x7ff00000 || u.i[LOW_HALF] != 0)) /* NaN */
145 return x;
146 if (qy >= 0x7ff00000 && (qy > 0x7ff00000 || v.i[LOW_HALF] != 0)) /* NaN */
147 return x == 1.0 ? 1.0 : y;
148
149 /* if x<0 */
150 if (u.i[HIGH_HALF] < 0)
151 {
152 k = checkint (y);
153 if (k == 0)
154 {
155 if (qy == 0x7ff00000)
156 {
157 if (x == -1.0)
158 return 1.0;
159 else if (x > -1.0)
160 return v.i[HIGH_HALF] < 0 ? INF.x : 0.0;
161 else
162 return v.i[HIGH_HALF] < 0 ? 0.0 : INF.x;
163 }
164 else if (qx == 0x7ff00000)
165 return y < 0 ? 0.0 : INF.x;
166 return (x - x) / (x - x); /* y not integer and x<0 */
167 }
168 else if (qx == 0x7ff00000)
169 {
170 if (k < 0)
171 return y < 0 ? nZERO.x : nINF.x;
172 else
173 return y < 0 ? 0.0 : INF.x;
174 }
175 /* if y even or odd */
176 if (k == 1)
177 return __ieee754_pow (-x, y);
178 else
179 {
180 double retval;
181 {
182 SET_RESTORE_ROUND (FE_TONEAREST);
183 retval = -__ieee754_pow (-x, y);
184 }
185 if (isinf (retval))
186 retval = -huge * huge;
187 else if (retval == 0)
188 retval = -tiny * tiny;
189 return retval;
190 }
191 }
192 /* x>0 */
193
194 if (qx == 0x7ff00000) /* x= 2^-0x3ff */
195 return y > 0 ? x : 0;
196
197 if (qy > 0x45f00000 && qy < 0x7ff00000)
198 {
199 if (x == 1.0)
200 return 1.0;
201 if (y > 0)
202 return (x > 1.0) ? huge * huge : tiny * tiny;
203 if (y < 0)
204 return (x < 1.0) ? huge * huge : tiny * tiny;
205 }
206
207 if (x == 1.0)
208 return 1.0;
209 if (y > 0)
210 return (x > 1.0) ? INF.x : 0;
211 if (y < 0)
212 return (x < 1.0) ? INF.x : 0;
213 return 0; /* unreachable, to make the compiler happy */
214}
215
216#ifndef __ieee754_pow
217strong_alias (__ieee754_pow, __pow_finite)
218#endif
219
220/* Compute x^y using more accurate but more slow log routine. */
221static double
222SECTION
223power1 (double x, double y)
224{
225 double z, a, aa, error, t, a1, a2, y1, y2;
226 z = my_log2 (x, &aa, &error);
227 t = y * CN;
228 y1 = t - (t - y);
229 y2 = y - y1;
230 t = z * CN;
231 a1 = t - (t - z);
232 a2 = z - a1;
233 a = y * z;
234 aa = ((y1 * a1 - a) + y1 * a2 + y2 * a1) + y2 * a2 + aa * y;
235 a1 = a + aa;
236 a2 = (a - a1) + aa;
237 error = error * fabs (y);
238 t = __exp1 (a1, a2, 1.9e16 * error);
239 return (t >= 0) ? t : __slowpow (x, y, z);
240}
241
242/* Compute log(x) (x is left argument). The result is the returned double + the
243 parameter DELTA. The result is bounded by ERROR. */
244static double
245SECTION
246log1 (double x, double *delta, double *error)
247{
248 unsigned int i, j;
249 int m;
250 double uu, vv, eps, nx, e, e1, e2, t, t1, t2, res, add = 0;
251 mynumber u, v;
252#ifdef BIG_ENDI
253 mynumber /**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
254#else
255# ifdef LITTLE_ENDI
256 mynumber /**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
257# endif
258#endif
259
260 u.x = x;
261 m = u.i[HIGH_HALF];
262 *error = 0;
263 *delta = 0;
264 if (m < 0x00100000) /* 1<x<2^-1007 */
265 {
266 x = x * t52.x;
267 add = -52.0;
268 u.x = x;
269 m = u.i[HIGH_HALF];
270 }
271
272 if ((m & 0x000fffff) < 0x0006a09e)
273 {
274 u.i[HIGH_HALF] = (m & 0x000fffff) | 0x3ff00000;
275 two52.i[LOW_HALF] = (m >> 20);
276 }
277 else
278 {
279 u.i[HIGH_HALF] = (m & 0x000fffff) | 0x3fe00000;
280 two52.i[LOW_HALF] = (m >> 20) + 1;
281 }
282
283 v.x = u.x + bigu.x;
284 uu = v.x - bigu.x;
285 i = (v.i[LOW_HALF] & 0x000003ff) << 2;
286 if (two52.i[LOW_HALF] == 1023) /* nx = 0 */
287 {
288 if (i > 1192 && i < 1208) /* |x-1| < 1.5*2**-10 */
289 {
290 t = x - 1.0;
291 t1 = (t + 5.0e6) - 5.0e6;
292 t2 = t - t1;
293 e1 = t - 0.5 * t1 * t1;
294 e2 = (t * t * t * (r3 + t * (r4 + t * (r5 + t * (r6 + t
295 * (r7 + t * r8)))))
296 - 0.5 * t2 * (t + t1));
297 res = e1 + e2;
298 *error = 1.0e-21 * fabs (t);
299 *delta = (e1 - res) + e2;
300 return res;
301 } /* |x-1| < 1.5*2**-10 */
302 else
303 {
304 v.x = u.x * (ui.x[i] + ui.x[i + 1]) + bigv.x;
305 vv = v.x - bigv.x;
306 j = v.i[LOW_HALF] & 0x0007ffff;
307 j = j + j + j;
308 eps = u.x - uu * vv;
309 e1 = eps * ui.x[i];
310 e2 = eps * (ui.x[i + 1] + vj.x[j] * (ui.x[i] + ui.x[i + 1]));
311 e = e1 + e2;
312 e2 = ((e1 - e) + e2);
313 t = ui.x[i + 2] + vj.x[j + 1];
314 t1 = t + e;
315 t2 = ((((t - t1) + e) + (ui.x[i + 3] + vj.x[j + 2])) + e2 + e * e
316 * (p2 + e * (p3 + e * p4)));
317 res = t1 + t2;
318 *error = 1.0e-24;
319 *delta = (t1 - res) + t2;
320 return res;
321 }
322 } /* nx = 0 */
323 else /* nx != 0 */
324 {
325 eps = u.x - uu;
326 nx = (two52.x - two52e.x) + add;
327 e1 = eps * ui.x[i];
328 e2 = eps * ui.x[i + 1];
329 e = e1 + e2;
330 e2 = (e1 - e) + e2;
331 t = nx * ln2a.x + ui.x[i + 2];
332 t1 = t + e;
333 t2 = ((((t - t1) + e) + nx * ln2b.x + ui.x[i + 3] + e2) + e * e
334 * (q2 + e * (q3 + e * (q4 + e * (q5 + e * q6)))));
335 res = t1 + t2;
336 *error = 1.0e-21;
337 *delta = (t1 - res) + t2;
338 return res;
339 } /* nx != 0 */
340}
341
342/* Slower but more accurate routine of log. The returned result is double +
343 DELTA. The result is bounded by ERROR. */
344static double
345SECTION
346my_log2 (double x, double *delta, double *error)
347{
348 unsigned int i, j;
349 int m;
350 double uu, vv, eps, nx, e, e1, e2, t, t1, t2, res, add = 0;
351 double ou1, ou2, lu1, lu2, ov, lv1, lv2, a, a1, a2;
352 double y, yy, z, zz, j1, j2, j7, j8;
353#ifndef DLA_FMS
354 double j3, j4, j5, j6;
355#endif
356 mynumber u, v;
357#ifdef BIG_ENDI
358 mynumber /**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
359#else
360# ifdef LITTLE_ENDI
361 mynumber /**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
362# endif
363#endif
364
365 u.x = x;
366 m = u.i[HIGH_HALF];
367 *error = 0;
368 *delta = 0;
369 add = 0;
370 if (m < 0x00100000)
371 { /* x < 2^-1022 */
372 x = x * t52.x;
373 add = -52.0;
374 u.x = x;
375 m = u.i[HIGH_HALF];
376 }
377
378 if ((m & 0x000fffff) < 0x0006a09e)
379 {
380 u.i[HIGH_HALF] = (m & 0x000fffff) | 0x3ff00000;
381 two52.i[LOW_HALF] = (m >> 20);
382 }
383 else
384 {
385 u.i[HIGH_HALF] = (m & 0x000fffff) | 0x3fe00000;
386 two52.i[LOW_HALF] = (m >> 20) + 1;
387 }
388
389 v.x = u.x + bigu.x;
390 uu = v.x - bigu.x;
391 i = (v.i[LOW_HALF] & 0x000003ff) << 2;
392 /*------------------------------------- |x-1| < 2**-11------------------------------- */
393 if ((two52.i[LOW_HALF] == 1023) && (i == 1200))
394 {
395 t = x - 1.0;
396 EMULV (t, s3, y, yy, j1, j2, j3, j4, j5);
397 ADD2 (-0.5, 0, y, yy, z, zz, j1, j2);
398 MUL2 (t, 0, z, zz, y, yy, j1, j2, j3, j4, j5, j6, j7, j8);
399 MUL2 (t, 0, y, yy, z, zz, j1, j2, j3, j4, j5, j6, j7, j8);
400
401 e1 = t + z;
402 e2 = ((((t - e1) + z) + zz) + t * t * t
403 * (ss3 + t * (s4 + t * (s5 + t * (s6 + t * (s7 + t * s8))))));
404 res = e1 + e2;
405 *error = 1.0e-25 * fabs (t);
406 *delta = (e1 - res) + e2;
407 return res;
408 }
409 /*----------------------------- |x-1| > 2**-11 -------------------------- */
410 else
411 { /*Computing log(x) according to log table */
412 nx = (two52.x - two52e.x) + add;
413 ou1 = ui.x[i];
414 ou2 = ui.x[i + 1];
415 lu1 = ui.x[i + 2];
416 lu2 = ui.x[i + 3];
417 v.x = u.x * (ou1 + ou2) + bigv.x;
418 vv = v.x - bigv.x;
419 j = v.i[LOW_HALF] & 0x0007ffff;
420 j = j + j + j;
421 eps = u.x - uu * vv;
422 ov = vj.x[j];
423 lv1 = vj.x[j + 1];
424 lv2 = vj.x[j + 2];
425 a = (ou1 + ou2) * (1.0 + ov);
426 a1 = (a + 1.0e10) - 1.0e10;
427 a2 = a * (1.0 - a1 * uu * vv);
428 e1 = eps * a1;
429 e2 = eps * a2;
430 e = e1 + e2;
431 e2 = (e1 - e) + e2;
432 t = nx * ln2a.x + lu1 + lv1;
433 t1 = t + e;
434 t2 = ((((t - t1) + e) + (lu2 + lv2 + nx * ln2b.x + e2)) + e * e
435 * (p2 + e * (p3 + e * p4)));
436 res = t1 + t2;
437 *error = 1.0e-27;
438 *delta = (t1 - res) + t2;
439 return res;
440 }
441}
442
443/* This function receives a double x and checks if it is an integer. If not,
444 it returns 0, else it returns 1 if even or -1 if odd. */
445static int
446SECTION
447checkint (double x)
448{
449 union
450 {
451 int4 i[2];
452 double x;
453 } u;
454 int k, m, n;
455 u.x = x;
456 m = u.i[HIGH_HALF] & 0x7fffffff; /* no sign */
457 if (m >= 0x7ff00000)
458 return 0; /* x is +/-inf or NaN */
459 if (m >= 0x43400000)
460 return 1; /* |x| >= 2**53 */
461 if (m < 0x40000000)
462 return 0; /* |x| < 2, can not be 0 or 1 */
463 n = u.i[LOW_HALF];
464 k = (m >> 20) - 1023; /* 1 <= k <= 52 */
465 if (k == 52)
466 return (n & 1) ? -1 : 1; /* odd or even */
467 if (k > 20)
468 {
469 if (n << (k - 20) != 0)
470 return 0; /* if not integer */
471 return (n << (k - 21) != 0) ? -1 : 1;
472 }
473 if (n)
474 return 0; /*if not integer */
475 if (k == 20)
476 return (m & 1) ? -1 : 1;
477 if (m << (k + 12) != 0)
478 return 0;
479 return (m << (k + 11) != 0) ? -1 : 1;
480}
481