]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/ieee754/dbl-64/e_pow.c
Add a number of alias, mostly for IBM codepages.
[thirdparty/glibc.git] / sysdeps / ieee754 / dbl-64 / e_pow.c
CommitLineData
f7eac6eb
RM
1/* @(#)e_pow.c 5.1 93/09/24 */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
ba1ffaa1 8 * software is freely granted, provided that this notice
f7eac6eb
RM
9 * is preserved.
10 * ====================================================
11 */
923609d1
UD
12/* Modified by Naohiko Shimizu/Tokai University, Japan 1997/08/25,
13 for performance improvement on pipelined processors.
14*/
f7eac6eb
RM
15
16#if defined(LIBM_SCCS) && !defined(lint)
17static char rcsid[] = "$NetBSD: e_pow.c,v 1.9 1995/05/12 04:57:32 jtc Exp $";
18#endif
19
20/* __ieee754_pow(x,y) return x**y
21 *
22 * n
23 * Method: Let x = 2 * (1+f)
24 * 1. Compute and return log2(x) in two pieces:
25 * log2(x) = w1 + w2,
26 * where w1 has 53-24 = 29 bit trailing zeros.
ba1ffaa1 27 * 2. Perform y*log2(x) = n+y' by simulating muti-precision
f7eac6eb
RM
28 * arithmetic, where |y'|<=0.5.
29 * 3. Return x**y = 2**n*exp(y'*log2)
30 *
31 * Special cases:
32 * 1. (anything) ** 0 is 1
33 * 2. (anything) ** 1 is itself
34 * 3. (anything) ** NAN is NAN
35 * 4. NAN ** (anything except 0) is NAN
36 * 5. +-(|x| > 1) ** +INF is +INF
37 * 6. +-(|x| > 1) ** -INF is +0
38 * 7. +-(|x| < 1) ** +INF is +0
39 * 8. +-(|x| < 1) ** -INF is +INF
40 * 9. +-1 ** +-INF is NAN
41 * 10. +0 ** (+anything except 0, NAN) is +0
42 * 11. -0 ** (+anything except 0, NAN, odd integer) is +0
43 * 12. +0 ** (-anything except 0, NAN) is +INF
44 * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF
45 * 14. -0 ** (odd integer) = -( +0 ** (odd integer) )
46 * 15. +INF ** (+anything except 0,NAN) is +INF
47 * 16. +INF ** (-anything except 0,NAN) is +0
48 * 17. -INF ** (anything) = -0 ** (-anything)
49 * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
50 * 19. (-anything except 0 and inf) ** (non-integer) is NAN
51 *
52 * Accuracy:
53 * pow(x,y) returns x**y nearly rounded. In particular
54 * pow(integer,integer)
ba1ffaa1 55 * always returns the correct integer provided it is
f7eac6eb
RM
56 * representable.
57 *
58 * Constants :
ba1ffaa1
UD
59 * The hexadecimal values are the intended ones for the following
60 * constants. The decimal values may be used, provided that the
61 * compiler will convert from decimal to binary accurately enough
f7eac6eb
RM
62 * to produce the hexadecimal values shown.
63 */
64
65#include "math.h"
66#include "math_private.h"
923609d1
UD
67#define zero C[0]
68#define one C[1]
69#define two C[2]
70#define two53 C[3]
71#define huge C[4]
72#define tiny C[5]
73#define L1 C[6]
74#define L2 C[7]
75#define L3 C[8]
76#define L4 C[9]
77#define L5 C[10]
78#define L6 C[11]
79#define P1 C[12]
80#define P2 C[13]
81#define P3 C[14]
82#define P4 C[15]
83#define P5 C[16]
84#define lg2 C[17]
85#define lg2_h C[18]
86#define lg2_l C[19]
87#define ovt C[20]
88#define cp C[21]
89#define cp_h C[22]
90#define cp_l C[23]
91#define ivln2 C[24]
92#define ivln2_h C[25]
93#define ivln2_l C[26]
f7eac6eb
RM
94
95#ifdef __STDC__
ba1ffaa1 96static const double
f7eac6eb 97#else
ba1ffaa1 98static double
f7eac6eb
RM
99#endif
100bp[] = {1.0, 1.5,},
101dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */
102dp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */
923609d1
UD
103C[] = {
1040.0,
1051.0,
1062.0,
1079007199254740992.0 ,
1081.0e300,
1091.0e-300,
1105.99999999999994648725e-01 ,
1114.28571428578550184252e-01 ,
1123.33333329818377432918e-01 ,
1132.72728123808534006489e-01 ,
1142.30660745775561754067e-01 ,
1152.06975017800338417784e-01 ,
1161.66666666666666019037e-01 ,
117-2.77777777770155933842e-03 ,
1186.61375632143793436117e-05 ,
119-1.65339022054652515390e-06 ,
1204.13813679705723846039e-08 ,
1216.93147180559945286227e-01 ,
1226.93147182464599609375e-01 ,
123-1.90465429995776804525e-09 ,
1248.0085662595372944372e-0017 ,
1259.61796693925975554329e-01 ,
1269.61796700954437255859e-01 ,
127-7.02846165095275826516e-09 ,
1281.44269504088896338700e+00 ,
1291.44269502162933349609e+00 ,
1301.92596299112661746887e-08 };
f7eac6eb
RM
131
132#ifdef __STDC__
133 double __ieee754_pow(double x, double y)
134#else
135 double __ieee754_pow(x,y)
136 double x, y;
137#endif
138{
139 double z,ax,z_h,z_l,p_h,p_l;
923609d1 140 double y1,t1,t2,r,s,t,u,v,w, t12,t14,r_1,r_2,r_3;
f7eac6eb
RM
141 int32_t i,j,k,yisint,n;
142 int32_t hx,hy,ix,iy;
143 u_int32_t lx,ly;
144
145 EXTRACT_WORDS(hx,lx,x);
146 EXTRACT_WORDS(hy,ly,y);
147 ix = hx&0x7fffffff; iy = hy&0x7fffffff;
148
149 /* y==zero: x**0 = 1 */
923609d1 150 if((iy|ly)==0) return C[1];
f7eac6eb 151
f14bd805
UD
152 /* x==+-1 */
153 if(x == 1.0) return C[1];
154 if(x == -1.0 && isinf(y)) return C[1];
155
f7eac6eb
RM
156 /* +-NaN return x+y */
157 if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) ||
ba1ffaa1
UD
158 iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0)))
159 return x+y;
f7eac6eb
RM
160
161 /* determine if y is an odd int when x < 0
162 * yisint = 0 ... y is not an integer
163 * yisint = 1 ... y is an odd int
164 * yisint = 2 ... y is an even int
165 */
166 yisint = 0;
ba1ffaa1 167 if(hx<0) {
f7eac6eb
RM
168 if(iy>=0x43400000) yisint = 2; /* even integer y */
169 else if(iy>=0x3ff00000) {
170 k = (iy>>20)-0x3ff; /* exponent */
171 if(k>20) {
172 j = ly>>(52-k);
ba1ffaa1 173 if((u_int32_t)(j<<(52-k))==ly) yisint = 2-(j&1);
f7eac6eb
RM
174 } else if(ly==0) {
175 j = iy>>(20-k);
ba1ffaa1 176 if((int32_t)(j<<(20-k))==iy) yisint = 2-(j&1);
f7eac6eb 177 }
ba1ffaa1
UD
178 }
179 }
f7eac6eb
RM
180
181 /* special value of y */
ba1ffaa1 182 if(ly==0) {
f7eac6eb
RM
183 if (iy==0x7ff00000) { /* y is +-inf */
184 if(((ix-0x3ff00000)|lx)==0)
185 return y - y; /* inf**+-1 is NaN */
186 else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */
923609d1 187 return (hy>=0)? y: C[0];
f7eac6eb 188 else /* (|x|<1)**-,+inf = inf,0 */
923609d1 189 return (hy<0)?-y: C[0];
ba1ffaa1 190 }
f7eac6eb 191 if(iy==0x3ff00000) { /* y is +-1 */
923609d1 192 if(hy<0) return C[1]/x; else return x;
f7eac6eb
RM
193 }
194 if(hy==0x40000000) return x*x; /* y is 2 */
195 if(hy==0x3fe00000) { /* y is 0.5 */
196 if(hx>=0) /* x >= +0 */
ba1ffaa1 197 return __ieee754_sqrt(x);
f7eac6eb
RM
198 }
199 }
200
201 ax = fabs(x);
202 /* special value of x */
203 if(lx==0) {
204 if(ix==0x7ff00000||ix==0||ix==0x3ff00000){
205 z = ax; /*x is +-0,+-inf,+-1*/
923609d1 206 if(hy<0) z = C[1]/z; /* z = (1/|x|) */
f7eac6eb
RM
207 if(hx<0) {
208 if(((ix-0x3ff00000)|yisint)==0) {
209 z = (z-z)/(z-z); /* (-1)**non-int is NaN */
ba1ffaa1 210 } else if(yisint==1)
f7eac6eb
RM
211 z = -z; /* (x<0)**odd = -(|x|**odd) */
212 }
213 return z;
214 }
215 }
ba1ffaa1 216
f7eac6eb
RM
217 /* (x<0)**(non-int) is NaN */
218 if(((((u_int32_t)hx>>31)-1)|yisint)==0) return (x-x)/(x-x);
219
220 /* |y| is huge */
221 if(iy>0x41e00000) { /* if |y| > 2**31 */
222 if(iy>0x43f00000){ /* if |y| > 2**64, must o/uflow */
923609d1
UD
223 if(ix<=0x3fefffff) return (hy<0)? C[4]*C[4]:C[5]*C[5];
224 if(ix>=0x3ff00000) return (hy>0)? C[4]*C[4]:C[5]*C[5];
f7eac6eb
RM
225 }
226 /* over/underflow if x is not close to one */
923609d1
UD
227 if(ix<0x3fefffff) return (hy<0)? C[4]*C[4]:C[5]*C[5];
228 if(ix>0x3ff00000) return (hy>0)? C[4]*C[4]:C[5]*C[5];
ba1ffaa1 229 /* now |1-x| is tiny <= 2**-20, suffice to compute
f7eac6eb
RM
230 log(x) by x-x^2/2+x^3/3-x^4/4 */
231 t = x-1; /* t has 20 trailing zeros */
232 w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25));
923609d1
UD
233 u = C[25]*t; /* ivln2_h has 21 sig. bits */
234 v = t*C[26]-w*C[24];
f7eac6eb
RM
235 t1 = u+v;
236 SET_LOW_WORD(t1,0);
237 t2 = v-(t1-u);
238 } else {
923609d1 239 double s2,s_h,s_l,t_h,t_l,s22,s24,s26,r1,r2,r3;
f7eac6eb
RM
240 n = 0;
241 /* take care subnormal number */
242 if(ix<0x00100000)
923609d1 243 {ax *= C[3]; n -= 53; GET_HIGH_WORD(ix,ax); }
f7eac6eb
RM
244 n += ((ix)>>20)-0x3ff;
245 j = ix&0x000fffff;
246 /* determine interval */
247 ix = j|0x3ff00000; /* normalize ix */
248 if(j<=0x3988E) k=0; /* |x|<sqrt(3/2) */
249 else if(j<0xBB67A) k=1; /* |x|<sqrt(3) */
250 else {k=0;n+=1;ix -= 0x00100000;}
251 SET_HIGH_WORD(ax,ix);
252
253 /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
254 u = ax-bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
923609d1 255 v = C[1]/(ax+bp[k]);
f7eac6eb
RM
256 s = u*v;
257 s_h = s;
258 SET_LOW_WORD(s_h,0);
259 /* t_h=ax+bp[k] High */
923609d1 260 t_h = C[0];
f7eac6eb
RM
261 SET_HIGH_WORD(t_h,((ix>>1)|0x20000000)+0x00080000+(k<<18));
262 t_l = ax - (t_h-bp[k]);
263 s_l = v*((u-s_h*t_h)-s_h*t_l);
264 /* compute log(ax) */
265 s2 = s*s;
923609d1 266#ifdef DO_NOT_USE_THIS
f7eac6eb 267 r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
923609d1
UD
268#else
269 r1 = C[10]+s2*C[11]; s22=s2*s2;
270 r2 = C[8]+s2*C[9]; s24=s22*s22;
271 r3 = C[6]+s2*C[7]; s26=s24*s22;
272 r = r3*s22 + r2*s24 + r1*s26;
b9337b6a 273#endif
f7eac6eb
RM
274 r += s_l*(s_h+s);
275 s2 = s_h*s_h;
276 t_h = 3.0+s2+r;
277 SET_LOW_WORD(t_h,0);
278 t_l = r-((t_h-3.0)-s2);
279 /* u+v = s*(1+...) */
280 u = s_h*t_h;
281 v = s_l*t_h+t_l*s;
282 /* 2/(3log2)*(s+...) */
283 p_h = u+v;
284 SET_LOW_WORD(p_h,0);
285 p_l = v-(p_h-u);
923609d1
UD
286 z_h = C[22]*p_h; /* cp_h+cp_l = 2/(3*log2) */
287 z_l = C[23]*p_h+p_l*C[21]+dp_l[k];
f7eac6eb
RM
288 /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
289 t = (double)n;
290 t1 = (((z_h+z_l)+dp_h[k])+t);
291 SET_LOW_WORD(t1,0);
292 t2 = z_l-(((t1-t)-dp_h[k])-z_h);
293 }
294
923609d1 295 s = C[1]; /* s (sign of result -ve**odd) = -1 else = 1 */
f7eac6eb 296 if(((((u_int32_t)hx>>31)-1)|(yisint-1))==0)
923609d1 297 s = -C[1];/* (-ve)**(odd int) */
f7eac6eb
RM
298
299 /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
300 y1 = y;
301 SET_LOW_WORD(y1,0);
302 p_l = (y-y1)*t1+y*t2;
303 p_h = y1*t1;
304 z = p_l+p_h;
305 EXTRACT_WORDS(j,i,z);
306 if (j>=0x40900000) { /* z >= 1024 */
307 if(((j-0x40900000)|i)!=0) /* if z > 1024 */
923609d1 308 return s*C[4]*C[4]; /* overflow */
f7eac6eb 309 else {
923609d1 310 if(p_l+C[20]>z-p_h) return s*C[4]*C[4]; /* overflow */
f7eac6eb
RM
311 }
312 } else if((j&0x7fffffff)>=0x4090cc00 ) { /* z <= -1075 */
313 if(((j-0xc090cc00)|i)!=0) /* z < -1075 */
923609d1 314 return s*C[5]*C[5]; /* underflow */
f7eac6eb 315 else {
923609d1 316 if(p_l<=z-p_h) return s*C[5]*C[5]; /* underflow */
f7eac6eb
RM
317 }
318 }
319 /*
320 * compute 2**(p_h+p_l)
321 */
322 i = j&0x7fffffff;
323 k = (i>>20)-0x3ff;
324 n = 0;
325 if(i>0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
326 n = j+(0x00100000>>(k+1));
327 k = ((n&0x7fffffff)>>20)-0x3ff; /* new k for n */
923609d1 328 t = C[0];
f7eac6eb
RM
329 SET_HIGH_WORD(t,n&~(0x000fffff>>k));
330 n = ((n&0x000fffff)|0x00100000)>>(20-k);
331 if(j<0) n = -n;
332 p_h -= t;
ba1ffaa1 333 }
f7eac6eb
RM
334 t = p_l+p_h;
335 SET_LOW_WORD(t,0);
923609d1
UD
336 u = t*C[18];
337 v = (p_l-(t-p_h))*C[17]+t*C[19];
f7eac6eb
RM
338 z = u+v;
339 w = v-(z-u);
340 t = z*z;
923609d1
UD
341#ifdef DO_NOT_USE_THIS
342 t1 = z - t*(C[12]+t*(C[13]+t*(C[14]+t*(C[15]+t*C[16]))));
343#else
344 r_1 = C[15]+t*C[16]; t12 = t*t;
345 r_2 = C[13]+t*C[14]; t14 = t12*t12;
346 r_3 = t*C[12];
347 t1 = z - r_3 - t12*r_2 - t14*r_1;
348#endif
349 r = (z*t1)/(t1-C[2])-(w+z*w);
350 z = C[1]-(r-z);
f7eac6eb
RM
351 GET_HIGH_WORD(j,z);
352 j += (n<<20);
353 if((j>>20)<=0) z = __scalbn(z,n); /* subnormal output */
354 else SET_HIGH_WORD(z,j);
355 return s*z;
356}