index.js 40 KB

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