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

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

Issue 896393004: Add modPow(int exponent, int modulus) method to int abstract class. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 5 years, 10 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') | sdk/lib/_internal/compiler/js_lib/js_number.dart » ('j') | 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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 var mask = radix - 1; 258 var mask = radix - 1;
259 do { 259 do {
260 string._setAt(--length, _digits.codeUnitAt(value & mask)); 260 string._setAt(--length, _digits.codeUnitAt(value & mask));
261 value >>= bitsPerDigit; 261 value >>= bitsPerDigit;
262 } while (value > 0); 262 } while (value > 0);
263 return string; 263 return string;
264 } 264 }
265 265
266 _leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32"; 266 _leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32";
267 267
268 // Return pow(this, e) % m. 268 // Returns pow(this, e) % m.
269 int modPow(int e, int m) { 269 int modPow(int e, int m) {
270 if (e is! int || e < 0) throw new ArgumentError(e); 270 if (e is! int) throw new ArgumentError(e);
271 if (m is! int || m <= 0) throw new ArgumentError(m); 271 if (m is! int) throw new ArgumentError(m);
272 if (e < 0) throw new RangeError(e);
273 if (m <= 0) throw new RangeError(m);
274 if (e == 0) return 1;
272 if (e is _Bigint || m is _Bigint) { 275 if (e is _Bigint || m is _Bigint) {
273 return _toBigint().modPow(e, m); 276 return _toBigint().modPow(e, m);
274 } 277 }
275 if (e < 1) return 1; 278 if (e < 1) return 1;
276 int b = this; 279 int b = this;
277 if (b < 0 || b > m) { 280 if (b < 0 || b > m) {
278 b = b % m; 281 b %= m;
279 } 282 }
280 int r = 1; 283 int r = 1;
281 while (e > 0) { 284 while (e > 0) {
282 if ((e & 1) != 0) { 285 if (e.isOdd) {
283 r = (r * b) % m; 286 r = (r * b) % m;
284 } 287 }
285 e >>= 1; 288 e >>= 1;
286 b = (b * b) % m; 289 b = (b * b) % m;
287 } 290 }
288 return r; 291 return r;
289 } 292 }
290 } 293 }
291 294
292 class _Smi extends _IntegerImplementation implements int { 295 class _Smi extends _IntegerImplementation implements int {
293 factory _Smi._uninstantiable() { 296 factory _Smi._uninstantiable() {
294 throw new UnsupportedError( 297 throw new UnsupportedError(
295 "_Smi can only be allocated by the VM"); 298 "_Smi can only be allocated by the VM");
296 } 299 }
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 // Shift by mint exceeds range that can be handled by the VM. 504 // Shift by mint exceeds range that can be handled by the VM.
502 int _shrFromInt(int other) { 505 int _shrFromInt(int other) {
503 if (other < 0) { 506 if (other < 0) {
504 return -1; 507 return -1;
505 } else { 508 } else {
506 return 0; 509 return 0;
507 } 510 }
508 } 511 }
509 int _shlFromInt(int other) native "Mint_shlFromInt"; 512 int _shlFromInt(int other) native "Mint_shlFromInt";
510 } 513 }
OLDNEW
« no previous file with comments | « runtime/lib/bigint.dart ('k') | sdk/lib/_internal/compiler/js_lib/js_number.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698