1/*
2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001-2018 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 && (y != 0 || issignaling (x)))
78 return x + x;
79 if (y == 1.0)
80 return x;
81 if (y == 2.0)
82 return x * x;
83 if (y == -1.0)
84 return 1.0 / x;
85 if (y == 0)
86 return 1.0;
87 }
88 /* else */
89 if (((u.i[HIGH_HALF] > 0 && u.i[HIGH_HALF] < 0x7ff00000) || /* x>0 and not x->0 */
90 (u.i[HIGH_HALF] == 0 && u.i[LOW_HALF] != 0)) &&
91 /* 2^-1023< x<= 2^-1023 * 0x1.0000ffffffff */
92 (v.i[HIGH_HALF] & 0x7fffffff) < 0x4ff00000)
93 { /* if y<-1 or y>1 */
94 double retval;
95
96 {
97 SET_RESTORE_ROUND (FE_TONEAREST);
98
99 /* Avoid internal underflow for tiny y. The exact value of y does
100 not matter if |y| <= 2**-64. */
101 if (fabs (y) < 0x1p-64)
102 y = y < 0 ? -0x1p-64 : 0x1p-64;
103 z = log1 (x, &aa, &error); /* x^y =e^(y log (X)) */
104 t = y * CN;
105 y1 = t - (t - y);
106 y2 = y - y1;
107 t = z * CN;
108 a1 = t - (t - z);
109 a2 = (z - a1) + aa;
110 a = y1 * a1;
111 aa = y2 * a1 + y * a2;
112 a1 = a + aa;
113 a2 = (a - a1) + aa;
114 error = error * fabs (y);
115 t = __exp1 (a1, a2, 1.9e16 * error); /* return -10 or 0 if wasn't computed exactly */
116 retval = (t > 0) ? t : power1 (x, y);
117 }
118
119 if (isinf (retval))
120 retval = huge * huge;
121 else if (retval == 0)
122 retval = tiny * tiny;
123 else
124 math_check_force_underflow_nonneg (retval);
125 return retval;
126 }
127
128 if (x == 0)
129 {
130 if (((v.i[HIGH_HALF] & 0x7fffffff) == 0x7ff00000 && v.i[LOW_HALF] != 0)
131 || (v.i[HIGH_HALF] & 0x7fffffff) > 0x7ff00000) /* NaN */
132 return y + y;
133 if (fabs (y) > 1.0e20)
134 return (y > 0) ? 0 : 1.0 / 0.0;
135 k = checkint (y);
136 if (k == -1)
137 return y < 0 ? 1.0 / x : x;
138 else
139 return y < 0 ? 1.0 / 0.0 : 0.0; /* return 0 */
140 }
141
142 qx = u.i[HIGH_HALF] & 0x7fffffff; /* no sign */
143 qy = v.i[HIGH_HALF] & 0x7fffffff; /* no sign */
144
145 if (qx >= 0x7ff00000 && (qx > 0x7ff00000 || u.i[LOW_HALF] != 0)) /* NaN */
146 return x + y;
147 if (qy >= 0x7ff00000 && (qy > 0x7ff00000 || v.i[LOW_HALF] != 0)) /* NaN */
148 return x == 1.0 && !issignaling (y) ? 1.0 : y + y;
149
150 /* if x<0 */
151 if (u.i[HIGH_HALF] < 0)
152 {
153 k = checkint (y);
154 if (k == 0)
155 {
156 if (qy == 0x7ff00000)
157 {
158 if (x == -1.0)
159 return 1.0;
160 else if (x > -1.0)
161 return v.i[HIGH_HALF] < 0 ? INF.x : 0.0;
162 else
163 return v.i[HIGH_HALF] < 0 ? 0.0 : INF.x;
164 }
165 else if (qx == 0x7ff00000)
166 return y < 0 ? 0.0 : INF.x;
167 return (x - x) / (x - x); /* y not integer and x<0 */
168 }
169 else if (qx == 0x7ff00000)
170 {
171 if (k < 0)
172 return y < 0 ? nZERO.x : nINF.x;
173 else
174 return y < 0 ? 0.0 : INF.x;
175 }
176 /* if y even or odd */
177 if (k == 1)
178 return __ieee754_pow (-x, y);
179 else
180 {
181 double retval;
182 {
183 SET_RESTORE_ROUND (FE_TONEAREST);
184 retval = -__ieee754_pow (-x, y);
185 }
186 if (isinf (retval))
187 retval = -huge * huge;
188 else if (retval == 0)
189 retval = -tiny * tiny;
190 return retval;
191 }
192 }
193 /* x>0 */
194
195 if (qx == 0x7ff00000) /* x= 2^-0x3ff */
196 return y > 0 ? x : 0;
197
198 if (qy > 0x45f00000 && qy < 0x7ff00000)
199 {
200 if (x == 1.0)
201 return 1.0;
202 if (y > 0)
203 return (x > 1.0) ? huge * huge : tiny * tiny;
204 if (y < 0)
205 return (x < 1.0) ? huge * huge : tiny * tiny;
206 }
207
208 if (x == 1.0)
209 return 1.0;
210 if (y > 0)
211 return (x > 1.0) ? INF.x : 0;
212 if (y < 0)
213 return (x < 1.0) ? INF.x : 0;
214 return 0; /* unreachable, to make the compiler happy */
215}
216
217#ifndef __ieee754_pow
218strong_alias (__ieee754_pow, __pow_finite)
219#endif
220
221/* Compute x^y using more accurate but more slow log routine. */
222static double
223SECTION
224power1 (double x, double y)
225{
226 double z, a, aa, error, t, a1, a2, y1, y2;
227 z = my_log2 (x, &aa, &error);
228 t = y * CN;
229 y1 = t - (t - y);
230 y2 = y - y1;
231 t = z * CN;
232 a1 = t - (t - z);
233 a2 = z - a1;
234 a = y * z;
235 aa = ((y1 * a1 - a) + y1 * a2 + y2 * a1) + y2 * a2 + aa * y;
236 a1 = a + aa;
237 a2 = (a - a1) + aa;
238 error = error * fabs (y);
239 t = __exp1 (a1, a2, 1.9e16 * error);
240 return (t >= 0) ? t : __slowpow (x, y, z);
241}
242
243/* Compute log(x) (x is left argument). The result is the returned double + the
244 parameter DELTA. The result is bounded by ERROR. */
245static double
246SECTION
247log1 (double x, double *delta, double *error)
248{
249 unsigned int i, j;
250 int m;
251 double uu, vv, eps, nx, e, e1, e2, t, t1, t2, res, add = 0;
252 mynumber u, v;
253#ifdef BIG_ENDI
254 mynumber /**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
255#else
256# ifdef LITTLE_ENDI
257 mynumber /**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
258# endif
259#endif
260
261 u.x = x;
262 m = u.i[HIGH_HALF];
263 *error = 0;
264 *delta = 0;
265 if (m < 0x00100000) /* 1<x<2^-1007 */
266 {
267 x = x * t52.x;
268 add = -52.0;
269 u.x = x;
270 m = u.i[HIGH_HALF];
271 }
272
273 if ((m & 0x000fffff) < 0x0006a09e)
274 {
275 u.i[HIGH_HALF] = (m & 0x000fffff) | 0x3ff00000;
276 two52.i[LOW_HALF] = (m >> 20);
277 }
278 else
279 {
280 u.i[HIGH_HALF] = (m & 0x000fffff) | 0x3fe00000;
281 two52.i[LOW_HALF] = (m >> 20) + 1;
282 }
283
284 v.x = u.x + bigu.x;
285 uu = v.x - bigu.x;
286 i = (v.i[LOW_HALF] & 0x000003ff) << 2;
287 if (two52.i[LOW_HALF] == 1023) /* nx = 0 */
288 {
289 if (i > 1192 && i < 1208) /* |x-1| < 1.5*2**-10 */
290 {
291 t = x - 1.0;
292 t1 = (t + 5.0e6) - 5.0e6;
293 t2 = t - t1;
294 e1 = t - 0.5 * t1 * t1;
295 e2 = (t * t * t * (r3 + t * (r4 + t * (r5 + t * (r6 + t
296 * (r7 + t * r8)))))
297 - 0.5 * t2 * (t + t1));
298 res = e1 + e2;
299 *error = 1.0e-21 * fabs (t);
300 *delta = (e1 - res) + e2;
301 return res;
302 } /* |x-1| < 1.5*2**-10 */
303 else
304 {
305 v.x = u.x * (ui.x[i] + ui.x[i + 1]) + bigv.x;
306 vv = v.x - bigv.x;
307 j = v.i[LOW_HALF] & 0x0007ffff;
308 j = j + j + j;
309 eps = u.x - uu * vv;
310 e1 = eps * ui.x[i];
311 e2 = eps * (ui.x[i + 1] + vj.x[j] * (ui.x[i] + ui.x[i + 1]));
312 e = e1 + e2;
313 e2 = ((e1 - e) + e2);
314 t = ui.x[i + 2] + vj.x[j + 1];
315 t1 = t + e;
316 t2 = ((((t - t1) + e) + (ui.x[i + 3] + vj.x[j + 2])) + e2 + e * e
317 * (p2 + e * (p3 + e * p4)));
318 res = t1 + t2;
319 *error = 1.0e-24;
320 *delta = (t1 - res) + t2;
321 return res;
322 }
323 } /* nx = 0 */
324 else /* nx != 0 */
325 {
326 eps = u.x - uu;
327 nx = (two52.x - two52e.x) + add;
328 e1 = eps * ui.x[i];
329 e2 = eps * ui.x[i + 1];
330 e = e1 + e2;
331 e2 = (e1 - e) + e2;
332 t = nx * ln2a.x + ui.x[i + 2];
333 t1 = t + e;
334 t2 = ((((t - t1) + e) + nx * ln2b.x + ui.x[i + 3] + e2) + e * e
335 * (q2 + e * (q3 + e * (q4 + e * (q5 + e * q6)))));
336 res = t1 + t2;
337 *error = 1.0e-21;
338 *delta = (t1 - res) + t2;
339 return res;
340 } /* nx != 0 */
341}
342
343/* Slower but more accurate routine of log. The returned result is double +
344 DELTA. The result is bounded by ERROR. */
345static double
346SECTION
347my_log2 (double x, double *delta, double *error)
348{
349 unsigned int i, j;
350 int m;
351 double uu, vv, eps, nx, e, e1, e2, t, t1, t2, res, add = 0;
352 double ou1, ou2, lu1, lu2, ov, lv1, lv2, a, a1, a2;
353 double y, yy, z, zz, j1, j2, j7, j8;
354#ifndef DLA_FMS
355 double j3, j4, j5, j6;
356#endif
357 mynumber u, v;
358#ifdef BIG_ENDI
359 mynumber /**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
360#else
361# ifdef LITTLE_ENDI
362 mynumber /**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
363# endif
364#endif
365
366 u.x = x;
367 m = u.i[HIGH_HALF];
368 *error = 0;
369 *delta = 0;
370 add = 0;
371 if (m < 0x00100000)
372 { /* x < 2^-1022 */
373 x = x * t52.x;
374 add = -52.0;
375 u.x = x;
376 m = u.i[HIGH_HALF];
377 }
378
379 if ((m & 0x000fffff) < 0x0006a09e)
380 {
381 u.i[HIGH_HALF] = (m & 0x000fffff) | 0x3ff00000;
382 two52.i[LOW_HALF] = (m >> 20);
383 }
384 else
385 {
386 u.i[HIGH_HALF] = (m & 0x000fffff) | 0x3fe00000;
387 two52.i[LOW_HALF] = (m >> 20) + 1;
388 }
389
390 v.x = u.x + bigu.x;
391 uu = v.x - bigu.x;
392 i = (v.i[LOW_HALF] & 0x000003ff) << 2;
393 /*------------------------------------- |x-1| < 2**-11------------------------------- */
394 if ((two52.i[LOW_HALF] == 1023) && (i == 1200))
395 {
396 t = x - 1.0;
397 EMULV (t, s3, y, yy, j1, j2, j3, j4, j5);
398 ADD2 (-0.5, 0, y, yy, z, zz, j1, j2);
399 MUL2 (t, 0, z, zz, y, yy, j1, j2, j3, j4, j5, j6, j7, j8);
400 MUL2 (t, 0, y, yy, z, zz, j1, j2, j3, j4, j5, j6, j7, j8);
401
402 e1 = t + z;
403 e2 = ((((t - e1) + z) + zz) + t * t * t
404 * (ss3 + t * (s4 + t * (s5 + t * (s6 + t * (s7 + t * s8))))));
405 res = e1 + e2;
406 *error = 1.0e-25 * fabs (t);
407 *delta = (e1 - res) + e2;
408 return res;
409 }
410 /*----------------------------- |x-1| > 2**-11 -------------------------- */
411 else
412 { /*Computing log(x) according to log table */
413 nx = (two52.x - two52e.x) + add;
414 ou1 = ui.x[i];
415 ou2 = ui.x[i + 1];
416 lu1 = ui.x[i + 2];
417 lu2 = ui.x[i + 3];
418 v.x = u.x * (ou1 + ou2) + bigv.x;
419 vv = v.x - bigv.x;
420 j = v.i[LOW_HALF] & 0x0007ffff;
421 j = j + j + j;
422 eps = u.x - uu * vv;
423 ov = vj.x[j];
424 lv1 = vj.x[j + 1];
425 lv2 = vj.x[j + 2];
426 a = (ou1 + ou2) * (1.0 + ov);
427 a1 = (a + 1.0e10) - 1.0e10;
428 a2 = a * (1.0 - a1 * uu * vv);
429 e1 = eps * a1;
430 e2 = eps * a2;
431 e = e1 + e2;
432 e2 = (e1 - e) + e2;
433 t = nx * ln2a.x + lu1 + lv1;
434 t1 = t + e;
435 t2 = ((((t - t1) + e) + (lu2 + lv2 + nx * ln2b.x + e2)) + e * e
436 * (p2 + e * (p3 + e * p4)));
437 res = t1 + t2;
438 *error = 1.0e-27;
439 *delta = (t1 - res) + t2;
440 return res;
441 }
442}
443
444/* This function receives a double x and checks if it is an integer. If not,
445 it returns 0, else it returns 1 if even or -1 if odd. */
446static int
447SECTION
448checkint (double x)
449{
450 union
451 {
452 int4 i[2];
453 double x;
454 } u;
455 int k;
456 unsigned int m, n;
457 u.x = x;
458 m = u.i[HIGH_HALF] & 0x7fffffff; /* no sign */
459 if (m >= 0x7ff00000)
460 return 0; /* x is +/-inf or NaN */
461 if (m >= 0x43400000)
462 return 1; /* |x| >= 2**53 */
463 if (m < 0x40000000)
464 return 0; /* |x| < 2, can not be 0 or 1 */
465 n = u.i[LOW_HALF];
466 k = (m >> 20) - 1023; /* 1 <= k <= 52 */
467 if (k == 52)
468 return (n & 1) ? -1 : 1; /* odd or even */
469 if (k > 20)
470 {
471 if (n << (k - 20) != 0)
472 return 0; /* if not integer */
473 return (n << (k - 21) != 0) ? -1 : 1;
474 }
475 if (n)
476 return 0; /*if not integer */
477 if (k == 20)
478 return (m & 1) ? -1 : 1;
479 if (m << (k + 12) != 0)
480 return 0;
481 return (m << (k + 11) != 0) ? -1 : 1;
482}
483