Changeset 180258 in webkit for trunk/Source/JavaScriptCore/jit/JITOperations.cpp
- Timestamp:
- Feb 18, 2015, 12:01:01 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/jit/JITOperations.cpp
r180102 r180258 1214 1214 #endif 1215 1215 1216 #if PLATFORM(IOS) && CPU(ARM_THUMB2)1217 1218 // The following code is taken from netlib.org:1219 // http://www.netlib.org/fdlibm/fdlibm.h1220 // http://www.netlib.org/fdlibm/e_pow.c1221 // http://www.netlib.org/fdlibm/s_scalbn.c1222 //1223 // And was originally distributed under the following license:1224 1225 /*1226 * ====================================================1227 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.1228 *1229 * Developed at SunSoft, a Sun Microsystems, Inc. business.1230 * Permission to use, copy, modify, and distribute this1231 * software is freely granted, provided that this notice1232 * is preserved.1233 * ====================================================1234 */1235 /*1236 * ====================================================1237 * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.1238 *1239 * Permission to use, copy, modify, and distribute this1240 * software is freely granted, provided that this notice1241 * is preserved.1242 * ====================================================1243 */1244 1245 /* __ieee754_pow(x,y) return x**y1246 *1247 * n1248 * Method: Let x = 2 * (1+f)1249 * 1. Compute and return log2(x) in two pieces:1250 * log2(x) = w1 + w2,1251 * where w1 has 53-24 = 29 bit trailing zeros.1252 * 2. Perform y*log2(x) = n+y' by simulating muti-precision1253 * arithmetic, where |y'|<=0.5.1254 * 3. Return x**y = 2**n*exp(y'*log2)1255 *1256 * Special cases:1257 * 1. (anything) ** 0 is 11258 * 2. (anything) ** 1 is itself1259 * 3. (anything) ** NAN is NAN1260 * 4. NAN ** (anything except 0) is NAN1261 * 5. +-(|x| > 1) ** +INF is +INF1262 * 6. +-(|x| > 1) ** -INF is +01263 * 7. +-(|x| < 1) ** +INF is +01264 * 8. +-(|x| < 1) ** -INF is +INF1265 * 9. +-1 ** +-INF is NAN1266 * 10. +0 ** (+anything except 0, NAN) is +01267 * 11. -0 ** (+anything except 0, NAN, odd integer) is +01268 * 12. +0 ** (-anything except 0, NAN) is +INF1269 * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF1270 * 14. -0 ** (odd integer) = -( +0 ** (odd integer) )1271 * 15. +INF ** (+anything except 0,NAN) is +INF1272 * 16. +INF ** (-anything except 0,NAN) is +01273 * 17. -INF ** (anything) = -0 ** (-anything)1274 * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)1275 * 19. (-anything except 0 and inf) ** (non-integer) is NAN1276 *1277 * Accuracy:1278 * pow(x,y) returns x**y nearly rounded. In particular1279 * pow(integer,integer)1280 * always returns the correct integer provided it is1281 * representable.1282 *1283 * Constants :1284 * The hexadecimal values are the intended ones for the following1285 * constants. The decimal values may be used, provided that the1286 * compiler will convert from decimal to binary accurately enough1287 * to produce the hexadecimal values shown.1288 */1289 1290 #define __HI(x) *(1+(int*)&x)1291 #define __LO(x) *(int*)&x1292 1293 static const double1294 bp[] = {1.0, 1.5,},1295 dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */1296 dp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */1297 zero = 0.0,1298 one = 1.0,1299 two = 2.0,1300 two53 = 9007199254740992.0, /* 0x43400000, 0x00000000 */1301 huge = 1.0e300,1302 tiny = 1.0e-300,1303 /* for scalbn */1304 two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */1305 twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */1306 /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */1307 L1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */1308 L2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */1309 L3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */1310 L4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */1311 L5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */1312 L6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */1313 P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */1314 P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */1315 P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */1316 P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */1317 P5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */1318 lg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */1319 lg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */1320 lg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */1321 ovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */1322 cp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */1323 cp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */1324 cp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/1325 ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */1326 ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/1327 ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/1328 1329 inline double fdlibmScalbn (double x, int n)1330 {1331 int k,hx,lx;1332 hx = __HI(x);1333 lx = __LO(x);1334 k = (hx&0x7ff00000)>>20; /* extract exponent */1335 if (k==0) { /* 0 or subnormal x */1336 if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */1337 x *= two54;1338 hx = __HI(x);1339 k = ((hx&0x7ff00000)>>20) - 54;1340 if (n< -50000) return tiny*x; /*underflow*/1341 }1342 if (k==0x7ff) return x+x; /* NaN or Inf */1343 k = k+n;1344 if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */1345 if (k > 0) /* normal result */1346 {__HI(x) = (hx&0x800fffff)|(k<<20); return x;}1347 if (k <= -54) {1348 if (n > 50000) /* in case integer overflow in n+k */1349 return huge*copysign(huge,x); /*overflow*/1350 else return tiny*copysign(tiny,x); /*underflow*/1351 }1352 k += 54; /* subnormal result */1353 __HI(x) = (hx&0x800fffff)|(k<<20);1354 return x*twom54;1355 }1356 1357 static double fdlibmPow(double x, double y)1358 {1359 double z,ax,z_h,z_l,p_h,p_l;1360 double y1,t1,t2,r,s,t,u,v,w;1361 int i0,i1,i,j,k,yisint,n;1362 int hx,hy,ix,iy;1363 unsigned lx,ly;1364 1365 i0 = ((*(int*)&one)>>29)^1; i1=1-i0;1366 hx = __HI(x); lx = __LO(x);1367 hy = __HI(y); ly = __LO(y);1368 ix = hx&0x7fffffff; iy = hy&0x7fffffff;1369 1370 /* y==zero: x**0 = 1 */1371 if((iy|ly)==0) return one;1372 1373 /* +-NaN return x+y */1374 if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) ||1375 iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0)))1376 return x+y;1377 1378 /* determine if y is an odd int when x < 01379 * yisint = 0 ... y is not an integer1380 * yisint = 1 ... y is an odd int1381 * yisint = 2 ... y is an even int1382 */1383 yisint = 0;1384 if(hx<0) {1385 if(iy>=0x43400000) yisint = 2; /* even integer y */1386 else if(iy>=0x3ff00000) {1387 k = (iy>>20)-0x3ff; /* exponent */1388 if(k>20) {1389 j = ly>>(52-k);1390 if(static_cast<unsigned>(j<<(52-k))==ly) yisint = 2-(j&1);1391 } else if(ly==0) {1392 j = iy>>(20-k);1393 if((j<<(20-k))==iy) yisint = 2-(j&1);1394 }1395 }1396 }1397 1398 /* special value of y */1399 if(ly==0) {1400 if (iy==0x7ff00000) { /* y is +-inf */1401 if(((ix-0x3ff00000)|lx)==0)1402 return y - y; /* inf**+-1 is NaN */1403 else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */1404 return (hy>=0)? y: zero;1405 else /* (|x|<1)**-,+inf = inf,0 */1406 return (hy<0)?-y: zero;1407 }1408 if(iy==0x3ff00000) { /* y is +-1 */1409 if(hy<0) return one/x; else return x;1410 }1411 if(hy==0x40000000) return x*x; /* y is 2 */1412 if(hy==0x3fe00000) { /* y is 0.5 */1413 if(hx>=0) /* x >= +0 */1414 return sqrt(x);1415 }1416 }1417 1418 ax = fabs(x);1419 /* special value of x */1420 if(lx==0) {1421 if(ix==0x7ff00000||ix==0||ix==0x3ff00000){1422 z = ax; /*x is +-0,+-inf,+-1*/1423 if(hy<0) z = one/z; /* z = (1/|x|) */1424 if(hx<0) {1425 if(((ix-0x3ff00000)|yisint)==0) {1426 z = (z-z)/(z-z); /* (-1)**non-int is NaN */1427 } else if(yisint==1)1428 z = -z; /* (x<0)**odd = -(|x|**odd) */1429 }1430 return z;1431 }1432 }1433 1434 n = (hx>>31)+1;1435 1436 /* (x<0)**(non-int) is NaN */1437 if((n|yisint)==0) return (x-x)/(x-x);1438 1439 s = one; /* s (sign of result -ve**odd) = -1 else = 1 */1440 if((n|(yisint-1))==0) s = -one;/* (-ve)**(odd int) */1441 1442 /* |y| is huge */1443 if(iy>0x41e00000) { /* if |y| > 2**31 */1444 if(iy>0x43f00000){ /* if |y| > 2**64, must o/uflow */1445 if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny;1446 if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny;1447 }1448 /* over/underflow if x is not close to one */1449 if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny;1450 if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny;1451 /* now |1-x| is tiny <= 2**-20, suffice to compute1452 log(x) by x-x^2/2+x^3/3-x^4/4 */1453 t = ax-one; /* t has 20 trailing zeros */1454 w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25));1455 u = ivln2_h*t; /* ivln2_h has 21 sig. bits */1456 v = t*ivln2_l-w*ivln2;1457 t1 = u+v;1458 __LO(t1) = 0;1459 t2 = v-(t1-u);1460 } else {1461 double ss,s2,s_h,s_l,t_h,t_l;1462 n = 0;1463 /* take care subnormal number */1464 if(ix<0x00100000)1465 {ax *= two53; n -= 53; ix = __HI(ax); }1466 n += ((ix)>>20)-0x3ff;1467 j = ix&0x000fffff;1468 /* determine interval */1469 ix = j|0x3ff00000; /* normalize ix */1470 if(j<=0x3988E) k=0; /* |x|<sqrt(3/2) */1471 else if(j<0xBB67A) k=1; /* |x|<sqrt(3) */1472 else {k=0;n+=1;ix -= 0x00100000;}1473 __HI(ax) = ix;1474 1475 /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */1476 u = ax-bp[k]; /* bp[0]=1.0, bp[1]=1.5 */1477 v = one/(ax+bp[k]);1478 ss = u*v;1479 s_h = ss;1480 __LO(s_h) = 0;1481 /* t_h=ax+bp[k] High */1482 t_h = zero;1483 __HI(t_h)=((ix>>1)|0x20000000)+0x00080000+(k<<18);1484 t_l = ax - (t_h-bp[k]);1485 s_l = v*((u-s_h*t_h)-s_h*t_l);1486 /* compute log(ax) */1487 s2 = ss*ss;1488 r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));1489 r += s_l*(s_h+ss);1490 s2 = s_h*s_h;1491 t_h = 3.0+s2+r;1492 __LO(t_h) = 0;1493 t_l = r-((t_h-3.0)-s2);1494 /* u+v = ss*(1+...) */1495 u = s_h*t_h;1496 v = s_l*t_h+t_l*ss;1497 /* 2/(3log2)*(ss+...) */1498 p_h = u+v;1499 __LO(p_h) = 0;1500 p_l = v-(p_h-u);1501 z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */1502 z_l = cp_l*p_h+p_l*cp+dp_l[k];1503 /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */1504 t = (double)n;1505 t1 = (((z_h+z_l)+dp_h[k])+t);1506 __LO(t1) = 0;1507 t2 = z_l-(((t1-t)-dp_h[k])-z_h);1508 }1509 1510 /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */1511 y1 = y;1512 __LO(y1) = 0;1513 p_l = (y-y1)*t1+y*t2;1514 p_h = y1*t1;1515 z = p_l+p_h;1516 j = __HI(z);1517 i = __LO(z);1518 if (j>=0x40900000) { /* z >= 1024 */1519 if(((j-0x40900000)|i)!=0) /* if z > 1024 */1520 return s*huge*huge; /* overflow */1521 else {1522 if(p_l+ovt>z-p_h) return s*huge*huge; /* overflow */1523 }1524 } else if((j&0x7fffffff)>=0x4090cc00 ) { /* z <= -1075 */1525 if(((j-0xc090cc00)|i)!=0) /* z < -1075 */1526 return s*tiny*tiny; /* underflow */1527 else {1528 if(p_l<=z-p_h) return s*tiny*tiny; /* underflow */1529 }1530 }1531 /*1532 * compute 2**(p_h+p_l)1533 */1534 i = j&0x7fffffff;1535 k = (i>>20)-0x3ff;1536 n = 0;1537 if(i>0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */1538 n = j+(0x00100000>>(k+1));1539 k = ((n&0x7fffffff)>>20)-0x3ff; /* new k for n */1540 t = zero;1541 __HI(t) = (n&~(0x000fffff>>k));1542 n = ((n&0x000fffff)|0x00100000)>>(20-k);1543 if(j<0) n = -n;1544 p_h -= t;1545 }1546 t = p_l+p_h;1547 __LO(t) = 0;1548 u = t*lg2_h;1549 v = (p_l-(t-p_h))*lg2+t*lg2_l;1550 z = u+v;1551 w = v-(z-u);1552 t = z*z;1553 t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));1554 r = (z*t1)/(t1-two)-(w+z*w);1555 z = one-(r-z);1556 j = __HI(z);1557 j += (n<<20);1558 if((j>>20)<=0) z = fdlibmScalbn(z,n); /* subnormal output */1559 else __HI(z) += (n<<20);1560 return s*z;1561 }1562 1563 static ALWAYS_INLINE bool isDenormal(double x)1564 {1565 static const uint64_t signbit = 0x8000000000000000ULL;1566 static const uint64_t minNormal = 0x0001000000000000ULL;1567 return (bitwise_cast<uint64_t>(x) & ~signbit) - 1 < minNormal - 1;1568 }1569 1570 static ALWAYS_INLINE bool isEdgeCase(double x)1571 {1572 static const uint64_t signbit = 0x8000000000000000ULL;1573 static const uint64_t infinity = 0x7fffffffffffffffULL;1574 return (bitwise_cast<uint64_t>(x) & ~signbit) - 1 >= infinity - 1;1575 }1576 1577 static ALWAYS_INLINE double mathPowInternal(double x, double y)1578 {1579 if (!isDenormal(x) && !isDenormal(y)) {1580 double libmResult = pow(x, y);1581 if (libmResult || isEdgeCase(x) || isEdgeCase(y))1582 return libmResult;1583 }1584 return fdlibmPow(x, y);1585 }1586 1587 #else1588 1589 ALWAYS_INLINE double mathPowInternal(double x, double y)1590 {1591 return pow(x, y);1592 }1593 1594 #endif1595 1596 double JIT_OPERATION operationMathPow(double x, double y)1597 {1598 if (std::isnan(y))1599 return PNaN;1600 if (std::isinf(y) && fabs(x) == 1)1601 return PNaN;1602 return mathPowInternal(x, y);1603 }1604 1605 1216 void JIT_OPERATION operationPutByIndex(ExecState* exec, EncodedJSValue encodedArrayValue, int32_t index, EncodedJSValue encodedValue) 1606 1217 {
Note:
See TracChangeset
for help on using the changeset viewer.