Index: runtime/lib/integers.dart |
diff --git a/runtime/lib/integers.dart b/runtime/lib/integers.dart |
index e87dba700b759503669df13e4622ceee8f6541d1..ff755e9c8dd2f9e65f9ef477f8a71029a62fff34 100644 |
--- a/runtime/lib/integers.dart |
+++ b/runtime/lib/integers.dart |
@@ -11,16 +11,19 @@ abstract class _IntegerImplementation { |
if (result != null) return result; |
return other._toBigint()._addFromInteger(this); |
} |
+ |
num operator -(num other) { |
var result = other._subFromInteger(this); |
if (result != null) return result; |
return other._toBigint()._subFromInteger(this); |
} |
+ |
num operator *(num other) { |
var result = other._mulFromInteger(this); |
if (result != null) return result; |
return other._toBigint()._mulFromInteger(this); |
} |
+ |
num operator ~/(num other) { |
if ((other is int) && (other == 0)) { |
throw const IntegerDivisionByZeroException(); |
@@ -29,9 +32,11 @@ abstract class _IntegerImplementation { |
if (result != null) return result; |
return other._toBigint()._truncDivFromInteger(this); |
} |
+ |
num operator /(num other) { |
return this.toDouble() / other.toDouble(); |
} |
+ |
num operator %(num other) { |
if ((other is int) && (other == 0)) { |
throw const IntegerDivisionByZeroException(); |
@@ -40,27 +45,33 @@ abstract class _IntegerImplementation { |
if (result != null) return result; |
return other._toBigint()._moduloFromInteger(this); |
} |
+ |
int operator -() { |
return 0 - this; |
} |
+ |
int operator &(int other) { |
var result = other._bitAndFromInteger(this); |
if (result != null) return result; |
return other._toBigint()._bitAndFromInteger(this); |
} |
+ |
int operator |(int other) { |
var result = other._bitOrFromInteger(this); |
if (result != null) return result; |
return other._toBigint()._bitOrFromInteger(this); |
} |
+ |
int operator ^(int other) { |
var result = other._bitXorFromInteger(this); |
if (result != null) return result; |
return other._toBigint()._bitXorFromInteger(this); |
} |
+ |
num remainder(num other) { |
return other._remainderFromInteger(this); |
} |
+ |
int _bitAndFromSmi(int other) native "Integer_bitAndFromInteger"; |
int _bitAndFromInteger(int other) native "Integer_bitAndFromInteger"; |
int _bitOrFromInteger(int other) native "Integer_bitOrFromInteger"; |
@@ -73,28 +84,35 @@ abstract class _IntegerImplementation { |
int _remainderFromInteger(int other) { |
return other - (other ~/ this) * this; |
} |
+ |
int operator >>(int other) { |
var result = other._shrFromInt(this); |
if (result != null) return result; |
return other._toBigint()._shrFromInt(this); |
} |
+ |
int operator <<(int other) { |
var result = other._shlFromInt(this); |
if (result != null) return result; |
return other._toBigint()._shlFromInt(this); |
} |
+ |
bool operator <(num other) { |
return other > this; |
} |
+ |
bool operator >(num other) { |
return other._greaterThanFromInteger(this); |
} |
+ |
bool operator >=(num other) { |
- return (this == other) || (this > other); |
+ return (this == other) || (this > other); |
} |
+ |
bool operator <=(num other) { |
return (this == other) || (this < other); |
} |
+ |
bool _greaterThanFromInteger(int other) |
native "Integer_greaterThanFromInteger"; |
bool operator ==(other) { |
@@ -103,13 +121,16 @@ abstract class _IntegerImplementation { |
} |
return false; |
} |
+ |
bool _equalToInteger(int other) native "Integer_equalToInteger"; |
int abs() { |
return this < 0 ? -this : this; |
} |
+ |
int get sign { |
return (this > 0) ? 1 : (this < 0) ? -1 : 0; |
} |
+ |
bool get isEven => ((this & 1) == 0); |
bool get isOdd => !isEven; |
bool get isNaN => false; |
@@ -134,7 +155,7 @@ abstract class _IntegerImplementation { |
int compareTo(num other) { |
const int EQUAL = 0, LESS = -1, GREATER = 1; |
if (other is double) { |
- const int MAX_EXACT_INT_TO_DOUBLE = 9007199254740992; // 2^53. |
+ const int MAX_EXACT_INT_TO_DOUBLE = 9007199254740992; // 2^53. |
const int MIN_EXACT_INT_TO_DOUBLE = -MAX_EXACT_INT_TO_DOUBLE; |
double d = other; |
if (d.isInfinite) { |
@@ -161,15 +182,37 @@ abstract class _IntegerImplementation { |
} |
} |
- int round() { return this; } |
- int floor() { return this; } |
- int ceil() { return this; } |
- int truncate() { return this; } |
+ int round() { |
+ return this; |
+ } |
+ |
+ int floor() { |
+ return this; |
+ } |
+ |
+ int ceil() { |
+ return this; |
+ } |
- double roundToDouble() { return this.toDouble(); } |
- double floorToDouble() { return this.toDouble(); } |
- double ceilToDouble() { return this.toDouble(); } |
- double truncateToDouble() { return this.toDouble(); } |
+ int truncate() { |
+ return this; |
+ } |
+ |
+ double roundToDouble() { |
+ return this.toDouble(); |
+ } |
+ |
+ double floorToDouble() { |
+ return this.toDouble(); |
+ } |
+ |
+ double ceilToDouble() { |
+ return this.toDouble(); |
+ } |
+ |
+ double truncateToDouble() { |
+ return this.toDouble(); |
+ } |
num clamp(num lowerLimit, num upperLimit) { |
if (lowerLimit is! num) { |
@@ -196,17 +239,30 @@ abstract class _IntegerImplementation { |
return this; |
} |
- int toInt() { return this; } |
- double toDouble() { return new _Double.fromInteger(this); } |
- _Bigint _toBigint() { return new _Bigint._fromInt(this); } |
- num _toBigintOrDouble() { return _toBigint(); } |
+ int toInt() { |
+ return this; |
+ } |
+ |
+ double toDouble() { |
+ return new _Double.fromInteger(this); |
+ } |
+ |
+ _Bigint _toBigint() { |
+ return new _Bigint._fromInt(this); |
+ } |
+ |
+ num _toBigintOrDouble() { |
+ return _toBigint(); |
+ } |
String toStringAsFixed(int fractionDigits) { |
return this.toDouble().toStringAsFixed(fractionDigits); |
} |
+ |
String toStringAsExponential([int fractionDigits]) { |
return this.toDouble().toStringAsExponential(fractionDigits); |
} |
+ |
String toStringAsPrecision(int precision) { |
return this.toDouble().toStringAsPrecision(precision); |
} |
@@ -229,7 +285,7 @@ abstract class _IntegerImplementation { |
value ~/= radix; |
temp.add(_digits.codeUnitAt(digit)); |
} while (value > 0); |
- if (isNegative) temp.add(0x2d); // '-'. |
+ if (isNegative) temp.add(0x2d); // '-'. |
_OneByteString string = _OneByteString._allocate(temp.length); |
for (int i = 0, j = temp.length; j > 0; i++) { |
@@ -252,7 +308,7 @@ abstract class _IntegerImplementation { |
// Integer division, rounding up, to find number of _digits. |
length += (value.bitLength + bitsPerDigit - 1) ~/ bitsPerDigit; |
_OneByteString string = _OneByteString._allocate(length); |
- string._setAt(0, 0x2d); // '-'. Is overwritten if not negative. |
+ string._setAt(0, 0x2d); // '-'. Is overwritten if not negative. |
var mask = radix - 1; |
do { |
string._setAt(--length, _digits.codeUnitAt(value & mask)); |
@@ -310,10 +366,7 @@ abstract class _IntegerImplementation { |
final bool ac = x.isEven; |
int u = x; |
int v = y; |
- int a = 1, |
- b = 0, |
- c = 0, |
- d = 1; |
+ int a = 1, b = 0, c = 0, d = 1; |
do { |
while (u.isEven) { |
u >>= 1; |
@@ -403,8 +456,7 @@ abstract class _IntegerImplementation { |
class _Smi extends _IntegerImplementation implements int { |
factory _Smi._uninstantiable() { |
- throw new UnsupportedError( |
- "_Smi can only be allocated by the VM"); |
+ throw new UnsupportedError("_Smi can only be allocated by the VM"); |
} |
int get hashCode => this; |
int get _identityHashCode => this; |
@@ -424,63 +476,63 @@ class _Smi extends _IntegerImplementation implements int { |
* `_digitTable[n * 2]` and `_digitTable[n * 2 + 1]`. |
*/ |
static const _digitTable = const [ |
- 0x30, 0x30, 0x30, 0x31, 0x30, 0x32, 0x30, 0x33, |
- 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x30, 0x37, |
- 0x30, 0x38, 0x30, 0x39, 0x31, 0x30, 0x31, 0x31, |
- 0x31, 0x32, 0x31, 0x33, 0x31, 0x34, 0x31, 0x35, |
- 0x31, 0x36, 0x31, 0x37, 0x31, 0x38, 0x31, 0x39, |
- 0x32, 0x30, 0x32, 0x31, 0x32, 0x32, 0x32, 0x33, |
- 0x32, 0x34, 0x32, 0x35, 0x32, 0x36, 0x32, 0x37, |
- 0x32, 0x38, 0x32, 0x39, 0x33, 0x30, 0x33, 0x31, |
- 0x33, 0x32, 0x33, 0x33, 0x33, 0x34, 0x33, 0x35, |
- 0x33, 0x36, 0x33, 0x37, 0x33, 0x38, 0x33, 0x39, |
- 0x34, 0x30, 0x34, 0x31, 0x34, 0x32, 0x34, 0x33, |
- 0x34, 0x34, 0x34, 0x35, 0x34, 0x36, 0x34, 0x37, |
- 0x34, 0x38, 0x34, 0x39, 0x35, 0x30, 0x35, 0x31, |
- 0x35, 0x32, 0x35, 0x33, 0x35, 0x34, 0x35, 0x35, |
- 0x35, 0x36, 0x35, 0x37, 0x35, 0x38, 0x35, 0x39, |
- 0x36, 0x30, 0x36, 0x31, 0x36, 0x32, 0x36, 0x33, |
- 0x36, 0x34, 0x36, 0x35, 0x36, 0x36, 0x36, 0x37, |
- 0x36, 0x38, 0x36, 0x39, 0x37, 0x30, 0x37, 0x31, |
- 0x37, 0x32, 0x37, 0x33, 0x37, 0x34, 0x37, 0x35, |
- 0x37, 0x36, 0x37, 0x37, 0x37, 0x38, 0x37, 0x39, |
- 0x38, 0x30, 0x38, 0x31, 0x38, 0x32, 0x38, 0x33, |
- 0x38, 0x34, 0x38, 0x35, 0x38, 0x36, 0x38, 0x37, |
- 0x38, 0x38, 0x38, 0x39, 0x39, 0x30, 0x39, 0x31, |
- 0x39, 0x32, 0x39, 0x33, 0x39, 0x34, 0x39, 0x35, |
- 0x39, 0x36, 0x39, 0x37, 0x39, 0x38, 0x39, 0x39 |
+ 0x30, 0x30, 0x30, 0x31, 0x30, 0x32, 0x30, 0x33, // |
+ 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x30, 0x37, // |
+ 0x30, 0x38, 0x30, 0x39, 0x31, 0x30, 0x31, 0x31, // |
+ 0x31, 0x32, 0x31, 0x33, 0x31, 0x34, 0x31, 0x35, // |
+ 0x31, 0x36, 0x31, 0x37, 0x31, 0x38, 0x31, 0x39, // |
+ 0x32, 0x30, 0x32, 0x31, 0x32, 0x32, 0x32, 0x33, // |
+ 0x32, 0x34, 0x32, 0x35, 0x32, 0x36, 0x32, 0x37, // |
+ 0x32, 0x38, 0x32, 0x39, 0x33, 0x30, 0x33, 0x31, // |
+ 0x33, 0x32, 0x33, 0x33, 0x33, 0x34, 0x33, 0x35, // |
+ 0x33, 0x36, 0x33, 0x37, 0x33, 0x38, 0x33, 0x39, // |
+ 0x34, 0x30, 0x34, 0x31, 0x34, 0x32, 0x34, 0x33, // |
+ 0x34, 0x34, 0x34, 0x35, 0x34, 0x36, 0x34, 0x37, // |
+ 0x34, 0x38, 0x34, 0x39, 0x35, 0x30, 0x35, 0x31, // |
+ 0x35, 0x32, 0x35, 0x33, 0x35, 0x34, 0x35, 0x35, // |
+ 0x35, 0x36, 0x35, 0x37, 0x35, 0x38, 0x35, 0x39, // |
+ 0x36, 0x30, 0x36, 0x31, 0x36, 0x32, 0x36, 0x33, // |
+ 0x36, 0x34, 0x36, 0x35, 0x36, 0x36, 0x36, 0x37, // |
+ 0x36, 0x38, 0x36, 0x39, 0x37, 0x30, 0x37, 0x31, // |
+ 0x37, 0x32, 0x37, 0x33, 0x37, 0x34, 0x37, 0x35, // |
+ 0x37, 0x36, 0x37, 0x37, 0x37, 0x38, 0x37, 0x39, // |
+ 0x38, 0x30, 0x38, 0x31, 0x38, 0x32, 0x38, 0x33, // |
+ 0x38, 0x34, 0x38, 0x35, 0x38, 0x36, 0x38, 0x37, // |
+ 0x38, 0x38, 0x38, 0x39, 0x39, 0x30, 0x39, 0x31, // |
+ 0x39, 0x32, 0x39, 0x33, 0x39, 0x34, 0x39, 0x35, // |
+ 0x39, 0x36, 0x39, 0x37, 0x39, 0x38, 0x39, 0x39, // |
]; |
/** |
* Result of int.toString for -99, -98, ..., 98, 99. |
*/ |
static const _smallLookupTable = const [ |
- "-99", "-98", "-97", "-96", "-95", "-94", "-93", "-92", "-91", "-90", |
- "-89", "-88", "-87", "-86", "-85", "-84", "-83", "-82", "-81", "-80", |
- "-79", "-78", "-77", "-76", "-75", "-74", "-73", "-72", "-71", "-70", |
- "-69", "-68", "-67", "-66", "-65", "-64", "-63", "-62", "-61", "-60", |
- "-59", "-58", "-57", "-56", "-55", "-54", "-53", "-52", "-51", "-50", |
- "-49", "-48", "-47", "-46", "-45", "-44", "-43", "-42", "-41", "-40", |
- "-39", "-38", "-37", "-36", "-35", "-34", "-33", "-32", "-31", "-30", |
- "-29", "-28", "-27", "-26", "-25", "-24", "-23", "-22", "-21", "-20", |
- "-19", "-18", "-17", "-16", "-15", "-14", "-13", "-12", "-11", "-10", |
- "-9", "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1", "0", |
- "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", |
- "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", |
- "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", |
- "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", |
- "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", |
- "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", |
- "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", |
- "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", |
- "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", |
- "91", "92", "93", "94", "95", "96", "97", "98", "99" |
+ "-99", "-98", "-97", "-96", "-95", "-94", "-93", "-92", "-91", "-90", // |
+ "-89", "-88", "-87", "-86", "-85", "-84", "-83", "-82", "-81", "-80", // |
+ "-79", "-78", "-77", "-76", "-75", "-74", "-73", "-72", "-71", "-70", // |
+ "-69", "-68", "-67", "-66", "-65", "-64", "-63", "-62", "-61", "-60", // |
+ "-59", "-58", "-57", "-56", "-55", "-54", "-53", "-52", "-51", "-50", // |
+ "-49", "-48", "-47", "-46", "-45", "-44", "-43", "-42", "-41", "-40", // |
+ "-39", "-38", "-37", "-36", "-35", "-34", "-33", "-32", "-31", "-30", // |
+ "-29", "-28", "-27", "-26", "-25", "-24", "-23", "-22", "-21", "-20", // |
+ "-19", "-18", "-17", "-16", "-15", "-14", "-13", "-12", "-11", "-10", // |
+ "-9", "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1", "0", // |
+ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", // |
+ "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", // |
+ "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", // |
+ "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", // |
+ "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", // |
+ "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", // |
+ "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", // |
+ "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", // |
+ "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", // |
+ "91", "92", "93", "94", "95", "96", "97", "98", "99" // |
]; |
// Powers of 10 above 1000000 are indistinguishable by eye. |
- static const int _POW_10_7 = 10000000; |
- static const int _POW_10_8 = 100000000; |
- static const int _POW_10_9 = 1000000000; |
+ static const int _POW_10_7 = 10000000; |
+ static const int _POW_10_8 = 100000000; |
+ static const int _POW_10_9 = 1000000000; |
// Find the number of decimal digits in a positive smi. |
// Never called with numbers < 100. These are handled before calling. |
@@ -565,20 +617,21 @@ class _Smi extends _IntegerImplementation implements int { |
// Character code for '0'. |
const int DIGIT_ZERO = 0x30; |
if (negSmi > -10) { |
- return _OneByteString._allocate(2).._setAt(0, MINUS_SIGN) |
- .._setAt(1, DIGIT_ZERO - negSmi); |
+ return _OneByteString._allocate(2) |
+ .._setAt(0, MINUS_SIGN) |
+ .._setAt(1, DIGIT_ZERO - negSmi); |
} |
if (negSmi > -100) { |
int digitIndex = 2 * -negSmi; |
return _OneByteString._allocate(3) |
- .._setAt(0, MINUS_SIGN) |
- .._setAt(1, _digitTable[digitIndex]) |
- .._setAt(2, _digitTable[digitIndex + 1]); |
+ .._setAt(0, MINUS_SIGN) |
+ .._setAt(1, _digitTable[digitIndex]) |
+ .._setAt(2, _digitTable[digitIndex + 1]); |
} |
// Number of digits, not including minus. |
int digitCount = _negativeBase10Length(negSmi); |
_OneByteString result = _OneByteString._allocate(digitCount + 1); |
- result._setAt(0, MINUS_SIGN); // '-'. |
+ result._setAt(0, MINUS_SIGN); // '-'. |
int index = digitCount; |
do { |
var twoDigits = negSmi.remainder(100); |
@@ -603,8 +656,7 @@ class _Smi extends _IntegerImplementation implements int { |
// Represents integers that cannot be represented by Smi but fit into 64bits. |
class _Mint extends _IntegerImplementation implements int { |
factory _Mint._uninstantiable() { |
- throw new UnsupportedError( |
- "_Mint can only be allocated by the VM"); |
+ throw new UnsupportedError("_Mint can only be allocated by the VM"); |
} |
int get hashCode => this; |
int get _identityHashCode => this; |
@@ -621,5 +673,6 @@ class _Mint extends _IntegerImplementation implements int { |
return 0; |
} |
} |
+ |
int _shlFromInt(int other) native "Mint_shlFromInt"; |
} |