Comparing version
{ | ||
"name": "long", | ||
"version": "3.0.0", | ||
"version": "3.0.3", | ||
"author": "Daniel Wirtz <dcode@dcode.io>", | ||
@@ -5,0 +5,0 @@ "description": "A Long class for representing a 64 bit two's-complement integer value.", |
430
dist/long.js
@@ -52,3 +52,3 @@ /* | ||
*/ | ||
this.low = low|0; | ||
this.low = low | 0; | ||
@@ -60,3 +60,3 @@ /** | ||
*/ | ||
this.high = high|0; | ||
this.high = high | 0; | ||
@@ -104,3 +104,14 @@ /** | ||
/** | ||
* @function | ||
* @param {*} obj Object | ||
* @returns {boolean} | ||
* @inner | ||
*/ | ||
function isLong(obj) { | ||
return (obj && obj["__isLong__"]) === true; | ||
} | ||
/** | ||
* Tests if the specified object is a Long. | ||
* @function | ||
* @param {*} obj Object | ||
@@ -110,5 +121,3 @@ * @returns {boolean} | ||
*/ | ||
Long.isLong = function isLong(obj) { | ||
return (obj && obj["__isLong__"]) === true; | ||
}; | ||
Long.isLong = isLong; | ||
@@ -130,38 +139,38 @@ /** | ||
/** | ||
* Returns a Long representing the given 32 bit integer value. | ||
* @param {number} value The 32 bit integer in question | ||
* @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
* @returns {!Long} The corresponding Long value | ||
* @expose | ||
* @param {number} value | ||
* @param {boolean=} unsigned | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
Long.fromInt = function fromInt(value, unsigned) { | ||
function fromInt(value, unsigned) { | ||
var obj, cachedObj, cache; | ||
if (!unsigned) { | ||
value = value | 0; | ||
if (cache = (-128 <= value && value < 128)) { | ||
cachedObj = INT_CACHE[value]; | ||
if (unsigned) { | ||
value >>>= 0; | ||
if (cache = (0 <= value && value < 256)) { | ||
cachedObj = UINT_CACHE[value]; | ||
if (cachedObj) | ||
return cachedObj; | ||
} | ||
obj = new Long(value, value < 0 ? -1 : 0, false); | ||
obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true); | ||
if (cache) | ||
INT_CACHE[value] = obj; | ||
UINT_CACHE[value] = obj; | ||
return obj; | ||
} else { | ||
value = value >>> 0; | ||
if (cache = (0 <= value && value < 256)) { | ||
cachedObj = UINT_CACHE[value]; | ||
value |= 0; | ||
if (cache = (-128 <= value && value < 128)) { | ||
cachedObj = INT_CACHE[value]; | ||
if (cachedObj) | ||
return cachedObj; | ||
} | ||
obj = new Long(value, (value | 0) < 0 ? -1 : 0, true); | ||
obj = fromBits(value, value < 0 ? -1 : 0, false); | ||
if (cache) | ||
UINT_CACHE[value] = obj; | ||
INT_CACHE[value] = obj; | ||
return obj; | ||
} | ||
}; | ||
} | ||
/** | ||
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. | ||
* @param {number} value The number in question | ||
* Returns a Long representing the given 32 bit integer value. | ||
* @function | ||
* @param {number} value The 32 bit integer in question | ||
* @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
@@ -171,20 +180,54 @@ * @returns {!Long} The corresponding Long value | ||
*/ | ||
Long.fromNumber = function fromNumber(value, unsigned) { | ||
unsigned = !!unsigned; | ||
Long.fromInt = fromInt; | ||
/** | ||
* @param {number} value | ||
* @param {boolean=} unsigned | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
function fromNumber(value, unsigned) { | ||
if (isNaN(value) || !isFinite(value)) | ||
return Long.ZERO; | ||
if (!unsigned && value <= -TWO_PWR_63_DBL) | ||
return Long.MIN_VALUE; | ||
if (!unsigned && value + 1 >= TWO_PWR_63_DBL) | ||
return Long.MAX_VALUE; | ||
if (unsigned && value >= TWO_PWR_64_DBL) | ||
return Long.MAX_UNSIGNED_VALUE; | ||
return unsigned ? UZERO : ZERO; | ||
if (unsigned) { | ||
if (value < 0) | ||
return UZERO; | ||
if (value >= TWO_PWR_64_DBL) | ||
return MAX_UNSIGNED_VALUE; | ||
} else { | ||
if (value <= -TWO_PWR_63_DBL) | ||
return MIN_VALUE; | ||
if (value + 1 >= TWO_PWR_63_DBL) | ||
return MAX_VALUE; | ||
} | ||
if (value < 0) | ||
return Long.fromNumber(-value, unsigned).neg(); | ||
return new Long((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); | ||
}; | ||
return fromNumber(-value, unsigned).neg(); | ||
return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); | ||
} | ||
/** | ||
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. | ||
* @function | ||
* @param {number} value The number in question | ||
* @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
* @returns {!Long} The corresponding Long value | ||
* @expose | ||
*/ | ||
Long.fromNumber = fromNumber; | ||
/** | ||
* @param {number} lowBits | ||
* @param {number} highBits | ||
* @param {boolean=} unsigned | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
function fromBits(lowBits, highBits, unsigned) { | ||
return new Long(lowBits, highBits, unsigned); | ||
} | ||
/** | ||
* Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is | ||
* assumed to use 32 bits. | ||
* @function | ||
* @param {number} lowBits The low 32 bits | ||
@@ -196,19 +239,25 @@ * @param {number} highBits The high 32 bits | ||
*/ | ||
Long.fromBits = function fromBits(lowBits, highBits, unsigned) { | ||
return new Long(lowBits, highBits, unsigned); | ||
}; | ||
Long.fromBits = fromBits; | ||
/** | ||
* Returns a Long representation of the given string, written using the specified radix. | ||
* @param {string} str The textual representation of the Long | ||
* @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
* @param {number=} radix The radix in which the text is written (2-36), defaults to 10 | ||
* @returns {!Long} The corresponding Long value | ||
* @expose | ||
* @function | ||
* @param {number} base | ||
* @param {number} exponent | ||
* @returns {number} | ||
* @inner | ||
*/ | ||
Long.fromString = function fromString(str, unsigned, radix) { | ||
var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4) | ||
/** | ||
* @param {string} str | ||
* @param {(boolean|number)=} unsigned | ||
* @param {number=} radix | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
function fromString(str, unsigned, radix) { | ||
if (str.length === 0) | ||
throw Error('number format error: empty string'); | ||
throw Error('empty string'); | ||
if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") | ||
return Long.ZERO; | ||
return ZERO; | ||
if (typeof unsigned === 'number') // For goog.math.long compatibility | ||
@@ -219,24 +268,25 @@ radix = unsigned, | ||
if (radix < 2 || 36 < radix) | ||
throw Error('radix out of range: ' + radix); | ||
throw RangeError('radix'); | ||
var p; | ||
if ((p = str.indexOf('-')) > 0) | ||
throw Error('number format error: interior "-" character: ' + str); | ||
else if (p === 0) | ||
return Long.fromString(str.substring(1), unsigned, radix).neg(); | ||
throw Error('interior hyphen'); | ||
else if (p === 0) { | ||
return fromString(str.substring(1), unsigned, radix).neg(); | ||
} | ||
// Do several (8) digits each time through the loop, so as to | ||
// minimize the calls to the very expensive emulated div. | ||
var radixToPower = Long.fromNumber(Math.pow(radix, 8)); | ||
var radixToPower = fromNumber(pow_dbl(radix, 8)); | ||
var result = Long.ZERO; | ||
var result = ZERO; | ||
for (var i = 0; i < str.length; i += 8) { | ||
var size = Math.min(8, str.length - i); | ||
var value = parseInt(str.substring(i, i + size), radix); | ||
var size = Math.min(8, str.length - i), | ||
value = parseInt(str.substring(i, i + size), radix); | ||
if (size < 8) { | ||
var power = Long.fromNumber(Math.pow(radix, size)); | ||
result = result.mul(power).add(Long.fromNumber(value)); | ||
var power = fromNumber(pow_dbl(radix, size)); | ||
result = result.mul(power).add(fromNumber(value)); | ||
} else { | ||
result = result.mul(radixToPower); | ||
result = result.add(Long.fromNumber(value)); | ||
result = result.add(fromNumber(value)); | ||
} | ||
@@ -246,21 +296,41 @@ } | ||
return result; | ||
}; | ||
} | ||
/** | ||
* Converts the specified value to a Long. | ||
* @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value | ||
* @returns {!Long} | ||
* Returns a Long representation of the given string, written using the specified radix. | ||
* @function | ||
* @param {string} str The textual representation of the Long | ||
* @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
* @param {number=} radix The radix in which the text is written (2-36), defaults to 10 | ||
* @returns {!Long} The corresponding Long value | ||
* @expose | ||
*/ | ||
Long.fromValue = function fromValue(val) { | ||
Long.fromString = fromString; | ||
/** | ||
* @function | ||
* @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
function fromValue(val) { | ||
if (val /* is compatible */ instanceof Long) | ||
return val; | ||
if (typeof val === 'number') | ||
return Long.fromNumber(val); | ||
return fromNumber(val); | ||
if (typeof val === 'string') | ||
return Long.fromString(val); | ||
return fromString(val); | ||
// Throws for non-objects, converts non-instanceof Long: | ||
return new Long(val.low, val.high, val.unsigned); | ||
}; | ||
return fromBits(val.low, val.high, val.unsigned); | ||
} | ||
/** | ||
* Converts the specified value to a Long. | ||
* @function | ||
* @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value | ||
* @returns {!Long} | ||
* @expose | ||
*/ | ||
Long.fromValue = fromValue; | ||
// NOTE: the compiler should inline these constant values below and then remove these variables, so there should be | ||
@@ -309,5 +379,11 @@ // no runtime penalty for these. | ||
*/ | ||
var TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL); | ||
var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL); | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var ZERO = fromInt(0); | ||
/** | ||
* Signed zero. | ||
@@ -317,5 +393,11 @@ * @type {!Long} | ||
*/ | ||
Long.ZERO = Long.fromInt(0); | ||
Long.ZERO = ZERO; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var UZERO = fromInt(0, true); | ||
/** | ||
* Unsigned zero. | ||
@@ -325,5 +407,11 @@ * @type {!Long} | ||
*/ | ||
Long.UZERO = Long.fromInt(0, true); | ||
Long.UZERO = UZERO; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var ONE = fromInt(1); | ||
/** | ||
* Signed one. | ||
@@ -333,5 +421,11 @@ * @type {!Long} | ||
*/ | ||
Long.ONE = Long.fromInt(1); | ||
Long.ONE = ONE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var UONE = fromInt(1, true); | ||
/** | ||
* Unsigned one. | ||
@@ -341,5 +435,11 @@ * @type {!Long} | ||
*/ | ||
Long.UONE = Long.fromInt(1, true); | ||
Long.UONE = UONE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var NEG_ONE = fromInt(-1); | ||
/** | ||
* Signed negative one. | ||
@@ -349,5 +449,11 @@ * @type {!Long} | ||
*/ | ||
Long.NEG_ONE = Long.fromInt(-1); | ||
Long.NEG_ONE = NEG_ONE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false); | ||
/** | ||
* Maximum signed value. | ||
@@ -357,5 +463,11 @@ * @type {!Long} | ||
*/ | ||
Long.MAX_VALUE = new Long(0xFFFFFFFF|0, 0x7FFFFFFF|0, false); | ||
Long.MAX_VALUE = MAX_VALUE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true); | ||
/** | ||
* Maximum unsigned value. | ||
@@ -365,5 +477,11 @@ * @type {!Long} | ||
*/ | ||
Long.MAX_UNSIGNED_VALUE = new Long(0xFFFFFFFF|0, 0xFFFFFFFF|0, true); | ||
Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var MIN_VALUE = fromBits(0, 0x80000000|0, false); | ||
/** | ||
* Minimum signed value. | ||
@@ -373,3 +491,3 @@ * @type {!Long} | ||
*/ | ||
Long.MIN_VALUE = new Long(0, 0x80000000|0, false); | ||
Long.MIN_VALUE = MIN_VALUE; | ||
@@ -397,5 +515,4 @@ /** | ||
LongPrototype.toNumber = function toNumber() { | ||
if (this.unsigned) { | ||
if (this.unsigned) | ||
return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0); | ||
} | ||
return this.high * TWO_PWR_32_DBL + (this.low >>> 0); | ||
@@ -415,14 +532,13 @@ }; | ||
if (radix < 2 || 36 < radix) | ||
throw RangeError('radix out of range: ' + radix); | ||
throw RangeError('radix'); | ||
if (this.isZero()) | ||
return '0'; | ||
var rem; | ||
if (this.isNegative()) { // Unsigned Longs are never negative | ||
if (this.eq(Long.MIN_VALUE)) { | ||
if (this.eq(MIN_VALUE)) { | ||
// We need to change the Long value before it can be negated, so we remove | ||
// the bottom-most digit in this base and then recurse to do the rest. | ||
var radixLong = Long.fromNumber(radix); | ||
var div = this.div(radixLong); | ||
rem = div.mul(radixLong).sub(this); | ||
return div.toString(radix) + rem.toInt().toString(radix); | ||
var radixLong = fromNumber(radix), | ||
div = this.div(radixLong), | ||
rem1 = div.mul(radixLong).sub(this); | ||
return div.toString(radix) + rem1.toInt().toString(radix); | ||
} else | ||
@@ -434,4 +550,4 @@ return '-' + this.neg().toString(radix); | ||
// minimize the calls to the very expensive emulated div. | ||
var radixToPower = Long.fromNumber(Math.pow(radix, 6), this.unsigned); | ||
rem = this; | ||
var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned), | ||
rem = this; | ||
var result = ''; | ||
@@ -496,3 +612,3 @@ while (true) { | ||
if (this.isNegative()) // Unsigned Longs are never negative | ||
return this.eq(Long.MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); | ||
return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); | ||
var val = this.high != 0 ? this.high : this.low; | ||
@@ -557,4 +673,4 @@ for (var bit = 31; bit > 0; bit--) | ||
LongPrototype.equals = function equals(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) | ||
@@ -600,3 +716,3 @@ return false; | ||
LongPrototype.lessThan = function lessThan(other) { | ||
return this.compare(/* validates */ other) < 0; | ||
return this.comp(/* validates */ other) < 0; | ||
}; | ||
@@ -620,3 +736,3 @@ | ||
LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) { | ||
return this.compare(/* validates */ other) <= 0; | ||
return this.comp(/* validates */ other) <= 0; | ||
}; | ||
@@ -640,3 +756,3 @@ | ||
LongPrototype.greaterThan = function greaterThan(other) { | ||
return this.compare(/* validates */ other) > 0; | ||
return this.comp(/* validates */ other) > 0; | ||
}; | ||
@@ -660,3 +776,3 @@ | ||
LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) { | ||
return this.compare(/* validates */ other) >= 0; | ||
return this.comp(/* validates */ other) >= 0; | ||
}; | ||
@@ -681,4 +797,4 @@ | ||
LongPrototype.compare = function compare(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
if (this.eq(other)) | ||
@@ -715,5 +831,5 @@ return 0; | ||
LongPrototype.negate = function negate() { | ||
if (!this.unsigned && this.eq(Long.MIN_VALUE)) | ||
return Long.MIN_VALUE; | ||
return this.not().add(Long.ONE); | ||
if (!this.unsigned && this.eq(MIN_VALUE)) | ||
return MIN_VALUE; | ||
return this.not().add(ONE); | ||
}; | ||
@@ -736,4 +852,4 @@ | ||
LongPrototype.add = function add(addend) { | ||
if (!Long.isLong(addend)) | ||
addend = Long.fromValue(addend); | ||
if (!isLong(addend)) | ||
addend = fromValue(addend); | ||
@@ -764,3 +880,3 @@ // Divide each number into 4 chunks of 16 bits, and then sum the chunks. | ||
c48 &= 0xFFFF; | ||
return new Long((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); | ||
return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); | ||
}; | ||
@@ -775,4 +891,4 @@ | ||
LongPrototype.subtract = function subtract(subtrahend) { | ||
if (!Long.isLong(subtrahend)) | ||
subtrahend = Long.fromValue(subtrahend); | ||
if (!isLong(subtrahend)) | ||
subtrahend = fromValue(subtrahend); | ||
return this.add(subtrahend.neg()); | ||
@@ -798,11 +914,11 @@ }; | ||
if (this.isZero()) | ||
return Long.ZERO; | ||
if (!Long.isLong(multiplier)) | ||
multiplier = Long.fromValue(multiplier); | ||
return ZERO; | ||
if (!isLong(multiplier)) | ||
multiplier = fromValue(multiplier); | ||
if (multiplier.isZero()) | ||
return Long.ZERO; | ||
if (this.eq(Long.MIN_VALUE)) | ||
return multiplier.isOdd() ? Long.MIN_VALUE : Long.ZERO; | ||
if (multiplier.eq(Long.MIN_VALUE)) | ||
return this.isOdd() ? Long.MIN_VALUE : Long.ZERO; | ||
return ZERO; | ||
if (this.eq(MIN_VALUE)) | ||
return multiplier.isOdd() ? MIN_VALUE : ZERO; | ||
if (multiplier.eq(MIN_VALUE)) | ||
return this.isOdd() ? MIN_VALUE : ZERO; | ||
@@ -819,3 +935,3 @@ if (this.isNegative()) { | ||
if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24)) | ||
return Long.fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); | ||
return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); | ||
@@ -856,3 +972,3 @@ // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. | ||
c48 &= 0xFFFF; | ||
return new Long((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); | ||
return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); | ||
}; | ||
@@ -876,14 +992,14 @@ | ||
LongPrototype.divide = function divide(divisor) { | ||
if (!Long.isLong(divisor)) | ||
divisor = Long.fromValue(divisor); | ||
if (!isLong(divisor)) | ||
divisor = fromValue(divisor); | ||
if (divisor.isZero()) | ||
throw Error('division by zero'); | ||
if (this.isZero()) | ||
return this.unsigned ? Long.UZERO : Long.ZERO; | ||
return this.unsigned ? UZERO : ZERO; | ||
var approx, rem, res; | ||
if (this.eq(Long.MIN_VALUE)) { | ||
if (divisor.eq(Long.ONE) || divisor.eq(Long.NEG_ONE)) | ||
return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE | ||
else if (divisor.eq(Long.MIN_VALUE)) | ||
return Long.ONE; | ||
if (this.eq(MIN_VALUE)) { | ||
if (divisor.eq(ONE) || divisor.eq(NEG_ONE)) | ||
return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE | ||
else if (divisor.eq(MIN_VALUE)) | ||
return ONE; | ||
else { | ||
@@ -893,4 +1009,4 @@ // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. | ||
approx = halfThis.div(divisor).shl(1); | ||
if (approx.eq(Long.ZERO)) { | ||
return divisor.isNegative() ? Long.ONE : Long.NEG_ONE; | ||
if (approx.eq(ZERO)) { | ||
return divisor.isNegative() ? ONE : NEG_ONE; | ||
} else { | ||
@@ -902,4 +1018,4 @@ rem = this.sub(divisor.mul(approx)); | ||
} | ||
} else if (divisor.eq(Long.MIN_VALUE)) | ||
return this.unsigned ? Long.UZERO : Long.ZERO; | ||
} else if (divisor.eq(MIN_VALUE)) | ||
return this.unsigned ? UZERO : ZERO; | ||
if (this.isNegative()) { | ||
@@ -917,3 +1033,3 @@ if (divisor.isNegative()) | ||
// remainder never becomes negative. | ||
res = Long.ZERO; | ||
res = ZERO; | ||
rem = this; | ||
@@ -928,11 +1044,11 @@ while (rem.gte(divisor)) { | ||
var log2 = Math.ceil(Math.log(approx) / Math.LN2), | ||
delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48), | ||
delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48), | ||
// Decrease the approximation until it is smaller than the remainder. Note | ||
// that if it is too large, the product overflows and is negative. | ||
approxRes = Long.fromNumber(approx), | ||
approxRes = fromNumber(approx), | ||
approxRem = approxRes.mul(divisor); | ||
while (approxRem.isNegative() || approxRem.gt(rem)) { | ||
approx -= delta; | ||
approxRes = Long.fromNumber(approx, this.unsigned); | ||
approxRes = fromNumber(approx, this.unsigned); | ||
approxRem = approxRes.mul(divisor); | ||
@@ -944,3 +1060,3 @@ } | ||
if (approxRes.isZero()) | ||
approxRes = Long.ONE; | ||
approxRes = ONE; | ||
@@ -969,4 +1085,4 @@ res = res.add(approxRes); | ||
LongPrototype.modulo = function modulo(divisor) { | ||
if (!Long.isLong(divisor)) | ||
divisor = Long.fromValue(divisor); | ||
if (!isLong(divisor)) | ||
divisor = fromValue(divisor); | ||
return this.sub(this.div(divisor).mul(divisor)); | ||
@@ -990,3 +1106,3 @@ }; | ||
LongPrototype.not = function not() { | ||
return new Long(~this.low, ~this.high, this.unsigned); | ||
return fromBits(~this.low, ~this.high, this.unsigned); | ||
}; | ||
@@ -1001,5 +1117,5 @@ | ||
LongPrototype.and = function and(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
return new Long(this.low & other.low, this.high & other.high, this.unsigned); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
return fromBits(this.low & other.low, this.high & other.high, this.unsigned); | ||
}; | ||
@@ -1014,5 +1130,5 @@ | ||
LongPrototype.or = function or(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
return new Long(this.low | other.low, this.high | other.high, this.unsigned); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
return fromBits(this.low | other.low, this.high | other.high, this.unsigned); | ||
}; | ||
@@ -1027,5 +1143,5 @@ | ||
LongPrototype.xor = function xor(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
return new Long(this.low ^ other.low, this.high ^ other.high, this.unsigned); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); | ||
}; | ||
@@ -1040,3 +1156,3 @@ | ||
LongPrototype.shiftLeft = function shiftLeft(numBits) { | ||
if (Long.isLong(numBits)) | ||
if (isLong(numBits)) | ||
numBits = numBits.toInt(); | ||
@@ -1046,5 +1162,5 @@ if ((numBits &= 63) === 0) | ||
else if (numBits < 32) | ||
return new Long(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); | ||
return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); | ||
else | ||
return new Long(0, this.low << (numBits - 32), this.unsigned); | ||
return fromBits(0, this.low << (numBits - 32), this.unsigned); | ||
}; | ||
@@ -1068,3 +1184,3 @@ | ||
LongPrototype.shiftRight = function shiftRight(numBits) { | ||
if (Long.isLong(numBits)) | ||
if (isLong(numBits)) | ||
numBits = numBits.toInt(); | ||
@@ -1074,5 +1190,5 @@ if ((numBits &= 63) === 0) | ||
else if (numBits < 32) | ||
return new Long((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); | ||
return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); | ||
else | ||
return new Long(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); | ||
return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); | ||
}; | ||
@@ -1096,3 +1212,3 @@ | ||
LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) { | ||
if (Long.isLong(numBits)) | ||
if (isLong(numBits)) | ||
numBits = numBits.toInt(); | ||
@@ -1106,7 +1222,7 @@ numBits &= 63; | ||
var low = this.low; | ||
return new Long((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned); | ||
return fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned); | ||
} else if (numBits === 32) | ||
return new Long(high, 0, this.unsigned); | ||
return fromBits(high, 0, this.unsigned); | ||
else | ||
return new Long(high >>> (numBits - 32), 0, this.unsigned); | ||
return fromBits(high >>> (numBits - 32), 0, this.unsigned); | ||
} | ||
@@ -1132,3 +1248,3 @@ }; | ||
return this; | ||
return new Long(this.low, this.high, false); | ||
return fromBits(this.low, this.high, false); | ||
}; | ||
@@ -1144,3 +1260,3 @@ | ||
return this; | ||
return new Long(this.low, this.high, true); | ||
return fromBits(this.low, this.high, true); | ||
}; | ||
@@ -1147,0 +1263,0 @@ |
@@ -6,17 +6,15 @@ /* | ||
*/ | ||
function p(){function c(a,c,b){this.low=a|0;this.high=c|0;this.unsigned=!!b}Object.defineProperty(c.prototype,"__isLong__",{value:!0,enumerable:!1,configurable:!1});c.isLong=function(a){return!0===(a&&a.__isLong__)};var r={},s={};c.fromInt=function(a,b){var d,e;if(b){a>>>=0;if(e=0<=a&&256>a)if(d=s[a])return d;d=new c(a,0>(a|0)?-1:0,!0);e&&(s[a]=d)}else{a|=0;if(e=-128<=a&&128>a)if(d=r[a])return d;d=new c(a,0>a?-1:0,!1);e&&(r[a]=d)}return d};c.fromNumber=function(a,b){b=!!b;return isNaN(a)||!isFinite(a)? | ||
c.ZERO:!b&&a<=-t?c.MIN_VALUE:!b&&a+1>=t?c.MAX_VALUE:b&&a>=u?c.MAX_UNSIGNED_VALUE:0>a?c.fromNumber(-a,b).neg():new c(a%4294967296|0,a/4294967296|0,b)};c.fromBits=function(a,b,d){return new c(a,b,d)};c.fromString=function(a,b,d){if(0===a.length)throw Error("number format error: empty string");if("NaN"===a||"Infinity"===a||"+Infinity"===a||"-Infinity"===a)return c.ZERO;"number"===typeof b&&(d=b,b=!1);d=d||10;if(2>d||36<d)throw Error("radix out of range: "+d);var e;if(0<(e=a.indexOf("-")))throw Error('number format error: interior "-" character: '+ | ||
a);if(0===e)return c.fromString(a.substring(1),b,d).neg();e=c.fromNumber(Math.pow(d,8));for(var f=c.ZERO,g=0;g<a.length;g+=8){var k=Math.min(8,a.length-g),l=parseInt(a.substring(g,g+k),d);8>k?(k=c.fromNumber(Math.pow(d,k)),f=f.mul(k).add(c.fromNumber(l))):(f=f.mul(e),f=f.add(c.fromNumber(l)))}f.unsigned=b;return f};c.fromValue=function(a){return a instanceof c?a:"number"===typeof a?c.fromNumber(a):"string"===typeof a?c.fromString(a):new c(a.low,a.high,a.unsigned)};var u=4294967296*4294967296,t=u/ | ||
2,v=c.fromInt(16777216);c.ZERO=c.fromInt(0);c.UZERO=c.fromInt(0,!0);c.ONE=c.fromInt(1);c.UONE=c.fromInt(1,!0);c.NEG_ONE=c.fromInt(-1);c.MAX_VALUE=new c(-1,2147483647,!1);c.MAX_UNSIGNED_VALUE=new c(-1,-1,!0);c.MIN_VALUE=new c(0,-2147483648,!1);var b=c.prototype;b.toInt=function(){return this.unsigned?this.low>>>0:this.low};b.toNumber=function(){return this.unsigned?4294967296*(this.high>>>0)+(this.low>>>0):4294967296*this.high+(this.low>>>0)};b.toString=function(a){a=a||10;if(2>a||36<a)throw RangeError("radix out of range: "+ | ||
a);if(this.isZero())return"0";var b;if(this.isNegative()){if(this.eq(c.MIN_VALUE)){b=c.fromNumber(a);var d=this.div(b);b=d.mul(b).sub(this);return d.toString(a)+b.toInt().toString(a)}return"-"+this.neg().toString(a)}d=c.fromNumber(Math.pow(a,6),this.unsigned);b=this;for(var e="";;){var f=b.div(d),g=(b.sub(f.mul(d)).toInt()>>>0).toString(a);b=f;if(b.isZero())return g+e;for(;6>g.length;)g="0"+g;e=""+g+e}};b.getHighBits=function(){return this.high};b.getHighBitsUnsigned=function(){return this.high>>> | ||
0};b.getLowBits=function(){return this.low};b.getLowBitsUnsigned=function(){return this.low>>>0};b.getNumBitsAbs=function(){if(this.isNegative())return this.eq(c.MIN_VALUE)?64:this.neg().getNumBitsAbs();for(var a=0!=this.high?this.high:this.low,b=31;0<b&&0==(a&1<<b);b--);return 0!=this.high?b+33:b+1};b.isZero=function(){return 0===this.high&&0===this.low};b.isNegative=function(){return!this.unsigned&&0>this.high};b.isPositive=function(){return this.unsigned||0<=this.high};b.isOdd=function(){return 1=== | ||
(this.low&1)};b.isEven=function(){return 0===(this.low&1)};b.equals=function(a){c.isLong(a)||(a=c.fromValue(a));return this.unsigned!==a.unsigned&&1===this.high>>>31&&1===a.high>>>31?!1:this.high===a.high&&this.low===a.low};b.eq=b.equals;b.notEquals=function(a){return!this.eq(a)};b.neq=b.notEquals;b.lessThan=function(a){return 0>this.compare(a)};b.lt=b.lessThan;b.lessThanOrEqual=function(a){return 0>=this.compare(a)};b.lte=b.lessThanOrEqual;b.greaterThan=function(a){return 0<this.compare(a)};b.gt= | ||
b.greaterThan;b.greaterThanOrEqual=function(a){return 0<=this.compare(a)};b.gte=b.greaterThanOrEqual;b.compare=function(a){c.isLong(a)||(a=c.fromValue(a));if(this.eq(a))return 0;var b=this.isNegative(),d=a.isNegative();return b&&!d?-1:!b&&d?1:this.unsigned?a.high>>>0>this.high>>>0||a.high===this.high&&a.low>>>0>this.low>>>0?-1:1:this.sub(a).isNegative()?-1:1};b.comp=b.compare;b.negate=function(){return!this.unsigned&&this.eq(c.MIN_VALUE)?c.MIN_VALUE:this.not().add(c.ONE)};b.neg=b.negate;b.add=function(a){c.isLong(a)|| | ||
(a=c.fromValue(a));var b=this.high>>>16,d=this.high&65535,e=this.low>>>16,f=a.high>>>16,g=a.high&65535,k=a.low>>>16,l;l=0+((this.low&65535)+(a.low&65535));a=0+(l>>>16);a+=e+k;e=0+(a>>>16);e+=d+g;d=0+(e>>>16);d=d+(b+f)&65535;return new c((a&65535)<<16|l&65535,d<<16|e&65535,this.unsigned)};b.subtract=function(a){c.isLong(a)||(a=c.fromValue(a));return this.add(a.neg())};b.sub=b.subtract;b.multiply=function(a){if(this.isZero())return c.ZERO;c.isLong(a)||(a=c.fromValue(a));if(a.isZero())return c.ZERO; | ||
if(this.eq(c.MIN_VALUE))return a.isOdd()?c.MIN_VALUE:c.ZERO;if(a.eq(c.MIN_VALUE))return this.isOdd()?c.MIN_VALUE:c.ZERO;if(this.isNegative())return a.isNegative()?this.neg().mul(a.neg()):this.neg().mul(a).neg();if(a.isNegative())return this.mul(a.neg()).neg();if(this.lt(v)&&a.lt(v))return c.fromNumber(this.toNumber()*a.toNumber(),this.unsigned);var b=this.high>>>16,d=this.high&65535,e=this.low>>>16,f=this.low&65535,g=a.high>>>16,k=a.high&65535,l=a.low>>>16;a=a.low&65535;var n,h,m,q;q=0+f*a;m=0+(q>>> | ||
16);m+=e*a;h=0+(m>>>16);m=(m&65535)+f*l;h+=m>>>16;m&=65535;h+=d*a;n=0+(h>>>16);h=(h&65535)+e*l;n+=h>>>16;h&=65535;h+=f*k;n+=h>>>16;h&=65535;n=n+(b*a+d*l+e*k+f*g)&65535;return new c(m<<16|q&65535,n<<16|h,this.unsigned)};b.mul=b.multiply;b.divide=function(a){c.isLong(a)||(a=c.fromValue(a));if(a.isZero())throw Error("division by zero");if(this.isZero())return this.unsigned?c.UZERO:c.ZERO;var b,d,e;if(this.eq(c.MIN_VALUE)){if(a.eq(c.ONE)||a.eq(c.NEG_ONE))return c.MIN_VALUE;if(a.eq(c.MIN_VALUE))return c.ONE; | ||
b=this.shr(1).div(a).shl(1);if(b.eq(c.ZERO))return a.isNegative()?c.ONE:c.NEG_ONE;d=this.sub(a.mul(b));return e=b.add(d.div(a))}if(a.eq(c.MIN_VALUE))return this.unsigned?c.UZERO:c.ZERO;if(this.isNegative())return a.isNegative()?this.neg().div(a.neg()):this.neg().div(a).neg();if(a.isNegative())return this.div(a.neg()).neg();e=c.ZERO;for(d=this;d.gte(a);){b=Math.max(1,Math.floor(d.toNumber()/a.toNumber()));for(var f=Math.ceil(Math.log(b)/Math.LN2),f=48>=f?1:Math.pow(2,f-48),g=c.fromNumber(b),k=g.mul(a);k.isNegative()|| | ||
k.gt(d);)b-=f,g=c.fromNumber(b,this.unsigned),k=g.mul(a);g.isZero()&&(g=c.ONE);e=e.add(g);d=d.sub(k)}return e};b.div=b.divide;b.modulo=function(a){c.isLong(a)||(a=c.fromValue(a));return this.sub(this.div(a).mul(a))};b.mod=b.modulo;b.not=function(){return new c(~this.low,~this.high,this.unsigned)};b.and=function(a){c.isLong(a)||(a=c.fromValue(a));return new c(this.low&a.low,this.high&a.high,this.unsigned)};b.or=function(a){c.isLong(a)||(a=c.fromValue(a));return new c(this.low|a.low,this.high|a.high, | ||
this.unsigned)};b.xor=function(a){c.isLong(a)||(a=c.fromValue(a));return new c(this.low^a.low,this.high^a.high,this.unsigned)};b.shiftLeft=function(a){c.isLong(a)&&(a=a.toInt());return 0===(a&=63)?this:32>a?new c(this.low<<a,this.high<<a|this.low>>>32-a,this.unsigned):new c(0,this.low<<a-32,this.unsigned)};b.shl=b.shiftLeft;b.shiftRight=function(a){c.isLong(a)&&(a=a.toInt());return 0===(a&=63)?this:32>a?new c(this.low>>>a|this.high<<32-a,this.high>>a,this.unsigned):new c(this.high>>a-32,0<=this.high? | ||
0:-1,this.unsigned)};b.shr=b.shiftRight;b.shiftRightUnsigned=function(a){c.isLong(a)&&(a=a.toInt());a&=63;if(0===a)return this;var b=this.high;return 32>a?new c(this.low>>>a|b<<32-a,b>>>a,this.unsigned):32===a?new c(b,0,this.unsigned):new c(b>>>a-32,0,this.unsigned)};b.shru=b.shiftRightUnsigned;b.toSigned=function(){return this.unsigned?new c(this.low,this.high,!1):this};b.toUnsigned=function(){return this.unsigned?this:new c(this.low,this.high,!0)};return c} | ||
"function"===typeof define&&define.amd?define([],p):"function"===typeof require&&"object"===typeof module&&module&&module.exports?module.exports=p():(this.dcodeIO=this.dcodeIO||{}).Long=p(); | ||
(function(d,g){"function"===typeof define&&define.amd?define([],g):"function"===typeof require&&"object"===typeof module&&module&&module.exports?module.exports=g():(d.dcodeIO=d.dcodeIO||{}).Long=g()})(this,function(){function d(a,b,c){this.low=a|0;this.high=b|0;this.unsigned=!!c}function g(a){return!0===(a&&a.__isLong__)}function m(a,b){var c,s;if(b){a>>>=0;if(s=0<=a&&256>a)if(c=z[a])return c;c=e(a,0>(a|0)?-1:0,!0);s&&(z[a]=c)}else{a|=0;if(s=-128<=a&&128>a)if(c=A[a])return c;c=e(a,0>a?-1:0,!1);s&& | ||
(A[a]=c)}return c}function n(a,b){if(isNaN(a)||!isFinite(a))return b?t:k;if(b){if(0>a)return t;if(a>=B)return C}else{if(a<=-D)return l;if(a+1>=D)return E}return 0>a?n(-a,b).neg():e(a%4294967296|0,a/4294967296|0,b)}function e(a,b,c){return new d(a,b,c)}function x(a,b,c){if(0===a.length)throw Error("empty string");if("NaN"===a||"Infinity"===a||"+Infinity"===a||"-Infinity"===a)return k;"number"===typeof b&&(c=b,b=!1);c=c||10;if(2>c||36<c)throw RangeError("radix");var s;if(0<(s=a.indexOf("-")))throw Error("interior hyphen"); | ||
if(0===s)return x(a.substring(1),b,c).neg();s=n(v(c,8));for(var e=k,f=0;f<a.length;f+=8){var d=Math.min(8,a.length-f),g=parseInt(a.substring(f,f+d),c);8>d?(d=n(v(c,d)),e=e.mul(d).add(n(g))):(e=e.mul(s),e=e.add(n(g)))}e.unsigned=b;return e}function p(a){return a instanceof d?a:"number"===typeof a?n(a):"string"===typeof a?x(a):e(a.low,a.high,a.unsigned)}Object.defineProperty(d.prototype,"__isLong__",{value:!0,enumerable:!1,configurable:!1});d.isLong=g;var A={},z={};d.fromInt=m;d.fromNumber=n;d.fromBits= | ||
e;var v=Math.pow;d.fromString=x;d.fromValue=p;var B=4294967296*4294967296,D=B/2,F=m(16777216),k=m(0);d.ZERO=k;var t=m(0,!0);d.UZERO=t;var q=m(1);d.ONE=q;var b=m(1,!0);d.UONE=b;var y=m(-1);d.NEG_ONE=y;var E=e(-1,2147483647,!1);d.MAX_VALUE=E;var C=e(-1,-1,!0);d.MAX_UNSIGNED_VALUE=C;var l=e(0,-2147483648,!1);d.MIN_VALUE=l;b=d.prototype;b.toInt=function(){return this.unsigned?this.low>>>0:this.low};b.toNumber=function(){return this.unsigned?4294967296*(this.high>>>0)+(this.low>>>0):4294967296*this.high+ | ||
(this.low>>>0)};b.toString=function(a){a=a||10;if(2>a||36<a)throw RangeError("radix");if(this.isZero())return"0";if(this.isNegative()){if(this.eq(l)){var b=n(a),c=this.div(b),b=c.mul(b).sub(this);return c.toString(a)+b.toInt().toString(a)}return"-"+this.neg().toString(a)}for(var c=n(v(a,6),this.unsigned),b=this,e="";;){var d=b.div(c),f=(b.sub(d.mul(c)).toInt()>>>0).toString(a),b=d;if(b.isZero())return f+e;for(;6>f.length;)f="0"+f;e=""+f+e}};b.getHighBits=function(){return this.high};b.getHighBitsUnsigned= | ||
function(){return this.high>>>0};b.getLowBits=function(){return this.low};b.getLowBitsUnsigned=function(){return this.low>>>0};b.getNumBitsAbs=function(){if(this.isNegative())return this.eq(l)?64:this.neg().getNumBitsAbs();for(var a=0!=this.high?this.high:this.low,b=31;0<b&&0==(a&1<<b);b--);return 0!=this.high?b+33:b+1};b.isZero=function(){return 0===this.high&&0===this.low};b.isNegative=function(){return!this.unsigned&&0>this.high};b.isPositive=function(){return this.unsigned||0<=this.high};b.isOdd= | ||
function(){return 1===(this.low&1)};b.isEven=function(){return 0===(this.low&1)};b.equals=function(a){g(a)||(a=p(a));return this.unsigned!==a.unsigned&&1===this.high>>>31&&1===a.high>>>31?!1:this.high===a.high&&this.low===a.low};b.eq=b.equals;b.notEquals=function(a){return!this.eq(a)};b.neq=b.notEquals;b.lessThan=function(a){return 0>this.comp(a)};b.lt=b.lessThan;b.lessThanOrEqual=function(a){return 0>=this.comp(a)};b.lte=b.lessThanOrEqual;b.greaterThan=function(a){return 0<this.comp(a)};b.gt=b.greaterThan; | ||
b.greaterThanOrEqual=function(a){return 0<=this.comp(a)};b.gte=b.greaterThanOrEqual;b.compare=function(a){g(a)||(a=p(a));if(this.eq(a))return 0;var b=this.isNegative(),c=a.isNegative();return b&&!c?-1:!b&&c?1:this.unsigned?a.high>>>0>this.high>>>0||a.high===this.high&&a.low>>>0>this.low>>>0?-1:1:this.sub(a).isNegative()?-1:1};b.comp=b.compare;b.negate=function(){return!this.unsigned&&this.eq(l)?l:this.not().add(q)};b.neg=b.negate;b.add=function(a){g(a)||(a=p(a));var b=this.high>>>16,c=this.high&65535, | ||
d=this.low>>>16,l=a.high>>>16,f=a.high&65535,n=a.low>>>16,k;k=0+((this.low&65535)+(a.low&65535));a=0+(k>>>16);a+=d+n;d=0+(a>>>16);d+=c+f;c=0+(d>>>16);c=c+(b+l)&65535;return e((a&65535)<<16|k&65535,c<<16|d&65535,this.unsigned)};b.subtract=function(a){g(a)||(a=p(a));return this.add(a.neg())};b.sub=b.subtract;b.multiply=function(a){if(this.isZero())return k;g(a)||(a=p(a));if(a.isZero())return k;if(this.eq(l))return a.isOdd()?l:k;if(a.eq(l))return this.isOdd()?l:k;if(this.isNegative())return a.isNegative()? | ||
this.neg().mul(a.neg()):this.neg().mul(a).neg();if(a.isNegative())return this.mul(a.neg()).neg();if(this.lt(F)&&a.lt(F))return n(this.toNumber()*a.toNumber(),this.unsigned);var b=this.high>>>16,c=this.high&65535,d=this.low>>>16,w=this.low&65535,f=a.high>>>16,m=a.high&65535,q=a.low>>>16;a=a.low&65535;var u,h,r,t;t=0+w*a;r=0+(t>>>16);r+=d*a;h=0+(r>>>16);r=(r&65535)+w*q;h+=r>>>16;r&=65535;h+=c*a;u=0+(h>>>16);h=(h&65535)+d*q;u+=h>>>16;h&=65535;h+=w*m;u+=h>>>16;h&=65535;u=u+(b*a+c*q+d*m+w*f)&65535;return e(r<< | ||
16|t&65535,u<<16|h,this.unsigned)};b.mul=b.multiply;b.divide=function(a){g(a)||(a=p(a));if(a.isZero())throw Error("division by zero");if(this.isZero())return this.unsigned?t:k;var b,c,d;if(this.eq(l)){if(a.eq(q)||a.eq(y))return l;if(a.eq(l))return q;b=this.shr(1).div(a).shl(1);if(b.eq(k))return a.isNegative()?q:y;c=this.sub(a.mul(b));return d=b.add(c.div(a))}if(a.eq(l))return this.unsigned?t:k;if(this.isNegative())return a.isNegative()?this.neg().div(a.neg()):this.neg().div(a).neg();if(a.isNegative())return this.div(a.neg()).neg(); | ||
d=k;for(c=this;c.gte(a);){b=Math.max(1,Math.floor(c.toNumber()/a.toNumber()));for(var e=Math.ceil(Math.log(b)/Math.LN2),e=48>=e?1:v(2,e-48),f=n(b),m=f.mul(a);m.isNegative()||m.gt(c);)b-=e,f=n(b,this.unsigned),m=f.mul(a);f.isZero()&&(f=q);d=d.add(f);c=c.sub(m)}return d};b.div=b.divide;b.modulo=function(a){g(a)||(a=p(a));return this.sub(this.div(a).mul(a))};b.mod=b.modulo;b.not=function(){return e(~this.low,~this.high,this.unsigned)};b.and=function(a){g(a)||(a=p(a));return e(this.low&a.low,this.high& | ||
a.high,this.unsigned)};b.or=function(a){g(a)||(a=p(a));return e(this.low|a.low,this.high|a.high,this.unsigned)};b.xor=function(a){g(a)||(a=p(a));return e(this.low^a.low,this.high^a.high,this.unsigned)};b.shiftLeft=function(a){g(a)&&(a=a.toInt());return 0===(a&=63)?this:32>a?e(this.low<<a,this.high<<a|this.low>>>32-a,this.unsigned):e(0,this.low<<a-32,this.unsigned)};b.shl=b.shiftLeft;b.shiftRight=function(a){g(a)&&(a=a.toInt());return 0===(a&=63)?this:32>a?e(this.low>>>a|this.high<<32-a,this.high>> | ||
a,this.unsigned):e(this.high>>a-32,0<=this.high?0:-1,this.unsigned)};b.shr=b.shiftRight;b.shiftRightUnsigned=function(a){g(a)&&(a=a.toInt());a&=63;if(0===a)return this;var b=this.high;return 32>a?e(this.low>>>a|b<<32-a,b>>>a,this.unsigned):32===a?e(b,0,this.unsigned):e(b>>>a-32,0,this.unsigned)};b.shru=b.shiftRightUnsigned;b.toSigned=function(){return this.unsigned?e(this.low,this.high,!1):this};b.toUnsigned=function(){return this.unsigned?this:e(this.low,this.high,!0)};return d}); |
@@ -8,3 +8,3 @@ Distributions | ||
* **[long.min.js](https://raw.githubusercontent.com/dcodeIO/Long.js/master/dist/long.min.js)** | ||
has been compiled with Closure Compiler using `--compilation_level=ADVANCED_OPTIMIZATIONS`. | ||
has been compiled with Closure Compiler. | ||
@@ -11,0 +11,0 @@ * **[long.min.js.gz](https://raw.githubusercontent.com/dcodeIO/Long.js/master/dist/long.min.js.gz)** |
@@ -66,2 +66,8 @@ /* | ||
/** | ||
* @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val | ||
* @returns {!Long} | ||
*/ | ||
Long.fromValue = function(val) {}; | ||
/** | ||
* @param {string} str | ||
@@ -275,2 +281,8 @@ * @param {(boolean|number)=} unsigned | ||
/** | ||
* @param {!Long|number|string} other | ||
* @return {number} | ||
*/ | ||
Long.prototype.comp = function(other) {}; | ||
/** | ||
* @return {!Long} | ||
@@ -277,0 +289,0 @@ */ |
@@ -6,3 +6,3 @@ { | ||
"source": { | ||
"include": ["dist/Long.js"] | ||
"include": ["dist/long.js"] | ||
}, | ||
@@ -9,0 +9,0 @@ "opts": { |
{ | ||
"name": "long", | ||
"version": "3.0.1", | ||
"version": "3.0.3", | ||
"author": "Daniel Wirtz <dcode@dcode.io>", | ||
@@ -29,3 +29,3 @@ "description": "A Long class for representing a 64-bit two's-complement integer value.", | ||
"make": "npm run-script build && npm run-script compile && npm run-script compress && npm test", | ||
"compile": "ccjs dist/long.js --compilation_level=ADVANCED_OPTIMIZATIONS --create_source_map=dist/long.min.map > dist/long.min.js", | ||
"compile": "ccjs dist/long.js --compilation_level=SIMPLE_OPTIMIZATIONS --create_source_map=dist/long.min.map > dist/long.min.js", | ||
"compress": "gzip -c -9 dist/long.min.js > dist/long.min.js.gz", | ||
@@ -32,0 +32,0 @@ "test": "node node_modules/testjs/bin/testjs tests/suite.js" |
@@ -223,3 +223,3 @@  | ||
#### Long#compare(other) | ||
#### Long#compare/comp(other) | ||
@@ -226,0 +226,0 @@ Compares this Long's value with the specified's. |
@@ -14,3 +14,3 @@ var MetaScript = require("metascript"), | ||
// Build | ||
console.log("Building Long with scope", JSON.stringify(scope, null, 2)); | ||
console.log("Building long.js with scope", JSON.stringify(scope, null, 2)); | ||
fs.writeFileSync( | ||
@@ -17,0 +17,0 @@ path.join(distDir, "long.js"), |
430
src/long.js
@@ -18,3 +18,3 @@ /** | ||
*/ | ||
this.low = low|0; | ||
this.low = low | 0; | ||
@@ -26,3 +26,3 @@ /** | ||
*/ | ||
this.high = high|0; | ||
this.high = high | 0; | ||
@@ -70,3 +70,14 @@ /** | ||
/** | ||
* @function | ||
* @param {*} obj Object | ||
* @returns {boolean} | ||
* @inner | ||
*/ | ||
function isLong(obj) { | ||
return (obj && obj["__isLong__"]) === true; | ||
} | ||
/** | ||
* Tests if the specified object is a Long. | ||
* @function | ||
* @param {*} obj Object | ||
@@ -76,5 +87,3 @@ * @returns {boolean} | ||
*/ | ||
Long.isLong = function isLong(obj) { | ||
return (obj && obj["__isLong__"]) === true; | ||
}; | ||
Long.isLong = isLong; | ||
@@ -96,38 +105,38 @@ /** | ||
/** | ||
* Returns a Long representing the given 32 bit integer value. | ||
* @param {number} value The 32 bit integer in question | ||
* @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
* @returns {!Long} The corresponding Long value | ||
* @expose | ||
* @param {number} value | ||
* @param {boolean=} unsigned | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
Long.fromInt = function fromInt(value, unsigned) { | ||
function fromInt(value, unsigned) { | ||
var obj, cachedObj, cache; | ||
if (!unsigned) { | ||
value = value | 0; | ||
if (cache = (-128 <= value && value < 128)) { | ||
cachedObj = INT_CACHE[value]; | ||
if (unsigned) { | ||
value >>>= 0; | ||
if (cache = (0 <= value && value < 256)) { | ||
cachedObj = UINT_CACHE[value]; | ||
if (cachedObj) | ||
return cachedObj; | ||
} | ||
obj = new Long(value, value < 0 ? -1 : 0, false); | ||
obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true); | ||
if (cache) | ||
INT_CACHE[value] = obj; | ||
UINT_CACHE[value] = obj; | ||
return obj; | ||
} else { | ||
value = value >>> 0; | ||
if (cache = (0 <= value && value < 256)) { | ||
cachedObj = UINT_CACHE[value]; | ||
value |= 0; | ||
if (cache = (-128 <= value && value < 128)) { | ||
cachedObj = INT_CACHE[value]; | ||
if (cachedObj) | ||
return cachedObj; | ||
} | ||
obj = new Long(value, (value | 0) < 0 ? -1 : 0, true); | ||
obj = fromBits(value, value < 0 ? -1 : 0, false); | ||
if (cache) | ||
UINT_CACHE[value] = obj; | ||
INT_CACHE[value] = obj; | ||
return obj; | ||
} | ||
}; | ||
} | ||
/** | ||
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. | ||
* @param {number} value The number in question | ||
* Returns a Long representing the given 32 bit integer value. | ||
* @function | ||
* @param {number} value The 32 bit integer in question | ||
* @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
@@ -137,20 +146,54 @@ * @returns {!Long} The corresponding Long value | ||
*/ | ||
Long.fromNumber = function fromNumber(value, unsigned) { | ||
unsigned = !!unsigned; | ||
Long.fromInt = fromInt; | ||
/** | ||
* @param {number} value | ||
* @param {boolean=} unsigned | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
function fromNumber(value, unsigned) { | ||
if (isNaN(value) || !isFinite(value)) | ||
return Long.ZERO; | ||
if (!unsigned && value <= -TWO_PWR_63_DBL) | ||
return Long.MIN_VALUE; | ||
if (!unsigned && value + 1 >= TWO_PWR_63_DBL) | ||
return Long.MAX_VALUE; | ||
if (unsigned && value >= TWO_PWR_64_DBL) | ||
return Long.MAX_UNSIGNED_VALUE; | ||
return unsigned ? UZERO : ZERO; | ||
if (unsigned) { | ||
if (value < 0) | ||
return UZERO; | ||
if (value >= TWO_PWR_64_DBL) | ||
return MAX_UNSIGNED_VALUE; | ||
} else { | ||
if (value <= -TWO_PWR_63_DBL) | ||
return MIN_VALUE; | ||
if (value + 1 >= TWO_PWR_63_DBL) | ||
return MAX_VALUE; | ||
} | ||
if (value < 0) | ||
return Long.fromNumber(-value, unsigned).neg(); | ||
return new Long((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); | ||
}; | ||
return fromNumber(-value, unsigned).neg(); | ||
return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); | ||
} | ||
/** | ||
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. | ||
* @function | ||
* @param {number} value The number in question | ||
* @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
* @returns {!Long} The corresponding Long value | ||
* @expose | ||
*/ | ||
Long.fromNumber = fromNumber; | ||
/** | ||
* @param {number} lowBits | ||
* @param {number} highBits | ||
* @param {boolean=} unsigned | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
function fromBits(lowBits, highBits, unsigned) { | ||
return new Long(lowBits, highBits, unsigned); | ||
} | ||
/** | ||
* Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is | ||
* assumed to use 32 bits. | ||
* @function | ||
* @param {number} lowBits The low 32 bits | ||
@@ -162,19 +205,25 @@ * @param {number} highBits The high 32 bits | ||
*/ | ||
Long.fromBits = function fromBits(lowBits, highBits, unsigned) { | ||
return new Long(lowBits, highBits, unsigned); | ||
}; | ||
Long.fromBits = fromBits; | ||
/** | ||
* Returns a Long representation of the given string, written using the specified radix. | ||
* @param {string} str The textual representation of the Long | ||
* @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
* @param {number=} radix The radix in which the text is written (2-36), defaults to 10 | ||
* @returns {!Long} The corresponding Long value | ||
* @expose | ||
* @function | ||
* @param {number} base | ||
* @param {number} exponent | ||
* @returns {number} | ||
* @inner | ||
*/ | ||
Long.fromString = function fromString(str, unsigned, radix) { | ||
var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4) | ||
/** | ||
* @param {string} str | ||
* @param {(boolean|number)=} unsigned | ||
* @param {number=} radix | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
function fromString(str, unsigned, radix) { | ||
if (str.length === 0) | ||
throw Error('number format error: empty string'); | ||
throw Error('empty string'); | ||
if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") | ||
return Long.ZERO; | ||
return ZERO; | ||
if (typeof unsigned === 'number') // For goog.math.long compatibility | ||
@@ -185,24 +234,25 @@ radix = unsigned, | ||
if (radix < 2 || 36 < radix) | ||
throw Error('radix out of range: ' + radix); | ||
throw RangeError('radix'); | ||
var p; | ||
if ((p = str.indexOf('-')) > 0) | ||
throw Error('number format error: interior "-" character: ' + str); | ||
else if (p === 0) | ||
return Long.fromString(str.substring(1), unsigned, radix).neg(); | ||
throw Error('interior hyphen'); | ||
else if (p === 0) { | ||
return fromString(str.substring(1), unsigned, radix).neg(); | ||
} | ||
// Do several (8) digits each time through the loop, so as to | ||
// minimize the calls to the very expensive emulated div. | ||
var radixToPower = Long.fromNumber(Math.pow(radix, 8)); | ||
var radixToPower = fromNumber(pow_dbl(radix, 8)); | ||
var result = Long.ZERO; | ||
var result = ZERO; | ||
for (var i = 0; i < str.length; i += 8) { | ||
var size = Math.min(8, str.length - i); | ||
var value = parseInt(str.substring(i, i + size), radix); | ||
var size = Math.min(8, str.length - i), | ||
value = parseInt(str.substring(i, i + size), radix); | ||
if (size < 8) { | ||
var power = Long.fromNumber(Math.pow(radix, size)); | ||
result = result.mul(power).add(Long.fromNumber(value)); | ||
var power = fromNumber(pow_dbl(radix, size)); | ||
result = result.mul(power).add(fromNumber(value)); | ||
} else { | ||
result = result.mul(radixToPower); | ||
result = result.add(Long.fromNumber(value)); | ||
result = result.add(fromNumber(value)); | ||
} | ||
@@ -212,21 +262,41 @@ } | ||
return result; | ||
}; | ||
} | ||
/** | ||
* Converts the specified value to a Long. | ||
* @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value | ||
* @returns {!Long} | ||
* Returns a Long representation of the given string, written using the specified radix. | ||
* @function | ||
* @param {string} str The textual representation of the Long | ||
* @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to `false` for signed | ||
* @param {number=} radix The radix in which the text is written (2-36), defaults to 10 | ||
* @returns {!Long} The corresponding Long value | ||
* @expose | ||
*/ | ||
Long.fromValue = function fromValue(val) { | ||
Long.fromString = fromString; | ||
/** | ||
* @function | ||
* @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val | ||
* @returns {!Long} | ||
* @inner | ||
*/ | ||
function fromValue(val) { | ||
if (val /* is compatible */ instanceof Long) | ||
return val; | ||
if (typeof val === 'number') | ||
return Long.fromNumber(val); | ||
return fromNumber(val); | ||
if (typeof val === 'string') | ||
return Long.fromString(val); | ||
return fromString(val); | ||
// Throws for non-objects, converts non-instanceof Long: | ||
return new Long(val.low, val.high, val.unsigned); | ||
}; | ||
return fromBits(val.low, val.high, val.unsigned); | ||
} | ||
/** | ||
* Converts the specified value to a Long. | ||
* @function | ||
* @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value | ||
* @returns {!Long} | ||
* @expose | ||
*/ | ||
Long.fromValue = fromValue; | ||
// NOTE: the compiler should inline these constant values below and then remove these variables, so there should be | ||
@@ -275,5 +345,11 @@ // no runtime penalty for these. | ||
*/ | ||
var TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL); | ||
var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL); | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var ZERO = fromInt(0); | ||
/** | ||
* Signed zero. | ||
@@ -283,5 +359,11 @@ * @type {!Long} | ||
*/ | ||
Long.ZERO = Long.fromInt(0); | ||
Long.ZERO = ZERO; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var UZERO = fromInt(0, true); | ||
/** | ||
* Unsigned zero. | ||
@@ -291,5 +373,11 @@ * @type {!Long} | ||
*/ | ||
Long.UZERO = Long.fromInt(0, true); | ||
Long.UZERO = UZERO; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var ONE = fromInt(1); | ||
/** | ||
* Signed one. | ||
@@ -299,5 +387,11 @@ * @type {!Long} | ||
*/ | ||
Long.ONE = Long.fromInt(1); | ||
Long.ONE = ONE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var UONE = fromInt(1, true); | ||
/** | ||
* Unsigned one. | ||
@@ -307,5 +401,11 @@ * @type {!Long} | ||
*/ | ||
Long.UONE = Long.fromInt(1, true); | ||
Long.UONE = UONE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var NEG_ONE = fromInt(-1); | ||
/** | ||
* Signed negative one. | ||
@@ -315,5 +415,11 @@ * @type {!Long} | ||
*/ | ||
Long.NEG_ONE = Long.fromInt(-1); | ||
Long.NEG_ONE = NEG_ONE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false); | ||
/** | ||
* Maximum signed value. | ||
@@ -323,5 +429,11 @@ * @type {!Long} | ||
*/ | ||
Long.MAX_VALUE = new Long(0xFFFFFFFF|0, 0x7FFFFFFF|0, false); | ||
Long.MAX_VALUE = MAX_VALUE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true); | ||
/** | ||
* Maximum unsigned value. | ||
@@ -331,5 +443,11 @@ * @type {!Long} | ||
*/ | ||
Long.MAX_UNSIGNED_VALUE = new Long(0xFFFFFFFF|0, 0xFFFFFFFF|0, true); | ||
Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE; | ||
/** | ||
* @type {!Long} | ||
* @inner | ||
*/ | ||
var MIN_VALUE = fromBits(0, 0x80000000|0, false); | ||
/** | ||
* Minimum signed value. | ||
@@ -339,3 +457,3 @@ * @type {!Long} | ||
*/ | ||
Long.MIN_VALUE = new Long(0, 0x80000000|0, false); | ||
Long.MIN_VALUE = MIN_VALUE; | ||
@@ -363,5 +481,4 @@ /** | ||
LongPrototype.toNumber = function toNumber() { | ||
if (this.unsigned) { | ||
if (this.unsigned) | ||
return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0); | ||
} | ||
return this.high * TWO_PWR_32_DBL + (this.low >>> 0); | ||
@@ -381,14 +498,13 @@ }; | ||
if (radix < 2 || 36 < radix) | ||
throw RangeError('radix out of range: ' + radix); | ||
throw RangeError('radix'); | ||
if (this.isZero()) | ||
return '0'; | ||
var rem; | ||
if (this.isNegative()) { // Unsigned Longs are never negative | ||
if (this.eq(Long.MIN_VALUE)) { | ||
if (this.eq(MIN_VALUE)) { | ||
// We need to change the Long value before it can be negated, so we remove | ||
// the bottom-most digit in this base and then recurse to do the rest. | ||
var radixLong = Long.fromNumber(radix); | ||
var div = this.div(radixLong); | ||
rem = div.mul(radixLong).sub(this); | ||
return div.toString(radix) + rem.toInt().toString(radix); | ||
var radixLong = fromNumber(radix), | ||
div = this.div(radixLong), | ||
rem1 = div.mul(radixLong).sub(this); | ||
return div.toString(radix) + rem1.toInt().toString(radix); | ||
} else | ||
@@ -400,4 +516,4 @@ return '-' + this.neg().toString(radix); | ||
// minimize the calls to the very expensive emulated div. | ||
var radixToPower = Long.fromNumber(Math.pow(radix, 6), this.unsigned); | ||
rem = this; | ||
var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned), | ||
rem = this; | ||
var result = ''; | ||
@@ -462,3 +578,3 @@ while (true) { | ||
if (this.isNegative()) // Unsigned Longs are never negative | ||
return this.eq(Long.MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); | ||
return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); | ||
var val = this.high != 0 ? this.high : this.low; | ||
@@ -523,4 +639,4 @@ for (var bit = 31; bit > 0; bit--) | ||
LongPrototype.equals = function equals(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) | ||
@@ -566,3 +682,3 @@ return false; | ||
LongPrototype.lessThan = function lessThan(other) { | ||
return this.compare(/* validates */ other) < 0; | ||
return this.comp(/* validates */ other) < 0; | ||
}; | ||
@@ -586,3 +702,3 @@ | ||
LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) { | ||
return this.compare(/* validates */ other) <= 0; | ||
return this.comp(/* validates */ other) <= 0; | ||
}; | ||
@@ -606,3 +722,3 @@ | ||
LongPrototype.greaterThan = function greaterThan(other) { | ||
return this.compare(/* validates */ other) > 0; | ||
return this.comp(/* validates */ other) > 0; | ||
}; | ||
@@ -626,3 +742,3 @@ | ||
LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) { | ||
return this.compare(/* validates */ other) >= 0; | ||
return this.comp(/* validates */ other) >= 0; | ||
}; | ||
@@ -647,4 +763,4 @@ | ||
LongPrototype.compare = function compare(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
if (this.eq(other)) | ||
@@ -681,5 +797,5 @@ return 0; | ||
LongPrototype.negate = function negate() { | ||
if (!this.unsigned && this.eq(Long.MIN_VALUE)) | ||
return Long.MIN_VALUE; | ||
return this.not().add(Long.ONE); | ||
if (!this.unsigned && this.eq(MIN_VALUE)) | ||
return MIN_VALUE; | ||
return this.not().add(ONE); | ||
}; | ||
@@ -702,4 +818,4 @@ | ||
LongPrototype.add = function add(addend) { | ||
if (!Long.isLong(addend)) | ||
addend = Long.fromValue(addend); | ||
if (!isLong(addend)) | ||
addend = fromValue(addend); | ||
@@ -730,3 +846,3 @@ // Divide each number into 4 chunks of 16 bits, and then sum the chunks. | ||
c48 &= 0xFFFF; | ||
return new Long((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); | ||
return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); | ||
}; | ||
@@ -741,4 +857,4 @@ | ||
LongPrototype.subtract = function subtract(subtrahend) { | ||
if (!Long.isLong(subtrahend)) | ||
subtrahend = Long.fromValue(subtrahend); | ||
if (!isLong(subtrahend)) | ||
subtrahend = fromValue(subtrahend); | ||
return this.add(subtrahend.neg()); | ||
@@ -764,11 +880,11 @@ }; | ||
if (this.isZero()) | ||
return Long.ZERO; | ||
if (!Long.isLong(multiplier)) | ||
multiplier = Long.fromValue(multiplier); | ||
return ZERO; | ||
if (!isLong(multiplier)) | ||
multiplier = fromValue(multiplier); | ||
if (multiplier.isZero()) | ||
return Long.ZERO; | ||
if (this.eq(Long.MIN_VALUE)) | ||
return multiplier.isOdd() ? Long.MIN_VALUE : Long.ZERO; | ||
if (multiplier.eq(Long.MIN_VALUE)) | ||
return this.isOdd() ? Long.MIN_VALUE : Long.ZERO; | ||
return ZERO; | ||
if (this.eq(MIN_VALUE)) | ||
return multiplier.isOdd() ? MIN_VALUE : ZERO; | ||
if (multiplier.eq(MIN_VALUE)) | ||
return this.isOdd() ? MIN_VALUE : ZERO; | ||
@@ -785,3 +901,3 @@ if (this.isNegative()) { | ||
if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24)) | ||
return Long.fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); | ||
return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); | ||
@@ -822,3 +938,3 @@ // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. | ||
c48 &= 0xFFFF; | ||
return new Long((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); | ||
return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); | ||
}; | ||
@@ -842,14 +958,14 @@ | ||
LongPrototype.divide = function divide(divisor) { | ||
if (!Long.isLong(divisor)) | ||
divisor = Long.fromValue(divisor); | ||
if (!isLong(divisor)) | ||
divisor = fromValue(divisor); | ||
if (divisor.isZero()) | ||
throw Error('division by zero'); | ||
if (this.isZero()) | ||
return this.unsigned ? Long.UZERO : Long.ZERO; | ||
return this.unsigned ? UZERO : ZERO; | ||
var approx, rem, res; | ||
if (this.eq(Long.MIN_VALUE)) { | ||
if (divisor.eq(Long.ONE) || divisor.eq(Long.NEG_ONE)) | ||
return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE | ||
else if (divisor.eq(Long.MIN_VALUE)) | ||
return Long.ONE; | ||
if (this.eq(MIN_VALUE)) { | ||
if (divisor.eq(ONE) || divisor.eq(NEG_ONE)) | ||
return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE | ||
else if (divisor.eq(MIN_VALUE)) | ||
return ONE; | ||
else { | ||
@@ -859,4 +975,4 @@ // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. | ||
approx = halfThis.div(divisor).shl(1); | ||
if (approx.eq(Long.ZERO)) { | ||
return divisor.isNegative() ? Long.ONE : Long.NEG_ONE; | ||
if (approx.eq(ZERO)) { | ||
return divisor.isNegative() ? ONE : NEG_ONE; | ||
} else { | ||
@@ -868,4 +984,4 @@ rem = this.sub(divisor.mul(approx)); | ||
} | ||
} else if (divisor.eq(Long.MIN_VALUE)) | ||
return this.unsigned ? Long.UZERO : Long.ZERO; | ||
} else if (divisor.eq(MIN_VALUE)) | ||
return this.unsigned ? UZERO : ZERO; | ||
if (this.isNegative()) { | ||
@@ -883,3 +999,3 @@ if (divisor.isNegative()) | ||
// remainder never becomes negative. | ||
res = Long.ZERO; | ||
res = ZERO; | ||
rem = this; | ||
@@ -894,11 +1010,11 @@ while (rem.gte(divisor)) { | ||
var log2 = Math.ceil(Math.log(approx) / Math.LN2), | ||
delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48), | ||
delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48), | ||
// Decrease the approximation until it is smaller than the remainder. Note | ||
// that if it is too large, the product overflows and is negative. | ||
approxRes = Long.fromNumber(approx), | ||
approxRes = fromNumber(approx), | ||
approxRem = approxRes.mul(divisor); | ||
while (approxRem.isNegative() || approxRem.gt(rem)) { | ||
approx -= delta; | ||
approxRes = Long.fromNumber(approx, this.unsigned); | ||
approxRes = fromNumber(approx, this.unsigned); | ||
approxRem = approxRes.mul(divisor); | ||
@@ -910,3 +1026,3 @@ } | ||
if (approxRes.isZero()) | ||
approxRes = Long.ONE; | ||
approxRes = ONE; | ||
@@ -935,4 +1051,4 @@ res = res.add(approxRes); | ||
LongPrototype.modulo = function modulo(divisor) { | ||
if (!Long.isLong(divisor)) | ||
divisor = Long.fromValue(divisor); | ||
if (!isLong(divisor)) | ||
divisor = fromValue(divisor); | ||
return this.sub(this.div(divisor).mul(divisor)); | ||
@@ -956,3 +1072,3 @@ }; | ||
LongPrototype.not = function not() { | ||
return new Long(~this.low, ~this.high, this.unsigned); | ||
return fromBits(~this.low, ~this.high, this.unsigned); | ||
}; | ||
@@ -967,5 +1083,5 @@ | ||
LongPrototype.and = function and(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
return new Long(this.low & other.low, this.high & other.high, this.unsigned); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
return fromBits(this.low & other.low, this.high & other.high, this.unsigned); | ||
}; | ||
@@ -980,5 +1096,5 @@ | ||
LongPrototype.or = function or(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
return new Long(this.low | other.low, this.high | other.high, this.unsigned); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
return fromBits(this.low | other.low, this.high | other.high, this.unsigned); | ||
}; | ||
@@ -993,5 +1109,5 @@ | ||
LongPrototype.xor = function xor(other) { | ||
if (!Long.isLong(other)) | ||
other = Long.fromValue(other); | ||
return new Long(this.low ^ other.low, this.high ^ other.high, this.unsigned); | ||
if (!isLong(other)) | ||
other = fromValue(other); | ||
return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); | ||
}; | ||
@@ -1006,3 +1122,3 @@ | ||
LongPrototype.shiftLeft = function shiftLeft(numBits) { | ||
if (Long.isLong(numBits)) | ||
if (isLong(numBits)) | ||
numBits = numBits.toInt(); | ||
@@ -1012,5 +1128,5 @@ if ((numBits &= 63) === 0) | ||
else if (numBits < 32) | ||
return new Long(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); | ||
return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); | ||
else | ||
return new Long(0, this.low << (numBits - 32), this.unsigned); | ||
return fromBits(0, this.low << (numBits - 32), this.unsigned); | ||
}; | ||
@@ -1034,3 +1150,3 @@ | ||
LongPrototype.shiftRight = function shiftRight(numBits) { | ||
if (Long.isLong(numBits)) | ||
if (isLong(numBits)) | ||
numBits = numBits.toInt(); | ||
@@ -1040,5 +1156,5 @@ if ((numBits &= 63) === 0) | ||
else if (numBits < 32) | ||
return new Long((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); | ||
return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); | ||
else | ||
return new Long(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); | ||
return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); | ||
}; | ||
@@ -1062,3 +1178,3 @@ | ||
LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) { | ||
if (Long.isLong(numBits)) | ||
if (isLong(numBits)) | ||
numBits = numBits.toInt(); | ||
@@ -1072,7 +1188,7 @@ numBits &= 63; | ||
var low = this.low; | ||
return new Long((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned); | ||
return fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned); | ||
} else if (numBits === 32) | ||
return new Long(high, 0, this.unsigned); | ||
return fromBits(high, 0, this.unsigned); | ||
else | ||
return new Long(high >>> (numBits - 32), 0, this.unsigned); | ||
return fromBits(high >>> (numBits - 32), 0, this.unsigned); | ||
} | ||
@@ -1098,3 +1214,3 @@ }; | ||
return this; | ||
return new Long(this.low, this.high, false); | ||
return fromBits(this.low, this.high, false); | ||
}; | ||
@@ -1110,3 +1226,3 @@ | ||
return this; | ||
return new Long(this.low, this.high, true); | ||
return fromBits(this.low, this.high, true); | ||
}; |
@@ -54,2 +54,22 @@ /* | ||
}, | ||
"dispose": function(test) { | ||
if (!Long.dispose) { | ||
test.log("Not supported"); | ||
test.done(); | ||
return; | ||
} | ||
var inst = Long.fromInt(0); | ||
test.strictEqual(Long.dispose(inst), false); // cached, not reused | ||
Long.dispose(inst); | ||
var inst2 = Long.fromInt(1); | ||
test.notStrictEqual(inst, inst2); | ||
test.done(); | ||
inst = Long.fromNumber(0); | ||
test.strictEqual(Long.dispose(inst), true); // not cached (our first inst is), reused | ||
inst2 = Long.fromNumber(1); | ||
test.strictEqual(inst, inst2); | ||
test.done(); | ||
}, | ||
@@ -56,0 +76,0 @@ "unsigned": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
191697
1.18%3440
6.97%