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
5
// http://www.apache.org/licenses/LICENSE-2.0
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.
13
// Copyright 2009 Google Inc. All Rights Reserved
16
* Defines a Long class for representing a 64-bit two's-complement
17
* integer value, which faithfully simulates the behavior of a Java "Long". This
18
* implementation is derived from LongLib in GWT.
20
* Constructs a 64-bit two's-complement integer, given its low and high 32-bit
21
* values as *signed* integers. See the from* functions below for more
22
* convenient ways of constructing Longs.
24
* The internal representation of a Long is the two given signed, 32-bit values.
25
* We use 32-bit pieces because these are the size of integers on which
26
* Javascript performs bit-operations. For operations like addition and
27
* multiplication, we split each number into 16-bit pieces, which can easily be
28
* multiplied within Javascript's floating-point representation without overflow
31
* In the algorithms below, we frequently reduce the negative case to the
32
* positive case by negating the input(s) and then post-processing the result.
33
* Note that we must ALWAYS check specially whether those values are MIN_VALUE
34
* (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
35
* a positive number, it overflows back into a negative). Not handling this
36
* case would often result in infinite recursion.
38
* @class Represents the BSON Long type.
39
* @param {Number} low the low (signed) 32 bits of the Long.
40
* @param {Number} high the high (signed) 32 bits of the Long.
42
function Long(low, high) {
43
if(!(this instanceof Long)) return new Long(low, high);
45
this._bsontype = 'Long';
50
this.low_ = low | 0; // force into 32 signed bits.
56
this.high_ = high | 0; // force into 32 signed bits.
60
* Return the int value.
62
* @return {Number} the value, assuming it is a 32-bit integer.
65
Long.prototype.toInt = function() {
70
* Return the Number value.
72
* @return {Number} the closest floating-point representation to this value.
75
Long.prototype.toNumber = function() {
76
return this.high_ * Long.TWO_PWR_32_DBL_ +
77
this.getLowBitsUnsigned();
81
* Return the JSON value.
83
* @return {String} the JSON representation.
86
Long.prototype.toJSON = function() {
87
return this.toString();
91
* Return the String value.
93
* @param {Number} [opt_radix] the radix in which the text should be written.
94
* @return {String} the textual representation of this value.
97
Long.prototype.toString = function(opt_radix) {
98
var radix = opt_radix || 10;
99
if (radix < 2 || 36 < radix) {
100
throw Error('radix out of range: ' + radix);
107
if (this.isNegative()) {
108
if (this.equals(Long.MIN_VALUE)) {
109
// We need to change the Long value before it can be negated, so we remove
110
// the bottom-most digit in this base and then recurse to do the rest.
111
var radixLong = Long.fromNumber(radix);
112
var div = this.div(radixLong);
113
var rem = div.multiply(radixLong).subtract(this);
114
return div.toString(radix) + rem.toInt().toString(radix);
116
return '-' + this.negate().toString(radix);
120
// Do several (6) digits each time through the loop, so as to
121
// minimize the calls to the very expensive emulated div.
122
var radixToPower = Long.fromNumber(Math.pow(radix, 6));
127
var remDiv = rem.div(radixToPower);
128
var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt();
129
var digits = intval.toString(radix);
133
return digits + result;
135
while (digits.length < 6) {
136
digits = '0' + digits;
138
result = '' + digits + result;
144
* Return the high 32-bits value.
146
* @return {Number} the high 32-bits as a signed value.
149
Long.prototype.getHighBits = function() {
154
* Return the low 32-bits value.
156
* @return {Number} the low 32-bits as a signed value.
159
Long.prototype.getLowBits = function() {
164
* Return the low unsigned 32-bits value.
166
* @return {Number} the low 32-bits as an unsigned value.
169
Long.prototype.getLowBitsUnsigned = function() {
170
return (this.low_ >= 0) ?
171
this.low_ : Long.TWO_PWR_32_DBL_ + this.low_;
175
* Returns the number of bits needed to represent the absolute value of this Long.
177
* @return {Number} Returns the number of bits needed to represent the absolute value of this Long.
180
Long.prototype.getNumBitsAbs = function() {
181
if (this.isNegative()) {
182
if (this.equals(Long.MIN_VALUE)) {
185
return this.negate().getNumBitsAbs();
188
var val = this.high_ != 0 ? this.high_ : this.low_;
189
for (var bit = 31; bit > 0; bit--) {
190
if ((val & (1 << bit)) != 0) {
194
return this.high_ != 0 ? bit + 33 : bit + 1;
199
* Return whether this value is zero.
201
* @return {Boolean} whether this value is zero.
204
Long.prototype.isZero = function() {
205
return this.high_ == 0 && this.low_ == 0;
209
* Return whether this value is negative.
211
* @return {Boolean} whether this value is negative.
214
Long.prototype.isNegative = function() {
215
return this.high_ < 0;
219
* Return whether this value is odd.
221
* @return {Boolean} whether this value is odd.
224
Long.prototype.isOdd = function() {
225
return (this.low_ & 1) == 1;
229
* Return whether this Long equals the other
231
* @param {Long} other Long to compare against.
232
* @return {Boolean} whether this Long equals the other
235
Long.prototype.equals = function(other) {
236
return (this.high_ == other.high_) && (this.low_ == other.low_);
240
* Return whether this Long does not equal the other.
242
* @param {Long} other Long to compare against.
243
* @return {Boolean} whether this Long does not equal the other.
246
Long.prototype.notEquals = function(other) {
247
return (this.high_ != other.high_) || (this.low_ != other.low_);
251
* Return whether this Long is less than the other.
253
* @param {Long} other Long to compare against.
254
* @return {Boolean} whether this Long is less than the other.
257
Long.prototype.lessThan = function(other) {
258
return this.compare(other) < 0;
262
* Return whether this Long is less than or equal to the other.
264
* @param {Long} other Long to compare against.
265
* @return {Boolean} whether this Long is less than or equal to the other.
268
Long.prototype.lessThanOrEqual = function(other) {
269
return this.compare(other) <= 0;
273
* Return whether this Long is greater than the other.
275
* @param {Long} other Long to compare against.
276
* @return {Boolean} whether this Long is greater than the other.
279
Long.prototype.greaterThan = function(other) {
280
return this.compare(other) > 0;
284
* Return whether this Long is greater than or equal to the other.
286
* @param {Long} other Long to compare against.
287
* @return {Boolean} whether this Long is greater than or equal to the other.
290
Long.prototype.greaterThanOrEqual = function(other) {
291
return this.compare(other) >= 0;
295
* Compares this Long with the given one.
297
* @param {Long} other Long to compare against.
298
* @return {Boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater.
301
Long.prototype.compare = function(other) {
302
if (this.equals(other)) {
306
var thisNeg = this.isNegative();
307
var otherNeg = other.isNegative();
308
if (thisNeg && !otherNeg) {
311
if (!thisNeg && otherNeg) {
315
// at this point, the signs are the same, so subtraction will not overflow
316
if (this.subtract(other).isNegative()) {
324
* The negation of this value.
326
* @return {Long} the negation of this value.
329
Long.prototype.negate = function() {
330
if (this.equals(Long.MIN_VALUE)) {
331
return Long.MIN_VALUE;
333
return this.not().add(Long.ONE);
338
* Returns the sum of this and the given Long.
340
* @param {Long} other Long to add to this one.
341
* @return {Long} the sum of this and the given Long.
344
Long.prototype.add = function(other) {
345
// Divide each number into 4 chunks of 16 bits, and then sum the chunks.
347
var a48 = this.high_ >>> 16;
348
var a32 = this.high_ & 0xFFFF;
349
var a16 = this.low_ >>> 16;
350
var a00 = this.low_ & 0xFFFF;
352
var b48 = other.high_ >>> 16;
353
var b32 = other.high_ & 0xFFFF;
354
var b16 = other.low_ >>> 16;
355
var b00 = other.low_ & 0xFFFF;
357
var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
369
return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
373
* Returns the difference of this and the given Long.
375
* @param {Long} other Long to subtract from this.
376
* @return {Long} the difference of this and the given Long.
379
Long.prototype.subtract = function(other) {
380
return this.add(other.negate());
384
* Returns the product of this and the given Long.
386
* @param {Long} other Long to multiply with this.
387
* @return {Long} the product of this and the other.
390
Long.prototype.multiply = function(other) {
393
} else if (other.isZero()) {
397
if (this.equals(Long.MIN_VALUE)) {
398
return other.isOdd() ? Long.MIN_VALUE : Long.ZERO;
399
} else if (other.equals(Long.MIN_VALUE)) {
400
return this.isOdd() ? Long.MIN_VALUE : Long.ZERO;
403
if (this.isNegative()) {
404
if (other.isNegative()) {
405
return this.negate().multiply(other.negate());
407
return this.negate().multiply(other).negate();
409
} else if (other.isNegative()) {
410
return this.multiply(other.negate()).negate();
413
// If both Longs are small, use float multiplication
414
if (this.lessThan(Long.TWO_PWR_24_) &&
415
other.lessThan(Long.TWO_PWR_24_)) {
416
return Long.fromNumber(this.toNumber() * other.toNumber());
419
// Divide each Long into 4 chunks of 16 bits, and then add up 4x4 products.
420
// We can skip products that would overflow.
422
var a48 = this.high_ >>> 16;
423
var a32 = this.high_ & 0xFFFF;
424
var a16 = this.low_ >>> 16;
425
var a00 = this.low_ & 0xFFFF;
427
var b48 = other.high_ >>> 16;
428
var b32 = other.high_ & 0xFFFF;
429
var b16 = other.low_ >>> 16;
430
var b00 = other.low_ & 0xFFFF;
432
var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
451
c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
453
return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);
457
* Returns this Long divided by the given one.
459
* @param {Long} other Long by which to divide.
460
* @return {Long} this Long divided by the given one.
463
Long.prototype.div = function(other) {
464
if (other.isZero()) {
465
throw Error('division by zero');
466
} else if (this.isZero()) {
470
if (this.equals(Long.MIN_VALUE)) {
471
if (other.equals(Long.ONE) ||
472
other.equals(Long.NEG_ONE)) {
473
return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
474
} else if (other.equals(Long.MIN_VALUE)) {
477
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
478
var halfThis = this.shiftRight(1);
479
var approx = halfThis.div(other).shiftLeft(1);
480
if (approx.equals(Long.ZERO)) {
481
return other.isNegative() ? Long.ONE : Long.NEG_ONE;
483
var rem = this.subtract(other.multiply(approx));
484
var result = approx.add(rem.div(other));
488
} else if (other.equals(Long.MIN_VALUE)) {
492
if (this.isNegative()) {
493
if (other.isNegative()) {
494
return this.negate().div(other.negate());
496
return this.negate().div(other).negate();
498
} else if (other.isNegative()) {
499
return this.div(other.negate()).negate();
502
// Repeat the following until the remainder is less than other: find a
503
// floating-point that approximates remainder / other *from below*, add this
504
// into the result, and subtract it from the remainder. It is critical that
505
// the approximate value is less than or equal to the real value so that the
506
// remainder never becomes negative.
509
while (rem.greaterThanOrEqual(other)) {
510
// Approximate the result of division. This may be a little greater or
511
// smaller than the actual value.
512
var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));
514
// We will tweak the approximate result by changing it in the 48-th digit or
515
// the smallest non-fractional digit, whichever is larger.
516
var log2 = Math.ceil(Math.log(approx) / Math.LN2);
517
var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48);
519
// Decrease the approximation until it is smaller than the remainder. Note
520
// that if it is too large, the product overflows and is negative.
521
var approxRes = Long.fromNumber(approx);
522
var approxRem = approxRes.multiply(other);
523
while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
525
approxRes = Long.fromNumber(approx);
526
approxRem = approxRes.multiply(other);
529
// We know the answer can't be zero... and actually, zero would cause
530
// infinite recursion since we would make no progress.
531
if (approxRes.isZero()) {
532
approxRes = Long.ONE;
535
res = res.add(approxRes);
536
rem = rem.subtract(approxRem);
542
* Returns this Long modulo the given one.
544
* @param {Long} other Long by which to mod.
545
* @return {Long} this Long modulo the given one.
548
Long.prototype.modulo = function(other) {
549
return this.subtract(this.div(other).multiply(other));
553
* The bitwise-NOT of this value.
555
* @return {Long} the bitwise-NOT of this value.
558
Long.prototype.not = function() {
559
return Long.fromBits(~this.low_, ~this.high_);
563
* Returns the bitwise-AND of this Long and the given one.
565
* @param {Long} other the Long with which to AND.
566
* @return {Long} the bitwise-AND of this and the other.
569
Long.prototype.and = function(other) {
570
return Long.fromBits(this.low_ & other.low_, this.high_ & other.high_);
574
* Returns the bitwise-OR of this Long and the given one.
576
* @param {Long} other the Long with which to OR.
577
* @return {Long} the bitwise-OR of this and the other.
580
Long.prototype.or = function(other) {
581
return Long.fromBits(this.low_ | other.low_, this.high_ | other.high_);
585
* Returns the bitwise-XOR of this Long and the given one.
587
* @param {Long} other the Long with which to XOR.
588
* @return {Long} the bitwise-XOR of this and the other.
591
Long.prototype.xor = function(other) {
592
return Long.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_);
596
* Returns this Long with bits shifted to the left by the given amount.
598
* @param {Number} numBits the number of bits by which to shift.
599
* @return {Long} this shifted to the left by the given amount.
602
Long.prototype.shiftLeft = function(numBits) {
609
var high = this.high_;
610
return Long.fromBits(
612
(high << numBits) | (low >>> (32 - numBits)));
614
return Long.fromBits(0, low << (numBits - 32));
620
* Returns this Long with bits shifted to the right by the given amount.
622
* @param {Number} numBits the number of bits by which to shift.
623
* @return {Long} this shifted to the right by the given amount.
626
Long.prototype.shiftRight = function(numBits) {
631
var high = this.high_;
634
return Long.fromBits(
635
(low >>> numBits) | (high << (32 - numBits)),
638
return Long.fromBits(
639
high >> (numBits - 32),
646
* Returns this Long with bits shifted to the right by the given amount, with the new top bits matching the current sign bit.
648
* @param {Number} numBits the number of bits by which to shift.
649
* @return {Long} this shifted to the right by the given amount, with zeros placed into the new leading bits.
652
Long.prototype.shiftRightUnsigned = function(numBits) {
657
var high = this.high_;
660
return Long.fromBits(
661
(low >>> numBits) | (high << (32 - numBits)),
663
} else if (numBits == 32) {
664
return Long.fromBits(high, 0);
666
return Long.fromBits(high >>> (numBits - 32), 0);
672
* Returns a Long representing the given (32-bit) integer value.
674
* @param {Number} value the 32-bit integer in question.
675
* @return {Long} the corresponding Long value.
678
Long.fromInt = function(value) {
679
if (-128 <= value && value < 128) {
680
var cachedObj = Long.INT_CACHE_[value];
686
var obj = new Long(value | 0, value < 0 ? -1 : 0);
687
if (-128 <= value && value < 128) {
688
Long.INT_CACHE_[value] = obj;
694
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
696
* @param {Number} value the number in question.
697
* @return {Long} the corresponding Long value.
700
Long.fromNumber = function(value) {
701
if (isNaN(value) || !isFinite(value)) {
703
} else if (value <= -Long.TWO_PWR_63_DBL_) {
704
return Long.MIN_VALUE;
705
} else if (value + 1 >= Long.TWO_PWR_63_DBL_) {
706
return Long.MAX_VALUE;
707
} else if (value < 0) {
708
return Long.fromNumber(-value).negate();
711
(value % Long.TWO_PWR_32_DBL_) | 0,
712
(value / Long.TWO_PWR_32_DBL_) | 0);
717
* 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.
719
* @param {Number} lowBits the low 32-bits.
720
* @param {Number} highBits the high 32-bits.
721
* @return {Long} the corresponding Long value.
724
Long.fromBits = function(lowBits, highBits) {
725
return new Long(lowBits, highBits);
729
* Returns a Long representation of the given string, written using the given radix.
731
* @param {String} str the textual representation of the Long.
732
* @param {Number} opt_radix the radix in which the text is written.
733
* @return {Long} the corresponding Long value.
736
Long.fromString = function(str, opt_radix) {
737
if (str.length == 0) {
738
throw Error('number format error: empty string');
741
var radix = opt_radix || 10;
742
if (radix < 2 || 36 < radix) {
743
throw Error('radix out of range: ' + radix);
746
if (str.charAt(0) == '-') {
747
return Long.fromString(str.substring(1), radix).negate();
748
} else if (str.indexOf('-') >= 0) {
749
throw Error('number format error: interior "-" character: ' + str);
752
// Do several (8) digits each time through the loop, so as to
753
// minimize the calls to the very expensive emulated div.
754
var radixToPower = Long.fromNumber(Math.pow(radix, 8));
756
var result = Long.ZERO;
757
for (var i = 0; i < str.length; i += 8) {
758
var size = Math.min(8, str.length - i);
759
var value = parseInt(str.substring(i, i + size), radix);
761
var power = Long.fromNumber(Math.pow(radix, size));
762
result = result.multiply(power).add(Long.fromNumber(value));
764
result = result.multiply(radixToPower);
765
result = result.add(Long.fromNumber(value));
771
// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the
772
// from* methods on which they depend.
776
* A cache of the Long representations of small integer values.
780
Long.INT_CACHE_ = {};
782
// NOTE: the compiler should inline these constant values below and then remove
783
// these variables, so there should be no runtime penalty for these.
786
* Number used repeated below in calculations. This must appear before the
787
* first call to any from* function below.
791
Long.TWO_PWR_16_DBL_ = 1 << 16;
797
Long.TWO_PWR_24_DBL_ = 1 << 24;
803
Long.TWO_PWR_32_DBL_ = Long.TWO_PWR_16_DBL_ * Long.TWO_PWR_16_DBL_;
809
Long.TWO_PWR_31_DBL_ = Long.TWO_PWR_32_DBL_ / 2;
815
Long.TWO_PWR_48_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_16_DBL_;
821
Long.TWO_PWR_64_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_32_DBL_;
827
Long.TWO_PWR_63_DBL_ = Long.TWO_PWR_64_DBL_ / 2;
830
Long.ZERO = Long.fromInt(0);
833
Long.ONE = Long.fromInt(1);
836
Long.NEG_ONE = Long.fromInt(-1);
840
Long.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0);
843
Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0);
849
Long.TWO_PWR_24_ = Long.fromInt(1 << 24);
b'\\ No newline at end of file'