diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-02-06 14:55:21 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-02-06 14:55:21 -0800 |
commit | 2631d859ccdf3c20bc34b2f6177946afe106ff0e (patch) | |
tree | e260828952cdcef2bd4b5a11c997695ff65fcf35 | |
parent | ccfe7725f576c0580a60708df457e5d1e83ec911 (diff) |
optimize runtime-knowable appearances of Math.pow
-rw-r--r-- | src/parseTools.js | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/src/parseTools.js b/src/parseTools.js index 56932877..a5752e67 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -1130,7 +1130,17 @@ function getFastValue(a, op, b, type) { a = a.toString(); b = b.toString(); if (isNumber(a) && isNumber(b)) { - return eval(a + op + b).toString(); + if (op == 'pow') { + return Math.pow(a, b).toString(); + } else { + return eval(a + op + b).toString(); + } + } + if (op == 'pow') { + if (a == '2' && isIntImplemented(type)) { + return '(1 << (' + b + '))'; + } + return 'Math.pow(' + a + ', ' + b + ')'; } if (op in PLUS_MUL && isNumber(a)) { // if one of them is a number, keep it last var c = b; @@ -1711,27 +1721,16 @@ function processMathop(item) { return ident1 + ' ^ ' + ident2; } case 'shl': { - // Note: Increases in size may reach the 32-bit limit... where our sign can flip. But this may be expected by the code... - /* - if (bits >= 32) { - if (CHECK_SIGNS && !CORRECT_SIGNS) return 'shlSignCheck(' + ident1 + ', ' + ident2 + ')'; - if (CORRECT_SIGNS) { - var mul = 'Math.pow(2, ' + ident2 + ')'; - if (isNumber(ident2)) mul = eval(mul); - return ident1 + ' * ' + mul; - } - } - */ - if (bits > 32) return ident1 + '*Math.pow(2,' + ident2 + ')'; // TODO: calculate Math.pow at runtime for consts, and below too + if (bits > 32) return ident1 + '*' + getFastValue(2, 'pow', ident2); return ident1 + ' << ' + ident2; } case 'ashr': { - if (bits > 32) return integerizeBignum(ident1 + '/Math.pow(2,' + ident2 + ')'); + if (bits > 32) return integerizeBignum(ident1 + '/' + getFastValue(2, 'pow', ident2)); if (bits === 32) return originalIdent1 + ' >> ' + ident2; // No need to reSign in this case return ident1 + ' >> ' + ident2; } case 'lshr': { - if (bits > 32) return integerizeBignum(ident1 + '/Math.pow(2,' + ident2 + ')'); + if (bits > 32) return integerizeBignum(ident1 + '/' + getFastValue(2, 'pow', ident2)); if (bits === 32) return originalIdent1 + ' >>> ' + ident2; // No need to unSign in this case return ident1 + ' >>> ' + ident2; } |