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

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 | « no previous file | sdk/lib/core/int.dart » ('j') | sdk/lib/core/int.dart » ('J')
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 43513)
+++ sdk/lib/_internal/compiler/js_lib/js_number.dart (working copy)
@@ -366,6 +366,26 @@
return _bitCount(_spread(nonneg));
}
+ // Return pow(this, e) % m.
floitsch 2015/02/06 20:05:19 Returns
regis 2015/02/06 21:46:54 Done.
+ 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);
Lasse Reichstein Nielsen 2015/02/06 19:57:03 Maybe not worth it, but you can use RangeError for
regis 2015/02/06 21:46:53 Done.
+ if (e < 1) return 1;
Lasse Reichstein Nielsen 2015/02/06 19:57:03 Why not if (e == 0) ...
regis 2015/02/06 21:46:53 Done.
+ int b = this;
+ if (b < 0 || b > m) {
+ b = b % m;
+ }
+ int r = 1;
+ while (e > 0) {
+ if ((e & 1) != 0) {
+ r = (r * b) % m;
+ }
+ e >>= 1;
Lasse Reichstein Nielsen 2015/02/06 19:57:03 Using bit-operations restricts e to 2^32. Maybe us
floitsch 2015/02/06 20:05:19 I'm not sure it's worth it. But it would allow the
regis 2015/02/06 21:46:54 I replaced e & 1 != 0 by e.isOdd, since & is a bit
+ b = (b * b) % m;
Lasse Reichstein Nielsen 2015/02/06 19:57:03 I guess %m is expensive. Would it be worth it to d
floitsch 2015/02/06 20:05:19 I would hope that % is optimized for that case any
regis 2015/02/06 21:46:53 The vm is optimizing this case. I do not know if d
+ }
+ 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 | « no previous file | sdk/lib/core/int.dart » ('j') | sdk/lib/core/int.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698