137 lines
4.4 KiB
JavaScript
137 lines
4.4 KiB
JavaScript
"use strict";
|
|
// Copyright (c) 2008, Fair Oaks Labs, Inc.
|
|
// All rights reserved.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without
|
|
// modification, are permitted provided that the following conditions are met:
|
|
//
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
// this list of conditions and the following disclaimer.
|
|
//
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
// and/or other materials provided with the distribution.
|
|
//
|
|
// * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
|
|
// may be used to endorse or promote products derived from this software
|
|
// without specific prior written permission.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
//
|
|
//
|
|
// Modifications to writeIEEE754 to support negative zeroes made by Brian White
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.writeIEEE754 = exports.readIEEE754 = void 0;
|
|
function readIEEE754(buffer, offset, endian, mLen, nBytes) {
|
|
var e;
|
|
var m;
|
|
var bBE = endian === 'big';
|
|
var eLen = nBytes * 8 - mLen - 1;
|
|
var eMax = (1 << eLen) - 1;
|
|
var eBias = eMax >> 1;
|
|
var nBits = -7;
|
|
var i = bBE ? 0 : nBytes - 1;
|
|
var d = bBE ? 1 : -1;
|
|
var s = buffer[offset + i];
|
|
i += d;
|
|
e = s & ((1 << -nBits) - 1);
|
|
s >>= -nBits;
|
|
nBits += eLen;
|
|
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8)
|
|
;
|
|
m = e & ((1 << -nBits) - 1);
|
|
e >>= -nBits;
|
|
nBits += mLen;
|
|
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8)
|
|
;
|
|
if (e === 0) {
|
|
e = 1 - eBias;
|
|
}
|
|
else if (e === eMax) {
|
|
return m ? NaN : (s ? -1 : 1) * Infinity;
|
|
}
|
|
else {
|
|
m = m + Math.pow(2, mLen);
|
|
e = e - eBias;
|
|
}
|
|
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
|
|
}
|
|
exports.readIEEE754 = readIEEE754;
|
|
function writeIEEE754(buffer, value, offset, endian, mLen, nBytes) {
|
|
var e;
|
|
var m;
|
|
var c;
|
|
var bBE = endian === 'big';
|
|
var eLen = nBytes * 8 - mLen - 1;
|
|
var eMax = (1 << eLen) - 1;
|
|
var eBias = eMax >> 1;
|
|
var rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
|
|
var i = bBE ? nBytes - 1 : 0;
|
|
var d = bBE ? -1 : 1;
|
|
var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
|
value = Math.abs(value);
|
|
if (isNaN(value) || value === Infinity) {
|
|
m = isNaN(value) ? 1 : 0;
|
|
e = eMax;
|
|
}
|
|
else {
|
|
e = Math.floor(Math.log(value) / Math.LN2);
|
|
if (value * (c = Math.pow(2, -e)) < 1) {
|
|
e--;
|
|
c *= 2;
|
|
}
|
|
if (e + eBias >= 1) {
|
|
value += rt / c;
|
|
}
|
|
else {
|
|
value += rt * Math.pow(2, 1 - eBias);
|
|
}
|
|
if (value * c >= 2) {
|
|
e++;
|
|
c /= 2;
|
|
}
|
|
if (e + eBias >= eMax) {
|
|
m = 0;
|
|
e = eMax;
|
|
}
|
|
else if (e + eBias >= 1) {
|
|
m = (value * c - 1) * Math.pow(2, mLen);
|
|
e = e + eBias;
|
|
}
|
|
else {
|
|
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
|
|
e = 0;
|
|
}
|
|
}
|
|
if (isNaN(value))
|
|
m = 0;
|
|
while (mLen >= 8) {
|
|
buffer[offset + i] = m & 0xff;
|
|
i += d;
|
|
m /= 256;
|
|
mLen -= 8;
|
|
}
|
|
e = (e << mLen) | m;
|
|
if (isNaN(value))
|
|
e += 8;
|
|
eLen += mLen;
|
|
while (eLen > 0) {
|
|
buffer[offset + i] = e & 0xff;
|
|
i += d;
|
|
e /= 256;
|
|
eLen -= 8;
|
|
}
|
|
buffer[offset + i - d] |= s * 128;
|
|
}
|
|
exports.writeIEEE754 = writeIEEE754;
|
|
//# sourceMappingURL=float_parser.js.map
|