Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Unified Diff: runtime/lib/bigint.dart

Issue 723963006: Specialize _toPow2String for bigint to make it linear instead of quadratic. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/bigint.dart
===================================================================
--- runtime/lib/bigint.dart (revision 41724)
+++ runtime/lib/bigint.dart (working copy)
@@ -1118,6 +1118,41 @@
bool get isEven => _used == 0 || (_digits[0] & 1) == 0;
bool get isNegative => _neg;
+ String _toPow2String(int radix) {
+ if (_used == 0) return "0";
+ assert(radix & (radix - 1) == 0);
+ final bitsPerChar = radix.bitLength - 1;
+ final firstcx = _neg ? 1 : 0; // Index of first char in str after the sign.
+ final lastdx = _used - 1; // Index of last digit in bigint.
+ final bitLength = lastdx*DIGIT_BITS + _nbits(_digits[lastdx]);
+ // Index of char in str. Initialize with str length.
+ var cx = firstcx + (bitLength + bitsPerChar - 1) ~/ bitsPerChar;
+ _OneByteString str = _OneByteString._allocate(cx);
+ str._setAt(0, 0x2d); // '-'. Is overwritten if not negative.
+ final mask = radix - 1;
+ var dx = 0; // Digit index in bigint.
+ var bx = 0; // Bit index in bigint digit.
+ do {
+ var ch;
+ if (bx > (DIGIT_BITS - bitsPerChar)) {
+ ch = _digits[dx++] >> bx;
+ bx += bitsPerChar - DIGIT_BITS;
+ if (dx <= lastdx) {
+ ch |= (_digits[dx] & ((1 << bx) - 1)) << (bitsPerChar - bx);
+ }
+ } else {
+ ch = (_digits[dx] >> bx) & mask;
+ bx += bitsPerChar;
+ if (bx >= DIGIT_BITS) {
+ bx -= DIGIT_BITS;
+ dx++;
+ }
+ }
+ str._setAt(--cx, _IntegerImplementation._digits.codeUnitAt(ch));
+ } while (cx > firstcx);
+ return str;
+ }
+
_leftShiftWithMask32(int count, int mask) {
if (_used == 0) return 0;
if (count is! _Smi) {
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698