Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

long.js 22KB

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