[79a0317] | 1 | 'use strict';
|
---|
| 2 | // IEEE754 conversions based on https://github.com/feross/ieee754
|
---|
| 3 | var $Array = Array;
|
---|
| 4 | var abs = Math.abs;
|
---|
| 5 | var pow = Math.pow;
|
---|
| 6 | var floor = Math.floor;
|
---|
| 7 | var log = Math.log;
|
---|
| 8 | var LN2 = Math.LN2;
|
---|
| 9 |
|
---|
| 10 | var pack = function (number, mantissaLength, bytes) {
|
---|
| 11 | var buffer = $Array(bytes);
|
---|
| 12 | var exponentLength = bytes * 8 - mantissaLength - 1;
|
---|
| 13 | var eMax = (1 << exponentLength) - 1;
|
---|
| 14 | var eBias = eMax >> 1;
|
---|
| 15 | var rt = mantissaLength === 23 ? pow(2, -24) - pow(2, -77) : 0;
|
---|
| 16 | var sign = number < 0 || number === 0 && 1 / number < 0 ? 1 : 0;
|
---|
| 17 | var index = 0;
|
---|
| 18 | var exponent, mantissa, c;
|
---|
| 19 | number = abs(number);
|
---|
| 20 | // eslint-disable-next-line no-self-compare -- NaN check
|
---|
| 21 | if (number !== number || number === Infinity) {
|
---|
| 22 | // eslint-disable-next-line no-self-compare -- NaN check
|
---|
| 23 | mantissa = number !== number ? 1 : 0;
|
---|
| 24 | exponent = eMax;
|
---|
| 25 | } else {
|
---|
| 26 | exponent = floor(log(number) / LN2);
|
---|
| 27 | c = pow(2, -exponent);
|
---|
| 28 | if (number * c < 1) {
|
---|
| 29 | exponent--;
|
---|
| 30 | c *= 2;
|
---|
| 31 | }
|
---|
| 32 | if (exponent + eBias >= 1) {
|
---|
| 33 | number += rt / c;
|
---|
| 34 | } else {
|
---|
| 35 | number += rt * pow(2, 1 - eBias);
|
---|
| 36 | }
|
---|
| 37 | if (number * c >= 2) {
|
---|
| 38 | exponent++;
|
---|
| 39 | c /= 2;
|
---|
| 40 | }
|
---|
| 41 | if (exponent + eBias >= eMax) {
|
---|
| 42 | mantissa = 0;
|
---|
| 43 | exponent = eMax;
|
---|
| 44 | } else if (exponent + eBias >= 1) {
|
---|
| 45 | mantissa = (number * c - 1) * pow(2, mantissaLength);
|
---|
| 46 | exponent += eBias;
|
---|
| 47 | } else {
|
---|
| 48 | mantissa = number * pow(2, eBias - 1) * pow(2, mantissaLength);
|
---|
| 49 | exponent = 0;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | while (mantissaLength >= 8) {
|
---|
| 53 | buffer[index++] = mantissa & 255;
|
---|
| 54 | mantissa /= 256;
|
---|
| 55 | mantissaLength -= 8;
|
---|
| 56 | }
|
---|
| 57 | exponent = exponent << mantissaLength | mantissa;
|
---|
| 58 | exponentLength += mantissaLength;
|
---|
| 59 | while (exponentLength > 0) {
|
---|
| 60 | buffer[index++] = exponent & 255;
|
---|
| 61 | exponent /= 256;
|
---|
| 62 | exponentLength -= 8;
|
---|
| 63 | }
|
---|
| 64 | buffer[index - 1] |= sign * 128;
|
---|
| 65 | return buffer;
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | var unpack = function (buffer, mantissaLength) {
|
---|
| 69 | var bytes = buffer.length;
|
---|
| 70 | var exponentLength = bytes * 8 - mantissaLength - 1;
|
---|
| 71 | var eMax = (1 << exponentLength) - 1;
|
---|
| 72 | var eBias = eMax >> 1;
|
---|
| 73 | var nBits = exponentLength - 7;
|
---|
| 74 | var index = bytes - 1;
|
---|
| 75 | var sign = buffer[index--];
|
---|
| 76 | var exponent = sign & 127;
|
---|
| 77 | var mantissa;
|
---|
| 78 | sign >>= 7;
|
---|
| 79 | while (nBits > 0) {
|
---|
| 80 | exponent = exponent * 256 + buffer[index--];
|
---|
| 81 | nBits -= 8;
|
---|
| 82 | }
|
---|
| 83 | mantissa = exponent & (1 << -nBits) - 1;
|
---|
| 84 | exponent >>= -nBits;
|
---|
| 85 | nBits += mantissaLength;
|
---|
| 86 | while (nBits > 0) {
|
---|
| 87 | mantissa = mantissa * 256 + buffer[index--];
|
---|
| 88 | nBits -= 8;
|
---|
| 89 | }
|
---|
| 90 | if (exponent === 0) {
|
---|
| 91 | exponent = 1 - eBias;
|
---|
| 92 | } else if (exponent === eMax) {
|
---|
| 93 | return mantissa ? NaN : sign ? -Infinity : Infinity;
|
---|
| 94 | } else {
|
---|
| 95 | mantissa += pow(2, mantissaLength);
|
---|
| 96 | exponent -= eBias;
|
---|
| 97 | } return (sign ? -1 : 1) * mantissa * pow(2, exponent - mantissaLength);
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | module.exports = {
|
---|
| 101 | pack: pack,
|
---|
| 102 | unpack: unpack
|
---|
| 103 | };
|
---|