2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001-2012 Free Software Foundation
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>
51 double __exp1(double x, double xx, double error);
52 static double log1(double x, double *delta, double *error);
53 static double my_log2(double x, double *delta, double *error);
54 double __slowpow(double x, double y,double z);
55 static double power1(double x, double y);
56 static int checkint(double x);
58 /***************************************************************************/
59 /* An ultimate power routine. Given two IEEE double machine numbers y,x */
60 /* it computes the correctly rounded (to nearest) value of X^y. */
61 /***************************************************************************/
64 __ieee754_pow(double x, double y) {
65 double z,a,aa,error, t,a1,a2,y1,y2;
74 if (v.i[LOW_HALF] == 0) { /* of y */
75 qx = u.i[HIGH_HALF]&0x7fffffff;
76 /* Checking if x is not too small to compute */
77 if (((qx==0x7ff00000)&&(u.i[LOW_HALF]!=0))||(qx>0x7ff00000)) return NaNQ.x;
78 if (y == 1.0) return x;
79 if (y == 2.0) return x*x;
80 if (y == -1.0) return 1.0/x;
81 if (y == 0) return 1.0;
84 if(((u.i[HIGH_HALF]>0 && u.i[HIGH_HALF]<0x7ff00000)|| /* x>0 and not x->0 */
85 (u.i[HIGH_HALF]==0 && u.i[LOW_HALF]!=0)) &&
86 /* 2^-1023< x<= 2^-1023 * 0x1.0000ffffffff */
87 (v.i[HIGH_HALF]&0x7fffffff) < 0x4ff00000) { /* if y<-1 or y>1 */
90 SET_RESTORE_ROUND (FE_TONEAREST);
92 z = log1(x,&aa,&error); /* x^y =e^(y log (X)) */
103 error = error*ABS(y);
104 t = __exp1(a1,a2,1.9e16*error); /* return -10 or 0 if wasn't computed exactly */
105 retval = (t>0)?t:power1(x,y);
111 if (((v.i[HIGH_HALF] & 0x7fffffff) == 0x7ff00000 && v.i[LOW_HALF] != 0)
112 || (v.i[HIGH_HALF] & 0x7fffffff) > 0x7ff00000)
114 if (ABS(y) > 1.0e20) return (y>0)?0:INF.x;
117 return y < 0 ? 1.0/x : x;
119 return y < 0 ? 1.0/ABS(x) : 0.0; /* return 0 */
122 qx = u.i[HIGH_HALF]&0x7fffffff; /* no sign */
123 qy = v.i[HIGH_HALF]&0x7fffffff; /* no sign */
125 if (qx >= 0x7ff00000 && (qx > 0x7ff00000 || u.i[LOW_HALF] != 0)) return NaNQ.x;
126 if (qy >= 0x7ff00000 && (qy > 0x7ff00000 || v.i[LOW_HALF] != 0))
127 return x == 1.0 ? 1.0 : NaNQ.x;
130 if (u.i[HIGH_HALF] < 0) {
133 if (qy == 0x7ff00000) {
134 if (x == -1.0) return 1.0;
135 else if (x > -1.0) return v.i[HIGH_HALF] < 0 ? INF.x : 0.0;
136 else return v.i[HIGH_HALF] < 0 ? 0.0 : INF.x;
138 else if (qx == 0x7ff00000)
139 return y < 0 ? 0.0 : INF.x;
140 return NaNQ.x; /* y not integer and x<0 */
142 else if (qx == 0x7ff00000)
145 return y < 0 ? nZERO.x : nINF.x;
147 return y < 0 ? 0.0 : INF.x;
149 return (k==1)?__ieee754_pow(-x,y):-__ieee754_pow(-x,y); /* if y even or odd */
153 if (qx == 0x7ff00000) /* x= 2^-0x3ff */
154 {if (y == 0) return NaNQ.x;
157 if (qy > 0x45f00000 && qy < 0x7ff00000) {
158 if (x == 1.0) return 1.0;
159 if (y>0) return (x>1.0)?INF.x:0;
160 if (y<0) return (x<1.0)?INF.x:0;
163 if (x == 1.0) return 1.0;
164 if (y>0) return (x>1.0)?INF.x:0;
165 if (y<0) return (x<1.0)?INF.x:0;
166 return 0; /* unreachable, to make the compiler happy */
168 #ifndef __ieee754_pow
169 strong_alias (__ieee754_pow, __pow_finite)
172 /**************************************************************************/
173 /* Computing x^y using more accurate but more slow log routine */
174 /**************************************************************************/
177 power1(double x, double y) {
178 double z,a,aa,error, t,a1,a2,y1,y2;
179 z = my_log2(x,&aa,&error);
187 aa = ((y1*a1-a)+y1*a2+y2*a1)+y2*a2+aa*y;
190 error = error*ABS(y);
191 t = __exp1(a1,a2,1.9e16*error);
192 return (t >= 0)?t:__slowpow(x,y,z);
195 /****************************************************************************/
196 /* Computing log(x) (x is left argument). The result is the returned double */
197 /* + the parameter delta. */
198 /* The result is bounded by error (rightmost argument) */
199 /****************************************************************************/
202 log1(double x, double *delta, double *error) {
207 double uu,vv,eps,nx,e,e1,e2,t,t1,t2,res,add=0;
214 /**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
218 /**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
226 if (m < 0x00100000) /* 1<x<2^-1007 */
227 { x = x*t52.x; add = -52.0; u.x = x; m = u.i[HIGH_HALF];}
229 if ((m&0x000fffff) < 0x0006a09e)
230 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3ff00000; two52.i[LOW_HALF]=(m>>20); }
232 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3fe00000; two52.i[LOW_HALF]=(m>>20)+1; }
236 i = (v.i[LOW_HALF]&0x000003ff)<<2;
237 if (two52.i[LOW_HALF] == 1023) /* nx = 0 */
239 if (i > 1192 && i < 1208) /* |x-1| < 1.5*2**-10 */
242 t1 = (t+5.0e6)-5.0e6;
245 e2 = t*t*t*(r3+t*(r4+t*(r5+t*(r6+t*(r7+t*r8)))))-0.5*t2*(t+t1);
247 *error = 1.0e-21*ABS(t);
248 *delta = (e1-res)+e2;
250 } /* |x-1| < 1.5*2**-10 */
253 v.x = u.x*(ui.x[i]+ui.x[i+1])+bigv.x;
255 j = v.i[LOW_HALF]&0x0007ffff;
259 e2 = eps*(ui.x[i+1]+vj.x[j]*(ui.x[i]+ui.x[i+1]));
262 t=ui.x[i+2]+vj.x[j+1];
264 t2 = (((t-t1)+e)+(ui.x[i+3]+vj.x[j+2]))+e2+e*e*(p2+e*(p3+e*p4));
267 *delta = (t1-res)+t2;
274 nx = (two52.x - two52e.x)+add;
279 t=nx*ln2a.x+ui.x[i+2];
281 t2=(((t-t1)+e)+nx*ln2b.x+ui.x[i+3]+e2)+e*e*(q2+e*(q3+e*(q4+e*(q5+e*q6))));
284 *delta = (t1-res)+t2;
289 /****************************************************************************/
290 /* More slow but more accurate routine of log */
291 /* Computing log(x)(x is left argument).The result is return double + delta.*/
292 /* The result is bounded by error (right argument) */
293 /****************************************************************************/
296 my_log2(double x, double *delta, double *error) {
301 double uu,vv,eps,nx,e,e1,e2,t,t1,t2,res,add=0;
305 double ou1,ou2,lu1,lu2,ov,lv1,lv2,a,a1,a2;
306 double y,yy,z,zz,j1,j2,j7,j8;
313 /**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
317 /**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
326 if (m<0x00100000) { /* x < 2^-1022 */
327 x = x*t52.x; add = -52.0; u.x = x; m = u.i[HIGH_HALF]; }
329 if ((m&0x000fffff) < 0x0006a09e)
330 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3ff00000; two52.i[LOW_HALF]=(m>>20); }
332 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3fe00000; two52.i[LOW_HALF]=(m>>20)+1; }
336 i = (v.i[LOW_HALF]&0x000003ff)<<2;
337 /*------------------------------------- |x-1| < 2**-11------------------------------- */
338 if ((two52.i[LOW_HALF] == 1023) && (i == 1200))
341 EMULV(t,s3,y,yy,j1,j2,j3,j4,j5);
342 ADD2(-0.5,0,y,yy,z,zz,j1,j2);
343 MUL2(t,0,z,zz,y,yy,j1,j2,j3,j4,j5,j6,j7,j8);
344 MUL2(t,0,y,yy,z,zz,j1,j2,j3,j4,j5,j6,j7,j8);
347 e2 = (((t-e1)+z)+zz)+t*t*t*(ss3+t*(s4+t*(s5+t*(s6+t*(s7+t*s8)))));
349 *error = 1.0e-25*ABS(t);
350 *delta = (e1-res)+e2;
353 /*----------------------------- |x-1| > 2**-11 -------------------------- */
355 { /*Computing log(x) according to log table */
356 nx = (two52.x - two52e.x)+add;
361 v.x = u.x*(ou1+ou2)+bigv.x;
363 j = v.i[LOW_HALF]&0x0007ffff;
369 a = (ou1+ou2)*(1.0+ov);
370 a1 = (a+1.0e10)-1.0e10;
371 a2 = a*(1.0-a1*uu*vv);
378 t2 = (((t-t1)+e)+(lu2+lv2+nx*ln2b.x+e2))+e*e*(p2+e*(p3+e*p4));
381 *delta = (t1-res)+t2;
386 /**********************************************************************/
387 /* Routine receives a double x and checks if it is an integer. If not */
388 /* it returns 0, else it returns 1 if even or -1 if odd. */
389 /**********************************************************************/
393 union {int4 i[2]; double x;} u;
399 m = u.i[HIGH_HALF]&0x7fffffff; /* no sign */
400 if (m >= 0x7ff00000) return 0; /* x is +/-inf or NaN */
401 if (m >= 0x43400000) return 1; /* |x| >= 2**53 */
402 if (m < 0x40000000) return 0; /* |x| < 2, can not be 0 or 1 */
404 k = (m>>20)-1023; /* 1 <= k <= 52 */
405 if (k == 52) return (n&1)? -1:1; /* odd or even*/
407 if (n<<(k-20)) return 0; /* if not integer */
408 return (n<<(k-21))?-1:1;
410 if (n) return 0; /*if not integer*/
411 if (k == 20) return (m&1)? -1:1;
412 if (m<<(k+12)) return 0;
413 return (m<<(k+11))?-1:1;