long.js 38 KB

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