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 // TODO(srdjan): fix limitations. | 5 // TODO(srdjan): fix limitations. |
6 // - shift amount must be a Smi. | 6 // - shift amount must be a Smi. |
7 class _IntegerImplementation { | 7 class _IntegerImplementation { |
8 factory _IntegerImplementation._uninstantiable() { | 8 factory _IntegerImplementation._uninstantiable() { |
9 throw new UnsupportedError( | 9 throw new UnsupportedError( |
10 "_IntegerImplementation can only be allocated by the VM"); | 10 "_IntegerImplementation can only be allocated by the VM"); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 bool _equalToInteger(int other) native "Integer_equalToInteger"; | 88 bool _equalToInteger(int other) native "Integer_equalToInteger"; |
89 int abs() { | 89 int abs() { |
90 return this < 0 ? -this : this; | 90 return this < 0 ? -this : this; |
91 } | 91 } |
92 bool get isEven => ((this & 1) == 0); | 92 bool get isEven => ((this & 1) == 0); |
93 bool get isOdd => !isEven; | 93 bool get isOdd => !isEven; |
94 bool get isNaN => false; | 94 bool get isNaN => false; |
95 bool get isNegative => this < 0; | 95 bool get isNegative => this < 0; |
96 bool get isInfinite => false; | 96 bool get isInfinite => false; |
97 | 97 |
98 int toUnsigned(int width) { | |
99 return this & ((1 << width) - 1); | |
100 } | |
101 | |
102 int toSigned(int width) { | |
103 int signMask = 1 << (width - 1); | |
104 return (this & (signMask - 1)) - (this & signMask); | |
Lasse Reichstein Nielsen
2013/09/06 12:40:11
Clever. I don't see a good way to document why thi
sra1
2013/09/06 22:11:57
I'll give it a shot.
| |
105 } | |
106 | |
98 int compareTo(num other) { | 107 int compareTo(num other) { |
99 final int EQUAL = 0, LESS = -1, GREATER = 1; | 108 final int EQUAL = 0, LESS = -1, GREATER = 1; |
100 if (other is double) { | 109 if (other is double) { |
101 // TODO(floitsch): the following locals should be 'const'. | 110 // TODO(floitsch): the following locals should be 'const'. |
102 int MAX_EXACT_INT_TO_DOUBLE = 9007199254740992; // 2^53. | 111 int MAX_EXACT_INT_TO_DOUBLE = 9007199254740992; // 2^53. |
103 int MIN_EXACT_INT_TO_DOUBLE = -MAX_EXACT_INT_TO_DOUBLE; | 112 int MIN_EXACT_INT_TO_DOUBLE = -MAX_EXACT_INT_TO_DOUBLE; |
104 double d = other; | 113 double d = other; |
105 if (d.isInfinite) { | 114 if (d.isInfinite) { |
106 return d == double.NEGATIVE_INFINITY ? GREATER : LESS; | 115 return d == double.NEGATIVE_INFINITY ? GREATER : LESS; |
107 } | 116 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 | 214 |
206 class _Smi extends _IntegerImplementation implements int { | 215 class _Smi extends _IntegerImplementation implements int { |
207 factory _Smi._uninstantiable() { | 216 factory _Smi._uninstantiable() { |
208 throw new UnsupportedError( | 217 throw new UnsupportedError( |
209 "_Smi can only be allocated by the VM"); | 218 "_Smi can only be allocated by the VM"); |
210 } | 219 } |
211 int get hashCode { | 220 int get hashCode { |
212 return this; | 221 return this; |
213 } | 222 } |
214 int operator ~() native "Smi_bitNegate"; | 223 int operator ~() native "Smi_bitNegate"; |
224 int get bitLength native "Smi_bitLength"; | |
225 | |
215 int _shrFromInt(int other) native "Smi_shrFromInt"; | 226 int _shrFromInt(int other) native "Smi_shrFromInt"; |
216 int _shlFromInt(int other) native "Smi_shlFromInt"; | 227 int _shlFromInt(int other) native "Smi_shlFromInt"; |
217 | 228 |
218 String toString() { | 229 String toString() { |
219 if (this == 0) return "0"; | 230 if (this == 0) return "0"; |
220 var reversed = new List(); | 231 var reversed = new List(); |
221 var val = this < 0 ? -this : this; | 232 var val = this < 0 ? -this : this; |
222 while (val > 0) { | 233 while (val > 0) { |
223 reversed.add((val % 10) + 0x30); | 234 reversed.add((val % 10) + 0x30); |
224 val = val ~/ 10; | 235 val = val ~/ 10; |
(...skipping 20 matching lines...) Expand all Loading... | |
245 // Represents integers that cannot be represented by Smi but fit into 64bits. | 256 // Represents integers that cannot be represented by Smi but fit into 64bits. |
246 class _Mint extends _IntegerImplementation implements int { | 257 class _Mint extends _IntegerImplementation implements int { |
247 factory _Mint._uninstantiable() { | 258 factory _Mint._uninstantiable() { |
248 throw new UnsupportedError( | 259 throw new UnsupportedError( |
249 "_Mint can only be allocated by the VM"); | 260 "_Mint can only be allocated by the VM"); |
250 } | 261 } |
251 int get hashCode { | 262 int get hashCode { |
252 return this; | 263 return this; |
253 } | 264 } |
254 int operator ~() native "Mint_bitNegate"; | 265 int operator ~() native "Mint_bitNegate"; |
266 int get bitLength native "Mint_bitLength"; | |
255 | 267 |
256 // Shift by mint exceeds range that can be handled by the VM. | 268 // Shift by mint exceeds range that can be handled by the VM. |
257 int _shrFromInt(int other) { | 269 int _shrFromInt(int other) { |
258 if (other < 0) { | 270 if (other < 0) { |
259 return -1; | 271 return -1; |
260 } else { | 272 } else { |
261 return 0; | 273 return 0; |
262 } | 274 } |
263 } | 275 } |
264 int _shlFromInt(int other) native "Mint_shlFromInt"; | 276 int _shlFromInt(int other) native "Mint_shlFromInt"; |
265 } | 277 } |
266 | 278 |
267 // A number that can be represented as Smi or Mint will never be represented as | 279 // A number that can be represented as Smi or Mint will never be represented as |
268 // Bigint. | 280 // Bigint. |
269 class _Bigint extends _IntegerImplementation implements int { | 281 class _Bigint extends _IntegerImplementation implements int { |
270 factory _Bigint._uninstantiable() { | 282 factory _Bigint._uninstantiable() { |
271 throw new UnsupportedError( | 283 throw new UnsupportedError( |
272 "_Bigint can only be allocated by the VM"); | 284 "_Bigint can only be allocated by the VM"); |
273 } | 285 } |
274 int get hashCode { | 286 int get hashCode { |
275 return this; | 287 return this; |
276 } | 288 } |
277 int operator ~() native "Bigint_bitNegate"; | 289 int operator ~() native "Bigint_bitNegate"; |
290 int get bitLength native "Bigint_bitLength"; | |
278 | 291 |
279 // Shift by bigint exceeds range that can be handled by the VM. | 292 // Shift by bigint exceeds range that can be handled by the VM. |
280 int _shrFromInt(int other) { | 293 int _shrFromInt(int other) { |
281 if (other < 0) { | 294 if (other < 0) { |
282 return -1; | 295 return -1; |
283 } else { | 296 } else { |
284 return 0; | 297 return 0; |
285 } | 298 } |
286 } | 299 } |
287 int _shlFromInt(int other) native "Bigint_shlFromInt"; | 300 int _shlFromInt(int other) native "Bigint_shlFromInt"; |
288 | 301 |
289 int pow(int exponent) { | 302 int pow(int exponent) { |
290 throw "Bigint.pow not implemented"; | 303 throw "Bigint.pow not implemented"; |
291 } | 304 } |
292 } | 305 } |
OLD | NEW |