Projekt

Obecné

Profil

Stáhnout (40.4 KB) Statistiky
| Větev: | Revize:
1
module.exports = Long;
2

    
3
/**
4
 * wasm optimizations, to do native i64 multiplication and divide
5
 */
6
var wasm = null;
7

    
8
try {
9
  wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([
10
    0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11
11
  ])), {}).exports;
12
} catch (e) {
13
  // no wasm support :(
14
}
15

    
16
/**
17
 * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
18
 *  See the from* functions below for more convenient ways of constructing Longs.
19
 * @exports Long
20
 * @class A Long class for representing a 64 bit two's-complement integer value.
21
 * @param {number} low The low (signed) 32 bits of the long
22
 * @param {number} high The high (signed) 32 bits of the long
23
 * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
24
 * @constructor
25
 */
26
function Long(low, high, unsigned) {
27

    
28
    /**
29
     * The low 32 bits as a signed value.
30
     * @type {number}
31
     */
32
    this.low = low | 0;
33

    
34
    /**
35
     * The high 32 bits as a signed value.
36
     * @type {number}
37
     */
38
    this.high = high | 0;
39

    
40
    /**
41
     * Whether unsigned or not.
42
     * @type {boolean}
43
     */
44
    this.unsigned = !!unsigned;
45
}
46

    
47
// The internal representation of a long is the two given signed, 32-bit values.
48
// We use 32-bit pieces because these are the size of integers on which
49
// Javascript performs bit-operations.  For operations like addition and
50
// multiplication, we split each number into 16 bit pieces, which can easily be
51
// multiplied within Javascript's floating-point representation without overflow
52
// or change in sign.
53
//
54
// In the algorithms below, we frequently reduce the negative case to the
55
// positive case by negating the input(s) and then post-processing the result.
56
// Note that we must ALWAYS check specially whether those values are MIN_VALUE
57
// (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
58
// a positive number, it overflows back into a negative).  Not handling this
59
// case would often result in infinite recursion.
60
//
61
// Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from*
62
// methods on which they depend.
63

    
64
/**
65
 * An indicator used to reliably determine if an object is a Long or not.
66
 * @type {boolean}
67
 * @const
68
 * @private
69
 */
70
Long.prototype.__isLong__;
71

    
72
Object.defineProperty(Long.prototype, "__isLong__", { value: true });
73

    
74
/**
75
 * @function
76
 * @param {*} obj Object
77
 * @returns {boolean}
78
 * @inner
79
 */
80
function isLong(obj) {
81
    return (obj && obj["__isLong__"]) === true;
82
}
83

    
84
/**
85
 * Tests if the specified object is a Long.
86
 * @function
87
 * @param {*} obj Object
88
 * @returns {boolean}
89
 */
90
Long.isLong = isLong;
91

    
92
/**
93
 * A cache of the Long representations of small integer values.
94
 * @type {!Object}
95
 * @inner
96
 */
97
var INT_CACHE = {};
98

    
99
/**
100
 * A cache of the Long representations of small unsigned integer values.
101
 * @type {!Object}
102
 * @inner
103
 */
104
var UINT_CACHE = {};
105

    
106
/**
107
 * @param {number} value
108
 * @param {boolean=} unsigned
109
 * @returns {!Long}
110
 * @inner
111
 */
112
function fromInt(value, unsigned) {
113
    var obj, cachedObj, cache;
114
    if (unsigned) {
115
        value >>>= 0;
116
        if (cache = (0 <= value && value < 256)) {
117
            cachedObj = UINT_CACHE[value];
118
            if (cachedObj)
119
                return cachedObj;
120
        }
121
        obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true);
122
        if (cache)
123
            UINT_CACHE[value] = obj;
124
        return obj;
125
    } else {
126
        value |= 0;
127
        if (cache = (-128 <= value && value < 128)) {
128
            cachedObj = INT_CACHE[value];
129
            if (cachedObj)
130
                return cachedObj;
131
        }
132
        obj = fromBits(value, value < 0 ? -1 : 0, false);
133
        if (cache)
134
            INT_CACHE[value] = obj;
135
        return obj;
136
    }
137
}
138

    
139
/**
140
 * Returns a Long representing the given 32 bit integer value.
141
 * @function
142
 * @param {number} value The 32 bit integer in question
143
 * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
144
 * @returns {!Long} The corresponding Long value
145
 */
146
Long.fromInt = fromInt;
147

    
148
/**
149
 * @param {number} value
150
 * @param {boolean=} unsigned
151
 * @returns {!Long}
152
 * @inner
153
 */
154
function fromNumber(value, unsigned) {
155
    if (isNaN(value))
156
        return unsigned ? UZERO : ZERO;
157
    if (unsigned) {
158
        if (value < 0)
159
            return UZERO;
160
        if (value >= TWO_PWR_64_DBL)
161
            return MAX_UNSIGNED_VALUE;
162
    } else {
163
        if (value <= -TWO_PWR_63_DBL)
164
            return MIN_VALUE;
165
        if (value + 1 >= TWO_PWR_63_DBL)
166
            return MAX_VALUE;
167
    }
168
    if (value < 0)
169
        return fromNumber(-value, unsigned).neg();
170
    return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
171
}
172

    
173
/**
174
 * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
175
 * @function
176
 * @param {number} value The number in question
177
 * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
178
 * @returns {!Long} The corresponding Long value
179
 */
180
Long.fromNumber = fromNumber;
181

    
182
/**
183
 * @param {number} lowBits
184
 * @param {number} highBits
185
 * @param {boolean=} unsigned
186
 * @returns {!Long}
187
 * @inner
188
 */
