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

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

Issue 11405003: Update fixnum to new package guidelines. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month 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
(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 int32 MIN_VALUE = const int32._internal(0x80000000);
24
25 /**
26 * An [int32] constant equal to 0.
27 */
28 static int32 ZERO = const int32._internal(0);
29
30 /**
31 * An [int32] constant equal to 1.
32 */
33 static int32 ONE = const int32._internal(1);
34
35 /**
36 * An [int32] constant equal to 2.
37 */
38 static int32 TWO = const int32._internal(2);
39
40 // Hex digit char codes
41 static const int _CC_0 = 48; // '0'.charCodeAt(0)
42 static const int _CC_9 = 57; // '9'.charCodeAt(0)
43 static const int _CC_a = 97; // 'a'.charCodeAt(0)
44 static const int _CC_z = 122; // 'z'.charCodeAt(0)
45 static const int _CC_A = 65; // 'A'.charCodeAt(0)
46 static const int _CC_Z = 90; // 'Z'.charCodeAt(0)
47
48 static int _decodeHex(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 /**
61 * Parses a [String] in a given [radix] between 2 and 16 and returns an
62 * [int32].
63 */
64 // TODO(rice) - Make this faster by converting several digits at once.
65 static int32 parseRadix(String s, int radix) {
66 if ((radix <= 1) || (radix > 16)) {
67 throw "Bad radix: $radix";
68 }
69 int32 x = ZERO;
70 for (int i = 0; i < s.length; i++) {
71 int c = s.charCodeAt(i);
72 int digit = _decodeHex(c);
73 if (digit < 0 || digit >= radix) {
74 throw new Exception("Non-radix char code: $c");
75 }
76 x = (x * radix) + digit;
77 }
78 return x;
79 }
80
81 /**
82 * Parses a decimal [String] and returns an [int32].
83 */
84 static int32 parseInt(String s) => new int32.fromInt(int.parse(s));
85
86 /**
87 * Parses a hexadecimal [String] and returns an [int32].
88 */
89 static int32 parseHex(String s) => parseRadix(s, 16);
90
91 // Assumes i is <= 32-bit.
92 static int _bitCount(int i) {
93 // See "Hacker's Delight", section 5-1, "Counting 1-Bits".
94
95 // The basic strategy is to use "divide and conquer" to
96 // add pairs (then quads, etc.) of bits together to obtain
97 // sub-counts.
98 //
99 // A straightforward approach would look like:
100 //
101 // i = (i & 0x55555555) + ((i >> 1) & 0x55555555);
102 // i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
103 // i = (i & 0x0F0F0F0F) + ((i >> 4) & 0x0F0F0F0F);
104 // i = (i & 0x00FF00FF) + ((i >> 8) & 0x00FF00FF);
105 // i = (i & 0x0000FFFF) + ((i >> 16) & 0x0000FFFF);
106 //
107 // The code below removes unnecessary &'s and uses a
108 // trick to remove one instruction in the first line.
109
110 i -= ((i >> 1) & 0x55555555);
111 i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
112 i = ((i + (i >> 4)) & 0x0F0F0F0F);
113 i += (i >> 8);
114 i += (i >> 16);
115 return (i & 0x0000003F);
116 }
117
118 // Assumes i is <= 32-bit
119 static int _numberOfLeadingZeros(int i) {
120 i |= i >> 1;
121 i |= i >> 2;
122 i |= i >> 4;
123 i |= i >> 8;
124 i |= i >> 16;
125 return _bitCount(~i);
126 }
127
128 static int _numberOfTrailingZeros(int i) => _bitCount((i & -i) - 1);
129
130 // The internal value, kept in the range [MIN_VALUE, MAX_VALUE].
131 final int _i;
132
133 const int32._internal(int i) : _i = i;
134
135 /**
136 * Constructs an [int32] from an [int]. Only the low 32 bits of the input
137 * are used.
138 */
139 int32.fromInt(int i) : _i = (i & 0x7fffffff) - (i & 0x80000000);
140
141 // Convert an [int] or [intx] to an [int32]. Note that an [int64]
142 // will be truncated.
143 int _convert(other) {
144 if (other == null) {
145 throw new NullPointerException();
146 } else if (other is intx) {
147 return other.toInt32()._i;
148 } else if (other is int) {
149 return other;
150 } else {
151 throw new Exception("Can't retrieve 32-bit int from $other");
152 }
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.fromInt(_i + _convert(other));
174 }
175
176 intx operator -(other) {
177 if (other is int64) {
178 return this.toInt64() - other;
179 }
180 return new int32.fromInt(_i - _convert(other));
181 }
182
183 int32 operator -() => new int32.fromInt(-_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.fromInt(_i % _convert(other));
199 }
200
201 int32 operator ~/(other) {
202 if (other is int64) {
203 // Result will be int32
204 return (this.toInt64() ~/ other).toInt32();
205 }
206 return new int32.fromInt(_i ~/ _convert(other));
207 }
208
209 int32 remainder(other) {
210 if (other is int64) {
211 // Result will be int32
212 int64 t = this.toInt64();
213 return (t - (t ~/ other) * other).toInt32();
214 }
215 return this - (this ~/ other) * other;
216 }
217
218 int32 operator &(other) {
219 if (other is int64) {
220 return (this.toInt64() & other).toInt32();
221 }
222 return new int32.fromInt(_i & _convert(other));
223 }
224
225 int32 operator |(other) {
226 if (other is int64) {
227 return (this.toInt64() | other).toInt32();
228 }
229 return new int32.fromInt(_i | _convert(other));
230 }
231
232 int32 operator ^(other) {
233 if (other is int64) {
234 return (this.toInt64() ^ other).toInt32();
235 }
236 return new int32.fromInt(_i ^ _convert(other));
237 }
238
239 int32 operator ~() => new int32.fromInt(~_i);
240
241 int32 operator <<(int n) {
242 if (n < 0) {
243 throw new ArgumentError("$n");
244 }
245 n &= 31;
246 return new int32.fromInt(_i << n);
247 }
248
249 int32 operator >>(int n) {
250 if (n < 0) {
251 throw new ArgumentError("$n");
252 }
253 n &= 31;
254 int value;
255 if (_i >= 0) {
256 value = _i >> n;
257 } else {
258 value = (_i >> n) | (0xffffffff << (32 - n));
259 }
260 return new int32.fromInt(value);
261 }
262
263 int32 shiftRightUnsigned(int n) {
264 if (n < 0) {
265 throw new ArgumentError("$n");
266 }
267 n &= 31;
268 int value;
269 if (_i >= 0) {
270 value = _i >> n;
271 } else {
272 value = (_i >> n) & ((1 << (32 - n)) - 1);
273 }
274 return new int32.fromInt(value);
275 }
276
277 /**
278 * Returns [true] if this [int32] has the same numeric value as the
279 * given object. The argument may be an [int] or an [intx].
280 */
281 bool operator ==(other) {
282 if (other == null) {
283 return false;
284 }
285 if (other is int64) {
286 return this.toInt64() == other;
287 }
288 return _i == _convert(other);
289 }
290
291 int compareTo(Comparable other) {
292 if (other is int64) {
293 return this.toInt64().compareTo(other);
294 }
295 return _i.compareTo(_convert(other));
296 }
297
298 bool operator <(other) {
299 if (other is int64) {
300 return this.toInt64() < other;
301 }
302 return _i < _convert(other);
303 }
304
305 bool operator <=(other) {
306 if (other is int64) {
307 return this.toInt64() < other;
308 }
309 return _i <= _convert(other);
310 }
311
312 bool operator >(other) {
313 if (other is int64) {
314 return this.toInt64() < other;
315 }
316 return _i > _convert(other);
317 }
318
319 bool operator >=(other) {
320 if (other is int64) {
321 return this.toInt64() < other;
322 }
323 return _i >= _convert(other);
324 }
325
326 bool get isEven => (_i & 0x1) == 0;
327 bool get isMaxValue => _i == 2147483647;
328 bool get isMinValue => _i == -2147483648;
329 bool get isNegative => _i < 0;
330 bool get isOdd => (_i & 0x1) == 1;
331 bool get isZero => _i == 0;
332
333 int get hashCode => _i;
334
335 int32 abs() => _i < 0 ? new int32.fromInt(-_i) : this;
336
337 int numberOfLeadingZeros() => _numberOfLeadingZeros(_i);
338 int numberOfTrailingZeros() => _numberOfTrailingZeros(_i);
339
340 List<int> toBytes() {
341 List<int> result = new List<int>(4);
342 result[0] = _i & 0xff;
343 result[1] = (_i >> 8) & 0xff;
344 result[2] = (_i >> 16) & 0xff;
345 result[3] = (_i >> 24) & 0xff;
346 return result;
347 }
348
349 int toInt() => _i;
350 int32 toInt32() => this;
351 int64 toInt64() => new int64.fromInt(_i);
352
353 String toString() => _i.toString();
354 String toHexString() => _i.toRadixString(16);
355 String toRadixString(int radix) => _i.toRadixString(radix);
356 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698