1 | /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
---|
2 | exports.read = function (buffer, offset, isLE, mLen, nBytes) {
|
---|
3 | var e, m
|
---|
4 | var eLen = (nBytes * 8) - mLen - 1
|
---|
5 | var eMax = (1 << eLen) - 1
|
---|
6 | var eBias = eMax >> 1
|
---|
7 | var nBits = -7
|
---|
8 | var i = isLE ? (nBytes - 1) : 0
|
---|
9 | var d = isLE ? -1 : 1
|
---|
10 | var s = buffer[offset + i]
|
---|
11 |
|
---|
12 | i += d
|
---|
13 |
|
---|
14 | e = s & ((1 << (-nBits)) - 1)
|
---|
15 | s >>= (-nBits)
|
---|
16 | nBits += eLen
|
---|
17 | for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
|
---|
18 |
|
---|
19 | m = e & ((1 << (-nBits)) - 1)
|
---|
20 | e >>= (-nBits)
|
---|
21 | nBits += mLen
|
---|
22 | for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
|
---|
23 |
|
---|
24 | if (e === 0) {
|
---|
25 | e = 1 - eBias
|
---|
26 | } else if (e === eMax) {
|
---|
27 | return m ? NaN : ((s ? -1 : 1) * Infinity)
|
---|
28 | } else {
|
---|
29 | m = m + Math.pow(2, mLen)
|
---|
30 | e = e - eBias
|
---|
31 | }
|
---|
32 | return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
|
---|
33 | }
|
---|
34 |
|
---|
35 | exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
|
---|
36 | var e, m, c
|
---|
37 | var eLen = (nBytes * 8) - mLen - 1
|
---|
38 | var eMax = (1 << eLen) - 1
|
---|
39 | var eBias = eMax >> 1
|
---|
40 | var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
|
---|
41 | var i = isLE ? 0 : (nBytes - 1)
|
---|
42 | var d = isLE ? 1 : -1
|
---|
43 | var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
|
---|
44 |
|
---|
45 | value = Math.abs(value)
|
---|
46 |
|
---|
47 | if (isNaN(value) || value === Infinity) {
|
---|
48 | m = isNaN(value) ? 1 : 0
|
---|
49 | e = eMax
|
---|
50 | } else {
|
---|
51 | e = Math.floor(Math.log(value) / Math.LN2)
|
---|
52 | if (value * (c = Math.pow(2, -e)) < 1) {
|
---|
53 | e--
|
---|
54 | c *= 2
|
---|
55 | }
|
---|
56 | if (e + eBias >= 1) {
|
---|
57 | value += rt / c
|
---|
58 | } else {
|
---|
59 | value += rt * Math.pow(2, 1 - eBias)
|
---|
60 | }
|
---|
61 | if (value * c >= 2) {
|
---|
62 | e++
|
---|
63 | c /= 2
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (e + eBias >= eMax) {
|
---|
67 | m = 0
|
---|
68 | e = eMax
|
---|
69 | } else if (e + eBias >= 1) {
|
---|
70 | m = ((value * c) - 1) * Math.pow(2, mLen)
|
---|
71 | e = e + eBias
|
---|
72 | } else {
|
---|
73 | m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
|
---|
74 | e = 0
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
|
---|
79 |
|
---|
80 | e = (e << mLen) | m
|
---|
81 | eLen += mLen
|
---|
82 | for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
|
---|
83 |
|
---|
84 | buffer[offset + i - d] |= s * 128
|
---|
85 | }
|
---|