diff --git a/.travis.yml b/.travis.yml index d523c5f..58f2371 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - 0.4 - 0.6 - 0.8 - 0.10 diff --git a/README.md b/README.md index 2e48bd8..47be29c 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ Documentation Downloads --------- +* [Distributions](https://github.com/dcodeIO/Long.js/tree/master/dist) * [ZIP-Archive](https://github.com/dcodeIO/Long.js/archive/master.zip) * [Tarball](https://github.com/dcodeIO/Long.js/tarball/master) diff --git a/Long.js b/dist/Long.js similarity index 100% rename from Long.js rename to dist/Long.js diff --git a/Long.min.js b/dist/Long.min.js similarity index 100% rename from Long.min.js rename to dist/Long.min.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..c26d553 --- /dev/null +++ b/index.js @@ -0,0 +1,20 @@ +/* + Copyright 2013 Daniel Wirtz + Copyright 2009 The Closure Library Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS-IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +var path = require("path"), + Long = require(path.join(__dirname, "dist", "Long.js")); + +module.exports = Long; diff --git a/jsdoc.json b/jsdoc.json index ad36e08..1225c8a 100644 --- a/jsdoc.json +++ b/jsdoc.json @@ -3,7 +3,7 @@ "allowUnknownTags": true }, "source": { - "include": ["Long.js"] + "include": ["dist/Long.js"] }, "opts": { "template": "templates/esoccer", diff --git a/package.json b/package.json index b1d1fdb..8a90169 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.1.3", "author": "Daniel Wirtz ", "description": "A Long class for representing a 64-bit two's-complement integer value.", - "main": "Long.js", + "main": "index.js", "repository": { "type": "git", "url": "https://github.com/dcodeIO/Long.js.git" @@ -20,8 +20,8 @@ "node": ">=0.6" }, "scripts": { - "make": "npm run-script compile && npm test && npm run-script jsdoc", - "compile": "ccjs Long.js --compilation_level=ADVANCED_OPTIMIZATIONS --output_wrapper=\"(function(){%output%})();\" > Long.min.js", + "make": "cp src/Long.js dist/Long.js && npm run-script compile && npm test && npm run-script jsdoc", + "compile": "ccjs dist/Long.js --compilation_level=ADVANCED_OPTIMIZATIONS --output_wrapper=\"(function(){%output%})();\" > dist/Long.min.js", "test": "node node_modules/testjs/bin/testjs tests/suite.js", "jsdoc": "jsdoc -c jsdoc.json" } diff --git a/src/Long.js b/src/Long.js new file mode 100644 index 0000000..cde012d --- /dev/null +++ b/src/Long.js @@ -0,0 +1,944 @@ +/* + Copyright 2013 Daniel Wirtz + Copyright 2009 The Closure Library Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS-IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +/** + * @license Long.js (c) 2013 Daniel Wirtz + * Released under the Apache License, Version 2.0 + * Derived from goog.math.Long from the Closure Library + * see: https://github.com/dcodeIO/Long.js for details + */ +(function(global) { + "use strict"; + + /** + * Constructs a 64-bit two's-complement integer, given its low and high 32-bit + * values as *signed* integers. See the from* functions below for more + * convenient ways of constructing Longs. + * + * The internal representation of a long is the two given signed, 32-bit values. + * We use 32-bit pieces because these are the size of integers on which + * Javascript performs bit-operations. For operations like addition and + * multiplication, we split each number into 16-bit pieces, which can easily be + * multiplied within Javascript's floating-point representation without overflow + * or change in sign. + * + * In the algorithms below, we frequently reduce the negative case to the + * positive case by negating the input(s) and then post-processing the result. + * Note that we must ALWAYS check specially whether those values are MIN_VALUE + * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as + * a positive number, it overflows back into a negative). Not handling this + * case would often result in infinite recursion. + * + * @exports Long + * @class A Long class for representing a 64-bit two's-complement integer value. + * @param {number} low The low (signed) 32 bits of the long. + * @param {number} high The high (signed) 32 bits of the long. + * @param {boolean=} unsigned Whether unsigned or not. Defaults to `false` (signed). + * @constructor + */ + var Long = function(low, high, unsigned) { + + /** + * The low 32 bits as a signed value. + * @type {number} + * @expose + */ + this.low = low | 0; + + /** + * The high 32 bits as a signed value. + * @type {number} + * @expose + */ + this.high = high | 0; + + /** + * Whether unsigned or not. + * @type {boolean} + * @expose + */ + this.unsigned = !!unsigned; + }; + + // NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from* methods on which they depend. + + // NOTE: The following cache variables are used internally only and are therefore not exposed as properties of the + // Long class. + + /** + * A cache of the Long representations of small integer values. + * @type {!Object} + */ + var INT_CACHE = {}; + + /** + * A cache of the Long representations of small unsigned integer values. + * @type {!Object} + */ + var UINT_CACHE = {}; + + /** + * 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 (signed). + * @return {!Long} The corresponding Long value. + * @expose + */ + Long.fromInt = function(value, unsigned) { + var obj, cachedObj; + if (!unsigned) { + value = value | 0; + if (-128 <= value && value < 128) { + cachedObj = INT_CACHE[value]; + if (cachedObj) return cachedObj; + } + obj = new Long(value, value < 0 ? -1 : 0, false); + if (-128 <= value && value < 128) { + INT_CACHE[value] = obj; + } + return obj; + } else { + value = value >>> 0; + if (0 <= value && value < 256) { + cachedObj = UINT_CACHE[value]; + if (cachedObj) return cachedObj; + } + obj = new Long(value, (value | 0) < 0 ? -1 : 0, true); + if (0 <= value && value < 256) { + UINT_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. + * @param {boolean=} unsigned Whether unsigned or not. Defaults to false (signed). + * @return {!Long} The corresponding Long value. + * @expose + */ + Long.fromNumber = function(value, unsigned) { + unsigned = !!unsigned; + if (isNaN(value) || !isFinite(value)) { + return Long.ZERO; + } else if (!unsigned && value <= -TWO_PWR_63_DBL) { + return Long.MIN_SIGNED_VALUE; + } else if (unsigned && value <= 0) { + return Long.MIN_UNSIGNED_VALUE; + } else if (!unsigned && value + 1 >= TWO_PWR_63_DBL) { + return Long.MAX_SIGNED_VALUE; + } else if (unsigned && value >= TWO_PWR_64_DBL) { + return Long.MAX_UNSIGNED_VALUE; + } else if (value < 0) { + return Long.fromNumber(-value, false).negate(); + } else { + return new Long((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); + } + }; + + /** + * Returns a Long representing the 64bit integer that comes by concatenating the given low and high bits. Each is + * assumed to use 32 bits. + * @param {number} lowBits The low 32 bits. + * @param {number} highBits The high 32 bits. + * @param {boolean=} unsigned Whether unsigned or not. Defaults to false (signed). + * @return {!Long} The corresponding Long value. + * @expose + */ + Long.fromBits = function(lowBits, highBits, unsigned) { + return new Long(lowBits, highBits, unsigned); + }; + + /** + * Returns a Long representing the 64bit integer that comes by concatenating the given low, middle and high bits. + * Each is assumed to use 28 bits. + * @param {number} part0 The low 28 bits + * @param {number} part1 The middle 28 bits + * @param {number} part2 The high 28 (8) bits + * @param {boolean=} unsigned Whether unsigned or not. Defaults to false (signed). + * @return {!Long} + * @expose + */ + Long.from28Bits = function(part0, part1, part2, unsigned) { + // 00000000000000000000000000001111 11111111111111111111111122222222 2222222222222 + // LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH + return Long.fromBits(part0 | (part1 << 28), (part1 >>> 4) | (part2) << 24, unsigned); + }; + + /** + * Returns a Long representation of the given string, written using the given + * radix. + * @param {string} str The textual representation of the Long. + * @param {(boolean|number)=} unsigned Whether unsigned or not. Defaults to false (signed). + * @param {number=} radix The radix in which the text is written. + * @return {!Long} The corresponding Long value. + * @expose + */ + Long.fromString = function(str, unsigned, radix) { + if (str.length == 0) { + throw(new Error('number format error: empty string')); + } + if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") { + return Long.ZERO; + } + if (typeof unsigned === 'number') { // For goog.math.Long compatibility + radix = unsigned; + unsigned = false; + } + radix = radix || 10; + if (radix < 2 || 36 < radix) { + throw(new Error('radix out of range: ' + radix)); + } + + if (str.charAt(0) == '-') { + return Long.fromString(str.substring(1), unsigned, radix).negate(); + } else if (str.indexOf('-') >= 0) { + throw(new Error('number format error: interior "-" character: ' + str)); + } + + // 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 result = Long.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); + if (size < 8) { + var power = Long.fromNumber(Math.pow(radix, size)); + result = result.multiply(power).add(Long.fromNumber(value)); + } else { + result = result.multiply(radixToPower); + result = result.add(Long.fromNumber(value)); + } + } + result.unsigned = unsigned; + return result; + }; + + // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be + // no runtime penalty for these. + + // NOTE: The following constant values are used internally only and are therefore not exposed as properties of the + // Long class. + + /** + * @type {number} + */ + var TWO_PWR_16_DBL = 1 << 16; + + /** + * @type {number} + */ + var TWO_PWR_24_DBL = 1 << 24; + + /** + * @type {number} + */ + var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL; + + /** + * @type {number} + */ + var TWO_PWR_31_DBL = TWO_PWR_32_DBL / 2; + + /** + * @type {number} + */ + var TWO_PWR_48_DBL = TWO_PWR_32_DBL * TWO_PWR_16_DBL; + + /** + * @type {number} + */ + var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL; + + /** + * @type {number} + */ + var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2; + + /** + * @type {!Long} + */ + var TWO_PWR_24 = Long.fromInt(1 << 24); + + /** + * @type {!Long} + * @expose + */ + Long.ZERO = Long.fromInt(0); + + /** + * @type {!Long} + * @expose + */ + Long.UZERO = Long.fromInt(0, true); + + /** + * @type {!Long} + * @expose + */ + Long.ONE = Long.fromInt(1); + + /** + * @type {!Long} + * @expose + */ + Long.UONE = Long.fromInt(1, true); + + /** + * @type {!Long} + * @expose + */ + Long.NEG_ONE = Long.fromInt(-1); + + /** + * @type {!Long} + * @expose + */ + Long.MAX_SIGNED_VALUE = Long.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0, false); + + /** + * @type {!Long} + * @expose + */ + Long.MAX_UNSIGNED_VALUE = Long.fromBits(0xFFFFFFFF | 0, 0xFFFFFFFF | 0, true); + + /** + * Alias of {@link Long.MAX_SIGNED_VALUE} for goog.math.Long compatibility. + * @type {!Long} + * @expose + */ + Long.MAX_VALUE = Long.MAX_SIGNED_VALUE; + + /** + * @type {!Long} + * @expose + */ + Long.MIN_SIGNED_VALUE = Long.fromBits(0, 0x80000000 | 0, false); + + /** + * @type {!Long} + * @expose + */ + Long.MIN_UNSIGNED_VALUE = Long.fromBits(0, 0, true); + + /** + * Alias of {@link Long.MIN_SIGNED_VALUE} for goog.math.Long compatibility. + * @type {!Long} + * @expose + */ + Long.MIN_VALUE = Long.MIN_SIGNED_VALUE; + + /** + * @return {number} The value, assuming it is a 32-bit integer. + * @expose + */ + Long.prototype.toInt = function() { + return this.unsigned ? this.low >>> 0 : this.low; + }; + + /** + * @return {number} The closest floating-point representation to this value. + * @expose + */ + Long.prototype.toNumber = function() { + if (this.unsigned) { + return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0); + } + return this.high * TWO_PWR_32_DBL + (this.low >>> 0); + }; + + /** + * @param {number=} radix The radix in which the text should be written. + * @return {string} The textual representation of this value. + * @override + * @expose + */ + Long.prototype.toString = function(radix) { + radix = radix || 10; + if (radix < 2 || 36 < radix) { + throw(new Error('radix out of range: ' + radix)); + } + if (this.isZero()) { + return '0'; + } + var rem; + if (this.isNegative()) { // Unsigned Longs are never negative + if (this.equals(Long.MIN_SIGNED_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.multiply(radixLong).subtract(this); + return div.toString(radix) + rem.toInt().toString(radix); + } else { + return '-' + this.negate().toString(radix); + } + } + + // Do several (6) 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, 6)); + rem = this; + var result = ''; + while (true) { + var remDiv = rem.div(radixToPower); + var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt(); + var digits = intval.toString(radix); + rem = remDiv; + if (rem.isZero()) { + return digits + result; + } else { + while (digits.length < 6) { + digits = '0' + digits; + } + result = '' + digits + result; + } + } + }; + + /** + * @return {number} The high 32 bits as a signed value. + * @expose + */ + Long.prototype.getHighBits = function() { + return this.high; + }; + + /** + * @return {number} The high 32 bits as an unsigned value. + * @expose + */ + Long.prototype.getHighBitsUnsigned = function() { + return this.high >>> 0; + }; + + /** + * @return {number} The low 32 bits as a signed value. + * @expose + */ + Long.prototype.getLowBits = function() { + return this.low; + }; + + /** + * @return {number} The low 32 bits as an unsigned value. + * @expose + */ + Long.prototype.getLowBitsUnsigned = function() { + return this.low >>> 0; + }; + + /** + * @return {number} Returns the number of bits needed to represent the absolute + * value of this Long. + * @expose + */ + Long.prototype.getNumBitsAbs = function() { + if (this.isNegative()) { // Unsigned Longs are never negative + if (this.equals(Long.MIN_SIGNED_VALUE)) { + return 64; + } else { + return this.negate().getNumBitsAbs(); + } + } else { + var val = this.high != 0 ? this.high : this.low; + for (var bit = 31; bit > 0; bit--) { + if ((val & (1 << bit)) != 0) { + break; + } + } + return this.high != 0 ? bit + 33 : bit + 1; + } + }; + + /** + * @return {boolean} Whether this value is zero. + * @expose + */ + Long.prototype.isZero = function() { + return this.high == 0 && this.low == 0; + }; + + /** + * @return {boolean} Whether this value is negative. + * @expose + */ + Long.prototype.isNegative = function() { + return !this.unsigned && this.high < 0; + }; + + /** + * @return {boolean} Whether this value is odd. + * @expose + */ + Long.prototype.isOdd = function() { + return (this.low & 1) == 1; + }; + + /** + * @return {boolean} Whether this value is even. + */ + Long.prototype.isEven = function() { + return (this.low & 1) == 0; + }; + + /** + * @param {Long} other Long to compare against. + * @return {boolean} Whether this Long equals the other. + * @expose + */ + Long.prototype.equals = function(other) { + if (this.unsigned != other.unsigned && (this.high >>> 31) != (other.high >>> 31)) return false; + return (this.high == other.high) && (this.low == other.low); + }; + + /** + * @param {Long} other Long to compare against. + * @return {boolean} Whether this Long does not equal the other. + * @expose + */ + Long.prototype.notEquals = function(other) { + return !this.equals(other); + }; + + /** + * @param {Long} other Long to compare against. + * @return {boolean} Whether this Long is less than the other. + * @expose + */ + Long.prototype.lessThan = function(other) { + return this.compare(other) < 0; + }; + + /** + * @param {Long} other Long to compare against. + * @return {boolean} Whether this Long is less than or equal to the other. + * @expose + */ + Long.prototype.lessThanOrEqual = function(other) { + return this.compare(other) <= 0; + }; + + /** + * @param {Long} other Long to compare against. + * @return {boolean} Whether this Long is greater than the other. + * @expose + */ + Long.prototype.greaterThan = function(other) { + return this.compare(other) > 0; + }; + + /** + * @param {Long} other Long to compare against. + * @return {boolean} Whether this Long is greater than or equal to the other. + * @expose + */ + Long.prototype.greaterThanOrEqual = function(other) { + return this.compare(other) >= 0; + }; + + /** + * Compares this Long with the given one. + * @param {Long} other Long to compare against. + * @return {number} 0 if they are the same, 1 if the this is greater, and -1 + * if the given one is greater. + * @expose + */ + Long.prototype.compare = function(other) { + if (this.equals(other)) { + return 0; + } + var thisNeg = this.isNegative(); + var otherNeg = other.isNegative(); + if (thisNeg && !otherNeg) return -1; + if (!thisNeg && otherNeg) return 1; + if (!this.unsigned) { + // At this point the signs are the same + return this.subtract(other).isNegative() ? -1 : 1; + } else { + // Both are positive if at least one is unsigned + return (other.high >>> 0) > (this.high >>> 0) || (other.high == this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1; + } + }; + + /** + * @return {!Long} The negation of this value. + * @expose + */ + Long.prototype.negate = function() { + if (!this.unsigned && this.equals(Long.MIN_SIGNED_VALUE)) { + return Long.MIN_SIGNED_VALUE; + } + return this.not().add(Long.ONE); + }; + + /** + * Returns the sum of this and the given Long. + * @param {Long} other Long to add to this one. + * @return {!Long} The sum of this and the given Long. + * @expose + */ + Long.prototype.add = function(other) { + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + var a48 = this.high >>> 16; + var a32 = this.high & 0xFFFF; + var a16 = this.low >>> 16; + var a00 = this.low & 0xFFFF; + + var b48 = other.high >>> 16; + var b32 = other.high & 0xFFFF; + var b16 = other.low >>> 16; + var b00 = other.low & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 + b48; + c48 &= 0xFFFF; + return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); + }; + + /** + * Returns the difference of this and the given Long. + * @param {Long} other Long to subtract from this. + * @return {!Long} The difference of this and the given Long. + * @expose + */ + Long.prototype.subtract = function(other) { + return this.add(other.negate()); + }; + + /** + * Returns the product of this and the given long. + * @param {Long} other Long to multiply with this. + * @return {!Long} The product of this and the other. + * @expose + */ + Long.prototype.multiply = function(other) { + if (this.isZero()) { + return Long.ZERO; + } else if (other.isZero()) { + return Long.ZERO; + } + + if (this.equals(Long.MIN_VALUE)) { + return other.isOdd() ? Long.MIN_VALUE : Long.ZERO; + } else if (other.equals(Long.MIN_VALUE)) { + return this.isOdd() ? Long.MIN_VALUE : Long.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().multiply(other.negate()); + } else { + return this.negate().multiply(other).negate(); + } + } else if (other.isNegative()) { + return this.multiply(other.negate()).negate(); + } + // If both longs are small, use float multiplication + if (this.lessThan(TWO_PWR_24) && + other.lessThan(TWO_PWR_24)) { + return Long.fromNumber(this.toNumber() * other.toNumber(), this.unsigned); + } + + // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. + // We can skip products that would overflow. + + var a48 = this.high >>> 16; + var a32 = this.high & 0xFFFF; + var a16 = this.low >>> 16; + var a00 = this.low & 0xFFFF; + + var b48 = other.high >>> 16; + var b32 = other.high & 0xFFFF; + var b16 = other.low >>> 16; + var b00 = other.low & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xFFFF; + return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); + }; + + /** + * Returns this Long divided by the given one. + * @param {Long} other Long by which to divide. + * @return {!Long} This Long divided by the given one. + * @expose + */ + Long.prototype.div = function(other) { + if (other.isZero()) { + throw(new Error('division by zero')); + } else if (this.isZero()) { + return this.unsigned ? Long.UZERO : Long.ZERO; + } + var approx, rem, res; + if (this.equals(Long.MIN_SIGNED_VALUE)) { + if (other.equals(Long.ONE) || other.equals(Long.NEG_ONE)) { + return Long.MIN_SIGNED_VALUE; // recall that -MIN_VALUE == MIN_VALUE + } else if (other.equals(Long.MIN_SIGNED_VALUE)) { + return Long.ONE; + } else { + // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. + var halfThis = this.shiftRight(1); + approx = halfThis.div(other).shiftLeft(1); + if (approx.equals(Long.ZERO)) { + return other.isNegative() ? Long.ONE : Long.NEG_ONE; + } else { + rem = this.subtract(other.multiply(approx)); + res = approx.add(rem.div(other)); + return res; + } + } + } else if (other.equals(Long.MIN_SIGNED_VALUE)) { + return this.unsigned ? Long.UZERO : Long.ZERO; + } + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().div(other.negate()); + } else { + return this.negate().div(other).negate(); + } + } else if (other.isNegative()) { + return this.div(other.negate()).negate(); + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + res = Long.ZERO; + rem = this; + while (rem.greaterThanOrEqual(other)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + var log2 = Math.ceil(Math.log(approx) / Math.LN2); + var delta = (log2 <= 48) ? 1 : Math.pow(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. + var approxRes = Long.fromNumber(approx, this.unsigned); + var approxRem = approxRes.multiply(other); + while (approxRem.isNegative() || approxRem.greaterThan(rem)) { + approx -= delta; + approxRes = Long.fromNumber(approx, this.unsigned); + approxRem = approxRes.multiply(other); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) { + approxRes = Long.ONE; + } + + res = res.add(approxRes); + rem = rem.subtract(approxRem); + } + return res; + }; + + /** + * Returns this Long modulo the given one. + * @param {Long} other Long by which to mod. + * @return {!Long} This Long modulo the given one. + * @expose + */ + Long.prototype.modulo = function(other) { + return this.subtract(this.div(other).multiply(other)); + }; + + /** + * @return {!Long} The bitwise-NOT of this value. + * @expose + */ + Long.prototype.not = function() { + return Long.fromBits(~this.low, ~this.high, this.unsigned); + }; + + /** + * Returns the bitwise-AND of this Long and the given one. + * @param {Long} other The Long with which to AND. + * @return {!Long} The bitwise-AND of this and the other. + * @expose + */ + Long.prototype.and = function(other) { + return Long.fromBits(this.low & other.low, this.high & other.high, this.unsigned); + }; + + /** + * Returns the bitwise-OR of this Long and the given one. + * @param {Long} other The Long with which to OR. + * @return {!Long} The bitwise-OR of this and the other. + * @expose + */ + Long.prototype.or = function(other) { + return Long.fromBits(this.low | other.low, this.high | other.high, this.unsigned); + }; + + /** + * Returns the bitwise-XOR of this Long and the given one. + * @param {Long} other The Long with which to XOR. + * @return {!Long} The bitwise-XOR of this and the other. + * @expose + */ + Long.prototype.xor = function(other) { + return Long.fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); + }; + + /** + * Returns this Long with bits shifted to the left by the given amount. + * @param {number} numBits The number of bits by which to shift. + * @return {!Long} This shifted to the left by the given amount. + * @expose + */ + Long.prototype.shiftLeft = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var low = this.low; + if (numBits < 32) { + var high = this.high; + return Long.fromBits(low << numBits, (high << numBits) | (low >>> (32 - numBits)), this.unsigned); + } else { + return Long.fromBits(0, low << (numBits - 32), this.unsigned); + } + } + }; + + /** + * Returns this Long with bits shifted to the right by the given amount. + * @param {number} numBits The number of bits by which to shift. + * @return {!Long} This shifted to the right by the given amount. + * @expose + */ + Long.prototype.shiftRight = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var high = this.high; + if (numBits < 32) { + var low = this.low; + return Long.fromBits((low >>> numBits) | (high << (32 - numBits)), high >> numBits, this.unsigned); + } else { + return Long.fromBits(high >> (numBits - 32), high >= 0 ? 0 : -1, this.unsigned); + } + } + }; + + /** + * Returns this Long with bits shifted to the right by the given amount, with + * the new top bits matching the current sign bit. + * @param {number} numBits The number of bits by which to shift. + * @return {!Long} This shifted to the right by the given amount, with + * zeros placed into the new leading bits. + * @expose + */ + Long.prototype.shiftRightUnsigned = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var high = this.high; + if (numBits < 32) { + var low = this.low; + return Long.fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned); + } else if (numBits == 32) { + return Long.fromBits(high, 0, this.unsigned); + } else { + return Long.fromBits(high >>> (numBits - 32), 0, this.unsigned); + } + } + }; + + /** + * @return {!Long} Signed long + * @expose + */ + Long.prototype.toSigned = function() { + var l = this.clone(); + l.unsigned = false; + return l; + }; + + /** + * @return {!Long} Unsigned long + * @expose + */ + Long.prototype.toUnsigned = function() { + var l = this.clone(); + l.unsigned = true; + return l; + }; + + /** + * @return {Long} Cloned instance with the same low/high bits and unsigned flag. + * @expose + */ + Long.prototype.clone = function() { + return new Long(this.low, this.high, this.unsigned); + }; + + // Enable module loading if available + if (typeof module != 'undefined' && module["exports"]) { // CommonJS + module["exports"] = Long; + } else if (typeof define != 'undefined' && define["amd"]) { // AMD + define("Math/Long", [], function() { return Long; }); + } else { // Shim + if (!global["dcodeIO"]) { + global["dcodeIO"] = {}; + } + global["dcodeIO"]["Long"] = Long; + } + +})(this); diff --git a/tests/suite.js b/tests/suite.js index 837a71b..4e1ff8c 100644 --- a/tests/suite.js +++ b/tests/suite.js @@ -17,7 +17,7 @@ /** * Long.js Pretty Simple Test Suite. */ -var Long = require(__dirname+"/../Long.min.js"); +var Long = require(__dirname+"/../index.js"); var suite = {