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

Side by Side Diff: runtime/lib/integers.dart

Issue 24497004: Improve speed of toRadixString, and extra much for powers of two. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // TODO(srdjan): fix limitations. 5 // TODO(srdjan): fix limitations.
6 // - shift amount must be a Smi. 6 // - shift amount must be a Smi.
7 class _IntegerImplementation { 7 class _IntegerImplementation {
8 factory _IntegerImplementation._uninstantiable() { 8 factory _IntegerImplementation._uninstantiable() {
9 throw new UnsupportedError( 9 throw new UnsupportedError(
10 "_IntegerImplementation can only be allocated by the VM"); 10 "_IntegerImplementation can only be allocated by the VM");
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 String toStringAsFixed(int fractionDigits) { 180 String toStringAsFixed(int fractionDigits) {
181 return this.toDouble().toStringAsFixed(fractionDigits); 181 return this.toDouble().toStringAsFixed(fractionDigits);
182 } 182 }
183 String toStringAsExponential([int fractionDigits]) { 183 String toStringAsExponential([int fractionDigits]) {
184 return this.toDouble().toStringAsExponential(fractionDigits); 184 return this.toDouble().toStringAsExponential(fractionDigits);
185 } 185 }
186 String toStringAsPrecision(int precision) { 186 String toStringAsPrecision(int precision) {
187 return this.toDouble().toStringAsPrecision(precision); 187 return this.toDouble().toStringAsPrecision(precision);
188 } 188 }
189 189
190 static const _digits = "0123456789abcdefghijklmnopqrstuvwxyz";
191
190 String toRadixString(int radix) { 192 String toRadixString(int radix) {
191 final table = const ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
192 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
193 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
194 "u", "v", "w", "x", "y", "z"];
195 if (radix is! int || radix < 2 || radix > 36) { 193 if (radix is! int || radix < 2 || radix > 36) {
196 throw new ArgumentError(radix); 194 throw new ArgumentError(radix);
197 } 195 }
196 if (radix & (radix - 1) == 0) {
197 return _toPow2String(this, radix);
198 }
198 final bool isNegative = this < 0; 199 final bool isNegative = this < 0;
199 int value = isNegative ? -this : this; 200 int value = isNegative ? -this : this;
200 List temp = new List(); 201 List temp = new List();
sra1 2013/09/25 18:03:44 You could estimate the length and allocate a one b
Lasse Reichstein Nielsen 2013/09/26 09:24:49 There are two possible optimizations here. One is
sra1 2013/09/27 01:12:42 No problem leaving it for later. The estimate is:
201 while (value > 0) { 202 do {
202 int digit = value % radix; 203 int digit = value % radix;
203 value ~/= radix; 204 value ~/= radix;
204 temp.add(digit); 205 temp.add(_digits.codeUnitAt(digit));
206 } while (value > 0);
207 if (isNegative) temp.add(0x2d); // '-'.
208
209 _OneByteString string = _OneByteString._allocate(temp.length);
210 for (int i = 0, j = temp.length; j > 0; i++) {
211 string._setAt(i, temp[--j]);
205 } 212 }
206 if (temp.isEmpty) { 213 return string;
207 return "0"; 214 }
215
216 static String _toPow2String(value, radix) {
217 if (value == 0) return "0";
218 assert(radix & (radix - 1) == 0);
219 var negative = value < 0;
220 var bitsPerDigit = radix.bitLength - 1;
221 var length = 0;
222 if (negative) {
223 value = -value;
224 length = 1;
208 } 225 }
209 StringBuffer buffer = new StringBuffer(); 226 // Integer division, rounding up, to find number of _digits.
210 if (isNegative) buffer.write("-"); 227 length += (value.bitLength + bitsPerDigit - 1) ~/ bitsPerDigit;
211 for (int i = temp.length - 1; i >= 0; i--) { 228 _OneByteString string = _OneByteString._allocate(length);
212 buffer.write(table[temp[i]]); 229 string._setAt(0, 0x2d); // '-'. Is overwritten if not negative.
213 } 230 var mask = radix - 1;
214 return buffer.toString(); 231 do {
232 string._setAt(--length, _digits.codeUnitAt(value & mask));
233 value >>= bitsPerDigit;
234 } while (value > 0);
235 return string;
215 } 236 }
216 237
217 _leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32"; 238 _leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32";
218 } 239 }
219 240
220 class _Smi extends _IntegerImplementation implements int { 241 class _Smi extends _IntegerImplementation implements int {
221 factory _Smi._uninstantiable() { 242 factory _Smi._uninstantiable() {
222 throw new UnsupportedError( 243 throw new UnsupportedError(
223 "_Smi can only be allocated by the VM"); 244 "_Smi can only be allocated by the VM");
224 } 245 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 } else { 322 } else {
302 return 0; 323 return 0;
303 } 324 }
304 } 325 }
305 int _shlFromInt(int other) native "Bigint_shlFromInt"; 326 int _shlFromInt(int other) native "Bigint_shlFromInt";
306 327
307 int pow(int exponent) { 328 int pow(int exponent) {
308 throw "Bigint.pow not implemented"; 329 throw "Bigint.pow not implemented";
309 } 330 }
310 } 331 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698