| OLD | NEW |
| 1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm), | 1 // The following is adapted from fdlibm (http://www.netlib.org/fdlibm), |
| 2 // | 2 // |
| 3 // ==================================================== | 3 // ==================================================== |
| 4 // Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. | 4 // Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. |
| 5 // | 5 // |
| 6 // Developed at SunSoft, a Sun Microsystems, Inc. business. | 6 // Developed at SunSoft, a Sun Microsystems, Inc. business. |
| 7 // Permission to use, copy, modify, and distribute this | 7 // Permission to use, copy, modify, and distribute this |
| 8 // software is freely granted, provided that this notice | 8 // software is freely granted, provided that this notice |
| 9 // is preserved. | 9 // is preserved. |
| 10 // ==================================================== | 10 // ==================================================== |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 (function(global, utils) { | 29 (function(global, utils) { |
| 30 | 30 |
| 31 "use strict"; | 31 "use strict"; |
| 32 | 32 |
| 33 %CheckIsBootstrapping(); | 33 %CheckIsBootstrapping(); |
| 34 | 34 |
| 35 // ------------------------------------------------------------------- | 35 // ------------------------------------------------------------------- |
| 36 // Imports | 36 // Imports |
| 37 | 37 |
| 38 var GlobalMath = global.Math; | 38 var GlobalMath = global.Math; |
| 39 | |
| 40 var MathAbs; | 39 var MathAbs; |
| 41 var MathExp; | 40 var MathExp; |
| 41 var NaN = %GetRootNaN(); |
| 42 | 42 |
| 43 utils.Import(function(from) { | 43 utils.Import(function(from) { |
| 44 MathAbs = from.MathAbs; | 44 MathAbs = from.MathAbs; |
| 45 MathExp = from.MathExp; | 45 MathExp = from.MathExp; |
| 46 }); | 46 }); |
| 47 | 47 |
| 48 // ------------------------------------------------------------------- | 48 // ------------------------------------------------------------------- |
| 49 | 49 |
| 50 define INVPIO2 = kMath[0]; | 50 define INVPIO2 = kMath[0]; |
| 51 define PIO2_1 = kMath[1]; | 51 define PIO2_1 = kMath[1]; |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 var hu = 1; | 469 var hu = 1; |
| 470 var c = 0; | 470 var c = 0; |
| 471 var u = x; | 471 var u = x; |
| 472 | 472 |
| 473 if (hx < 0x3fda827a) { | 473 if (hx < 0x3fda827a) { |
| 474 // x < 0.41422 | 474 // x < 0.41422 |
| 475 if (ax >= 0x3ff00000) { // |x| >= 1 | 475 if (ax >= 0x3ff00000) { // |x| >= 1 |
| 476 if (x === -1) { | 476 if (x === -1) { |
| 477 return -INFINITY; // log1p(-1) = -inf | 477 return -INFINITY; // log1p(-1) = -inf |
| 478 } else { | 478 } else { |
| 479 return NAN; // log1p(x<-1) = NaN | 479 return NaN; // log1p(x<-1) = NaN |
| 480 } | 480 } |
| 481 } else if (ax < 0x3c900000) { | 481 } else if (ax < 0x3c900000) { |
| 482 // For |x| < 2^-54 we can return x. | 482 // For |x| < 2^-54 we can return x. |
| 483 return x; | 483 return x; |
| 484 } else if (ax < 0x3e200000) { | 484 } else if (ax < 0x3e200000) { |
| 485 // For |x| < 2^-29 we can use a simple two-term Taylor series. | 485 // For |x| < 2^-29 we can use a simple two-term Taylor series. |
| 486 return x - x * x * 0.5; | 486 return x - x * x * 0.5; |
| 487 } | 487 } |
| 488 | 488 |
| 489 if ((hx > 0) || (hx <= -0x402D413D)) { // (int) 0xbfd2bec3 = -0x402d413d | 489 if ((hx > 0) || (hx <= -0x402D413D)) { // (int) 0xbfd2bec3 = -0x402d413d |
| 490 // -.2929 < x < 0.41422 | 490 // -.2929 < x < 0.41422 |
| 491 k = 0; | 491 k = 0; |
| 492 } | 492 } |
| 493 } | 493 } |
| 494 | 494 |
| 495 // Handle Infinity and NAN | 495 // Handle Infinity and NaN |
| 496 if (hx >= 0x7ff00000) return x; | 496 if (hx >= 0x7ff00000) return x; |
| 497 | 497 |
| 498 if (k !== 0) { | 498 if (k !== 0) { |
| 499 if (hx < 0x43400000) { | 499 if (hx < 0x43400000) { |
| 500 // x < 2^53 | 500 // x < 2^53 |
| 501 u = 1 + x; | 501 u = 1 + x; |
| 502 hu = %_DoubleHi(u); | 502 hu = %_DoubleHi(u); |
| 503 k = (hu >> 20) - 1023; | 503 k = (hu >> 20) - 1023; |
| 504 c = (k > 0) ? 1 - (u - x) : x - (u - 1); | 504 c = (k > 0) ? 1 - (u - x) : x - (u - 1); |
| 505 c = c / u; | 505 c = c / u; |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 x = x * 1; // Convert to number. | 878 x = x * 1; // Convert to number. |
| 879 var hx = %_DoubleHi(x); | 879 var hx = %_DoubleHi(x); |
| 880 var lx = %_DoubleLo(x); | 880 var lx = %_DoubleLo(x); |
| 881 var k = 0; | 881 var k = 0; |
| 882 | 882 |
| 883 if (hx < 0x00100000) { | 883 if (hx < 0x00100000) { |
| 884 // x < 2^-1022 | 884 // x < 2^-1022 |
| 885 // log10(+/- 0) = -Infinity. | 885 // log10(+/- 0) = -Infinity. |
| 886 if (((hx & 0x7fffffff) | lx) === 0) return -INFINITY; | 886 if (((hx & 0x7fffffff) | lx) === 0) return -INFINITY; |
| 887 // log10 of negative number is NaN. | 887 // log10 of negative number is NaN. |
| 888 if (hx < 0) return NAN; | 888 if (hx < 0) return NaN; |
| 889 // Subnormal number. Scale up x. | 889 // Subnormal number. Scale up x. |
| 890 k -= 54; | 890 k -= 54; |
| 891 x *= TWO54; | 891 x *= TWO54; |
| 892 hx = %_DoubleHi(x); | 892 hx = %_DoubleHi(x); |
| 893 lx = %_DoubleLo(x); | 893 lx = %_DoubleLo(x); |
| 894 } | 894 } |
| 895 | 895 |
| 896 // Infinity or NaN. | 896 // Infinity or NaN. |
| 897 if (hx >= 0x7ff00000) return x; | 897 if (hx >= 0x7ff00000) return x; |
| 898 | 898 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 940 var ax = MathAbs(x); | 940 var ax = MathAbs(x); |
| 941 var hx = %_DoubleHi(x); | 941 var hx = %_DoubleHi(x); |
| 942 var lx = %_DoubleLo(x); | 942 var lx = %_DoubleLo(x); |
| 943 var ix = hx & 0x7fffffff; | 943 var ix = hx & 0x7fffffff; |
| 944 | 944 |
| 945 // Handle special cases. | 945 // Handle special cases. |
| 946 // log2(+/- 0) = -Infinity | 946 // log2(+/- 0) = -Infinity |
| 947 if ((ix | lx) == 0) return -INFINITY; | 947 if ((ix | lx) == 0) return -INFINITY; |
| 948 | 948 |
| 949 // log(x) = NaN, if x < 0 | 949 // log(x) = NaN, if x < 0 |
| 950 if (hx < 0) return NAN; | 950 if (hx < 0) return NaN; |
| 951 | 951 |
| 952 // log2(Infinity) = Infinity, log2(NaN) = NaN | 952 // log2(Infinity) = Infinity, log2(NaN) = NaN |
| 953 if (ix >= 0x7ff00000) return x; | 953 if (ix >= 0x7ff00000) return x; |
| 954 | 954 |
| 955 var n = 0; | 955 var n = 0; |
| 956 | 956 |
| 957 // Take care of subnormal number. | 957 // Take care of subnormal number. |
| 958 if (ix < 0x00100000) { | 958 if (ix < 0x00100000) { |
| 959 ax *= TWO53; | 959 ax *= TWO53; |
| 960 n -= 53; | 960 n -= 53; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1032 "log10", MathLog10, | 1032 "log10", MathLog10, |
| 1033 "log2", MathLog2, | 1033 "log2", MathLog2, |
| 1034 "log1p", MathLog1p, | 1034 "log1p", MathLog1p, |
| 1035 "expm1", MathExpm1 | 1035 "expm1", MathExpm1 |
| 1036 ]); | 1036 ]); |
| 1037 | 1037 |
| 1038 %SetForceInlineFlag(MathSin); | 1038 %SetForceInlineFlag(MathSin); |
| 1039 %SetForceInlineFlag(MathCos); | 1039 %SetForceInlineFlag(MathCos); |
| 1040 | 1040 |
| 1041 }) | 1041 }) |
| OLD | NEW |