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

Side by Side Diff: sdk/lib/_internal/compiler/js_lib/js_number.dart

Issue 1188843004: Implement modInverse for an even modulus. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 6 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
« 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 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 e ~/= 2; 408 e ~/= 2;
409 b = (b * b) % m; 409 b = (b * b) % m;
410 } 410 }
411 return r; 411 return r;
412 } 412 }
413 413
414 // Returns 1/this % m, with m > 0. 414 // Returns 1/this % m, with m > 0.
415 int modInverse(int m) { 415 int modInverse(int m) {
416 if (m is! int) throw new ArgumentError(m); 416 if (m is! int) throw new ArgumentError(m);
417 if (m <= 0) throw new RangeError(m); 417 if (m <= 0) throw new RangeError(m);
418 if (this == 0) return 0; 418 if (m == 1) return 0;
419 bool ac = m.isEven; 419 int t = this;
420 if ((t < 0) || (t >= m)) t %= m;
421 if (t == 1) return 1;
422 final bool ac = m.isEven;
423 if ((t == 0) || (ac && t.isEven)) throw new RangeError("Not coprime");
420 int u = m; 424 int u = m;
421 int v = this; 425 int v = t;
422 int a = 1, 426 int a = 1,
423 b = 0, 427 b = 0,
424 c = 0, 428 c = 0,
425 d = 1; 429 d = 1;
426 while (u != 0) { 430 do {
427 while (u.isEven) { 431 while (u.isEven) {
428 u ~/= 2; 432 u ~/= 2;
429 if (ac) { 433 if (ac) {
430 if (!a.isEven || !b.isEven) { 434 if (!a.isEven || !b.isEven) {
431 a += this; 435 a += t;
432 b -= m; 436 b -= m;
433 } 437 }
434 a ~/= 2; 438 a ~/= 2;
435 } else if (!b.isEven) { 439 } else if (!b.isEven) {
436 b -= m; 440 b -= m;
437 } 441 }
438 b ~/= 2; 442 b ~/= 2;
439 } 443 }
440 while (v.isEven) { 444 while (v.isEven) {
441 v ~/= 2; 445 v ~/= 2;
442 if (ac) { 446 if (ac) {
443 if (!c.isEven || !d.isEven) { 447 if (!c.isEven || !d.isEven) {
444 c += this; 448 c += t;
445 d -= m; 449 d -= m;
446 } 450 }
447 c ~/= 2; 451 c ~/= 2;
448 } else if (!d.isEven) { 452 } else if (!d.isEven) {
449 d -= m; 453 d -= m;
450 } 454 }
451 d ~/= 2; 455 d ~/= 2;
452 } 456 }
453 if (u >= v) { 457 if (u >= v) {
454 u -= v; 458 u -= v;
455 if (ac) a -= c; 459 if (ac) a -= c;
456 b -= d; 460 b -= d;
457 } else { 461 } else {
458 v -= u; 462 v -= u;
459 if (ac) c -= a; 463 if (ac) c -= a;
460 d -= b; 464 d -= b;
461 } 465 }
466 } while (u != 0);
467 if (v != 1) throw new RangeError("Not coprime");
468 if (d < 0) {
469 d += m;
470 if (d < 0) d += m;
471 } else if (d > m) {
472 d -= m;
473 if (d > m) d -= m;
462 } 474 }
463 if (v != 1) return 0;
464 if (d > m) return d - m;
465 if (d < 0) d += m;
466 if (d < 0) d += m;
467 return d; 475 return d;
468 } 476 }
469 477
470 // Assumes i is <= 32-bit and unsigned. 478 // Assumes i is <= 32-bit and unsigned.
471 static int _bitCount(int i) { 479 static int _bitCount(int i) {
472 // See "Hacker's Delight", section 5-1, "Counting 1-Bits". 480 // See "Hacker's Delight", section 5-1, "Counting 1-Bits".
473 481
474 // The basic strategy is to use "divide and conquer" to 482 // The basic strategy is to use "divide and conquer" to
475 // add pairs (then quads, etc.) of bits together to obtain 483 // add pairs (then quads, etc.) of bits together to obtain
476 // sub-counts. 484 // sub-counts.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 } 522 }
515 523
516 class JSDouble extends JSNumber implements double { 524 class JSDouble extends JSNumber implements double {
517 const JSDouble(); 525 const JSDouble();
518 Type get runtimeType => double; 526 Type get runtimeType => double;
519 } 527 }
520 528
521 class JSPositiveInt extends JSInt {} 529 class JSPositiveInt extends JSInt {}
522 class JSUInt32 extends JSPositiveInt {} 530 class JSUInt32 extends JSPositiveInt {}
523 class JSUInt31 extends JSUInt32 {} 531 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