189
function fromBits(lowBits, highBits, unsigned) {
190
    return new Long(lowBits, highBits, unsigned);
191
}
192

    
193
/**
194
 * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is
195
 *  assumed to use 32 bits.
196
 * @function
197
 * @param {number} lowBits The low 32 bits
198
 * @param {number} highBits The high 32 bits
199
 * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
200
 * @returns {!Long} The corresponding Long value
201
 */
202
Long.fromBits = fromBits;
203

    
204
/**
205
 * @function
206
 * @param {number} base
207
 * @param {number} exponent
208
 * @returns {number}
209
 * @inner
210
 */
211
var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4)
212

    
213
/**
214
 * @param {string} str
215
 * @param {(boolean|number)=} unsigned
216
 * @param {number=} radix
217
 * @returns {!Long}
218
 * @inner
219
 */
220
function fromString(str, unsigned, radix) {
221
    if (str.length === 0)
222
        throw Error('empty string');
223
    if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity")
224
        return ZERO;
225
    if (typeof unsigned === 'number') {
226
        // For goog.math.long compatibility
227
        radix = unsigned,
228
        unsigned = false;
229
    } else {
230
        unsigned = !! unsigned;
231
    }
232
    radix = radix || 10;
233
    if (radix < 2 || 36 < radix)
234
        throw RangeError('radix');
235

    
236
    var p;
237
    if ((p = str.indexOf('-')) > 0)
238
        throw Error('interior hyphen');
239
    else if (p === 0) {
240
        return fromString(str.substring(1), unsigned, radix).neg();
241
    }
242

    
243
    // Do several (8) digits each time through the loop, so as to
244
    // minimize the calls to the very expensive emulated div.
245
    var radixToPower = fromNumber(pow_dbl(radix, 8));
246

    
247
    var result = ZERO;
248
    for (var i = 0; i < str.length; i += 8) {
249
        var size = Math.min(8, str.length - i),
250
            value = parseInt(str.substring(i, i + size), radix);
251
        if (size < 8) {
252
            var power = fromNumber(pow_dbl(radix, size));
253
            result = result.mul(power).add(fromNumber(value));
254
        } else {
255
            result = result.mul(radixToPower);
256
            result = result.add(fromNumber(value));
257
        }
258
    }
259
    result.unsigned = unsigned;
260
    return result;
261
}
262

    
263
/**
264
 * Returns a Long representation of the given string, written using the specified radix.
265
 * @function
266
 * @param {string} str The textual representation of the Long
267
 * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to signed
268
 * @param {number=} radix The radix in which the text is written (2-36), defaults to 10
269
 * @returns {!Long} The corresponding Long value
270
 */
271
Long.fromString = fromString;
272

    
273
/**
274
 * @function
275
 * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val
276
 * @param {boolean=} unsigned
277
 * @returns {!Long}
278
 * @inner
279
 */
280
function fromValue(val, unsigned) {
281
    if (typeof val === 'number')
282
        return fromNumber(val, unsigned);
283
    if (typeof val === 'string')
284
        return fromString(val, unsigned);
285
    // Throws for non-objects, converts non-instanceof Long:
286
    return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned);
287
}
288

    
289
/**
290
 * Converts the specified value to a Long using the appropriate from* function for its type.
291
 * @function
292
 * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value
293
 * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
294
 * @returns {!Long}
295
 */
296
Long.fromValue = fromValue;
297

    
298
// NOTE: the compiler should inline these constant values below and then remove these variables, so there should be
299
// no runtime penalty for these.
300

    
301
/**
302
 * @type {number}
303
 * @const
304
 * @inner
305
 */
306
var TWO_PWR_16_DBL = 1 << 16;
307

    
308
/**
309
 * @type {number}
310
 * @const
311
 * @inner
312
 */
313
var TWO_PWR_24_DBL = 1 << 24;
314

    
315
/**
316
 * @type {number}
317
 * @const
318
 * @inner
319
 */
320
var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
321

    
322
/**
323
 * @type {number}
324
 * @const
325
 * @inner
326
 */
327
var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
328

    
329
/**
330
 * @type {number}
331
 * @const
332
 * @inner
333
 */
334
var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
335

    
336
/**
337
 * @type {!Long}
338
 * @const
339
 * @inner
340
 */
341
var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL);
342

    
343
/**
344
 * @type {!Long}
345
 * @inner
346
 */
347
var ZERO = fromInt(0);
348

    
349
/**
350
 * Signed zero.
351
 * @type {!Long}
352
 */
353
Long.ZERO = ZERO;
354

    
355
/**
356
 * @type {!Long}
357
 * @inner
358
 */
359
var UZERO = fromInt(0, true);
360

    
361
/**
362
 * Unsigned zero.
363
 * @type {!Long}
364
 */
365
Long.UZERO = UZERO;
366

    
367
/**
368
 * @type {!Long}
369
 * @inner
370
 */
371
var ONE = fromInt(1);
372

    
373
/**
374
 * Signed one.
375
 * @type {!Long}
376
 */
377
Long.ONE = ONE;
378

    
379
/**
380
 * @type {!Long}
381
 * @inner
382
 */
383
var UONE = fromInt(1, true);
384

    
385
/**
386
 * Unsigned one.
387
 * @type {!Long}
388
 */
389
Long.UONE = UONE;
390

    
391
/**
392
 * @type {!Long}
393
 * @inner
394
 */
395
var NEG_ONE = fromInt(-1);
396

    
397
/**
398
 * Signed negative one.
399
 * @type {!Long}
400
 */
401
Long.NEG_ONE = NEG_ONE;
402

    
403
/**
404
 * @type {!Long}
405
 * @inner
406
 */
407
var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false);
408

    
409
/**
410
 * Maximum signed value.
411
 * @type {!Long}
412
 */
