| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of fixnum; | |
| 6 | |
| 7 /** | |
| 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. | |
| 10 */ | |
| 11 class int64 implements intx { | |
| 12 | |
| 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 | |
| 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]. | |
| 17 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 | |
| 24 // Note: several functions require _BITS == 22 -- do not change this value. | |
| 25 static const int _BITS = 22; | |
| 26 static const int _BITS01 = 44; // 2 * _BITS | |
| 27 static const int _BITS2 = 20; // 64 - _BITS01 | |
| 28 static const int _MASK = 4194303; // (1 << _BITS) - 1 | |
| 29 static const int _MASK_2 = 1048575; // (1 << _BITS2) - 1 | |
| 30 static const int _SIGN_BIT = 19; // _BITS2 - 1 | |
| 31 static const int _SIGN_BIT_VALUE = 524288; // 1 << _SIGN_BIT | |
| 32 | |
| 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 // 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. | |
| 62 static int64 _remainder; | |
| 63 | |
| 64 /** | |
| 65 * The maximum positive value attainable by an [int64], namely | |
| 66 * 9,223,372,036,854,775,807. | |
| 67 */ | |
| 68 static int64 get MAX_VALUE { | |
| 69 if (_MAX_VALUE == null) { | |
| 70 _MAX_VALUE = new int64._bits(_MASK, _MASK, _MASK_2 >> 1); | |
| 71 } | |
| 72 return _MAX_VALUE; | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * The minimum positive value attainable by an [int64], namely | |
| 77 * -9,223,372,036,854,775,808. | |
| 78 */ | |
| 79 static int64 get MIN_VALUE { | |
| 80 if (_MIN_VALUE == null) { | |
| 81 _MIN_VALUE = new int64._bits(0, 0, _SIGN_BIT_VALUE); | |
| 82 } | |
| 83 return _MIN_VALUE; | |
| 84 } | |
| 85 | |
| 86 /** | |
| 87 * An [int64] constant equal to 0. | |
| 88 */ | |
| 89 static int64 get ZERO { | |
| 90 if (_ZERO == null) { | |
| 91 _ZERO = new int64(); | |
| 92 } | |
| 93 return _ZERO; | |
| 94 } | |
| 95 | |
| 96 /** | |
| 97 * An [int64] constant equal to 1. | |
| 98 */ | |
| 99 static int64 get ONE { | |
| 100 if (_ONE == null) { | |
| 101 _ONE = new int64._bits(1, 0, 0); | |
| 102 } | |
| 103 return _ONE; | |
| 104 } | |
| 105 | |
| 106 /** | |
| 107 * An [int64] constant equal to 2. | |
| 108 */ | |
| 109 static int64 get TWO { | |
| 110 if (_TWO == null) { | |
| 111 _TWO = new int64._bits(2, 0, 0); | |
| 112 } | |
| 113 return _TWO; | |
| 114 } | |
| 115 | |
| 116 /** | |
| 117 * Parses a [String] in a given [radix] between 2 and 16 and returns an | |
| 118 * [int64]. | |
| 119 */ | |
| 120 // TODO(rice) - make this faster by converting several digits at once. | |
| 121 static int64 parseRadix(String s, int radix) { | |
| 122 if ((radix <= 1) || (radix > 16)) { | |
| 123 throw "Bad radix: $radix"; | |
| 124 } | |
| 125 int64 x = ZERO; | |
| 126 int i = 0; | |
| 127 bool negative = false; | |
| 128 if (s[0] == '-') { | |
| 129 negative = true; | |
| 130 i++; | |
| 131 } | |
| 132 for (; i < s.length; i++) { | |
| 133 int c = s.charCodeAt(i); | |
| 134 int digit = int32._decodeHex(c); | |
| 135 if (digit < 0 || digit >= radix) { | |
| 136 throw new Exception("Non-radix char code: $c"); | |
| 137 } | |
| 138 x = (x * radix) + digit; | |
| 139 } | |
| 140 return negative ? -x : x; | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * Parses a decimal [String] and returns an [int64]. | |
| 145 */ | |
| 146 static int64 parseInt(String s) => parseRadix(s, 10); | |
| 147 | |
| 148 /** | |
| 149 * Parses a hexadecimal [String] and returns an [int64]. | |
| 150 */ | |
| 151 static int64 parseHex(String s) => parseRadix(s, 16); | |
| 152 | |
| 153 // | |
| 154 // Public constructors | |
| 155 // | |
| 156 | |
| 157 /** | |
| 158 * Constructs an [int64] equal to 0. | |
| 159 */ | |
| 160 int64() : _l = 0, _m = 0, _h = 0; | |
| 161 | |
| 162 /** | |
| 163 * Constructs an [int64] with a given [int] value. | |
| 164 */ | |
| 165 int64.fromInt(int value) { | |
| 166 bool negative = false; | |
| 167 if (value < 0) { | |
| 168 negative = true; | |
| 169 value = -value - 1; | |
| 170 } | |
| 171 if (_haveBigInts) { | |
| 172 _l = value & _MASK; | |
| 173 _m = (value >> _BITS) & _MASK; | |
| 174 _h = (value >> _BITS01) & _MASK_2; | |
| 175 } else { | |
| 176 // Avoid using bitwise operations that coerce their input to 32 bits. | |
| 177 _h = value ~/ 17592186044416; // 2^44 | |
| 178 value -= _h * 17592186044416; | |
| 179 _m = value ~/ 4194304; // 2^22 | |
| 180 value -= _m * 4194304; | |
| 181 _l = value; | |
| 182 } | |
| 183 | |
| 184 if (negative) { | |
| 185 _l = ~_l & _MASK; | |
| 186 _m = ~_m & _MASK; | |
| 187 _h = ~_h & _MASK_2; | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 factory int64.fromBytes(List<int> bytes) { | |
| 192 int top = bytes[7] & 0xff; | |
| 193 top <<= 8; | |
| 194 top |= bytes[6] & 0xff; | |
| 195 top <<= 8; | |
| 196 top |= bytes[5] & 0xff; | |
| 197 top <<= 8; | |
| 198 top |= bytes[4] & 0xff; | |
| 199 | |
| 200 int bottom = bytes[3] & 0xff; | |
| 201 bottom <<= 8; | |
| 202 bottom |= bytes[2] & 0xff; | |
| 203 bottom <<= 8; | |
| 204 bottom |= bytes[1] & 0xff; | |
| 205 bottom <<= 8; | |
| 206 bottom |= bytes[0] & 0xff; | |
| 207 | |
| 208 return new int64.fromInts(top, bottom); | |
| 209 } | |
| 210 | |
| 211 factory int64.fromBytesBigEndian(List<int> bytes) { | |
| 212 int top = bytes[0] & 0xff; | |
| 213 top <<= 8; | |
| 214 top |= bytes[1] & 0xff; | |
| 215 top <<= 8; | |
| 216 top |= bytes[2] & 0xff; | |
| 217 top <<= 8; | |
| 218 top |= bytes[3] & 0xff; | |
| 219 | |
| 220 int bottom = bytes[4] & 0xff; | |
| 221 bottom <<= 8; | |
| 222 bottom |= bytes[5] & 0xff; | |
| 223 bottom <<= 8; | |
| 224 bottom |= bytes[6] & 0xff; | |
| 225 bottom <<= 8; | |
| 226 bottom |= bytes[7] & 0xff; | |
| 227 | |
| 228 return new int64.fromInts(top, bottom); | |
| 229 } | |
| 230 | |
| 231 /** | |
| 232 * Constructs an [int64] from a pair of 32-bit integers having the value | |
| 233 * [:((top & 0xffffffff) << 32) | (bottom & 0xffffffff):]. | |
| 234 */ | |
| 235 int64.fromInts(int top, int bottom) { | |
| 236 top &= 0xffffffff; | |
| 237 bottom &= 0xffffffff; | |
| 238 _l = bottom & _MASK; | |
| 239 _m = ((top & 0xfff) << 10) | ((bottom >> _BITS) & 0x3ff); | |
| 240 _h = (top >> 12) & _MASK_2; | |
| 241 } | |
| 242 | |
| 243 int64 _promote(other) { | |
| 244 if (other == null) { | |
| 245 throw new NullPointerException(); | |
| 246 } else if (other is intx) { | |
| 247 other = other.toInt64(); | |
| 248 } else if (other is int) { | |
| 249 other = new int64.fromInt(other); | |
| 250 } | |
| 251 if (other is !int64) { | |
| 252 throw new Exception("Can't promote $other to int64"); | |
| 253 } | |
| 254 return other; | |
| 255 } | |
| 256 | |
| 257 int64 operator +(other) { | |
| 258 int64 o = _promote(other); | |
| 259 int sum0 = _l + o._l; | |
| 260 int sum1 = _m + o._m + _shiftRight(sum0, _BITS); | |
| 261 int sum2 = _h + o._h + _shiftRight(sum1, _BITS); | |
| 262 | |
| 263 int64 result = new int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK_2); | |
| 264 return result; | |
| 265 } | |
| 266 | |
| 267 int64 operator -(other) { | |
| 268 int64 o = _promote(other); | |
| 269 | |
| 270 int sum0 = _l - o._l; | |
| 271 int sum1 = _m - o._m + _shiftRight(sum0, _BITS); | |
| 272 int sum2 = _h - o._h + _shiftRight(sum1, _BITS); | |
| 273 | |
| 274 int64 result = new int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK_2); | |
| 275 return result; | |
| 276 } | |
| 277 | |
| 278 int64 operator -() { | |
| 279 // Like 0 - this. | |
| 280 int sum0 = -_l; | |
| 281 int sum1 = -_m + _shiftRight(sum0, _BITS); | |
| 282 int sum2 = -_h + _shiftRight(sum1, _BITS); | |
| 283 | |
| 284 return new int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK_2); | |
| 285 } | |
| 286 | |
| 287 int64 operator *(other) { | |
| 288 int64 o = _promote(other); | |
| 289 // Grab 13-bit chunks. | |
| 290 int a0 = _l & 0x1fff; | |
| 291 int a1 = (_l >> 13) | ((_m & 0xf) << 9); | |
| 292 int a2 = (_m >> 4) & 0x1fff; | |
| 293 int a3 = (_m >> 17) | ((_h & 0xff) << 5); | |
| 294 int a4 = (_h & 0xfff00) >> 8; | |
| 295 | |
| 296 int b0 = o._l & 0x1fff; | |
| 297 int b1 = (o._l >> 13) | ((o._m & 0xf) << 9); | |
| 298 int b2 = (o._m >> 4) & 0x1fff; | |
| 299 int b3 = (o._m >> 17) | ((o._h & 0xff) << 5); | |
| 300 int b4 = (o._h & 0xfff00) >> 8; | |
| 301 | |
| 302 // Compute partial products. | |
| 303 // Optimization: if b is small, avoid multiplying by parts that are 0. | |
| 304 int p0 = a0 * b0; // << 0 | |
| 305 int p1 = a1 * b0; // << 13 | |
| 306 int p2 = a2 * b0; // << 26 | |
| 307 int p3 = a3 * b0; // << 39 | |
| 308 int p4 = a4 * b0; // << 52 | |
| 309 | |
| 310 if (b1 != 0) { | |
| 311 p1 += a0 * b1; | |
| 312 p2 += a1 * b1; | |
| 313 p3 += a2 * b1; | |
| 314 p4 += a3 * b1; | |
| 315 } | |
| 316 if (b2 != 0) { | |
| 317 p2 += a0 * b2; | |
| 318 p3 += a1 * b2; | |
| 319 p4 += a2 * b2; | |
| 320 } | |
| 321 if (b3 != 0) { | |
| 322 p3 += a0 * b3; | |
| 323 p4 += a1 * b3; | |
| 324 } | |
| 325 if (b4 != 0) { | |
| 326 p4 += a0 * b4; | |
| 327 } | |
| 328 | |
| 329 // Accumulate into 22-bit chunks: | |
| 330 // .........................................c10|...................c00| | |
| 331 // |....................|..................xxxx|xxxxxxxxxxxxxxxxxxxxxx| p0 | |
| 332 // |....................|......................|......................| | |
| 333 // |....................|...................c11|......c01.............| | |
| 334 // |....................|....xxxxxxxxxxxxxxxxxx|xxxxxxxxx.............| p1 | |
| 335 // |....................|......................|......................| | |
| 336 // |.................c22|...............c12....|......................| | |
| 337 // |..........xxxxxxxxxx|xxxxxxxxxxxxxxxxxx....|......................| p2 | |
| 338 // |....................|......................|......................| | |
| 339 // |.................c23|..c13.................|......................| | |
| 340 // |xxxxxxxxxxxxxxxxxxxx|xxxxx.................|......................| p3 | |
| 341 // |....................|......................|......................| | |
| 342 // |.........c24........|......................|......................| | |
| 343 // |xxxxxxxxxxxx........|......................|......................| p4 | |
| 344 | |
| 345 int c00 = p0 & 0x3fffff; | |
| 346 int c01 = (p1 & 0x1ff) << 13; | |
| 347 int c0 = c00 + c01; | |
| 348 | |
| 349 int c10 = p0 >> 22; | |
| 350 int c11 = p1 >> 9; | |
| 351 int c12 = (p2 & 0x3ffff) << 4; | |
| 352 int c13 = (p3 & 0x1f) << 17; | |
| 353 int c1 = c10 + c11 + c12 + c13; | |
| 354 | |
| 355 int c22 = p2 >> 18; | |
| 356 int c23 = p3 >> 5; | |
| 357 int c24 = (p4 & 0xfff) << 8; | |
| 358 int c2 = c22 + c23 + c24; | |
| 359 | |
| 360 // Propagate high bits from c0 -> c1, c1 -> c2. | |
| 361 c1 += c0 >> _BITS; | |
| 362 c0 &= _MASK; | |
| 363 c2 += c1 >> _BITS; | |
| 364 c1 &= _MASK; | |
| 365 c2 &= _MASK_2; | |
| 366 | |
| 367 return new int64._bits(c0, c1, c2); | |
| 368 } | |
| 369 | |
| 370 int64 operator %(other) { | |
| 371 if (other.isZero) { | |
| 372 throw new IntegerDivisionByZeroException(); | |
| 373 } | |
| 374 if (this.isZero) { | |
| 375 return ZERO; | |
| 376 } | |
| 377 int64 o = _promote(other).abs(); | |
| 378 _divMod(this, o, true); | |
| 379 return _remainder < 0 ? (_remainder + o) : _remainder; | |
| 380 } | |
| 381 | |
| 382 int64 operator ~/(other) => _divMod(this, _promote(other), false); | |
| 383 | |
| 384 // int64 remainder(other) => this - (this ~/ other) * other; | |
| 385 int64 remainder(other) { | |
| 386 if (other.isZero) { | |
| 387 throw new IntegerDivisionByZeroException(); | |
| 388 } | |
| 389 int64 o = _promote(other).abs(); | |
| 390 _divMod(this, o, true); | |
| 391 return _remainder; | |
| 392 } | |
| 393 | |
| 394 int64 operator &(other) { | |
| 395 int64 o = _promote(other); | |
| 396 int a0 = _l & o._l; | |
| 397 int a1 = _m & o._m; | |
| 398 int a2 = _h & o._h; | |
| 399 return new int64._bits(a0, a1, a2); | |
| 400 } | |
| 401 | |
| 402 int64 operator |(other) { | |
| 403 int64 o = _promote(other); | |
| 404 int a0 = _l | o._l; | |
| 405 int a1 = _m | o._m; | |
| 406 int a2 = _h | o._h; | |
| 407 return new int64._bits(a0, a1, a2); | |
| 408 } | |
| 409 | |
| 410 int64 operator ^(other) { | |
| 411 int64 o = _promote(other); | |
| 412 int a0 = _l ^ o._l; | |
| 413 int a1 = _m ^ o._m; | |
| 414 int a2 = _h ^ o._h; | |
| 415 return new int64._bits(a0, a1, a2); | |
| 416 } | |
| 417 | |
| 418 int64 operator ~() { | |
| 419 var result = new int64._bits((~_l) & _MASK, (~_m) & _MASK, (~_h) & _MASK_2); | |
| 420 return result; | |
| 421 } | |
| 422 | |
| 423 int64 operator <<(int n) { | |
| 424 if (n < 0) { | |
| 425 throw new ArgumentError("$n"); | |
| 426 } | |
| 427 n &= 63; | |
| 428 | |
| 429 int res0, res1, res2; | |
| 430 if (n < _BITS) { | |
| 431 res0 = _l << n; | |
| 432 res1 = (_m << n) | (_l >> (_BITS - n)); | |
| 433 res2 = (_h << n) | (_m >> (_BITS - n)); | |
| 434 } else if (n < _BITS01) { | |
| 435 res0 = 0; | |
| 436 res1 = _l << (n - _BITS); | |
| 437 res2 = (_m << (n - _BITS)) | (_l >> (_BITS01 - n)); | |
| 438 } else { | |
| 439 res0 = 0; | |
| 440 res1 = 0; | |
| 441 res2 = _l << (n - _BITS01); | |
| 442 } | |
| 443 | |
| 444 return new int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK_2); | |
| 445 } | |
| 446 | |
| 447 int64 operator >>(int n) { | |
| 448 if (n < 0) { | |
| 449 throw new ArgumentError("$n"); | |
| 450 } | |
| 451 n &= 63; | |
| 452 | |
| 453 int res0, res1, res2; | |
| 454 | |
| 455 // Sign extend h(a). | |
| 456 int a2 = _h; | |
| 457 bool negative = (a2 & _SIGN_BIT_VALUE) != 0; | |
| 458 if (negative) { | |
| 459 a2 += 0x3 << _BITS2; // add extra one bits on the left | |
| 460 } | |
| 461 | |
| 462 if (n < _BITS) { | |
| 463 res2 = _shiftRight(a2, n); | |
| 464 if (negative) { | |
| 465 res2 |= _MASK_2 & ~(_MASK_2 >> n); | |
| 466 } | |
| 467 res1 = _shiftRight(_m, n) | (a2 << (_BITS - n)); | |
| 468 res0 = _shiftRight(_l, n) | (_m << (_BITS - n)); | |
| 469 } else if (n < _BITS01) { | |
| 470 res2 = negative ? _MASK_2 : 0; | |
| 471 res1 = _shiftRight(a2, n - _BITS); | |
| 472 if (negative) { | |
| 473 res1 |= _MASK & ~(_MASK >> (n - _BITS)); | |
| 474 } | |
| 475 res0 = _shiftRight(_m, n - _BITS) | (a2 << (_BITS01 - n)); | |
| 476 } else { | |
| 477 res2 = negative ? _MASK_2 : 0; | |
| 478 res1 = negative ? _MASK : 0; | |
| 479 res0 = _shiftRight(a2, n - _BITS01); | |
| 480 if (negative) { | |
| 481 res0 |= _MASK & ~(_MASK >> (n - _BITS01)); | |
| 482 } | |
| 483 } | |
| 484 | |
| 485 return new int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK_2); | |
| 486 } | |
| 487 | |
| 488 int64 shiftRightUnsigned(int n) { | |
| 489 if (n < 0) { | |
| 490 throw new ArgumentError("$n"); | |
| 491 } | |
| 492 n &= 63; | |
| 493 | |
| 494 int res0, res1, res2; | |
| 495 int a2 = _h & _MASK_2; // Ensure a2 is positive. | |
| 496 if (n < _BITS) { | |
| 497 res2 = a2 >> n; | |
| 498 res1 = (_m >> n) | (a2 << (_BITS - n)); | |
| 499 res0 = (_l >> n) | (_m << (_BITS - n)); | |
| 500 } else if (n < _BITS01) { | |
| 501 res2 = 0; | |
| 502 res1 = a2 >> (n - _BITS); | |
| 503 res0 = (_m >> (n - _BITS)) | (_h << (_BITS01 - n)); | |
| 504 } else { | |
| 505 res2 = 0; | |
| 506 res1 = 0; | |
| 507 res0 = a2 >> (n - _BITS01); | |
| 508 } | |
| 509 | |
| 510 return new int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK_2); | |
| 511 } | |
| 512 | |
| 513 /** | |
| 514 * Returns [true] if this [int64] has the same numeric value as the | |
| 515 * given object. The argument may be an [int] or an [intx]. | |
| 516 */ | |
| 517 bool operator ==(other) { | |
| 518 if (other == null) { | |
| 519 return false; | |
| 520 } | |
| 521 int64 o = _promote(other); | |
| 522 return _l == o._l && _m == o._m && _h == o._h; | |
| 523 } | |
| 524 | |
| 525 int compareTo(Comparable other) { | |
| 526 int64 o = _promote(other); | |
| 527 int signa = _h >> (_BITS2 - 1); | |
| 528 int signb = o._h >> (_BITS2 - 1); | |
| 529 if (signa != signb) { | |
| 530 return signa == 0 ? 1 : -1; | |
| 531 } | |
| 532 if (_h > o._h) { | |
| 533 return 1; | |
| 534 } else if (_h < o._h) { | |
| 535 return -1; | |
| 536 } | |
| 537 if (_m > o._m) { | |
| 538 return 1; | |
| 539 } else if (_m < o._m) { | |
| 540 return -1; | |
| 541 } | |
| 542 if (_l > o._l) { | |
| 543 return 1; | |
| 544 } else if (_l < o._l) { | |
| 545 return -1; | |
| 546 } | |
| 547 return 0; | |
| 548 } | |
| 549 | |
| 550 bool operator <(other) { | |
| 551 return this.compareTo(other) < 0; | |
| 552 } | |
| 553 | |
| 554 bool operator <=(other) { | |
| 555 return this.compareTo(other) <= 0; | |
| 556 } | |
| 557 | |
| 558 bool operator >(other) { | |
| 559 return this.compareTo(other) > 0; | |
| 560 } | |
| 561 | |
| 562 bool operator >=(other) { | |
| 563 return this.compareTo(other) >= 0; | |
| 564 } | |
| 565 | |
| 566 bool get isEven => (_l & 0x1) == 0; | |
| 567 bool get isMaxValue => (_h == _MASK_2 >> 1) && _m == _MASK && _l == _MASK; | |
| 568 bool get isMinValue => _h == _SIGN_BIT_VALUE && _m == 0 && _l == 0; | |
| 569 bool get isNegative => (_h >> (_BITS2 - 1)) != 0; | |
| 570 bool get isOdd => (_l & 0x1) == 1; | |
| 571 bool get isZero => _h == 0 && _m == 0 && _l == 0; | |
| 572 | |
| 573 /** | |
| 574 * Returns a hash code based on all the bits of this [int64]. | |
| 575 */ | |
| 576 int get hashCode { | |
| 577 int bottom = ((_m & 0x3ff) << _BITS) | _l; | |
| 578 int top = (_h << 12) | ((_m >> 10) & 0xfff); | |
| 579 return bottom ^ top; | |
| 580 } | |
| 581 | |
| 582 int64 abs() { | |
| 583 return this < 0 ? -this : this; | |
| 584 } | |
| 585 | |
| 586 /** | |
| 587 * Returns the number of leading zeros in this [int64] as an [int] | |
| 588 * between 0 and 64. | |
| 589 */ | |
| 590 int numberOfLeadingZeros() { | |
| 591 int b2 = int32._numberOfLeadingZeros(_h); | |
| 592 if (b2 == 32) { | |
| 593 int b1 = int32._numberOfLeadingZeros(_m); | |
| 594 if (b1 == 32) { | |
| 595 return int32._numberOfLeadingZeros(_l) + 32; | |
| 596 } else { | |
| 597 return b1 + _BITS2 - (32 - _BITS); | |
| 598 } | |
| 599 } else { | |
| 600 return b2 - (32 - _BITS2); | |
| 601 } | |
| 602 } | |
| 603 | |
| 604 /** | |
| 605 * Returns the number of trailing zeros in this [int64] as an [int] | |
| 606 * between 0 and 64. | |
| 607 */ | |
| 608 int numberOfTrailingZeros() { | |
| 609 int zeros = int32._numberOfTrailingZeros(_l); | |
| 610 if (zeros < 32) { | |
| 611 return zeros; | |
| 612 } | |
| 613 | |
| 614 zeros = int32._numberOfTrailingZeros(_m); | |
| 615 if (zeros < 32) { | |
| 616 return _BITS + zeros; | |
| 617 } | |
| 618 | |
| 619 zeros = int32._numberOfTrailingZeros(_h); | |
| 620 if (zeros < 32) { | |
| 621 return _BITS01 + zeros; | |
| 622 } | |
| 623 // All zeros | |
| 624 return 64; | |
| 625 } | |
| 626 | |
| 627 List<int> toBytes() { | |
| 628 List<int> result = new List<int>(8); | |
| 629 result[0] = _l & 0xff; | |
| 630 result[1] = (_l >> 8) & 0xff; | |
| 631 result[2] = ((_m << 6) & 0xfc) | ((_l >> 16) & 0x3f); | |
| 632 result[3] = (_m >> 2) & 0xff; | |
| 633 result[4] = (_m >> 10) & 0xff; | |
| 634 result[5] = ((_h << 4) & 0xf0) | ((_m >> 18) & 0xf); | |
| 635 result[6] = (_h >> 4) & 0xff; | |
| 636 result[7] = (_h >> 12) & 0xff; | |
| 637 return result; | |
| 638 } | |
| 639 | |
| 640 int toInt() { | |
| 641 int l = _l; | |
| 642 int m = _m; | |
| 643 int h = _h; | |
| 644 bool negative = false; | |
| 645 if ((_h & _SIGN_BIT_VALUE) != 0) { | |
| 646 l = ~_l & _MASK; | |
| 647 m = ~_m & _MASK; | |
| 648 h = ~_h & _MASK_2; | |
| 649 negative = true; | |
| 650 } | |
| 651 | |
| 652 int result; | |
| 653 if (_haveBigInts) { | |
| 654 result = (h << _BITS01) | (m << _BITS) | l; | |
| 655 } else { | |
| 656 result = (h * 17592186044416) + (m * 4194304) + l; | |
| 657 } | |
| 658 return negative ? -result - 1 : result; | |
| 659 } | |
| 660 | |
| 661 /** | |
| 662 * Returns an [int32] containing the low 32 bits of this [int64]. | |
| 663 */ | |
| 664 int32 toInt32() { | |
| 665 return new int32.fromInt(((_m & 0x3ff) << _BITS) | _l); | |
| 666 } | |
| 667 | |
| 668 /** | |
| 669 * Returns [this]. | |
| 670 */ | |
| 671 int64 toInt64() => this; | |
| 672 | |
| 673 /** | |
| 674 * Returns the value of this [int64] as a decimal [String]. | |
| 675 */ | |
| 676 // TODO(rice) - Make this faster by converting several digits at once. | |
| 677 String toString() { | |
| 678 int64 a = this; | |
| 679 if (a.isZero) { | |
| 680 return "0"; | |
| 681 } | |
| 682 if (a.isMinValue) { | |
| 683 return "-9223372036854775808"; | |
| 684 } | |
| 685 | |
| 686 String result = ""; | |
| 687 bool negative = false; | |
| 688 if (a.isNegative) { | |
| 689 negative = true; | |
| 690 a = -a; | |
| 691 } | |
| 692 | |
| 693 int64 ten = new int64._bits(10, 0, 0); | |
| 694 while (!a.isZero) { | |
| 695 a = _divMod(a, ten, true); | |
| 696 result = "${_remainder._l}$result"; | |
| 697 } | |
| 698 if (negative) { | |
| 699 result = "-$result"; | |
| 700 } | |
| 701 return result; | |
| 702 } | |
| 703 | |
| 704 // TODO(rice) - Make this faster by avoiding arithmetic. | |
| 705 String toHexString() { | |
| 706 int64 x = new int64._copy(this); | |
| 707 if (isZero) { | |
| 708 return "0"; | |
| 709 } | |
| 710 String hexStr = ""; | |
| 711 int64 digit_f = new int64.fromInt(0xf); | |
| 712 while (!x.isZero) { | |
| 713 int digit = x._l & 0xf; | |
| 714 hexStr = "${_hexDigit(digit)}$hexStr"; | |
| 715 x = x.shiftRightUnsigned(4); | |
| 716 } | |
| 717 return hexStr; | |
| 718 } | |
| 719 | |
| 720 String toRadixString(int radix) { | |
| 721 if ((radix <= 1) || (radix > 16)) { | |
| 722 throw "Bad radix: $radix"; | |
| 723 } | |
| 724 int64 a = this; | |
| 725 if (a.isZero) { | |
| 726 return "0"; | |
| 727 } | |
| 728 if (a.isMinValue) { | |
| 729 return _minValues[radix]; | |
| 730 } | |
| 731 | |
| 732 String result = ""; | |
| 733 bool negative = false; | |
| 734 if (a.isNegative) { | |
| 735 negative = true; | |
| 736 a = -a; | |
| 737 } | |
| 738 | |
| 739 int64 r = new int64._bits(radix, 0, 0); | |
| 740 while (!a.isZero) { | |
| 741 a = _divMod(a, r, true); | |
| 742 result = "${_hexDigit(_remainder._l)}$result"; | |
| 743 } | |
| 744 return negative ? "-$result" : result; | |
| 745 } | |
| 746 | |
| 747 String toDebugString() { | |
| 748 return "int64[_l=$_l, _m=$_m, _h=$_h]"; | |
| 749 } | |
| 750 | |
| 751 /** | |
| 752 * Constructs an [int64] with a given bitwise representation. No validation | |
| 753 * is performed. | |
| 754 */ | |
| 755 int64._bits(int this._l, int this._m, int this._h); | |
| 756 | |
| 757 /** | |
| 758 * Constructs an [int64] with the same value as an existing [int64]. | |
| 759 */ | |
| 760 int64._copy(int64 other) { | |
| 761 _l = other._l; | |
| 762 _m = other._m; | |
| 763 _h = other._h; | |
| 764 } | |
| 765 | |
| 766 // Determine whether the platform supports ints greater than 2^53 | |
| 767 // without loss of precision. | |
| 768 static bool _haveBigIntsCached = null; | |
| 769 | |
| 770 static bool get _haveBigInts { | |
| 771 if (_haveBigIntsCached == null) { | |
| 772 var x = 9007199254740992; | |
| 773 // Defeat compile-time constant folding. | |
| 774 if (2 + 2 != 4) { | |
| 775 x = 0; | |
| 776 } | |
| 777 var y = x + 1; | |
| 778 var same = y == x; | |
| 779 _haveBigIntsCached = !same; | |
| 780 } | |
| 781 return _haveBigIntsCached; | |
| 782 } | |
| 783 | |
| 784 String _hexDigit(int digit) => "0123456789ABCDEF"[digit]; | |
| 785 | |
| 786 // Implementation of '~/' and '%'. | |
| 787 | |
| 788 // Note: mutates [this]. | |
| 789 void _negate() { | |
| 790 int neg0 = (~_l + 1) & _MASK; | |
| 791 int neg1 = (~_m + (neg0 == 0 ? 1 : 0)) & _MASK; | |
| 792 int neg2 = (~_h + ((neg0 == 0 && neg1 == 0) ? 1 : 0)) & _MASK_2; | |
| 793 | |
| 794 _l = neg0; | |
| 795 _m = neg1; | |
| 796 _h = neg2; | |
| 797 } | |
| 798 | |
| 799 // Note: mutates [this]. | |
| 800 void _setBit(int bit) { | |
| 801 if (bit < _BITS) { | |
| 802 _l |= 0x1 << bit; | |
| 803 } else if (bit < _BITS01) { | |
| 804 _m |= 0x1 << (bit - _BITS); | |
| 805 } else { | |
| 806 _h |= 0x1 << (bit - _BITS01); | |
| 807 } | |
| 808 } | |
| 809 | |
| 810 // Note: mutates [this]. | |
| 811 void _toShru1() { | |
| 812 int a2 = _h; | |
| 813 int a1 = _m; | |
| 814 int a0 = _l; | |
| 815 | |
| 816 _h = a2 >> 1; | |
| 817 _m = (a1 >> 1) | ((a2 & 0x1) << (_BITS - 1)); | |
| 818 _l = (a0 >> 1) | ((a1 & 0x1) << (_BITS - 1)); | |
| 819 } | |
| 820 | |
| 821 // Work around dart2js bugs with negative arguments to '>>' operator. | |
| 822 static int _shiftRight(int x, int n) { | |
| 823 if (x >= 0) { | |
| 824 return x >> n; | |
| 825 } else { | |
| 826 int shifted = x >> n; | |
| 827 if (shifted >= 0x80000000) { | |
| 828 shifted -= 4294967296; | |
| 829 } | |
| 830 return shifted; | |
| 831 } | |
| 832 } | |
| 833 | |
| 834 /** | |
| 835 * Attempt to subtract b from a if a >= b: | |
| 836 * | |
| 837 * if (a >= b) { | |
| 838 * a -= b; | |
| 839 * return true; | |
| 840 * } else { | |
| 841 * return false; | |
| 842 * } | |
| 843 */ | |
| 844 // Note: mutates [a]. | |
| 845 static bool _trialSubtract(int64 a, int64 b) { | |
| 846 // Early exit. | |
| 847 int sum2 = a._h - b._h; | |
| 848 if (sum2 < 0) { | |
| 849 return false; | |
| 850 } | |
| 851 | |
| 852 int sum0 = a._l - b._l; | |
| 853 int sum1 = a._m - b._m + _shiftRight(sum0, _BITS); | |
| 854 sum2 += _shiftRight(sum1, _BITS); | |
| 855 | |
| 856 if (sum2 < 0) { | |
| 857 return false; | |
| 858 } | |
| 859 | |
| 860 a._l = sum0 & _MASK; | |
| 861 a._m = sum1 & _MASK; | |
| 862 a._h = sum2 & _MASK_2; | |
| 863 | |
| 864 return true; | |
| 865 } | |
| 866 | |
| 867 // Note: mutates [a] via _trialSubtract. | |
| 868 static int64 _divModHelper(int64 a, int64 b, | |
| 869 bool negative, bool aIsNegative, bool aIsMinValue, | |
| 870 bool computeRemainder) { | |
| 871 // Align the leading one bits of a and b by shifting b left. | |
| 872 int shift = b.numberOfLeadingZeros() - a.numberOfLeadingZeros(); | |
| 873 int64 bshift = b << shift; | |
| 874 | |
| 875 // Quotient must be a new instance since we mutate it. | |
| 876 int64 quotient = new int64(); | |
| 877 while (shift >= 0) { | |
| 878 bool gte = _trialSubtract(a, bshift); | |
| 879 if (gte) { | |
| 880 quotient._setBit(shift); | |
| 881 if (a.isZero) { | |
| 882 break; | |
| 883 } | |
| 884 } | |
| 885 | |
| 886 bshift._toShru1(); | |
| 887 shift--; | |
| 888 } | |
| 889 | |
| 890 if (negative) { | |
| 891 quotient._negate(); | |
| 892 } | |
| 893 | |
| 894 if (computeRemainder) { | |
| 895 if (aIsNegative) { | |
| 896 _remainder = -a; | |
| 897 if (aIsMinValue) { | |
| 898 _remainder = _remainder - ONE; | |
| 899 } | |
| 900 } else { | |
| 901 _remainder = a; | |
| 902 } | |
| 903 } | |
| 904 | |
| 905 return quotient; | |
| 906 } | |
| 907 | |
| 908 int64 _divModByMinValue(bool computeRemainder) { | |
| 909 // MIN_VALUE / MIN_VALUE == 1, remainder = 0 | |
| 910 // (x != MIN_VALUE) / MIN_VALUE == 0, remainder == x | |
| 911 if (isMinValue) { | |
| 912 if (computeRemainder) { | |
| 913 _remainder = ZERO; | |
| 914 } | |
| 915 return ONE; | |
| 916 } | |
| 917 if (computeRemainder) { | |
| 918 _remainder = this; | |
| 919 } | |
| 920 return ZERO; | |
| 921 } | |
| 922 | |
| 923 /** | |
| 924 * this &= ((1L << bits) - 1) | |
| 925 */ | |
| 926 // Note: mutates [this]. | |
| 927 int64 _maskRight(int bits) { | |
| 928 int b0, b1, b2; | |
| 929 if (bits <= _BITS) { | |
| 930 b0 = _l & ((1 << bits) - 1); | |
| 931 b1 = b2 = 0; | |
| 932 } else if (bits <= _BITS01) { | |
| 933 b0 = _l; | |
| 934 b1 = _m & ((1 << (bits - _BITS)) - 1); | |
| 935 b2 = 0; | |
| 936 } else { | |
| 937 b0 = _l; | |
| 938 b1 = _m; | |
| 939 b2 = _h & ((1 << (bits - _BITS01)) - 1); | |
| 940 } | |
| 941 | |
| 942 _l = b0; | |
| 943 _m = b1; | |
| 944 _h = b2; | |
| 945 } | |
| 946 | |
| 947 int64 _divModByShift(int64 a, int bpower, bool negative, bool aIsCopy, | |
| 948 bool aIsNegative, bool computeRemainder) { | |
| 949 int64 c = a >> bpower; | |
| 950 if (negative) { | |
| 951 c._negate(); | |
| 952 } | |
| 953 | |
| 954 if (computeRemainder) { | |
| 955 if (!aIsCopy) { | |
| 956 a = new int64._copy(a); | |
| 957 } | |
| 958 a._maskRight(bpower); | |
| 959 if (aIsNegative) { | |
| 960 a._negate(); | |
| 961 } | |
| 962 _remainder = a; | |
| 963 } | |
| 964 return c; | |
| 965 } | |
| 966 | |
| 967 /** | |
| 968 * Return the exact log base 2 of this, or -1 if this is not a power of two. | |
| 969 */ | |
| 970 int _powerOfTwo() { | |
| 971 // Power of two or 0. | |
| 972 int l = _l; | |
| 973 if ((l & (l - 1)) != 0) { | |
| 974 return -1; | |
| 975 } | |
| 976 int m = _m; | |
| 977 if ((m & (m - 1)) != 0) { | |
| 978 return -1; | |
| 979 } | |
| 980 int h = _h; | |
| 981 if ((h & (h - 1)) != 0) { | |
| 982 return -1; | |
| 983 } | |
| 984 if (h == 0 && m == 0 && l == 0) { | |
| 985 return -1; | |
| 986 } | |
| 987 if (h == 0 && m == 0 && l != 0) { | |
| 988 return int32._numberOfTrailingZeros(l); | |
| 989 } | |
| 990 if (h == 0 && m != 0 && l == 0) { | |
| 991 return int32._numberOfTrailingZeros(m) + _BITS; | |
| 992 } | |
| 993 if (h != 0 && m == 0 && l == 0) { | |
| 994 return int32._numberOfTrailingZeros(h) + _BITS01; | |
| 995 } | |
| 996 | |
| 997 return -1; | |
| 998 } | |
| 999 | |
| 1000 int64 _divMod(int64 a, int64 b, bool computeRemainder) { | |
| 1001 if (b.isZero) { | |
| 1002 throw new IntegerDivisionByZeroException(); | |
| 1003 } | |
| 1004 if (a.isZero) { | |
| 1005 if (computeRemainder) { | |
| 1006 _remainder = ZERO; | |
| 1007 } | |
| 1008 return ZERO; | |
| 1009 } | |
| 1010 // MIN_VALUE / MIN_VALUE = 1, anything other a / MIN_VALUE is 0. | |
| 1011 if (b.isMinValue) { | |
| 1012 return a._divModByMinValue(computeRemainder); | |
| 1013 } | |
| 1014 // Normalize b to abs(b), keeping track of the parity in 'negative'. | |
| 1015 // We can do this because we have already ensured that b != MIN_VALUE. | |
| 1016 bool negative = false; | |
| 1017 if (b.isNegative) { | |
| 1018 b = -b; | |
| 1019 negative = !negative; | |
| 1020 } | |
| 1021 // If b == 2^n, bpower will be n, otherwise it will be -1. | |
| 1022 int bpower = b._powerOfTwo(); | |
| 1023 | |
| 1024 // True if the original value of a is negative. | |
| 1025 bool aIsNegative = false; | |
| 1026 // True if the original value of a is int64.MIN_VALUE. | |
| 1027 bool aIsMinValue = false; | |
| 1028 | |
| 1029 /* | |
| 1030 * Normalize a to a positive value, keeping track of the sign change in | |
| 1031 * 'negative' (which tracks the sign of both a and b and is used to | |
| 1032 * determine the sign of the quotient) and 'aIsNegative' (which is used to | |
| 1033 * determine the sign of the remainder). | |
| 1034 * | |
| 1035 * For all values of a except MIN_VALUE, we can just negate a and modify | |
| 1036 * negative and aIsNegative appropriately. When a == MIN_VALUE, negation is | |
| 1037 * not possible without overflowing 64 bits, so instead of computing | |
| 1038 * abs(MIN_VALUE) / abs(b) we compute (abs(MIN_VALUE) - 1) / abs(b). The | |
| 1039 * only circumstance under which these quotients differ is when b is a power | |
| 1040 * of two, which will divide abs(MIN_VALUE) == 2^64 exactly. In this case, | |
| 1041 * we can get the proper result by shifting MIN_VALUE in unsigned fashion. | |
| 1042 * | |
| 1043 * We make a single copy of a before the first operation that needs to | |
| 1044 * modify its value. | |
| 1045 */ | |
| 1046 bool aIsCopy = false; | |
| 1047 if (a.isMinValue) { | |
| 1048 aIsMinValue = true; | |
| 1049 aIsNegative = true; | |
| 1050 // If b is not a power of two, treat -a as MAX_VALUE (instead of the | |
| 1051 // actual value (MAX_VALUE + 1)). | |
| 1052 if (bpower == -1) { | |
| 1053 a = new int64._copy(MAX_VALUE); | |
| 1054 aIsCopy = true; | |
| 1055 negative = !negative; | |
| 1056 } else { | |
| 1057 // Signed shift of MIN_VALUE produces the right answer. | |
| 1058 int64 c = a >> bpower; | |
| 1059 if (negative) { | |
| 1060 c._negate(); | |
| 1061 } | |
| 1062 if (computeRemainder) { | |
| 1063 _remainder = ZERO; | |
| 1064 } | |
| 1065 return c; | |
| 1066 } | |
| 1067 } else if (a.isNegative) { | |
| 1068 aIsNegative = true; | |
| 1069 a = -a; | |
| 1070 aIsCopy = true; | |
| 1071 negative = !negative; | |
| 1072 } | |
| 1073 | |
| 1074 // Now both a and b are non-negative. | |
| 1075 // If b is a power of two, just shift. | |
| 1076 if (bpower != -1) { | |
| 1077 return _divModByShift(a, bpower, negative, aIsCopy, aIsNegative, | |
| 1078 computeRemainder); | |
| 1079 } | |
| 1080 | |
| 1081 // If a < b, the quotient is 0 and the remainder is a. | |
| 1082 if (a < b) { | |
| 1083 if (computeRemainder) { | |
| 1084 if (aIsNegative) { | |
| 1085 _remainder = -a; | |
| 1086 } else { | |
| 1087 _remainder = aIsCopy ? a : new int64._copy(a); | |
| 1088 } | |
| 1089 } | |
| 1090 return ZERO; | |
| 1091 } | |
| 1092 | |
| 1093 // Generate the quotient using bit-at-a-time long division. | |
| 1094 return _divModHelper(aIsCopy ? a : new int64._copy(a), b, negative, | |
| 1095 aIsNegative, aIsMinValue, computeRemainder); | |
| 1096 } | |
| 1097 } | |
| OLD | NEW |