long.cjs.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = Long;
  6. /**
  7. * wasm optimizations, to do native i64 multiplication and divide
  8. */
  9. var wasm = null;
  10. try {
  11. wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11])), {}).exports;
  12. } catch (e) {} // no wasm support :(
  13. /**
  14. * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
  15. * See the from* functions below for more convenient ways of constructing Longs.
  16. * @exports Long
  17. * @class A Long class for representing a 64 bit two's-complement integer value.
  18. * @param {number} low The low (signed) 32 bits of the long
  19. * @param {number} high The high (signed) 32 bits of the long
  20. * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
  21. * @constructor
  22. */
  23. function Long(low, high, unsigned) {
  24. /**
  25. * The low 32 bits as a signed value.
  26. * @type {number}
  27. */
  28. this.low = low | 0;
  29. /**
  30. * The high 32 bits as a signed value.
  31. * @type {number}
  32. */
  33. this.high = high | 0;
  34. /**
  35. * Whether unsigned or not.
  36. * @type {boolean}
  37. */
  38. this.unsigned = !!unsigned;
  39. } // The internal representation of a long is the two given signed, 32-bit values.
  40. // We use 32-bit pieces because these are the size of integers on which
  41. // Javascript performs bit-operations. For operations like addition and
  42. // multiplication, we split each number into 16 bit pieces, which can easily be
  43. // multiplied within Javascript's floating-point representation without overflow
  44. // or change in sign.
  45. //
  46. // In the algorithms below, we frequently reduce the negative case to the
  47. // positive case by negating the input(s) and then post-processing the result.
  48. // Note that we must ALWAYS check specially whether those values are MIN_VALUE
  49. // (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
  50. // a positive number, it overflows back into a negative). Not handling this
  51. // case would often result in infinite recursion.
  52. //
  53. // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from*
  54. // methods on which they depend.
  55. /**
  56. * An indicator used to reliably determine if an object is a Long or not.
  57. * @type {boolean}
  58. * @const
  59. * @private
  60. */
  61. Long.prototype.__isLong__;
  62. Object.defineProperty(Long.prototype, "__isLong__", {
  63. value: true
  64. });
  65. /**
  66. * @function
  67. * @param {*} obj Object
  68. * @returns {boolean}
  69. * @inner
  70. */
  71. function isLong(obj) {
  72. return (obj && obj["__isLong__"]) === true;
  73. }
  74. /**
  75. * Tests if the specified object is a Long.
  76. * @function
  77. * @param {*} obj Object
  78. * @returns {boolean}
  79. */
  80. Long.isLong = isLong;
  81. /**
  82. * A cache of the Long representations of small integer values.
  83. * @type {!Object}
  84. * @inner
  85. */
  86. var INT_CACHE = {};
  87. /**
  88. * A cache of the Long representations of small unsigned integer values.
  89. * @type {!Object}
  90. * @inner
  91. */
  92. var UINT_CACHE = {};
  93. /**
  94. * @param {number} value
  95. * @param {boolean=} unsigned
  96. * @returns {!Long}
  97. * @inner
  98. */
  99. function fromInt(value, unsigned) {
  100. var obj, cachedObj, cache;
  101. if (unsigned) {
  102. value >>>= 0;
  103. if (cache = 0 <= value && value < 256) {
  104. cachedObj = UINT_CACHE[value];
  105. if (cachedObj) return cachedObj;
  106. }
  107. obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true);
  108. if (cache) UINT_CACHE[value] = obj;
  109. return obj;
  110. } else {
  111. value |= 0;
  112. if (cache = -128 <= value && value < 128) {
  113. cachedObj = INT_CACHE[value];
  114. if (cachedObj) return cachedObj;
  115. }
  116. obj = fromBits(value, value < 0 ? -1 : 0, false);
  117. if (cache) INT_CACHE[value] = obj;
  118. return obj;
  119. }
  120. }
  121. /**
  122. * Returns a Long representing the given 32 bit integer value.
  123. * @function
  124. * @param {number} value The 32 bit integer in question
  125. * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
  126. * @returns {!Long} The corresponding Long value
  127. */
  128. Long.fromInt = fromInt;
  129. /**
  130. * @param {number} value
  131. * @param {boolean=} unsigned
  132. * @returns {!Long}
  133. * @inner
  134. */
  135. function fromNumber(value, unsigned) {
  136. if (isNaN(value)) return unsigned ? UZERO : ZERO;
  137. if (unsigned) {
  138. if (value < 0) return UZERO;
  139. if (value >= TWO_PWR_64_DBL) return MAX_UNSIGNED_VALUE;
  140. } else {
  141. if (value <= -TWO_PWR_63_DBL) return MIN_VALUE;
  142. if (value + 1 >= TWO_PWR_63_DBL) return MAX_VALUE;
  143. }
  144. if (value < 0) return fromNumber(-value, unsigned).neg();
  145. return fromBits(value % TWO_PWR_32_DBL | 0, value / TWO_PWR_32_DBL | 0, unsigned);
  146. }
  147. /**
  148. * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
  149. * @function
  150. * @param {number} value The number in question
  151. * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
  152. * @returns {!Long} The corresponding Long value
  153. */
  154. Long.fromNumber = fromNumber;
  155. /**
  156. * @param {number} lowBits
  157. * @param {number} highBits
  158. * @param {boolean=} unsigned
  159. * @returns {!Long}
  160. * @inner
  161. */
  162. function fromBits(lowBits, highBits, unsigned) {
  163. return new Long(lowBits, highBits, unsigned);
  164. }
  165. /**
  166. * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is
  167. * assumed to use 32 bits.
  168. * @function
  169. * @param {number} lowBits The low 32 bits
  170. * @param {number} highBits The high 32 bits
  171. * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
  172. * @returns {!Long} The corresponding Long value
  173. */
  174. Long.fromBits = fromBits;
  175. /**
  176. * @function
  177. * @param {number} base
  178. * @param {number} exponent
  179. * @returns {number}
  180. * @inner
  181. */
  182. var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4)
  183. /**
  184. * @param {string} str
  185. * @param {(boolean|number)=} unsigned
  186. * @param {number=} radix
  187. * @returns {!Long}
  188. * @inner
  189. */
  190. function fromString(str, unsigned, radix) {
  191. if (str.length === 0) throw Error('empty string');
  192. if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") return ZERO;
  193. if (typeof unsigned === 'number') {
  194. // For goog.math.long compatibility
  195. radix = unsigned, unsigned = false;
  196. } else {
  197. unsigned = !!unsigned;
  198. }
  199. radix = radix || 10;
  200. if (radix < 2 || 36 < radix) throw RangeError('radix');
  201. var p;
  202. if ((p = str.indexOf('-')) > 0) throw Error('interior hyphen');else if (p === 0) {
  203. return fromString(str.substring(1), unsigned, radix).neg();
  204. } // Do several (8) digits each time through the loop, so as to
  205. // minimize the calls to the very expensive emulated div.
  206. var radixToPower = fromNumber(pow_dbl(radix, 8));
  207. var result = ZERO;
  208. for (var i = 0; i < str.length; i += 8) {
  209. var size = Math.min(8, str.length - i),
  210. value = parseInt(str.substring(i, i + size), radix);
  211. if (size < 8) {
  212. var power = fromNumber(pow_dbl(radix, size));
  213. result = result.mul(power).add(fromNumber(value));
  214. } else {
  215. result = result.mul(radixToPower);
  216. result = result.add(fromNumber(value));
  217. }
  218. }
  219. result.unsigned = unsigned;
  220. return result;
  221. }
  222. /**
  223. * Returns a Long representation of the given string, written using the specified radix.
  224. * @function
  225. * @param {string} str The textual representation of the Long
  226. * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to signed
  227. * @param {number=} radix The radix in which the text is written (2-36), defaults to 10
  228. * @returns {!Long} The corresponding Long value
  229. */
  230. Long.fromString = fromString;
  231. /**
  232. * @function
  233. * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val
  234. * @param {boolean=} unsigned
  235. * @returns {!Long}
  236. * @inner
  237. */
  238. function fromValue(val, unsigned) {
  239. if (typeof val === 'number') return fromNumber(val, unsigned);
  240. if (typeof val === 'string') return fromString(val, unsigned); // Throws for non-objects, converts non-instanceof Long:
  241. return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned);
  242. }
  243. /**
  244. * Converts the specified value to a Long using the appropriate from* function for its type.
  245. * @function
  246. * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value
  247. * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
  248. * @returns {!Long}
  249. */
  250. Long.fromValue = fromValue; // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be
  251. // no runtime penalty for these.
  252. /**
  253. * @type {number}
  254. * @const
  255. * @inner
  256. */
  257. var TWO_PWR_16_DBL = 1 << 16;
  258. /**
  259. * @type {number}
  260. * @const
  261. * @inner
  262. */
  263. var TWO_PWR_24_DBL = 1 << 24;
  264. /**
  265. * @type {number}
  266. * @const
  267. * @inner
  268. */
  269. var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
  270. /**
  271. * @type {number}
  272. * @const
  273. * @inner
  274. */
  275. var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
  276. /**
  277. * @type {number}
  278. * @const
  279. * @inner
  280. */
  281. var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
  282. /**
  283. * @type {!Long}
  284. * @const
  285. * @inner
  286. */
  287. var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL);
  288. /**
  289. * @type {!Long}
  290. * @inner
  291. */
  292. var ZERO = fromInt(0);
  293. /**
  294. * Signed zero.
  295. * @type {!Long}
  296. */
  297. Long.ZERO = ZERO;
  298. /**
  299. * @type {!Long}
  300. * @inner
  301. */
  302. var UZERO = fromInt(0, true);
  303. /**
  304. * Unsigned zero.
  305. * @type {!Long}
  306. */
  307. Long.UZERO = UZERO;
  308. /**
  309. * @type {!Long}
  310. * @inner
  311. */
  312. var ONE = fromInt(1);
  313. /**
  314. * Signed one.
  315. * @type {!Long}
  316. */
  317. Long.ONE = ONE;
  318. /**
  319. * @type {!Long}
  320. * @inner
  321. */
  322. var UONE = fromInt(1, true);
  323. /**
  324. * Unsigned one.
  325. * @type {!Long}
  326. */
  327. Long.UONE = UONE;
  328. /**
  329. * @type {!Long}
  330. * @inner
  331. */
  332. var NEG_ONE = fromInt(-1);
  333. /**
  334. * Signed negative one.
  335. * @type {!Long}
  336. */
  337. Long.NEG_ONE = NEG_ONE;
  338. /**
  339. * @type {!Long}
  340. * @inner
  341. */
  342. var MAX_VALUE = fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0, false);
  343. /**
  344. * Maximum signed value.
  345. * @type {!Long}
  346. */
  347. Long.MAX_VALUE = MAX_VALUE;
  348. /**
  349. * @type {!Long}
  350. * @inner
  351. */
  352. var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF | 0, 0xFFFFFFFF | 0, true);
  353. /**
  354. * Maximum unsigned value.
  355. * @type {!Long}
  356. */
  357. Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE;
  358. /**
  359. * @type {!Long}
  360. * @inner
  361. */
  362. var MIN_VALUE = fromBits(0, 0x80000000 | 0, false);
  363. /**
  364. * Minimum signed value.
  365. * @type {!Long}
  366. */
  367. Long.MIN_VALUE = MIN_VALUE;
  368. /**
  369. * @alias Long.prototype
  370. * @inner
  371. */
  372. var LongPrototype = Long.prototype;
  373. /**
  374. * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
  375. * @this {!Long}
  376. * @returns {number}
  377. */
  378. LongPrototype.toInt = function toInt() {
  379. return this.unsigned ? this.low >>> 0 : this.low;
  380. };
  381. /**
  382. * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
  383. * @this {!Long}
  384. * @returns {number}
  385. */
  386. LongPrototype.toNumber = function toNumber() {
  387. if (this.unsigned) return (this.high >>> 0) * TWO_PWR_32_DBL + (this.low >>> 0);
  388. return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
  389. };
  390. /**
  391. * Converts the Long to a string written in the specified radix.
  392. * @this {!Long}
  393. * @param {number=} radix Radix (2-36), defaults to 10
  394. * @returns {string}
  395. * @override
  396. * @throws {RangeError} If `radix` is out of range
  397. */
  398. LongPrototype.toString = function toString(radix) {
  399. radix = radix || 10;
  400. if (radix < 2 || 36 < radix) throw RangeError('radix');
  401. if (this.isZero()) return '0';
  402. if (this.isNegative()) {
  403. // Unsigned Longs are never negative
  404. if (this.eq(MIN_VALUE)) {
  405. // We need to change the Long value before it can be negated, so we remove
  406. // the bottom-most digit in this base and then recurse to do the rest.
  407. var radixLong = fromNumber(radix),
  408. div = this.div(radixLong),
  409. rem1 = div.mul(radixLong).sub(this);
  410. return div.toString(radix) + rem1.toInt().toString(radix);
  411. } else return '-' + this.neg().toString(radix);
  412. } // Do several (6) digits each time through the loop, so as to
  413. // minimize the calls to the very expensive emulated div.
  414. var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned),
  415. rem = this;
  416. var result = '';
  417. while (true) {
  418. var remDiv = rem.div(radixToPower),
  419. intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0,
  420. digits = intval.toString(radix);
  421. rem = remDiv;
  422. if (rem.isZero()) return digits + result;else {
  423. while (digits.length < 6) digits = '0' + digits;
  424. result = '' + digits + result;
  425. }
  426. }
  427. };
  428. /**
  429. * Gets the high 32 bits as a signed integer.
  430. * @this {!Long}
  431. * @returns {number} Signed high bits
  432. */
  433. LongPrototype.getHighBits = function getHighBits() {
  434. return this.high;
  435. };
  436. /**
  437. * Gets the high 32 bits as an unsigned integer.
  438. * @this {!Long}
  439. * @returns {number} Unsigned high bits
  440. */
  441. LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() {
  442. return this.high >>> 0;
  443. };
  444. /**
  445. * Gets the low 32 bits as a signed integer.
  446. * @this {!Long}
  447. * @returns {number} Signed low bits
  448. */
  449. LongPrototype.getLowBits = function getLowBits() {
  450. return this.low;
  451. };
  452. /**
  453. * Gets the low 32 bits as an unsigned integer.
  454. * @this {!Long}
  455. * @returns {number} Unsigned low bits
  456. */
  457. LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() {
  458. return this.low >>> 0;
  459. };
  460. /**
  461. * Gets the number of bits needed to represent the absolute value of this Long.
  462. * @this {!Long}
  463. * @returns {number}
  464. */
  465. LongPrototype.getNumBitsAbs = function getNumBitsAbs() {
  466. if (this.isNegative()) // Unsigned Longs are never negative
  467. return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
  468. var val = this.high != 0 ? this.high : this.low;
  469. for (var bit = 31; bit > 0; bit--) if ((val & 1 << bit) != 0) break;
  470. return this.high != 0 ? bit + 33 : bit + 1;
  471. };
  472. /**
  473. * Tests if this Long's value equals zero.
  474. * @this {!Long}
  475. * @returns {boolean}
  476. */
  477. LongPrototype.isZero = function isZero() {
  478. return this.high === 0 && this.low === 0;
  479. };
  480. /**
  481. * Tests if this Long's value equals zero. This is an alias of {@link Long#isZero}.
  482. * @returns {boolean}
  483. */
  484. LongPrototype.eqz = LongPrototype.isZero;
  485. /**
  486. * Tests if this Long's value is negative.
  487. * @this {!Long}
  488. * @returns {boolean}
  489. */
  490. LongPrototype.isNegative = function isNegative() {
  491. return !this.unsigned && this.high < 0;
  492. };
  493. /**
  494. * Tests if this Long's value is positive.
  495. * @this {!Long}
  496. * @returns {boolean}
  497. */
  498. LongPrototype.isPositive = function isPositive() {
  499. return this.unsigned || this.high >= 0;
  500. };
  501. /**
  502. * Tests if this Long's value is odd.
  503. * @this {!Long}
  504. * @returns {boolean}
  505. */
  506. LongPrototype.isOdd = function isOdd() {
  507. return (this.low & 1) === 1;
  508. };
  509. /**
  510. * Tests if this Long's value is even.
  511. * @this {!Long}
  512. * @returns {boolean}
  513. */
  514. LongPrototype.isEven = function isEven() {
  515. return (this.low & 1) === 0;
  516. };
  517. /**
  518. * Tests if this Long's value equals the specified's.
  519. * @this {!Long}
  520. * @param {!Long|number|string} other Other value
  521. * @returns {boolean}
  522. */
  523. LongPrototype.equals = function equals(other) {
  524. if (!isLong(other)) other = fromValue(other);
  525. if (this.unsigned !== other.unsigned && this.high >>> 31 === 1 && other.high >>> 31 === 1) return false;
  526. return this.high === other.high && this.low === other.low;
  527. };
  528. /**
  529. * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}.
  530. * @function
  531. * @param {!Long|number|string} other Other value
  532. * @returns {boolean}
  533. */
  534. LongPrototype.eq = LongPrototype.equals;
  535. /**
  536. * Tests if this Long's value differs from the specified's.
  537. * @this {!Long}
  538. * @param {!Long|number|string} other Other value
  539. * @returns {boolean}
  540. */
  541. LongPrototype.notEquals = function notEquals(other) {
  542. return !this.eq(
  543. /* validates */
  544. other);
  545. };
  546. /**
  547. * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
  548. * @function
  549. * @param {!Long|number|string} other Other value
  550. * @returns {boolean}
  551. */
  552. LongPrototype.neq = LongPrototype.notEquals;
  553. /**
  554. * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
  555. * @function
  556. * @param {!Long|number|string} other Other value
  557. * @returns {boolean}
  558. */
  559. LongPrototype.ne = LongPrototype.notEquals;
  560. /**
  561. * Tests if this Long's value is less than the specified's.
  562. * @this {!Long}
  563. * @param {!Long|number|string} other Other value
  564. * @returns {boolean}
  565. */
  566. LongPrototype.lessThan = function lessThan(other) {
  567. return this.comp(
  568. /* validates */
  569. other) < 0;
  570. };
  571. /**
  572. * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}.
  573. * @function
  574. * @param {!Long|number|string} other Other value
  575. * @returns {boolean}
  576. */
  577. LongPrototype.lt = LongPrototype.lessThan;
  578. /**
  579. * Tests if this Long's value is less than or equal the specified's.
  580. * @this {!Long}
  581. * @param {!Long|number|string} other Other value
  582. * @returns {boolean}
  583. */
  584. LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) {
  585. return this.comp(
  586. /* validates */
  587. other) <= 0;
  588. };
  589. /**
  590. * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
  591. * @function
  592. * @param {!Long|number|string} other Other value
  593. * @returns {boolean}
  594. */
  595. LongPrototype.lte = LongPrototype.lessThanOrEqual;
  596. /**
  597. * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
  598. * @function
  599. * @param {!Long|number|string} other Other value
  600. * @returns {boolean}
  601. */
  602. LongPrototype.le = LongPrototype.lessThanOrEqual;
  603. /**
  604. * Tests if this Long's value is greater than the specified's.
  605. * @this {!Long}
  606. * @param {!Long|number|string} other Other value
  607. * @returns {boolean}
  608. */
  609. LongPrototype.greaterThan = function greaterThan(other) {
  610. return this.comp(
  611. /* validates */
  612. other) > 0;
  613. };
  614. /**
  615. * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}.
  616. * @function
  617. * @param {!Long|number|string} other Other value
  618. * @returns {boolean}
  619. */
  620. LongPrototype.gt = LongPrototype.greaterThan;
  621. /**
  622. * Tests if this Long's value is greater than or equal the specified's.
  623. * @this {!Long}
  624. * @param {!Long|number|string} other Other value
  625. * @returns {boolean}
  626. */
  627. LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) {
  628. return this.comp(
  629. /* validates */
  630. other) >= 0;
  631. };
  632. /**
  633. * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
  634. * @function
  635. * @param {!Long|number|string} other Other value
  636. * @returns {boolean}
  637. */
  638. LongPrototype.gte = LongPrototype.greaterThanOrEqual;
  639. /**
  640. * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
  641. * @function
  642. * @param {!Long|number|string} other Other value
  643. * @returns {boolean}
  644. */
  645. LongPrototype.ge = LongPrototype.greaterThanOrEqual;
  646. /**
  647. * Compares this Long's value with the specified's.
  648. * @this {!Long}
  649. * @param {!Long|number|string} other Other value
  650. * @returns {number} 0 if they are the same, 1 if the this is greater and -1
  651. * if the given one is greater
  652. */
  653. LongPrototype.compare = function compare(other) {
  654. if (!isLong(other)) other = fromValue(other);
  655. if (this.eq(other)) return 0;
  656. var thisNeg = this.isNegative(),
  657. otherNeg = other.isNegative();
  658. if (thisNeg && !otherNeg) return -1;
  659. if (!thisNeg && otherNeg) return 1; // At this point the sign bits are the same
  660. if (!this.unsigned) return this.sub(other).isNegative() ? -1 : 1; // Both are positive if at least one is unsigned
  661. return other.high >>> 0 > this.high >>> 0 || other.high === this.high && other.low >>> 0 > this.low >>> 0 ? -1 : 1;
  662. };
  663. /**
  664. * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}.
  665. * @function
  666. * @param {!Long|number|string} other Other value
  667. * @returns {number} 0 if they are the same, 1 if the this is greater and -1
  668. * if the given one is greater
  669. */
  670. LongPrototype.comp = LongPrototype.compare;
  671. /**
  672. * Negates this Long's value.
  673. * @this {!Long}
  674. * @returns {!Long} Negated Long
  675. */
  676. LongPrototype.negate = function negate() {
  677. if (!this.unsigned && this.eq(MIN_VALUE)) return MIN_VALUE;
  678. return this.not().add(ONE);
  679. };
  680. /**
  681. * Negates this Long's value. This is an alias of {@link Long#negate}.
  682. * @function
  683. * @returns {!Long} Negated Long
  684. */
  685. LongPrototype.neg = LongPrototype.negate;
  686. /**
  687. * Returns the sum of this and the specified Long.
  688. * @this {!Long}
  689. * @param {!Long|number|string} addend Addend
  690. * @returns {!Long} Sum
  691. */
  692. LongPrototype.add = function add(addend) {
  693. if (!isLong(addend)) addend = fromValue(addend); // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
  694. var a48 = this.high >>> 16;
  695. var a32 = this.high & 0xFFFF;
  696. var a16 = this.low >>> 16;
  697. var a00 = this.low & 0xFFFF;
  698. var b48 = addend.high >>> 16;
  699. var b32 = addend.high & 0xFFFF;
  700. var b16 = addend.low >>> 16;
  701. var b00 = addend.low & 0xFFFF;
  702. var c48 = 0,
  703. c32 = 0,
  704. c16 = 0,
  705. c00 = 0;
  706. c00 += a00 + b00;
  707. c16 += c00 >>> 16;
  708. c00 &= 0xFFFF;
  709. c16 += a16 + b16;
  710. c32 += c16 >>> 16;
  711. c16 &= 0xFFFF;
  712. c32 += a32 + b32;
  713. c48 += c32 >>> 16;
  714. c32 &= 0xFFFF;
  715. c48 += a48 + b48;
  716. c48 &= 0xFFFF;
  717. return fromBits(c16 << 16 | c00, c48 << 16 | c32, this.unsigned);
  718. };
  719. /**
  720. * Returns the difference of this and the specified Long.
  721. * @this {!Long}
  722. * @param {!Long|number|string} subtrahend Subtrahend
  723. * @returns {!Long} Difference
  724. */
  725. LongPrototype.subtract = function subtract(subtrahend) {
  726. if (!isLong(subtrahend)) subtrahend = fromValue(subtrahend);
  727. return this.add(subtrahend.neg());
  728. };
  729. /**
  730. * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}.
  731. * @function
  732. * @param {!Long|number|string} subtrahend Subtrahend
  733. * @returns {!Long} Difference
  734. */
  735. LongPrototype.sub = LongPrototype.subtract;
  736. /**
  737. * Returns the product of this and the specified Long.
  738. * @this {!Long}
  739. * @param {!Long|number|string} multiplier Multiplier
  740. * @returns {!Long} Product
  741. */
  742. LongPrototype.multiply = function multiply(multiplier) {
  743. if (this.isZero()) return ZERO;
  744. if (!isLong(multiplier)) multiplier = fromValue(multiplier); // use wasm support if present
  745. if (wasm) {
  746. var low = wasm["mul"](this.low, this.high, multiplier.low, multiplier.high);
  747. return fromBits(low, wasm["get_high"](), this.unsigned);
  748. }
  749. if (multiplier.isZero()) return ZERO;
  750. if (this.eq(MIN_VALUE)) return multiplier.isOdd() ? MIN_VALUE : ZERO;
  751. if (multiplier.eq(MIN_VALUE)) return this.isOdd() ? MIN_VALUE : ZERO;
  752. if (this.isNegative()) {
  753. if (multiplier.isNegative()) return this.neg().mul(multiplier.neg());else return this.neg().mul(multiplier).neg();
  754. } else if (multiplier.isNegative()) return this.mul(multiplier.neg()).neg(); // If both longs are small, use float multiplication
  755. if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24)) return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
  756. // We can skip products that would overflow.
  757. var a48 = this.high >>> 16;
  758. var a32 = this.high & 0xFFFF;
  759. var a16 = this.low >>> 16;
  760. var a00 = this.low & 0xFFFF;
  761. var b48 = multiplier.high >>> 16;
  762. var b32 = multiplier.high & 0xFFFF;
  763. var b16 = multiplier.low >>> 16;
  764. var b00 = multiplier.low & 0xFFFF;
  765. var c48 = 0,
  766. c32 = 0,
  767. c16 = 0,
  768. c00 = 0;
  769. c00 += a00 * b00;
  770. c16 += c00 >>> 16;
  771. c00 &= 0xFFFF;
  772. c16 += a16 * b00;
  773. c32 += c16 >>> 16;
  774. c16 &= 0xFFFF;
  775. c16 += a00 * b16;
  776. c32 += c16 >>> 16;
  777. c16 &= 0xFFFF;
  778. c32 += a32 * b00;
  779. c48 += c32 >>> 16;
  780. c32 &= 0xFFFF;
  781. c32 += a16 * b16;
  782. c48 += c32 >>> 16;
  783. c32 &= 0xFFFF;
  784. c32 += a00 * b32;
  785. c48 += c32 >>> 16;
  786. c32 &= 0xFFFF;
  787. c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
  788. c48 &= 0xFFFF;
  789. return fromBits(c16 << 16 | c00, c48 << 16 | c32, this.unsigned);
  790. };
  791. /**
  792. * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}.
  793. * @function
  794. * @param {!Long|number|string} multiplier Multiplier
  795. * @returns {!Long} Product
  796. */
  797. LongPrototype.mul = LongPrototype.multiply;
  798. /**
  799. * Returns this Long divided by the specified. The result is signed if this Long is signed or
  800. * unsigned if this Long is unsigned.
  801. * @this {!Long}
  802. * @param {!Long|number|string} divisor Divisor
  803. * @returns {!Long} Quotient
  804. */
  805. LongPrototype.divide = function divide(divisor) {
  806. if (!isLong(divisor)) divisor = fromValue(divisor);
  807. if (divisor.isZero()) throw Error('division by zero'); // use wasm support if present
  808. if (wasm) {
  809. // guard against signed division overflow: the largest
  810. // negative number / -1 would be 1 larger than the largest
  811. // positive number, due to two's complement.
  812. if (!this.unsigned && this.high === -0x80000000 && divisor.low === -1 && divisor.high === -1) {
  813. // be consistent with non-wasm code path
  814. return this;
  815. }
  816. var low = (this.unsigned ? wasm["div_u"] : wasm["div_s"])(this.low, this.high, divisor.low, divisor.high);
  817. return fromBits(low, wasm["get_high"](), this.unsigned);
  818. }
  819. if (this.isZero()) return this.unsigned ? UZERO : ZERO;
  820. var approx, rem, res;
  821. if (!this.unsigned) {
  822. // This section is only relevant for signed longs and is derived from the
  823. // closure library as a whole.
  824. if (this.eq(MIN_VALUE)) {
  825. if (divisor.eq(ONE) || divisor.eq(NEG_ONE)) return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
  826. else if (divisor.eq(MIN_VALUE)) return ONE;else {
  827. // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
  828. var halfThis = this.shr(1);
  829. approx = halfThis.div(divisor).shl(1);
  830. if (approx.eq(ZERO)) {
  831. return divisor.isNegative() ? ONE : NEG_ONE;
  832. } else {
  833. rem = this.sub(divisor.mul(approx));
  834. res = approx.add(rem.div(divisor));
  835. return res;
  836. }
  837. }
  838. } else if (divisor.eq(MIN_VALUE)) return this.unsigned ? UZERO : ZERO;
  839. if (this.isNegative()) {
  840. if (divisor.isNegative()) return this.neg().div(divisor.neg());
  841. return this.neg().div(divisor).neg();
  842. } else if (divisor.isNegative()) return this.div(divisor.neg()).neg();
  843. res = ZERO;
  844. } else {
  845. // The algorithm below has not been made for unsigned longs. It's therefore
  846. // required to take special care of the MSB prior to running it.
  847. if (!divisor.unsigned) divisor = divisor.toUnsigned();
  848. if (divisor.gt(this)) return UZERO;
  849. if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true
  850. return UONE;
  851. res = UZERO;
  852. } // Repeat the following until the remainder is less than other: find a
  853. // floating-point that approximates remainder / other *from below*, add this
  854. // into the result, and subtract it from the remainder. It is critical that
  855. // the approximate value is less than or equal to the real value so that the
  856. // remainder never becomes negative.
  857. rem = this;
  858. while (rem.gte(divisor)) {
  859. // Approximate the result of division. This may be a little greater or
  860. // smaller than the actual value.
  861. approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber())); // We will tweak the approximate result by changing it in the 48-th digit or
  862. // the smallest non-fractional digit, whichever is larger.
  863. var log2 = Math.ceil(Math.log(approx) / Math.LN2),
  864. delta = log2 <= 48 ? 1 : pow_dbl(2, log2 - 48),
  865. // Decrease the approximation until it is smaller than the remainder. Note
  866. // that if it is too large, the product overflows and is negative.
  867. approxRes = fromNumber(approx),
  868. approxRem = approxRes.mul(divisor);
  869. while (approxRem.isNegative() || approxRem.gt(rem)) {
  870. approx -= delta;
  871. approxRes = fromNumber(approx, this.unsigned);
  872. approxRem = approxRes.mul(divisor);
  873. } // We know the answer can't be zero... and actually, zero would cause
  874. // infinite recursion since we would make no progress.
  875. if (approxRes.isZero()) approxRes = ONE;
  876. res = res.add(approxRes);
  877. rem = rem.sub(approxRem);
  878. }
  879. return res;
  880. };
  881. /**
  882. * Returns this Long divided by the specified. This is an alias of {@link Long#divide}.
  883. * @function
  884. * @param {!Long|number|string} divisor Divisor
  885. * @returns {!Long} Quotient
  886. */
  887. LongPrototype.div = LongPrototype.divide;
  888. /**
  889. * Returns this Long modulo the specified.
  890. * @this {!Long}
  891. * @param {!Long|number|string} divisor Divisor
  892. * @returns {!Long} Remainder
  893. */
  894. LongPrototype.modulo = function modulo(divisor) {
  895. if (!isLong(divisor)) divisor = fromValue(divisor); // use wasm support if present
  896. if (wasm) {
  897. var low = (this.unsigned ? wasm["rem_u"] : wasm["rem_s"])(this.low, this.high, divisor.low, divisor.high);
  898. return fromBits(low, wasm["get_high"](), this.unsigned);
  899. }
  900. return this.sub(this.div(divisor).mul(divisor));
  901. };
  902. /**
  903. * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
  904. * @function
  905. * @param {!Long|number|string} divisor Divisor
  906. * @returns {!Long} Remainder
  907. */
  908. LongPrototype.mod = LongPrototype.modulo;
  909. /**
  910. * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
  911. * @function
  912. * @param {!Long|number|string} divisor Divisor
  913. * @returns {!Long} Remainder
  914. */
  915. LongPrototype.rem = LongPrototype.modulo;
  916. /**
  917. * Returns the bitwise NOT of this Long.
  918. * @this {!Long}
  919. * @returns {!Long}
  920. */
  921. LongPrototype.not = function not() {
  922. return fromBits(~this.low, ~this.high, this.unsigned);
  923. };
  924. /**
  925. * Returns the bitwise AND of this Long and the specified.
  926. * @this {!Long}
  927. * @param {!Long|number|string} other Other Long
  928. * @returns {!Long}
  929. */
  930. LongPrototype.and = function and(other) {
  931. if (!isLong(other)) other = fromValue(other);
  932. return fromBits(this.low & other.low, this.high & other.high, this.unsigned);
  933. };
  934. /**
  935. * Returns the bitwise OR of this Long and the specified.
  936. * @this {!Long}
  937. * @param {!Long|number|string} other Other Long
  938. * @returns {!Long}
  939. */
  940. LongPrototype.or = function or(other) {
  941. if (!isLong(other)) other = fromValue(other);
  942. return fromBits(this.low | other.low, this.high | other.high, this.unsigned);
  943. };
  944. /**
  945. * Returns the bitwise XOR of this Long and the given one.
  946. * @this {!Long}
  947. * @param {!Long|number|string} other Other Long
  948. * @returns {!Long}
  949. */
  950. LongPrototype.xor = function xor(other) {
  951. if (!isLong(other)) other = fromValue(other);
  952. return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
  953. };
  954. /**
  955. * Returns this Long with bits shifted to the left by the given amount.
  956. * @this {!Long}
  957. * @param {number|!Long} numBits Number of bits
  958. * @returns {!Long} Shifted Long
  959. */
  960. LongPrototype.shiftLeft = function shiftLeft(numBits) {
  961. if (isLong(numBits)) numBits = numBits.toInt();
  962. if ((numBits &= 63) === 0) return this;else if (numBits < 32) return fromBits(this.low << numBits, this.high << numBits | this.low >>> 32 - numBits, this.unsigned);else return fromBits(0, this.low << numBits - 32, this.unsigned);
  963. };
  964. /**
  965. * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}.
  966. * @function
  967. * @param {number|!Long} numBits Number of bits
  968. * @returns {!Long} Shifted Long
  969. */
  970. LongPrototype.shl = LongPrototype.shiftLeft;
  971. /**
  972. * Returns this Long with bits arithmetically shifted to the right by the given amount.
  973. * @this {!Long}
  974. * @param {number|!Long} numBits Number of bits
  975. * @returns {!Long} Shifted Long
  976. */
  977. LongPrototype.shiftRight = function shiftRight(numBits) {
  978. if (isLong(numBits)) numBits = numBits.toInt();
  979. if ((numBits &= 63) === 0) return this;else if (numBits < 32) return fromBits(this.low >>> numBits | this.high << 32 - numBits, this.high >> numBits, this.unsigned);else return fromBits(this.high >> numBits - 32, this.high >= 0 ? 0 : -1, this.unsigned);
  980. };
  981. /**
  982. * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}.
  983. * @function
  984. * @param {number|!Long} numBits Number of bits
  985. * @returns {!Long} Shifted Long
  986. */
  987. LongPrototype.shr = LongPrototype.shiftRight;
  988. /**
  989. * Returns this Long with bits logically shifted to the right by the given amount.
  990. * @this {!Long}
  991. * @param {number|!Long} numBits Number of bits
  992. * @returns {!Long} Shifted Long
  993. */
  994. LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) {
  995. if (isLong(numBits)) numBits = numBits.toInt();
  996. numBits &= 63;
  997. if (numBits === 0) return this;else {
  998. var high = this.high;
  999. if (numBits < 32) {
  1000. var low = this.low;
  1001. return fromBits(low >>> numBits | high << 32 - numBits, high >>> numBits, this.unsigned);
  1002. } else if (numBits === 32) return fromBits(high, 0, this.unsigned);else return fromBits(high >>> numBits - 32, 0, this.unsigned);
  1003. }
  1004. };
  1005. /**
  1006. * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
  1007. * @function
  1008. * @param {number|!Long} numBits Number of bits
  1009. * @returns {!Long} Shifted Long
  1010. */
  1011. LongPrototype.shru = LongPrototype.shiftRightUnsigned;
  1012. /**
  1013. * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
  1014. * @function
  1015. * @param {number|!Long} numBits Number of bits
  1016. * @returns {!Long} Shifted Long
  1017. */
  1018. LongPrototype.shr_u = LongPrototype.shiftRightUnsigned;
  1019. /**
  1020. * Converts this Long to signed.
  1021. * @this {!Long}
  1022. * @returns {!Long} Signed long
  1023. */
  1024. LongPrototype.toSigned = function toSigned() {
  1025. if (!this.unsigned) return this;
  1026. return fromBits(this.low, this.high, false);
  1027. };
  1028. /**
  1029. * Converts this Long to unsigned.
  1030. * @this {!Long}
  1031. * @returns {!Long} Unsigned long
  1032. */
  1033. LongPrototype.toUnsigned = function toUnsigned() {
  1034. if (this.unsigned) return this;
  1035. return fromBits(this.low, this.high, true);
  1036. };
  1037. /**
  1038. * Converts this Long to its byte representation.
  1039. * @param {boolean=} le Whether little or big endian, defaults to big endian
  1040. * @this {!Long}
  1041. * @returns {!Array.<number>} Byte representation
  1042. */
  1043. LongPrototype.toBytes = function toBytes(le) {
  1044. return le ? this.toBytesLE() : this.toBytesBE();
  1045. };
  1046. /**
  1047. * Converts this Long to its little endian byte representation.
  1048. * @this {!Long}
  1049. * @returns {!Array.<number>} Little endian byte representation
  1050. */
  1051. LongPrototype.toBytesLE = function toBytesLE() {
  1052. var hi = this.high,
  1053. lo = this.low;
  1054. return [lo & 0xff, lo >>> 8 & 0xff, lo >>> 16 & 0xff, lo >>> 24, hi & 0xff, hi >>> 8 & 0xff, hi >>> 16 & 0xff, hi >>> 24];
  1055. };
  1056. /**
  1057. * Converts this Long to its big endian byte representation.
  1058. * @this {!Long}
  1059. * @returns {!Array.<number>} Big endian byte representation
  1060. */
  1061. LongPrototype.toBytesBE = function toBytesBE() {
  1062. var hi = this.high,
  1063. lo = this.low;
  1064. return [hi >>> 24, hi >>> 16 & 0xff, hi >>> 8 & 0xff, hi & 0xff, lo >>> 24, lo >>> 16 & 0xff, lo >>> 8 & 0xff, lo & 0xff];
  1065. };
  1066. /**
  1067. * Creates a Long from its byte representation.
  1068. * @param {!Array.<number>} bytes Byte representation
  1069. * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
  1070. * @param {boolean=} le Whether little or big endian, defaults to big endian
  1071. * @returns {Long} The corresponding Long value
  1072. */
  1073. Long.fromBytes = function fromBytes(bytes, unsigned, le) {
  1074. return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned);
  1075. };
  1076. /**
  1077. * Creates a Long from its little endian byte representation.
  1078. * @param {!Array.<number>} bytes Little endian byte representation
  1079. * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
  1080. * @returns {Long} The corresponding Long value
  1081. */
  1082. Long.fromBytesLE = function fromBytesLE(bytes, unsigned) {
  1083. return new Long(bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24, bytes[4] | bytes[5] << 8 | bytes[6] << 16 | bytes[7] << 24, unsigned);
  1084. };
  1085. /**
  1086. * Creates a Long from its big endian byte representation.
  1087. * @param {!Array.<number>} bytes Big endian byte representation
  1088. * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
  1089. * @returns {Long} The corresponding Long value
  1090. */
  1091. Long.fromBytesBE = function fromBytesBE(bytes, unsigned) {
  1092. return new Long(bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7], bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], unsigned);
  1093. };