413
Long.MAX_VALUE = MAX_VALUE;
414

    
415
/**
416
 * @type {!Long}
417
 * @inner
418
 */
419
var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true);
420

    
421
/**
422
 * Maximum unsigned value.
423
 * @type {!Long}
424
 */
425
Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE;
426

    
427
/**
428
 * @type {!Long}
429
 * @inner
430
 */
431
var MIN_VALUE = fromBits(0, 0x80000000|0, false);
432

    
433
/**
434
 * Minimum signed value.
435
 * @type {!Long}
436
 */
437
Long.MIN_VALUE = MIN_VALUE;
438

    
439
/**
440
 * @alias Long.prototype
441
 * @inner
442
 */
443
var LongPrototype = Long.prototype;
444

    
445
/**
446
 * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
447
 * @this {!Long}
448
 * @returns {number}
449
 */
450
LongPrototype.toInt = function toInt() {
451
    return this.unsigned ? this.low >>> 0 : this.low;
452
};
453

    
454
/**
455
 * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
456
 * @this {!Long}
457
 * @returns {number}
458
 */
459
LongPrototype.toNumber = function toNumber() {
460
    if (this.unsigned)
461
        return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0);
462
    return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
463
};
464

    
465
/**
466
 * Converts the Long to a string written in the specified radix.
467
 * @this {!Long}
468
 * @param {number=} radix Radix (2-36), defaults to 10
469
 * @returns {string}
470
 * @override
471
 * @throws {RangeError} If `radix` is out of range
472
 */
473
LongPrototype.toString = function toString(radix) {
474
    radix = radix || 10;
475
    if (radix < 2 || 36 < radix)
476
        throw RangeError('radix');
477
    if (this.isZero())
478
        return '0';
479
    if (this.isNegative()) { // Unsigned Longs are never negative
480
        if (this.eq(MIN_VALUE)) {
481
            // We need to change the Long value before it can be negated, so we remove
482
            // the bottom-most digit in this base and then recurse to do the rest.
483
            var radixLong = fromNumber(radix),
484
                div = this.div(radixLong),
485
                rem1 = div.mul(radixLong).sub(this);
486
            return div.toString(radix) + rem1.toInt().toString(radix);
487
        } else
488
            return '-' + this.neg().toString(radix);
489
    }
490

    
491
    // Do several (6) digits each time through the loop, so as to
492
    // minimize the calls to the very expensive emulated div.
493
    var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned),
494
        rem = this;
495
    var result = '';
496
    while (true) {
497
        var remDiv = rem.div(radixToPower),
498
            intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0,
499
            digits = intval.toString(radix);
500
        rem = remDiv;
501
        if (rem.isZero())
502
            return digits + result;
503
        else {
504
            while (digits.length < 6)
505
                digits = '0' + digits;
506
            result = '' + digits + result;
507
        }
508
    }
509
};
510

    
511
/**
512
 * Gets the high 32 bits as a signed integer.
513
 * @this {!Long}
514
 * @returns {number} Signed high bits
515
 */
516
LongPrototype.getHighBits = function getHighBits() {
517
    return this.high;
518
};
519

    
520
/**
521
 * Gets the high 32 bits as an unsigned integer.
522
 * @this {!Long}
523
 * @returns {number} Unsigned high bits
524
 */
525
LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() {
526
    return this.high >>> 0;
527
};
528

    
529
/**
530
 * Gets the low 32 bits as a signed integer.
531
 * @this {!Long}
532
 * @returns {number} Signed low bits
533
 */
534
LongPrototype.getLowBits = function getLowBits() {
535
    return this.low;
536
};
537

    
538
/**
539
 * Gets the low 32 bits as an unsigned integer.
540
 * @this {!Long}
541
 * @returns {number} Unsigned low bits
542
 */
543
LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() {
544
    return this.low >>> 0;
545
};
546

    
547
/**
548
 * Gets the number of bits needed to represent the absolute value of this Long.
549
 * @this {!Long}
550
 * @returns {number}
551
 */
552
LongPrototype.getNumBitsAbs = function getNumBitsAbs() {
553
    if (this.isNegative()) // Unsigned Longs are never negative
554
        return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
555
    var val = this.high != 0 ? this.high : this.low;
556
    for (var bit = 31; bit > 0; bit--)
557
        if ((val & (1 << bit)) != 0)
558
            break;
559
    return this.high != 0 ? bit + 33 : bit + 1;
560
};
561

    
562
/**
563
 * Tests if this Long's value equals zero.
564
 * @this {!Long}
565
 * @returns {boolean}
566
 */
567
LongPrototype.isZero = function isZero() {
568
    return this.high === 0 && this.low === 0;
569
};
570

    
571
/**
572
 * Tests if this Long's value equals zero. This is an alias of {@link Long#isZero}.
573
 * @returns {boolean}
574
 */
575
LongPrototype.eqz = LongPrototype.isZero;
576

    
577
/**
578
 * Tests if this Long's value is negative.
579
 * @this {!Long}
580
 * @returns {boolean}
581
 */
582
LongPrototype.isNegative = function isNegative() {
583
    return !this.unsigned && this.high < 0;
584
};
585

    
586
/**
587
 * Tests if this Long's value is positive.
588
 * @this {!Long}
589
 * @returns {boolean}
590
 */
591
LongPrototype.isPositive = function isPositive() {
592
    return this.unsigned || this.high >= 0;
593
};
594

    
595
/**
596
 * Tests if this Long's value is odd.
597
 * @this {!Long}
598
 * @returns {boolean}
599
 */
600
LongPrototype.isOdd = function isOdd() {
601
    return (this.low & 1) === 1;
602
};
603

    
604
/**
605
 * Tests if this Long's value is even.
606
 * @this {!Long}
607
 * @returns {boolean}
608
 */
