aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-02-22 19:06:25 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-02-22 19:06:25 -0800
commit022afe18f791b15d02f8ac8525bf033208c1db08 (patch)
tree85027bd696188b97953cf2ac09fdbd465379de1f
parentc00e097d60100c7a562ac25664eb3c25bb9403ae (diff)
use Math.imul for precise 32-bit mul
-rw-r--r--src/parseTools.js5
-rw-r--r--src/preamble.js12
2 files changed, 13 insertions, 4 deletions
diff --git a/src/parseTools.js b/src/parseTools.js
index 514d74d0..e6138fc1 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -2129,9 +2129,8 @@ function processMathop(item) {
case 'sub': return handleOverflow(getFastValue(idents[0], '-', idents[1], item.type), bits);
case 'sdiv': case 'udiv': return makeRounding(getFastValue(idents[0], '/', idents[1], item.type), bits, op[0] === 's');
case 'mul': {
- if (bits == 32 && PRECISE_I32_MUL) {
- Types.preciseI64MathUsed = true;
- return '(i64Math' + (ASM_JS ? '_' : '.') + 'multiply(' + asmCoercion(idents[0], 'i32') + ',0,' + asmCoercion(idents[1], 'i32') + ',0),' + makeGetValue('tempDoublePtr', 0, 'i32') + ')';
+ if (bits == 32) {
+ return 'Math.imul(' + idents[0] + ',' + idents[1] + ')';
} else {
return '((' +getFastValue(idents[0], '*', idents[1], item.type) + ')&-1)'; // force a non-eliminatable coercion here, to prevent a double result from leaking
}
diff --git a/src/preamble.js b/src/preamble.js
index b355f130..05269c6e 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -742,7 +742,17 @@ Module['writeArrayToMemory'] = writeArrayToMemory;
{{{ unSign }}}
{{{ reSign }}}
-if (!Math.imul) Math.imul = function(x, y) { return (x*y)|0 }; // # not a real polyfill since semantics not identical, but close and fast
+if (!Math.imul) Math.imul = function(a, b) {
+#if PRECISE_I32_MUL
+ var ah = a >>> 16;
+ var al = a & 0xffff;
+ var bh = b >>> 16;
+ var bl = b & 0xffff;
+ return (al*bl + ((ah*bl + al*bh) << 16))|0;
+#else
+ return (a*b)|0; // fast but imprecise
+#endif
+};
// A counter of dependencies for calling run(). If we need to
// do asynchronous work before running, increment this and