rochars /
ieee754-buffer
| 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/ieee754-buffer |
||
| 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 | * @module ieee754-buffer |
||
| 36 | */ |
||
| 37 | |||
| 38 | /** |
||
| 39 | * A class to encode and decode IEEE 754 floating-point numbers. |
||
| 40 | */ |
||
| 41 | export class IEEE754Buffer { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Pack a IEEE 754 floating point number. |
||
| 45 | * @param {number} ebits The exponent bits. |
||
| 46 | * @param {number} fbits The fraction bits. |
||
| 47 | */ |
||
| 48 | View Code Duplication | constructor(ebits, fbits) { |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 49 | this.ebits = ebits; |
||
| 50 | this.fbits = fbits; |
||
| 51 | this.bias = (1 << (ebits - 1)) - 1; |
||
| 52 | this.numBytes = Math.ceil((ebits + fbits) / 8); |
||
| 53 | this.biasP2 = Math.pow(2, this.bias + 1); |
||
| 54 | this.ebitsFbits = (ebits + fbits); |
||
| 55 | this.fbias = Math.pow(2, -(8 * this.numBytes - 1 - ebits)); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Pack a IEEE 754 floating point number. |
||
| 60 | * @param {!Uint8Array|!Array<number>} buffer The buffer. |
||
| 61 | * @param {number} num The number. |
||
| 62 | * @param {number} index The index to write on the buffer. |
||
| 63 | * @return {number} The next index to write on the buffer. |
||
| 64 | */ |
||
| 65 | pack(buffer, num, index) { |
||
| 66 | // Round overflows |
||
| 67 | if (Math.abs(num) > this.biasP2 - (this.ebitsFbits * 2)) { |
||
| 68 | num = num < 0 ? -Infinity : Infinity; |
||
| 69 | } |
||
| 70 | /** |
||
| 71 | * sign, need this to handle negative zero |
||
| 72 | * @see http://cwestblog.com/2014/02/25/javascript-testing-for-negative-zero/ |
||
| 73 | * @type {number} |
||
| 74 | */ |
||
| 75 | let sign = (((num = +num) || 1 / num) < 0) ? 1 : num < 0 ? 1 : 0; |
||
| 76 | num = Math.abs(num); |
||
| 77 | /** @type {number} */ |
||
| 78 | let exp = Math.min(Math.floor(Math.log(num) / Math.LN2), 1023); |
||
| 79 | /** @type {number} */ |
||
| 80 | let fraction = this.roundToEven(num / Math.pow(2, exp) * Math.pow(2, this.fbits)); |
||
| 81 | // NaN |
||
| 82 | if (num !== num) { |
||
| 83 | fraction = Math.pow(2, this.fbits - 1); |
||
| 84 | exp = (1 << this.ebits) - 1; |
||
| 85 | // Number |
||
| 86 | } else if (num !== 0) { |
||
| 87 | if (num >= Math.pow(2, 1 - this.bias)) { |
||
| 88 | if (fraction / Math.pow(2, this.fbits) >= 2) { |
||
| 89 | exp = exp + 1; |
||
| 90 | fraction = 1; |
||
| 91 | } |
||
| 92 | // Overflow |
||
| 93 | if (exp > this.bias) { |
||
| 94 | exp = (1 << this.ebits) - 1; |
||
| 95 | fraction = 0; |
||
| 96 | } else { |
||
| 97 | exp = exp + this.bias; |
||
| 98 | fraction = this.roundToEven(fraction) - Math.pow(2, this.fbits); |
||
| 99 | } |
||
| 100 | } else { |
||
| 101 | fraction = this.roundToEven(num / Math.pow(2, 1 - this.bias - this.fbits)); |
||
| 102 | exp = 0; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | return this.packFloatBits_(buffer, index, sign, exp, fraction); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Unpack a IEEE 754 floating point number. |
||
| 110 | * Derived from IEEE754 by DeNA Co., Ltd., MIT License. |
||
| 111 | * Adapted to handle NaN. Should port the solution to the original repo. |
||
| 112 | * @param {!Uint8Array|!Array<number>} buffer The buffer. |
||
| 113 | * @param {number} index The index to read from the buffer. |
||
| 114 | * @return {number} The floating point number. |
||
| 115 | */ |
||
| 116 | unpack(buffer, index) { |
||
| 117 | /** @type {number} */ |
||
| 118 | let eMax = (1 << this.ebits) - 1; |
||
| 119 | /** @type {number} */ |
||
| 120 | let significand; |
||
| 121 | /** @type {string} */ |
||
| 122 | let leftBits = ""; |
||
| 123 | for (let i = this.numBytes - 1; i >= 0 ; i--) { |
||
| 124 | /** @type {string} */ |
||
| 125 | let t = buffer[i + index].toString(2); |
||
| 126 | leftBits += "00000000".substring(t.length) + t; |
||
| 127 | } |
||
| 128 | /** @type {number} */ |
||
| 129 | let sign = leftBits.charAt(0) == "1" ? -1 : 1; |
||
| 130 | leftBits = leftBits.substring(1); |
||
| 131 | /** @type {number} */ |
||
| 132 | let exponent = parseInt(leftBits.substring(0, this.ebits), 2); |
||
| 133 | leftBits = leftBits.substring(this.ebits); |
||
| 134 | if (exponent == eMax) { |
||
| 135 | if (parseInt(leftBits, 2) !== 0) { |
||
| 136 | return NaN; |
||
| 137 | } |
||
| 138 | return sign * Infinity; |
||
| 139 | } else if (exponent === 0) { |
||
| 140 | exponent += 1; |
||
| 141 | significand = parseInt(leftBits, 2); |
||
| 142 | } else { |
||
| 143 | significand = parseInt("1" + leftBits, 2); |
||
| 144 | } |
||
| 145 | return sign * significand * this.fbias * Math.pow(2, exponent - this.bias); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Pack a IEEE754 from its sign, exponent and fraction bits |
||
| 150 | * and place it in a byte buffer. |
||
| 151 | * @param {!Uint8Array|!Array<number>} buffer The byte buffer to write to. |
||
| 152 | * @param {number} index The buffer index to write. |
||
| 153 | * @param {number} sign The sign. |
||
| 154 | * @param {number} exp the exponent. |
||
| 155 | * @param {number} fraction The fraction. |
||
| 156 | * @return {number} |
||
| 157 | * @private |
||
| 158 | */ |
||
| 159 | packFloatBits_(buffer, index, sign, exp, fraction) { |
||
| 160 | /** @type {!Array<number>} */ |
||
| 161 | let bits = []; |
||
| 162 | // the sign |
||
| 163 | bits.push(sign); |
||
| 164 | // the exponent |
||
| 165 | for (let i = this.ebits; i > 0; i -= 1) { |
||
| 166 | bits[i] = (exp % 2 ? 1 : 0); |
||
| 167 | exp = Math.floor(exp / 2); |
||
| 168 | } |
||
| 169 | // the fraction |
||
| 170 | let len = bits.length; |
||
| 171 | for (let i = this.fbits; i > 0; i -= 1) { |
||
| 172 | bits[len + i] = (fraction % 2 ? 1 : 0); |
||
| 173 | fraction = Math.floor(fraction / 2); |
||
| 174 | } |
||
| 175 | // pack as bytes |
||
| 176 | /** @type {string} */ |
||
| 177 | let str = bits.join(''); |
||
| 178 | /** @type {number} */ |
||
| 179 | let numBytes = this.numBytes + index - 1; |
||
| 180 | /** @type {number} */ |
||
| 181 | let k = index; |
||
| 182 | while (numBytes >= index) { |
||
| 183 | buffer[numBytes] = parseInt(str.substring(0, 8), 2); |
||
| 184 | str = str.substring(8); |
||
| 185 | numBytes--; |
||
| 186 | k++; |
||
| 187 | } |
||
| 188 | return k; |
||
| 189 | } |
||
| 190 | |||
| 191 | roundToEven(n) { |
||
| 192 | var w = Math.floor(n), f = n - w; |
||
| 193 | if (f < 0.5) { |
||
| 194 | return w; |
||
| 195 | } |
||
| 196 | if (f > 0.5) { |
||
| 197 | return w + 1; |
||
| 198 | } |
||
| 199 | return w % 2 ? w + 1 : w; |
||
| 200 | } |
||
| 201 | } |
||
| 202 |