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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // Note: Not declared as final in order to satisfy optimizer, which expects | 59 // Note: Not declared as final in order to satisfy optimizer, which expects |
60 // constants to be in canonical form (Smi). | 60 // constants to be in canonical form (Smi). |
61 static _Bigint ZERO = new _Bigint(); | 61 static _Bigint ZERO = new _Bigint(); |
62 static _Bigint ONE = new _Bigint()._setInt(1); | 62 static _Bigint ONE = new _Bigint()._setInt(1); |
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 value) 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 value) native "Bigint_setUsed"; |
72 Uint32List get _digits native "Bigint_getDigits"; | 72 Uint32List get _digits native "Bigint_getDigits"; |
73 void set _digits(Uint32List digits) { | 73 void set _digits(Uint32List digits) { |
74 // The VM expects digits_ to be a Uint32List. | 74 // The VM expects digits_ to be a Uint32List. |
75 assert(digits != null); | 75 assert(digits != null); |
76 _set_digits(digits); | 76 _set_digits(digits); |
77 } | 77 } |
78 | 78 |
79 void _set_digits(Uint32List digits) native "Bigint_setDigits"; | 79 void _set_digits(Uint32List value) native "Bigint_setDigits"; |
80 | 80 |
81 // Factory returning an instance initialized to value 0. | 81 // Factory returning an instance initialized to value 0. |
82 factory _Bigint() native "Bigint_allocate"; | 82 factory _Bigint() native "Bigint_allocate"; |
83 | 83 |
84 // Factory returning an instance initialized to an integer value. | 84 // Factory returning an instance initialized to an integer value. |
85 factory _Bigint._fromInt(int i) { | 85 factory _Bigint._fromInt(int i) { |
86 return new _Bigint()._setInt(i); | 86 return new _Bigint()._setInt(i); |
87 } | 87 } |
88 | 88 |
89 // Factory returning an instance initialized to a hex string. | 89 // Factory returning an instance initialized to a hex string. |
(...skipping 1021 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1111 return r._toValidInt(); | 1111 return r._toValidInt(); |
1112 } | 1112 } |
1113 | 1113 |
1114 int get sign { | 1114 int get sign { |
1115 return (_used == 0) ? 0 : _neg ? -1 : 1; | 1115 return (_used == 0) ? 0 : _neg ? -1 : 1; |
1116 } | 1116 } |
1117 | 1117 |
1118 bool get isEven => _used == 0 || (_digits[0] & 1) == 0; | 1118 bool get isEven => _used == 0 || (_digits[0] & 1) == 0; |
1119 bool get isNegative => _neg; | 1119 bool get isNegative => _neg; |
1120 | 1120 |
| 1121 String _toPow2String(int radix) { |
| 1122 if (_used == 0) return "0"; |
| 1123 assert(radix & (radix - 1) == 0); |
| 1124 final bitsPerChar = radix.bitLength - 1; |
| 1125 final firstcx = _neg ? 1 : 0; // Index of first char in str after the sign. |
| 1126 final lastdx = _used - 1; // Index of last digit in bigint. |
| 1127 final bitLength = lastdx*DIGIT_BITS + _nbits(_digits[lastdx]); |
| 1128 // Index of char in str. Initialize with str length. |
| 1129 var cx = firstcx + (bitLength + bitsPerChar - 1) ~/ bitsPerChar; |
| 1130 _OneByteString str = _OneByteString._allocate(cx); |
| 1131 str._setAt(0, 0x2d); // '-'. Is overwritten if not negative. |
| 1132 final mask = radix - 1; |
| 1133 var dx = 0; // Digit index in bigint. |
| 1134 var bx = 0; // Bit index in bigint digit. |
| 1135 do { |
| 1136 var ch; |
| 1137 if (bx > (DIGIT_BITS - bitsPerChar)) { |
| 1138 ch = _digits[dx++] >> bx; |
| 1139 bx += bitsPerChar - DIGIT_BITS; |
| 1140 if (dx <= lastdx) { |
| 1141 ch |= (_digits[dx] & ((1 << bx) - 1)) << (bitsPerChar - bx); |
| 1142 } |
| 1143 } else { |
| 1144 ch = (_digits[dx] >> bx) & mask; |
| 1145 bx += bitsPerChar; |
| 1146 if (bx >= DIGIT_BITS) { |
| 1147 bx -= DIGIT_BITS; |
| 1148 dx++; |
| 1149 } |
| 1150 } |
| 1151 str._setAt(--cx, _IntegerImplementation._digits.codeUnitAt(ch)); |
| 1152 } while (cx > firstcx); |
| 1153 return str; |
| 1154 } |
| 1155 |
1121 _leftShiftWithMask32(int count, int mask) { | 1156 _leftShiftWithMask32(int count, int mask) { |
1122 if (_used == 0) return 0; | 1157 if (_used == 0) return 0; |
1123 if (count is! _Smi) { | 1158 if (count is! _Smi) { |
1124 _shlFromInt(count); // Throws out of memory exception. | 1159 _shlFromInt(count); // Throws out of memory exception. |
1125 } | 1160 } |
1126 assert(DIGIT_BITS == 32); // Otherwise this code needs to be revised. | 1161 assert(DIGIT_BITS == 32); // Otherwise this code needs to be revised. |
1127 if (count > 31) return 0; | 1162 if (count > 31) return 0; |
1128 return (_digits[0] << count) & mask; | 1163 return (_digits[0] << count) & mask; |
1129 } | 1164 } |
1130 | 1165 |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1441 void _sqrTo(_Bigint x, _Bigint r) { | 1476 void _sqrTo(_Bigint x, _Bigint r) { |
1442 x._sqrTo(r); | 1477 x._sqrTo(r); |
1443 _reduce(r); | 1478 _reduce(r); |
1444 } | 1479 } |
1445 | 1480 |
1446 void _mulTo(_Bigint x, _Bigint y, _Bigint r) { | 1481 void _mulTo(_Bigint x, _Bigint y, _Bigint r) { |
1447 x._mulTo(y, r); | 1482 x._mulTo(y, r); |
1448 _reduce(r); | 1483 _reduce(r); |
1449 } | 1484 } |
1450 } | 1485 } |
OLD | NEW |