Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(694)

Side by Side Diff: pkg/fixnum/lib/src/int64.dart

Issue 23541010: New divide algorithm for Int64, final fields (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/fixnum/test/int_64_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 */
11 class Int64 implements IntX { 11 class Int64 implements IntX {
12 12
13 // A 64-bit integer is represented internally as three non-negative 13 // A 64-bit integer is represented internally as three non-negative
14 // integers, storing the 22 low, 22 middle, and 20 high bits of the 14 // integers, storing the 22 low, 22 middle, and 20 high bits of the
15 // 64-bit value. _l (low) and _m (middle) are in the range 15 // 64-bit value. _l (low) and _m (middle) are in the range
16 // [0, 2^22 - 1] and _h (high) is in the range [0, 2^20 - 1]. 16 // [0, 2^22 - 1] and _h (high) is in the range [0, 2^20 - 1].
17 int _l, _m, _h; 17 final int _l, _m, _h;
18
19 // Note: instances of [Int64] are immutable outside of this library,
20 // therefore we may return a reference to an existing instance.
21 // We take care to perform mutation only on internally-generated
22 // instances before they are exposed to external code.
23 18
24 // Note: several functions require _BITS == 22 -- do not change this value. 19 // Note: several functions require _BITS == 22 -- do not change this value.
25 static const int _BITS = 22; 20 static const int _BITS = 22;
26 static const int _BITS01 = 44; // 2 * _BITS 21 static const int _BITS01 = 44; // 2 * _BITS
27 static const int _BITS2 = 20; // 64 - _BITS01 22 static const int _BITS2 = 20; // 64 - _BITS01
28 static const int _MASK = 4194303; // (1 << _BITS) - 1 23 static const int _MASK = 4194303; // (1 << _BITS) - 1
29 static const int _MASK2 = 1048575; // (1 << _BITS2) - 1 24 static const int _MASK2 = 1048575; // (1 << _BITS2) - 1
30 static const int _SIGN_BIT = 19; // _BITS2 - 1 25 static const int _SIGN_BIT = 19; // _BITS2 - 1
31 static const int _SIGN_BIT_MASK = 524288; // 1 << _SIGN_BIT 26 static const int _SIGN_BIT_MASK = 524288; // 1 << _SIGN_BIT
32 27
33 // Cached constants
34 static Int64 _MAX_VALUE;
35 static Int64 _MIN_VALUE;
36 static Int64 _ZERO;
37 static Int64 _ONE;
38 static Int64 _TWO;
39
40 // The remainder of the last divide operation.
41 static Int64 _remainder;
42
43 /** 28 /**
44 * The maximum positive value attainable by an [Int64], namely 29 * The maximum positive value attainable by an [Int64], namely
45 * 9,223,372,036,854,775,807. 30 * 9,223,372,036,854,775,807.
46 */ 31 */
47 static Int64 get MAX_VALUE { 32 static const Int64 MAX_VALUE = const Int64._bits(_MASK, _MASK, _MASK2 >> 1);
48 if (_MAX_VALUE == null) {
49 _MAX_VALUE = new Int64._bits(_MASK, _MASK, _MASK2 >> 1);
50 }
51 return _MAX_VALUE;
52 }
53 33
54 /** 34 /**
55 * The minimum positive value attainable by an [Int64], namely 35 * The minimum positive value attainable by an [Int64], namely
56 * -9,223,372,036,854,775,808. 36 * -9,223,372,036,854,775,808.
57 */ 37 */
58 static Int64 get MIN_VALUE { 38 static const Int64 MIN_VALUE = const Int64._bits(0, 0, _SIGN_BIT_MASK);
59 if (_MIN_VALUE == null) {
60 _MIN_VALUE = new Int64._bits(0, 0, _SIGN_BIT_MASK);
61 }
62 return _MIN_VALUE;
63 }
64 39
65 /** 40 /**
66 * An [Int64] constant equal to 0. 41 * An [Int64] constant equal to 0.
67 */ 42 */
68 static Int64 get ZERO { 43 static const Int64 ZERO = const Int64._bits(0, 0, 0);
69 if (_ZERO == null) {
70 _ZERO = new Int64();
71 }
72 return _ZERO;
73 }
74 44
75 /** 45 /**
76 * An [Int64] constant equal to 1. 46 * An [Int64] constant equal to 1.
77 */ 47 */
78 static Int64 get ONE { 48 static const Int64 ONE = const Int64._bits(1, 0, 0);
79 if (_ONE == null) {
80 _ONE = new Int64._bits(1, 0, 0);
81 }
82 return _ONE;
83 }
84 49
85 /** 50 /**
86 * An [Int64] constant equal to 2. 51 * An [Int64] constant equal to 2.
87 */ 52 */
88 static Int64 get TWO { 53 static const Int64 TWO = const Int64._bits(2, 0, 0);
89 if (_TWO == null) { 54
90 _TWO = new Int64._bits(2, 0, 0); 55 /**
91 } 56 * Constructs an [Int64] with a given bitwise representation. No validation
92 return _TWO; 57 * is performed.
93 } 58 */
59 const Int64._bits(int this._l, int this._m, int this._h);
94 60
95 /** 61 /**
96 * Parses a [String] in a given [radix] between 2 and 36 and returns an 62 * Parses a [String] in a given [radix] between 2 and 36 and returns an
97 * [Int64]. 63 * [Int64].
98 */ 64 */
99 static Int64 parseRadix(String s, int radix) { 65 static Int64 parseRadix(String s, int radix) {
100 if ((radix <= 1) || (radix > 36)) { 66 if ((radix <= 1) || (radix > 36)) {
101 throw new ArgumentError("Bad radix: $radix"); 67 throw new ArgumentError("Bad radix: $radix");
102 } 68 }
103 return _parseRadix(s, radix); 69 return _parseRadix(s, radix);
(...skipping 11 matching lines...) Expand all
115 int c = s.codeUnitAt(i); 81 int c = s.codeUnitAt(i);
116 int digit = Int32._decodeDigit(c); 82 int digit = Int32._decodeDigit(c);
117 if (digit < 0 || digit >= radix) { 83 if (digit < 0 || digit >= radix) {
118 throw new Exception("Non-radix char code: $c"); 84 throw new Exception("Non-radix char code: $c");
119 } 85 }
120 86
121 // [radix] and [digit] are at most 6 bits, component is 22, so we can 87 // [radix] and [digit] are at most 6 bits, component is 22, so we can
122 // multiply and add within 30 bit temporary values. 88 // multiply and add within 30 bit temporary values.
123 d0 = d0 * radix + digit; 89 d0 = d0 * radix + digit;
124 int carry = d0 >> _BITS; 90 int carry = d0 >> _BITS;
125 d0 &= _MASK; 91 d0 = _MASK & d0;
126 92
127 d1 = d1 * radix + carry; 93 d1 = d1 * radix + carry;
128 carry = d1 >> _BITS; 94 carry = d1 >> _BITS;
129 d1 &= _MASK; 95 d1 = _MASK & d1;;
130 96
131 d2 = d2 * radix + carry; 97 d2 = d2 * radix + carry;
132 d2 &= _MASK2; 98 d2 = _MASK2 & d2;
133 } 99 }
134 100
135 if (negative) { 101 if (negative) return _negate(d0, d1, d2);
136 d0 = 0 - d0; 102
137 int borrow = (d0 >> _BITS) & 1;
138 d0 &= _MASK;
139 d1 = 0 - d1 - borrow;
140 borrow = (d1 >> _BITS) & 1;
141 d1 &= _MASK;
142 d2 = 0 - d2 - borrow;
143 d2 &= _MASK2;
144 }
145 return new Int64._bits(d0, d1, d2); 103 return new Int64._bits(d0, d1, d2);
146 } 104 }
147 105
148 /** 106 /**
149 * Parses a decimal [String] and returns an [Int64]. 107 * Parses a decimal [String] and returns an [Int64].
150 */ 108 */
151 static Int64 parseInt(String s) => _parseRadix(s, 10); 109 static Int64 parseInt(String s) => _parseRadix(s, 10);
152 110
153 /** 111 /**
154 * Parses a hexadecimal [String] and returns an [Int64]. 112 * Parses a hexadecimal [String] and returns an [Int64].
155 */ 113 */
156 static Int64 parseHex(String s) => _parseRadix(s, 16); 114 static Int64 parseHex(String s) => _parseRadix(s, 16);
157 115
158 // 116 //
159 // Public constructors 117 // Public constructors
160 // 118 //
161 119
162 /** 120 /**
163 * Constructs an [Int64] equal to 0. 121 * Constructs an [Int64] equal to 0.
164 */ 122 */
165 Int64() : _l = 0, _m = 0, _h = 0; 123 Int64() : _l = 0, _m = 0, _h = 0;
166 124
167 /** 125 /**
168 * Constructs an [Int64] with a given [int] value. 126 * Constructs an [Int64] with a given [int] value.
169 */ 127 */
170 Int64.fromInt(int value) { 128 factory Int64.fromInt(int value) {
129 int v0 = 0, v1 = 0, v2 = 0;
171 bool negative = false; 130 bool negative = false;
172 if (value < 0) { 131 if (value < 0) {
173 negative = true; 132 negative = true;
174 value = -value - 1; 133 value = -value - 1;
175 } 134 }
176 if (_haveBigInts) { 135 if (_haveBigInts) {
177 _l = value & _MASK; 136 v0 = _MASK & value;
178 _m = (value >> _BITS) & _MASK; 137 v1 = _MASK & (value >> _BITS);
179 _h = (value >> _BITS01) & _MASK2; 138 v2 = _MASK2 & (value >> _BITS01);
180 } else { 139 } else {
181 // Avoid using bitwise operations that coerce their input to 32 bits. 140 // Avoid using bitwise operations that coerce their input to 32 bits.
182 _h = value ~/ 17592186044416; // 2^44 141 v2 = value ~/ 17592186044416; // 2^44
183 value -= _h * 17592186044416; 142 value -= v2 * 17592186044416;
184 _m = value ~/ 4194304; // 2^22 143 v1 = value ~/ 4194304; // 2^22
185 value -= _m * 4194304; 144 value -= v1 * 4194304;
186 _l = value; 145 v0 = value;
187 } 146 }
188 147
189 if (negative) { 148 if (negative) {
190 _l = ~_l & _MASK; 149 v0 = _MASK & ~v0;
191 _m = ~_m & _MASK; 150 v1 = _MASK & ~v1;
192 _h = ~_h & _MASK2; 151 v2 = _MASK2 & ~v2;
193 } 152 }
153 return new Int64._bits(v0, v1, v2);
194 } 154 }
195 155
196 factory Int64.fromBytes(List<int> bytes) { 156 factory Int64.fromBytes(List<int> bytes) {
197 int top = bytes[7] & 0xff; 157 int top = bytes[7] & 0xff;
198 top <<= 8; 158 top <<= 8;
199 top |= bytes[6] & 0xff; 159 top |= bytes[6] & 0xff;
200 top <<= 8; 160 top <<= 8;
201 top |= bytes[5] & 0xff; 161 top |= bytes[5] & 0xff;
202 top <<= 8; 162 top <<= 8;
203 top |= bytes[4] & 0xff; 163 top |= bytes[4] & 0xff;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 top &= 0xffffffff; 201 top &= 0xffffffff;
242 bottom &= 0xffffffff; 202 bottom &= 0xffffffff;
243 int d0 = bottom & _MASK; 203 int d0 = bottom & _MASK;
244 int d1 = ((top & 0xfff) << 10) | ((bottom >> _BITS) & 0x3ff); 204 int d1 = ((top & 0xfff) << 10) | ((bottom >> _BITS) & 0x3ff);
245 int d2 = (top >> 12) & _MASK2; 205 int d2 = (top >> 12) & _MASK2;
246 return new Int64._bits(d0, d1, d2); 206 return new Int64._bits(d0, d1, d2);
247 } 207 }
248 208
249 // Returns the [Int64] representation of the specified value. Throws 209 // Returns the [Int64] representation of the specified value. Throws
250 // [ArgumentError] for non-integer arguments. 210 // [ArgumentError] for non-integer arguments.
251 Int64 _promote(val) { 211 static Int64 _promote(val) {
252 if (val is Int64) { 212 if (val is Int64) {
253 return val; 213 return val;
254 } else if (val is int) { 214 } else if (val is int) {
255 return new Int64.fromInt(val); 215 return new Int64.fromInt(val);
256 } else if (val is Int32) { 216 } else if (val is Int32) {
257 return val.toInt64(); 217 return val.toInt64();
258 } 218 }
259 throw new ArgumentError(val); 219 throw new ArgumentError(val);
260 } 220 }
261 221
262 Int64 operator +(other) { 222 Int64 operator +(other) {
263 Int64 o = _promote(other); 223 Int64 o = _promote(other);
264 int sum0 = _l + o._l; 224 int sum0 = _l + o._l;
265 int sum1 = _m + o._m + _shiftRight(sum0, _BITS); 225 int sum1 = _m + o._m + (sum0 >> _BITS);
266 int sum2 = _h + o._h + _shiftRight(sum1, _BITS); 226 int sum2 = _h + o._h + (sum1 >> _BITS);
267 227 return Int64._masked(sum0, sum1, sum2);
268 Int64 result = new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK2);
269 return result;
270 } 228 }
271 229
272 Int64 operator -(other) { 230 Int64 operator -(other) {
273 Int64 o = _promote(other); 231 Int64 o = _promote(other);
274 int sum0 = _l - o._l; 232 return _sub(_l, _m, _h, o._l, o._m, o._h);
275 int sum1 = _m - o._m + _shiftRight(sum0, _BITS);
276 int sum2 = _h - o._h + _shiftRight(sum1, _BITS);
277
278 Int64 result = new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK2);
279 return result;
280 } 233 }
281 234
282 Int64 operator -() { 235 Int64 operator -() => _negate(_l, _m, _h);
283 // Like 0 - this.
284 int sum0 = -_l;
285 int sum1 = -_m + _shiftRight(sum0, _BITS);
286 int sum2 = -_h + _shiftRight(sum1, _BITS);
287
288 return new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK2);
289 }
290 236
291 Int64 operator *(other) { 237 Int64 operator *(other) {
292 Int64 o = _promote(other); 238 Int64 o = _promote(other);
293 239
294 // Grab 13-bit chunks. 240 // Grab 13-bit chunks.
295 int a0 = _l & 0x1fff; 241 int a0 = _l & 0x1fff;
296 int a1 = (_l >> 13) | ((_m & 0xf) << 9); 242 int a1 = (_l >> 13) | ((_m & 0xf) << 9);
297 int a2 = (_m >> 4) & 0x1fff; 243 int a2 = (_m >> 4) & 0x1fff;
298 int a3 = (_m >> 17) | ((_h & 0xff) << 5); 244 int a3 = (_m >> 17) | ((_h & 0xff) << 5);
299 int a4 = (_h & 0xfff00) >> 8; 245 int a4 = (_h & 0xfff00) >> 8;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 // Propagate high bits from c0 -> c1, c1 -> c2. 311 // Propagate high bits from c0 -> c1, c1 -> c2.
366 c1 += c0 >> _BITS; 312 c1 += c0 >> _BITS;
367 c0 &= _MASK; 313 c0 &= _MASK;
368 c2 += c1 >> _BITS; 314 c2 += c1 >> _BITS;
369 c1 &= _MASK; 315 c1 &= _MASK;
370 c2 &= _MASK2; 316 c2 &= _MASK2;
371 317
372 return new Int64._bits(c0, c1, c2); 318 return new Int64._bits(c0, c1, c2);
373 } 319 }
374 320
375 Int64 operator %(other) { 321 Int64 operator %(other) => _divide(this, other, _RETURN_MOD);
376 if (other.isZero) {
377 throw new IntegerDivisionByZeroException();
378 }
379 if (this.isZero) {
380 return ZERO;
381 }
382 Int64 o = _promote(other).abs();
383 _divMod(this, o, true);
384 return _remainder < 0 ? (_remainder + o) : _remainder;
385 }
386 322
387 Int64 operator ~/(other) => _divMod(this, _promote(other), false); 323 Int64 operator ~/(other) => _divide(this, other, _RETURN_DIV);
388 324
389 // Int64 remainder(other) => this - (this ~/ other) * other; 325 Int64 remainder(other) => _divide(this, other, _RETURN_REM);
390 Int64 remainder(other) {
391 if (other.isZero) {
392 throw new IntegerDivisionByZeroException();
393 }
394 Int64 o = _promote(other).abs();
395 _divMod(this, o, true);
396 return _remainder;
397 }
398 326
399 Int64 operator &(other) { 327 Int64 operator &(other) {
400 Int64 o = _promote(other); 328 Int64 o = _promote(other);
401 int a0 = _l & o._l; 329 int a0 = _l & o._l;
402 int a1 = _m & o._m; 330 int a1 = _m & o._m;
403 int a2 = _h & o._h; 331 int a2 = _h & o._h;
404 return new Int64._bits(a0, a1, a2); 332 return new Int64._bits(a0, a1, a2);
405 } 333 }
406 334
407 Int64 operator |(other) { 335 Int64 operator |(other) {
408 Int64 o = _promote(other); 336 Int64 o = _promote(other);
409 int a0 = _l | o._l; 337 int a0 = _l | o._l;
410 int a1 = _m | o._m; 338 int a1 = _m | o._m;
411 int a2 = _h | o._h; 339 int a2 = _h | o._h;
412 return new Int64._bits(a0, a1, a2); 340 return new Int64._bits(a0, a1, a2);
413 } 341 }
414 342
415 Int64 operator ^(other) { 343 Int64 operator ^(other) {
416 Int64 o = _promote(other); 344 Int64 o = _promote(other);
417 int a0 = _l ^ o._l; 345 int a0 = _l ^ o._l;
418 int a1 = _m ^ o._m; 346 int a1 = _m ^ o._m;
419 int a2 = _h ^ o._h; 347 int a2 = _h ^ o._h;
420 return new Int64._bits(a0, a1, a2); 348 return new Int64._bits(a0, a1, a2);
421 } 349 }
422 350
423 Int64 operator ~() { 351 Int64 operator ~() {
424 var result = new Int64._bits((~_l) & _MASK, (~_m) & _MASK, (~_h) & _MASK2); 352 return Int64._masked(~_l, ~_m, ~_h);
425 return result;
426 } 353 }
427 354
428 Int64 operator <<(int n) { 355 Int64 operator <<(int n) {
429 if (n < 0) { 356 if (n < 0) {
430 throw new ArgumentError(n); 357 throw new ArgumentError(n);
431 } 358 }
432 n &= 63; 359 n &= 63;
433 360
434 int res0, res1, res2; 361 int res0, res1, res2;
435 if (n < _BITS) { 362 if (n < _BITS) {
436 res0 = _l << n; 363 res0 = _l << n;
437 res1 = (_m << n) | (_l >> (_BITS - n)); 364 res1 = (_m << n) | (_l >> (_BITS - n));
438 res2 = (_h << n) | (_m >> (_BITS - n)); 365 res2 = (_h << n) | (_m >> (_BITS - n));
439 } else if (n < _BITS01) { 366 } else if (n < _BITS01) {
440 res0 = 0; 367 res0 = 0;
441 res1 = _l << (n - _BITS); 368 res1 = _l << (n - _BITS);
442 res2 = (_m << (n - _BITS)) | (_l >> (_BITS01 - n)); 369 res2 = (_m << (n - _BITS)) | (_l >> (_BITS01 - n));
443 } else { 370 } else {
444 res0 = 0; 371 res0 = 0;
445 res1 = 0; 372 res1 = 0;
446 res2 = _l << (n - _BITS01); 373 res2 = _l << (n - _BITS01);
447 } 374 }
448 375
449 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK2); 376 return Int64._masked(res0, res1, res2);
450 } 377 }
451 378
452 Int64 operator >>(int n) { 379 Int64 operator >>(int n) {
453 if (n < 0) { 380 if (n < 0) {
454 throw new ArgumentError(n); 381 throw new ArgumentError(n);
455 } 382 }
456 n &= 63; 383 n &= 63;
457 384
458 int res0, res1, res2; 385 int res0, res1, res2;
459 386
460 // Sign extend h(a). 387 // Sign extend h(a).
461 int a2 = _h; 388 int a2 = _h;
462 bool negative = (a2 & _SIGN_BIT_MASK) != 0; 389 bool negative = (a2 & _SIGN_BIT_MASK) != 0;
463 if (negative) { 390 if (negative && _MASK > _MASK2) {
464 a2 += 0x3 << _BITS2; // add extra one bits on the left 391 // Add extra one bits on the left so the sign gets shifted into the wider
392 // lower words.
393 a2 += (_MASK - _MASK2);
465 } 394 }
466 395
467 if (n < _BITS) { 396 if (n < _BITS) {
468 res2 = _shiftRight(a2, n); 397 res2 = _shiftRight(a2, n);
469 if (negative) { 398 if (negative) {
470 res2 |= _MASK2 & ~(_MASK2 >> n); 399 res2 |= _MASK2 & ~(_MASK2 >> n);
471 } 400 }
472 res1 = _shiftRight(_m, n) | (a2 << (_BITS - n)); 401 res1 = _shiftRight(_m, n) | (a2 << (_BITS - n));
473 res0 = _shiftRight(_l, n) | (_m << (_BITS - n)); 402 res0 = _shiftRight(_l, n) | (_m << (_BITS - n));
474 } else if (n < _BITS01) { 403 } else if (n < _BITS01) {
475 res2 = negative ? _MASK2 : 0; 404 res2 = negative ? _MASK2 : 0;
476 res1 = _shiftRight(a2, n - _BITS); 405 res1 = _shiftRight(a2, n - _BITS);
477 if (negative) { 406 if (negative) {
478 res1 |= _MASK & ~(_MASK >> (n - _BITS)); 407 res1 |= _MASK & ~(_MASK >> (n - _BITS));
479 } 408 }
480 res0 = _shiftRight(_m, n - _BITS) | (a2 << (_BITS01 - n)); 409 res0 = _shiftRight(_m, n - _BITS) | (a2 << (_BITS01 - n));
481 } else { 410 } else {
482 res2 = negative ? _MASK2 : 0; 411 res2 = negative ? _MASK2 : 0;
483 res1 = negative ? _MASK : 0; 412 res1 = negative ? _MASK : 0;
484 res0 = _shiftRight(a2, n - _BITS01); 413 res0 = _shiftRight(a2, n - _BITS01);
485 if (negative) { 414 if (negative) {
486 res0 |= _MASK & ~(_MASK >> (n - _BITS01)); 415 res0 |= _MASK & ~(_MASK >> (n - _BITS01));
487 } 416 }
488 } 417 }
489 418
490 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK2); 419 return Int64._masked(res0, res1, res2);
491 } 420 }
492 421
493 Int64 shiftRightUnsigned(int n) { 422 Int64 shiftRightUnsigned(int n) {
494 if (n < 0) { 423 if (n < 0) {
495 throw new ArgumentError(n); 424 throw new ArgumentError(n);
496 } 425 }
497 n &= 63; 426 n &= 63;
498 427
499 int res0, res1, res2; 428 int res0, res1, res2;
500 int a2 = _h & _MASK2; // Ensure a2 is positive. 429 int a2 = _MASK2 & _h; // Ensure a2 is positive.
501 if (n < _BITS) { 430 if (n < _BITS) {
502 res2 = a2 >> n; 431 res2 = a2 >> n;
503 res1 = (_m >> n) | (a2 << (_BITS - n)); 432 res1 = (_m >> n) | (a2 << (_BITS - n));
504 res0 = (_l >> n) | (_m << (_BITS - n)); 433 res0 = (_l >> n) | (_m << (_BITS - n));
505 } else if (n < _BITS01) { 434 } else if (n < _BITS01) {
506 res2 = 0; 435 res2 = 0;
507 res1 = a2 >> (n - _BITS); 436 res1 = a2 >> (n - _BITS);
508 res0 = (_m >> (n - _BITS)) | (_h << (_BITS01 - n)); 437 res0 = (_m >> (n - _BITS)) | (_h << (_BITS01 - n));
509 } else { 438 } else {
510 res2 = 0; 439 res2 = 0;
511 res1 = 0; 440 res1 = 0;
512 res0 = a2 >> (n - _BITS01); 441 res0 = a2 >> (n - _BITS01);
513 } 442 }
514 443
515 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK2); 444 return Int64._masked(res0, res1, res2);
516 } 445 }
517 446
518 /** 447 /**
519 * Returns [true] if this [Int64] has the same numeric value as the 448 * Returns [true] if this [Int64] has the same numeric value as the
520 * given object. The argument may be an [int] or an [IntX]. 449 * given object. The argument may be an [int] or an [IntX].
521 */ 450 */
522 bool operator ==(other) { 451 bool operator ==(other) {
523 Int64 o; 452 Int64 o;
524 if (other is Int64) { 453 if (other is Int64) {
525 o = other; 454 o = other;
526 } else if (other is int) { 455 } else if (other is int) {
456 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 // low word then it can't be numerically equal.
459 if ((_MASK & other) == other) return false;
527 o = new Int64.fromInt(other); 460 o = new Int64.fromInt(other);
528 } else if (other is Int32) { 461 } else if (other is Int32) {
529 o = other.toInt64(); 462 o = other.toInt64();
530 } 463 }
531 if (o != null) { 464 if (o != null) {
532 return _l == o._l && _m == o._m && _h == o._h; 465 return _l == o._l && _m == o._m && _h == o._h;
533 } 466 }
534 return false; 467 return false;
535 } 468 }
536 469
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 return this.compareTo(other) > 0; 504 return this.compareTo(other) > 0;
572 } 505 }
573 506
574 bool operator >=(other) { 507 bool operator >=(other) {
575 return this.compareTo(other) >= 0; 508 return this.compareTo(other) >= 0;
576 } 509 }
577 510
578 bool get isEven => (_l & 0x1) == 0; 511 bool get isEven => (_l & 0x1) == 0;
579 bool get isMaxValue => (_h == _MASK2 >> 1) && _m == _MASK && _l == _MASK; 512 bool get isMaxValue => (_h == _MASK2 >> 1) && _m == _MASK && _l == _MASK;
580 bool get isMinValue => _h == _SIGN_BIT_MASK && _m == 0 && _l == 0; 513 bool get isMinValue => _h == _SIGN_BIT_MASK && _m == 0 && _l == 0;
581 bool get isNegative => (_h >> (_BITS2 - 1)) != 0; 514 bool get isNegative => (_h & _SIGN_BIT_MASK) != 0;
582 bool get isOdd => (_l & 0x1) == 1; 515 bool get isOdd => (_l & 0x1) == 1;
583 bool get isZero => _h == 0 && _m == 0 && _l == 0; 516 bool get isZero => _h == 0 && _m == 0 && _l == 0;
584 517
585 /** 518 /**
586 * Returns a hash code based on all the bits of this [Int64]. 519 * Returns a hash code based on all the bits of this [Int64].
587 */ 520 */
588 int get hashCode { 521 int get hashCode {
522 // TODO(sra): Should we ensure that hashCode values match corresponding int?
523 // i.e. should `new Int64.fromInt(x).hashCode == x.hashCode`?
589 int bottom = ((_m & 0x3ff) << _BITS) | _l; 524 int bottom = ((_m & 0x3ff) << _BITS) | _l;
590 int top = (_h << 12) | ((_m >> 10) & 0xfff); 525 int top = (_h << 12) | ((_m >> 10) & 0xfff);
591 return bottom ^ top; 526 return bottom ^ top;
592 } 527 }
593 528
594 Int64 abs() { 529 Int64 abs() {
595 return this < 0 ? -this : this; 530 return this.isNegative ? -this : this;
596 } 531 }
597 532
598 /** 533 /**
599 * Returns the number of leading zeros in this [Int64] as an [int] 534 * Returns the number of leading zeros in this [Int64] as an [int]
600 * between 0 and 64. 535 * between 0 and 64.
601 */ 536 */
602 int numberOfLeadingZeros() { 537 int numberOfLeadingZeros() {
603 int b2 = Int32._numberOfLeadingZeros(_h); 538 int b2 = Int32._numberOfLeadingZeros(_h);
604 if (b2 == 32) { 539 if (b2 == 32) {
605 int b1 = Int32._numberOfLeadingZeros(_m); 540 int b1 = Int32._numberOfLeadingZeros(_m);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 */ 617 */
683 Int64 toInt64() => this; 618 Int64 toInt64() => this;
684 619
685 /** 620 /**
686 * Returns the value of this [Int64] as a decimal [String]. 621 * Returns the value of this [Int64] as a decimal [String].
687 */ 622 */
688 String toString() => _toRadixString(10); 623 String toString() => _toRadixString(10);
689 624
690 // TODO(rice) - Make this faster by avoiding arithmetic. 625 // TODO(rice) - Make this faster by avoiding arithmetic.
691 String toHexString() { 626 String toHexString() {
692 Int64 x = new Int64._copy(this); 627 if (isZero) return "0";
693 if (isZero) { 628 Int64 x = this;
694 return "0";
695 }
696 String hexStr = ""; 629 String hexStr = "";
697 Int64 digit_f = new Int64.fromInt(0xf); 630 Int64 digit_f = new Int64.fromInt(0xf);
698 while (!x.isZero) { 631 while (!x.isZero) {
699 int digit = x._l & 0xf; 632 int digit = x._l & 0xf;
700 hexStr = "${_hexDigit(digit)}$hexStr"; 633 hexStr = "${_hexDigit(digit)}$hexStr";
701 x = x.shiftRightUnsigned(4); 634 x = x.shiftRightUnsigned(4);
702 } 635 }
703 return hexStr; 636 return hexStr;
704 } 637 }
705 638
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 33 * 33 * 33, 780 33 * 33 * 33,
848 34 * 34 * 34, 781 34 * 34 * 34,
849 35 * 35 * 35, 782 35 * 35 * 35,
850 36 * 36 * 36 783 36 * 36 * 36
851 ]; 784 ];
852 785
853 String toDebugString() { 786 String toDebugString() {
854 return "Int64[_l=$_l, _m=$_m, _h=$_h]"; 787 return "Int64[_l=$_l, _m=$_m, _h=$_h]";
855 } 788 }
856 789
857 /**
858 * Constructs an [Int64] with a given bitwise representation. No validation
859 * is performed.
860 */
861 Int64._bits(int this._l, int this._m, int this._h);
862 790
863 /** 791 static Int64 _masked(int a0, int a1, int a2) =>
864 * Constructs an [Int64] with the same value as an existing [Int64]. 792 new Int64._bits(_MASK & a0, _MASK & a1, _MASK2 & a2);
865 */ 793
866 Int64._copy(Int64 other) 794 static Int64 _sub(int a0, int a1, int a2, int b0, int b1, int b2) {
867 : _l = other._l, 795 int diff0 = a0 - b0;
868 _m = other._m, 796 int diff1 = a1 - b1 - ((diff0 >> _BITS) & 1);
869 _h = other._h; 797 int diff2 = a2 - b2 - ((diff1 >> _BITS) & 1);
798 return _masked(diff0, diff1, diff2);
799 }
800
801 static Int64 _negate(int b0, int b1, int b2) {
802 return _sub(0, 0, 0, b0, b1, b2);
803 }
870 804
871 // Determine whether the platform supports ints greater than 2^53 805 // Determine whether the platform supports ints greater than 2^53
872 // without loss of precision. 806 // without loss of precision.
873 static bool _haveBigIntsCached = null; 807 static bool _haveBigIntsCached = null;
874 808
875 static bool get _haveBigInts { 809 static bool get _haveBigInts {
876 if (_haveBigIntsCached == null) { 810 if (_haveBigIntsCached == null) {
877 var x = 9007199254740992; 811 var x = 9007199254740992;
878 // Defeat compile-time constant folding. 812 // Defeat compile-time constant folding.
879 if (2 + 2 != 4) { 813 if (2 + 2 != 4) {
880 x = 0; 814 x = 0;
881 } 815 }
882 var y = x + 1; 816 var y = x + 1;
883 var same = y == x; 817 var same = y == x;
884 _haveBigIntsCached = !same; 818 _haveBigIntsCached = !same;
885 } 819 }
886 return _haveBigIntsCached; 820 return _haveBigIntsCached;
887 } 821 }
888 822
889 String _hexDigit(int digit) => "0123456789ABCDEF"[digit]; 823 String _hexDigit(int digit) => "0123456789ABCDEF"[digit];
890 824
891 // Implementation of '~/' and '%'.
892
893 // Note: mutates [this].
894 void _negate() {
895 int neg0 = (~_l + 1) & _MASK;
896 int neg1 = (~_m + (neg0 == 0 ? 1 : 0)) & _MASK;
897 int neg2 = (~_h + ((neg0 == 0 && neg1 == 0) ? 1 : 0)) & _MASK2;
898
899 _l = neg0;
900 _m = neg1;
901 _h = neg2;
902 }
903
904 // Note: mutates [this].
905 void _setBit(int bit) {
906 if (bit < _BITS) {
907 _l |= 0x1 << bit;
908 } else if (bit < _BITS01) {
909 _m |= 0x1 << (bit - _BITS);
910 } else {
911 _h |= 0x1 << (bit - _BITS01);
912 }
913 }
914
915 // Note: mutates [this].
916 void _toShru1() {
917 int a2 = _h;
918 int a1 = _m;
919 int a0 = _l;
920
921 _h = a2 >> 1;
922 _m = (a1 >> 1) | ((a2 & 0x1) << (_BITS - 1));
923 _l = (a0 >> 1) | ((a1 & 0x1) << (_BITS - 1));
924 }
925 825
926 // Work around dart2js bugs with negative arguments to '>>' operator. 826 // Work around dart2js bugs with negative arguments to '>>' operator.
927 static int _shiftRight(int x, int n) { 827 static int _shiftRight(int x, int n) {
928 if (x >= 0) { 828 if (x >= 0) {
929 return x >> n; 829 return x >> n;
930 } else { 830 } else {
931 int shifted = x >> n; 831 int shifted = x >> n;
932 if (shifted >= 0x80000000) { 832 if (shifted >= 0x80000000) {
933 shifted -= 4294967296; 833 shifted -= 4294967296;
934 } 834 }
935 return shifted; 835 return shifted;
936 } 836 }
937 } 837 }
938 838
939 /** 839
940 * Attempt to subtract b from a if a >= b: 840 // Implementation of '~/', '%' and 'remainder'.
941 * 841
942 * if (a >= b) { 842 static Int64 _divide(Int64 a, other, int what) {
943 * a -= b; 843 Int64 b = _promote(other);
944 * return true; 844 if (b.isZero) {
945 * } else { 845 throw new IntegerDivisionByZeroException();
946 * return false;
947 * }
948 */
949 // Note: mutates [a].
950 static bool _trialSubtract(Int64 a, Int64 b) {
951 // Early exit.
952 int sum2 = a._h - b._h;
953 if (sum2 < 0) {
954 return false;
955 } 846 }
847 if (a.isZero) return ZERO;
956 848
957 int sum0 = a._l - b._l; 849 bool aNeg = a.isNegative;
958 int sum1 = a._m - b._m + _shiftRight(sum0, _BITS); 850 bool bNeg = b.isNegative;
959 sum2 += _shiftRight(sum1, _BITS); 851 a = a.abs();
852 b = b.abs();
960 853
961 if (sum2 < 0) { 854 int a0 = a._l;
962 return false; 855 int a1 = a._m;
963 } 856 int a2 = a._h;
964 857
965 a._l = sum0 & _MASK; 858 int b0 = b._l;
966 a._m = sum1 & _MASK; 859 int b1 = b._m;
967 a._h = sum2 & _MASK2; 860 int b2 = b._h;
968 861 return _divideHelper(a0, a1, a2, aNeg, b0, b1, b2, bNeg, what);
969 return true;
970 } 862 }
971 863
972 // Note: mutates [a] via _trialSubtract. 864 static const _RETURN_DIV = 1;
973 static Int64 _divModHelper(Int64 a, Int64 b, 865 static const _RETURN_REM = 2;
974 bool negative, bool aIsNegative, bool aIsMinValue, 866 static const _RETURN_MOD = 3;
975 bool computeRemainder) {
976 // Align the leading one bits of a and b by shifting b left.
977 int shift = b.numberOfLeadingZeros() - a.numberOfLeadingZeros();
978 Int64 bshift = b << shift;
979 867
980 // Quotient must be a new instance since we mutate it. 868 static _divideHelper(
981 Int64 quotient = new Int64(); 869 // up to 64 bits unsigned in a2/a1/a0 and b2/b1/b0
982 while (shift >= 0) { 870 int a0, int a1, int a2, bool aNeg, // input A.
983 bool gte = _trialSubtract(a, bshift); 871 int b0, int b1, int b2, bool bNeg, // input B.
984 if (gte) { 872 int what) {
985 quotient._setBit(shift); 873 int q0 = 0, q1 = 0, q2 = 0; // result Q.
986 if (a.isZero) { 874 int r0 = 0, r1 = 0, r2 = 0; // result R.
987 break;
988 }
989 }
990 875
991 bshift._toShru1(); 876 if (b2 == 0 && b1 == 0 && b0 < (1 << (30 - _BITS))) {
992 shift--; 877 // Small divisor can be handled by single-digit division within Smi range.
993 } 878 //
879 // Handling small divisors here helps the estimate version below by
880 // handling cases where the estimate is off by more than a small amount.
994 881
995 if (negative) { 882 q2 = a2 ~/ b0;
996 quotient._negate(); 883 int carry = a2 - q2 * b0;
997 } 884 int d1 = a1 + (carry << _BITS);
885 q1 = d1 ~/ b0;
886 carry = d1 - q1 * b0;
887 int d0 = a0 + (carry << _BITS);
888 q0 = d0 ~/ b0;
889 r0 = d0 - q0 * b0;
890 } else {
891 // Approximate Q = A ~/ B and R = A - Q * B using doubles.
998 892
999 if (computeRemainder) { 893 // The floating point approximation is very close to the correct value
1000 if (aIsNegative) { 894 // when floor(A/B) fits in fewer that 53 bits.
1001 _remainder = -a; 895
1002 if (aIsMinValue) { 896 // We use double arithmetic for intermediate values. Double arithmetic on
1003 _remainder = _remainder - ONE; 897 // non-negative values is exact under the following conditions:
1004 } 898 //
1005 } else { 899 // - The values are integer values that fit in 53 bits.
1006 _remainder = a; 900 // - Dividing by powers of two (adjusts exponent only).
901 // - Floor (zeroes bits with fractional weight).
902
903 const double K2 = 17592186044416.0; // 2^44
904 const double K1 = 4194304.0; // 2^22
905
906 // Approximate double values for [a] and [b].
907 double ad = a0 + K1 * a1 + K2 * a2;
908 double bd = b0 + K1 * b1 + K2 * b2;
909 // Approximate quotient.
910 double qd = (ad / bd).floorToDouble();
911
912 // Extract components of [qd] using double arithmetic.
913 double q2d = (qd / K2).floorToDouble();
914 qd = qd - K2 * q2d;
915 double q1d = (qd / K1).floorToDouble();
916 double q0d = qd - K1 * q1d;
917 q2 = q2d.toInt();
918 q1 = q1d.toInt();
919 q0 = q0d.toInt();
920
921 assert(q0 + K1 * q1 + K2 * q2 == (ad / bd).floorToDouble());
922 assert(q2 == 0 || b2 == 0); // Q and B can't both be big since Q*B <= A.
923
924 // P = Q * B, using doubles to hold intermediates.
925 // We don't need all partial sums since Q*B <= A.
926 double p0d = q0d * b0;
927 double p0carry = (p0d / K1).floorToDouble();
928 p0d = p0d - p0carry * K1;
929 double p1d = q1d * b0 + q0d * b1 + p0carry;
930 double p1carry = (p1d / K1).floorToDouble();
931 p1d = p1d - p1carry * K1;
932 double p2d = q2d * b0 + q1d * b1 + q0d * b2 + p1carry;
933 assert(p2d <= _MASK2); // No partial sum overflow.
934
935 // R = A - P
936 int diff0 = a0 - p0d.toInt();
937 int diff1 = a1 - p1d.toInt() - ((diff0 >> _BITS) & 1);
938 int diff2 = a2 - p2d.toInt() - ((diff1 >> _BITS) & 1);
939 r0 = _MASK & diff0;
940 r1 = _MASK & diff1;
941 r2 = _MASK2 & diff2;
942
943 // while (R < 0 || R >= B)
944 // adjust R towards [0, B)
945 while (
946 r2 >= _SIGN_BIT_MASK ||
947 r2 > b2 ||
948 (r2 == b2 && (r1 > b1 || (r1 == b1 && r0 >= b0)))) {
949 // Direction multiplier for adjustment.
950 int m = (r2 & _SIGN_BIT_MASK) == 0 ? 1 : -1;
951 // R = R - B or R = R + B
952 int d0 = r0 - m * b0;
953 int d1 = r1 - m * (b1 + ((d0 >> _BITS) & 1));
954 int d2 = r2 - m * (b2 + ((d1 >> _BITS) & 1));
955 r0 = _MASK & d0;
956 r1 = _MASK & d1;
957 r2 = _MASK2 & d2;
958
959 // Q = Q + 1 or Q = Q - 1
960 d0 = q0 + m;
961 d1 = q1 + m * ((d0 >> _BITS) & 1);
962 d2 = q2 + m * ((d1 >> _BITS) & 1);
963 q0 = _MASK & d0;
964 q1 = _MASK & d1;
965 q2 = _MASK2 & d2;
1007 } 966 }
1008 } 967 }
1009 968
1010 return quotient; 969 // 0 <= R < B
1011 } 970 assert(Int64.ZERO <= new Int64._bits(r0, r1, r2));
971 assert(r2 < b2 || // Handles case where B = -(MIN_VALUE)
972 new Int64._bits(r0, r1, r2) < new Int64._bits(b0, b1, b2));
1012 973
1013 Int64 _divModByMinValue(bool computeRemainder) { 974 assert(what == _RETURN_DIV || what == _RETURN_MOD || what == _RETURN_REM);
1014 // MIN_VALUE / MIN_VALUE == 1, remainder = 0 975 if (what == _RETURN_DIV) {
1015 // (x != MIN_VALUE) / MIN_VALUE == 0, remainder == x 976 if (aNeg != bNeg) return _negate(q0, q1, q2);
1016 if (isMinValue) { 977 return new Int64._bits(q0, q1, q2);
1017 if (computeRemainder) {
1018 _remainder = ZERO;
1019 }
1020 return ONE;
1021 }
1022 if (computeRemainder) {
1023 _remainder = this;
1024 }
1025 return ZERO;
1026 }
1027
1028 /**
1029 * this &= ((1L << bits) - 1)
1030 */
1031 // Note: mutates [this].
1032 Int64 _maskRight(int bits) {
1033 int b0, b1, b2;
1034 if (bits <= _BITS) {
1035 b0 = _l & ((1 << bits) - 1);
1036 b1 = b2 = 0;
1037 } else if (bits <= _BITS01) {
1038 b0 = _l;
1039 b1 = _m & ((1 << (bits - _BITS)) - 1);
1040 b2 = 0;
1041 } else {
1042 b0 = _l;
1043 b1 = _m;
1044 b2 = _h & ((1 << (bits - _BITS01)) - 1);
1045 } 978 }
1046 979
1047 _l = b0; 980 if (!aNeg) return new Int64._bits(r0, r1, r2);
1048 _m = b1;
1049 _h = b2;
1050 }
1051 981
1052 static Int64 _divModByShift(Int64 a, int bpower, bool negative, bool aIsCopy, 982 if (what == _RETURN_MOD) {
1053 bool aIsNegative, bool computeRemainder) { 983 if (r0 == 0 && r1 == 0 && r2 == 0) {
1054 Int64 c = a >> bpower; 984 return ZERO;
1055 if (negative) { 985 } else {
1056 c._negate(); 986 return _sub(b0, b1, b2, r0, r1, r2);
987 }
988 } else {
989 return _negate(r0, r1, r2);
1057 } 990 }
1058
1059 if (computeRemainder) {
1060 if (!aIsCopy) {
1061 a = new Int64._copy(a);
1062 }
1063 a._maskRight(bpower);
1064 if (aIsNegative) {
1065 a._negate();
1066 }
1067 _remainder = a;
1068 }
1069 return c;
1070 }
1071
1072 /**
1073 * Return the exact log base 2 of this, or -1 if this is not a power of two.
1074 */
1075 int _powerOfTwo() {
1076 // Power of two or 0.
1077 int l = _l;
1078 if ((l & (l - 1)) != 0) {
1079 return -1;
1080 }
1081 int m = _m;
1082 if ((m & (m - 1)) != 0) {
1083 return -1;
1084 }
1085 int h = _h;
1086 if ((h & (h - 1)) != 0) {
1087 return -1;
1088 }
1089 if (h == 0 && m == 0 && l == 0) {
1090 return -1;
1091 }
1092 if (h == 0 && m == 0 && l != 0) {
1093 return Int32._numberOfTrailingZeros(l);
1094 }
1095 if (h == 0 && m != 0 && l == 0) {
1096 return Int32._numberOfTrailingZeros(m) + _BITS;
1097 }
1098 if (h != 0 && m == 0 && l == 0) {
1099 return Int32._numberOfTrailingZeros(h) + _BITS01;
1100 }
1101
1102 return -1;
1103 }
1104
1105 static Int64 _divMod(Int64 a, Int64 b, bool computeRemainder) {
1106 if (b.isZero) {
1107 throw new IntegerDivisionByZeroException();
1108 }
1109 if (a.isZero) {
1110 if (computeRemainder) {
1111 _remainder = ZERO;
1112 }
1113 return ZERO;
1114 }
1115 // MIN_VALUE / MIN_VALUE = 1, anything other a / MIN_VALUE is 0.
1116 if (b.isMinValue) {
1117 return a._divModByMinValue(computeRemainder);
1118 }
1119 // Normalize b to abs(b), keeping track of the parity in 'negative'.
1120 // We can do this because we have already ensured that b != MIN_VALUE.
1121 bool negative = false;
1122 if (b.isNegative) {
1123 b = -b;
1124 negative = !negative;
1125 }
1126 // If b == 2^n, bpower will be n, otherwise it will be -1.
1127 int bpower = b._powerOfTwo();
1128
1129 // True if the original value of a is negative.
1130 bool aIsNegative = false;
1131 // True if the original value of a is Int64.MIN_VALUE.
1132 bool aIsMinValue = false;
1133
1134 /*
1135 * Normalize a to a positive value, keeping track of the sign change in
1136 * 'negative' (which tracks the sign of both a and b and is used to
1137 * determine the sign of the quotient) and 'aIsNegative' (which is used to
1138 * determine the sign of the remainder).
1139 *
1140 * For all values of a except MIN_VALUE, we can just negate a and modify
1141 * negative and aIsNegative appropriately. When a == MIN_VALUE, negation is
1142 * not possible without overflowing 64 bits, so instead of computing
1143 * abs(MIN_VALUE) / abs(b) we compute (abs(MIN_VALUE) - 1) / abs(b). The
1144 * only circumstance under which these quotients differ is when b is a power
1145 * of two, which will divide abs(MIN_VALUE) == 2^64 exactly. In this case,
1146 * we can get the proper result by shifting MIN_VALUE in unsigned fashion.
1147 *
1148 * We make a single copy of a before the first operation that needs to
1149 * modify its value.
1150 */
1151 bool aIsCopy = false;
1152 if (a.isMinValue) {
1153 aIsMinValue = true;
1154 aIsNegative = true;
1155 // If b is not a power of two, treat -a as MAX_VALUE (instead of the
1156 // actual value (MAX_VALUE + 1)).
1157 if (bpower == -1) {
1158 a = new Int64._copy(MAX_VALUE);
1159 aIsCopy = true;
1160 negative = !negative;
1161 } else {
1162 // Signed shift of MIN_VALUE produces the right answer.
1163 Int64 c = a >> bpower;
1164 if (negative) {
1165 c._negate();
1166 }
1167 if (computeRemainder) {
1168 _remainder = ZERO;
1169 }
1170 return c;
1171 }
1172 } else if (a.isNegative) {
1173 aIsNegative = true;
1174 a = -a;
1175 aIsCopy = true;
1176 negative = !negative;
1177 }
1178
1179 // Now both a and b are non-negative.
1180 // If b is a power of two, just shift.
1181 if (bpower != -1) {
1182 return _divModByShift(a, bpower, negative, aIsCopy, aIsNegative,
1183 computeRemainder);
1184 }
1185
1186 // If a < b, the quotient is 0 and the remainder is a.
1187 if (a < b) {
1188 if (computeRemainder) {
1189 if (aIsNegative) {
1190 _remainder = -a;
1191 } else {
1192 _remainder = aIsCopy ? a : new Int64._copy(a);
1193 }
1194 }
1195 return ZERO;
1196 }
1197
1198 // Generate the quotient using bit-at-a-time long division.
1199 return _divModHelper(aIsCopy ? a : new Int64._copy(a), b, negative,
1200 aIsNegative, aIsMinValue, computeRemainder);
1201 } 991 }
1202 } 992 }
OLDNEW
« no previous file with comments | « no previous file | pkg/fixnum/test/int_64_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698