609
LongPrototype.isEven = function isEven() {
610
    return (this.low & 1) === 0;
611
};
612

    
613
/**
614
 * Tests if this Long's value equals the specified's.
615
 * @this {!Long}
616
 * @param {!Long|number|string} other Other value
617
 * @returns {boolean}
618
 */
619
LongPrototype.equals = function equals(other) {
620
    if (!isLong(other))
621
        other = fromValue(other);
622
    if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1)
623
        return false;
624
    return this.high === other.high && this.low === other.low;
625
};
626

    
627
/**
628
 * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}.
629
 * @function
630
 * @param {!Long|number|string} other Other value
631
 * @returns {boolean}
632
 */
633
LongPrototype.eq = LongPrototype.equals;
634

    
635
/**
636
 * Tests if this Long's value differs from the specified's.
637
 * @this {!Long}
638
 * @param {!Long|number|string} other Other value
639
 * @returns {boolean}
640
 */
641
LongPrototype.notEquals = function notEquals(other) {
642
    return !this.eq(/* validates */ other);
643
};
644

    
645
/**
646
 * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
647
 * @function
648
 * @param {!Long|number|string} other Other value
649
 * @returns {boolean}
650
 */
651
LongPrototype.neq = LongPrototype.notEquals;
652

    
653
/**
654
 * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
655
 * @function
656
 * @param {!Long|number|string} other Other value
657
 * @returns {boolean}
658
 */
659
LongPrototype.ne = LongPrototype.notEquals;
660

    
661
/**
662
 * Tests if this Long's value is less than the specified's.
663
 * @this {!Long}
664
 * @param {!Long|number|string} other Other value
665
 * @returns {boolean}
666
 */
667
LongPrototype.lessThan = function lessThan(other) {
668
    return this.comp(/* validates */ other) < 0;
669
};
670

    
671
/**
672
 * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}.
673
 * @function
674
 * @param {!Long|number|string} other Other value
675
 * @returns {boolean}
676
 */
677
LongPrototype.lt = LongPrototype.lessThan;
678

    
679
/**
680
 * Tests if this Long's value is less than or equal the specified's.
681
 * @this {!Long}
682
 * @param {!Long|number|string} other Other value
683
 * @returns {boolean}
684
 */
685
LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) {
686
    return this.comp(/* validates */ other) <= 0;
687
};
688

    
689
/**
690
 * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
691
 * @function
692
 * @param {!Long|number|string} other Other value
693
 * @returns {boolean}
694
 */
695
LongPrototype.lte = LongPrototype.lessThanOrEqual;
696

    
697
/**
698
 * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
699
 * @function
700
 * @param {!Long|number|string} other Other value
701
 * @returns {boolean}
702
 */
703
LongPrototype.le = LongPrototype.lessThanOrEqual;
704

    
705
/**
706
 * Tests if this Long's value is greater than the specified's.
707
 * @this {!Long}
708
 * @param {!Long|number|string} other Other value
709
 * @returns {boolean}
710
 */
711
LongPrototype.greaterThan = function greaterThan(other) {
712
    return this.comp(/* validates */ other) > 0;
713
};
714

    
715
/**
716
 * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}.
717
 * @function
718
 * @param {!Long|number|string} other Other value
719
 * @returns {boolean}
720
 */
721
LongPrototype.gt = LongPrototype.greaterThan;
722

    
723
/**
724
 * Tests if this Long's value is greater than or equal the specified's.
725
 * @this {!Long}
726
 * @param {!Long|number|string} other Other value
727
 * @returns {boolean}
728
 */
729
LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) {
730
    return this.comp(/* validates */ other) >= 0;
731
};
732

    
733
/**
734
 * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
735
 * @function
736
 * @param {!Long|number|string} other Other value
737
 * @returns {boolean}
738
 */
739
LongPrototype.gte = LongPrototype.greaterThanOrEqual;
740

    
741
/**
742
 * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
743
 * @function
744
 * @param {!Long|number|string} other Other value
745
 * @returns {boolean}
746
 */
747
LongPrototype.ge = LongPrototype.greaterThanOrEqual;
748

    
749
/**
750
 * Compares this Long's value with the specified's.
751
 * @this {!Long}
752
 * @param {!Long|number|string} other Other value
753
 * @returns {number} 0 if they are the same, 1 if the this is greater and -1
754
 *  if the given one is greater
755
 */
756
LongPrototype.compare = function compare(other) {
757
    if (!isLong(other))
758
        other = fromValue(other);
759
    if (this.eq(other))
760
        return 0;
761
    var thisNeg = this.isNegative(),
762
        otherNeg = other.isNegative();
763
    if (thisNeg && !otherNeg)
764
        return -1;
765
    if (!thisNeg && otherNeg)
766
        return 1;
767
    // At this point the sign bits are the same
768
    if (!this.unsigned)
769
        return this.sub(other).isNegative() ? -1 : 1;
770
    // Both are positive if at least one is unsigned
771
    return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1;
772
};
773

    
774
/**
775
 * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}.
776
 * @function
777
 * @param {!Long|number|string} other Other value
778
 * @returns {number} 0 if they are the same, 1 if the this is greater and -1
779
 *  if the given one is greater
780
 */
781
LongPrototype.comp = LongPrototype.compare;
782

    
783
/**
784
 * Negates this Long's value.
785
 * @this {!Long}
786
 * @returns {!Long} Negated Long
787
 */
788
LongPrototype.negate = function negate() {
789
    if (!this.unsigned && this.eq(MIN_VALUE))
790
        return MIN_VALUE;
791
    return this.not().add(ONE);
792
};
793

    
794
/**
795
 * Negates this Long's value. This is an alias of {@link Long#negate}.
796
 * @function
797
 * @returns {!Long} Negated Long
798
 */
