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

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

Issue 23441004: More efficient Int64 parsing and printing. (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 | « pkg/fixnum/lib/src/int32.dart ('k') | 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 int _l, _m, _h;
18 18
19 // Note: instances of [Int64] are immutable outside of this library, 19 // Note: instances of [Int64] are immutable outside of this library,
20 // therefore we may return a reference to an existing instance. 20 // therefore we may return a reference to an existing instance.
21 // We take care to perform mutation only on internally-generated 21 // We take care to perform mutation only on internally-generated
22 // instances before they are exposed to external code. 22 // instances before they are exposed to external code.
23 23
24 // Note: several functions require _BITS == 22 -- do not change this value. 24 // Note: several functions require _BITS == 22 -- do not change this value.
25 static const int _BITS = 22; 25 static const int _BITS = 22;
26 static const int _BITS01 = 44; // 2 * _BITS 26 static const int _BITS01 = 44; // 2 * _BITS
27 static const int _BITS2 = 20; // 64 - _BITS01 27 static const int _BITS2 = 20; // 64 - _BITS01
28 static const int _MASK = 4194303; // (1 << _BITS) - 1 28 static const int _MASK = 4194303; // (1 << _BITS) - 1
29 static const int _MASK_2 = 1048575; // (1 << _BITS2) - 1 29 static const int _MASK2 = 1048575; // (1 << _BITS2) - 1
30 static const int _SIGN_BIT = 19; // _BITS2 - 1 30 static const int _SIGN_BIT = 19; // _BITS2 - 1
31 static const int _SIGN_BIT_VALUE = 524288; // 1 << _SIGN_BIT 31 static const int _SIGN_BIT_MASK = 524288; // 1 << _SIGN_BIT
32 32
33 // Cached constants 33 // Cached constants
34 static Int64 _MAX_VALUE; 34 static Int64 _MAX_VALUE;
35 static Int64 _MIN_VALUE; 35 static Int64 _MIN_VALUE;
36 static Int64 _ZERO; 36 static Int64 _ZERO;
37 static Int64 _ONE; 37 static Int64 _ONE;
38 static Int64 _TWO; 38 static Int64 _TWO;
39 39
40 // Precompute the radix strings for MIN_VALUE to avoid the problem
41 // of overflow of -MIN_VALUE.
42 static List<String> _minValues = const <String>[
43 null, null,
44 "-1000000000000000000000000000000000000000000000000000000000000000", // 2
45 "-2021110011022210012102010021220101220222", // base 3
46 "-20000000000000000000000000000000", // base 4
47 "-1104332401304422434310311213", // base 5
48 "-1540241003031030222122212", // base 6
49 "-22341010611245052052301", // base 7
50 "-1000000000000000000000", // base 8
51 "-67404283172107811828", // base 9
52 "-9223372036854775808", // base 10
53 "-1728002635214590698", // base 11
54 "-41A792678515120368", // base 12
55 "-10B269549075433C38", // base 13
56 "-4340724C6C71DC7A8", // base 14
57 "-160E2AD3246366808", // base 15
58 "-8000000000000000" // base 16
59 ];
60
61 // The remainder of the last divide operation. 40 // The remainder of the last divide operation.
62 static Int64 _remainder; 41 static Int64 _remainder;
63 42
64 /** 43 /**
65 * The maximum positive value attainable by an [Int64], namely 44 * The maximum positive value attainable by an [Int64], namely
66 * 9,223,372,036,854,775,807. 45 * 9,223,372,036,854,775,807.
67 */ 46 */
68 static Int64 get MAX_VALUE { 47 static Int64 get MAX_VALUE {
69 if (_MAX_VALUE == null) { 48 if (_MAX_VALUE == null) {
70 _MAX_VALUE = new Int64._bits(_MASK, _MASK, _MASK_2 >> 1); 49 _MAX_VALUE = new Int64._bits(_MASK, _MASK, _MASK2 >> 1);
71 } 50 }
72 return _MAX_VALUE; 51 return _MAX_VALUE;
73 } 52 }
74 53
75 /** 54 /**
76 * The minimum positive value attainable by an [Int64], namely 55 * The minimum positive value attainable by an [Int64], namely
77 * -9,223,372,036,854,775,808. 56 * -9,223,372,036,854,775,808.
78 */ 57 */
79 static Int64 get MIN_VALUE { 58 static Int64 get MIN_VALUE {
80 if (_MIN_VALUE == null) { 59 if (_MIN_VALUE == null) {
81 _MIN_VALUE = new Int64._bits(0, 0, _SIGN_BIT_VALUE); 60 _MIN_VALUE = new Int64._bits(0, 0, _SIGN_BIT_MASK);
82 } 61 }
83 return _MIN_VALUE; 62 return _MIN_VALUE;
84 } 63 }
85 64
86 /** 65 /**
87 * An [Int64] constant equal to 0. 66 * An [Int64] constant equal to 0.
88 */ 67 */
89 static Int64 get ZERO { 68 static Int64 get ZERO {
90 if (_ZERO == null) { 69 if (_ZERO == null) {
91 _ZERO = new Int64(); 70 _ZERO = new Int64();
(...skipping 15 matching lines...) Expand all
107 * An [Int64] constant equal to 2. 86 * An [Int64] constant equal to 2.
108 */ 87 */
109 static Int64 get TWO { 88 static Int64 get TWO {
110 if (_TWO == null) { 89 if (_TWO == null) {
111 _TWO = new Int64._bits(2, 0, 0); 90 _TWO = new Int64._bits(2, 0, 0);
112 } 91 }
113 return _TWO; 92 return _TWO;
114 } 93 }
115 94
116 /** 95 /**
117 * Parses a [String] in a given [radix] between 2 and 16 and returns an 96 * Parses a [String] in a given [radix] between 2 and 36 and returns an
118 * [Int64]. 97 * [Int64].
119 */ 98 */
120 // TODO(rice) - make this faster by converting several digits at once.
121 static Int64 parseRadix(String s, int radix) { 99 static Int64 parseRadix(String s, int radix) {
122 if ((radix <= 1) || (radix > 16)) { 100 if ((radix <= 1) || (radix > 36)) {
123 throw new ArgumentError("Bad radix: $radix"); 101 throw new ArgumentError("Bad radix: $radix");
124 } 102 }
125 Int64 x = ZERO; 103 return _parseRadix(s, radix);
104 }
105
106 static Int64 _parseRadix(String s, int radix) {
126 int i = 0; 107 int i = 0;
127 bool negative = false; 108 bool negative = false;
128 if (s[0] == '-') { 109 if (s[0] == '-') {
129 negative = true; 110 negative = true;
130 i++; 111 i++;
131 } 112 }
113 int d0 = 0, d1 = 0, d2 = 0; // low, middle, high components.
132 for (; i < s.length; i++) { 114 for (; i < s.length; i++) {
133 int c = s.codeUnitAt(i); 115 int c = s.codeUnitAt(i);
134 int digit = Int32._decodeHex(c); 116 int digit = Int32._decodeDigit(c);
135 if (digit < 0 || digit >= radix) { 117 if (digit < 0 || digit >= radix) {
136 throw new Exception("Non-radix char code: $c"); 118 throw new Exception("Non-radix char code: $c");
137 } 119 }
138 x = (x * radix) + digit; 120
121 // [radix] and [digit] are at most 6 bits, component is 22, so we can
122 // multiply and add within 30 bit temporary values.
123 d0 = d0 * radix + digit;
124 int carry = d0 >> _BITS;
125 d0 &= _MASK;
126
127 d1 = d1 * radix + carry;
128 carry = d1 >> _BITS;
129 d1 &= _MASK;
130
131 d2 = d2 * radix + carry;
132 d2 &= _MASK2;
139 } 133 }
140 return negative ? -x : x; 134
135 if (negative) {
136 d0 = 0 - d0;
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);
141 } 146 }
142 147
143 /** 148 /**
144 * Parses a decimal [String] and returns an [Int64]. 149 * Parses a decimal [String] and returns an [Int64].
145 */ 150 */
146 static Int64 parseInt(String s) => parseRadix(s, 10); 151 static Int64 parseInt(String s) => _parseRadix(s, 10);
147 152
148 /** 153 /**
149 * Parses a hexadecimal [String] and returns an [Int64]. 154 * Parses a hexadecimal [String] and returns an [Int64].
150 */ 155 */
151 static Int64 parseHex(String s) => parseRadix(s, 16); 156 static Int64 parseHex(String s) => _parseRadix(s, 16);
152 157
153 // 158 //
154 // Public constructors 159 // Public constructors
155 // 160 //
156 161
157 /** 162 /**
158 * Constructs an [Int64] equal to 0. 163 * Constructs an [Int64] equal to 0.
159 */ 164 */
160 Int64() : _l = 0, _m = 0, _h = 0; 165 Int64() : _l = 0, _m = 0, _h = 0;
161 166
162 /** 167 /**
163 * Constructs an [Int64] with a given [int] value. 168 * Constructs an [Int64] with a given [int] value.
164 */ 169 */
165 Int64.fromInt(int value) { 170 Int64.fromInt(int value) {
166 bool negative = false; 171 bool negative = false;
167 if (value < 0) { 172 if (value < 0) {
168 negative = true; 173 negative = true;
169 value = -value - 1; 174 value = -value - 1;
170 } 175 }
171 if (_haveBigInts) { 176 if (_haveBigInts) {
172 _l = value & _MASK; 177 _l = value & _MASK;
173 _m = (value >> _BITS) & _MASK; 178 _m = (value >> _BITS) & _MASK;
174 _h = (value >> _BITS01) & _MASK_2; 179 _h = (value >> _BITS01) & _MASK2;
175 } else { 180 } else {
176 // Avoid using bitwise operations that coerce their input to 32 bits. 181 // Avoid using bitwise operations that coerce their input to 32 bits.
177 _h = value ~/ 17592186044416; // 2^44 182 _h = value ~/ 17592186044416; // 2^44
178 value -= _h * 17592186044416; 183 value -= _h * 17592186044416;
179 _m = value ~/ 4194304; // 2^22 184 _m = value ~/ 4194304; // 2^22
180 value -= _m * 4194304; 185 value -= _m * 4194304;
181 _l = value; 186 _l = value;
182 } 187 }
183 188
184 if (negative) { 189 if (negative) {
185 _l = ~_l & _MASK; 190 _l = ~_l & _MASK;
186 _m = ~_m & _MASK; 191 _m = ~_m & _MASK;
187 _h = ~_h & _MASK_2; 192 _h = ~_h & _MASK2;
188 } 193 }
189 } 194 }
190 195
191 factory Int64.fromBytes(List<int> bytes) { 196 factory Int64.fromBytes(List<int> bytes) {
192 int top = bytes[7] & 0xff; 197 int top = bytes[7] & 0xff;
193 top <<= 8; 198 top <<= 8;
194 top |= bytes[6] & 0xff; 199 top |= bytes[6] & 0xff;
195 top <<= 8; 200 top <<= 8;
196 top |= bytes[5] & 0xff; 201 top |= bytes[5] & 0xff;
197 top <<= 8; 202 top <<= 8;
(...skipping 27 matching lines...) Expand all
225 bottom <<= 8; 230 bottom <<= 8;
226 bottom |= bytes[7] & 0xff; 231 bottom |= bytes[7] & 0xff;
227 232
228 return new Int64.fromInts(top, bottom); 233 return new Int64.fromInts(top, bottom);
229 } 234 }
230 235
231 /** 236 /**
232 * Constructs an [Int64] from a pair of 32-bit integers having the value 237 * Constructs an [Int64] from a pair of 32-bit integers having the value
233 * [:((top & 0xffffffff) << 32) | (bottom & 0xffffffff):]. 238 * [:((top & 0xffffffff) << 32) | (bottom & 0xffffffff):].
234 */ 239 */
235 Int64.fromInts(int top, int bottom) { 240 factory Int64.fromInts(int top, int bottom) {
236 top &= 0xffffffff; 241 top &= 0xffffffff;
237 bottom &= 0xffffffff; 242 bottom &= 0xffffffff;
238 _l = bottom & _MASK; 243 int d0 = bottom & _MASK;
239 _m = ((top & 0xfff) << 10) | ((bottom >> _BITS) & 0x3ff); 244 int d1 = ((top & 0xfff) << 10) | ((bottom >> _BITS) & 0x3ff);
240 _h = (top >> 12) & _MASK_2; 245 int d2 = (top >> 12) & _MASK2;
246 return new Int64._bits(d0, d1, d2);
241 } 247 }
242 248
243 // Returns the [Int64] representation of the specified value. Throws 249 // Returns the [Int64] representation of the specified value. Throws
244 // [ArgumentError] for non-integer arguments. 250 // [ArgumentError] for non-integer arguments.
245 Int64 _promote(val) { 251 Int64 _promote(val) {
246 if (val is Int64) { 252 if (val is Int64) {
247 return val; 253 return val;
248 } else if (val is int) { 254 } else if (val is int) {
249 return new Int64.fromInt(val); 255 return new Int64.fromInt(val);
250 } else if (val is Int32) { 256 } else if (val is Int32) {
251 return val.toInt64(); 257 return val.toInt64();
252 } 258 }
253 throw new ArgumentError(val); 259 throw new ArgumentError(val);
254 } 260 }
255 261
256 Int64 operator +(other) { 262 Int64 operator +(other) {
257 Int64 o = _promote(other); 263 Int64 o = _promote(other);
258 int sum0 = _l + o._l; 264 int sum0 = _l + o._l;
259 int sum1 = _m + o._m + _shiftRight(sum0, _BITS); 265 int sum1 = _m + o._m + _shiftRight(sum0, _BITS);
260 int sum2 = _h + o._h + _shiftRight(sum1, _BITS); 266 int sum2 = _h + o._h + _shiftRight(sum1, _BITS);
261 267
262 Int64 result = new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK_2); 268 Int64 result = new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK2);
263 return result; 269 return result;
264 } 270 }
265 271
266 Int64 operator -(other) { 272 Int64 operator -(other) {
267 Int64 o = _promote(other); 273 Int64 o = _promote(other);
268 int sum0 = _l - o._l; 274 int sum0 = _l - o._l;
269 int sum1 = _m - o._m + _shiftRight(sum0, _BITS); 275 int sum1 = _m - o._m + _shiftRight(sum0, _BITS);
270 int sum2 = _h - o._h + _shiftRight(sum1, _BITS); 276 int sum2 = _h - o._h + _shiftRight(sum1, _BITS);
271 277
272 Int64 result = new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK_2); 278 Int64 result = new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK2);
273 return result; 279 return result;
274 } 280 }
275 281
276 Int64 operator -() { 282 Int64 operator -() {
277 // Like 0 - this. 283 // Like 0 - this.
278 int sum0 = -_l; 284 int sum0 = -_l;
279 int sum1 = -_m + _shiftRight(sum0, _BITS); 285 int sum1 = -_m + _shiftRight(sum0, _BITS);
280 int sum2 = -_h + _shiftRight(sum1, _BITS); 286 int sum2 = -_h + _shiftRight(sum1, _BITS);
281 287
282 return new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK_2); 288 return new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK2);
283 } 289 }
284 290
285 Int64 operator *(other) { 291 Int64 operator *(other) {
286 Int64 o = _promote(other); 292 Int64 o = _promote(other);
287 293
288 // Grab 13-bit chunks. 294 // Grab 13-bit chunks.
289 int a0 = _l & 0x1fff; 295 int a0 = _l & 0x1fff;
290 int a1 = (_l >> 13) | ((_m & 0xf) << 9); 296 int a1 = (_l >> 13) | ((_m & 0xf) << 9);
291 int a2 = (_m >> 4) & 0x1fff; 297 int a2 = (_m >> 4) & 0x1fff;
292 int a3 = (_m >> 17) | ((_h & 0xff) << 5); 298 int a3 = (_m >> 17) | ((_h & 0xff) << 5);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 int c22 = p2 >> 18; 360 int c22 = p2 >> 18;
355 int c23 = p3 >> 5; 361 int c23 = p3 >> 5;
356 int c24 = (p4 & 0xfff) << 8; 362 int c24 = (p4 & 0xfff) << 8;
357 int c2 = c22 + c23 + c24; 363 int c2 = c22 + c23 + c24;
358 364
359 // Propagate high bits from c0 -> c1, c1 -> c2. 365 // Propagate high bits from c0 -> c1, c1 -> c2.
360 c1 += c0 >> _BITS; 366 c1 += c0 >> _BITS;
361 c0 &= _MASK; 367 c0 &= _MASK;
362 c2 += c1 >> _BITS; 368 c2 += c1 >> _BITS;
363 c1 &= _MASK; 369 c1 &= _MASK;
364 c2 &= _MASK_2; 370 c2 &= _MASK2;
365 371
366 return new Int64._bits(c0, c1, c2); 372 return new Int64._bits(c0, c1, c2);
367 } 373 }
368 374
369 Int64 operator %(other) { 375 Int64 operator %(other) {
370 if (other.isZero) { 376 if (other.isZero) {
371 throw new IntegerDivisionByZeroException(); 377 throw new IntegerDivisionByZeroException();
372 } 378 }
373 if (this.isZero) { 379 if (this.isZero) {
374 return ZERO; 380 return ZERO;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 414
409 Int64 operator ^(other) { 415 Int64 operator ^(other) {
410 Int64 o = _promote(other); 416 Int64 o = _promote(other);
411 int a0 = _l ^ o._l; 417 int a0 = _l ^ o._l;
412 int a1 = _m ^ o._m; 418 int a1 = _m ^ o._m;
413 int a2 = _h ^ o._h; 419 int a2 = _h ^ o._h;
414 return new Int64._bits(a0, a1, a2); 420 return new Int64._bits(a0, a1, a2);
415 } 421 }
416 422
417 Int64 operator ~() { 423 Int64 operator ~() {
418 var result = new Int64._bits((~_l) & _MASK, (~_m) & _MASK, (~_h) & _MASK_2); 424 var result = new Int64._bits((~_l) & _MASK, (~_m) & _MASK, (~_h) & _MASK2);
419 return result; 425 return result;
420 } 426 }
421 427
422 Int64 operator <<(int n) { 428 Int64 operator <<(int n) {
423 if (n < 0) { 429 if (n < 0) {
424 throw new ArgumentError(n); 430 throw new ArgumentError(n);
425 } 431 }
426 n &= 63; 432 n &= 63;
427 433
428 int res0, res1, res2; 434 int res0, res1, res2;
429 if (n < _BITS) { 435 if (n < _BITS) {
430 res0 = _l << n; 436 res0 = _l << n;
431 res1 = (_m << n) | (_l >> (_BITS - n)); 437 res1 = (_m << n) | (_l >> (_BITS - n));
432 res2 = (_h << n) | (_m >> (_BITS - n)); 438 res2 = (_h << n) | (_m >> (_BITS - n));
433 } else if (n < _BITS01) { 439 } else if (n < _BITS01) {
434 res0 = 0; 440 res0 = 0;
435 res1 = _l << (n - _BITS); 441 res1 = _l << (n - _BITS);
436 res2 = (_m << (n - _BITS)) | (_l >> (_BITS01 - n)); 442 res2 = (_m << (n - _BITS)) | (_l >> (_BITS01 - n));
437 } else { 443 } else {
438 res0 = 0; 444 res0 = 0;
439 res1 = 0; 445 res1 = 0;
440 res2 = _l << (n - _BITS01); 446 res2 = _l << (n - _BITS01);
441 } 447 }
442 448
443 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK_2); 449 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK2);
444 } 450 }
445 451
446 Int64 operator >>(int n) { 452 Int64 operator >>(int n) {
447 if (n < 0) { 453 if (n < 0) {
448 throw new ArgumentError(n); 454 throw new ArgumentError(n);
449 } 455 }
450 n &= 63; 456 n &= 63;
451 457
452 int res0, res1, res2; 458 int res0, res1, res2;
453 459
454 // Sign extend h(a). 460 // Sign extend h(a).
455 int a2 = _h; 461 int a2 = _h;
456 bool negative = (a2 & _SIGN_BIT_VALUE) != 0; 462 bool negative = (a2 & _SIGN_BIT_MASK) != 0;
457 if (negative) { 463 if (negative) {
458 a2 += 0x3 << _BITS2; // add extra one bits on the left 464 a2 += 0x3 << _BITS2; // add extra one bits on the left
459 } 465 }
460 466
461 if (n < _BITS) { 467 if (n < _BITS) {
462 res2 = _shiftRight(a2, n); 468 res2 = _shiftRight(a2, n);
463 if (negative) { 469 if (negative) {
464 res2 |= _MASK_2 & ~(_MASK_2 >> n); 470 res2 |= _MASK2 & ~(_MASK2 >> n);
465 } 471 }
466 res1 = _shiftRight(_m, n) | (a2 << (_BITS - n)); 472 res1 = _shiftRight(_m, n) | (a2 << (_BITS - n));
467 res0 = _shiftRight(_l, n) | (_m << (_BITS - n)); 473 res0 = _shiftRight(_l, n) | (_m << (_BITS - n));
468 } else if (n < _BITS01) { 474 } else if (n < _BITS01) {
469 res2 = negative ? _MASK_2 : 0; 475 res2 = negative ? _MASK2 : 0;
470 res1 = _shiftRight(a2, n - _BITS); 476 res1 = _shiftRight(a2, n - _BITS);
471 if (negative) { 477 if (negative) {
472 res1 |= _MASK & ~(_MASK >> (n - _BITS)); 478 res1 |= _MASK & ~(_MASK >> (n - _BITS));
473 } 479 }
474 res0 = _shiftRight(_m, n - _BITS) | (a2 << (_BITS01 - n)); 480 res0 = _shiftRight(_m, n - _BITS) | (a2 << (_BITS01 - n));
475 } else { 481 } else {
476 res2 = negative ? _MASK_2 : 0; 482 res2 = negative ? _MASK2 : 0;
477 res1 = negative ? _MASK : 0; 483 res1 = negative ? _MASK : 0;
478 res0 = _shiftRight(a2, n - _BITS01); 484 res0 = _shiftRight(a2, n - _BITS01);
479 if (negative) { 485 if (negative) {
480 res0 |= _MASK & ~(_MASK >> (n - _BITS01)); 486 res0 |= _MASK & ~(_MASK >> (n - _BITS01));
481 } 487 }
482 } 488 }
483 489
484 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK_2); 490 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK2);
485 } 491 }
486 492
487 Int64 shiftRightUnsigned(int n) { 493 Int64 shiftRightUnsigned(int n) {
488 if (n < 0) { 494 if (n < 0) {
489 throw new ArgumentError(n); 495 throw new ArgumentError(n);
490 } 496 }
491 n &= 63; 497 n &= 63;
492 498
493 int res0, res1, res2; 499 int res0, res1, res2;
494 int a2 = _h & _MASK_2; // Ensure a2 is positive. 500 int a2 = _h & _MASK2; // Ensure a2 is positive.
495 if (n < _BITS) { 501 if (n < _BITS) {
496 res2 = a2 >> n; 502 res2 = a2 >> n;
497 res1 = (_m >> n) | (a2 << (_BITS - n)); 503 res1 = (_m >> n) | (a2 << (_BITS - n));
498 res0 = (_l >> n) | (_m << (_BITS - n)); 504 res0 = (_l >> n) | (_m << (_BITS - n));
499 } else if (n < _BITS01) { 505 } else if (n < _BITS01) {
500 res2 = 0; 506 res2 = 0;
501 res1 = a2 >> (n - _BITS); 507 res1 = a2 >> (n - _BITS);
502 res0 = (_m >> (n - _BITS)) | (_h << (_BITS01 - n)); 508 res0 = (_m >> (n - _BITS)) | (_h << (_BITS01 - n));
503 } else { 509 } else {
504 res2 = 0; 510 res2 = 0;
505 res1 = 0; 511 res1 = 0;
506 res0 = a2 >> (n - _BITS01); 512 res0 = a2 >> (n - _BITS01);
507 } 513 }
508 514
509 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK_2); 515 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK2);
510 } 516 }
511 517
512 /** 518 /**
513 * Returns [true] if this [Int64] has the same numeric value as the 519 * Returns [true] if this [Int64] has the same numeric value as the
514 * given object. The argument may be an [int] or an [IntX]. 520 * given object. The argument may be an [int] or an [IntX].
515 */ 521 */
516 bool operator ==(other) { 522 bool operator ==(other) {
517 Int64 o; 523 Int64 o;
518 if (other is Int64) { 524 if (other is Int64) {
519 o = other; 525 o = other;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 569
564 bool operator >(other) { 570 bool operator >(other) {
565 return this.compareTo(other) > 0; 571 return this.compareTo(other) > 0;
566 } 572 }
567 573
568 bool operator >=(other) { 574 bool operator >=(other) {
569 return this.compareTo(other) >= 0; 575 return this.compareTo(other) >= 0;
570 } 576 }
571 577
572 bool get isEven => (_l & 0x1) == 0; 578 bool get isEven => (_l & 0x1) == 0;
573 bool get isMaxValue => (_h == _MASK_2 >> 1) && _m == _MASK && _l == _MASK; 579 bool get isMaxValue => (_h == _MASK2 >> 1) && _m == _MASK && _l == _MASK;
574 bool get isMinValue => _h == _SIGN_BIT_VALUE && _m == 0 && _l == 0; 580 bool get isMinValue => _h == _SIGN_BIT_MASK && _m == 0 && _l == 0;
575 bool get isNegative => (_h >> (_BITS2 - 1)) != 0; 581 bool get isNegative => (_h >> (_BITS2 - 1)) != 0;
576 bool get isOdd => (_l & 0x1) == 1; 582 bool get isOdd => (_l & 0x1) == 1;
577 bool get isZero => _h == 0 && _m == 0 && _l == 0; 583 bool get isZero => _h == 0 && _m == 0 && _l == 0;
578 584
579 /** 585 /**
580 * Returns a hash code based on all the bits of this [Int64]. 586 * Returns a hash code based on all the bits of this [Int64].
581 */ 587 */
582 int get hashCode { 588 int get hashCode {
583 int bottom = ((_m & 0x3ff) << _BITS) | _l; 589 int bottom = ((_m & 0x3ff) << _BITS) | _l;
584 int top = (_h << 12) | ((_m >> 10) & 0xfff); 590 int top = (_h << 12) | ((_m >> 10) & 0xfff);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 result[6] = (_h >> 4) & 0xff; 647 result[6] = (_h >> 4) & 0xff;
642 result[7] = (_h >> 12) & 0xff; 648 result[7] = (_h >> 12) & 0xff;
643 return result; 649 return result;
644 } 650 }
645 651
646 int toInt() { 652 int toInt() {
647 int l = _l; 653 int l = _l;
648 int m = _m; 654 int m = _m;
649 int h = _h; 655 int h = _h;
650 bool negative = false; 656 bool negative = false;
651 if ((_h & _SIGN_BIT_VALUE) != 0) { 657 if ((_h & _SIGN_BIT_MASK) != 0) {
652 l = ~_l & _MASK; 658 l = ~_l & _MASK;
653 m = ~_m & _MASK; 659 m = ~_m & _MASK;
654 h = ~_h & _MASK_2; 660 h = ~_h & _MASK2;
655 negative = true; 661 negative = true;
656 } 662 }
657 663
658 int result; 664 int result;
659 if (_haveBigInts) { 665 if (_haveBigInts) {
660 result = (h << _BITS01) | (m << _BITS) | l; 666 result = (h << _BITS01) | (m << _BITS) | l;
661 } else { 667 } else {
662 result = (h * 17592186044416) + (m * 4194304) + l; 668 result = (h * 17592186044416) + (m * 4194304) + l;
663 } 669 }
664 return negative ? -result - 1 : result; 670 return negative ? -result - 1 : result;
665 } 671 }
666 672
667 /** 673 /**
668 * Returns an [Int32] containing the low 32 bits of this [Int64]. 674 * Returns an [Int32] containing the low 32 bits of this [Int64].
669 */ 675 */
670 Int32 toInt32() { 676 Int32 toInt32() {
671 return new Int32.fromInt(((_m & 0x3ff) << _BITS) | _l); 677 return new Int32.fromInt(((_m & 0x3ff) << _BITS) | _l);
672 } 678 }
673 679
674 /** 680 /**
675 * Returns [this]. 681 * Returns [this].
676 */ 682 */
677 Int64 toInt64() => this; 683 Int64 toInt64() => this;
678 684
679 /** 685 /**
680 * Returns the value of this [Int64] as a decimal [String]. 686 * Returns the value of this [Int64] as a decimal [String].
681 */ 687 */
682 // TODO(rice) - Make this faster by converting several digits at once. 688 String toString() => _toRadixString(10);
683 String toString() {
684 Int64 a = this;
685 if (a.isZero) {
686 return "0";
687 }
688 if (a.isMinValue) {
689 return "-9223372036854775808";
690 }
691
692 String result = "";
693 bool negative = false;
694 if (a.isNegative) {
695 negative = true;
696 a = -a;
697 }
698
699 Int64 ten = new Int64._bits(10, 0, 0);
700 while (!a.isZero) {
701 a = _divMod(a, ten, true);
702 result = "${_remainder._l}$result";
703 }
704 if (negative) {
705 result = "-$result";
706 }
707 return result;
708 }
709 689
710 // TODO(rice) - Make this faster by avoiding arithmetic. 690 // TODO(rice) - Make this faster by avoiding arithmetic.
711 String toHexString() { 691 String toHexString() {
712 Int64 x = new Int64._copy(this); 692 Int64 x = new Int64._copy(this);
713 if (isZero) { 693 if (isZero) {
714 return "0"; 694 return "0";
715 } 695 }
716 String hexStr = ""; 696 String hexStr = "";
717 Int64 digit_f = new Int64.fromInt(0xf); 697 Int64 digit_f = new Int64.fromInt(0xf);
718 while (!x.isZero) { 698 while (!x.isZero) {
719 int digit = x._l & 0xf; 699 int digit = x._l & 0xf;
720 hexStr = "${_hexDigit(digit)}$hexStr"; 700 hexStr = "${_hexDigit(digit)}$hexStr";
721 x = x.shiftRightUnsigned(4); 701 x = x.shiftRightUnsigned(4);
722 } 702 }
723 return hexStr; 703 return hexStr;
724 } 704 }
725 705
726 String toRadixString(int radix) { 706 String toRadixString(int radix) {
727 if ((radix <= 1) || (radix > 16)) { 707 if ((radix <= 1) || (radix > 36)) {
728 throw new ArgumentError("Bad radix: $radix"); 708 throw new ArgumentError("Bad radix: $radix");
729 } 709 }
730 Int64 a = this; 710 return _toRadixString(radix);
731 if (a.isZero) { 711 }
732 return "0"; 712
733 } 713 String _toRadixString(int radix) {
734 if (a.isMinValue) { 714 int d0 = _l;
735 return _minValues[radix]; 715 int d1 = _m;
716 int d2 = _h;
717
718 if (d0 == 0 && d1 == 0 && d2 == 0) return '0';
719
720 String sign = '';
721 if ((d2 & _SIGN_BIT_MASK) != 0) {
722 sign = '-';
723
724 // Negate in-place.
725 d0 = 0 - d0;
726 int borrow = (d0 >> _BITS) & 1;
727 d0 &= _MASK;
728 d1 = 0 - d1 - borrow;
729 borrow = (d1 >> _BITS) & 1;
730 d1 &= _MASK;
731 d2 = 0 - d2 - borrow;
732 d2 &= _MASK2;
733 // d2, d1, d0 now are an unsigned 64 bit integer for MIN_VALUE and an
734 // unsigned 63 bit integer for other values.
736 } 735 }
737 736
738 String result = ""; 737 // Rearrange components into five components where all but the most
739 bool negative = false; 738 // significant are 10 bits wide.
740 if (a.isNegative) { 739 //
741 negative = true; 740 // d4, d3, d4, d1, d0: 24 + 10 + 10 + 10 + 10 bits
742 a = -a; 741 //
742 // The choice of 10 bits allows a remainder of 20 bits to be scaled by 10
743 // bits and added during division while keeping all intermediate values
744 // within 30 bits (unsigned small integer range for 32 bit implementations
745 // of Dart VM and V8).
746 //
747 // 6 6 5 4 3 2 1
748 // 3210987654321098765432109876543210987654321098765432109876543210
749 // [--------d2--------][---------d1---------][---------d0---------]
750 // -->
751 // [----------d4----------][---d3---][---d2---][---d1---][---d0---]
752
753
754 int d4 = (d2 << 4) | (d1 >> 18);
755 int d3 = (d1 >> 8) & 0x3ff;
756 d2 = ((d1 << 2) | (d0 >> 20)) & 0x3ff;
757 d1 = (d0 >> 10) & 0x3ff;
758 d0 = d0 & 0x3ff;
759
760 int fatRadix = _fatRadixTable[radix];
761
762 // Generate chunks of digits. In radix 10, generate 6 digits per chunk.
763 //
764 // This loop generates at most 3 chunks, so we store the chunks in locals
765 // rather than a list. We are trying to generate digits 20 bits at a time
766 // until we have only 30 bits left. 20 + 20 + 30 > 64 would imply that we
767 // need only two chunks, but radix values 17-19 and 33-36 generate only 15
768 // or 16 bits per iteration, so sometimes the third chunk is needed.
769
770 String chunk1 = "", chunk2 = "", chunk3 = "";
771
772 while (!(d4 == 0 && d3 == 0)) {
773 int q = d4 ~/ fatRadix;
774 int r = d4 - q * fatRadix;
775 d4 = q;
776 d3 += r << 10;
777
778 q = d3 ~/ fatRadix;
779 r = d3 - q * fatRadix;
780 d3 = q;
781 d2 += r << 10;
782
783 q = d2 ~/ fatRadix;
784 r = d2 - q * fatRadix;
785 d2 = q;
786 d1 += r << 10;
787
788 q = d1 ~/ fatRadix;
789 r = d1 - q * fatRadix;
790 d1 = q;
791 d0 += r << 10;
792
793 q = d0 ~/ fatRadix;
794 r = d0 - q * fatRadix;
795 d0 = q;
796
797 assert(chunk2 == "");
798 chunk3 = chunk2;
799 chunk2 = chunk1;
800 // Adding [fatRadix] Forces an extra digit which we discard to get a fixed
801 // width. E.g. (1000000 + 123) -> "1000123" -> "000123". An alternative
802 // would be to pad to the left with zeroes.
803 chunk1 = (fatRadix + r).toRadixString(radix).substring(1);
743 } 804 }
805 int residue = (d2 << 20) + (d1 << 10) + d0;
806 String leadingDigits = residue == 0 ? '' : residue.toRadixString(radix);
807 return '$sign$leadingDigits$chunk1$chunk2$chunk3';
808 }
744 809
745 Int64 r = new Int64._bits(radix, 0, 0); 810 // Table of 'fat' radix values. Each entry for index `i` is the largest power
746 while (!a.isZero) { 811 // of `i` whose remainder fits in 20 bits.
747 a = _divMod(a, r, true); 812 static const _fatRadixTable = const <int>[
748 result = "${_hexDigit(_remainder._l)}$result"; 813 0,
749 } 814 0,
750 return negative ? "-$result" : result; 815 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
751 } 816 * 2,
817 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3,
818 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4,
819 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
820 6 * 6 * 6 * 6 * 6 * 6 * 6,
821 7 * 7 * 7 * 7 * 7 * 7 * 7,
822 8 * 8 * 8 * 8 * 8 * 8,
823 9 * 9 * 9 * 9 * 9 * 9,
824 10 * 10 * 10 * 10 * 10 * 10,
825 11 * 11 * 11 * 11 * 11,
826 12 * 12 * 12 * 12 * 12,
827 13 * 13 * 13 * 13 * 13,
828 14 * 14 * 14 * 14 * 14,
829 15 * 15 * 15 * 15 * 15,
830 16 * 16 * 16 * 16 * 16,
831 17 * 17 * 17 * 17,
832 18 * 18 * 18 * 18,
833 19 * 19 * 19 * 19,
834 20 * 20 * 20 * 20,
835 21 * 21 * 21 * 21,
836 22 * 22 * 22 * 22,
837 23 * 23 * 23 * 23,
838 24 * 24 * 24 * 24,
839 25 * 25 * 25 * 25,
840 26 * 26 * 26 * 26,
841 27 * 27 * 27 * 27,
842 28 * 28 * 28 * 28,
843 29 * 29 * 29 * 29,
844 30 * 30 * 30 * 30,
845 31 * 31 * 31 * 31,
846 32 * 32 * 32 * 32,
847 33 * 33 * 33,
848 34 * 34 * 34,
849 35 * 35 * 35,
850 36 * 36 * 36
851 ];
752 852
753 String toDebugString() { 853 String toDebugString() {
754 return "Int64[_l=$_l, _m=$_m, _h=$_h]"; 854 return "Int64[_l=$_l, _m=$_m, _h=$_h]";
755 } 855 }
756 856
757 /** 857 /**
758 * Constructs an [Int64] with a given bitwise representation. No validation 858 * Constructs an [Int64] with a given bitwise representation. No validation
759 * is performed. 859 * is performed.
760 */ 860 */
761 Int64._bits(int this._l, int this._m, int this._h); 861 Int64._bits(int this._l, int this._m, int this._h);
762 862
763 /** 863 /**
764 * Constructs an [Int64] with the same value as an existing [Int64]. 864 * Constructs an [Int64] with the same value as an existing [Int64].
765 */ 865 */
766 Int64._copy(Int64 other) { 866 Int64._copy(Int64 other)
767 _l = other._l; 867 : _l = other._l,
768 _m = other._m; 868 _m = other._m,
769 _h = other._h; 869 _h = other._h;
770 }
771 870
772 // Determine whether the platform supports ints greater than 2^53 871 // Determine whether the platform supports ints greater than 2^53
773 // without loss of precision. 872 // without loss of precision.
774 static bool _haveBigIntsCached = null; 873 static bool _haveBigIntsCached = null;
775 874
776 static bool get _haveBigInts { 875 static bool get _haveBigInts {
777 if (_haveBigIntsCached == null) { 876 if (_haveBigIntsCached == null) {
778 var x = 9007199254740992; 877 var x = 9007199254740992;
779 // Defeat compile-time constant folding. 878 // Defeat compile-time constant folding.
780 if (2 + 2 != 4) { 879 if (2 + 2 != 4) {
781 x = 0; 880 x = 0;
782 } 881 }
783 var y = x + 1; 882 var y = x + 1;
784 var same = y == x; 883 var same = y == x;
785 _haveBigIntsCached = !same; 884 _haveBigIntsCached = !same;
786 } 885 }
787 return _haveBigIntsCached; 886 return _haveBigIntsCached;
788 } 887 }
789 888
790 String _hexDigit(int digit) => "0123456789ABCDEF"[digit]; 889 String _hexDigit(int digit) => "0123456789ABCDEF"[digit];
791 890
792 // Implementation of '~/' and '%'. 891 // Implementation of '~/' and '%'.
793 892
794 // Note: mutates [this]. 893 // Note: mutates [this].
795 void _negate() { 894 void _negate() {
796 int neg0 = (~_l + 1) & _MASK; 895 int neg0 = (~_l + 1) & _MASK;
797 int neg1 = (~_m + (neg0 == 0 ? 1 : 0)) & _MASK; 896 int neg1 = (~_m + (neg0 == 0 ? 1 : 0)) & _MASK;
798 int neg2 = (~_h + ((neg0 == 0 && neg1 == 0) ? 1 : 0)) & _MASK_2; 897 int neg2 = (~_h + ((neg0 == 0 && neg1 == 0) ? 1 : 0)) & _MASK2;
799 898
800 _l = neg0; 899 _l = neg0;
801 _m = neg1; 900 _m = neg1;
802 _h = neg2; 901 _h = neg2;
803 } 902 }
804 903
805 // Note: mutates [this]. 904 // Note: mutates [this].
806 void _setBit(int bit) { 905 void _setBit(int bit) {
807 if (bit < _BITS) { 906 if (bit < _BITS) {
808 _l |= 0x1 << bit; 907 _l |= 0x1 << bit;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 int sum0 = a._l - b._l; 957 int sum0 = a._l - b._l;
859 int sum1 = a._m - b._m + _shiftRight(sum0, _BITS); 958 int sum1 = a._m - b._m + _shiftRight(sum0, _BITS);
860 sum2 += _shiftRight(sum1, _BITS); 959 sum2 += _shiftRight(sum1, _BITS);
861 960
862 if (sum2 < 0) { 961 if (sum2 < 0) {
863 return false; 962 return false;
864 } 963 }
865 964
866 a._l = sum0 & _MASK; 965 a._l = sum0 & _MASK;
867 a._m = sum1 & _MASK; 966 a._m = sum1 & _MASK;
868 a._h = sum2 & _MASK_2; 967 a._h = sum2 & _MASK2;
869 968
870 return true; 969 return true;
871 } 970 }
872 971
873 // Note: mutates [a] via _trialSubtract. 972 // Note: mutates [a] via _trialSubtract.
874 static Int64 _divModHelper(Int64 a, Int64 b, 973 static Int64 _divModHelper(Int64 a, Int64 b,
875 bool negative, bool aIsNegative, bool aIsMinValue, 974 bool negative, bool aIsNegative, bool aIsMinValue,
876 bool computeRemainder) { 975 bool computeRemainder) {
877 // Align the leading one bits of a and b by shifting b left. 976 // Align the leading one bits of a and b by shifting b left.
878 int shift = b.numberOfLeadingZeros() - a.numberOfLeadingZeros(); 977 int shift = b.numberOfLeadingZeros() - a.numberOfLeadingZeros();
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
1094 } 1193 }
1095 } 1194 }
1096 return ZERO; 1195 return ZERO;
1097 } 1196 }
1098 1197
1099 // Generate the quotient using bit-at-a-time long division. 1198 // Generate the quotient using bit-at-a-time long division.
1100 return _divModHelper(aIsCopy ? a : new Int64._copy(a), b, negative, 1199 return _divModHelper(aIsCopy ? a : new Int64._copy(a), b, negative,
1101 aIsNegative, aIsMinValue, computeRemainder); 1200 aIsNegative, aIsMinValue, computeRemainder);
1102 } 1201 }
1103 } 1202 }
OLDNEW
« no previous file with comments | « pkg/fixnum/lib/src/int32.dart ('k') | pkg/fixnum/test/int_64_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698