long.js 40 KB

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