Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 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 Loading... | |
| 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 Loading... | |
| 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 |
| 268 Int64 result = new Int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK2); | 228 return new Int64._bits(_MASK & sum0, _MASK & sum1, _MASK2 & sum2); |
|
Chris Bracken
2013/09/17 01:04:11
return _masked(sum0, sum1, sum2)
sra1
2013/09/17 05:04:36
Done.
| |
| 269 return result; | |
| 270 } | 229 } |
| 271 | 230 |
| 272 Int64 operator -(other) { | 231 Int64 operator -(other) { |
| 273 Int64 o = _promote(other); | 232 Int64 o = _promote(other); |
| 274 int sum0 = _l - o._l; | 233 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 } | 234 } |
| 281 | 235 |
| 282 Int64 operator -() { | 236 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 | 237 |
| 291 Int64 operator *(other) { | 238 Int64 operator *(other) { |
| 292 Int64 o = _promote(other); | 239 Int64 o = _promote(other); |
| 293 | 240 |
| 294 // Grab 13-bit chunks. | 241 // Grab 13-bit chunks. |
| 295 int a0 = _l & 0x1fff; | 242 int a0 = _l & 0x1fff; |
| 296 int a1 = (_l >> 13) | ((_m & 0xf) << 9); | 243 int a1 = (_l >> 13) | ((_m & 0xf) << 9); |
| 297 int a2 = (_m >> 4) & 0x1fff; | 244 int a2 = (_m >> 4) & 0x1fff; |
| 298 int a3 = (_m >> 17) | ((_h & 0xff) << 5); | 245 int a3 = (_m >> 17) | ((_h & 0xff) << 5); |
| 299 int a4 = (_h & 0xfff00) >> 8; | 246 int a4 = (_h & 0xfff00) >> 8; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 // Propagate high bits from c0 -> c1, c1 -> c2. | 312 // Propagate high bits from c0 -> c1, c1 -> c2. |
| 366 c1 += c0 >> _BITS; | 313 c1 += c0 >> _BITS; |
| 367 c0 &= _MASK; | 314 c0 &= _MASK; |
| 368 c2 += c1 >> _BITS; | 315 c2 += c1 >> _BITS; |
| 369 c1 &= _MASK; | 316 c1 &= _MASK; |
| 370 c2 &= _MASK2; | 317 c2 &= _MASK2; |
| 371 | 318 |
| 372 return new Int64._bits(c0, c1, c2); | 319 return new Int64._bits(c0, c1, c2); |
| 373 } | 320 } |
| 374 | 321 |
| 375 Int64 operator %(other) { | 322 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 | 323 |
| 387 Int64 operator ~/(other) => _divMod(this, _promote(other), false); | 324 Int64 operator ~/(other) => _divide(this, other, _RETURN_DIV); |
| 388 | 325 |
| 389 // Int64 remainder(other) => this - (this ~/ other) * other; | 326 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 | 327 |
| 399 Int64 operator &(other) { | 328 Int64 operator &(other) { |
| 400 Int64 o = _promote(other); | 329 Int64 o = _promote(other); |
| 401 int a0 = _l & o._l; | 330 int a0 = _l & o._l; |
| 402 int a1 = _m & o._m; | 331 int a1 = _m & o._m; |
| 403 int a2 = _h & o._h; | 332 int a2 = _h & o._h; |
| 404 return new Int64._bits(a0, a1, a2); | 333 return new Int64._bits(a0, a1, a2); |
| 405 } | 334 } |
| 406 | 335 |
| 407 Int64 operator |(other) { | 336 Int64 operator |(other) { |
| 408 Int64 o = _promote(other); | 337 Int64 o = _promote(other); |
| 409 int a0 = _l | o._l; | 338 int a0 = _l | o._l; |
| 410 int a1 = _m | o._m; | 339 int a1 = _m | o._m; |
| 411 int a2 = _h | o._h; | 340 int a2 = _h | o._h; |
| 412 return new Int64._bits(a0, a1, a2); | 341 return new Int64._bits(a0, a1, a2); |
| 413 } | 342 } |
| 414 | 343 |
| 415 Int64 operator ^(other) { | 344 Int64 operator ^(other) { |
| 416 Int64 o = _promote(other); | 345 Int64 o = _promote(other); |
| 417 int a0 = _l ^ o._l; | 346 int a0 = _l ^ o._l; |
| 418 int a1 = _m ^ o._m; | 347 int a1 = _m ^ o._m; |
| 419 int a2 = _h ^ o._h; | 348 int a2 = _h ^ o._h; |
| 420 return new Int64._bits(a0, a1, a2); | 349 return new Int64._bits(a0, a1, a2); |
| 421 } | 350 } |
| 422 | 351 |
| 423 Int64 operator ~() { | 352 Int64 operator ~() { |
| 424 var result = new Int64._bits((~_l) & _MASK, (~_m) & _MASK, (~_h) & _MASK2); | 353 return new Int64._bits(_MASK & ~_l, _MASK & ~_m, _MASK2 & ~_h); |
|
Chris Bracken
2013/09/17 01:04:11
_masked(~l,...)
sra1
2013/09/17 05:04:36
Done.
| |
| 425 return result; | |
| 426 } | 354 } |
| 427 | 355 |
| 428 Int64 operator <<(int n) { | 356 Int64 operator <<(int n) { |
| 429 if (n < 0) { | 357 if (n < 0) { |
| 430 throw new ArgumentError(n); | 358 throw new ArgumentError(n); |
| 431 } | 359 } |
| 432 n &= 63; | 360 n &= 63; |
| 433 | 361 |
| 434 int res0, res1, res2; | 362 int res0, res1, res2; |
| 435 if (n < _BITS) { | 363 if (n < _BITS) { |
| 436 res0 = _l << n; | 364 res0 = _l << n; |
| 437 res1 = (_m << n) | (_l >> (_BITS - n)); | 365 res1 = (_m << n) | (_l >> (_BITS - n)); |
| 438 res2 = (_h << n) | (_m >> (_BITS - n)); | 366 res2 = (_h << n) | (_m >> (_BITS - n)); |
| 439 } else if (n < _BITS01) { | 367 } else if (n < _BITS01) { |
| 440 res0 = 0; | 368 res0 = 0; |
| 441 res1 = _l << (n - _BITS); | 369 res1 = _l << (n - _BITS); |
| 442 res2 = (_m << (n - _BITS)) | (_l >> (_BITS01 - n)); | 370 res2 = (_m << (n - _BITS)) | (_l >> (_BITS01 - n)); |
| 443 } else { | 371 } else { |
| 444 res0 = 0; | 372 res0 = 0; |
| 445 res1 = 0; | 373 res1 = 0; |
| 446 res2 = _l << (n - _BITS01); | 374 res2 = _l << (n - _BITS01); |
| 447 } | 375 } |
| 448 | 376 |
| 449 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK2); | 377 return new Int64._bits(_MASK & res0, _MASK & res1, _MASK2 & res2); |
|
Chris Bracken
2013/09/17 01:04:11
_masked(res0,...)
sra1
2013/09/17 05:04:36
Done.
| |
| 450 } | 378 } |
| 451 | 379 |
| 452 Int64 operator >>(int n) { | 380 Int64 operator >>(int n) { |
| 453 if (n < 0) { | 381 if (n < 0) { |
| 454 throw new ArgumentError(n); | 382 throw new ArgumentError(n); |
| 455 } | 383 } |
| 456 n &= 63; | 384 n &= 63; |
| 457 | 385 |
| 458 int res0, res1, res2; | 386 int res0, res1, res2; |
| 459 | 387 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 480 res0 = _shiftRight(_m, n - _BITS) | (a2 << (_BITS01 - n)); | 408 res0 = _shiftRight(_m, n - _BITS) | (a2 << (_BITS01 - n)); |
| 481 } else { | 409 } else { |
| 482 res2 = negative ? _MASK2 : 0; | 410 res2 = negative ? _MASK2 : 0; |
| 483 res1 = negative ? _MASK : 0; | 411 res1 = negative ? _MASK : 0; |
| 484 res0 = _shiftRight(a2, n - _BITS01); | 412 res0 = _shiftRight(a2, n - _BITS01); |
| 485 if (negative) { | 413 if (negative) { |
| 486 res0 |= _MASK & ~(_MASK >> (n - _BITS01)); | 414 res0 |= _MASK & ~(_MASK >> (n - _BITS01)); |
| 487 } | 415 } |
| 488 } | 416 } |
| 489 | 417 |
| 490 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK2); | 418 return new Int64._bits(_MASK & res0, _MASK & res1, _MASK2 & res2); |
|
Chris Bracken
2013/09/17 01:04:11
_masked(res0,...)
sra1
2013/09/17 05:04:36
Done.
| |
| 491 } | 419 } |
| 492 | 420 |
| 493 Int64 shiftRightUnsigned(int n) { | 421 Int64 shiftRightUnsigned(int n) { |
| 494 if (n < 0) { | 422 if (n < 0) { |
| 495 throw new ArgumentError(n); | 423 throw new ArgumentError(n); |
| 496 } | 424 } |
| 497 n &= 63; | 425 n &= 63; |
| 498 | 426 |
| 499 int res0, res1, res2; | 427 int res0, res1, res2; |
| 500 int a2 = _h & _MASK2; // Ensure a2 is positive. | 428 int a2 = _MASK2 & _h; // Ensure a2 is positive. |
| 501 if (n < _BITS) { | 429 if (n < _BITS) { |
| 502 res2 = a2 >> n; | 430 res2 = a2 >> n; |
| 503 res1 = (_m >> n) | (a2 << (_BITS - n)); | 431 res1 = (_m >> n) | (a2 << (_BITS - n)); |
| 504 res0 = (_l >> n) | (_m << (_BITS - n)); | 432 res0 = (_l >> n) | (_m << (_BITS - n)); |
| 505 } else if (n < _BITS01) { | 433 } else if (n < _BITS01) { |
| 506 res2 = 0; | 434 res2 = 0; |
| 507 res1 = a2 >> (n - _BITS); | 435 res1 = a2 >> (n - _BITS); |
| 508 res0 = (_m >> (n - _BITS)) | (_h << (_BITS01 - n)); | 436 res0 = (_m >> (n - _BITS)) | (_h << (_BITS01 - n)); |
| 509 } else { | 437 } else { |
| 510 res2 = 0; | 438 res2 = 0; |
| 511 res1 = 0; | 439 res1 = 0; |
| 512 res0 = a2 >> (n - _BITS01); | 440 res0 = a2 >> (n - _BITS01); |
| 513 } | 441 } |
| 514 | 442 |
| 515 return new Int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK2); | 443 return new Int64._bits(_MASK & res0, _MASK & res1, _MASK2 & res2); |
|
Chris Bracken
2013/09/17 01:04:11
_masked(res0,...)
sra1
2013/09/17 05:04:36
Done.
| |
| 516 } | 444 } |
| 517 | 445 |
| 518 /** | 446 /** |
| 519 * Returns [true] if this [Int64] has the same numeric value as the | 447 * Returns [true] if this [Int64] has the same numeric value as the |
| 520 * given object. The argument may be an [int] or an [IntX]. | 448 * given object. The argument may be an [int] or an [IntX]. |
| 521 */ | 449 */ |
| 522 bool operator ==(other) { | 450 bool operator ==(other) { |
| 523 Int64 o; | 451 Int64 o; |
| 524 if (other is Int64) { | 452 if (other is Int64) { |
| 525 o = other; | 453 o = other; |
| 526 } else if (other is int) { | 454 } else if (other is int) { |
| 455 if (_h == 0 && _m == 0) return _l == other; | |
| 527 o = new Int64.fromInt(other); | 456 o = new Int64.fromInt(other); |
| 528 } else if (other is Int32) { | 457 } else if (other is Int32) { |
| 529 o = other.toInt64(); | 458 o = other.toInt64(); |
| 530 } | 459 } |
| 531 if (o != null) { | 460 if (o != null) { |
| 532 return _l == o._l && _m == o._m && _h == o._h; | 461 return _l == o._l && _m == o._m && _h == o._h; |
| 533 } | 462 } |
| 534 return false; | 463 return false; |
| 535 } | 464 } |
| 536 | 465 |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 847 33 * 33 * 33, | 776 33 * 33 * 33, |
| 848 34 * 34 * 34, | 777 34 * 34 * 34, |
| 849 35 * 35 * 35, | 778 35 * 35 * 35, |
| 850 36 * 36 * 36 | 779 36 * 36 * 36 |
| 851 ]; | 780 ]; |
| 852 | 781 |
| 853 String toDebugString() { | 782 String toDebugString() { |
| 854 return "Int64[_l=$_l, _m=$_m, _h=$_h]"; | 783 return "Int64[_l=$_l, _m=$_m, _h=$_h]"; |
| 855 } | 784 } |
| 856 | 785 |
| 857 /** | 786 |
| 858 * Constructs an [Int64] with a given bitwise representation. No validation | 787 static Int64 _masked(int a0, int a1, int a2) => |
| 859 * is performed. | 788 new Int64._bits(_MASK & a0, _MASK & a1, _MASK2 & a2); |
| 860 */ | 789 |
| 861 Int64._bits(int this._l, int this._m, int this._h); | 790 static Int64 _sub(int a0, int a1, int a2, int b0, int b1, int b2) { |
| 791 int diff0 = a0 - b0; | |
| 792 int diff1 = a1 - b1 - ((diff0 >> _BITS) & 1); | |
| 793 int diff2 = a2 - b2 - ((diff1 >> _BITS) & 1); | |
| 794 return _masked(diff0, diff1, diff2); | |
| 795 } | |
| 796 | |
| 797 static Int64 _negate(int b0, int b1, int b2) { | |
| 798 return _sub(0, 0, 0, b0, b1, b2); | |
| 799 } | |
| 862 | 800 |
| 863 /** | 801 /** |
| 864 * Constructs an [Int64] with the same value as an existing [Int64]. | 802 * Constructs an [Int64] with the same value as an existing [Int64]. |
| 865 */ | 803 */ |
| 866 Int64._copy(Int64 other) | 804 Int64._copy(Int64 other) |
| 867 : _l = other._l, | 805 : _l = other._l, |
| 868 _m = other._m, | 806 _m = other._m, |
| 869 _h = other._h; | 807 _h = other._h; |
| 870 | 808 |
| 871 // Determine whether the platform supports ints greater than 2^53 | 809 // Determine whether the platform supports ints greater than 2^53 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 883 var same = y == x; | 821 var same = y == x; |
| 884 _haveBigIntsCached = !same; | 822 _haveBigIntsCached = !same; |
| 885 } | 823 } |
| 886 return _haveBigIntsCached; | 824 return _haveBigIntsCached; |
| 887 } | 825 } |
| 888 | 826 |
| 889 String _hexDigit(int digit) => "0123456789ABCDEF"[digit]; | 827 String _hexDigit(int digit) => "0123456789ABCDEF"[digit]; |
| 890 | 828 |
| 891 // Implementation of '~/' and '%'. | 829 // Implementation of '~/' and '%'. |
| 892 | 830 |
| 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 | 831 |
| 926 // Work around dart2js bugs with negative arguments to '>>' operator. | 832 // Work around dart2js bugs with negative arguments to '>>' operator. |
| 927 static int _shiftRight(int x, int n) { | 833 static int _shiftRight(int x, int n) { |
| 928 if (x >= 0) { | 834 if (x >= 0) { |
| 929 return x >> n; | 835 return x >> n; |
| 930 } else { | 836 } else { |
| 931 int shifted = x >> n; | 837 int shifted = x >> n; |
| 932 if (shifted >= 0x80000000) { | 838 if (shifted >= 0x80000000) { |
| 933 shifted -= 4294967296; | 839 shifted -= 4294967296; |
| 934 } | 840 } |
| 935 return shifted; | 841 return shifted; |
| 936 } | 842 } |
| 937 } | 843 } |
| 938 | 844 |
| 939 /** | 845 |
| 940 * Attempt to subtract b from a if a >= b: | 846 static Int64 _divide(Int64 a, other, int what) { |
| 941 * | 847 Int64 b = _promote(other); |
| 942 * if (a >= b) { | |
| 943 * a -= b; | |
| 944 * return true; | |
| 945 * } else { | |
| 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 } | |
| 956 | |
| 957 int sum0 = a._l - b._l; | |
| 958 int sum1 = a._m - b._m + _shiftRight(sum0, _BITS); | |
| 959 sum2 += _shiftRight(sum1, _BITS); | |
| 960 | |
| 961 if (sum2 < 0) { | |
| 962 return false; | |
| 963 } | |
| 964 | |
| 965 a._l = sum0 & _MASK; | |
| 966 a._m = sum1 & _MASK; | |
| 967 a._h = sum2 & _MASK2; | |
| 968 | |
| 969 return true; | |
| 970 } | |
| 971 | |
| 972 // Note: mutates [a] via _trialSubtract. | |
| 973 static Int64 _divModHelper(Int64 a, Int64 b, | |
| 974 bool negative, bool aIsNegative, bool aIsMinValue, | |
| 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 | |
| 980 // Quotient must be a new instance since we mutate it. | |
| 981 Int64 quotient = new Int64(); | |
| 982 while (shift >= 0) { | |
| 983 bool gte = _trialSubtract(a, bshift); | |
| 984 if (gte) { | |
| 985 quotient._setBit(shift); | |
| 986 if (a.isZero) { | |
| 987 break; | |
| 988 } | |
| 989 } | |
| 990 | |
| 991 bshift._toShru1(); | |
| 992 shift--; | |
| 993 } | |
| 994 | |
| 995 if (negative) { | |
| 996 quotient._negate(); | |
| 997 } | |
| 998 | |
| 999 if (computeRemainder) { | |
| 1000 if (aIsNegative) { | |
| 1001 _remainder = -a; | |
| 1002 if (aIsMinValue) { | |
| 1003 _remainder = _remainder - ONE; | |
| 1004 } | |
| 1005 } else { | |
| 1006 _remainder = a; | |
| 1007 } | |
| 1008 } | |
| 1009 | |
| 1010 return quotient; | |
| 1011 } | |
| 1012 | |
| 1013 Int64 _divModByMinValue(bool computeRemainder) { | |
| 1014 // MIN_VALUE / MIN_VALUE == 1, remainder = 0 | |
| 1015 // (x != MIN_VALUE) / MIN_VALUE == 0, remainder == x | |
| 1016 if (isMinValue) { | |
| 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 } | |
| 1046 | |
| 1047 _l = b0; | |
| 1048 _m = b1; | |
| 1049 _h = b2; | |
| 1050 } | |
| 1051 | |
| 1052 static Int64 _divModByShift(Int64 a, int bpower, bool negative, bool aIsCopy, | |
| 1053 bool aIsNegative, bool computeRemainder) { | |
| 1054 Int64 c = a >> bpower; | |
| 1055 if (negative) { | |
| 1056 c._negate(); | |
| 1057 } | |
| 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) { | 848 if (b.isZero) { |
| 1107 throw new IntegerDivisionByZeroException(); | 849 throw new IntegerDivisionByZeroException(); |
| 1108 } | 850 } |
| 1109 if (a.isZero) { | 851 if (a.isZero) return ZERO; |
| 1110 if (computeRemainder) { | 852 |
| 1111 _remainder = ZERO; | 853 bool aNeg = a.isNegative; |
| 854 bool bNeg = b.isNegative; | |
| 855 a = a.abs(); | |
| 856 b = b.abs(); | |
| 857 | |
| 858 int a0 = a._l; | |
| 859 int a1 = a._m; | |
| 860 int a2 = a._h; | |
| 861 | |
| 862 int b0 = b._l; | |
| 863 int b1 = b._m; | |
| 864 int b2 = b._h; | |
| 865 return _divideHelper(a0, a1, a2, aNeg, b0, b1, b2, bNeg, what); | |
| 866 } | |
| 867 | |
| 868 static const _RETURN_DIV = 1; | |
| 869 static const _RETURN_REM = 2; | |
| 870 static const _RETURN_MOD = 3; | |
| 871 | |
| 872 static _divideHelper( | |
| 873 // up to 64 bits unsigned in a2/a1/a0 and b2/b1/b0 | |
| 874 int a0, int a1, int a2, bool aNeg, // input A. | |
| 875 int b0, int b1, int b2, bool bNeg, // input B. | |
| 876 int what) { | |
| 877 int q0 = 0, q1 = 0, q2 = 0; // result Q. | |
| 878 int r0 = 0, r1 = 0, r2 = 0; // result R. | |
| 879 | |
| 880 val(x0,x1,x2) => x0 + 4194304 * x1 + 17592186044416 * x2; | |
| 881 demoS(x0,x1,x2) => '$x0 $x1 $x2 (${new Int64._bits(x0,x1,x2)})'; | |
| 882 demo(x0,x1,x2) => '$x0 $x1 $x2 (${val(x0,x1,x2)})'; | |
| 883 | |
| 884 var splat; | |
| 885 splat = () { | |
| 886 print('divide A ${demo(a0,a1,a2)} B ${demo(b0,b1,b2)}'); | |
| 887 splat = (){}; | |
| 888 }; | |
|
Chris Bracken
2013/09/17 01:04:11
delete test code/prints
sra1
2013/09/17 05:04:36
Done.
| |
| 889 | |
| 890 if (b2 == 0 && b1 == 0 && b0 < (1 << (30 - _BITS))) { | |
| 891 // Small divisor can be handled by single-digit division within Smi range. | |
| 892 // | |
| 893 // Handling small divisors here helps the estimate version below by | |
| 894 // handling cases where the estimate is off by more than a small amount. | |
| 895 | |
| 896 q2 = a2 ~/ b0; | |
| 897 int carry = a2 - q2 * b0; | |
| 898 int d1 = a1 + (carry << _BITS); | |
| 899 q1 = d1 ~/ b0; | |
| 900 carry = d1 - q1 * b0; | |
| 901 int d0 = a0 + (carry << _BITS); | |
| 902 q0 = d0 ~/ b0; | |
| 903 r0 = d0 - q0 * b0; | |
| 904 } else { | |
| 905 // Approximate Q = A ~/ B and R = A - Q * B using doubles. | |
| 906 | |
| 907 // The floating point approximation is very close to the correct value | |
| 908 // when floor(A/B) fits in fewer that 53 bits. | |
| 909 | |
| 910 // We use double arithmetic for intermediate values. Double arithmetic on | |
| 911 // non-negative values is exact under the following conditions: | |
| 912 // | |
| 913 // - The values are integer values that fit in 53 bits. | |
| 914 // - Dividing by powers of two (adjusts exponent only). | |
| 915 // - Floor (zeroes bits with fractional weight). | |
| 916 | |
| 917 const double K2 = 17592186044416.0; // 2^44 | |
| 918 const double K1 = 4194304.0; // 2^22 | |
| 919 | |
| 920 // Approximate double values for [a] and [b]. | |
| 921 double ad = a0 + K1 * a1 + K2 * a2; | |
| 922 double bd = b0 + K1 * b1 + K2 * b2; | |
| 923 // Approximate quotient. | |
| 924 double qd = (ad / bd).floorToDouble(); | |
| 925 | |
| 926 // Extract components of [qd] using double arithmetic. | |
| 927 double q2d = (qd / K2).floorToDouble(); | |
| 928 qd = qd - K2 * q2d; | |
| 929 double q1d = (qd / K1).floorToDouble(); | |
| 930 double q0d = qd - K1 * q1d; | |
| 931 q2 = q2d.toInt(); | |
| 932 q1 = q1d.toInt(); | |
| 933 q0 = q0d.toInt(); | |
| 934 | |
| 935 assert(q0 + K1 * q1 + K2 * q2 == (ad / bd).floorToDouble()); | |
| 936 assert(q2 == 0 || b2 == 0); // Q and B can't both be big since Q*B <= A. | |
| 937 | |
| 938 // P = Q * B, using doubles to hold intermediates. | |
| 939 // We don't need all partial sums since Q*B <= A. | |
| 940 double p0d = q0d * b0; | |
| 941 double p0carry = (p0d / K1).floorToDouble(); | |
| 942 p0d = p0d - p0carry * K1; | |
| 943 double p1d = q1d * b0 + q0d * b1 + p0carry; | |
| 944 double p1carry = (p1d / K1).floorToDouble(); | |
| 945 p1d = p1d - p1carry * K1; | |
| 946 double p2d = q2d * b0 + q1d * b1 + q0d * b2 + p1carry; | |
| 947 assert(p2d <= _MASK2); // No partial sum overflow. | |
| 948 | |
| 949 // R = A - P | |
| 950 int diff0 = a0 - p0d.toInt(); | |
| 951 int diff1 = a1 - p1d.toInt() - ((diff0 >> _BITS) & 1); | |
| 952 int diff2 = a2 - p2d.toInt() - ((diff1 >> _BITS) & 1); | |
| 953 r0 = _MASK & diff0; | |
| 954 r1 = _MASK & diff1; | |
| 955 r2 = _MASK2 & diff2; | |
| 956 | |
| 957 // A = Q * B + R | |
| 958 if (new Int64._bits(a0, a1, a2) != | |
| 959 new Int64._bits(q0, q1, q2) * new Int64._bits(b0, b1, b2) + | |
| 960 new Int64._bits(r0, r1, r2)) { | |
| 961 | |
| 962 splat(); | |
| 963 | |
| 964 print('A($a0, $a1, $a2) == ' | |
| 965 'Q($q0, $q1, $q2) * B($b0, $b1, $b2)} + R($r0, $r1, $r2)}'); | |
| 966 print('${new Int64._bits(a0, a1, a2)} == ' | |
| 967 '${new Int64._bits(q0, q1, q2)} * ${new Int64._bits(b0, b1, b2)} + ' | |
| 968 '${new Int64._bits(r0, r1, r2)}'); | |
| 969 print('${new Int64._bits(a0, a1, a2)} == \n' | |
| 970 '${new Int64._bits(q0, q1, q2) * new Int64._bits(b0, b1, b2)} + ' | |
| 971 '${new Int64._bits(r0, r1, r2)}'); | |
| 972 | |
| 973 print('${val(a0, a1, a2)} == \n' | |
| 974 '${val(q0, q1, q2) * val(b0, b1, b2)} + ' | |
| 975 '${val(r0, r1, r2)}'); | |
| 976 | |
| 977 assert(new Int64._bits(a0, a1, a2) == | |
| 978 new Int64._bits(q0, q1, q2) * new Int64._bits(b0, b1, b2) + | |
| 979 new Int64._bits(r0, r1, r2)); | |
| 1112 } | 980 } |
| 1113 return ZERO; | 981 |
| 1114 } | 982 // while (R < 0 || R >= B) |
| 1115 // MIN_VALUE / MIN_VALUE = 1, anything other a / MIN_VALUE is 0. | 983 // adjust R towards [0, B) |
| 1116 if (b.isMinValue) { | 984 int tc = 1; |
| 1117 return a._divModByMinValue(computeRemainder); | 985 while ( |
| 1118 } | 986 r2 >= _SIGN_BIT_MASK || |
| 1119 // Normalize b to abs(b), keeping track of the parity in 'negative'. | 987 r2 > b2 || |
| 1120 // We can do this because we have already ensured that b != MIN_VALUE. | 988 (r2 == b2 && (r1 > b1 || (r1 == b1 && r0 >= b0)))) { |
| 1121 bool negative = false; | 989 splat(); |
| 1122 if (b.isNegative) { | 990 print(' adjust $tc R ${demoS(r0,r1,r2)} >= B ${demo(b0,b1,b2)}' |
| 1123 b = -b; | 991 ' Q ${demo(q0,q1,q2)}'); |
| 1124 negative = !negative; | 992 if (++tc > 10) throw 123; |
| 1125 } | 993 // Direction multiplier for adjustment. |
| 1126 // If b == 2^n, bpower will be n, otherwise it will be -1. | 994 int m = (r2 & _SIGN_BIT_MASK) == 0 ? 1 : -1; |
| 1127 int bpower = b._powerOfTwo(); | 995 // R = R - B or R = R + B |
| 1128 | 996 int d0 = r0 - m * b0; |
| 1129 // True if the original value of a is negative. | 997 int d1 = r1 - m * (b1 + ((d0 >> _BITS) & 1)); |
| 1130 bool aIsNegative = false; | 998 int d2 = r2 - m * (b2 + ((d1 >> _BITS) & 1)); |
| 1131 // True if the original value of a is Int64.MIN_VALUE. | 999 r0 = _MASK & d0; |
| 1132 bool aIsMinValue = false; | 1000 r1 = _MASK & d1; |
| 1133 | 1001 r2 = _MASK2 & d2; |
| 1134 /* | 1002 |
| 1135 * Normalize a to a positive value, keeping track of the sign change in | 1003 // Q = Q + 1 or Q = Q - 1 |
| 1136 * 'negative' (which tracks the sign of both a and b and is used to | 1004 d0 = q0 + m; |
| 1137 * determine the sign of the quotient) and 'aIsNegative' (which is used to | 1005 d1 = q1 + m * ((d0 >> _BITS) & 1); |
| 1138 * determine the sign of the remainder). | 1006 d2 = q2 + m * ((d1 >> _BITS) & 1); |
| 1139 * | 1007 q0 = _MASK & d0; |
| 1140 * For all values of a except MIN_VALUE, we can just negate a and modify | 1008 q1 = _MASK & d1; |
| 1141 * negative and aIsNegative appropriately. When a == MIN_VALUE, negation is | 1009 q2 = _MASK2 & d2; |
| 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 } | 1010 } |
| 1172 } else if (a.isNegative) { | 1011 } |
| 1173 aIsNegative = true; | 1012 |
| 1174 a = -a; | 1013 // A = Q * B + R |
| 1175 aIsCopy = true; | 1014 if (new Int64._bits(a0, a1, a2) != |
| 1176 negative = !negative; | 1015 new Int64._bits(q0, q1, q2) * new Int64._bits(b0, b1, b2) + |
| 1177 } | 1016 new Int64._bits(r0, r1, r2)) { |
| 1178 | 1017 |
| 1179 // Now both a and b are non-negative. | 1018 splat(); |
| 1180 // If b is a power of two, just shift. | 1019 |
| 1181 if (bpower != -1) { | 1020 print('A($a0, $a1, $a2) == ' |
| 1182 return _divModByShift(a, bpower, negative, aIsCopy, aIsNegative, | 1021 'Q($q0, $q1, $q2) * B($b0, $b1, $b2)} + R($r0, $r1, $r2)}'); |
| 1183 computeRemainder); | 1022 print('${new Int64._bits(a0, a1, a2)} == ' |
| 1184 } | 1023 '${new Int64._bits(q0, q1, q2)} * ${new Int64._bits(b0, b1, b2)} + ' |
| 1185 | 1024 '${new Int64._bits(r0, r1, r2)}'); |
| 1186 // If a < b, the quotient is 0 and the remainder is a. | 1025 print('${new Int64._bits(a0, a1, a2)} == \n' |
| 1187 if (a < b) { | 1026 '${new Int64._bits(q0, q1, q2) * new Int64._bits(b0, b1, b2)} + ' |
| 1188 if (computeRemainder) { | 1027 '${new Int64._bits(r0, r1, r2)}'); |
| 1189 if (aIsNegative) { | 1028 |
| 1190 _remainder = -a; | 1029 print('${val(a0, a1, a2)} == \n' |
| 1191 } else { | 1030 '${val(q0, q1, q2) * val(b0, b1, b2)} + ' |
| 1192 _remainder = aIsCopy ? a : new Int64._copy(a); | 1031 '${val(r0, r1, r2)}'); |
| 1193 } | 1032 |
| 1194 } | 1033 assert(new Int64._bits(a0, a1, a2) == |
| 1195 return ZERO; | 1034 new Int64._bits(q0, q1, q2) * new Int64._bits(b0, b1, b2) + |
| 1196 } | 1035 new Int64._bits(r0, r1, r2)); |
| 1197 | 1036 } |
| 1198 // Generate the quotient using bit-at-a-time long division. | 1037 |
| 1199 return _divModHelper(aIsCopy ? a : new Int64._copy(a), b, negative, | 1038 // 0 <= R < B |
| 1200 aIsNegative, aIsMinValue, computeRemainder); | 1039 assert(Int64.ZERO <= new Int64._bits(r0, r1, r2)); |
| 1040 assert(val(r0, r1, r2) < val(b0, b1, b2)); | |
| 1041 | |
| 1042 assert(what == _RETURN_DIV || what == _RETURN_MOD || what == _RETURN_REM); | |
| 1043 if (what == _RETURN_DIV) { | |
| 1044 if (aNeg != bNeg) return _negate(q0, q1, q2); | |
| 1045 return new Int64._bits(q0, q1, q2); | |
| 1046 } | |
| 1047 | |
| 1048 if (!aNeg) return new Int64._bits(r0, r1, r2); | |
| 1049 | |
| 1050 if (what == _RETURN_MOD) { | |
| 1051 return _sub(b0, b1, b2, r0, r1, r2); | |
|
Chris Bracken
2013/09/17 01:04:11
Return 0 where r1==r2==r3==0
sra1
2013/09/17 19:42:27
Done.
| |
| 1052 } else { | |
| 1053 return _negate(r0, r1, r2); | |
| 1054 } | |
| 1201 } | 1055 } |
| 1202 } | 1056 } |
| OLD | NEW |