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

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

Issue 1909043004: Moved pkg/fixnum to github (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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 | « pkg/fixnum/lib/fixnum.dart ('k') | pkg/fixnum/lib/src/int64.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of fixnum;
6
7 /**
8 * An immutable 32-bit signed integer, in the range [-2^31, 2^31 - 1].
9 * Arithmetic operations may overflow in order to maintain this range.
10 */
11 class Int32 implements IntX {
12
13 /**
14 * The maximum positive value attainable by an [Int32], namely
15 * 2147483647.
16 */
17 static const Int32 MAX_VALUE = const Int32._internal(0x7FFFFFFF);
18
19 /**
20 * The minimum positive value attainable by an [Int32], namely
21 * -2147483648.
22 */
23 static const Int32 MIN_VALUE = const Int32._internal(-0x80000000);
24
25 /**
26 * An [Int32] constant equal to 0.
27 */
28 static const Int32 ZERO = const Int32._internal(0);
29
30 /**
31 * An [Int32] constant equal to 1.
32 */
33 static const Int32 ONE = const Int32._internal(1);
34
35 /**
36 * An [Int32] constant equal to 2.
37 */
38 static const Int32 TWO = const Int32._internal(2);
39
40 // Hex digit char codes
41 static const int _CC_0 = 48; // '0'.codeUnitAt(0)
42 static const int _CC_9 = 57; // '9'.codeUnitAt(0)
43 static const int _CC_a = 97; // 'a'.codeUnitAt(0)
44 static const int _CC_z = 122; // 'z'.codeUnitAt(0)
45 static const int _CC_A = 65; // 'A'.codeUnitAt(0)
46 static const int _CC_Z = 90; // 'Z'.codeUnitAt(0)
47
48 static int _decodeDigit(int c) {
49 if (c >= _CC_0 && c <= _CC_9) {
50 return c - _CC_0;
51 } else if (c >= _CC_a && c <= _CC_z) {
52 return c - _CC_a + 10;
53 } else if (c >= _CC_A && c <= _CC_Z) {
54 return c - _CC_A + 10;
55 } else {
56 return -1; // bad char code
57 }
58 }
59
60 static int _validateRadix(int radix) {
61 if (2 <= radix && radix <= 36) return radix;
62 throw new RangeError.range(radix, 2, 36, 'radix');
63 }
64
65 /**
66 * Parses a [String] in a given [radix] between 2 and 16 and returns an
67 * [Int32].
68 */
69 // TODO(rice) - Make this faster by converting several digits at once.
70 static Int32 parseRadix(String s, int radix) {
71 _validateRadix(radix);
72 Int32 x = ZERO;
73 for (int i = 0; i < s.length; i++) {
74 int c = s.codeUnitAt(i);
75 int digit = _decodeDigit(c);
76 if (digit < 0 || digit >= radix) {
77 throw new FormatException("Non-radix code unit: $c");
78 }
79 x = (x * radix) + digit;
80 }
81 return x;
82 }
83
84 /**
85 * Parses a decimal [String] and returns an [Int32].
86 */
87 static Int32 parseInt(String s) => new Int32(int.parse(s));
88
89 /**
90 * Parses a hexadecimal [String] and returns an [Int32].
91 */
92 static Int32 parseHex(String s) => parseRadix(s, 16);
93
94 // Assumes i is <= 32-bit.
95 static int _bitCount(int i) {
96 // See "Hacker's Delight", section 5-1, "Counting 1-Bits".
97
98 // The basic strategy is to use "divide and conquer" to
99 // add pairs (then quads, etc.) of bits together to obtain
100 // sub-counts.
101 //
102 // A straightforward approach would look like:
103 //
104 // i = (i & 0x55555555) + ((i >> 1) & 0x55555555);
105 // i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
106 // i = (i & 0x0F0F0F0F) + ((i >> 4) & 0x0F0F0F0F);
107 // i = (i & 0x00FF00FF) + ((i >> 8) & 0x00FF00FF);
108 // i = (i & 0x0000FFFF) + ((i >> 16) & 0x0000FFFF);
109 //
110 // The code below removes unnecessary &'s and uses a
111 // trick to remove one instruction in the first line.
112
113 i -= ((i >> 1) & 0x55555555);
114 i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
115 i = ((i + (i >> 4)) & 0x0F0F0F0F);
116 i += (i >> 8);
117 i += (i >> 16);
118 return (i & 0x0000003F);
119 }
120
121 // Assumes i is <= 32-bit
122 static int _numberOfLeadingZeros(int i) {
123 i |= i >> 1;
124 i |= i >> 2;
125 i |= i >> 4;
126 i |= i >> 8;
127 i |= i >> 16;
128 return _bitCount(~i);
129 }
130
131 static int _numberOfTrailingZeros(int i) => _bitCount((i & -i) - 1);
132
133 // The internal value, kept in the range [MIN_VALUE, MAX_VALUE].
134 final int _i;
135
136 const Int32._internal(int i) : _i = i;
137
138 /**
139 * Constructs an [Int32] from an [int]. Only the low 32 bits of the input
140 * are used.
141 */
142 Int32([int i=0]) : _i = (i & 0x7fffffff) - (i & 0x80000000);
143
144 // Returns the [int] representation of the specified value. Throws
145 // [ArgumentError] for non-integer arguments.
146 int _toInt(val) {
147 if (val is Int32) {
148 return val._i;
149 } else if (val is int) {
150 return val;
151 }
152 throw new ArgumentError(val);
153 }
154
155 // The +, -, * , &, |, and ^ operaters deal with types as follows:
156 //
157 // Int32 + int => Int32
158 // Int32 + Int32 => Int32
159 // Int32 + Int64 => Int64
160 //
161 // The %, ~/ and remainder operators return an Int32 even with an Int64
162 // argument, since the result cannot be greater than the value on the
163 // left-hand side:
164 //
165 // Int32 % int => Int32
166 // Int32 % Int32 => Int32
167 // Int32 % Int64 => Int32
168
169 IntX operator +(other) {
170 if (other is Int64) {
171 return this.toInt64() + other;
172 }
173 return new Int32(_i + _toInt(other));
174 }
175
176 IntX operator -(other) {
177 if (other is Int64) {
178 return this.toInt64() - other;
179 }
180 return new Int32(_i - _toInt(other));
181 }
182
183 Int32 operator -() => new Int32(-_i);
184
185 IntX operator *(other) {
186 if (other is Int64) {
187 return this.toInt64() * other;
188 }
189 // TODO(rice) - optimize
190 return (this.toInt64() * other).toInt32();
191 }
192
193 Int32 operator %(other) {
194 if (other is Int64) {
195 // Result will be Int32
196 return (this.toInt64() % other).toInt32();
197 }
198 return new Int32(_i % _toInt(other));
199 }
200
201 Int32 operator ~/(other) {
202 if (other is Int64) {
203 return (this.toInt64() ~/ other).toInt32();
204 }
205 return new Int32(_i ~/ _toInt(other));
206 }
207
208 Int32 remainder(other) {
209 if (other is Int64) {
210 Int64 t = this.toInt64();
211 return (t - (t ~/ other) * other).toInt32();
212 }
213 return this - (this ~/ other) * other;
214 }
215
216 Int32 operator &(other) {
217 if (other is Int64) {
218 return (this.toInt64() & other).toInt32();
219 }
220 return new Int32(_i & _toInt(other));
221 }
222
223 Int32 operator |(other) {
224 if (other is Int64) {
225 return (this.toInt64() | other).toInt32();
226 }
227 return new Int32(_i | _toInt(other));
228 }
229
230 Int32 operator ^(other) {
231 if (other is Int64) {
232 return (this.toInt64() ^ other).toInt32();
233 }
234 return new Int32(_i ^ _toInt(other));
235 }
236
237 Int32 operator ~() => new Int32(~_i);
238
239 Int32 operator <<(int n) {
240 if (n < 0) {
241 throw new ArgumentError(n);
242 }
243 n &= 31;
244 return new Int32(_i << n);
245 }
246
247 Int32 operator >>(int n) {
248 if (n < 0) {
249 throw new ArgumentError(n);
250 }
251 n &= 31;
252 int value;
253 if (_i >= 0) {
254 value = _i >> n;
255 } else {
256 value = (_i >> n) | (0xffffffff << (32 - n));
257 }
258 return new Int32(value);
259 }
260
261 Int32 shiftRightUnsigned(int n) {
262 if (n < 0) {
263 throw new ArgumentError(n);
264 }
265 n &= 31;
266 int value;
267 if (_i >= 0) {
268 value = _i >> n;
269 } else {
270 value = (_i >> n) & ((1 << (32 - n)) - 1);
271 }
272 return new Int32(value);
273 }
274
275 /**
276 * Returns [:true:] if this [Int32] has the same numeric value as the
277 * given object. The argument may be an [int] or an [IntX].
278 */
279 bool operator ==(other) {
280 if (other is Int32) {
281 return _i == other._i;
282 } else if (other is Int64) {
283 return this.toInt64() == other;
284 } else if (other is int) {
285 return _i == other;
286 }
287 return false;
288 }
289
290 int compareTo(IntX other) {
291 if (other is Int64) {
292 return this.toInt64().compareTo(other);
293 }
294 return _i.compareTo(_toInt(other));
295 }
296
297 bool operator <(other) {
298 if (other is Int64) {
299 return this.toInt64() < other;
300 }
301 return _i < _toInt(other);
302 }
303
304 bool operator <=(other) {
305 if (other is Int64) {
306 return this.toInt64() <= other;
307 }
308 return _i <= _toInt(other);
309 }
310
311 bool operator >(other) {
312 if (other is Int64) {
313 return this.toInt64() > other;
314 }
315 return _i > _toInt(other);
316 }
317
318 bool operator >=(other) {
319 if (other is Int64) {
320 return this.toInt64() >= other;
321 }
322 return _i >= _toInt(other);
323 }
324
325 bool get isEven => (_i & 0x1) == 0;
326 bool get isMaxValue => _i == 2147483647;
327 bool get isMinValue => _i == -2147483648;
328 bool get isNegative => _i < 0;
329 bool get isOdd => (_i & 0x1) == 1;
330 bool get isZero => _i == 0;
331 int get bitLength => _i.bitLength;
332
333 int get hashCode => _i;
334
335 Int32 abs() => _i < 0 ? new Int32(-_i) : this;
336
337 Int32 clamp(lowerLimit, upperLimit) {
338 if (this < lowerLimit) {
339 if (lowerLimit is IntX) return lowerLimit.toInt32();
340 if (lowerLimit is int) return new Int32(lowerLimit);
341 throw new ArgumentError(lowerLimit);
342 } else if (this > upperLimit) {
343 if (upperLimit is IntX) return upperLimit.toInt32();
344 if (upperLimit is int) return new Int32(upperLimit);
345 throw new ArgumentError(upperLimit);
346 }
347 return this;
348 }
349
350 int numberOfLeadingZeros() => _numberOfLeadingZeros(_i);
351 int numberOfTrailingZeros() => _numberOfTrailingZeros(_i);
352
353 Int32 toSigned(int width) {
354 if (width < 1 || width > 32) throw new RangeError.range(width, 1, 32);
355 return new Int32(_i.toSigned(width));
356 }
357
358 Int32 toUnsigned(int width) {
359 if (width < 0 || width > 32) throw new RangeError.range(width, 0, 32);
360 return new Int32(_i.toUnsigned(width));
361 }
362
363 List<int> toBytes() {
364 List<int> result = new List<int>(4);
365 result[0] = _i & 0xff;
366 result[1] = (_i >> 8) & 0xff;
367 result[2] = (_i >> 16) & 0xff;
368 result[3] = (_i >> 24) & 0xff;
369 return result;
370 }
371
372 double toDouble() => _i.toDouble();
373 int toInt() => _i;
374 Int32 toInt32() => this;
375 Int64 toInt64() => new Int64(_i);
376
377 String toString() => _i.toString();
378 String toHexString() => _i.toRadixString(16);
379 String toRadixString(int radix) => _i.toRadixString(radix);
380 }
OLDNEW
« no previous file with comments | « pkg/fixnum/lib/fixnum.dart ('k') | pkg/fixnum/lib/src/int64.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698