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