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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: pkg/fixnum/lib/src/int64.dart
diff --git a/pkg/fixnum/lib/src/int64.dart b/pkg/fixnum/lib/src/int64.dart
index aed28a24d9402cd24a1f355f428bffe1e6c7b847..3fb57ccc8ae2a75a1d0f28924067758129ee9e90 100644
--- a/pkg/fixnum/lib/src/int64.dart
+++ b/pkg/fixnum/lib/src/int64.dart
@@ -516,6 +516,21 @@ class Int64 implements IntX {
bool get isOdd => (_l & 0x1) == 1;
bool get isZero => _h == 0 && _m == 0 && _l == 0;
+ int get bitLength {
+ if (isZero) return 0;
+ int a0 = _l, a1 = _m, a2 = _h;
+ if (isNegative) {
+ 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.
+ a1 = ~a1 & _MASK;
+ a2 = ~a2 & _MASK2;
+ }
+ int len = a2.bitLength;
+ 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.
+ len = a1.bitLength;
+ if (len > 0) return len + _BITS;
+ return a0.bitLength;
+ }
+
/**
* Returns a hash code based on all the bits of this [Int64].
*/
@@ -531,6 +546,21 @@ class Int64 implements IntX {
return this.isNegative ? -this : this;
}
+ Int64 clamp(lowerLimit, upperLimit) {
+ Int64 lower = _promote(lowerLimit);
+ Int64 upper = _promote(upperLimit);
+ 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
+ if (lowerLimit is IntX) return lowerLimit.toInt64();
+ if (lowerLimit is int) return new Int64.fromInt(lowerLimit);
+ throw new ArgumentError(lowerLimit);
+ } else if (this > upperLimit) {
+ if (upperLimit is IntX) return upperLimit.toInt64();
+ if (upperLimit is int) return new Int64.fromInt(upperLimit);
+ throw new ArgumentError(upperLimit);
+ }
+ return this;
+ }
+
/**
* Returns the number of leading zeros in this [Int64] as an [int]
* between 0 and 64.
@@ -572,6 +602,35 @@ class Int64 implements IntX {
return 64;
}
+ Int64 toSigned(int width) {
+ if (width < 1 || width > 64) throw new ArgumentError(width);
+ if (width > _BITS01) {
+ return Int64._masked(_l, _m, _h.toSigned(width - _BITS01));
+ } else if (width > _BITS) {
+ int m = _m.toSigned(width - _BITS);
+ return m.isNegative ? Int64._masked(_l, m, _MASK2) :
+ new Int64._bits(_l, m, 0);
+ } else {
+ int l = _l.toSigned(width);
+ return l.isNegative ? Int64._masked(l, _MASK, _MASK2) :
+ new Int64._bits(l, 0, 0);
+ }
+ }
+
+ Int64 toUnsigned(int width) {
+ if (width < 0 || width > 64) throw new ArgumentError(width);
+ if (width > 2 * _BITS) {
+ int h = _h.toUnsigned(width - 2 * _BITS);
+ return Int64._masked(_l, _m, h);
+ } else if (width > _BITS) {
+ int m = _m.toUnsigned(width - _BITS);
+ return Int64._masked(_l, m, 0);
+ } else {
+ int l = _l.toUnsigned(width);
+ return Int64._masked(l, 0, 0);
+ }
+ }
+
List<int> toBytes() {
List<int> result = new List<int>(8);
result[0] = _l & 0xff;
@@ -585,6 +644,8 @@ class Int64 implements IntX {
return result;
}
+ 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.
+
int toInt() {
int l = _l;
int m = _m;

Powered by Google App Engine
This is Rietveld 408576698