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 1100 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 |