long.js 36 KB

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