799
LongPrototype.neg = LongPrototype.negate;
800

    
801
/**
802
 * Returns the sum of this and the specified Long.
803
 * @this {!Long}
804
 * @param {!Long|number|string} addend Addend
805
 * @returns {!Long} Sum
806
 */
807
LongPrototype.add = function add(addend) {
808
    if (!isLong(addend))
809
        addend = fromValue(addend);
810

    
811
    // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
812

    
813
    var a48 = this.high >>> 16;
814
    var a32 = this.high & 0xFFFF;
815
    var a16 = this.low >>> 16;
816
    var a00 = this.low & 0xFFFF;
817

    
818
    var b48 = addend.high >>> 16;
819
    var b32 = addend.high & 0xFFFF;
820
    var b16 = addend.low >>> 16;
821
    var b00 = addend.low & 0xFFFF;
822

    
823
    var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
824
    c00 += a00 + b00;
825
    c16 += c00 >>> 16;
826
    c00 &= 0xFFFF;
827
    c16 += a16 + b16;
828
    c32 += c16 >>> 16;
829
    c16 &= 0xFFFF;
830
    c32 += a32 + b32;
831
    c48 += c32 >>> 16;
832
    c32 &= 0xFFFF;
833
    c48 += a48 + b48;
834
    c48 &= 0xFFFF;
835
    return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
836
};
837

    
838
/**
839
 * Returns the difference of this and the specified Long.
840
 * @this {!Long}
841
 * @param {!Long|number|string} subtrahend Subtrahend
842
 * @returns {!Long} Difference
843
 */
844
LongPrototype.subtract = function subtract(subtrahend) {
845
    if (!isLong(subtrahend))
846
        subtrahend = fromValue(subtrahend);
847
    return this.add(subtrahend.neg());
848
};
849

    
850
/**
851
 * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}.
852
 * @function
853
 * @param {!Long|number|string} subtrahend Subtrahend
854
 * @returns {!Long} Difference
855
 */
856
LongPrototype.sub = LongPrototype.subtract;
857

    
858
/**
859
 * Returns the product of this and the specified Long.
860
 * @this {!Long}
861
 * @param {!Long|number|string} multiplier Multiplier
862
 * @returns {!Long} Product
863
 */
864
LongPrototype.multiply = function multiply(multiplier) {
865
    if (this.isZero())
866
        return ZERO;
867
    if (!isLong(multiplier))
868
        multiplier = fromValue(multiplier);
869

    
870
    // use wasm support if present
871
    if (wasm) {
872
        var low = wasm["mul"](this.low,
873
                              this.high,
874
                              multiplier.low,
875
                              multiplier.high);
876
        return fromBits(low, wasm["get_high"](), this.unsigned);
877
    }
878

    
879
    if (multiplier.isZero())
880
        return ZERO;
881
    if (this.eq(MIN_VALUE))
882
        return multiplier.isOdd() ? MIN_VALUE : ZERO;
883
    if (multiplier.eq(MIN_VALUE))
884
        return this.isOdd() ? MIN_VALUE : ZERO;
885

    
886
    if (this.isNegative()) {
887
        if (multiplier.isNegative())
888
            return this.neg().mul(multiplier.neg());
889
        else
890
            return this.neg().mul(multiplier).neg();
891
    } else if (multiplier.isNegative())
892
        return this.mul(multiplier.neg()).neg();
893

    
894
    // If both longs are small, use float multiplication
895
    if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24))
896
        return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
897

    
898
    // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
899
    // We can skip products that would overflow.
900

    
901
    var a48 = this.high >>> 16;
902
    var a32 = this.high & 0xFFFF;
903
    var a16 = this.low >>> 16;
904
    var a00 = this.low & 0xFFFF;
905

    
906
    var b48 = multiplier.high >>> 16;
907
    var b32 = multiplier.high & 0xFFFF;
908
    var b16 = multiplier.low >>> 16;
909
    var b00 = multiplier.low & 0xFFFF;
910

    
911
    var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
912
    c00 += a00 * b00;
913
    c16 += c00 >>> 16;
914
    c00 &= 0xFFFF;
915
    c16 += a16 * b00;
916
    c32 += c16 >>> 16;
917
    c16 &= 0xFFFF;
918
    c16 += a00 * b16;
919
    c32 += c16 >>> 16;
920
    c16 &= 0xFFFF;
921
    c32 += a32 * b00;
922
    c48 += c32 >>> 16;
923
    c32 &= 0xFFFF;
924
    c32 += a16 * b16;
925
    c48 += c32 >>> 16;
926
    c32 &= 0xFFFF;
927
    c32 += a00 * b32;
928
    c48 += c32 >>> 16;
929
    c32 &= 0xFFFF;
930
    c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
931
    c48 &= 0xFFFF;
932
    return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
933
};
934

    
935
/**
936
 * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}.
937
 * @function
938
 * @param {!Long|number|string} multiplier Multiplier
939
 * @returns {!Long} Product
940
 */
941
LongPrototype.mul = LongPrototype.multiply;
942

    
943
/**
944
 * Returns this Long divided by the specified. The result is signed if this Long is signed or
945
 *  unsigned if this Long is unsigned.
946
 * @this {!Long}
947
 * @param {!Long|number|string} divisor Divisor
948
 * @returns {!Long} Quotient
949
 */
