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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/integers.dart
===================================================================
--- runtime/lib/integers.dart (revision 43571)
+++ runtime/lib/integers.dart (working copy)
@@ -265,10 +265,13 @@
_leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32";
- // Return pow(this, e) % m.
+ // Returns pow(this, e) % m.
int modPow(int e, int m) {
- if (e is! int || e < 0) throw new ArgumentError(e);
- if (m is! int || m <= 0) throw new ArgumentError(m);
+ if (e is! int) throw new ArgumentError(e);
+ if (m is! int) throw new ArgumentError(m);
+ if (e < 0) throw new RangeError(e);
+ if (m <= 0) throw new RangeError(m);
+ if (e == 0) return 1;
if (e is _Bigint || m is _Bigint) {
return _toBigint().modPow(e, m);
}
@@ -275,15 +278,15 @@
if (e < 1) return 1;
int b = this;
if (b < 0 || b > m) {
- b = b % m;
+ b %= m;
}
int r = 1;
while (e > 0) {
- if ((e & 1) != 0) {
- r = (r * b) % m;
- }
- e >>= 1;
- b = (b * b) % m;
+ if (e.isOdd) {
+ r = (r * b) % m;
+ }
+ e >>= 1;
+ b = (b * b) % m;
}
return r;
}
« 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