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