|
1
|
|
|
/* |
|
2
|
|
|
* Copyright (c) 2018-2019 Rafael da Silva Rocha. |
|
3
|
|
|
* Copyright (c) 2013 DeNA Co., Ltd. |
|
4
|
|
|
* Copyright (c) 2010, Linden Research, Inc |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
7
|
|
|
* a copy of this software and associated documentation files (the |
|
8
|
|
|
* "Software"), to deal in the Software without restriction, including |
|
9
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
|
10
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
|
11
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
|
12
|
|
|
* the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be |
|
15
|
|
|
* included in all copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
18
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
19
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
20
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
21
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
22
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
23
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @fileoverview Encode and decode IEEE 754 floating point numbers. |
|
29
|
|
|
* @see https://github.com/rochars/byte-data |
|
30
|
|
|
* @see https://bitbucket.org/lindenlab/llsd/raw/7d2646cd3f9b4c806e73aebc4b32bd81e4047fdc/js/typedarray.js |
|
31
|
|
|
* @see https://github.com/kazuho/ieee754.js/blob/master/ieee754.js |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A class to encode and decode IEEE 754 floating-point numbers. |
|
36
|
|
|
*/ |
|
37
|
|
|
export class FloatParser { |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Pack a IEEE 754 floating point number. |
|
41
|
|
|
* @param {number} ebits The exponent bits. |
|
42
|
|
|
* @param {number} fbits The fraction bits. |
|
43
|
|
|
*/ |
|
44
|
|
|
constructor(ebits, fbits) { |
|
45
|
|
|
/** |
|
46
|
|
|
* @type {number} |
|
47
|
|
|
* @private |
|
48
|
|
|
*/ |
|
49
|
|
|
this.ebits = ebits; |
|
50
|
|
|
/** |
|
51
|
|
|
* @type {number} |
|
52
|
|
|
* @private |
|
53
|
|
|
*/ |
|
54
|
|
|
this.fbits = fbits; |
|
55
|
|
|
/** |
|
56
|
|
|
* @type {number} |
|
57
|
|
|
* @private |
|
58
|
|
|
*/ |
|
59
|
|
|
this.bias = (1 << (ebits - 1)) - 1; |
|
60
|
|
|
/** |
|
61
|
|
|
* @type {number} |
|
62
|
|
|
* @private |
|
63
|
|
|
*/ |
|
64
|
|
|
this.numBytes = Math.ceil((ebits + fbits) / 8); |
|
65
|
|
|
/** |
|
66
|
|
|
* @type {number} |
|
67
|
|
|
* @private |
|
68
|
|
|
*/ |
|
69
|
|
|
this.biasP2 = Math.pow(2, this.bias + 1); |
|
70
|
|
|
/** |
|
71
|
|
|
* @type {number} |
|
72
|
|
|
* @private |
|
73
|
|
|
*/ |
|
74
|
|
|
this.ebitsFbits = (ebits + fbits); |
|
75
|
|
|
/** |
|
76
|
|
|
* @type {number} |
|
77
|
|
|
* @private |
|
78
|
|
|
*/ |
|
79
|
|
|
this.fbias = Math.pow(2, -(8 * this.numBytes - 1 - ebits)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Pack a IEEE 754 floating point number. |
|
84
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The buffer. |
|
85
|
|
|
* @param {number} num The number. |
|
86
|
|
|
* @param {number} index The index to write on the buffer. |
|
87
|
|
|
* @return {number} The next index to write on the buffer. |
|
88
|
|
|
* @throws {TypeError} If input is not a number. |
|
89
|
|
|
*/ |
|
90
|
|
|
pack(buffer, num, index) { |
|
91
|
|
|
// Only numbers can be packed |
|
92
|
|
|
if (typeof num != 'number') { |
|
93
|
|
|
throw new TypeError(); |
|
94
|
|
|
} |
|
95
|
|
|
// Round overflows |
|
96
|
|
|
if (Math.abs(num) > this.biasP2 - (this.ebitsFbits * 2)) { |
|
97
|
|
|
num = num < 0 ? -Infinity : Infinity; |
|
98
|
|
|
} |
|
99
|
|
|
/** |
|
100
|
|
|
* sign, need this to handle negative zero |
|
101
|
|
|
* @see http://cwestblog.com/2014/02/25/javascript-testing-for-negative-zero/ |
|
102
|
|
|
* @type {number} |
|
103
|
|
|
*/ |
|
104
|
|
|
let sign = (((num = +num) || 1 / num) < 0) ? 1 : num < 0 ? 1 : 0; |
|
105
|
|
|
num = Math.abs(num); |
|
106
|
|
|
/** @type {number} */ |
|
107
|
|
|
let exp = Math.min(Math.floor(Math.log(num) / Math.LN2), 1023); |
|
108
|
|
|
/** @type {number} */ |
|
109
|
|
|
let fraction = roundToEven(num / Math.pow(2, exp) * Math.pow(2, this.fbits)); |
|
110
|
|
|
// NaN |
|
111
|
|
|
if (num !== num) { |
|
112
|
|
|
fraction = Math.pow(2, this.fbits - 1); |
|
113
|
|
|
exp = (1 << this.ebits) - 1; |
|
114
|
|
|
// Number |
|
115
|
|
|
} else if (num !== 0) { |
|
116
|
|
|
if (num >= Math.pow(2, 1 - this.bias)) { |
|
117
|
|
|
if (fraction / Math.pow(2, this.fbits) >= 2) { |
|
118
|
|
|
exp = exp + 1; |
|
119
|
|
|
fraction = 1; |
|
120
|
|
|
} |
|
121
|
|
|
// Overflow |
|
122
|
|
|
if (exp > this.bias) { |
|
123
|
|
|
exp = (1 << this.ebits) - 1; |
|
124
|
|
|
fraction = 0; |
|
125
|
|
|
} else { |
|
126
|
|
|
exp = exp + this.bias; |
|
127
|
|
|
fraction = roundToEven(fraction) - Math.pow(2, this.fbits); |
|
128
|
|
|
} |
|
129
|
|
|
} else { |
|
130
|
|
|
fraction = roundToEven(num / Math.pow(2, 1 - this.bias - this.fbits)); |
|
131
|
|
|
exp = 0; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
return this.packFloatBits_(buffer, index, sign, exp, fraction); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Unpack a IEEE 754 floating point number. |
|
139
|
|
|
* Derived from IEEE754 by DeNA Co., Ltd., MIT License. |
|
140
|
|
|
* Adapted to handle NaN. Should port the solution to the original repo. |
|
141
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The buffer. |
|
142
|
|
|
* @param {number} index The index to read from the buffer. |
|
143
|
|
|
* @return {number} The floating point number. |
|
144
|
|
|
*/ |
|
145
|
|
|
unpack(buffer, index) { |
|
146
|
|
|
/** @type {number} */ |
|
147
|
|
|
let eMax = (1 << this.ebits) - 1; |
|
148
|
|
|
/** @type {number} */ |
|
149
|
|
|
let significand; |
|
150
|
|
|
/** @type {string} */ |
|
151
|
|
|
let leftBits = ""; |
|
152
|
|
|
for (let i = this.numBytes - 1; i >= 0 ; i--) { |
|
153
|
|
|
/** @type {string} */ |
|
154
|
|
|
let t = buffer[i + index].toString(2); |
|
155
|
|
|
leftBits += "00000000".substring(t.length) + t; |
|
156
|
|
|
} |
|
157
|
|
|
/** @type {number} */ |
|
158
|
|
|
let sign = leftBits.charAt(0) == "1" ? -1 : 1; |
|
159
|
|
|
leftBits = leftBits.substring(1); |
|
160
|
|
|
/** @type {number} */ |
|
161
|
|
|
let exponent = parseInt(leftBits.substring(0, this.ebits), 2); |
|
162
|
|
|
leftBits = leftBits.substring(this.ebits); |
|
163
|
|
|
if (exponent == eMax) { |
|
164
|
|
|
if (parseInt(leftBits, 2) !== 0) { |
|
165
|
|
|
return NaN; |
|
166
|
|
|
} |
|
167
|
|
|
return sign * Infinity; |
|
168
|
|
|
} else if (exponent === 0) { |
|
169
|
|
|
exponent += 1; |
|
170
|
|
|
significand = parseInt(leftBits, 2); |
|
171
|
|
|
} else { |
|
172
|
|
|
significand = parseInt("1" + leftBits, 2); |
|
173
|
|
|
} |
|
174
|
|
|
return sign * significand * this.fbias * Math.pow(2, exponent - this.bias); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Pack a IEEE754 from its sign, exponent and fraction bits |
|
179
|
|
|
* and place it in a byte buffer. |
|
180
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer to write to. |
|
181
|
|
|
* @param {number} index The buffer index to write. |
|
182
|
|
|
* @param {number} sign The sign. |
|
183
|
|
|
* @param {number} exp the exponent. |
|
184
|
|
|
* @param {number} fraction The fraction. |
|
185
|
|
|
* @return {number} |
|
186
|
|
|
* @private |
|
187
|
|
|
*/ |
|
188
|
|
|
packFloatBits_(buffer, index, sign, exp, fraction) { |
|
189
|
|
|
/** @type {!Array<number>} */ |
|
190
|
|
|
let bits = []; |
|
191
|
|
|
// the sign |
|
192
|
|
|
bits.push(sign); |
|
193
|
|
|
// the exponent |
|
194
|
|
|
for (let i = this.ebits; i > 0; i -= 1) { |
|
195
|
|
|
bits[i] = (exp % 2 ? 1 : 0); |
|
196
|
|
|
exp = Math.floor(exp / 2); |
|
197
|
|
|
} |
|
198
|
|
|
// the fraction |
|
199
|
|
|
let len = bits.length; |
|
200
|
|
|
for (let i = this.fbits; i > 0; i -= 1) { |
|
201
|
|
|
bits[len + i] = (fraction % 2 ? 1 : 0); |
|
202
|
|
|
fraction = Math.floor(fraction / 2); |
|
203
|
|
|
} |
|
204
|
|
|
// pack as bytes |
|
205
|
|
|
/** @type {string} */ |
|
206
|
|
|
let str = bits.join(''); |
|
207
|
|
|
/** @type {number} */ |
|
208
|
|
|
let numBytes = this.numBytes + index - 1; |
|
209
|
|
|
/** @type {number} */ |
|
210
|
|
|
let k = index; |
|
211
|
|
|
while (numBytes >= index) { |
|
212
|
|
|
buffer[numBytes] = parseInt(str.substring(0, 8), 2); |
|
213
|
|
|
str = str.substring(8); |
|
214
|
|
|
numBytes--; |
|
215
|
|
|
k++; |
|
216
|
|
|
} |
|
217
|
|
|
return k; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Round a number to its nearest even value. |
|
223
|
|
|
* @param {number} n The number. |
|
224
|
|
|
* @return {number} |
|
225
|
|
|
* @private |
|
226
|
|
|
*/ |
|
227
|
|
|
function roundToEven(n) { |
|
228
|
|
|
/** @type {number} */ |
|
229
|
|
|
let w = Math.floor(n); |
|
230
|
|
|
let f = n - w; |
|
231
|
|
|
if (f < 0.5) { |
|
232
|
|
|
return w; |
|
233
|
|
|
} |
|
234
|
|
|
if (f > 0.5) { |
|
235
|
|
|
return w + 1; |
|
236
|
|
|
} |
|
237
|
|
|
return w % 2 ? w + 1 : w; |
|
238
|
|
|
} |
|
239
|
|
|
|