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

Side by Side Diff: pkg/fixnum/lib/src/int64.dart

Issue 24388004: Added bitLength, clamp(), toDouble(), toSigned(), toUnsigned(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 unified diff | Download patch | Annotate | Revision Log
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 fixnum; 5 part of fixnum;
6 6
7 /** 7 /**
8 * An immutable 64-bit signed integer, in the range [-2^63, 2^63 - 1]. 8 * An immutable 64-bit signed integer, in the range [-2^63, 2^63 - 1].
9 * Arithmetic operations may overflow in order to maintain this range. 9 * Arithmetic operations may overflow in order to maintain this range.
10 */ 10 */
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 return this.compareTo(other) >= 0; 509 return this.compareTo(other) >= 0;
510 } 510 }
511 511
512 bool get isEven => (_l & 0x1) == 0; 512 bool get isEven => (_l & 0x1) == 0;
513 bool get isMaxValue => (_h == _MASK2 >> 1) && _m == _MASK && _l == _MASK; 513 bool get isMaxValue => (_h == _MASK2 >> 1) && _m == _MASK && _l == _MASK;
514 bool get isMinValue => _h == _SIGN_BIT_MASK && _m == 0 && _l == 0; 514 bool get isMinValue => _h == _SIGN_BIT_MASK && _m == 0 && _l == 0;
515 bool get isNegative => (_h & _SIGN_BIT_MASK) != 0; 515 bool get isNegative => (_h & _SIGN_BIT_MASK) != 0;
516 bool get isOdd => (_l & 0x1) == 1; 516 bool get isOdd => (_l & 0x1) == 1;
517 bool get isZero => _h == 0 && _m == 0 && _l == 0; 517 bool get isZero => _h == 0 && _m == 0 && _l == 0;
518 518
519 int get bitLength {
520 if (isZero) return 0;
521 int a0 = _l, a1 = _m, a2 = _h;
522 if (isNegative) {
523 a0 = ~a0 & _MASK;
sra1 2013/09/24 02:58:04 a0 = _MASK & a0 to help dart2js inference and con
Chris Bracken 2013/09/24 20:16:00 Done.
524 a1 = ~a1 & _MASK;
525 a2 = ~a2 & _MASK2;
526 }
527 int len = a2.bitLength;
528 if (len > 0) return len + _BITS01;
sra1 2013/09/24 02:58:04 if a2 is nonzero, we don't use len. How about i
Chris Bracken 2013/09/24 20:16:00 Done.
529 len = a1.bitLength;
530 if (len > 0) return len + _BITS;
531 return a0.bitLength;
532 }
533
519 /** 534 /**
520 * Returns a hash code based on all the bits of this [Int64]. 535 * Returns a hash code based on all the bits of this [Int64].
521 */ 536 */
522 int get hashCode { 537 int get hashCode {
523 // TODO(sra): Should we ensure that hashCode values match corresponding int? 538 // TODO(sra): Should we ensure that hashCode values match corresponding int?
524 // i.e. should `new Int64(x).hashCode == x.hashCode`? 539 // i.e. should `new Int64(x).hashCode == x.hashCode`?
525 int bottom = ((_m & 0x3ff) << _BITS) | _l; 540 int bottom = ((_m & 0x3ff) << _BITS) | _l;
526 int top = (_h << 12) | ((_m >> 10) & 0xfff); 541 int top = (_h << 12) | ((_m >> 10) & 0xfff);
527 return bottom ^ top; 542 return bottom ^ top;
528 } 543 }
529 544
530 Int64 abs() { 545 Int64 abs() {
531 return this.isNegative ? -this : this; 546 return this.isNegative ? -this : this;
532 } 547 }
533 548
549 Int64 clamp(lowerLimit, upperLimit) {
550 Int64 lower = _promote(lowerLimit);
551 Int64 upper = _promote(upperLimit);
552 if (this < lowerLimit) {
sra1 2013/09/24 02:58:04 Use the promoted values :-)
Chris Bracken 2013/09/24 20:16:00 Done. Looks like I got two lines into cleaning thi
553 if (lowerLimit is IntX) return lowerLimit.toInt64();
554 if (lowerLimit is int) return new Int64.fromInt(lowerLimit);
555 throw new ArgumentError(lowerLimit);
556 } else if (this > upperLimit) {
557 if (upperLimit is IntX) return upperLimit.toInt64();
558 if (upperLimit is int) return new Int64.fromInt(upperLimit);
559 throw new ArgumentError(upperLimit);
560 }
561 return this;
562 }
563
534 /** 564 /**
535 * Returns the number of leading zeros in this [Int64] as an [int] 565 * Returns the number of leading zeros in this [Int64] as an [int]
536 * between 0 and 64. 566 * between 0 and 64.
537 */ 567 */
538 int numberOfLeadingZeros() { 568 int numberOfLeadingZeros() {
539 int b2 = Int32._numberOfLeadingZeros(_h); 569 int b2 = Int32._numberOfLeadingZeros(_h);
540 if (b2 == 32) { 570 if (b2 == 32) {
541 int b1 = Int32._numberOfLeadingZeros(_m); 571 int b1 = Int32._numberOfLeadingZeros(_m);
542 if (b1 == 32) { 572 if (b1 == 32) {
543 return Int32._numberOfLeadingZeros(_l) + 32; 573 return Int32._numberOfLeadingZeros(_l) + 32;
(...skipping 21 matching lines...) Expand all
565 } 595 }
566 596
567 zeros = Int32._numberOfTrailingZeros(_h); 597 zeros = Int32._numberOfTrailingZeros(_h);
568 if (zeros < 32) { 598 if (zeros < 32) {
569 return _BITS01 + zeros; 599 return _BITS01 + zeros;
570 } 600 }
571 // All zeros 601 // All zeros
572 return 64; 602 return 64;
573 } 603 }
574 604
605 Int64 toSigned(int width) {
606 if (width < 1 || width > 64) throw new ArgumentError(width);
607 if (width > _BITS01) {
608 return Int64._masked(_l, _m, _h.toSigned(width - _BITS01));
609 } else if (width > _BITS) {
610 int m = _m.toSigned(width - _BITS);
611 return m.isNegative ? Int64._masked(_l, m, _MASK2) :
612 new Int64._bits(_l, m, 0);
613 } else {
614 int l = _l.toSigned(width);
615 return l.isNegative ? Int64._masked(l, _MASK, _MASK2) :
616 new Int64._bits(l, 0, 0);
617 }
618 }
619
620 Int64 toUnsigned(int width) {
621 if (width < 0 || width > 64) throw new ArgumentError(width);
622 if (width > 2 * _BITS) {
623 int h = _h.toUnsigned(width - 2 * _BITS);
624 return Int64._masked(_l, _m, h);
625 } else if (width > _BITS) {
626 int m = _m.toUnsigned(width - _BITS);
627 return Int64._masked(_l, m, 0);
628 } else {
629 int l = _l.toUnsigned(width);
630 return Int64._masked(l, 0, 0);
631 }
632 }
633
575 List<int> toBytes() { 634 List<int> toBytes() {
576 List<int> result = new List<int>(8); 635 List<int> result = new List<int>(8);
577 result[0] = _l & 0xff; 636 result[0] = _l & 0xff;
578 result[1] = (_l >> 8) & 0xff; 637 result[1] = (_l >> 8) & 0xff;
579 result[2] = ((_m << 6) & 0xfc) | ((_l >> 16) & 0x3f); 638 result[2] = ((_m << 6) & 0xfc) | ((_l >> 16) & 0x3f);
580 result[3] = (_m >> 2) & 0xff; 639 result[3] = (_m >> 2) & 0xff;
581 result[4] = (_m >> 10) & 0xff; 640 result[4] = (_m >> 10) & 0xff;
582 result[5] = ((_h << 4) & 0xf0) | ((_m >> 18) & 0xf); 641 result[5] = ((_h << 4) & 0xf0) | ((_m >> 18) & 0xf);
583 result[6] = (_h >> 4) & 0xff; 642 result[6] = (_h >> 4) & 0xff;
584 result[7] = (_h >> 12) & 0xff; 643 result[7] = (_h >> 12) & 0xff;
585 return result; 644 return result;
586 } 645 }
587 646
647 double toDouble() => toInt().toDouble();
sra1 2013/09/24 02:58:04 I think this can round incorrectly. The problem l
Chris Bracken 2013/09/24 20:16:00 Done.
648
588 int toInt() { 649 int toInt() {
589 int l = _l; 650 int l = _l;
590 int m = _m; 651 int m = _m;
591 int h = _h; 652 int h = _h;
592 bool negative = false; 653 bool negative = false;
593 if ((_h & _SIGN_BIT_MASK) != 0) { 654 if ((_h & _SIGN_BIT_MASK) != 0) {
594 l = ~_l & _MASK; 655 l = ~_l & _MASK;
595 m = ~_m & _MASK; 656 m = ~_m & _MASK;
596 h = ~_h & _MASK2; 657 h = ~_h & _MASK2;
597 negative = true; 658 negative = true;
598 } 659 }
599 660
600 int result; 661 int result;
601 if (_haveBigInts) { 662 if (_haveBigInts) {
602 result = (h << _BITS01) | (m << _BITS) | l; 663 result = (h << _BITS01) | (m << _BITS) | l;
sra1 2013/09/24 02:58:04 return negative ? -result - 1 : result;
Chris Bracken 2013/09/24 20:16:00 Done.
603 } else { 664 } else {
604 result = (h * 17592186044416) + (m * 4194304) + l; 665 result = (h * 17592186044416) + (m * 4194304) + l;
sra1 2013/09/24 02:58:04 return negative ? -result - 1 : result; But that
Chris Bracken 2013/09/24 20:16:00 Done.
605 } 666 }
606 return negative ? -result - 1 : result; 667 return negative ? -result - 1 : result;
607 } 668 }
608 669
609 /** 670 /**
610 * Returns an [Int32] containing the low 32 bits of this [Int64]. 671 * Returns an [Int32] containing the low 32 bits of this [Int64].
611 */ 672 */
612 Int32 toInt32() { 673 Int32 toInt32() {
613 return new Int32(((_m & 0x3ff) << _BITS) | _l); 674 return new Int32(((_m & 0x3ff) << _BITS) | _l);
614 } 675 }
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
984 if (r0 == 0 && r1 == 0 && r2 == 0) { 1045 if (r0 == 0 && r1 == 0 && r2 == 0) {
985 return ZERO; 1046 return ZERO;
986 } else { 1047 } else {
987 return _sub(b0, b1, b2, r0, r1, r2); 1048 return _sub(b0, b1, b2, r0, r1, r2);
988 } 1049 }
989 } else { 1050 } else {
990 return _negate(r0, r1, r2); 1051 return _negate(r0, r1, r2);
991 } 1052 }
992 } 1053 }
993 } 1054 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698