950
LongPrototype.divide = function divide(divisor) {
951
    if (!isLong(divisor))
952
        divisor = fromValue(divisor);
953
    if (divisor.isZero())
954
        throw Error('division by zero');
955

    
956
    // use wasm support if present
957
    if (wasm) {
958
        // guard against signed division overflow: the largest
959
        // negative number / -1 would be 1 larger than the largest
960
        // positive number, due to two's complement.
961
        if (!this.unsigned &&
962
            this.high === -0x80000000 &&
963
            divisor.low === -1 && divisor.high === -1) {
964
            // be consistent with non-wasm code path
965
            return this;
966
        }
967
        var low = (this.unsigned ? wasm["div_u"] : wasm["div_s"])(
968
            this.low,
969
            this.high,
970
            divisor.low,
971
            divisor.high
972
        );
973
        return fromBits(low, wasm["get_high"](), this.unsigned);
974
    }
975

    
976
    if (this.isZero())
977
        return this.unsigned ? UZERO : ZERO;
978
    var approx, rem, res;
979
    if (!this.unsigned) {
980
        // This section is only relevant for signed longs and is derived from the
981
        // closure library as a whole.
982
        if (this.eq(MIN_VALUE)) {
983
            if (divisor.eq(ONE) || divisor.eq(NEG_ONE))
984
                return MIN_VALUE;  // recall that -MIN_VALUE == MIN_VALUE
985
            else if (divisor.eq(MIN_VALUE))
986
                return ONE;
987
            else {
988
                // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
989
                var halfThis = this.shr(1);
990
                approx = halfThis.div(divisor).shl(1);
991
                if (approx.eq(ZERO)) {
992
                    return divisor.isNegative() ? ONE : NEG_ONE;
993
                } else {
994
                    rem = this.sub(divisor.mul(approx));
995
                    res = approx.add(rem.div(divisor));
996
                    return res;
997
                }
998
            }
999
        } else if (divisor.eq(MIN_VALUE))
1000
            return this.unsigned ? UZERO : ZERO;
1001
        if (this.isNegative()) {
1002
            if (divisor.isNegative())
1003
                return this.neg().div(divisor.neg());
1004
            return this.neg().div(divisor).neg();
1005
        } else if (divisor.isNegative())
1006
            return this.div(divisor.neg()).neg();
1007
        res = ZERO;
1008
    } else {
1009
        // The algorithm below has not been made for unsigned longs. It's therefore
1010
        // required to take special care of the MSB prior to running it.
1011
        if (!divisor.unsigned)
1012
            divisor = divisor.toUnsigned();
1013
        if (divisor.gt(this))
1014
            return UZERO;
1015
        if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true
1016
            return UONE;
1017
        res = UZERO;
1018
    }
1019

    
1020
    // Repeat the following until the remainder is less than other:  find a
1021
    // floating-point that approximates remainder / other *from below*, add this
1022
    // into the result, and subtract it from the remainder.  It is critical that
1023
    // the approximate value is less than or equal to the real value so that the
1024
    // remainder never becomes negative.
1025
    rem = this;
1026
    while (rem.gte(divisor)) {
1027
        // Approximate the result of division. This may be a little greater or
1028
        // smaller than the actual value.
1029
        approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
1030

    
1031
        // We will tweak the approximate result by changing it in the 48-th digit or
1032
        // the smallest non-fractional digit, whichever is larger.
1033
        var log2 = Math.ceil(Math.log(approx) / Math.LN2),
1034
            delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48),
1035

    
1036
        // Decrease the approximation until it is smaller than the remainder.  Note
1037
        // that if it is too large, the product overflows and is negative.
1038
            approxRes = fromNumber(approx),
1039
            approxRem = approxRes.mul(divisor);
1040
        while (approxRem.isNegative() || approxRem.gt(rem)) {
1041
            approx -= delta;
1042
            approxRes = fromNumber(approx, this.unsigned);
1043
            approxRem = approxRes.mul(divisor);
1044
        }
1045

    
1046
        // We know the answer can't be zero... and actually, zero would cause
1047
        // infinite recursion since we would make no progress.
1048
        if (approxRes.isZero())
1049
            approxRes = ONE;
1050

    
1051
        res = res.add(approxRes);
1052
        rem = rem.sub(approxRem);
1053
    }
1054
    return res;
1055
};
1056

    
1057
/**
1058
 * Returns this Long divided by the specified. This is an alias of {@link Long#divide}.
1059
 * @function
1060
 * @param {!Long|number|string} divisor Divisor
1061
 * @returns {!Long} Quotient
1062
 */
1063
LongPrototype.div = LongPrototype.divide;
1064

    
1065
/**
1066
 * Returns this Long modulo the specified.
1067
 * @this {!Long}
1068
 * @param {!Long|number|string} divisor Divisor
1069
 * @returns {!Long} Remainder
1070
 */
1071
LongPrototype.modulo = function modulo(divisor) {
1072
    if (!isLong(divisor))
1073
        divisor = fromValue(divisor);
1074

    
1075
    // use wasm support if present
1076
    if (wasm) {
1077
        var low = (this.unsigned ? wasm["rem_u"] : wasm["rem_s"])(
1078
            this.low,
1079
            this.high,
1080
            divisor.low,
1081
            divisor.high
1082
        );
1083
        return fromBits(low, wasm["get_high"](), this.unsigned);
1084
    }
1085

    
1086
    return this.sub(this.div(divisor).mul(divisor));
1087
};
1088

    
1089
/**
1090
 * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
1091
 * @function
1092
 * @param {!Long|number|string} divisor Divisor
1093
 * @returns {!Long} Remainder
1094
 */
1095
LongPrototype.mod = LongPrototype.modulo;
1096

    
1097
/**
1098
 * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
1099
 * @function
1100
 * @param {!Long|number|string} divisor Divisor
1101
 * @returns {!Long} Remainder
1102
 */
1103
LongPrototype.rem = LongPrototype.modulo;
1104

    
1105
/**
1106
 * Returns the bitwise NOT of this Long.
1107
 * @this {!Long}
1108
 * @returns {!Long}
1109
 */
