Long.js 30 KB

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