index.js 50 KB

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