aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xemscripten.py2
-rw-r--r--src/parseTools.js17
-rw-r--r--src/preamble.js7
-rw-r--r--src/settings.js1
4 files changed, 22 insertions, 5 deletions
diff --git a/emscripten.py b/emscripten.py
index 217fd6d5..1b1284c7 100755
--- a/emscripten.py
+++ b/emscripten.py
@@ -423,6 +423,8 @@ def emscript(infile, settings, outfile, libraries=[], compiler_engine=None,
math_envs = ['Math.min'] # TODO: move min to maths
asm_setup += '\n'.join(['var %s = %s;' % (f.replace('.', '_'), f) for f in math_envs])
+ if settings['TO_FLOAT32']: maths += ['Math.toFloat32']
+
basic_funcs = ['abort', 'assert', 'asmPrintInt', 'asmPrintFloat'] + [m.replace('.', '_') for m in math_envs]
if settings['RESERVED_FUNCTION_POINTERS'] > 0: basic_funcs.append('jsCall')
if settings['SAFE_HEAP']: basic_funcs += ['SAFE_HEAP_LOAD', 'SAFE_HEAP_STORE', 'SAFE_HEAP_CLEAR']
diff --git a/src/parseTools.js b/src/parseTools.js
index b655d13e..f11c867a 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -2020,6 +2020,13 @@ function makeIsNaN(value) {
return 'isNaN(' + value + ')';
}
+function makeFloat(value, type) {
+ if (TO_FLOAT32 && type == 'float') {
+ return 'Math.toFloat32(' + value + ')';
+ }
+ return value;
+}
+
// fptoui and fptosi are not in these, because we need to be careful about what we do there. We can't
// just sign/unsign the input first.
var UNSIGNED_OP = set('udiv', 'urem', 'uitofp', 'zext', 'lshr');
@@ -2275,11 +2282,11 @@ function processMathop(item) {
return idents[0] + ' >>> ' + idents[1];
}
// basic float ops
- case 'fadd': return getFastValue(idents[0], '+', idents[1], item.type);
- case 'fsub': return getFastValue(idents[0], '-', idents[1], item.type);
- case 'fdiv': return getFastValue(idents[0], '/', idents[1], item.type);
- case 'fmul': return getFastValue(idents[0], '*', idents[1], item.type);
- case 'frem': return getFastValue(idents[0], '%', idents[1], item.type);
+ case 'fadd': return makeFloat(getFastValue(idents[0], '+', idents[1], item.type), item.type);
+ case 'fsub': return makeFloat(getFastValue(idents[0], '-', idents[1], item.type), item.type);
+ case 'fdiv': return makeFloat(getFastValue(idents[0], '/', idents[1], item.type), item.type);
+ case 'fmul': return makeFloat(getFastValue(idents[0], '*', idents[1], item.type), item.type);
+ case 'frem': return makeFloat(getFastValue(idents[0], '%', idents[1], item.type), item.type);
case 'uitofp': case 'sitofp': return asmCoercion(idents[0], 'double', op[0]);
case 'fptoui': case 'fptosi': return makeRounding(idents[0], bitsLeft, op === 'fptosi', true);
diff --git a/src/preamble.js b/src/preamble.js
index 95bf2dc2..0f3f52c9 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -853,6 +853,13 @@ Math['imul'] = function(a, b) {
#endif
Math.imul = Math['imul'];
+#if TO_FLOAT32
+if (!Math['toFloat32']) Math['toFloat32'] = function(x) {
+ return x;
+};
+Math.toFloat32 = Math['toFloat32'];
+#endif
+
// A counter of dependencies for calling run(). If we need to
// do asynchronous work before running, increment this and
// decrement it. Incrementing must happen in a place like
diff --git a/src/settings.js b/src/settings.js
index 3515091d..3ecac040 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -108,6 +108,7 @@ var PRECISE_I64_MATH = 1; // If enabled, i64 addition etc. is emulated - which i
var PRECISE_I32_MUL = 1; // If enabled, i32 multiplication is done with full precision, which means it is
// correct even if the value exceeds the JS double-integer limit of ~52 bits (otherwise,
// rounding will occur above that range).
+var TO_FLOAT32 = 0; // Use Math.toFloat32
var CLOSURE_ANNOTATIONS = 0; // If set, the generated code will be annotated for the closure
// compiler. This potentially lets closure optimize the code better.