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

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

Issue 10854162: Move fixnum to from lib/ to pkg/ . Once pub.dartlang.org (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 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
(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 /**
6 * An immutable 32-bit signed integer, in the range [-2^31, 2^31 - 1].
7 * Arithmetic operations may overflow in order to maintain this range.
8 */
9 class int32 implements intx {
10
11 /**
12 * The maximum positive value attainable by an [int32], namely
13 * 2147483647.
14 */
15 static final int32 MAX_VALUE = const int32._internal(0x7FFFFFFF);
16
17 /**
18 * The minimum positive value attainable by an [int32], namely
19 * -2147483648.
20 */
21 static int32 MIN_VALUE = const int32._internal(0x80000000);
22
23 /**
24 * An [int32] constant equal to 0.
25 */
26 static int32 ZERO = const int32._internal(0);
27
28 /**
29 * An [int32] constant equal to 1.
30 */
31 static int32 ONE = const int32._internal(1);
32
33 /**
34 * An [int32] constant equal to 2.
35 */
36 static int32 TWO = const int32._internal(2);
37
38 // Hex digit char codes
39 static final int _CC_0 = 48; // '0'.charCodeAt(0)
40 static final int _CC_9 = 57; // '9'.charCodeAt(0)
41 static final int _CC_a = 97; // 'a'.charCodeAt(0)
42 static final int _CC_z = 122; // 'z'.charCodeAt(0)
43 static final int _CC_A = 65; // 'A'.charCodeAt(0)
44 static final int _CC_Z = 90; // 'Z'.charCodeAt(0)
45
46 static int _decodeHex(int c) {
47 if (c >= _CC_0 && c <= _CC_9) {
48 return c - _CC_0;
49 } else if (c >= _CC_a && c <= _CC_z) {
50 return c - _CC_a + 10;
51 } else if (c >= _CC_A && c <= _CC_Z) {
52 return c - _CC_A + 10;
53 } else {
54 return -1; // bad char code
55 }
56 }
57
58 /**
59 * Parses a [String] in a given [radix] between 2 and 16 and returns an
60 * [int32].
61 */
62 // TODO(rice) - Make this faster by converting several digits at once.
63 static int32 parseRadix(String s, int radix) {
64 if ((radix <= 1) || (radix > 16)) {
65 throw "Bad radix: $radix";
66 }
67 int32 x = ZERO;
68 for (int i = 0; i < s.length; i++) {
69 int c = s.charCodeAt(i);
70 int digit = _decodeHex(c);
71 if (digit < 0 || digit >= radix) {
72 throw new Exception("Non-radix char code: $c");
73 }
74 x = (x * radix) + digit;
75 }
76 return x;
77 }
78
79 /**
80 * Parses a decimal [String] and returns an [int32].
81 */
82 static int32 parseInt(String s) => new int32.fromInt(Math.parseInt(s));
83
84 /**
85 * Parses a hexadecimal [String] and returns an [int32].
86 */
87 static int32 parseHex(String s) => parseRadix(s, 16);
88
89 // Assumes i is <= 32-bit.
90 static int _bitCount(int i) {
91 // See "Hacker's Delight", section 5-1, "Counting 1-Bits".
92
93 // The basic strategy is to use "divide and conquer" to
94 // add pairs (then quads, etc.) of bits together to obtain
95 // sub-counts.
96 //
97 // A straightforward approach would look like:
98 //
99 // i = (i & 0x55555555) + ((i >> 1) & 0x55555555);
100 // i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
101 // i = (i & 0x0F0F0F0F) + ((i >> 4) & 0x0F0F0F0F);
102 // i = (i & 0x00FF00FF) + ((i >> 8) & 0x00FF00FF);
103 // i = (i & 0x0000FFFF) + ((i >> 16) & 0x0000FFFF);
104 //
105 // The code below removes unnecessary &'s and uses a
106 // trick to remove one instruction in the first line.
107
108 i -= ((i >> 1) & 0x55555555);
109 i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
110 i = ((i + (i >> 4)) & 0x0F0F0F0F);
111 i += (i >> 8);
112 i += (i >> 16);
113 return (i & 0x0000003F);
114 }
115
116 // Assumes i is <= 32-bit
117 static int _numberOfLeadingZeros(int i) {
118 i |= i >> 1;
119 i |= i >> 2;
120 i |= i >> 4;
121 i |= i >> 8;
122 i |= i >> 16;
123 return _bitCount(~i);
124 }
125
126 static int _numberOfTrailingZeros(int i) => _bitCount((i & -i) - 1);
127
128 // The internal value, kept in the range [MIN_VALUE, MAX_VALUE].
129 final int _i;
130
131 const int32._internal(int i) : _i = i;
132
133 /**
134 * Constructs an [int32] from an [int]. Only the low 32 bits of the input
135 * are used.
136 */
137 int32.fromInt(int i) : _i = (i & 0x7fffffff) - (i & 0x80000000);
138
139 // Convert an [int] or [intx] to an [int32]. Note that an [int64]
140 // will be truncated.
141 int _convert(other) {
142 if (other == null) {
143 throw new NullPointerException();
144 } else if (other is intx) {
145 return other.toInt32()._i;
146 } else if (other is int) {
147 return other;
148 } else {
149 throw new Exception("Can't retrieve 32-bit int from $other");
150 }
151 }
152
153 // The +, -, * , &, |, and ^ operaters deal with types as follows:
154 //
155 // int32 + int => int32
156 // int32 + int32 => int32
157 // int32 + int64 => int64
158 //
159 // The %, ~/ and remainder operators return an int32 even with an int64
160 // argument, since the result cannot be greater than the value on the
161 // left-hand side:
162 //
163 // int32 % int => int32
164 // int32 % int32 => int32
165 // int32 % int64 => int32
166
167 intx operator +(other) {
168 if (other is int64) {
169 return this.toInt64() + other;
170 }
171 return new int32.fromInt(_i + _convert(other));
172 }
173
174 intx operator -(other) {
175 if (other is int64) {
176 return this.toInt64() - other;
177 }
178 return new int32.fromInt(_i - _convert(other));
179 }
180
181 int32 operator negate() => new int32.fromInt(-_i);
182
183 intx operator *(other) {
184 if (other is int64) {
185 return this.toInt64() * other;
186 }
187 // TODO(rice) - optimize
188 return (this.toInt64() * other).toInt32();
189 }
190
191 int32 operator %(other) {
192 if (other is int64) {
193 // Result will be int32
194 return (this.toInt64() % other).toInt32();
195 }
196 return new int32.fromInt(_i % _convert(other));
197 }
198
199 int32 operator ~/(other) {
200 if (other is int64) {
201 // Result will be int32
202 return (this.toInt64() ~/ other).toInt32();
203 }
204 return new int32.fromInt(_i ~/ _convert(other));
205 }
206
207 int32 remainder(other) {
208 if (other is int64) {
209 // Result will be int32
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.fromInt(_i & _convert(other));
221 }
222
223 int32 operator |(other) {
224 if (other is int64) {
225 return (this.toInt64() | other).toInt32();
226 }
227 return new int32.fromInt(_i | _convert(other));
228 }
229
230 int32 operator ^(other) {
231 if (other is int64) {
232 return (this.toInt64() ^ other).toInt32();
233 }
234 return new int32.fromInt(_i ^ _convert(other));
235 }
236
237 int32 operator ~() => new int32.fromInt(~_i);
238
239 int32 operator <<(int n) {
240 if (n < 0) {
241 throw new IllegalArgumentException("$n");
242 }
243 n &= 31;
244 return new int32.fromInt(_i << n);
245 }
246
247 int32 operator >>(int n) {
248 if (n < 0) {
249 throw new IllegalArgumentException("$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.fromInt(value);
259 }
260
261 int32 shiftRightUnsigned(int n) {
262 if (n < 0) {
263 throw new IllegalArgumentException("$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.fromInt(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 == null) {
281 return false;
282 }
283 if (other is int64) {
284 return this.toInt64() == other;
285 }
286 return _i == _convert(other);
287 }
288
289 int compareTo(Comparable other) {
290 if (other is int64) {
291 return this.toInt64().compareTo(other);
292 }
293 return _i.compareTo(_convert(other));
294 }
295
296 bool operator <(other) {
297 if (other is int64) {
298 return this.toInt64() < other;
299 }
300 return _i < _convert(other);
301 }
302
303 bool operator <=(other) {
304 if (other is int64) {
305 return this.toInt64() < other;
306 }
307 return _i <= _convert(other);
308 }
309
310 bool operator >(other) {
311 if (other is int64) {
312 return this.toInt64() < other;
313 }
314 return _i > _convert(other);
315 }
316
317 bool operator >=(other) {
318 if (other is int64) {
319 return this.toInt64() < other;
320 }
321 return _i >= _convert(other);
322 }
323
324 bool isEven() => (_i & 0x1) == 0;
325 bool isMaxValue() => _i == 2147483647;
326 bool isMinValue() => _i == -2147483648;
327 bool isNegative() => _i < 0;
328 bool isOdd() => (_i & 0x1) == 1;
329 bool isZero() => _i == 0;
330
331 int hashCode() => _i;
332
333 int32 abs() => _i < 0 ? new int32.fromInt(-_i) : this;
334
335 int numberOfLeadingZeros() => _numberOfLeadingZeros(_i);
336 int numberOfTrailingZeros() => _numberOfTrailingZeros(_i);
337
338 List<int> toBytes() {
339 List<int> result = new List<int>(4);
340 result[0] = _i & 0xff;
341 result[1] = (_i >> 8) & 0xff;
342 result[2] = (_i >> 16) & 0xff;
343 result[3] = (_i >> 24) & 0xff;
344 return result;
345 }
346
347 int toInt() => _i;
348 int32 toInt32() => this;
349 int64 toInt64() => new int64.fromInt(_i);
350
351 String toString() => _i.toString();
352 String toHexString() => _i.toRadixString(16);
353 String toRadixString(int radix) => _i.toRadixString(radix);
354 }
OLDNEW
« no previous file with comments | « lib/fixnum/fixnum.dart ('k') | lib/fixnum/int64.dart » ('j') | pkg/fixnum/pubspec.yaml » ('J')

Powered by Google App Engine
This is Rietveld 408576698