1110
LongPrototype.not = function not() {
1111
    return fromBits(~this.low, ~this.high, this.unsigned);
1112
};
1113

    
1114
/**
1115
 * Returns the bitwise AND of this Long and the specified.
1116
 * @this {!Long}
1117
 * @param {!Long|number|string} other Other Long
1118
 * @returns {!Long}
1119
 */
1120
LongPrototype.and = function and(other) {
1121
    if (!isLong(other))
1122
        other = fromValue(other);
1123
    return fromBits(this.low & other.low, this.high & other.high, this.unsigned);
1124
};
1125

    
1126
/**
1127
 * Returns the bitwise OR of this Long and the specified.
1128
 * @this {!Long}
1129
 * @param {!Long|number|string} other Other Long
1130
 * @returns {!Long}
1131
 */
1132
LongPrototype.or = function or(other) {
1133
    if (!isLong(other))
1134
        other = fromValue(other);
1135
    return fromBits(this.low | other.low, this.high | other.high, this.unsigned);
1136
};
1137

    
1138
/**
1139
 * Returns the bitwise XOR of this Long and the given one.
1140
 * @this {!Long}
1141
 * @param {!Long|number|string} other Other Long
1142
 * @returns {!Long}
1143
 */
1144
LongPrototype.xor = function xor(other) {
1145
    if (!isLong(other))
1146
        other = fromValue(other);
1147
    return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
1148
};
1149

    
1150
/**
1151
 * Returns this Long with bits shifted to the left by the given amount.
1152
 * @this {!Long}
1153
 * @param {number|!Long} numBits Number of bits
1154
 * @returns {!Long} Shifted Long
1155
 */
1156
LongPrototype.shiftLeft = function shiftLeft(numBits) {
1157
    if (isLong(numBits))
1158
        numBits = numBits.toInt();
1159
    if ((numBits &= 63) === 0)
1160
        return this;
1161
    else if (numBits < 32)
1162
        return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned);
1163
    else
1164
        return fromBits(0, this.low << (numBits - 32), this.unsigned);
1165
};
1166

    
1167
/**
1168
 * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}.
1169
 * @function
1170
 * @param {number|!Long} numBits Number of bits
1171
 * @returns {!Long} Shifted Long
1172
 */
1173
LongPrototype.shl = LongPrototype.shiftLeft;
1174

    
1175
/**
1176
 * Returns this Long with bits arithmetically shifted to the right by the given amount.
1177
 * @this {!Long}
1178
 * @param {number|!Long} numBits Number of bits
1179
 * @returns {!Long} Shifted Long
1180
 */
1181
LongPrototype.shiftRight = function shiftRight(numBits) {
1182
    if (isLong(numBits))
1183
        numBits = numBits.toInt();
1184
    if ((numBits &= 63) === 0)
1185
        return this;
1186
    else if (numBits < 32)
1187
        return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned);
1188
    else
1189
        return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
1190
};
1191

    
1192
/**
1193
 * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}.
1194
 * @function
1195
 * @param {number|!Long} numBits Number of bits
1196
 * @returns {!Long} Shifted Long
1197
 */
1198
LongPrototype.shr = LongPrototype.shiftRight;
1199

    
1200
/**
1201
 * Returns this Long with bits logically shifted to the right by the given amount.
1202
 * @this {!Long}
1203
 * @param {number|!Long} numBits Number of bits
1204
 * @returns {!Long} Shifted Long
1205
 */
1206
LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) {
1207
    if (isLong(numBits)) numBits = numBits.toInt();
1208
    if ((numBits &= 63) === 0) return this;
1209
    if (numBits < 32) return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >>> numBits, this.unsigned);
1210
    if (numBits === 32) return fromBits(this.high, 0, this.unsigned);
1211
    return fromBits(this.high >>> (numBits - 32), 0, this.unsigned);
1212
};
1213

    
1214
/**
1215
 * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
1216
 * @function
1217
 * @param {number|!Long} numBits Number of bits
1218
 * @returns {!Long} Shifted Long
1219
 */
1220
LongPrototype.shru = LongPrototype.shiftRightUnsigned;
1221

    
1222
/**
1223
 * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
1224
 * @function
1225
 * @param {number|!Long} numBits Number of bits
1226
 * @returns {!Long} Shifted Long
1227
 */
1228
LongPrototype.shr_u = LongPrototype.shiftRightUnsigned;
1229

    
1230
/**
1231
 * Returns this Long with bits rotated to the left by the given amount.
1232
 * @this {!Long}
1233
 * @param {number|!Long} numBits Number of bits
1234
 * @returns {!Long} Rotated Long
1235
 */
1236
LongPrototype.rotateLeft = function rotateLeft(numBits) {
1237
    var b;
1238
    if (isLong(numBits)) numBits = numBits.toInt();
1239
    if ((numBits &= 63) === 0) return this;
1240
    if (numBits === 32) return fromBits(this.high, this.low, this.unsigned);
1241
    if (numBits < 32) {
1242
        b = (32 - numBits);
1243
        return fromBits(((this.low << numBits) | (this.high >>> b)), ((this.high << numBits) | (this.low >>> b)), this.unsigned);
1244
    }
1245
    numBits -= 32;
1246
    b = (32 - numBits);
1247
    return fromBits(((this.high << numBits) | (this.low >>> b)), ((this.low << numBits) | (this.high >>> b)), this.unsigned);
1248
}
1249
/**
1250
 * Returns this Long with bits rotated to the left by the given amount. This is an alias of {@link Long#rotateLeft}.
1251
 * @function
1252
 * @param {number|!Long} numBits Number of bits
1253
 * @returns {!Long} Rotated Long
1254
 */
