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

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 | « runtime/lib/integers.dart ('k') | sdk/lib/core/int.dart » ('j') | no next file with comments »
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 // Returns pow(this, e) % m.
370 int modPow(int e, int m) {
371 if (e is! int) throw new ArgumentError(e);
372 if (m is! int) throw new ArgumentError(m);
373 if (e < 0) throw new RangeError(e);
374 if (m <= 0) throw new RangeError(m);
375 if (e == 0) return 1;
376 int b = this;
377 if (b < 0 || b > m) {
378 b %= m;
379 }
380 int r = 1;
381 while (e > 0) {
382 if (e.isOdd) {
383 r = (r * b) % m;
384 }
385 e ~/= 2;
386 b = (b * b) % m;
387 }
388 return r;
389 }
390
369 // Assumes i is <= 32-bit and unsigned. 391 // Assumes i is <= 32-bit and unsigned.
370 static int _bitCount(int i) { 392 static int _bitCount(int i) {
371 // See "Hacker's Delight", section 5-1, "Counting 1-Bits". 393 // See "Hacker's Delight", section 5-1, "Counting 1-Bits".
372 394
373 // The basic strategy is to use "divide and conquer" to 395 // The basic strategy is to use "divide and conquer" to
374 // add pairs (then quads, etc.) of bits together to obtain 396 // add pairs (then quads, etc.) of bits together to obtain
375 // sub-counts. 397 // sub-counts.
376 // 398 //
377 // A straightforward approach would look like: 399 // A straightforward approach would look like:
378 // 400 //
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 } 435 }
414 436
415 class JSDouble extends JSNumber implements double { 437 class JSDouble extends JSNumber implements double {
416 const JSDouble(); 438 const JSDouble();
417 Type get runtimeType => double; 439 Type get runtimeType => double;
418 } 440 }
419 441
420 class JSPositiveInt extends JSInt {} 442 class JSPositiveInt extends JSInt {}
421 class JSUInt32 extends JSPositiveInt {} 443 class JSUInt32 extends JSPositiveInt {}
422 class JSUInt31 extends JSUInt32 {} 444 class JSUInt31 extends JSUInt32 {}
OLDNEW
« 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