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

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

Issue 570563004: Temporarily add a public modPow method to int interface. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 3 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 | « runtime/lib/bigint.dart ('k') | 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 extends _Num { 7 class _IntegerImplementation extends _Num {
8 // The Dart class _Bigint extending _IntegerImplementation requires a 8 // The Dart class _Bigint extending _IntegerImplementation requires a
9 // default constructor. 9 // default constructor.
10 10
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 string._setAt(0, 0x2d); // '-'. Is overwritten if not negative. 256 string._setAt(0, 0x2d); // '-'. Is overwritten if not negative.
257 var mask = radix - 1; 257 var mask = radix - 1;
258 do { 258 do {
259 string._setAt(--length, _digits.codeUnitAt(value & mask)); 259 string._setAt(--length, _digits.codeUnitAt(value & mask));
260 value >>= bitsPerDigit; 260 value >>= bitsPerDigit;
261 } while (value > 0); 261 } while (value > 0);
262 return string; 262 return string;
263 } 263 }
264 264
265 _leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32"; 265 _leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32";
266
267 // TODO(regis): Make this method private once the plumbing to invoke it from
268 // dart:math is in place. Move the argument checking to dart:math.
269 // Return pow(this, e) % m.
270 int modPow(int e, int m) {
271 if (e is! int) throw new ArgumentError(e);
272 if (m is! int) throw new ArgumentError(m);
srdjan 2014/09/16 18:08:36 Maybe instead: if ((e is! int) || (m is! int) {
regis 2014/09/16 18:18:54 This can be done when we add modPow to dart:math.
273 if (e is _Bigint || m is _Bigint) {
274 return _toBigint().modPow(e, m);
275 }
276 if (e < 1) return 1;
277 int b = this;
278 if (b < 0 || b > m) {
279 b = b % m;
280 }
281 int r = 1;
282 while (e > 0) {
283 if ((e & 1) != 0) {
284 r = (r * b) % m;
285 }
286 e >>= 1;
287 b = (b * b) % m;
288 }
289 return r;
290 }
266 } 291 }
267 292
268 class _Smi extends _IntegerImplementation implements int { 293 class _Smi extends _IntegerImplementation implements int {
269 factory _Smi._uninstantiable() { 294 factory _Smi._uninstantiable() {
270 throw new UnsupportedError( 295 throw new UnsupportedError(
271 "_Smi can only be allocated by the VM"); 296 "_Smi can only be allocated by the VM");
272 } 297 }
273 int get _identityHashCode { 298 int get _identityHashCode {
274 return this; 299 return this;
275 } 300 }
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 // Shift by mint exceeds range that can be handled by the VM. 502 // Shift by mint exceeds range that can be handled by the VM.
478 int _shrFromInt(int other) { 503 int _shrFromInt(int other) {
479 if (other < 0) { 504 if (other < 0) {
480 return -1; 505 return -1;
481 } else { 506 } else {
482 return 0; 507 return 0;
483 } 508 }
484 } 509 }
485 int _shlFromInt(int other) native "Mint_shlFromInt"; 510 int _shlFromInt(int other) native "Mint_shlFromInt";
486 } 511 }
OLDNEW
« no previous file with comments | « runtime/lib/bigint.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698