2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001-2013 Free Software Foundation, Inc.
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.
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.
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/>.
19 /***************************************************************************/
20 /* MODULE_NAME: upow.c */
27 /* FILES NEEDED: dla.h endian.h mpa.h mydefs.h */
28 /* halfulp.c mpexp.c mplog.c slowexp.c slowpow.c mpa.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. */
36 /***************************************************************************/
43 #include <math_private.h>
50 static const double huge = 1.0e300, tiny = 1.0e-300;
52 double __exp1(double x, double xx, double error);
53 static double log1(double x, double *delta, double *error);
54 static double my_log2(double x, double *delta, double *error);
55 double __slowpow(double x, double y,double z);
56 static double power1(double x, double y);
57 static int checkint(double x);
59 /***************************************************************************/
60 /* An ultimate power routine. Given two IEEE double machine numbers y,x */
61 /* it computes the correctly rounded (to nearest) value of X^y. */
62 /***************************************************************************/
65 __ieee754_pow(double x, double y) {
66 double z,a,aa,error, t,a1,a2,y1,y2;
75 if (v.i[LOW_HALF] == 0) { /* of y */
76 qx = u.i[HIGH_HALF]&0x7fffffff;
77 /* Checking if x is not too small to compute */
78 if (((qx==0x7ff00000)&&(u.i[LOW_HALF]!=0))||(qx>0x7ff00000)) return NaNQ.x;
79 if (y == 1.0) return x;
80 if (y == 2.0) return x*x;
81 if (y == -1.0) return 1.0/x;
82 if (y == 0) return 1.0;
85 if(((u.i[HIGH_HALF]>0 && u.i[HIGH_HALF]<0x7ff00000)|| /* x>0 and not x->0 */
86 (u.i[HIGH_HALF]==0 && u.i[LOW_HALF]!=0)) &&
87 /* 2^-1023< x<= 2^-1023 * 0x1.0000ffffffff */
88 (v.i[HIGH_HALF]&0x7fffffff) < 0x4ff00000) { /* if y<-1 or y>1 */
91 SET_RESTORE_ROUND (FE_TONEAREST);
93 /* Avoid internal underflow for tiny y. The exact value of y does
94 not matter if |y| <= 2**-64. */
95 if (ABS (y) < 0x1p-64)
96 y = y < 0 ? -0x1p-64 : 0x1p-64;
97 z = log1(x,&aa,&error); /* x^y =e^(y log (X)) */
108 error = error*ABS(y);
109 t = __exp1(a1,a2,1.9e16*error); /* return -10 or 0 if wasn't computed exactly */
110 retval = (t>0)?t:power1(x,y);
116 if (((v.i[HIGH_HALF] & 0x7fffffff) == 0x7ff00000 && v.i[LOW_HALF] != 0)
117 || (v.i[HIGH_HALF] & 0x7fffffff) > 0x7ff00000)
119 if (ABS(y) > 1.0e20) return (y>0)?0:1.0/0.0;
122 return y < 0 ? 1.0/x : x;
124 return y < 0 ? 1.0/0.0 : 0.0; /* return 0 */
127 qx = u.i[HIGH_HALF]&0x7fffffff; /* no sign */
128 qy = v.i[HIGH_HALF]&0x7fffffff; /* no sign */
130 if (qx >= 0x7ff00000 && (qx > 0x7ff00000 || u.i[LOW_HALF] != 0)) return NaNQ.x;
131 if (qy >= 0x7ff00000 && (qy > 0x7ff00000 || v.i[LOW_HALF] != 0))
132 return x == 1.0 ? 1.0 : NaNQ.x;
135 if (u.i[HIGH_HALF] < 0) {
138 if (qy == 0x7ff00000) {
139 if (x == -1.0) return 1.0;
140 else if (x > -1.0) return v.i[HIGH_HALF] < 0 ? INF.x : 0.0;
141 else return v.i[HIGH_HALF] < 0 ? 0.0 : INF.x;
143 else if (qx == 0x7ff00000)
144 return y < 0 ? 0.0 : INF.x;
145 return NaNQ.x; /* y not integer and x<0 */
147 else if (qx == 0x7ff00000)
150 return y < 0 ? nZERO.x : nINF.x;
152 return y < 0 ? 0.0 : INF.x;
154 return (k==1)?__ieee754_pow(-x,y):-__ieee754_pow(-x,y); /* if y even or odd */
158 if (qx == 0x7ff00000) /* x= 2^-0x3ff */
159 {if (y == 0) return NaNQ.x;
162 if (qy > 0x45f00000 && qy < 0x7ff00000) {
163 if (x == 1.0) return 1.0;
164 if (y>0) return (x>1.0)?huge*huge:tiny*tiny;
165 if (y<0) return (x<1.0)?huge*huge:tiny*tiny;
168 if (x == 1.0) return 1.0;
169 if (y>0) return (x>1.0)?INF.x:0;
170 if (y<0) return (x<1.0)?INF.x:0;
171 return 0; /* unreachable, to make the compiler happy */
173 #ifndef __ieee754_pow
174 strong_alias (__ieee754_pow, __pow_finite)
177 /**************************************************************************/
178 /* Computing x^y using more accurate but more slow log routine */
179 /**************************************************************************/
182 power1(double x, double y) {
183 double z,a,aa,error, t,a1,a2,y1,y2;
184 z = my_log2(x,&aa,&error);
192 aa = ((y1*a1-a)+y1*a2+y2*a1)+y2*a2+aa*y;
195 error = error*ABS(y);
196 t = __exp1(a1,a2,1.9e16*error);
197 return (t >= 0)?t:__slowpow(x,y,z);
200 /****************************************************************************/
201 /* Computing log(x) (x is left argument). The result is the returned double */
202 /* + the parameter delta. */
203 /* The result is bounded by error (rightmost argument) */
204 /****************************************************************************/
207 log1(double x, double *delta, double *error) {
212 double uu,vv,eps,nx,e,e1,e2,t,t1,t2,res,add=0;
219 /**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
223 /**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
231 if (m < 0x00100000) /* 1<x<2^-1007 */
232 { x = x*t52.x; add = -52.0; u.x = x; m = u.i[HIGH_HALF];}
234 if ((m&0x000fffff) < 0x0006a09e)
235 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3ff00000; two52.i[LOW_HALF]=(m>>20); }
237 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3fe00000; two52.i[LOW_HALF]=(m>>20)+1; }
241 i = (v.i[LOW_HALF]&0x000003ff)<<2;
242 if (two52.i[LOW_HALF] == 1023) /* nx = 0 */
244 if (i > 1192 && i < 1208) /* |x-1| < 1.5*2**-10 */
247 t1 = (t+5.0e6)-5.0e6;
250 e2 = t*t*t*(r3+t*(r4+t*(r5+t*(r6+t*(r7+t*r8)))))-0.5*t2*(t+t1);
252 *error = 1.0e-21*ABS(t);
253 *delta = (e1-res)+e2;
255 } /* |x-1| < 1.5*2**-10 */
258 v.x = u.x*(ui.x[i]+ui.x[i+1])+bigv.x;
260 j = v.i[LOW_HALF]&0x0007ffff;
264 e2 = eps*(ui.x[i+1]+vj.x[j]*(ui.x[i]+ui.x[i+1]));
267 t=ui.x[i+2]+vj.x[j+1];
269 t2 = (((t-t1)+e)+(ui.x[i+3]+vj.x[j+2]))+e2+e*e*(p2+e*(p3+e*p4));
272 *delta = (t1-res)+t2;
279 nx = (two52.x - two52e.x)+add;
284 t=nx*ln2a.x+ui.x[i+2];
286 t2=(((t-t1)+e)+nx*ln2b.x+ui.x[i+3]+e2)+e*e*(q2+e*(q3+e*(q4+e*(q5+e*q6))));
289 *delta = (t1-res)+t2;
294 /****************************************************************************/
295 /* More slow but more accurate routine of log */
296 /* Computing log(x)(x is left argument).The result is return double + delta.*/
297 /* The result is bounded by error (right argument) */
298 /****************************************************************************/
301 my_log2(double x, double *delta, double *error) {
306 double uu,vv,eps,nx,e,e1,e2,t,t1,t2,res,add=0;
310 double ou1,ou2,lu1,lu2,ov,lv1,lv2,a,a1,a2;
311 double y,yy,z,zz,j1,j2,j7,j8;
318 /**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
322 /**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
331 if (m<0x00100000) { /* x < 2^-1022 */
332 x = x*t52.x; add = -52.0; u.x = x; m = u.i[HIGH_HALF]; }
334 if ((m&0x000fffff) < 0x0006a09e)
335 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3ff00000; two52.i[LOW_HALF]=(m>>20); }
337 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3fe00000; two52.i[LOW_HALF]=(m>>20)+1; }
341 i = (v.i[LOW_HALF]&0x000003ff)<<2;
342 /*------------------------------------- |x-1| < 2**-11------------------------------- */
343 if ((two52.i[LOW_HALF] == 1023) && (i == 1200))
346 EMULV(t,s3,y,yy,j1,j2,j3,j4,j5);
347 ADD2(-0.5,0,y,yy,z,zz,j1,j2);
348 MUL2(t,0,z,zz,y,yy,j1,j2,j3,j4,j5,j6,j7,j8);
349 MUL2(t,0,y,yy,z,zz,j1,j2,j3,j4,j5,j6,j7,j8);
352 e2 = (((t-e1)+z)+zz)+t*t*t*(ss3+t*(s4+t*(s5+t*(s6+t*(s7+t*s8)))));
354 *error = 1.0e-25*ABS(t);
355 *delta = (e1-res)+e2;
358 /*----------------------------- |x-1| > 2**-11 -------------------------- */
360 { /*Computing log(x) according to log table */
361 nx = (two52.x - two52e.x)+add;
366 v.x = u.x*(ou1+ou2)+bigv.x;
368 j = v.i[LOW_HALF]&0x0007ffff;
374 a = (ou1+ou2)*(1.0+ov);
375 a1 = (a+1.0e10)-1.0e10;
376 a2 = a*(1.0-a1*uu*vv);
383 t2 = (((t-t1)+e)+(lu2+lv2+nx*ln2b.x+e2))+e*e*(p2+e*(p3+e*p4));
386 *delta = (t1-res)+t2;
391 /**********************************************************************/
392 /* Routine receives a double x and checks if it is an integer. If not */
393 /* it returns 0, else it returns 1 if even or -1 if odd. */
394 /**********************************************************************/
398 union {int4 i[2]; double x;} u;
404 m = u.i[HIGH_HALF]&0x7fffffff; /* no sign */
405 if (m >= 0x7ff00000) return 0; /* x is +/-inf or NaN */
406 if (m >= 0x43400000) return 1; /* |x| >= 2**53 */
407 if (m < 0x40000000) return 0; /* |x| < 2, can not be 0 or 1 */
409 k = (m>>20)-1023; /* 1 <= k <= 52 */
410 if (k == 52) return (n&1)? -1:1; /* odd or even*/
412 if (n<<(k-20)) return 0; /* if not integer */
413 return (n<<(k-21))?-1:1;
415 if (n) return 0; /*if not integer*/
416 if (k == 20) return (m&1)? -1:1;
417 if (m<<(k+12)) return 0;
418 return (m<<(k+11))?-1:1;