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

Unified Diff: sdk/lib/_internal/compiler/js_lib/js_number.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/integers.dart ('k') | sdk/lib/core/int.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/compiler/js_lib/js_number.dart
===================================================================
--- sdk/lib/_internal/compiler/js_lib/js_number.dart (revision 43571)
+++ sdk/lib/_internal/compiler/js_lib/js_number.dart (working copy)
@@ -366,6 +366,28 @@
return _bitCount(_spread(nonneg));
}
+ // Returns pow(this, e) % m.
+ int modPow(int e, int 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;
+ int b = this;
+ if (b < 0 || b > m) {
+ b %= m;
+ }
+ int r = 1;
+ while (e > 0) {
+ if (e.isOdd) {
+ r = (r * b) % m;
+ }
+ e ~/= 2;
+ b = (b * b) % m;
+ }
+ return r;
+ }
+
// Assumes i is <= 32-bit and unsigned.
static int _bitCount(int i) {
// See "Hacker's Delight", section 5-1, "Counting 1-Bits".
« no previous file with comments | « runtime/lib/integers.dart ('k') | sdk/lib/core/int.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698