long.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. // Licensed under the Apache License, Version 2.0 (the "License");
  2. // you may not use this file except in compliance with the License.
  3. // You may obtain a copy of the License at
  4. //
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Unless required by applicable law or agreed to in writing, software
  8. // distributed under the License is distributed on an "AS IS" BASIS,
  9. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. // See the License for the specific language governing permissions and
  11. // limitations under the License.
  12. //
  13. // Copyright 2009 Google Inc. All Rights Reserved
  14. /**
  15. * Defines a Long class for representing a 64-bit two's-complement
  16. * integer value, which faithfully simulates the behavior of a Java "Long". This
  17. * implementation is derived from LongLib in GWT.
  18. *
  19. * Constructs a 64-bit two's-complement integer, given its low and high 32-bit
  20. * values as *signed* integers. See the from* functions below for more
  21. * convenient ways of constructing Longs.
  22. *
  23. * The internal representation of a Long is the two given signed, 32-bit values.
  24. * We use 32-bit pieces because these are the size of integers on which
  25. * Javascript performs bit-operations. For operations like addition and
  26. * multiplication, we split each number into 16-bit pieces, which can easily be
  27. * multiplied within Javascript's floating-point representation without overflow
  28. * or change in sign.
  29. *
  30. * In the algorithms below, we frequently reduce the negative case to the
  31. * positive case by negating the input(s) and then post-processing the result.
  32. * Note that we must ALWAYS check specially whether those values are MIN_VALUE
  33. * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
  34. * a positive number, it overflows back into a negative). Not handling this
  35. * case would often result in infinite recursion.
  36. *
  37. * @class
  38. * @param {number} low the low (signed) 32 bits of the Long.
  39. * @param {number} high the high (signed) 32 bits of the Long.
  40. * @return {Long}
  41. */
  42. function Long(low, high) {
  43. if(!(this instanceof Long)) return new Long(low, high);
  44. this._bsontype = 'Long';
  45. /**
  46. * @type {number}
  47. * @ignore
  48. */
  49. this.low_ = low | 0; // force into 32 signed bits.
  50. /**
  51. * @type {number}
  52. * @ignore
  53. */
  54. this.high_ = high | 0; // force into 32 signed bits.
  55. };
  56. /**
  57. * Return the int value.
  58. *
  59. * @method
  60. * @return {number} the value, assuming it is a 32-bit integer.
  61. */
  62. Long.prototype.toInt = function() {
  63. return this.low_;
  64. };
  65. /**
  66. * Return the Number value.
  67. *
  68. * @method
  69. * @return {number} the closest floating-point representation to this value.
  70. */
  71. Long.prototype.toNumber = function() {
  72. return this.high_ * Long.TWO_PWR_32_DBL_ +
  73. this.getLowBitsUnsigned();
  74. };
  75. /**
  76. * Return the JSON value.
  77. *
  78. * @method
  79. * @return {string} the JSON representation.
  80. */
  81. Long.prototype.toJSON = function() {
  82. return this.toString();
  83. }
  84. /**
  85. * Return the String value.
  86. *
  87. * @method
  88. * @param {number} [opt_radix] the radix in which the text should be written.
  89. * @return {string} the textual representation of this value.
  90. */
  91. Long.prototype.toString = function(opt_radix) {
  92. var radix = opt_radix || 10;
  93. if (radix < 2 || 36 < radix) {
  94. throw Error('radix out of range: ' + radix);
  95. }
  96. if (this.isZero()) {
  97. return '0';
  98. }
  99. if (this.isNegative()) {
  100. if (this.equals(Long.MIN_VALUE)) {
  101. // We need to change the Long value before it can be negated, so we remove
  102. // the bottom-most digit in this base and then recurse to do the rest.
  103. var radixLong = Long.fromNumber(radix);
  104. var div = this.div(radixLong);
  105. var rem = div.multiply(radixLong).subtract(this);
  106. return div.toString(radix) + rem.toInt().toString(radix);
  107. } else {
  108. return '-' + this.negate().toString(radix);
  109. }
  110. }
  111. // Do several (6) digits each time through the loop, so as to
  112. // minimize the calls to the very expensive emulated div.
  113. var radixToPower = Long.fromNumber(Math.pow(radix, 6));
  114. var rem = this;
  115. var result = '';
  116. while (true) {
  117. var remDiv = rem.div(radixToPower);
  118. var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt();
  119. var digits = intval.toString(radix);
  120. rem = remDiv;
  121. if (rem.isZero()) {
  122. return digits + result;
  123. } else {
  124. while (digits.length < 6) {
  125. digits = '0' + digits;
  126. }
  127. result = '' + digits + result;
  128. }
  129. }
  130. };
  131. /**
  132. * Return the high 32-bits value.
  133. *
  134. * @method
  135. * @return {number} the high 32-bits as a signed value.
  136. */
  137. Long.prototype.getHighBits = function() {
  138. return this.high_;
  139. };
  140. /**
  141. * Return the low 32-bits value.
  142. *
  143. * @method
  144. * @return {number} the low 32-bits as a signed value.
  145. */
  146. Long.prototype.getLowBits = function() {
  147. return this.low_;
  148. };
  149. /**
  150. * Return the low unsigned 32-bits value.
  151. *
  152. * @method
  153. * @return {number} the low 32-bits as an unsigned value.
  154. */
  155. Long.prototype.getLowBitsUnsigned = function() {
  156. return (this.low_ >= 0) ?
  157. this.low_ : Long.TWO_PWR_32_DBL_ + this.low_;
  158. };
  159. /**
  160. * Returns the number of bits needed to represent the absolute value of this Long.
  161. *
  162. * @method
  163. * @return {number} Returns the number of bits needed to represent the absolute value of this Long.
  164. */
  165. Long.prototype.getNumBitsAbs = function() {
  166. if (this.isNegative()) {
  167. if (this.equals(Long.MIN_VALUE)) {
  168. return 64;
  169. } else {
  170. return this.negate().getNumBitsAbs();
  171. }
  172. } else {
  173. var val = this.high_ != 0 ? this.high_ : this.low_;
  174. for (var bit = 31; bit > 0; bit--) {
  175. if ((val & (1 << bit)) != 0) {
  176. break;
  177. }
  178. }
  179. return this.high_ != 0 ? bit + 33 : bit + 1;
  180. }
  181. };
  182. /**
  183. * Return whether this value is zero.
  184. *
  185. * @method
  186. * @return {boolean} whether this value is zero.
  187. */
  188. Long.prototype.isZero = function() {
  189. return this.high_ == 0 && this.low_ == 0;
  190. };
  191. /**
  192. * Return whether this value is negative.
  193. *
  194. * @method
  195. * @return {boolean} whether this value is negative.
  196. */
  197. Long.prototype.isNegative = function() {
  198. return this.high_ < 0;
  199. };
  200. /**
  201. * Return whether this value is odd.
  202. *
  203. * @method
  204. * @return {boolean} whether this value is odd.
  205. */
  206. Long.prototype.isOdd = function() {
  207. return (this.low_ & 1) == 1;
  208. };
  209. /**
  210. * Return whether this Long equals the other
  211. *
  212. * @method
  213. * @param {Long} other Long to compare against.
  214. * @return {boolean} whether this Long equals the other
  215. */
  216. Long.prototype.equals = function(other) {
  217. return (this.high_ == other.high_) && (this.low_ == other.low_);
  218. };
  219. /**
  220. * Return whether this Long does not equal the other.
  221. *
  222. * @method
  223. * @param {Long} other Long to compare against.
  224. * @return {boolean} whether this Long does not equal the other.
  225. */
  226. Long.prototype.notEquals = function(other) {
  227. return (this.high_ != other.high_) || (this.low_ != other.low_);
  228. };
  229. /**
  230. * Return whether this Long is less than the other.
  231. *
  232. * @method
  233. * @param {Long} other Long to compare against.
  234. * @return {boolean} whether this Long is less than the other.
  235. */
  236. Long.prototype.lessThan = function(other) {
  237. return this.compare(other) < 0;
  238. };
  239. /**
  240. * Return whether this Long is less than or equal to the other.
  241. *
  242. * @method
  243. * @param {Long} other Long to compare against.
  244. * @return {boolean} whether this Long is less than or equal to the other.
  245. */
  246. Long.prototype.lessThanOrEqual = function(other) {
  247. return this.compare(other) <= 0;
  248. };
  249. /**
  250. * Return whether this Long is greater than the other.
  251. *
  252. * @method
  253. * @param {Long} other Long to compare against.
  254. * @return {boolean} whether this Long is greater than the other.
  255. */
  256. Long.prototype.greaterThan = function(other) {
  257. return this.compare(other) > 0;
  258. };
  259. /**
  260. * Return whether this Long is greater than or equal to the other.
  261. *
  262. * @method
  263. * @param {Long} other Long to compare against.
  264. * @return {boolean} whether this Long is greater than or equal to the other.
  265. */
  266. Long.prototype.greaterThanOrEqual = function(other) {
  267. return this.compare(other) >= 0;
  268. };
  269. /**
  270. * Compares this Long with the given one.
  271. *
  272. * @method
  273. * @param {Long} other Long to compare against.
  274. * @return {boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater.
  275. */
  276. Long.prototype.compare = function(other) {
  277. if (this.equals(other)) {
  278. return 0;
  279. }
  280. var thisNeg = this.isNegative();
  281. var otherNeg = other.isNegative();
  282. if (thisNeg && !otherNeg) {
  283. return -1;
  284. }
  285. if (!thisNeg && otherNeg) {
  286. return 1;
  287. }
  288. // at this point, the signs are the same, so subtraction will not overflow
  289. if (this.subtract(other).isNegative()) {
  290. return -1;
  291. } else {
  292. return 1;
  293. }
  294. };
  295. /**
  296. * The negation of this value.
  297. *
  298. * @method
  299. * @return {Long} the negation of this value.
  300. */
  301. Long.prototype.negate = function() {
  302. if (this.equals(Long.MIN_VALUE)) {
  303. return Long.MIN_VALUE;
  304. } else {
  305. return this.not().add(Long.ONE);
  306. }
  307. };
  308. /**
  309. * Returns the sum of this and the given Long.
  310. *
  311. * @method
  312. * @param {Long} other Long to add to this one.
  313. * @return {Long} the sum of this and the given Long.
  314. */
  315. Long.prototype.add = function(other) {
  316. // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
  317. var a48 = this.high_ >>> 16;
  318. var a32 = this.high_ & 0xFFFF;
  319. var a16 = this.low_ >>> 16;
  320. var a00 = this.low_ & 0xFFFF;
  321. var b48 = other.high_ >>> 16;
  322. var b32 = other.high_ & 0xFFFF;
  323. var b16 = other.low_ >>> 16;
  324. var b00 = other.low_ & 0xFFFF;
  325. var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
  326. c00 += a00 + b00;
  327. c16 += c00 >>> 16;
  328. c00 &= 0xFFFF;
  329. c16 += a16 + b16;
  330. c32 += c16 >>> 16;
  331. c16 &= 0xFFFF;
  332. c32 += a32 + b32;
  333. c48 += c32 >>> 16;
  334. c32 &= 0xFFFF;
  335. c48 += a48 + b48;
  336. c48 &= 0xFFFF;
  337. return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
  338. };
  339. /**
  340. * Returns the difference of this and the given Long.
  341. *
  342. * @method
  343. * @param {Long} other Long to subtract from this.
  344. * @return {Long} the difference of this and the given Long.
  345. */
  346. Long.prototype.subtract = function(other) {
  347. return this.add(other.negate());
  348. };
  349. /**
  350. * Returns the product of this and the given Long.
  351. *
  352. * @method
  353. * @param {Long} other Long to multiply with this.
  354. * @return {Long} the product of this and the other.
  355. */
  356. Long.prototype.multiply = function(other) {
  357. if (this.isZero()) {
  358. return Long.ZERO;
  359. } else if (other.isZero()) {
  360. return Long.ZERO;
  361. }
  362. if (this.equals(Long.MIN_VALUE)) {
  363. return other.isOdd() ? Long.MIN_VALUE : Long.ZERO;
  364. } else if (other.equals(Long.MIN_VALUE)) {
  365. return this.isOdd() ? Long.MIN_VALUE : Long.ZERO;
  366. }
  367. if (this.isNegative()) {
  368. if (other.isNegative()) {
  369. return this.negate().multiply(other.negate());
  370. } else {
  371. return this.negate().multiply(other).negate();
  372. }
  373. } else if (other.isNegative()) {
  374. return this.multiply(other.negate()).negate();
  375. }
  376. // If both Longs are small, use float multiplication
  377. if (this.lessThan(Long.TWO_PWR_24_) &&
  378. other.lessThan(Long.TWO_PWR_24_)) {
  379. return Long.fromNumber(this.toNumber() * other.toNumber());
  380. }
  381. // Divide each Long into 4 chunks of 16 bits, and then add up 4x4 products.
  382. // We can skip products that would overflow.
  383. var a48 = this.high_ >>> 16;
  384. var a32 = this.high_ & 0xFFFF;
  385. var a16 = this.low_ >>> 16;
  386. var a00 = this.low_ & 0xFFFF;
  387. var b48 = other.high_ >>> 16;
  388. var b32 = other.high_ & 0xFFFF;
  389. var b16 = other.low_ >>> 16;
  390. var b00 = other.low_ & 0xFFFF;
  391. var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
  392. c00 += a00 * b00;
  393. c16 += c00 >>> 16;
  394. c00 &= 0xFFFF;
  395. c16 += a16 * b00;
  396. c32 += c16 >>> 16;
  397. c16 &= 0xFFFF;
  398. c16 += a00 * b16;
  399. c32 += c16 >>> 16;
  400. c16 &= 0xFFFF;
  401. c32 += a32 * b00;
  402. c48 += c32 >>> 16;
  403. c32 &= 0xFFFF;
  404. c32 += a16 * b16;
  405. c48 += c32 >>> 16;
  406. c32 &= 0xFFFF;
  407. c32 += a00 * b32;
  408. c48 += c32 >>> 16;
  409. c32 &= 0xFFFF;
  410. c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
  411. c48 &= 0xFFFF;
  412. return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
  413. };
  414. /**
  415. * Returns this Long divided by the given one.
  416. *
  417. * @method
  418. * @param {Long} other Long by which to divide.
  419. * @return {Long} this Long divided by the given one.
  420. */
  421. Long.prototype.div = function(other) {
  422. if (other.isZero()) {
  423. throw Error('division by zero');
  424. } else if (this.isZero()) {
  425. return Long.ZERO;
  426. }
  427. if (this.equals(Long.MIN_VALUE)) {
  428. if (other.equals(Long.ONE) ||
  429. other.equals(Long.NEG_ONE)) {
  430. return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
  431. } else if (other.equals(Long.MIN_VALUE)) {
  432. return Long.ONE;
  433. } else {
  434. // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
  435. var halfThis = this.shiftRight(1);
  436. var approx = halfThis.div(other).shiftLeft(1);
  437. if (approx.equals(Long.ZERO)) {
  438. return other.isNegative() ? Long.ONE : Long.NEG_ONE;
  439. } else {
  440. var rem = this.subtract(other.multiply(approx));
  441. var result = approx.add(rem.div(other));
  442. return result;
  443. }
  444. }
  445. } else if (other.equals(Long.MIN_VALUE)) {
  446. return Long.ZERO;
  447. }
  448. if (this.isNegative()) {
  449. if (other.isNegative()) {
  450. return this.negate().div(other.negate());
  451. } else {
  452. return this.negate().div(other).negate();
  453. }
  454. } else if (other.isNegative()) {
  455. return this.div(other.negate()).negate();
  456. }
  457. // Repeat the following until the remainder is less than other: find a
  458. // floating-point that approximates remainder / other *from below*, add this
  459. // into the result, and subtract it from the remainder. It is critical that
  460. // the approximate value is less than or equal to the real value so that the
  461. // remainder never becomes negative.
  462. var res = Long.ZERO;
  463. var rem = this;
  464. while (rem.greaterThanOrEqual(other)) {
  465. // Approximate the result of division. This may be a little greater or
  466. // smaller than the actual value.
  467. var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
  468. // We will tweak the approximate result by changing it in the 48-th digit or
  469. // the smallest non-fractional digit, whichever is larger.
  470. var log2 = Math.ceil(Math.log(approx) / Math.LN2);
  471. var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48);
  472. // Decrease the approximation until it is smaller than the remainder. Note
  473. // that if it is too large, the product overflows and is negative.
  474. var approxRes = Long.fromNumber(approx);
  475. var approxRem = approxRes.multiply(other);
  476. while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
  477. approx -= delta;
  478. approxRes = Long.fromNumber(approx);
  479. approxRem = approxRes.multiply(other);
  480. }
  481. // We know the answer can't be zero... and actually, zero would cause
  482. // infinite recursion since we would make no progress.
  483. if (approxRes.isZero()) {
  484. approxRes = Long.ONE;
  485. }
  486. res = res.add(approxRes);
  487. rem = rem.subtract(approxRem);
  488. }
  489. return res;
  490. };
  491. /**
  492. * Returns this Long modulo the given one.
  493. *
  494. * @method
  495. * @param {Long} other Long by which to mod.
  496. * @return {Long} this Long modulo the given one.
  497. */
  498. Long.prototype.modulo = function(other) {
  499. return this.subtract(this.div(other).multiply(other));
  500. };
  501. /**
  502. * The bitwise-NOT of this value.
  503. *
  504. * @method
  505. * @return {Long} the bitwise-NOT of this value.
  506. */
  507. Long.prototype.not = function() {
  508. return Long.fromBits(~this.low_, ~this.high_);
  509. };
  510. /**
  511. * Returns the bitwise-AND of this Long and the given one.
  512. *
  513. * @method
  514. * @param {Long} other the Long with which to AND.
  515. * @return {Long} the bitwise-AND of this and the other.
  516. */
  517. Long.prototype.and = function(other) {
  518. return Long.fromBits(this.low_ & other.low_, this.high_ & other.high_);
  519. };
  520. /**
  521. * Returns the bitwise-OR of this Long and the given one.
  522. *
  523. * @method
  524. * @param {Long} other the Long with which to OR.
  525. * @return {Long} the bitwise-OR of this and the other.
  526. */
  527. Long.prototype.or = function(other) {
  528. return Long.fromBits(this.low_ | other.low_, this.high_ | other.high_);
  529. };
  530. /**
  531. * Returns the bitwise-XOR of this Long and the given one.
  532. *
  533. * @method
  534. * @param {Long} other the Long with which to XOR.
  535. * @return {Long} the bitwise-XOR of this and the other.
  536. */
  537. Long.prototype.xor = function(other) {
  538. return Long.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_);
  539. };
  540. /**
  541. * Returns this Long with bits shifted to the left by the given amount.
  542. *
  543. * @method
  544. * @param {number} numBits the number of bits by which to shift.
  545. * @return {Long} this shifted to the left by the given amount.
  546. */
  547. Long.prototype.shiftLeft = function(numBits) {
  548. numBits &= 63;
  549. if (numBits == 0) {
  550. return this;
  551. } else {
  552. var low = this.low_;
  553. if (numBits < 32) {
  554. var high = this.high_;
  555. return Long.fromBits(
  556. low << numBits,
  557. (high << numBits) | (low >>> (32 - numBits)));
  558. } else {
  559. return Long.fromBits(0, low << (numBits - 32));
  560. }
  561. }
  562. };
  563. /**
  564. * Returns this Long with bits shifted to the right by the given amount.
  565. *
  566. * @method
  567. * @param {number} numBits the number of bits by which to shift.
  568. * @return {Long} this shifted to the right by the given amount.
  569. */
  570. Long.prototype.shiftRight = function(numBits) {
  571. numBits &= 63;
  572. if (numBits == 0) {
  573. return this;
  574. } else {
  575. var high = this.high_;
  576. if (numBits < 32) {
  577. var low = this.low_;
  578. return Long.fromBits(
  579. (low >>> numBits) | (high << (32 - numBits)),
  580. high >> numBits);
  581. } else {
  582. return Long.fromBits(
  583. high >> (numBits - 32),
  584. high >= 0 ? 0 : -1);
  585. }
  586. }
  587. };
  588. /**
  589. * Returns this Long with bits shifted to the right by the given amount, with the new top bits matching the current sign bit.
  590. *
  591. * @method
  592. * @param {number} numBits the number of bits by which to shift.
  593. * @return {Long} this shifted to the right by the given amount, with zeros placed into the new leading bits.
  594. */
  595. Long.prototype.shiftRightUnsigned = function(numBits) {
  596. numBits &= 63;
  597. if (numBits == 0) {
  598. return this;
  599. } else {
  600. var high = this.high_;
  601. if (numBits < 32) {
  602. var low = this.low_;
  603. return Long.fromBits(
  604. (low >>> numBits) | (high << (32 - numBits)),
  605. high >>> numBits);
  606. } else if (numBits == 32) {
  607. return Long.fromBits(high, 0);
  608. } else {
  609. return Long.fromBits(high >>> (numBits - 32), 0);
  610. }
  611. }
  612. };
  613. /**
  614. * Returns a Long representing the given (32-bit) integer value.
  615. *
  616. * @method
  617. * @param {number} value the 32-bit integer in question.
  618. * @return {Long} the corresponding Long value.
  619. */
  620. Long.fromInt = function(value) {
  621. if (-128 <= value && value < 128) {
  622. var cachedObj = Long.INT_CACHE_[value];
  623. if (cachedObj) {
  624. return cachedObj;
  625. }
  626. }
  627. var obj = new Long(value | 0, value < 0 ? -1 : 0);
  628. if (-128 <= value && value < 128) {
  629. Long.INT_CACHE_[value] = obj;
  630. }
  631. return obj;
  632. };
  633. /**
  634. * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
  635. *
  636. * @method
  637. * @param {number} value the number in question.
  638. * @return {Long} the corresponding Long value.
  639. */
  640. Long.fromNumber = function(value) {
  641. if (isNaN(value) || !isFinite(value)) {
  642. return Long.ZERO;
  643. } else if (value <= -Long.TWO_PWR_63_DBL_) {
  644. return Long.MIN_VALUE;
  645. } else if (value + 1 >= Long.TWO_PWR_63_DBL_) {
  646. return Long.MAX_VALUE;
  647. } else if (value < 0) {
  648. return Long.fromNumber(-value).negate();
  649. } else {
  650. return new Long(
  651. (value % Long.TWO_PWR_32_DBL_) | 0,
  652. (value / Long.TWO_PWR_32_DBL_) | 0);
  653. }
  654. };
  655. /**
  656. * Returns a Long representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits.
  657. *
  658. * @method
  659. * @param {number} lowBits the low 32-bits.
  660. * @param {number} highBits the high 32-bits.
  661. * @return {Long} the corresponding Long value.
  662. */
  663. Long.fromBits = function(lowBits, highBits) {
  664. return new Long(lowBits, highBits);
  665. };
  666. /**
  667. * Returns a Long representation of the given string, written using the given radix.
  668. *
  669. * @method
  670. * @param {string} str the textual representation of the Long.
  671. * @param {number} opt_radix the radix in which the text is written.
  672. * @return {Long} the corresponding Long value.
  673. */
  674. Long.fromString = function(str, opt_radix) {
  675. if (str.length == 0) {
  676. throw Error('number format error: empty string');
  677. }
  678. var radix = opt_radix || 10;
  679. if (radix < 2 || 36 < radix) {
  680. throw Error('radix out of range: ' + radix);
  681. }
  682. if (str.charAt(0) == '-') {
  683. return Long.fromString(str.substring(1), radix).negate();
  684. } else if (str.indexOf('-') >= 0) {
  685. throw Error('number format error: interior "-" character: ' + str);
  686. }
  687. // Do several (8) digits each time through the loop, so as to
  688. // minimize the calls to the very expensive emulated div.
  689. var radixToPower = Long.fromNumber(Math.pow(radix, 8));
  690. var result = Long.ZERO;
  691. for (var i = 0; i < str.length; i += 8) {
  692. var size = Math.min(8, str.length - i);
  693. var value = parseInt(str.substring(i, i + size), radix);
  694. if (size < 8) {
  695. var power = Long.fromNumber(Math.pow(radix, size));
  696. result = result.multiply(power).add(Long.fromNumber(value));
  697. } else {
  698. result = result.multiply(radixToPower);
  699. result = result.add(Long.fromNumber(value));
  700. }
  701. }
  702. return result;
  703. };
  704. // NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
  705. // from* methods on which they depend.
  706. /**
  707. * A cache of the Long representations of small integer values.
  708. * @type {Object}
  709. * @ignore
  710. */
  711. Long.INT_CACHE_ = {};
  712. // NOTE: the compiler should inline these constant values below and then remove
  713. // these variables, so there should be no runtime penalty for these.
  714. /**
  715. * Number used repeated below in calculations. This must appear before the
  716. * first call to any from* function below.
  717. * @type {number}
  718. * @ignore
  719. */
  720. Long.TWO_PWR_16_DBL_ = 1 << 16;
  721. /**
  722. * @type {number}
  723. * @ignore
  724. */
  725. Long.TWO_PWR_24_DBL_ = 1 << 24;
  726. /**
  727. * @type {number}
  728. * @ignore
  729. */
  730. Long.TWO_PWR_32_DBL_ = Long.TWO_PWR_16_DBL_ * Long.TWO_PWR_16_DBL_;
  731. /**
  732. * @type {number}
  733. * @ignore
  734. */
  735. Long.TWO_PWR_31_DBL_ = Long.TWO_PWR_32_DBL_ / 2;
  736. /**
  737. * @type {number}
  738. * @ignore
  739. */
  740. Long.TWO_PWR_48_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_16_DBL_;
  741. /**
  742. * @type {number}
  743. * @ignore
  744. */
  745. Long.TWO_PWR_64_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_32_DBL_;
  746. /**
  747. * @type {number}
  748. * @ignore
  749. */
  750. Long.TWO_PWR_63_DBL_ = Long.TWO_PWR_64_DBL_ / 2;
  751. /** @type {Long} */
  752. Long.ZERO = Long.fromInt(0);
  753. /** @type {Long} */
  754. Long.ONE = Long.fromInt(1);
  755. /** @type {Long} */
  756. Long.NEG_ONE = Long.fromInt(-1);
  757. /** @type {Long} */
  758. Long.MAX_VALUE =
  759. Long.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0);
  760. /** @type {Long} */
  761. Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0);
  762. /**
  763. * @type {Long}
  764. * @ignore
  765. */
  766. Long.TWO_PWR_24_ = Long.fromInt(1 << 24);
  767. /**
  768. * Expose.
  769. */
  770. module.exports = Long;
  771. module.exports.Long = Long;