1255
LongPrototype.rotl = LongPrototype.rotateLeft;
1256

    
1257
/**
1258
 * Returns this Long with bits rotated to the right by the given amount.
1259
 * @this {!Long}
1260
 * @param {number|!Long} numBits Number of bits
1261
 * @returns {!Long} Rotated Long
1262
 */
1263
LongPrototype.rotateRight = function rotateRight(numBits) {
1264
    var b;
1265
    if (isLong(numBits)) numBits = numBits.toInt();
1266
    if ((numBits &= 63) === 0) return this;
1267
    if (numBits === 32) return fromBits(this.high, this.low, this.unsigned);
1268
    if (numBits < 32) {
1269
        b = (32 - numBits);
1270
        return fromBits(((this.high << b) | (this.low >>> numBits)), ((this.low << b) | (this.high >>> numBits)), this.unsigned);
1271
    }
1272
    numBits -= 32;
1273
    b = (32 - numBits);
1274
    return fromBits(((this.low << b) | (this.high >>> numBits)), ((this.high << b) | (this.low >>> numBits)), this.unsigned);
1275
}
1276
/**
1277
 * Returns this Long with bits rotated to the right by the given amount. This is an alias of {@link Long#rotateRight}.
1278
 * @function
1279
 * @param {number|!Long} numBits Number of bits
1280
 * @returns {!Long} Rotated Long
1281
 */
1282
LongPrototype.rotr = LongPrototype.rotateRight;
1283

    
1284
/**
1285
 * Converts this Long to signed.
1286
 * @this {!Long}
1287
 * @returns {!Long} Signed long
1288
 */
1289
LongPrototype.toSigned = function toSigned() {
1290
    if (!this.unsigned)
1291
        return this;
1292
    return fromBits(this.low, this.high, false);
1293
};
1294

    
1295
/**
1296
 * Converts this Long to unsigned.
1297
 * @this {!Long}
1298
 * @returns {!Long} Unsigned long
1299
 */
1300
LongPrototype.toUnsigned = function toUnsigned() {
1301
    if (this.unsigned)
1302
        return this;
1303
    return fromBits(this.low, this.high, true);
1304
};
1305

    
1306
/**
1307
 * Converts this Long to its byte representation.
1308
 * @param {boolean=} le Whether little or big endian, defaults to big endian
1309
 * @this {!Long}
1310
 * @returns {!Array.<number>} Byte representation
1311
 */
1312
LongPrototype.toBytes = function toBytes(le) {
1313
    return le ? this.toBytesLE() : this.toBytesBE();
1314
};
1315

    
1316
/**
1317
 * Converts this Long to its little endian byte representation.
1318
 * @this {!Long}
1319
 * @returns {!Array.<number>} Little endian byte representation
1320
 */
1321
LongPrototype.toBytesLE = function toBytesLE() {
1322
    var hi = this.high,
1323
        lo = this.low;
1324
    return [
1325
        lo        & 0xff,
1326
        lo >>>  8 & 0xff,
1327
        lo >>> 16 & 0xff,
1328
        lo >>> 24       ,
1329
        hi        & 0xff,
1330
        hi >>>  8 & 0xff,
1331
        hi >>> 16 & 0xff,
1332
        hi >>> 24
1333
    ];
1334
};
1335

    
1336
/**
1337
 * Converts this Long to its big endian byte representation.
1338
 * @this {!Long}
1339
 * @returns {!Array.<number>} Big endian byte representation
1340
 */
1341
LongPrototype.toBytesBE = function toBytesBE() {
1342
    var hi = this.high,
1343
        lo = this.low;
1344
    return [
1345
        hi >>> 24       ,
1346
        hi >>> 16 & 0xff,
1347
        hi >>>  8 & 0xff,
1348
        hi        & 0xff,
1349
        lo >>> 24       ,
1350
        lo >>> 16 & 0xff,
1351
        lo >>>  8 & 0xff,
1352
        lo        & 0xff
1353
    ];
1354
};
1355

    
1356
/**
1357
 * Creates a Long from its byte representation.
1358
 * @param {!Array.<number>} bytes Byte representation
1359
 * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
1360
 * @param {boolean=} le Whether little or big endian, defaults to big endian
1361
 * @returns {Long} The corresponding Long value
1362
 */
1363
Long.fromBytes = function fromBytes(bytes, unsigned, le) {
1364
    return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned);
1365
};
1366

    
1367
/**
1368
 * Creates a Long from its little endian byte representation.
1369
 * @param {!Array.<number>} bytes Little endian byte representation
1370
 * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
1371
 * @returns {Long} The corresponding Long value
1372
 */
1373
Long.fromBytesLE = function fromBytesLE(bytes, unsigned) {
1374
    return new Long(
1375
        bytes[0]       |
1376
        bytes[1] <<  8 |
1377
        bytes[2] << 16 |
1378
        bytes[3] << 24,
1379
        bytes[4]       |
1380
        bytes[5] <<  8 |
1381
        bytes[6] << 16 |
1382
        bytes[7] << 24,
1383
        unsigned
1384
    );
1385
};
1386

    
1387
/**
1388
 * Creates a Long from its big endian byte representation.
1389
 * @param {!Array.<number>} bytes Big endian byte representation
1390
 * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
1391
 * @returns {Long} The corresponding Long value
1392
 */
1393
Long.fromBytesBE = function fromBytesBE(bytes, unsigned) {
1394
    return new Long(
1395
        bytes[4] << 24 |
1396
        bytes[5] << 16 |
1397
        bytes[6] <<  8 |
1398
        bytes[7],
1399
        bytes[0] << 24 |
1400
        bytes[1] << 16 |
1401
        bytes[2] <<  8 |
1402
        bytes[3],
1403
        unsigned
1404
    );
1405
};
    (1-1/1)