19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* @(#)e_pow.c 5.1 93/09/24 */ 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * ==================================================== 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Developed at SunPro, a Sun Microsystems, Inc. business. 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Permission to use, copy, modify, and distribute this 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * software is freely granted, provided that this notice 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * is preserved. 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * ==================================================== 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(LIBM_SCCS) && !defined(lint) 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char rcsid[] = "$NetBSD: e_pow.c,v 1.9 1995/05/12 04:57:32 jtc Exp $"; 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* __ieee754_pow(x,y) return x**y 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * n 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Method: Let x = 2 * (1+f) 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 1. Compute and return log2(x) in two pieces: 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * log2(x) = w1 + w2, 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * where w1 has 53-24 = 29 bit trailing zeros. 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 2. Perform y*log2(x) = n+y' by simulating muti-precision 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * arithmetic, where |y'|<=0.5. 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 3. Return x**y = 2**n*exp(y'*log2) 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Special cases: 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 1. (anything) ** 0 is 1 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 2. (anything) ** 1 is itself 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 3. (anything) ** NAN is NAN 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 4. NAN ** (anything except 0) is NAN 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 5. +-(|x| > 1) ** +INF is +INF 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 6. +-(|x| > 1) ** -INF is +0 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 7. +-(|x| < 1) ** +INF is +0 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 8. +-(|x| < 1) ** -INF is +INF 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 9. +-1 ** +-INF is NAN 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 10. +0 ** (+anything except 0, NAN) is +0 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 11. -0 ** (+anything except 0, NAN, odd integer) is +0 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 12. +0 ** (-anything except 0, NAN) is +INF 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 14. -0 ** (odd integer) = -( +0 ** (odd integer) ) 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 15. +INF ** (+anything except 0,NAN) is +INF 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 16. +INF ** (-anything except 0,NAN) is +0 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 17. -INF ** (anything) = -0 ** (-anything) 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer) 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 19. (-anything except 0 and inf) ** (non-integer) is NAN 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Accuracy: 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * pow(x,y) returns x**y nearly rounded. In particular 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * pow(integer,integer) 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * always returns the correct integer provided it is 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * representable. 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Constants : 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * The hexadecimal values are the intended ones for the following 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * constants. The decimal values may be used, provided that the 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * compiler will convert from decimal to binary accurately enough 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * to produce the hexadecimal values shown. 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*#include "math.h"*/ 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "math_private.h" 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __STDC__ 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const double 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic double 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallbp[] = {1.0, 1.5,}, 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */ 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halldp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */ 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallL1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */ 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallL2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */ 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallL3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */ 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallL4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */ 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallL5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */ 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallL6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */ 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallP1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallP2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallP3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallP4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallP5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */ 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halllg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */ 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halllg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */ 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halllg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */ 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */ 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallcp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */ 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallcp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */ 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallcp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/ 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */ 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/ 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __STDC__ 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall double __ieee754_pow(double x, double y) 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall double __ieee754_pow(x,y) 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall double x, y; 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall double z,ax,z_h,z_l,p_h,p_l; 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall double y1,t1,t2,r,s,t,u,v,w; 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int32_t i,j,k,yisint,n; 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int32_t hx,hy,ix,iy; 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall u_int32_t lx,ly; 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall EXTRACT_WORDS(hx,lx,x); 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall EXTRACT_WORDS(hy,ly,y); 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ix = hx&0x7fffffff; iy = hy&0x7fffffff; 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* y==zero: x**0 = 1 */ 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if((iy|ly)==0) return one; 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* +-NaN return x+y */ 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) || 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0))) 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return x+y; 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* determine if y is an odd int when x < 0 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * yisint = 0 ... y is not an integer 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * yisint = 1 ... y is an odd int 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * yisint = 2 ... y is an even int 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall yisint = 0; 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(hx<0) { 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(iy>=0x43400000) yisint = 2; /* even integer y */ 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else if(iy>=0x3ff00000) { 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall k = (iy>>20)-0x3ff; /* exponent */ 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(k>20) { 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall j = ly>>(52-k); 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if((u_int32_t)(j<<(52-k))==ly) yisint = 2-(j&1); 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else if(ly==0) { 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall j = iy>>(20-k); 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if((j<<(20-k))==iy) yisint = 2-(j&1); 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* special value of y */ 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(ly==0) { 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (iy==0x7ff00000) { /* y is +-inf */ 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(((ix-0x3ff00000)|lx)==0) 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return y - y; /* inf**+-1 is NaN */ 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */ 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (hy>=0)? y: zero; 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else /* (|x|<1)**-,+inf = inf,0 */ 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (hy<0)?-y: zero; 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(iy==0x3ff00000) { /* y is +-1 */ 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(hy<0) return one/x; else return x; 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(hy==0x40000000) return x*x; /* y is 2 */ 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(hy==0x3fe00000) { /* y is 0.5 */ 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(hx>=0) /* x >= +0 */ 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return __ieee754_sqrt(x); 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ax = x < 0 ? -x : x; /*fabs(x);*/ 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* special value of x */ 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(lx==0) { 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(ix==0x7ff00000||ix==0||ix==0x3ff00000){ 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall z = ax; /*x is +-0,+-inf,+-1*/ 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(hy<0) z = one/z; /* z = (1/|x|) */ 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(hx<0) { 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(((ix-0x3ff00000)|yisint)==0) { 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall z = (z-z)/(z-z); /* (-1)**non-int is NaN */ 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else if(yisint==1) 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall z = -z; /* (x<0)**odd = -(|x|**odd) */ 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return z; 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* (x<0)**(non-int) is NaN */ 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(((((u_int32_t)hx>>31)-1)|yisint)==0) return (x-x)/(x-x); 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* |y| is huge */ 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(iy>0x41e00000) { /* if |y| > 2**31 */ 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(iy>0x43f00000){ /* if |y| > 2**64, must o/uflow */ 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny; 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny; 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* over/underflow if x is not close to one */ 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(ix<0x3fefffff) return (hy<0)? huge*huge:tiny*tiny; 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(ix>0x3ff00000) return (hy>0)? huge*huge:tiny*tiny; 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* now |1-x| is tiny <= 2**-20, suffice to compute 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall log(x) by x-x^2/2+x^3/3-x^4/4 */ 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t = x-1; /* t has 20 trailing zeros */ 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25)); 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall u = ivln2_h*t; /* ivln2_h has 21 sig. bits */ 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v = t*ivln2_l-w*ivln2; 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t1 = u+v; 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_LOW_WORD(t1,0); 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t2 = v-(t1-u); 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall double s2,s_h,s_l,t_h,t_l; 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall n = 0; 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* take care subnormal number */ 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(ix<0x00100000) 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall {ax *= two53; n -= 53; GET_HIGH_WORD(ix,ax); } 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall n += ((ix)>>20)-0x3ff; 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall j = ix&0x000fffff; 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* determine interval */ 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ix = j|0x3ff00000; /* normalize ix */ 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(j<=0x3988E) k=0; /* |x|<sqrt(3/2) */ 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else if(j<0xBB67A) k=1; /* |x|<sqrt(3) */ 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else {k=0;n+=1;ix -= 0x00100000;} 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_HIGH_WORD(ax,ix); 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall u = ax-bp[k]; /* bp[0]=1.0, bp[1]=1.5 */ 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v = one/(ax+bp[k]); 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall s = u*v; 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall s_h = s; 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_LOW_WORD(s_h,0); 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* t_h=ax+bp[k] High */ 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t_h = zero; 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_HIGH_WORD(t_h,((ix>>1)|0x20000000)+0x00080000+(k<<18)); 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t_l = ax - (t_h-bp[k]); 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall s_l = v*((u-s_h*t_h)-s_h*t_l); 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* compute log(ax) */ 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall s2 = s*s; 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6))))); 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall r += s_l*(s_h+s); 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall s2 = s_h*s_h; 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t_h = 3.0+s2+r; 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_LOW_WORD(t_h,0); 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t_l = r-((t_h-3.0)-s2); 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* u+v = s*(1+...) */ 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall u = s_h*t_h; 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v = s_l*t_h+t_l*s; 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* 2/(3log2)*(s+...) */ 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall p_h = u+v; 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_LOW_WORD(p_h,0); 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall p_l = v-(p_h-u); 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */ 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall z_l = cp_l*p_h+p_l*cp+dp_l[k]; 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */ 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t = (double)n; 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t1 = (((z_h+z_l)+dp_h[k])+t); 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_LOW_WORD(t1,0); 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t2 = z_l-(((t1-t)-dp_h[k])-z_h); 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall s = one; /* s (sign of result -ve**odd) = -1 else = 1 */ 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(((((u_int32_t)hx>>31)-1)|(yisint-1))==0) 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall s = -one;/* (-ve)**(odd int) */ 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall y1 = y; 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_LOW_WORD(y1,0); 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall p_l = (y-y1)*t1+y*t2; 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall p_h = y1*t1; 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall z = p_l+p_h; 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall EXTRACT_WORDS(j,i,z); 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (j>=0x40900000) { /* z >= 1024 */ 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(((j-0x40900000)|i)!=0) /* if z > 1024 */ 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return s*huge*huge; /* overflow */ 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else { 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(p_l+ovt>z-p_h) return s*huge*huge; /* overflow */ 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else if((j&0x7fffffff)>=0x4090cc00 ) { /* z <= -1075 */ 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(((j-0xc090cc00)|i)!=0) /* z < -1075 */ 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return s*tiny*tiny; /* underflow */ 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else { 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(p_l<=z-p_h) return s*tiny*tiny; /* underflow */ 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * compute 2**(p_h+p_l) 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall i = j&0x7fffffff; 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall k = (i>>20)-0x3ff; 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall n = 0; 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(i>0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */ 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall n = j+(0x00100000>>(k+1)); 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall k = ((n&0x7fffffff)>>20)-0x3ff; /* new k for n */ 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t = zero; 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_HIGH_WORD(t,n&~(0x000fffff>>k)); 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall n = ((n&0x000fffff)|0x00100000)>>(20-k); 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if(j<0) n = -n; 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall p_h -= t; 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t = p_l+p_h; 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SET_LOW_WORD(t,0); 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall u = t*lg2_h; 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v = (p_l-(t-p_h))*lg2+t*lg2_l; 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall z = u+v; 2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall w = v-(z-u); 2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t = z*z; 2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); 2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall r = (z*t1)/(t1-two)-(w+z*w); 2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall z = one-(r-z); 2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall GET_HIGH_WORD(j,z); 2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall j += (n<<20); 2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if((j>>20)<=0) z = SDL_NAME(scalbn)(z,n); /* subnormal output */ 3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else SET_HIGH_WORD(z,j); 3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return s*z; 3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 303