diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-03-28 17:18:36 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-03-28 17:18:36 -0700 |
commit | c4ed137c4537cc0103f8def5ea25f03cbd3b42d6 (patch) | |
tree | b440279f9703e5df2f0c6f13cd319b148c27dc6d | |
parent | 054b2403e959ef760f48a8a501477b675bd90221 (diff) |
do not js-optimize away rounding corrections
-rw-r--r-- | src/parseTools.js | 3 | ||||
-rw-r--r-- | tools/test-js-optimizer-output.js | 5 | ||||
-rw-r--r-- | tools/test-js-optimizer.js | 6 |
3 files changed, 11 insertions, 3 deletions
diff --git a/src/parseTools.js b/src/parseTools.js index 520d278e..0e6ddee8 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -1537,7 +1537,8 @@ function makeRounding(value, bits, signed, floatConversion) { // TODO: handle roundings of i64s assert(bits); // C rounds to 0 (-5.5 to -5, +5.5 to 5), while JS has no direct way to do that. - if (bits <= 32 && signed) return '((' + value + ')|0)'; // This is fast and even correct, for all cases + if (bits <= 32 && signed) return '((' + value + ')&-1)'; // This is fast and even correct, for all cases. Note that it is the same + // as |0, but &-1 hints to the js optimizer that this is a rounding correction // Do Math.floor, which is reasonably fast, if we either don't care, or if we can be sure // the value is non-negative if (!correctRoundings() || (!signed && !floatConversion)) return 'Math.floor(' + value + ')'; diff --git a/tools/test-js-optimizer-output.js b/tools/test-js-optimizer-output.js index ca76cae1..273efdb7 100644 --- a/tools/test-js-optimizer-output.js +++ b/tools/test-js-optimizer-output.js @@ -311,4 +311,7 @@ function notComps() { shoo(); } } -// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths", "hoisting", "demangle", "lua", "moreLabels", "notComps"] +function tricky() { + var $conv642 = $conv6374 - (($132 << 16 >> 16) / 2 & -1) & 65535; +} +// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths", "hoisting", "demangle", "lua", "moreLabels", "notComps", "tricky"] diff --git a/tools/test-js-optimizer.js b/tools/test-js-optimizer.js index 18c9ac75..e9d973dd 100644 --- a/tools/test-js-optimizer.js +++ b/tools/test-js-optimizer.js @@ -422,4 +422,8 @@ function notComps() { shoo(); } } -// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths", "hoisting", "demangle", "lua", "moreLabels", "notComps"] +function tricky() { + // The &-1 is a rounding correction, and must not be removed + var $conv642 = ($conv6374 - (($132 << 16 >> 16 | 0) / 2 & -1) | 0) & 65535; +} +// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths", "hoisting", "demangle", "lua", "moreLabels", "notComps", "tricky"] |