2 * IBM Accurate Mathematical Library
3 * Copyright (c) International Business Machines Corp., 2001
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 /***************************************************************************/
45 double __exp1(double x, double xx, double error);
46 static double log1(double x, double *delta, double *error);
47 static double log2(double x, double *delta, double *error);
48 double slowpow(double x, double y,double z);
49 static double power1(double x, double y);
50 static int checkint(double x);
52 /***************************************************************************/
53 /* An ultimate power routine. Given two IEEE double machine numbers y,x */
54 /* it computes the correctly rounded (to nearest) value of X^y. */
55 /***************************************************************************/
56 double __ieee754_upow(double x, double y) {
57 double z,a,aa,error, t,a1,a2,y1,y2;
66 if (v.i[LOW_HALF] == 0) { /* of y */
67 qx = u.i[HIGH_HALF]&0x7fffffff;
68 /* Checking if x is not too small to compute */
69 if (((qx==0x7ff00000)&&(u.i[LOW_HALF]!=0))||(qx>0x7ff00000)) return NaNQ.x;
70 if (y == 1.0) return x;
71 if (y == 2.0) return x*x;
72 if (y == -1.0) return (x!=0)?1.0/x:NaNQ.x;
73 if (y == 0) return ((x>0)&&(qx<0x7ff00000))?1.0:NaNQ.x;
76 if(((u.i[HIGH_HALF]>0 && u.i[HIGH_HALF]<0x7ff00000)|| /* x>0 and not x->0 */
77 (u.i[HIGH_HALF]==0 && u.i[LOW_HALF]!=0)) &&
78 /* 2^-1023< x<= 2^-1023 * 0x1.0000ffffffff */
79 (v.i[HIGH_HALF]&0x7fffffff) < 0x4ff00000) { /* if y<-1 or y>1 */
80 z = log1(x,&aa,&error); /* x^y =e^(y log (X)) */
92 t = __exp1(a1,a2,1.9e16*error); /* return -10 or 0 if wasn't computed exactly */
93 return (t>0)?t:power1(x,y);
97 if (ABS(y) > 1.0e20) return (y>0)?0:NaNQ.x;
99 if (k == 0 || y < 0) return NaNQ.x;
100 else return (k==1)?0:x; /* return 0 */
103 if (u.i[HIGH_HALF] < 0) {
105 if (k==0) return NaNQ.x; /* y not integer and x<0 */
106 return (k==1)?upow(-x,y):-upow(-x,y); /* if y even or odd */
109 qx = u.i[HIGH_HALF]&0x7fffffff; /* no sign */
110 qy = v.i[HIGH_HALF]&0x7fffffff; /* no sign */
112 if (qx > 0x7ff00000 || (qx == 0x7ff00000 && u.i[LOW_HALF] != 0)) return NaNQ.x;
113 /* if 0<x<2^-0x7fe */
114 if (qy > 0x7ff00000 || (qy == 0x7ff00000 && v.i[LOW_HALF] != 0)) return NaNQ.x;
117 if (qx == 0x7ff00000) /* x= 2^-0x3ff */
118 {if (y == 0) return NaNQ.x;
121 if (qy > 0x45f00000 && qy < 0x7ff00000) {
122 if (x == 1.0) return 1.0;
123 if (y>0) return (x>1.0)?INF.x:0;
124 if (y<0) return (x<1.0)?INF.x:0;
127 if (x == 1.0) return NaNQ.x;
128 if (y>0) return (x>1.0)?INF.x:0;
129 if (y<0) return (x<1.0)?INF.x:0;
130 return 0; /* unreachable, to make the compiler happy */
133 /**************************************************************************/
134 /* Computing x^y using more accurate but more slow log routine */
135 /**************************************************************************/
136 static double power1(double x, double y) {
137 double z,a,aa,error, t,a1,a2,y1,y2;
138 z = log2(x,&aa,&error);
146 aa = ((y1*a1-a)+y1*a2+y2*a1)+y2*a2+aa*y;
149 error = error*ABS(y);
150 t = __exp1(a1,a2,1.9e16*error);
151 return (t >= 0)?t:slowpow(x,y,z);
154 /****************************************************************************/
155 /* Computing log(x) (x is left argument). The result is the returned double */
156 /* + the parameter delta. */
157 /* The result is bounded by error (rightmost argument) */
158 /****************************************************************************/
159 static double log1(double x, double *delta, double *error) {
164 double uu,vv,eps,nx,e,e1,e2,t,t1,t2,res,add=0;
174 if (m < 0x00100000) /* 1<x<2^-1007 */
175 { x = x*t52.x; add = -52.0; u.x = x; m = u.i[HIGH_HALF];}
177 if ((m&0x000fffff) < 0x0006a09e)
178 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3ff00000; two52.i[LOW_HALF]=(m>>20); }
180 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3fe00000; two52.i[LOW_HALF]=(m>>20)+1; }
184 i = (v.i[LOW_HALF]&0x000003ff)<<2;
185 if (two52.i[LOW_HALF] == 1023) /* nx = 0 */
187 if (i > 1192 && i < 1208) /* |x-1| < 1.5*2**-10 */
190 t1 = (t+5.0e6)-5.0e6;
193 e2 = t*t*t*(r3+t*(r4+t*(r5+t*(r6+t*(r7+t*r8)))))-0.5*t2*(t+t1);
195 *error = 1.0e-21*ABS(t);
196 *delta = (e1-res)+e2;
198 } /* |x-1| < 1.5*2**-10 */
201 v.x = u.x*(ui.x[i]+ui.x[i+1])+bigv.x;
203 j = v.i[LOW_HALF]&0x0007ffff;
207 e2 = eps*(ui.x[i+1]+vj.x[j]*(ui.x[i]+ui.x[i+1]));
210 t=ui.x[i+2]+vj.x[j+1];
212 t2 = (((t-t1)+e)+(ui.x[i+3]+vj.x[j+2]))+e2+e*e*(p2+e*(p3+e*p4));
215 *delta = (t1-res)+t2;
222 nx = (two52.x - two52e.x)+add;
227 t=nx*ln2a.x+ui.x[i+2];
229 t2=(((t-t1)+e)+nx*ln2b.x+ui.x[i+3]+e2)+e*e*(q2+e*(q3+e*(q4+e*(q5+e*q6))));
232 *delta = (t1-res)+t2;
237 /****************************************************************************/
238 /* More slow but more accurate routine of log */
239 /* Computing log(x)(x is left argument).The result is return double + delta.*/
240 /* The result is bounded by error (right argument) */
241 /****************************************************************************/
242 static double log2(double x, double *delta, double *error) {
247 double uu,vv,eps,nx,e,e1,e2,t,t1,t2,res,add=0;
251 double ou1,ou2,lu1,lu2,ov,lv1,lv2,a,a1,a2;
252 double y,yy,z,zz,j1,j2,j3,j4,j5,j6,j7,j8;
260 if (m<0x00100000) { /* x < 2^-1022 */
261 x = x*t52.x; add = -52.0; u.x = x; m = u.i[HIGH_HALF]; }
263 if ((m&0x000fffff) < 0x0006a09e)
264 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3ff00000; two52.i[LOW_HALF]=(m>>20); }
266 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3fe00000; two52.i[LOW_HALF]=(m>>20)+1; }
270 i = (v.i[LOW_HALF]&0x000003ff)<<2;
271 /*------------------------------------- |x-1| < 2**-11------------------------------- */
272 if ((two52.i[LOW_HALF] == 1023) && (i == 1200))
275 EMULV(t,s3,y,yy,j1,j2,j3,j4,j5);
276 ADD2(-0.5,0,y,yy,z,zz,j1,j2);
277 MUL2(t,0,z,zz,y,yy,j1,j2,j3,j4,j5,j6,j7,j8);
278 MUL2(t,0,y,yy,z,zz,j1,j2,j3,j4,j5,j6,j7,j8);
281 e2 = (((t-e1)+z)+zz)+t*t*t*(ss3+t*(s4+t*(s5+t*(s6+t*(s7+t*s8)))));
283 *error = 1.0e-25*ABS(t);
284 *delta = (e1-res)+e2;
287 /*----------------------------- |x-1| > 2**-11 -------------------------- */
289 { /*Computing log(x) according to log table */
290 nx = (two52.x - two52e.x)+add;
295 v.x = u.x*(ou1+ou2)+bigv.x;
297 j = v.i[LOW_HALF]&0x0007ffff;
303 a = (ou1+ou2)*(1.0+ov);
304 a1 = (a+1.0e10)-1.0e10;
305 a2 = a*(1.0-a1*uu*vv);
312 t2 = (((t-t1)+e)+(lu2+lv2+nx*ln2b.x+e2))+e*e*(p2+e*(p3+e*p4));
315 *delta = (t1-res)+t2;
320 /**********************************************************************/
321 /* Routine receives a double x and checks if it is an integer. If not */
322 /* it returns 0, else it returns 1 if even or -1 if odd. */
323 /**********************************************************************/
324 static int checkint(double x) {
325 union {int4 i[2]; double x;} u;
331 m = u.i[HIGH_HALF]&0x7fffffff; /* no sign */
332 if (m >= 0x7ff00000) return 0; /* x is +/-inf or NaN */
333 if (m >= 0x43400000) return 1; /* |x| >= 2**53 */
334 if (m < 0x40000000) return 0; /* |x| < 2, can not be 0 or 1 */
336 k = (m>>20)-1023; /* 1 <= k <= 52 */
337 if (k == 52) return (n&1)? -1:1; /* odd or even*/
339 if (n<<(k-20)) return 0; /* if not integer */
340 return (n<<(k-21))?-1:1;
342 if (n) return 0; /*if not integer*/
343 if (k == 20) return (m&1)? -1:1;
344 if (m<<(k+12)) return 0;
345 return (m<<(k+11))?-1:1;