Path: blob/trunk/third_party/closure/goog/math/long.js
2868 views
// Copyright 2009 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Defines a Long class for representing a 64-bit two's-complement16* integer value, which faithfully simulates the behavior of a Java "long". This17* implementation is derived from LongLib in GWT.18*19*/2021goog.provide('goog.math.Long');2223goog.require('goog.asserts');24goog.require('goog.reflect');25262728/**29* Constructs a 64-bit two's-complement integer, given its low and high 32-bit30* values as *signed* integers. See the from* functions below for more31* convenient ways of constructing Longs.32*33* The internal representation of a long is the two given signed, 32-bit values.34* We use 32-bit pieces because these are the size of integers on which35* Javascript performs bit-operations. For operations like addition and36* multiplication, we split each number into 16-bit pieces, which can easily be37* multiplied within Javascript's floating-point representation without overflow38* or change in sign.39*40* In the algorithms below, we frequently reduce the negative case to the41* positive case by negating the input(s) and then post-processing the result.42* Note that we must ALWAYS check specially whether those values are MIN_VALUE43* (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as44* a positive number, it overflows back into a negative). Not handling this45* case would often result in infinite recursion.46*47* @param {number} low The low (signed) 32 bits of the long.48* @param {number} high The high (signed) 32 bits of the long.49* @struct50* @constructor51* @final52*/53goog.math.Long = function(low, high) {54/**55* @type {number}56* @private57*/58this.low_ = low | 0; // force into 32 signed bits.5960/**61* @type {number}62* @private63*/64this.high_ = high | 0; // force into 32 signed bits.65};666768// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the69// from* methods on which they depend.707172/**73* A cache of the Long representations of small integer values.74* @type {!Object<number, !goog.math.Long>}75* @private76*/77goog.math.Long.IntCache_ = {};787980/**81* A cache of the Long representations of common values.82* @type {!Object<goog.math.Long.ValueCacheId_, !goog.math.Long>}83* @private84*/85goog.math.Long.valueCache_ = {};8687/**88* Returns a cached long number representing the given (32-bit) integer value.89* @param {number} value The 32-bit integer in question.90* @return {!goog.math.Long} The corresponding Long value.91* @private92*/93goog.math.Long.getCachedIntValue_ = function(value) {94return goog.reflect.cache(goog.math.Long.IntCache_, value, function(val) {95return new goog.math.Long(val, val < 0 ? -1 : 0);96});97};9899/**100* The array of maximum values of a Long in string representation for a given101* radix between 2 and 36, inclusive.102* @private @const {!Array<string>}103*/104goog.math.Long.MAX_VALUE_FOR_RADIX_ = [105'', '', // unused106'111111111111111111111111111111111111111111111111111111111111111',107// base 2108'2021110011022210012102010021220101220221', // base 3109'13333333333333333333333333333333', // base 4110'1104332401304422434310311212', // base 5111'1540241003031030222122211', // base 6112'22341010611245052052300', // base 7113'777777777777777777777', // base 8114'67404283172107811827', // base 9115'9223372036854775807', // base 10116'1728002635214590697', // base 11117'41a792678515120367', // base 12118'10b269549075433c37', // base 13119'4340724c6c71dc7a7', // base 14120'160e2ad3246366807', // base 15121'7fffffffffffffff', // base 16122'33d3d8307b214008', // base 17123'16agh595df825fa7', // base 18124'ba643dci0ffeehh', // base 19125'5cbfjia3fh26ja7', // base 20126'2heiciiie82dh97', // base 21127'1adaibb21dckfa7', // base 22128'i6k448cf4192c2', // base 23129'acd772jnc9l0l7', // base 24130'64ie1focnn5g77', // base 25131'3igoecjbmca687', // base 26132'27c48l5b37oaop', // base 27133'1bk39f3ah3dmq7', // base 28134'q1se8f0m04isb', // base 29135'hajppbc1fc207', // base 30136'bm03i95hia437', // base 31137'7vvvvvvvvvvvv', // base 32138'5hg4ck9jd4u37', // base 33139'3tdtk1v8j6tpp', // base 34140'2pijmikexrxp7', // base 35141'1y2p0ij32e8e7' // base 36142];143144145/**146* The array of minimum values of a Long in string representation for a given147* radix between 2 and 36, inclusive.148* @private @const {!Array<string>}149*/150goog.math.Long.MIN_VALUE_FOR_RADIX_ = [151'', '', // unused152'-1000000000000000000000000000000000000000000000000000000000000000',153// base 2154'-2021110011022210012102010021220101220222', // base 3155'-20000000000000000000000000000000', // base 4156'-1104332401304422434310311213', // base 5157'-1540241003031030222122212', // base 6158'-22341010611245052052301', // base 7159'-1000000000000000000000', // base 8160'-67404283172107811828', // base 9161'-9223372036854775808', // base 10162'-1728002635214590698', // base 11163'-41a792678515120368', // base 12164'-10b269549075433c38', // base 13165'-4340724c6c71dc7a8', // base 14166'-160e2ad3246366808', // base 15167'-8000000000000000', // base 16168'-33d3d8307b214009', // base 17169'-16agh595df825fa8', // base 18170'-ba643dci0ffeehi', // base 19171'-5cbfjia3fh26ja8', // base 20172'-2heiciiie82dh98', // base 21173'-1adaibb21dckfa8', // base 22174'-i6k448cf4192c3', // base 23175'-acd772jnc9l0l8', // base 24176'-64ie1focnn5g78', // base 25177'-3igoecjbmca688', // base 26178'-27c48l5b37oaoq', // base 27179'-1bk39f3ah3dmq8', // base 28180'-q1se8f0m04isc', // base 29181'-hajppbc1fc208', // base 30182'-bm03i95hia438', // base 31183'-8000000000000', // base 32184'-5hg4ck9jd4u38', // base 33185'-3tdtk1v8j6tpq', // base 34186'-2pijmikexrxp8', // base 35187'-1y2p0ij32e8e8' // base 36188];189190191/**192* Returns a Long representing the given (32-bit) integer value.193* @param {number} value The 32-bit integer in question.194* @return {!goog.math.Long} The corresponding Long value.195*/196goog.math.Long.fromInt = function(value) {197var intValue = value | 0;198goog.asserts.assert(value === intValue, 'value should be a 32-bit integer');199200if (-128 <= intValue && intValue < 128) {201return goog.math.Long.getCachedIntValue_(intValue);202} else {203return new goog.math.Long(intValue, intValue < 0 ? -1 : 0);204}205};206207208/**209* Returns a Long representing the given value.210* NaN will be returned as zero. Infinity is converted to max value and211* -Infinity to min value.212* @param {number} value The number in question.213* @return {!goog.math.Long} The corresponding Long value.214*/215goog.math.Long.fromNumber = function(value) {216if (isNaN(value)) {217return goog.math.Long.getZero();218} else if (value <= -goog.math.Long.TWO_PWR_63_DBL_) {219return goog.math.Long.getMinValue();220} else if (value + 1 >= goog.math.Long.TWO_PWR_63_DBL_) {221return goog.math.Long.getMaxValue();222} else if (value < 0) {223return goog.math.Long.fromNumber(-value).negate();224} else {225return new goog.math.Long(226(value % goog.math.Long.TWO_PWR_32_DBL_) | 0,227(value / goog.math.Long.TWO_PWR_32_DBL_) | 0);228}229};230231232/**233* Returns a Long representing the 64-bit integer that comes by concatenating234* the given high and low bits. Each is assumed to use 32 bits.235* @param {number} lowBits The low 32-bits.236* @param {number} highBits The high 32-bits.237* @return {!goog.math.Long} The corresponding Long value.238*/239goog.math.Long.fromBits = function(lowBits, highBits) {240return new goog.math.Long(lowBits, highBits);241};242243244/**245* Returns a Long representation of the given string, written using the given246* radix.247* @param {string} str The textual representation of the Long.248* @param {number=} opt_radix The radix in which the text is written.249* @return {!goog.math.Long} The corresponding Long value.250*/251goog.math.Long.fromString = function(str, opt_radix) {252if (str.length == 0) {253throw Error('number format error: empty string');254}255256var radix = opt_radix || 10;257if (radix < 2 || 36 < radix) {258throw Error('radix out of range: ' + radix);259}260261if (str.charAt(0) == '-') {262return goog.math.Long.fromString(str.substring(1), radix).negate();263} else if (str.indexOf('-') >= 0) {264throw Error('number format error: interior "-" character: ' + str);265}266267// Do several (8) digits each time through the loop, so as to268// minimize the calls to the very expensive emulated div.269var radixToPower = goog.math.Long.fromNumber(Math.pow(radix, 8));270271var result = goog.math.Long.getZero();272for (var i = 0; i < str.length; i += 8) {273var size = Math.min(8, str.length - i);274var value = parseInt(str.substring(i, i + size), radix);275if (size < 8) {276var power = goog.math.Long.fromNumber(Math.pow(radix, size));277result = result.multiply(power).add(goog.math.Long.fromNumber(value));278} else {279result = result.multiply(radixToPower);280result = result.add(goog.math.Long.fromNumber(value));281}282}283return result;284};285286/**287* Returns the boolean value of whether the input string is within a Long's288* range. Assumes an input string containing only numeric characters with an289* optional preceding '-'.290* @param {string} str The textual representation of the Long.291* @param {number=} opt_radix The radix in which the text is written.292* @return {boolean} Whether the string is within the range of a Long.293*/294goog.math.Long.isStringInRange = function(str, opt_radix) {295var radix = opt_radix || 10;296if (radix < 2 || 36 < radix) {297throw Error('radix out of range: ' + radix);298}299300var extremeValue = (str.charAt(0) == '-') ?301goog.math.Long.MIN_VALUE_FOR_RADIX_[radix] :302goog.math.Long.MAX_VALUE_FOR_RADIX_[radix];303304if (str.length < extremeValue.length) {305return true;306} else if (str.length == extremeValue.length && str <= extremeValue) {307return true;308} else {309return false;310}311};312313// NOTE: the compiler should inline these constant values below and then remove314// these variables, so there should be no runtime penalty for these.315316317/**318* Number used repeated below in calculations. This must appear before the319* first call to any from* function below.320* @type {number}321* @private322*/323goog.math.Long.TWO_PWR_16_DBL_ = 1 << 16;324325326/**327* @type {number}328* @private329*/330goog.math.Long.TWO_PWR_32_DBL_ =331goog.math.Long.TWO_PWR_16_DBL_ * goog.math.Long.TWO_PWR_16_DBL_;332333334/**335* @type {number}336* @private337*/338goog.math.Long.TWO_PWR_64_DBL_ =339goog.math.Long.TWO_PWR_32_DBL_ * goog.math.Long.TWO_PWR_32_DBL_;340341342/**343* @type {number}344* @private345*/346goog.math.Long.TWO_PWR_63_DBL_ = goog.math.Long.TWO_PWR_64_DBL_ / 2;347348349/**350* @return {!goog.math.Long}351* @public352*/353goog.math.Long.getZero = function() {354return goog.math.Long.getCachedIntValue_(0);355};356357358/**359* @return {!goog.math.Long}360* @public361*/362goog.math.Long.getOne = function() {363return goog.math.Long.getCachedIntValue_(1);364};365366367/**368* @return {!goog.math.Long}369* @public370*/371goog.math.Long.getNegOne = function() {372return goog.math.Long.getCachedIntValue_(-1);373};374375376/**377* @return {!goog.math.Long}378* @public379*/380goog.math.Long.getMaxValue = function() {381return goog.reflect.cache(382goog.math.Long.valueCache_, goog.math.Long.ValueCacheId_.MAX_VALUE,383function() {384return goog.math.Long.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0);385});386};387388389/**390* @return {!goog.math.Long}391* @public392*/393goog.math.Long.getMinValue = function() {394return goog.reflect.cache(395goog.math.Long.valueCache_, goog.math.Long.ValueCacheId_.MIN_VALUE,396function() { return goog.math.Long.fromBits(0, 0x80000000 | 0); });397};398399400/**401* @return {!goog.math.Long}402* @public403*/404goog.math.Long.getTwoPwr24 = function() {405return goog.reflect.cache(406goog.math.Long.valueCache_, goog.math.Long.ValueCacheId_.TWO_PWR_24,407function() { return goog.math.Long.fromInt(1 << 24); });408};409410411/** @return {number} The value, assuming it is a 32-bit integer. */412goog.math.Long.prototype.toInt = function() {413return this.low_;414};415416417/** @return {number} The closest floating-point representation to this value. */418goog.math.Long.prototype.toNumber = function() {419return this.high_ * goog.math.Long.TWO_PWR_32_DBL_ +420this.getLowBitsUnsigned();421};422423424/**425* @param {number=} opt_radix The radix in which the text should be written.426* @return {string} The textual representation of this value.427* @override428*/429goog.math.Long.prototype.toString = function(opt_radix) {430var radix = opt_radix || 10;431if (radix < 2 || 36 < radix) {432throw Error('radix out of range: ' + radix);433}434435if (this.isZero()) {436return '0';437}438439if (this.isNegative()) {440if (this.equals(goog.math.Long.getMinValue())) {441// We need to change the Long value before it can be negated, so we remove442// the bottom-most digit in this base and then recurse to do the rest.443var radixLong = goog.math.Long.fromNumber(radix);444var div = this.div(radixLong);445var rem = div.multiply(radixLong).subtract(this);446return div.toString(radix) + rem.toInt().toString(radix);447} else {448return '-' + this.negate().toString(radix);449}450}451452// Do several (6) digits each time through the loop, so as to453// minimize the calls to the very expensive emulated div.454var radixToPower = goog.math.Long.fromNumber(Math.pow(radix, 6));455456var rem = this;457var result = '';458while (true) {459var remDiv = rem.div(radixToPower);460// The right shifting fixes negative values in the case when461// intval >= 2^31; for more details see462// https://github.com/google/closure-library/pull/498463var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt() >>> 0;464var digits = intval.toString(radix);465466rem = remDiv;467if (rem.isZero()) {468return digits + result;469} else {470while (digits.length < 6) {471digits = '0' + digits;472}473result = '' + digits + result;474}475}476};477478479/** @return {number} The high 32-bits as a signed value. */480goog.math.Long.prototype.getHighBits = function() {481return this.high_;482};483484485/** @return {number} The low 32-bits as a signed value. */486goog.math.Long.prototype.getLowBits = function() {487return this.low_;488};489490491/** @return {number} The low 32-bits as an unsigned value. */492goog.math.Long.prototype.getLowBitsUnsigned = function() {493return (this.low_ >= 0) ? this.low_ :494goog.math.Long.TWO_PWR_32_DBL_ + this.low_;495};496497498/**499* @return {number} Returns the number of bits needed to represent the absolute500* value of this Long.501*/502goog.math.Long.prototype.getNumBitsAbs = function() {503if (this.isNegative()) {504if (this.equals(goog.math.Long.getMinValue())) {505return 64;506} else {507return this.negate().getNumBitsAbs();508}509} else {510var val = this.high_ != 0 ? this.high_ : this.low_;511for (var bit = 31; bit > 0; bit--) {512if ((val & (1 << bit)) != 0) {513break;514}515}516return this.high_ != 0 ? bit + 33 : bit + 1;517}518};519520521/** @return {boolean} Whether this value is zero. */522goog.math.Long.prototype.isZero = function() {523return this.high_ == 0 && this.low_ == 0;524};525526527/** @return {boolean} Whether this value is negative. */528goog.math.Long.prototype.isNegative = function() {529return this.high_ < 0;530};531532533/** @return {boolean} Whether this value is odd. */534goog.math.Long.prototype.isOdd = function() {535return (this.low_ & 1) == 1;536};537538539/**540* @param {goog.math.Long} other Long to compare against.541* @return {boolean} Whether this Long equals the other.542*/543goog.math.Long.prototype.equals = function(other) {544return (this.high_ == other.high_) && (this.low_ == other.low_);545};546547548/**549* @param {goog.math.Long} other Long to compare against.550* @return {boolean} Whether this Long does not equal the other.551*/552goog.math.Long.prototype.notEquals = function(other) {553return (this.high_ != other.high_) || (this.low_ != other.low_);554};555556557/**558* @param {goog.math.Long} other Long to compare against.559* @return {boolean} Whether this Long is less than the other.560*/561goog.math.Long.prototype.lessThan = function(other) {562return this.compare(other) < 0;563};564565566/**567* @param {goog.math.Long} other Long to compare against.568* @return {boolean} Whether this Long is less than or equal to the other.569*/570goog.math.Long.prototype.lessThanOrEqual = function(other) {571return this.compare(other) <= 0;572};573574575/**576* @param {goog.math.Long} other Long to compare against.577* @return {boolean} Whether this Long is greater than the other.578*/579goog.math.Long.prototype.greaterThan = function(other) {580return this.compare(other) > 0;581};582583584/**585* @param {goog.math.Long} other Long to compare against.586* @return {boolean} Whether this Long is greater than or equal to the other.587*/588goog.math.Long.prototype.greaterThanOrEqual = function(other) {589return this.compare(other) >= 0;590};591592593/**594* Compares this Long with the given one.595* @param {goog.math.Long} other Long to compare against.596* @return {number} 0 if they are the same, 1 if the this is greater, and -1597* if the given one is greater.598*/599goog.math.Long.prototype.compare = function(other) {600if (this.equals(other)) {601return 0;602}603604var thisNeg = this.isNegative();605var otherNeg = other.isNegative();606if (thisNeg && !otherNeg) {607return -1;608}609if (!thisNeg && otherNeg) {610return 1;611}612613// at this point, the signs are the same, so subtraction will not overflow614if (this.subtract(other).isNegative()) {615return -1;616} else {617return 1;618}619};620621622/** @return {!goog.math.Long} The negation of this value. */623goog.math.Long.prototype.negate = function() {624if (this.equals(goog.math.Long.getMinValue())) {625return goog.math.Long.getMinValue();626} else {627return this.not().add(goog.math.Long.getOne());628}629};630631632/**633* Returns the sum of this and the given Long.634* @param {goog.math.Long} other Long to add to this one.635* @return {!goog.math.Long} The sum of this and the given Long.636*/637goog.math.Long.prototype.add = function(other) {638// Divide each number into 4 chunks of 16 bits, and then sum the chunks.639640var a48 = this.high_ >>> 16;641var a32 = this.high_ & 0xFFFF;642var a16 = this.low_ >>> 16;643var a00 = this.low_ & 0xFFFF;644645var b48 = other.high_ >>> 16;646var b32 = other.high_ & 0xFFFF;647var b16 = other.low_ >>> 16;648var b00 = other.low_ & 0xFFFF;649650var c48 = 0, c32 = 0, c16 = 0, c00 = 0;651c00 += a00 + b00;652c16 += c00 >>> 16;653c00 &= 0xFFFF;654c16 += a16 + b16;655c32 += c16 >>> 16;656c16 &= 0xFFFF;657c32 += a32 + b32;658c48 += c32 >>> 16;659c32 &= 0xFFFF;660c48 += a48 + b48;661c48 &= 0xFFFF;662return goog.math.Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);663};664665666/**667* Returns the difference of this and the given Long.668* @param {goog.math.Long} other Long to subtract from this.669* @return {!goog.math.Long} The difference of this and the given Long.670*/671goog.math.Long.prototype.subtract = function(other) {672return this.add(other.negate());673};674675676/**677* Returns the product of this and the given long.678* @param {goog.math.Long} other Long to multiply with this.679* @return {!goog.math.Long} The product of this and the other.680*/681goog.math.Long.prototype.multiply = function(other) {682if (this.isZero()) {683return goog.math.Long.getZero();684} else if (other.isZero()) {685return goog.math.Long.getZero();686}687688if (this.equals(goog.math.Long.getMinValue())) {689return other.isOdd() ? goog.math.Long.getMinValue() :690goog.math.Long.getZero();691} else if (other.equals(goog.math.Long.getMinValue())) {692return this.isOdd() ? goog.math.Long.getMinValue() :693goog.math.Long.getZero();694}695696if (this.isNegative()) {697if (other.isNegative()) {698return this.negate().multiply(other.negate());699} else {700return this.negate().multiply(other).negate();701}702} else if (other.isNegative()) {703return this.multiply(other.negate()).negate();704}705706// If both longs are small, use float multiplication707if (this.lessThan(goog.math.Long.getTwoPwr24()) &&708other.lessThan(goog.math.Long.getTwoPwr24())) {709return goog.math.Long.fromNumber(this.toNumber() * other.toNumber());710}711712// Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.713// We can skip products that would overflow.714715var a48 = this.high_ >>> 16;716var a32 = this.high_ & 0xFFFF;717var a16 = this.low_ >>> 16;718var a00 = this.low_ & 0xFFFF;719720var b48 = other.high_ >>> 16;721var b32 = other.high_ & 0xFFFF;722var b16 = other.low_ >>> 16;723var b00 = other.low_ & 0xFFFF;724725var c48 = 0, c32 = 0, c16 = 0, c00 = 0;726c00 += a00 * b00;727c16 += c00 >>> 16;728c00 &= 0xFFFF;729c16 += a16 * b00;730c32 += c16 >>> 16;731c16 &= 0xFFFF;732c16 += a00 * b16;733c32 += c16 >>> 16;734c16 &= 0xFFFF;735c32 += a32 * b00;736c48 += c32 >>> 16;737c32 &= 0xFFFF;738c32 += a16 * b16;739c48 += c32 >>> 16;740c32 &= 0xFFFF;741c32 += a00 * b32;742c48 += c32 >>> 16;743c32 &= 0xFFFF;744c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;745c48 &= 0xFFFF;746return goog.math.Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32);747};748749750/**751* Returns this Long divided by the given one.752* @param {goog.math.Long} other Long by which to divide.753* @return {!goog.math.Long} This Long divided by the given one.754*/755goog.math.Long.prototype.div = function(other) {756if (other.isZero()) {757throw Error('division by zero');758} else if (this.isZero()) {759return goog.math.Long.getZero();760}761762if (this.equals(goog.math.Long.getMinValue())) {763if (other.equals(goog.math.Long.getOne()) ||764other.equals(goog.math.Long.getNegOne())) {765return goog.math.Long.getMinValue(); // recall -MIN_VALUE == MIN_VALUE766} else if (other.equals(goog.math.Long.getMinValue())) {767return goog.math.Long.getOne();768} else {769// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.770var halfThis = this.shiftRight(1);771var approx = halfThis.div(other).shiftLeft(1);772if (approx.equals(goog.math.Long.getZero())) {773return other.isNegative() ? goog.math.Long.getOne() :774goog.math.Long.getNegOne();775} else {776var rem = this.subtract(other.multiply(approx));777var result = approx.add(rem.div(other));778return result;779}780}781} else if (other.equals(goog.math.Long.getMinValue())) {782return goog.math.Long.getZero();783}784785if (this.isNegative()) {786if (other.isNegative()) {787return this.negate().div(other.negate());788} else {789return this.negate().div(other).negate();790}791} else if (other.isNegative()) {792return this.div(other.negate()).negate();793}794795// Repeat the following until the remainder is less than other: find a796// floating-point that approximates remainder / other *from below*, add this797// into the result, and subtract it from the remainder. It is critical that798// the approximate value is less than or equal to the real value so that the799// remainder never becomes negative.800var res = goog.math.Long.getZero();801var rem = this;802while (rem.greaterThanOrEqual(other)) {803// Approximate the result of division. This may be a little greater or804// smaller than the actual value.805var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber()));806807// We will tweak the approximate result by changing it in the 48-th digit or808// the smallest non-fractional digit, whichever is larger.809var log2 = Math.ceil(Math.log(approx) / Math.LN2);810var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48);811812// Decrease the approximation until it is smaller than the remainder. Note813// that if it is too large, the product overflows and is negative.814var approxRes = goog.math.Long.fromNumber(approx);815var approxRem = approxRes.multiply(other);816while (approxRem.isNegative() || approxRem.greaterThan(rem)) {817approx -= delta;818approxRes = goog.math.Long.fromNumber(approx);819approxRem = approxRes.multiply(other);820}821822// We know the answer can't be zero... and actually, zero would cause823// infinite recursion since we would make no progress.824if (approxRes.isZero()) {825approxRes = goog.math.Long.getOne();826}827828res = res.add(approxRes);829rem = rem.subtract(approxRem);830}831return res;832};833834835/**836* Returns this Long modulo the given one.837* @param {goog.math.Long} other Long by which to mod.838* @return {!goog.math.Long} This Long modulo the given one.839*/840goog.math.Long.prototype.modulo = function(other) {841return this.subtract(this.div(other).multiply(other));842};843844845/** @return {!goog.math.Long} The bitwise-NOT of this value. */846goog.math.Long.prototype.not = function() {847return goog.math.Long.fromBits(~this.low_, ~this.high_);848};849850851/**852* Returns the bitwise-AND of this Long and the given one.853* @param {goog.math.Long} other The Long with which to AND.854* @return {!goog.math.Long} The bitwise-AND of this and the other.855*/856goog.math.Long.prototype.and = function(other) {857return goog.math.Long.fromBits(858this.low_ & other.low_, this.high_ & other.high_);859};860861862/**863* Returns the bitwise-OR of this Long and the given one.864* @param {goog.math.Long} other The Long with which to OR.865* @return {!goog.math.Long} The bitwise-OR of this and the other.866*/867goog.math.Long.prototype.or = function(other) {868return goog.math.Long.fromBits(869this.low_ | other.low_, this.high_ | other.high_);870};871872873/**874* Returns the bitwise-XOR of this Long and the given one.875* @param {goog.math.Long} other The Long with which to XOR.876* @return {!goog.math.Long} The bitwise-XOR of this and the other.877*/878goog.math.Long.prototype.xor = function(other) {879return goog.math.Long.fromBits(880this.low_ ^ other.low_, this.high_ ^ other.high_);881};882883884/**885* Returns this Long with bits shifted to the left by the given amount.886* @param {number} numBits The number of bits by which to shift.887* @return {!goog.math.Long} This shifted to the left by the given amount.888*/889goog.math.Long.prototype.shiftLeft = function(numBits) {890numBits &= 63;891if (numBits == 0) {892return this;893} else {894var low = this.low_;895if (numBits < 32) {896var high = this.high_;897return goog.math.Long.fromBits(898low << numBits, (high << numBits) | (low >>> (32 - numBits)));899} else {900return goog.math.Long.fromBits(0, low << (numBits - 32));901}902}903};904905906/**907* Returns this Long with bits shifted to the right by the given amount.908* The new leading bits match the current sign bit.909* @param {number} numBits The number of bits by which to shift.910* @return {!goog.math.Long} This shifted to the right by the given amount.911*/912goog.math.Long.prototype.shiftRight = function(numBits) {913numBits &= 63;914if (numBits == 0) {915return this;916} else {917var high = this.high_;918if (numBits < 32) {919var low = this.low_;920return goog.math.Long.fromBits(921(low >>> numBits) | (high << (32 - numBits)), high >> numBits);922} else {923return goog.math.Long.fromBits(924high >> (numBits - 32), high >= 0 ? 0 : -1);925}926}927};928929930/**931* Returns this Long with bits shifted to the right by the given amount, with932* zeros placed into the new leading bits.933* @param {number} numBits The number of bits by which to shift.934* @return {!goog.math.Long} This shifted to the right by the given amount, with935* zeros placed into the new leading bits.936*/937goog.math.Long.prototype.shiftRightUnsigned = function(numBits) {938numBits &= 63;939if (numBits == 0) {940return this;941} else {942var high = this.high_;943if (numBits < 32) {944var low = this.low_;945return goog.math.Long.fromBits(946(low >>> numBits) | (high << (32 - numBits)), high >>> numBits);947} else if (numBits == 32) {948return goog.math.Long.fromBits(high, 0);949} else {950return goog.math.Long.fromBits(high >>> (numBits - 32), 0);951}952}953};954955956/**957* @enum {number} Ids of commonly requested Long instances.958* @private959*/960goog.math.Long.ValueCacheId_ = {961MAX_VALUE: 1,962MIN_VALUE: 2,963TWO_PWR_24: 6964};965966967