summaryrefslogtreecommitdiff
path: root/src/luaconf.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-04-26 23:48:06 +1000
committerdaurnimator <quae@daurnimator.com>2017-04-26 23:56:55 +1000
commit0f0e4d15f7e23102ebb374504322942e3771357e (patch)
treeb352f0cdab47f52e3ce2821190c0bd6974e32dd7 /src/luaconf.js
parent2c6ad8ae871ec8f511f1b983867fccf031bf38b3 (diff)
downloadfengari-0f0e4d15f7e23102ebb374504322942e3771357e.tar.gz
fengari-0f0e4d15f7e23102ebb374504322942e3771357e.tar.bz2
fengari-0f0e4d15f7e23102ebb374504322942e3771357e.zip
Move frexp and ldexp to luaconf.js
Diffstat (limited to 'src/luaconf.js')
-rw-r--r--src/luaconf.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/luaconf.js b/src/luaconf.js
index 803bb90..7cf3d06 100644
--- a/src/luaconf.js
+++ b/src/luaconf.js
@@ -32,6 +32,31 @@ const lua_getlocaledecpoint = function() {
return (1.1).toLocaleString().substring(1, 2);
};
+// See: http://croquetweak.blogspot.fr/2014/08/deconstructing-floats-frexp-and-ldexp.html
+const frexp = function(value) {
+ if (value === 0) return [value, 0];
+ var data = new DataView(new ArrayBuffer(8));
+ data.setFloat64(0, value);
+ var bits = (data.getUint32(0) >>> 20) & 0x7FF;
+ if (bits === 0) { // denormal
+ data.setFloat64(0, value * Math.pow(2, 64)); // exp + 64
+ bits = ((data.getUint32(0) >>> 20) & 0x7FF) - 64;
+ }
+ var exponent = bits - 1022;
+ var mantissa = ldexp(value, -exponent);
+ return [mantissa, exponent];
+};
+
+const ldexp = function(mantissa, exponent) {
+ var steps = Math.min(3, Math.ceil(Math.abs(exponent) / 1023));
+ var result = mantissa;
+ for (var i = 0; i < steps; i++)
+ result *= Math.pow(2, Math.floor((exponent + i) / steps));
+ return result;
+};
+
+module.exports.frexp = frexp;
+module.exports.ldexp = ldexp;
module.exports.LUAI_MAXSTACK = LUAI_MAXSTACK;
module.exports.LUA_IDSIZE = LUA_IDSIZE;
module.exports.LUA_INTEGER_FMT = LUA_INTEGER_FMT;