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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/core/int.dart » ('j') | sdk/lib/core/int.dart » ('J')
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 part of _interceptors; 5 part of _interceptors;
6 6
7 /** 7 /**
8 * The super interceptor class for [JSInt] and [JSDouble]. The compiler 8 * The super interceptor class for [JSInt] and [JSDouble]. The compiler
9 * recognizes this class as an interceptor, and changes references to 9 * recognizes this class as an interceptor, and changes references to
10 * [:this:] to actually use the receiver of the method, which is 10 * [:this:] to actually use the receiver of the method, which is
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 359
360 int get bitLength { 360 int get bitLength {
361 int nonneg = this < 0 ? -this - 1 : this; 361 int nonneg = this < 0 ? -this - 1 : this;
362 if (nonneg >= 0x100000000) { 362 if (nonneg >= 0x100000000) {
363 nonneg = nonneg ~/ 0x100000000; 363 nonneg = nonneg ~/ 0x100000000;
364 return _bitCount(_spread(nonneg)) + 32; 364 return _bitCount(_spread(nonneg)) + 32;
365 } 365 }
366 return _bitCount(_spread(nonneg)); 366 return _bitCount(_spread(nonneg));
367 } 367 }
368 368
369 // Return pow(this, e) % m.
floitsch 2015/02/06 20:05:19 Returns
regis 2015/02/06 21:46:54 Done.
370 int modPow(int e, int m) {
371 if (e is! int || e < 0) throw new ArgumentError(e);
372 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.
373 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.
374 int b = this;
375 if (b < 0 || b > m) {
376 b = b % m;
377 }
378 int r = 1;
379 while (e > 0) {
380 if ((e & 1) != 0) {
381 r = (r * b) % m;
382 }
383 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
384 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
385 }
386 return r;
387 }
388
369 // Assumes i is <= 32-bit and unsigned. 389 // Assumes i is <= 32-bit and unsigned.
370 static int _bitCount(int i) { 390 static int _bitCount(int i) {
371 // See "Hacker's Delight", section 5-1, "Counting 1-Bits". 391 // See "Hacker's Delight", section 5-1, "Counting 1-Bits".
372 392
373 // The basic strategy is to use "divide and conquer" to 393 // The basic strategy is to use "divide and conquer" to
374 // add pairs (then quads, etc.) of bits together to obtain 394 // add pairs (then quads, etc.) of bits together to obtain
375 // sub-counts. 395 // sub-counts.
376 // 396 //
377 // A straightforward approach would look like: 397 // A straightforward approach would look like:
378 // 398 //
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 } 433 }
414 434
415 class JSDouble extends JSNumber implements double { 435 class JSDouble extends JSNumber implements double {
416 const JSDouble(); 436 const JSDouble();
417 Type get runtimeType => double; 437 Type get runtimeType => double;
418 } 438 }
419 439
420 class JSPositiveInt extends JSInt {} 440 class JSPositiveInt extends JSInt {}
421 class JSUInt32 extends JSPositiveInt {} 441 class JSUInt32 extends JSPositiveInt {}
422 class JSUInt31 extends JSUInt32 {} 442 class JSUInt31 extends JSUInt32 {}
OLDNEW
« 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