| OLD | NEW |
| 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 32-bit signed integer, in the range [-2^31, 2^31 - 1]. | 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. | 9 * Arithmetic operations may overflow in order to maintain this range. |
| 10 */ | 10 */ |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 throw new Exception("Non-radix code unit: $c"); | 74 throw new Exception("Non-radix code unit: $c"); |
| 75 } | 75 } |
| 76 x = (x * radix) + digit; | 76 x = (x * radix) + digit; |
| 77 } | 77 } |
| 78 return x; | 78 return x; |
| 79 } | 79 } |
| 80 | 80 |
| 81 /** | 81 /** |
| 82 * Parses a decimal [String] and returns an [Int32]. | 82 * Parses a decimal [String] and returns an [Int32]. |
| 83 */ | 83 */ |
| 84 static Int32 parseInt(String s) => new Int32.fromInt(int.parse(s)); | 84 static Int32 parseInt(String s) => new Int32(int.parse(s)); |
| 85 | 85 |
| 86 /** | 86 /** |
| 87 * Parses a hexadecimal [String] and returns an [Int32]. | 87 * Parses a hexadecimal [String] and returns an [Int32]. |
| 88 */ | 88 */ |
| 89 static Int32 parseHex(String s) => parseRadix(s, 16); | 89 static Int32 parseHex(String s) => parseRadix(s, 16); |
| 90 | 90 |
| 91 // Assumes i is <= 32-bit. | 91 // Assumes i is <= 32-bit. |
| 92 static int _bitCount(int i) { | 92 static int _bitCount(int i) { |
| 93 // See "Hacker's Delight", section 5-1, "Counting 1-Bits". | 93 // See "Hacker's Delight", section 5-1, "Counting 1-Bits". |
| 94 | 94 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 129 |
| 130 // The internal value, kept in the range [MIN_VALUE, MAX_VALUE]. | 130 // The internal value, kept in the range [MIN_VALUE, MAX_VALUE]. |
| 131 final int _i; | 131 final int _i; |
| 132 | 132 |
| 133 const Int32._internal(int i) : _i = i; | 133 const Int32._internal(int i) : _i = i; |
| 134 | 134 |
| 135 /** | 135 /** |
| 136 * Constructs an [Int32] from an [int]. Only the low 32 bits of the input | 136 * Constructs an [Int32] from an [int]. Only the low 32 bits of the input |
| 137 * are used. | 137 * are used. |
| 138 */ | 138 */ |
| 139 Int32.fromInt(int i) : _i = (i & 0x7fffffff) - (i & 0x80000000); | 139 Int32([int i=0]) : _i = (i & 0x7fffffff) - (i & 0x80000000); |
| 140 |
| 141 /** |
| 142 * Constructs an [Int32] from an [int]. Only the low 32 bits of the input |
| 143 * are used. |
| 144 */ |
| 145 @deprecated |
| 146 Int32.fromInt(int i) : this(i); |
| 140 | 147 |
| 141 // Returns the [int] representation of the specified value. Throws | 148 // Returns the [int] representation of the specified value. Throws |
| 142 // [ArgumentError] for non-integer arguments. | 149 // [ArgumentError] for non-integer arguments. |
| 143 int _toInt(val) { | 150 int _toInt(val) { |
| 144 if (val is Int32) { | 151 if (val is Int32) { |
| 145 return val._i; | 152 return val._i; |
| 146 } else if (val is int) { | 153 } else if (val is int) { |
| 147 return val; | 154 return val; |
| 148 } | 155 } |
| 149 throw new ArgumentError(val); | 156 throw new ArgumentError(val); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 160 // left-hand side: | 167 // left-hand side: |
| 161 // | 168 // |
| 162 // Int32 % int => Int32 | 169 // Int32 % int => Int32 |
| 163 // Int32 % Int32 => Int32 | 170 // Int32 % Int32 => Int32 |
| 164 // Int32 % Int64 => Int32 | 171 // Int32 % Int64 => Int32 |
| 165 | 172 |
| 166 IntX operator +(other) { | 173 IntX operator +(other) { |
| 167 if (other is Int64) { | 174 if (other is Int64) { |
| 168 return this.toInt64() + other; | 175 return this.toInt64() + other; |
| 169 } | 176 } |
| 170 return new Int32.fromInt(_i + _toInt(other)); | 177 return new Int32(_i + _toInt(other)); |
| 171 } | 178 } |
| 172 | 179 |
| 173 IntX operator -(other) { | 180 IntX operator -(other) { |
| 174 if (other is Int64) { | 181 if (other is Int64) { |
| 175 return this.toInt64() - other; | 182 return this.toInt64() - other; |
| 176 } | 183 } |
| 177 return new Int32.fromInt(_i - _toInt(other)); | 184 return new Int32(_i - _toInt(other)); |
| 178 } | 185 } |
| 179 | 186 |
| 180 Int32 operator -() => new Int32.fromInt(-_i); | 187 Int32 operator -() => new Int32(-_i); |
| 181 | 188 |
| 182 IntX operator *(other) { | 189 IntX operator *(other) { |
| 183 if (other is Int64) { | 190 if (other is Int64) { |
| 184 return this.toInt64() * other; | 191 return this.toInt64() * other; |
| 185 } | 192 } |
| 186 // TODO(rice) - optimize | 193 // TODO(rice) - optimize |
| 187 return (this.toInt64() * other).toInt32(); | 194 return (this.toInt64() * other).toInt32(); |
| 188 } | 195 } |
| 189 | 196 |
| 190 Int32 operator %(other) { | 197 Int32 operator %(other) { |
| 191 if (other is Int64) { | 198 if (other is Int64) { |
| 192 // Result will be Int32 | 199 // Result will be Int32 |
| 193 return (this.toInt64() % other).toInt32(); | 200 return (this.toInt64() % other).toInt32(); |
| 194 } | 201 } |
| 195 return new Int32.fromInt(_i % _toInt(other)); | 202 return new Int32(_i % _toInt(other)); |
| 196 } | 203 } |
| 197 | 204 |
| 198 Int32 operator ~/(other) { | 205 Int32 operator ~/(other) { |
| 199 if (other is Int64) { | 206 if (other is Int64) { |
| 200 return (this.toInt64() ~/ other).toInt32(); | 207 return (this.toInt64() ~/ other).toInt32(); |
| 201 } | 208 } |
| 202 return new Int32.fromInt(_i ~/ _toInt(other)); | 209 return new Int32(_i ~/ _toInt(other)); |
| 203 } | 210 } |
| 204 | 211 |
| 205 Int32 remainder(other) { | 212 Int32 remainder(other) { |
| 206 if (other is Int64) { | 213 if (other is Int64) { |
| 207 Int64 t = this.toInt64(); | 214 Int64 t = this.toInt64(); |
| 208 return (t - (t ~/ other) * other).toInt32(); | 215 return (t - (t ~/ other) * other).toInt32(); |
| 209 } | 216 } |
| 210 return this - (this ~/ other) * other; | 217 return this - (this ~/ other) * other; |
| 211 } | 218 } |
| 212 | 219 |
| 213 Int32 operator &(other) { | 220 Int32 operator &(other) { |
| 214 if (other is Int64) { | 221 if (other is Int64) { |
| 215 return (this.toInt64() & other).toInt32(); | 222 return (this.toInt64() & other).toInt32(); |
| 216 } | 223 } |
| 217 return new Int32.fromInt(_i & _toInt(other)); | 224 return new Int32(_i & _toInt(other)); |
| 218 } | 225 } |
| 219 | 226 |
| 220 Int32 operator |(other) { | 227 Int32 operator |(other) { |
| 221 if (other is Int64) { | 228 if (other is Int64) { |
| 222 return (this.toInt64() | other).toInt32(); | 229 return (this.toInt64() | other).toInt32(); |
| 223 } | 230 } |
| 224 return new Int32.fromInt(_i | _toInt(other)); | 231 return new Int32(_i | _toInt(other)); |
| 225 } | 232 } |
| 226 | 233 |
| 227 Int32 operator ^(other) { | 234 Int32 operator ^(other) { |
| 228 if (other is Int64) { | 235 if (other is Int64) { |
| 229 return (this.toInt64() ^ other).toInt32(); | 236 return (this.toInt64() ^ other).toInt32(); |
| 230 } | 237 } |
| 231 return new Int32.fromInt(_i ^ _toInt(other)); | 238 return new Int32(_i ^ _toInt(other)); |
| 232 } | 239 } |
| 233 | 240 |
| 234 Int32 operator ~() => new Int32.fromInt(~_i); | 241 Int32 operator ~() => new Int32(~_i); |
| 235 | 242 |
| 236 Int32 operator <<(int n) { | 243 Int32 operator <<(int n) { |
| 237 if (n < 0) { | 244 if (n < 0) { |
| 238 throw new ArgumentError(n); | 245 throw new ArgumentError(n); |
| 239 } | 246 } |
| 240 n &= 31; | 247 n &= 31; |
| 241 return new Int32.fromInt(_i << n); | 248 return new Int32(_i << n); |
| 242 } | 249 } |
| 243 | 250 |
| 244 Int32 operator >>(int n) { | 251 Int32 operator >>(int n) { |
| 245 if (n < 0) { | 252 if (n < 0) { |
| 246 throw new ArgumentError(n); | 253 throw new ArgumentError(n); |
| 247 } | 254 } |
| 248 n &= 31; | 255 n &= 31; |
| 249 int value; | 256 int value; |
| 250 if (_i >= 0) { | 257 if (_i >= 0) { |
| 251 value = _i >> n; | 258 value = _i >> n; |
| 252 } else { | 259 } else { |
| 253 value = (_i >> n) | (0xffffffff << (32 - n)); | 260 value = (_i >> n) | (0xffffffff << (32 - n)); |
| 254 } | 261 } |
| 255 return new Int32.fromInt(value); | 262 return new Int32(value); |
| 256 } | 263 } |
| 257 | 264 |
| 258 Int32 shiftRightUnsigned(int n) { | 265 Int32 shiftRightUnsigned(int n) { |
| 259 if (n < 0) { | 266 if (n < 0) { |
| 260 throw new ArgumentError(n); | 267 throw new ArgumentError(n); |
| 261 } | 268 } |
| 262 n &= 31; | 269 n &= 31; |
| 263 int value; | 270 int value; |
| 264 if (_i >= 0) { | 271 if (_i >= 0) { |
| 265 value = _i >> n; | 272 value = _i >> n; |
| 266 } else { | 273 } else { |
| 267 value = (_i >> n) & ((1 << (32 - n)) - 1); | 274 value = (_i >> n) & ((1 << (32 - n)) - 1); |
| 268 } | 275 } |
| 269 return new Int32.fromInt(value); | 276 return new Int32(value); |
| 270 } | 277 } |
| 271 | 278 |
| 272 /** | 279 /** |
| 273 * Returns [true] if this [Int32] has the same numeric value as the | 280 * Returns [true] if this [Int32] has the same numeric value as the |
| 274 * given object. The argument may be an [int] or an [IntX]. | 281 * given object. The argument may be an [int] or an [IntX]. |
| 275 */ | 282 */ |
| 276 bool operator ==(other) { | 283 bool operator ==(other) { |
| 277 if (other is Int32) { | 284 if (other is Int32) { |
| 278 return _i == other._i; | 285 return _i == other._i; |
| 279 } else if (other is Int64) { | 286 } else if (other is Int64) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 328 |
| 322 bool get isEven => (_i & 0x1) == 0; | 329 bool get isEven => (_i & 0x1) == 0; |
| 323 bool get isMaxValue => _i == 2147483647; | 330 bool get isMaxValue => _i == 2147483647; |
| 324 bool get isMinValue => _i == -2147483648; | 331 bool get isMinValue => _i == -2147483648; |
| 325 bool get isNegative => _i < 0; | 332 bool get isNegative => _i < 0; |
| 326 bool get isOdd => (_i & 0x1) == 1; | 333 bool get isOdd => (_i & 0x1) == 1; |
| 327 bool get isZero => _i == 0; | 334 bool get isZero => _i == 0; |
| 328 | 335 |
| 329 int get hashCode => _i; | 336 int get hashCode => _i; |
| 330 | 337 |
| 331 Int32 abs() => _i < 0 ? new Int32.fromInt(-_i) : this; | 338 Int32 abs() => _i < 0 ? new Int32(-_i) : this; |
| 332 | 339 |
| 333 int numberOfLeadingZeros() => _numberOfLeadingZeros(_i); | 340 int numberOfLeadingZeros() => _numberOfLeadingZeros(_i); |
| 334 int numberOfTrailingZeros() => _numberOfTrailingZeros(_i); | 341 int numberOfTrailingZeros() => _numberOfTrailingZeros(_i); |
| 335 | 342 |
| 336 List<int> toBytes() { | 343 List<int> toBytes() { |
| 337 List<int> result = new List<int>(4); | 344 List<int> result = new List<int>(4); |
| 338 result[0] = _i & 0xff; | 345 result[0] = _i & 0xff; |
| 339 result[1] = (_i >> 8) & 0xff; | 346 result[1] = (_i >> 8) & 0xff; |
| 340 result[2] = (_i >> 16) & 0xff; | 347 result[2] = (_i >> 16) & 0xff; |
| 341 result[3] = (_i >> 24) & 0xff; | 348 result[3] = (_i >> 24) & 0xff; |
| 342 return result; | 349 return result; |
| 343 } | 350 } |
| 344 | 351 |
| 345 int toInt() => _i; | 352 int toInt() => _i; |
| 346 Int32 toInt32() => this; | 353 Int32 toInt32() => this; |
| 347 Int64 toInt64() => new Int64.fromInt(_i); | 354 Int64 toInt64() => new Int64(_i); |
| 348 | 355 |
| 349 String toString() => _i.toString(); | 356 String toString() => _i.toString(); |
| 350 String toHexString() => _i.toRadixString(16); | 357 String toHexString() => _i.toRadixString(16); |
| 351 String toRadixString(int radix) => _i.toRadixString(radix); | 358 String toRadixString(int radix) => _i.toRadixString(radix); |
| 352 } | 359 } |
| OLD | NEW |