long.js 38 KB

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