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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/lib/bigint.dart ('k') | no next file » | 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 40320)
+++ runtime/lib/integers.dart (working copy)
@@ -263,6 +263,31 @@
}
_leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32";
+
+ // TODO(regis): Make this method private once the plumbing to invoke it from
+ // dart:math is in place. Move the argument checking to dart:math.
+ // Return 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);
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.
+ if (e is _Bigint || m is _Bigint) {
+ return _toBigint().modPow(e, m);
+ }
+ if (e < 1) return 1;
+ 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;
+ b = (b * b) % m;
+ }
+ return r;
+ }
}
class _Smi extends _IntegerImplementation implements int {
« 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