index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. "use strict";
  2. module.exports = factory(factory);
  3. /**
  4. * Reads / writes floats / doubles from / to buffers.
  5. * @name util.float
  6. * @namespace
  7. */
  8. /**
  9. * Writes a 32 bit float to a buffer using little endian byte order.
  10. * @name util.float.writeFloatLE
  11. * @function
  12. * @param {number} val Value to write
  13. * @param {Uint8Array} buf Target buffer
  14. * @param {number} pos Target buffer offset
  15. * @returns {undefined}
  16. */
  17. /**
  18. * Writes a 32 bit float to a buffer using big endian byte order.
  19. * @name util.float.writeFloatBE
  20. * @function
  21. * @param {number} val Value to write
  22. * @param {Uint8Array} buf Target buffer
  23. * @param {number} pos Target buffer offset
  24. * @returns {undefined}
  25. */
  26. /**
  27. * Reads a 32 bit float from a buffer using little endian byte order.
  28. * @name util.float.readFloatLE
  29. * @function
  30. * @param {Uint8Array} buf Source buffer
  31. * @param {number} pos Source buffer offset
  32. * @returns {number} Value read
  33. */
  34. /**
  35. * Reads a 32 bit float from a buffer using big endian byte order.
  36. * @name util.float.readFloatBE
  37. * @function
  38. * @param {Uint8Array} buf Source buffer
  39. * @param {number} pos Source buffer offset
  40. * @returns {number} Value read
  41. */
  42. /**
  43. * Writes a 64 bit double to a buffer using little endian byte order.
  44. * @name util.float.writeDoubleLE
  45. * @function
  46. * @param {number} val Value to write
  47. * @param {Uint8Array} buf Target buffer
  48. * @param {number} pos Target buffer offset
  49. * @returns {undefined}
  50. */
  51. /**
  52. * Writes a 64 bit double to a buffer using big endian byte order.
  53. * @name util.float.writeDoubleBE
  54. * @function
  55. * @param {number} val Value to write
  56. * @param {Uint8Array} buf Target buffer
  57. * @param {number} pos Target buffer offset
  58. * @returns {undefined}
  59. */
  60. /**
  61. * Reads a 64 bit double from a buffer using little endian byte order.
  62. * @name util.float.readDoubleLE
  63. * @function
  64. * @param {Uint8Array} buf Source buffer
  65. * @param {number} pos Source buffer offset
  66. * @returns {number} Value read
  67. */
  68. /**
  69. * Reads a 64 bit double from a buffer using big endian byte order.
  70. * @name util.float.readDoubleBE
  71. * @function
  72. * @param {Uint8Array} buf Source buffer
  73. * @param {number} pos Source buffer offset
  74. * @returns {number} Value read
  75. */
  76. // Factory function for the purpose of node-based testing in modified global environments
  77. function factory(exports) {
  78. // float: typed array
  79. if (typeof Float32Array !== "undefined") (function() {
  80. var f32 = new Float32Array([ -0 ]),
  81. f8b = new Uint8Array(f32.buffer),
  82. le = f8b[3] === 128;
  83. function writeFloat_f32_cpy(val, buf, pos) {
  84. f32[0] = val;
  85. buf[pos ] = f8b[0];
  86. buf[pos + 1] = f8b[1];
  87. buf[pos + 2] = f8b[2];
  88. buf[pos + 3] = f8b[3];
  89. }
  90. function writeFloat_f32_rev(val, buf, pos) {
  91. f32[0] = val;
  92. buf[pos ] = f8b[3];
  93. buf[pos + 1] = f8b[2];
  94. buf[pos + 2] = f8b[1];
  95. buf[pos + 3] = f8b[0];
  96. }
  97. /* istanbul ignore next */
  98. exports.writeFloatLE = le ? writeFloat_f32_cpy : writeFloat_f32_rev;
  99. /* istanbul ignore next */
  100. exports.writeFloatBE = le ? writeFloat_f32_rev : writeFloat_f32_cpy;
  101. function readFloat_f32_cpy(buf, pos) {
  102. f8b[0] = buf[pos ];
  103. f8b[1] = buf[pos + 1];
  104. f8b[2] = buf[pos + 2];
  105. f8b[3] = buf[pos + 3];
  106. return f32[0];
  107. }
  108. function readFloat_f32_rev(buf, pos) {
  109. f8b[3] = buf[pos ];
  110. f8b[2] = buf[pos + 1];
  111. f8b[1] = buf[pos + 2];
  112. f8b[0] = buf[pos + 3];
  113. return f32[0];
  114. }
  115. /* istanbul ignore next */
  116. exports.readFloatLE = le ? readFloat_f32_cpy : readFloat_f32_rev;
  117. /* istanbul ignore next */
  118. exports.readFloatBE = le ? readFloat_f32_rev : readFloat_f32_cpy;
  119. // float: ieee754
  120. })(); else (function() {
  121. function writeFloat_ieee754(writeUint, val, buf, pos) {
  122. var sign = val < 0 ? 1 : 0;
  123. if (sign)
  124. val = -val;
  125. if (val === 0)
  126. writeUint(1 / val > 0 ? /* positive */ 0 : /* negative 0 */ 2147483648, buf, pos);
  127. else if (isNaN(val))
  128. writeUint(2143289344, buf, pos);
  129. else if (val > 3.4028234663852886e+38) // +-Infinity
  130. writeUint((sign << 31 | 2139095040) >>> 0, buf, pos);
  131. else if (val < 1.1754943508222875e-38) // denormal
  132. writeUint((sign << 31 | Math.round(val / 1.401298464324817e-45)) >>> 0, buf, pos);
  133. else {
  134. var exponent = Math.floor(Math.log(val) / Math.LN2),
  135. mantissa = Math.round(val * Math.pow(2, -exponent) * 8388608) & 8388607;
  136. writeUint((sign << 31 | exponent + 127 << 23 | mantissa) >>> 0, buf, pos);
  137. }
  138. }
  139. exports.writeFloatLE = writeFloat_ieee754.bind(null, writeUintLE);
  140. exports.writeFloatBE = writeFloat_ieee754.bind(null, writeUintBE);
  141. function readFloat_ieee754(readUint, buf, pos) {
  142. var uint = readUint(buf, pos),
  143. sign = (uint >> 31) * 2 + 1,
  144. exponent = uint >>> 23 & 255,
  145. mantissa = uint & 8388607;
  146. return exponent === 255
  147. ? mantissa
  148. ? NaN
  149. : sign * Infinity
  150. : exponent === 0 // denormal
  151. ? sign * 1.401298464324817e-45 * mantissa
  152. : sign * Math.pow(2, exponent - 150) * (mantissa + 8388608);
  153. }
  154. exports.readFloatLE = readFloat_ieee754.bind(null, readUintLE);
  155. exports.readFloatBE = readFloat_ieee754.bind(null, readUintBE);
  156. })();
  157. // double: typed array
  158. if (typeof Float64Array !== "undefined") (function() {
  159. var f64 = new Float64Array([-0]),
  160. f8b = new Uint8Array(f64.buffer),
  161. le = f8b[7] === 128;
  162. function writeDouble_f64_cpy(val, buf, pos) {
  163. f64[0] = val;
  164. buf[pos ] = f8b[0];
  165. buf[pos + 1] = f8b[1];
  166. buf[pos + 2] = f8b[2];
  167. buf[pos + 3] = f8b[3];
  168. buf[pos + 4] = f8b[4];
  169. buf[pos + 5] = f8b[5];
  170. buf[pos + 6] = f8b[6];
  171. buf[pos + 7] = f8b[7];
  172. }
  173. function writeDouble_f64_rev(val, buf, pos) {
  174. f64[0] = val;
  175. buf[pos ] = f8b[7];
  176. buf[pos + 1] = f8b[6];
  177. buf[pos + 2] = f8b[5];
  178. buf[pos + 3] = f8b[4];
  179. buf[pos + 4] = f8b[3];
  180. buf[pos + 5] = f8b[2];
  181. buf[pos + 6] = f8b[1];
  182. buf[pos + 7] = f8b[0];
  183. }
  184. /* istanbul ignore next */
  185. exports.writeDoubleLE = le ? writeDouble_f64_cpy : writeDouble_f64_rev;
  186. /* istanbul ignore next */
  187. exports.writeDoubleBE = le ? writeDouble_f64_rev : writeDouble_f64_cpy;
  188. function readDouble_f64_cpy(buf, pos) {
  189. f8b[0] = buf[pos ];
  190. f8b[1] = buf[pos + 1];
  191. f8b[2] = buf[pos + 2];
  192. f8b[3] = buf[pos + 3];
  193. f8b[4] = buf[pos + 4];
  194. f8b[5] = buf[pos + 5];
  195. f8b[6] = buf[pos + 6];
  196. f8b[7] = buf[pos + 7];
  197. return f64[0];
  198. }
  199. function readDouble_f64_rev(buf, pos) {
  200. f8b[7] = buf[pos ];
  201. f8b[6] = buf[pos + 1];
  202. f8b[5] = buf[pos + 2];
  203. f8b[4] = buf[pos + 3];
  204. f8b[3] = buf[pos + 4];
  205. f8b[2] = buf[pos + 5];
  206. f8b[1] = buf[pos + 6];
  207. f8b[0] = buf[pos + 7];
  208. return f64[0];
  209. }
  210. /* istanbul ignore next */
  211. exports.readDoubleLE = le ? readDouble_f64_cpy : readDouble_f64_rev;
  212. /* istanbul ignore next */
  213. exports.readDoubleBE = le ? readDouble_f64_rev : readDouble_f64_cpy;
  214. // double: ieee754
  215. })(); else (function() {
  216. function writeDouble_ieee754(writeUint, off0, off1, val, buf, pos) {
  217. var sign = val < 0 ? 1 : 0;
  218. if (sign)
  219. val = -val;
  220. if (val === 0) {
  221. writeUint(0, buf, pos + off0);
  222. writeUint(1 / val > 0 ? /* positive */ 0 : /* negative 0 */ 2147483648, buf, pos + off1);
  223. } else if (isNaN(val)) {
  224. writeUint(0, buf, pos + off0);
  225. writeUint(2146959360, buf, pos + off1);
  226. } else if (val > 1.7976931348623157e+308) { // +-Infinity
  227. writeUint(0, buf, pos + off0);
  228. writeUint((sign << 31 | 2146435072) >>> 0, buf, pos + off1);
  229. } else {
  230. var mantissa;
  231. if (val < 2.2250738585072014e-308) { // denormal
  232. mantissa = val / 5e-324;
  233. writeUint(mantissa >>> 0, buf, pos + off0);
  234. writeUint((sign << 31 | mantissa / 4294967296) >>> 0, buf, pos + off1);
  235. } else {
  236. var exponent = Math.floor(Math.log(val) / Math.LN2);
  237. if (exponent === 1024)
  238. exponent = 1023;
  239. mantissa = val * Math.pow(2, -exponent);
  240. writeUint(mantissa * 4503599627370496 >>> 0, buf, pos + off0);
  241. writeUint((sign << 31 | exponent + 1023 << 20 | mantissa * 1048576 & 1048575) >>> 0, buf, pos + off1);
  242. }
  243. }
  244. }
  245. exports.writeDoubleLE = writeDouble_ieee754.bind(null, writeUintLE, 0, 4);
  246. exports.writeDoubleBE = writeDouble_ieee754.bind(null, writeUintBE, 4, 0);
  247. function readDouble_ieee754(readUint, off0, off1, buf, pos) {
  248. var lo = readUint(buf, pos + off0),
  249. hi = readUint(buf, pos + off1);
  250. var sign = (hi >> 31) * 2 + 1,
  251. exponent = hi >>> 20 & 2047,
  252. mantissa = 4294967296 * (hi & 1048575) + lo;
  253. return exponent === 2047
  254. ? mantissa
  255. ? NaN
  256. : sign * Infinity
  257. : exponent === 0 // denormal
  258. ? sign * 5e-324 * mantissa
  259. : sign * Math.pow(2, exponent - 1075) * (mantissa + 4503599627370496);
  260. }
  261. exports.readDoubleLE = readDouble_ieee754.bind(null, readUintLE, 0, 4);
  262. exports.readDoubleBE = readDouble_ieee754.bind(null, readUintBE, 4, 0);
  263. })();
  264. return exports;
  265. }
  266. // uint helpers
  267. function writeUintLE(val, buf, pos) {
  268. buf[pos ] = val & 255;
  269. buf[pos + 1] = val >>> 8 & 255;
  270. buf[pos + 2] = val >>> 16 & 255;
  271. buf[pos + 3] = val >>> 24;
  272. }
  273. function writeUintBE(val, buf, pos) {
  274. buf[pos ] = val >>> 24;
  275. buf[pos + 1] = val >>> 16 & 255;
  276. buf[pos + 2] = val >>> 8 & 255;
  277. buf[pos + 3] = val & 255;
  278. }
  279. function readUintLE(buf, pos) {
  280. return (buf[pos ]
  281. | buf[pos + 1] << 8
  282. | buf[pos + 2] << 16
  283. | buf[pos + 3] << 24) >>> 0;
  284. }
  285. function readUintBE(buf, pos) {
  286. return (buf[pos ] << 24
  287. | buf[pos + 1] << 16
  288. | buf[pos + 2] << 8
  289. | buf[pos + 3]) >>> 0;
  290. }