index.js 43 KB

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