]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/ieee754/dbl-64/e_pow.c
Finish renamed DLA_FMA -> DLA_FMS
[thirdparty/glibc.git] / sysdeps / ieee754 / dbl-64 / e_pow.c
CommitLineData
f7eac6eb 1/*
e4d82761 2 * IBM Accurate Mathematical Library
aeb25823 3 * written by International Business Machines Corp.
0ac5ae23 4 * Copyright (C) 2001, 2002, 2004, 2011 Free Software Foundation
f7eac6eb 5 *
e4d82761
UD
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
cc7375ce 8 * the Free Software Foundation; either version 2.1 of the License, or
e4d82761 9 * (at your option) any later version.
f7eac6eb 10 *
e4d82761
UD
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
c6c6dd48 14 * GNU Lesser General Public License for more details.
f7eac6eb 15 *
e4d82761
UD
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
f7eac6eb 19 */
e4d82761
UD
20/***************************************************************************/
21/* MODULE_NAME: upow.c */
22/* */
23/* FUNCTIONS: upow */
24/* power1 */
0ac5ae23 25/* my_log2 */
e4d82761
UD
26/* log1 */
27/* checkint */
28/* FILES NEEDED: dla.h endian.h mpa.h mydefs.h */
29/* halfulp.c mpexp.c mplog.c slowexp.c slowpow.c mpa.c */
0ac5ae23 30/* uexp.c upow.c */
e4d82761
UD
31/* root.tbl uexp.tbl upow.tbl */
32/* An ultimate power routine. Given two IEEE double machine numbers y,x */
33/* it computes the correctly rounded (to nearest) value of x^y. */
34/* Assumption: Machine arithmetic operations are performed in */
35/* round to nearest mode of IEEE 754 standard. */
36/* */
37/***************************************************************************/
38#include "endian.h"
39#include "upow.h"
c8b3296b 40#include <dla.h>
e4d82761
UD
41#include "mydefs.h"
42#include "MathLib.h"
43#include "upow.tbl"
e859d1d9 44#include "math_private.h"
f7eac6eb 45
f7eac6eb 46
e4d82761
UD
47double __exp1(double x, double xx, double error);
48static double log1(double x, double *delta, double *error);
1f81acbc 49static double my_log2(double x, double *delta, double *error);
ca58f1db 50double __slowpow(double x, double y,double z);
e4d82761
UD
51static double power1(double x, double y);
52static int checkint(double x);
f7eac6eb 53
e4d82761
UD
54/***************************************************************************/
55/* An ultimate power routine. Given two IEEE double machine numbers y,x */
56/* it computes the correctly rounded (to nearest) value of X^y. */
57/***************************************************************************/
9a656848 58double __ieee754_pow(double x, double y) {
50944bca
UD
59 double z,a,aa,error, t,a1,a2,y1,y2;
60#if 0
61 double gor=1.0;
62#endif
e4d82761
UD
63 mynumber u,v;
64 int k;
65 int4 qx,qy;
66 v.x=y;
67 u.x=x;
68 if (v.i[LOW_HALF] == 0) { /* of y */
69 qx = u.i[HIGH_HALF]&0x7fffffff;
70 /* Checking if x is not too small to compute */
71 if (((qx==0x7ff00000)&&(u.i[LOW_HALF]!=0))||(qx>0x7ff00000)) return NaNQ.x;
72 if (y == 1.0) return x;
73 if (y == 2.0) return x*x;
ca58f1db
UD
74 if (y == -1.0) return 1.0/x;
75 if (y == 0) return 1.0;
e4d82761
UD
76 }
77 /* else */
78 if(((u.i[HIGH_HALF]>0 && u.i[HIGH_HALF]<0x7ff00000)|| /* x>0 and not x->0 */
79 (u.i[HIGH_HALF]==0 && u.i[LOW_HALF]!=0)) &&
0ac5ae23 80 /* 2^-1023< x<= 2^-1023 * 0x1.0000ffffffff */
e4d82761
UD
81 (v.i[HIGH_HALF]&0x7fffffff) < 0x4ff00000) { /* if y<-1 or y>1 */
82 z = log1(x,&aa,&error); /* x^y =e^(y log (X)) */
83 t = y*134217729.0;
84 y1 = t - (t-y);
85 y2 = y - y1;
86 t = z*134217729.0;
87 a1 = t - (t-z);
88 a2 = (z - a1)+aa;
89 a = y1*a1;
90 aa = y2*a1 + y*a2;
91 a1 = a+aa;
92 a2 = (a-a1)+aa;
93 error = error*ABS(y);
94 t = __exp1(a1,a2,1.9e16*error); /* return -10 or 0 if wasn't computed exactly */
95 return (t>0)?t:power1(x,y);
96 }
f7eac6eb 97
e4d82761 98 if (x == 0) {
ca58f1db
UD
99 if (((v.i[HIGH_HALF] & 0x7fffffff) == 0x7ff00000 && v.i[LOW_HALF] != 0)
100 || (v.i[HIGH_HALF] & 0x7fffffff) > 0x7ff00000)
101 return y;
102 if (ABS(y) > 1.0e20) return (y>0)?0:INF.x;
e4d82761 103 k = checkint(y);
ca58f1db
UD
104 if (k == -1)
105 return y < 0 ? 1.0/x : x;
106 else
107 return y < 0 ? 1.0/ABS(x) : 0.0; /* return 0 */
e4d82761 108 }
8f3edfee
UD
109
110 qx = u.i[HIGH_HALF]&0x7fffffff; /* no sign */
111 qy = v.i[HIGH_HALF]&0x7fffffff; /* no sign */
112
113 if (qx >= 0x7ff00000 && (qx > 0x7ff00000 || u.i[LOW_HALF] != 0)) return NaNQ.x;
114 if (qy >= 0x7ff00000 && (qy > 0x7ff00000 || v.i[LOW_HALF] != 0))
115 return x == 1.0 ? 1.0 : NaNQ.x;
116
e4d82761
UD
117 /* if x<0 */
118 if (u.i[HIGH_HALF] < 0) {
119 k = checkint(y);
ca58f1db 120 if (k==0) {
8f3edfee 121 if (qy == 0x7ff00000) {
ca58f1db
UD
122 if (x == -1.0) return 1.0;
123 else if (x > -1.0) return v.i[HIGH_HALF] < 0 ? INF.x : 0.0;
124 else return v.i[HIGH_HALF] < 0 ? 0.0 : INF.x;
125 }
8f3edfee 126 else if (qx == 0x7ff00000)
ca58f1db
UD
127 return y < 0 ? 0.0 : INF.x;
128 return NaNQ.x; /* y not integer and x<0 */
129 }
8f3edfee 130 else if (qx == 0x7ff00000)
ca58f1db
UD
131 {
132 if (k < 0)
133 return y < 0 ? nZERO.x : nINF.x;
134 else
135 return y < 0 ? 0.0 : INF.x;
136 }
137 return (k==1)?__ieee754_pow(-x,y):-__ieee754_pow(-x,y); /* if y even or odd */
e4d82761
UD
138 }
139 /* x>0 */
f7eac6eb 140
e4d82761
UD
141 if (qx == 0x7ff00000) /* x= 2^-0x3ff */
142 {if (y == 0) return NaNQ.x;
143 return (y>0)?x:0; }
f14bd805 144
e4d82761
UD
145 if (qy > 0x45f00000 && qy < 0x7ff00000) {
146 if (x == 1.0) return 1.0;
147 if (y>0) return (x>1.0)?INF.x:0;
148 if (y<0) return (x<1.0)?INF.x:0;
149 }
f7eac6eb 150
ca58f1db 151 if (x == 1.0) return 1.0;
e4d82761
UD
152 if (y>0) return (x>1.0)?INF.x:0;
153 if (y<0) return (x<1.0)?INF.x:0;
154 return 0; /* unreachable, to make the compiler happy */
155}
0ac5ae23 156strong_alias (__ieee754_pow, __pow_finite)
f7eac6eb 157
e4d82761
UD
158/**************************************************************************/
159/* Computing x^y using more accurate but more slow log routine */
160/**************************************************************************/
161static double power1(double x, double y) {
162 double z,a,aa,error, t,a1,a2,y1,y2;
1f81acbc 163 z = my_log2(x,&aa,&error);
e4d82761
UD
164 t = y*134217729.0;
165 y1 = t - (t-y);
166 y2 = y - y1;
167 t = z*134217729.0;
168 a1 = t - (t-z);
169 a2 = z - a1;
170 a = y*z;
171 aa = ((y1*a1-a)+y1*a2+y2*a1)+y2*a2+aa*y;
172 a1 = a+aa;
173 a2 = (a-a1)+aa;
174 error = error*ABS(y);
175 t = __exp1(a1,a2,1.9e16*error);
ca58f1db 176 return (t >= 0)?t:__slowpow(x,y,z);
e4d82761 177}
f7eac6eb 178
e4d82761
UD
179/****************************************************************************/
180/* Computing log(x) (x is left argument). The result is the returned double */
181/* + the parameter delta. */
182/* The result is bounded by error (rightmost argument) */
183/****************************************************************************/
184static double log1(double x, double *delta, double *error) {
50944bca
UD
185 int i,j,m;
186#if 0
187 int n;
188#endif
189 double uu,vv,eps,nx,e,e1,e2,t,t1,t2,res,add=0;
190#if 0
191 double cor;
192#endif
e4d82761 193 mynumber u,v;
27692f89
UD
194#ifdef BIG_ENDI
195 mynumber
196/**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
197#else
198#ifdef LITTLE_ENDI
199 mynumber
200/**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
201#endif
202#endif
ba1ffaa1 203
e4d82761
UD
204 u.x = x;
205 m = u.i[HIGH_HALF];
206 *error = 0;
207 *delta = 0;
208 if (m < 0x00100000) /* 1<x<2^-1007 */
209 { x = x*t52.x; add = -52.0; u.x = x; m = u.i[HIGH_HALF];}
f7eac6eb 210
e4d82761
UD
211 if ((m&0x000fffff) < 0x0006a09e)
212 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3ff00000; two52.i[LOW_HALF]=(m>>20); }
213 else
214 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3fe00000; two52.i[LOW_HALF]=(m>>20)+1; }
f7eac6eb 215
e4d82761
UD
216 v.x = u.x + bigu.x;
217 uu = v.x - bigu.x;
218 i = (v.i[LOW_HALF]&0x000003ff)<<2;
219 if (two52.i[LOW_HALF] == 1023) /* nx = 0 */
220 {
221 if (i > 1192 && i < 1208) /* |x-1| < 1.5*2**-10 */
222 {
223 t = x - 1.0;
224 t1 = (t+5.0e6)-5.0e6;
225 t2 = t-t1;
226 e1 = t - 0.5*t1*t1;
227 e2 = t*t*t*(r3+t*(r4+t*(r5+t*(r6+t*(r7+t*r8)))))-0.5*t2*(t+t1);
228 res = e1+e2;
229 *error = 1.0e-21*ABS(t);
230 *delta = (e1-res)+e2;
231 return res;
232 } /* |x-1| < 1.5*2**-10 */
233 else
234 {
235 v.x = u.x*(ui.x[i]+ui.x[i+1])+bigv.x;
236 vv = v.x-bigv.x;
237 j = v.i[LOW_HALF]&0x0007ffff;
238 j = j+j+j;
239 eps = u.x - uu*vv;
240 e1 = eps*ui.x[i];
241 e2 = eps*(ui.x[i+1]+vj.x[j]*(ui.x[i]+ui.x[i+1]));
242 e = e1+e2;
243 e2 = ((e1-e)+e2);
244 t=ui.x[i+2]+vj.x[j+1];
245 t1 = t+e;
246 t2 = (((t-t1)+e)+(ui.x[i+3]+vj.x[j+2]))+e2+e*e*(p2+e*(p3+e*p4));
247 res=t1+t2;
248 *error = 1.0e-24;
249 *delta = (t1-res)+t2;
250 return res;
251 }
252 } /* nx = 0 */
253 else /* nx != 0 */
254 {
255 eps = u.x - uu;
256 nx = (two52.x - two52e.x)+add;
257 e1 = eps*ui.x[i];
258 e2 = eps*ui.x[i+1];
259 e=e1+e2;
260 e2 = (e1-e)+e2;
261 t=nx*ln2a.x+ui.x[i+2];
262 t1=t+e;
263 t2=(((t-t1)+e)+nx*ln2b.x+ui.x[i+3]+e2)+e*e*(q2+e*(q3+e*(q4+e*(q5+e*q6))));
264 res = t1+t2;
265 *error = 1.0e-21;
266 *delta = (t1-res)+t2;
267 return res;
268 } /* nx != 0 */
269}
270
271/****************************************************************************/
272/* More slow but more accurate routine of log */
273/* Computing log(x)(x is left argument).The result is return double + delta.*/
274/* The result is bounded by error (right argument) */
275/****************************************************************************/
1f81acbc 276static double my_log2(double x, double *delta, double *error) {
50944bca
UD
277 int i,j,m;
278#if 0
279 int n;
280#endif
281 double uu,vv,eps,nx,e,e1,e2,t,t1,t2,res,add=0;
282#if 0
283 double cor;
284#endif
e4d82761 285 double ou1,ou2,lu1,lu2,ov,lv1,lv2,a,a1,a2;
a1a87169 286 double y,yy,z,zz,j1,j2,j7,j8;
58985aa9 287#ifndef DLA_FMS
a1a87169
UD
288 double j3,j4,j5,j6;
289#endif
e4d82761 290 mynumber u,v;
27692f89
UD
291#ifdef BIG_ENDI
292 mynumber
293/**/ two52 = {{0x43300000, 0x00000000}}; /* 2**52 */
294#else
295#ifdef LITTLE_ENDI
296 mynumber
297/**/ two52 = {{0x00000000, 0x43300000}}; /* 2**52 */
298#endif
299#endif
e4d82761
UD
300
301 u.x = x;
302 m = u.i[HIGH_HALF];
303 *error = 0;
304 *delta = 0;
305 add=0;
306 if (m<0x00100000) { /* x < 2^-1022 */
307 x = x*t52.x; add = -52.0; u.x = x; m = u.i[HIGH_HALF]; }
f7eac6eb 308
e4d82761
UD
309 if ((m&0x000fffff) < 0x0006a09e)
310 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3ff00000; two52.i[LOW_HALF]=(m>>20); }
311 else
312 {u.i[HIGH_HALF] = (m&0x000fffff)|0x3fe00000; two52.i[LOW_HALF]=(m>>20)+1; }
313
314 v.x = u.x + bigu.x;
315 uu = v.x - bigu.x;
316 i = (v.i[LOW_HALF]&0x000003ff)<<2;
317 /*------------------------------------- |x-1| < 2**-11------------------------------- */
318 if ((two52.i[LOW_HALF] == 1023) && (i == 1200))
319 {
320 t = x - 1.0;
321 EMULV(t,s3,y,yy,j1,j2,j3,j4,j5);
322 ADD2(-0.5,0,y,yy,z,zz,j1,j2);
323 MUL2(t,0,z,zz,y,yy,j1,j2,j3,j4,j5,j6,j7,j8);
324 MUL2(t,0,y,yy,z,zz,j1,j2,j3,j4,j5,j6,j7,j8);
325
326 e1 = t+z;
327 e2 = (((t-e1)+z)+zz)+t*t*t*(ss3+t*(s4+t*(s5+t*(s6+t*(s7+t*s8)))));
328 res = e1+e2;
329 *error = 1.0e-25*ABS(t);
330 *delta = (e1-res)+e2;
331 return res;
332 }
333 /*----------------------------- |x-1| > 2**-11 -------------------------- */
334 else
335 { /*Computing log(x) according to log table */
336 nx = (two52.x - two52e.x)+add;
337 ou1 = ui.x[i];
338 ou2 = ui.x[i+1];
339 lu1 = ui.x[i+2];
340 lu2 = ui.x[i+3];
341 v.x = u.x*(ou1+ou2)+bigv.x;
342 vv = v.x-bigv.x;
343 j = v.i[LOW_HALF]&0x0007ffff;
344 j = j+j+j;
345 eps = u.x - uu*vv;
346 ov = vj.x[j];
347 lv1 = vj.x[j+1];
348 lv2 = vj.x[j+2];
349 a = (ou1+ou2)*(1.0+ov);
350 a1 = (a+1.0e10)-1.0e10;
351 a2 = a*(1.0-a1*uu*vv);
352 e1 = eps*a1;
353 e2 = eps*a2;
354 e = e1+e2;
355 e2 = (e1-e)+e2;
356 t=nx*ln2a.x+lu1+lv1;
357 t1 = t+e;
358 t2 = (((t-t1)+e)+(lu2+lv2+nx*ln2b.x+e2))+e*e*(p2+e*(p3+e*p4));
359 res=t1+t2;
360 *error = 1.0e-27;
361 *delta = (t1-res)+t2;
362 return res;
363 }
364}
f7eac6eb 365
e4d82761
UD
366/**********************************************************************/
367/* Routine receives a double x and checks if it is an integer. If not */
368/* it returns 0, else it returns 1 if even or -1 if odd. */
369/**********************************************************************/
370static int checkint(double x) {
371 union {int4 i[2]; double x;} u;
50944bca
UD
372 int k,m,n;
373#if 0
374 int l;
375#endif
e4d82761
UD
376 u.x = x;
377 m = u.i[HIGH_HALF]&0x7fffffff; /* no sign */
378 if (m >= 0x7ff00000) return 0; /* x is +/-inf or NaN */
379 if (m >= 0x43400000) return 1; /* |x| >= 2**53 */
380 if (m < 0x40000000) return 0; /* |x| < 2, can not be 0 or 1 */
381 n = u.i[LOW_HALF];
382 k = (m>>20)-1023; /* 1 <= k <= 52 */
383 if (k == 52) return (n&1)? -1:1; /* odd or even*/
384 if (k>20) {
385 if (n<<(k-20)) return 0; /* if not integer */
386 return (n<<(k-21))?-1:1;
387 }
388 if (n) return 0; /*if not integer*/
389 if (k == 20) return (m&1)? -1:1;
390 if (m<<(k+12)) return 0;
391 return (m<<(k+11))?-1:1;
f7eac6eb 392}