| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of fixnum; | 5 part of fixnum; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An immutable 64-bit signed integer, in the range [-2^63, 2^63 - 1]. | 8 * An immutable 64-bit signed integer, in the range [-2^63, 2^63 - 1]. |
| 9 * Arithmetic operations may overflow in order to maintain this range. | 9 * Arithmetic operations may overflow in order to maintain this range. |
| 10 */ | 10 */ |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 /** | 111 /** |
| 112 * Parses a hexadecimal [String] and returns an [Int64]. | 112 * Parses a hexadecimal [String] and returns an [Int64]. |
| 113 */ | 113 */ |
| 114 static Int64 parseHex(String s) => _parseRadix(s, 16); | 114 static Int64 parseHex(String s) => _parseRadix(s, 16); |
| 115 | 115 |
| 116 // | 116 // |
| 117 // Public constructors | 117 // Public constructors |
| 118 // | 118 // |
| 119 | 119 |
| 120 /** | 120 /** |
| 121 * Constructs an [Int64] equal to 0. | 121 * Constructs an [Int64] with a given [int] value; zero by default. |
| 122 */ | 122 */ |
| 123 Int64() : _l = 0, _m = 0, _h = 0; | 123 factory Int64([int value=0]) { |
| 124 | |
| 125 /** | |
| 126 * Constructs an [Int64] with a given [int] value. | |
| 127 */ | |
| 128 factory Int64.fromInt(int value) { | |
| 129 int v0 = 0, v1 = 0, v2 = 0; | 124 int v0 = 0, v1 = 0, v2 = 0; |
| 130 bool negative = false; | 125 bool negative = false; |
| 131 if (value < 0) { | 126 if (value < 0) { |
| 132 negative = true; | 127 negative = true; |
| 133 value = -value - 1; | 128 value = -value - 1; |
| 134 } | 129 } |
| 135 if (_haveBigInts) { | 130 if (_haveBigInts) { |
| 136 v0 = _MASK & value; | 131 v0 = _MASK & value; |
| 137 v1 = _MASK & (value >> _BITS); | 132 v1 = _MASK & (value >> _BITS); |
| 138 v2 = _MASK2 & (value >> _BITS01); | 133 v2 = _MASK2 & (value >> _BITS01); |
| 139 } else { | 134 } else { |
| 140 // Avoid using bitwise operations that coerce their input to 32 bits. | 135 // Avoid using bitwise operations that coerce their input to 32 bits. |
| 141 v2 = value ~/ 17592186044416; // 2^44 | 136 v2 = value ~/ 17592186044416; // 2^44 |
| 142 value -= v2 * 17592186044416; | 137 value -= v2 * 17592186044416; |
| 143 v1 = value ~/ 4194304; // 2^22 | 138 v1 = value ~/ 4194304; // 2^22 |
| 144 value -= v1 * 4194304; | 139 value -= v1 * 4194304; |
| 145 v0 = value; | 140 v0 = value; |
| 146 } | 141 } |
| 147 | 142 |
| 148 if (negative) { | 143 if (negative) { |
| 149 v0 = _MASK & ~v0; | 144 v0 = _MASK & ~v0; |
| 150 v1 = _MASK & ~v1; | 145 v1 = _MASK & ~v1; |
| 151 v2 = _MASK2 & ~v2; | 146 v2 = _MASK2 & ~v2; |
| 152 } | 147 } |
| 153 return new Int64._bits(v0, v1, v2); | 148 return new Int64._bits(v0, v1, v2); |
| 154 } | 149 } |
| 155 | 150 |
| 151 /** |
| 152 * Constructs an [Int64] with a given [int] value. |
| 153 */ |
| 154 @deprecated |
| 155 factory Int64.fromInt(int value) => new Int64(value); |
| 156 |
| 156 factory Int64.fromBytes(List<int> bytes) { | 157 factory Int64.fromBytes(List<int> bytes) { |
| 157 int top = bytes[7] & 0xff; | 158 int top = bytes[7] & 0xff; |
| 158 top <<= 8; | 159 top <<= 8; |
| 159 top |= bytes[6] & 0xff; | 160 top |= bytes[6] & 0xff; |
| 160 top <<= 8; | 161 top <<= 8; |
| 161 top |= bytes[5] & 0xff; | 162 top |= bytes[5] & 0xff; |
| 162 top <<= 8; | 163 top <<= 8; |
| 163 top |= bytes[4] & 0xff; | 164 top |= bytes[4] & 0xff; |
| 164 | 165 |
| 165 int bottom = bytes[3] & 0xff; | 166 int bottom = bytes[3] & 0xff; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 int d2 = (top >> 12) & _MASK2; | 206 int d2 = (top >> 12) & _MASK2; |
| 206 return new Int64._bits(d0, d1, d2); | 207 return new Int64._bits(d0, d1, d2); |
| 207 } | 208 } |
| 208 | 209 |
| 209 // Returns the [Int64] representation of the specified value. Throws | 210 // Returns the [Int64] representation of the specified value. Throws |
| 210 // [ArgumentError] for non-integer arguments. | 211 // [ArgumentError] for non-integer arguments. |
| 211 static Int64 _promote(val) { | 212 static Int64 _promote(val) { |
| 212 if (val is Int64) { | 213 if (val is Int64) { |
| 213 return val; | 214 return val; |
| 214 } else if (val is int) { | 215 } else if (val is int) { |
| 215 return new Int64.fromInt(val); | 216 return new Int64(val); |
| 216 } else if (val is Int32) { | 217 } else if (val is Int32) { |
| 217 return val.toInt64(); | 218 return val.toInt64(); |
| 218 } | 219 } |
| 219 throw new ArgumentError(val); | 220 throw new ArgumentError(val); |
| 220 } | 221 } |
| 221 | 222 |
| 222 Int64 operator +(other) { | 223 Int64 operator +(other) { |
| 223 Int64 o = _promote(other); | 224 Int64 o = _promote(other); |
| 224 int sum0 = _l + o._l; | 225 int sum0 = _l + o._l; |
| 225 int sum1 = _m + o._m + (sum0 >> _BITS); | 226 int sum1 = _m + o._m + (sum0 >> _BITS); |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 */ | 451 */ |
| 451 bool operator ==(other) { | 452 bool operator ==(other) { |
| 452 Int64 o; | 453 Int64 o; |
| 453 if (other is Int64) { | 454 if (other is Int64) { |
| 454 o = other; | 455 o = other; |
| 455 } else if (other is int) { | 456 } else if (other is int) { |
| 456 if (_h == 0 && _m == 0) return _l == other; | 457 if (_h == 0 && _m == 0) return _l == other; |
| 457 // Since we know one of [_h] or [_m] is non-zero, if [other] fits in the | 458 // Since we know one of [_h] or [_m] is non-zero, if [other] fits in the |
| 458 // low word then it can't be numerically equal. | 459 // low word then it can't be numerically equal. |
| 459 if ((_MASK & other) == other) return false; | 460 if ((_MASK & other) == other) return false; |
| 460 o = new Int64.fromInt(other); | 461 o = new Int64(other); |
| 461 } else if (other is Int32) { | 462 } else if (other is Int32) { |
| 462 o = other.toInt64(); | 463 o = other.toInt64(); |
| 463 } | 464 } |
| 464 if (o != null) { | 465 if (o != null) { |
| 465 return _l == o._l && _m == o._m && _h == o._h; | 466 return _l == o._l && _m == o._m && _h == o._h; |
| 466 } | 467 } |
| 467 return false; | 468 return false; |
| 468 } | 469 } |
| 469 | 470 |
| 470 int compareTo(Comparable other) { | 471 int compareTo(Comparable other) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 bool get isMinValue => _h == _SIGN_BIT_MASK && _m == 0 && _l == 0; | 514 bool get isMinValue => _h == _SIGN_BIT_MASK && _m == 0 && _l == 0; |
| 514 bool get isNegative => (_h & _SIGN_BIT_MASK) != 0; | 515 bool get isNegative => (_h & _SIGN_BIT_MASK) != 0; |
| 515 bool get isOdd => (_l & 0x1) == 1; | 516 bool get isOdd => (_l & 0x1) == 1; |
| 516 bool get isZero => _h == 0 && _m == 0 && _l == 0; | 517 bool get isZero => _h == 0 && _m == 0 && _l == 0; |
| 517 | 518 |
| 518 /** | 519 /** |
| 519 * Returns a hash code based on all the bits of this [Int64]. | 520 * Returns a hash code based on all the bits of this [Int64]. |
| 520 */ | 521 */ |
| 521 int get hashCode { | 522 int get hashCode { |
| 522 // TODO(sra): Should we ensure that hashCode values match corresponding int? | 523 // TODO(sra): Should we ensure that hashCode values match corresponding int? |
| 523 // i.e. should `new Int64.fromInt(x).hashCode == x.hashCode`? | 524 // i.e. should `new Int64(x).hashCode == x.hashCode`? |
| 524 int bottom = ((_m & 0x3ff) << _BITS) | _l; | 525 int bottom = ((_m & 0x3ff) << _BITS) | _l; |
| 525 int top = (_h << 12) | ((_m >> 10) & 0xfff); | 526 int top = (_h << 12) | ((_m >> 10) & 0xfff); |
| 526 return bottom ^ top; | 527 return bottom ^ top; |
| 527 } | 528 } |
| 528 | 529 |
| 529 Int64 abs() { | 530 Int64 abs() { |
| 530 return this.isNegative ? -this : this; | 531 return this.isNegative ? -this : this; |
| 531 } | 532 } |
| 532 | 533 |
| 533 /** | 534 /** |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 } else { | 603 } else { |
| 603 result = (h * 17592186044416) + (m * 4194304) + l; | 604 result = (h * 17592186044416) + (m * 4194304) + l; |
| 604 } | 605 } |
| 605 return negative ? -result - 1 : result; | 606 return negative ? -result - 1 : result; |
| 606 } | 607 } |
| 607 | 608 |
| 608 /** | 609 /** |
| 609 * Returns an [Int32] containing the low 32 bits of this [Int64]. | 610 * Returns an [Int32] containing the low 32 bits of this [Int64]. |
| 610 */ | 611 */ |
| 611 Int32 toInt32() { | 612 Int32 toInt32() { |
| 612 return new Int32.fromInt(((_m & 0x3ff) << _BITS) | _l); | 613 return new Int32(((_m & 0x3ff) << _BITS) | _l); |
| 613 } | 614 } |
| 614 | 615 |
| 615 /** | 616 /** |
| 616 * Returns [this]. | 617 * Returns [this]. |
| 617 */ | 618 */ |
| 618 Int64 toInt64() => this; | 619 Int64 toInt64() => this; |
| 619 | 620 |
| 620 /** | 621 /** |
| 621 * Returns the value of this [Int64] as a decimal [String]. | 622 * Returns the value of this [Int64] as a decimal [String]. |
| 622 */ | 623 */ |
| 623 String toString() => _toRadixString(10); | 624 String toString() => _toRadixString(10); |
| 624 | 625 |
| 625 // TODO(rice) - Make this faster by avoiding arithmetic. | 626 // TODO(rice) - Make this faster by avoiding arithmetic. |
| 626 String toHexString() { | 627 String toHexString() { |
| 627 if (isZero) return "0"; | 628 if (isZero) return "0"; |
| 628 Int64 x = this; | 629 Int64 x = this; |
| 629 String hexStr = ""; | 630 String hexStr = ""; |
| 630 Int64 digit_f = new Int64.fromInt(0xf); | 631 Int64 digit_f = new Int64(0xf); |
| 631 while (!x.isZero) { | 632 while (!x.isZero) { |
| 632 int digit = x._l & 0xf; | 633 int digit = x._l & 0xf; |
| 633 hexStr = "${_hexDigit(digit)}$hexStr"; | 634 hexStr = "${_hexDigit(digit)}$hexStr"; |
| 634 x = x.shiftRightUnsigned(4); | 635 x = x.shiftRightUnsigned(4); |
| 635 } | 636 } |
| 636 return hexStr; | 637 return hexStr; |
| 637 } | 638 } |
| 638 | 639 |
| 639 String toRadixString(int radix) { | 640 String toRadixString(int radix) { |
| 640 if ((radix <= 1) || (radix > 36)) { | 641 if ((radix <= 1) || (radix > 36)) { |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 983 if (r0 == 0 && r1 == 0 && r2 == 0) { | 984 if (r0 == 0 && r1 == 0 && r2 == 0) { |
| 984 return ZERO; | 985 return ZERO; |
| 985 } else { | 986 } else { |
| 986 return _sub(b0, b1, b2, r0, r1, r2); | 987 return _sub(b0, b1, b2, r0, r1, r2); |
| 987 } | 988 } |
| 988 } else { | 989 } else { |
| 989 return _negate(r0, r1, r2); | 990 return _negate(r0, r1, r2); |
| 990 } | 991 } |
| 991 } | 992 } |
| 992 } | 993 } |
| OLD | NEW |