| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 // Copyright 2009 The Go Authors. All rights reserved. | 5 // Copyright 2009 The Go Authors. All rights reserved. |
| 6 // Use of this source code is governed by a BSD-style | 6 // Use of this source code is governed by a BSD-style |
| 7 // license that can be found in the LICENSE file. | 7 // license that can be found in the LICENSE file. |
| 8 | 8 |
| 9 /* | 9 /* |
| 10 * Copyright (c) 2003-2005 Tom Wu | 10 * Copyright (c) 2003-2005 Tom Wu |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 // Digit conversion table for parsing. | 64 // Digit conversion table for parsing. |
| 65 static final Map<int, int> DIGIT_TABLE = _createDigitTable(); | 65 static final Map<int, int> DIGIT_TABLE = _createDigitTable(); |
| 66 | 66 |
| 67 // Internal data structure. | 67 // Internal data structure. |
| 68 bool get _neg native "Bigint_getNeg"; | 68 bool get _neg native "Bigint_getNeg"; |
| 69 void set _neg(bool neg) native "Bigint_setNeg"; | 69 void set _neg(bool neg) native "Bigint_setNeg"; |
| 70 int get _used native "Bigint_getUsed"; | 70 int get _used native "Bigint_getUsed"; |
| 71 void set _used(int used) native "Bigint_setUsed"; | 71 void set _used(int used) native "Bigint_setUsed"; |
| 72 Uint32List get _digits native "Bigint_getDigits"; | 72 Uint32List get _digits native "Bigint_getDigits"; |
| 73 void set _digits(Uint32List digits) native "Bigint_setDigits"; | 73 void set _digits(Uint32List digits) { |
| 74 // The VM expects digits_ to be a Uint32List. |
| 75 assert(digits != null); |
| 76 _set_digits(digits); |
| 77 } |
| 78 |
| 79 void _set_digits(Uint32List digits) native "Bigint_setDigits"; |
| 74 | 80 |
| 75 // Factory returning an instance initialized to value 0. | 81 // Factory returning an instance initialized to value 0. |
| 76 factory _Bigint() native "Bigint_allocate"; | 82 factory _Bigint() native "Bigint_allocate"; |
| 77 | 83 |
| 78 // Factory returning an instance initialized to an integer value. | 84 // Factory returning an instance initialized to an integer value. |
| 79 factory _Bigint._fromInt(int i) { | 85 factory _Bigint._fromInt(int i) { |
| 80 return new _Bigint()._setInt(i); | 86 return new _Bigint()._setInt(i); |
| 81 } | 87 } |
| 82 | 88 |
| 83 // Factory returning an instance initialized to a hex string. | 89 // Factory returning an instance initialized to a hex string. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 200 } |
| 195 | 201 |
| 196 // Conversion from int to bigint. | 202 // Conversion from int to bigint. |
| 197 _Bigint _toBigint() => this; | 203 _Bigint _toBigint() => this; |
| 198 | 204 |
| 199 // Make sure at least 'length' _digits are allocated. | 205 // Make sure at least 'length' _digits are allocated. |
| 200 // Copy existing _digits if reallocation is necessary. | 206 // Copy existing _digits if reallocation is necessary. |
| 201 // TODO(regis): Check that we are not preserving _digits unnecessarily. | 207 // TODO(regis): Check that we are not preserving _digits unnecessarily. |
| 202 void _ensureLength(int length) { | 208 void _ensureLength(int length) { |
| 203 var digits = _digits; | 209 var digits = _digits; |
| 204 if (length > 0 && (digits == null || length > digits.length)) { | 210 if (length > 0 && (length > digits.length)) { |
| 205 var new_digits = new Uint32List(length + EXTRA_DIGITS); | 211 var new_digits = new Uint32List(length + EXTRA_DIGITS); |
| 206 if (digits != null) { | 212 for (var i = _used; --i >= 0; ) { |
| 207 for (var i = _used; --i >= 0; ) { | 213 new_digits[i] = digits[i]; |
| 208 new_digits[i] = digits[i]; | |
| 209 } | |
| 210 } | 214 } |
| 211 _digits = new_digits; | 215 _digits = new_digits; |
| 212 } | 216 } |
| 213 } | 217 } |
| 214 | 218 |
| 215 // Clamp off excess high _digits. | 219 // Clamp off excess high _digits. |
| 216 void _clamp() { | 220 void _clamp() { |
| 217 var digits = _digits; | 221 var digits = _digits; |
| 218 while (_used > 0 && digits[_used - 1] == 0) { | 222 while (_used > 0 && digits[_used - 1] == 0) { |
| 219 --_used; | 223 --_used; |
| (...skipping 760 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 980 if (_neg) return (~this).bitLength; | 984 if (_neg) return (~this).bitLength; |
| 981 return DIGIT_BITS*(_used - 1) + _nbits(_digits[_used - 1]); | 985 return DIGIT_BITS*(_used - 1) + _nbits(_digits[_used - 1]); |
| 982 } | 986 } |
| 983 | 987 |
| 984 // This method must support smi._toBigint()._shrFromInt(int). | 988 // This method must support smi._toBigint()._shrFromInt(int). |
| 985 int _shrFromInt(int other) { | 989 int _shrFromInt(int other) { |
| 986 if (_used == 0) return other; // Shift amount is zero. | 990 if (_used == 0) return other; // Shift amount is zero. |
| 987 if (_neg) throw "negative shift amount"; // TODO(regis): What exception? | 991 if (_neg) throw "negative shift amount"; // TODO(regis): What exception? |
| 988 assert(DIGIT_BITS == 32); // Otherwise this code needs to be revised. | 992 assert(DIGIT_BITS == 32); // Otherwise this code needs to be revised. |
| 989 var shift; | 993 var shift; |
| 990 if (_used > 2 || (_used == 2 && _digits[1] > 0x10000000)) { | 994 if ((_used > 2) || ((_used == 2) && (_digits[1] > 0x10000000))) { |
| 991 if (other < 0) { | 995 if (other < 0) { |
| 992 return -1; | 996 return -1; |
| 993 } else { | 997 } else { |
| 994 return 0; | 998 return 0; |
| 995 } | 999 } |
| 996 } else { | 1000 } else { |
| 997 shift = ((_used == 2) ? (_digits[1] << DIGIT_BITS) : 0) + _digits[0]; | 1001 shift = ((_used == 2) ? (_digits[1] << DIGIT_BITS) : 0) + _digits[0]; |
| 998 } | 1002 } |
| 999 _Bigint result = new _Bigint(); | 1003 _Bigint result = new _Bigint(); |
| 1000 other._toBigint()._rShiftTo(shift, result); | 1004 other._toBigint()._rShiftTo(shift, result); |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1405 void _sqrTo(_Bigint x, _Bigint r) { | 1409 void _sqrTo(_Bigint x, _Bigint r) { |
| 1406 x._sqrTo(r); | 1410 x._sqrTo(r); |
| 1407 _reduce(r); | 1411 _reduce(r); |
| 1408 } | 1412 } |
| 1409 | 1413 |
| 1410 void _mulTo(_Bigint x, _Bigint y, _Bigint r) { | 1414 void _mulTo(_Bigint x, _Bigint y, _Bigint r) { |
| 1411 x._mulTo(y, r); | 1415 x._mulTo(y, r); |
| 1412 _reduce(r); | 1416 _reduce(r); |
| 1413 } | 1417 } |
| 1414 } | 1418 } |
| OLD | NEW |