Comparing version 0.0.2 to 0.0.3
@@ -0,1 +1,8 @@ | ||
0.0.3 / 2014-01-02 | ||
================== | ||
* shiftLeft() and shiftRight() fixes when n > 16 | ||
* not() fix | ||
* adjusted fromString() slice from 8 to 6 to avoid radix overflow | ||
0.0.2 / 2014-01-02 | ||
@@ -2,0 +9,0 @@ ================== |
exports.UINT32 = require('./lib/uint32') | ||
// exports.UINT64 = require('./lib/uint64') | ||
exports.UINT64 = require('./lib/uint64') |
@@ -10,5 +10,5 @@ /** | ||
var radixPowerCache = { | ||
16: UINT32( Math.pow(16, 8) ) | ||
, 10: UINT32( Math.pow(10, 8) ) | ||
, 2: UINT32( Math.pow(2, 8) ) | ||
16: UINT32( Math.pow(16, 6) ) | ||
, 10: UINT32( Math.pow(10, 6) ) | ||
, 2: UINT32( Math.pow(2, 6) ) | ||
} | ||
@@ -21,2 +21,4 @@ var radixCache = { | ||
var ONE = new UINT32(1) | ||
/** | ||
@@ -85,9 +87,9 @@ * Represents an unsigned 32 bits integer | ||
var radixUint = radixPowerCache[radix] || new UINT32( Math.pow(radix, 8) ) | ||
var radixUint = radixPowerCache[radix] || new UINT32( Math.pow(radix, 6) ) | ||
for (var i = 0, len = s.length; i < len; i += 8) { | ||
var size = Math.min(8, len - i) | ||
for (var i = 0, len = s.length; i < len; i += 6) { | ||
var size = Math.min(6, len - i) | ||
var value = parseInt( s.slice(i, i + size), radix ) | ||
this.multiply( | ||
size < 8 | ||
size < 6 | ||
? new UINT32( Math.pow(radix, size) ) | ||
@@ -122,3 +124,3 @@ : radixUint | ||
if ( this.lt(radixUint) ) return this.toNumber().toString(radix) | ||
if ( !this.gt(radixUint) ) return this.toNumber().toString(radix) | ||
@@ -278,6 +280,6 @@ var self = this.clone() | ||
UINT32.prototype.negate = UINT32.prototype.not = function () { | ||
this._low = (~this._low & 0xFFFF) + 1 | ||
this._low = ~this._low & 0xFFFF | ||
this._high = ~this._high & 0xFFFF | ||
return this | ||
return this.add(ONE) | ||
} | ||
@@ -372,4 +374,4 @@ | ||
} else { | ||
this._low = ( (this._low >>> n) | (this._high << (16-n)) ) & 0xFFFF | ||
this._high = (this._high >> n) & 0xFFFF | ||
this._low = (this._low >>> n) | ( (this._high << (16-n)) & 0xFFFF ) | ||
this._high = this._high >> n | ||
} | ||
@@ -391,2 +393,5 @@ | ||
this._low = 0 | ||
if (!allowOverflow) { | ||
this._high &= 0xFFFF | ||
} | ||
} else if (n == 16) { | ||
@@ -393,0 +398,0 @@ this._high = this._low |
@@ -10,5 +10,5 @@ /** | ||
var radixPowerCache = { | ||
16: UINT64( Math.pow(16, 8) ) | ||
, 10: UINT64( Math.pow(10, 8) ) | ||
, 2: UINT64( Math.pow(2, 8) ) | ||
16: UINT64( Math.pow(16, 6) ) | ||
, 10: UINT64( Math.pow(10, 6) ) | ||
, 2: UINT64( Math.pow(2, 6) ) | ||
} | ||
@@ -21,2 +21,4 @@ var radixCache = { | ||
var ONE = new UINT64(1) | ||
/** | ||
@@ -107,12 +109,14 @@ * Represents an unsigned 64 bits integer | ||
this._low = 0 | ||
this._high = 0 | ||
this._a00 = 0 | ||
this._a16 = 0 | ||
this._a32 = 0 | ||
this._a48 = 0 | ||
var radixUint = radixPowerCache[radix] || new UINT64( Math.pow(radix, 8) ) | ||
var radixUint = radixPowerCache[radix] || new UINT64( Math.pow(radix, 6) ) | ||
for (var i = 0, len = s.length; i < len; i += 8) { | ||
var size = Math.min(8, len - i) | ||
for (var i = 0, len = s.length; i < len; i += 6) { | ||
var size = Math.min(6, len - i) | ||
var value = parseInt( s.slice(i, i + size), radix ) | ||
this.multiply( | ||
size < 8 | ||
size < 6 | ||
? new UINT64( Math.pow(radix, size) ) | ||
@@ -147,7 +151,7 @@ : radixUint | ||
if ( this.lt(radixUint) ) return this.toNumber().toString(radix) | ||
if ( !this.gt(radixUint) ) return this.toNumber().toString(radix) | ||
var self = this.clone() | ||
var res = new Array(32) | ||
for (var i = 31; i >= 0; i--) { | ||
var res = new Array(64) | ||
for (var i = 63; i >= 0; i--) { | ||
self.div(radixUint, true) | ||
@@ -231,4 +235,4 @@ res[i] = self.remainder.toNumber().toString(radix) | ||
var b16 = other._a16 | ||
var b32 = other._b32 | ||
var b48 = other._b48 | ||
var b32 = other._a32 | ||
var b48 = other._a48 | ||
@@ -239,19 +243,22 @@ var c00 = a00 * b00 | ||
c16 += a00 * b16 | ||
c16 &= 0xFFFF // Not required but improves performance | ||
var c32 = c16 >>> 16 | ||
c16 &= 0xFFFF | ||
c16 += a16 * b00 | ||
var c32 = c16 >>> 16 | ||
c32 += c16 >>> 16 | ||
c32 += a00 * b32 | ||
c32 &= 0xFFFF // Not required but improves performance | ||
var c48 = c32 >>> 16 | ||
c32 &= 0xFFFF | ||
c32 += a16 * b16 | ||
c32 &= 0xFFFF // Not required but improves performance | ||
c48 += c32 >>> 16 | ||
c32 &= 0xFFFF | ||
c32 += a32 * b00 | ||
var c48 = c32 >>> 16 | ||
c48 += c32 >>> 16 | ||
c48 += a00 * b48 | ||
c48 &= 0xFFFF // Not required but improves performance | ||
c48 &= 0xFFFF | ||
c48 += a16 * b32 | ||
c48 &= 0xFFFF // Not required but improves performance | ||
c48 &= 0xFFFF | ||
c48 += a32 * b16 | ||
c48 &= 0xFFFF // Not required but improves performance | ||
c48 &= 0xFFFF | ||
c48 += a48 * b00 | ||
@@ -276,12 +283,15 @@ | ||
UINT64.prototype.div = function (other) { | ||
if ( (other._low == 0) && (other._high == 0) ) throw Error('division by zero') | ||
if ( (other._a16 == 0) && (other._a32 == 0) && (other._a48 == 0) ) { | ||
if (other._a00 == 0) throw Error('division by zero') | ||
// other == 1 or this == 1 | ||
if ((other._high == 0 && other._low == 1) || (this._high == 0 && this._low == 1)) | ||
return this | ||
// other == 1: this | ||
if (other._a00 == 1) return this | ||
} | ||
// other > this: 0 | ||
if ( other.gt(this) ) { | ||
this._low = 0 | ||
this._high = 0 | ||
this._a00 = 0 | ||
this._a16 = 0 | ||
this._a32 = 0 | ||
this._a48 = 0 | ||
return this | ||
@@ -291,4 +301,6 @@ } | ||
if ( this.eq(other) ) { | ||
this._low = 1 | ||
this._high = 0 | ||
this._a00 = 1 | ||
this._a16 = 0 | ||
this._a32 = 0 | ||
this._a48 = 0 | ||
return this | ||
@@ -311,4 +323,6 @@ } | ||
// Initialize the current result to 0 | ||
this._low = 0 | ||
this._high = 0 | ||
this._a00 = 0 | ||
this._a16 = 0 | ||
this._a32 = 0 | ||
this._a48 = 0 | ||
for (; i >= 0; i--) { | ||
@@ -321,6 +335,10 @@ _other.shiftRight(1) | ||
// Update the current result | ||
if (i > 16) { | ||
this._high |= 1 << (i - 16) | ||
if (i > 48) { | ||
this._a48 |= 1 << (i - 48) | ||
} else if (i > 32) { | ||
this._a32 |= 1 << (i - 32) | ||
} else if (i > 16) { | ||
this._a16 |= 1 << (i - 16) | ||
} else { | ||
this._low |= 1 << i | ||
this._a00 |= 1 << i | ||
} | ||
@@ -339,10 +357,12 @@ } | ||
UINT64.prototype.negate = UINT64.prototype.not = function () { | ||
this._low = (~this._low & 0xFFFF) + 1 | ||
this._high = ~this._high & 0xFFFF | ||
this._a00 = ~this._a00 & 0xFFFF | ||
this._a16 = ~this._a16 & 0xFFFF | ||
this._a32 = ~this._a32 & 0xFFFF | ||
this._a48 = ~this._a48 & 0xFFFF | ||
return this | ||
return this.add(ONE) | ||
} | ||
/** | ||
* Equals | ||
* @method eq | ||
@@ -353,3 +373,4 @@ * @param {Object} other UINT64 | ||
UINT64.prototype.equals = UINT64.prototype.eq = function (other) { | ||
return (this._low == other._low) && (this._high == other._high) | ||
return (this._a48 == other._a48) && (this._a00 == other._a00) | ||
&& (this._a32 == other._a32) && (this._a16 == other._a16) | ||
} | ||
@@ -364,5 +385,9 @@ | ||
UINT64.prototype.greaterThan = UINT64.prototype.gt = function (other) { | ||
if (this._high > other._high) return true | ||
if (this._high < other._high) return false | ||
return this._low > other._low | ||
if (this._a48 > other._a48) return true | ||
if (this._a48 < other._a48) return false | ||
if (this._a32 > other._a32) return true | ||
if (this._a32 < other._a32) return false | ||
if (this._a16 > other._a16) return true | ||
if (this._a16 < other._a16) return false | ||
return this._a00 > other._a00 | ||
} | ||
@@ -377,5 +402,9 @@ | ||
UINT64.prototype.lessThan = UINT64.prototype.lt = function (other) { | ||
if (this._high < other._high) return true | ||
if (this._high > other._high) return false | ||
return this._low < other._low | ||
if (this._a48 < other._a48) return true | ||
if (this._a48 > other._a48) return false | ||
if (this._a32 < other._a32) return true | ||
if (this._a32 > other._a32) return false | ||
if (this._a16 < other._a16) return true | ||
if (this._a16 > other._a16) return false | ||
return this._a00 < other._a00 | ||
} | ||
@@ -390,4 +419,6 @@ | ||
UINT64.prototype.or = function (other) { | ||
this._low |= other._low | ||
this._high |= other._high | ||
this._a00 |= other._a00 | ||
this._a16 |= other._a16 | ||
this._a32 |= other._a32 | ||
this._a48 |= other._a48 | ||
@@ -404,4 +435,6 @@ return this | ||
UINT64.prototype.and = function (other) { | ||
this._low &= other._low | ||
this._high &= other._high | ||
this._a00 &= other._a00 | ||
this._a16 &= other._a16 | ||
this._a32 &= other._a32 | ||
this._a48 &= other._a48 | ||
@@ -418,4 +451,6 @@ return this | ||
UINT64.prototype.xor = function (other) { | ||
this._low ^= other._low | ||
this._high ^= other._high | ||
this._a00 ^= other._a00 | ||
this._a16 ^= other._a16 | ||
this._a32 ^= other._a32 | ||
this._a48 ^= other._a48 | ||
@@ -432,11 +467,24 @@ return this | ||
UINT64.prototype.shiftRight = UINT64.prototype.shiftr = function (n) { | ||
if (n > 16) { | ||
this._low = this._high >>> (n - 16) | ||
this._high = 0 | ||
} else if (n == 16) { | ||
this._low = this._high | ||
this._high = 0 | ||
if (n >= 48) { | ||
this._a00 = this._a48 >>> (n - 48) | ||
this._a16 = 0 | ||
this._a32 = 0 | ||
this._a48 = 0 | ||
} else if (n >= 32) { | ||
n -= 32 | ||
this._a00 = ( (this._a32 >>> n) | (this._a48 << (16-n)) ) & 0xFFFF | ||
this._a16 = (this._a48 >> n) & 0xFFFF | ||
this._a32 = 0 | ||
this._a48 = 0 | ||
} else if (n >= 16) { | ||
n -= 16 | ||
this._a00 = ( (this._a16 >>> n) | (this._a32 << (16-n)) ) & 0xFFFF | ||
this._a16 = ( (this._a32 >>> n) | (this._a48 << (16-n)) ) & 0xFFFF | ||
this._a32 = (this._a48 >> n) & 0xFFFF | ||
this._a48 = 0 | ||
} else { | ||
this._low = ( (this._low >>> n) | (this._high << (16-n)) ) & 0xFFFF | ||
this._high = (this._high >> n) & 0xFFFF | ||
this._a00 = ( (this._a00 >>> n) | (this._a16 << (16-n)) ) & 0xFFFF | ||
this._a16 = ( (this._a16 >>> n) | (this._a32 << (16-n)) ) & 0xFFFF | ||
this._a32 = ( (this._a32 >>> n) | (this._a48 << (16-n)) ) & 0xFFFF | ||
this._a48 = (this._a48 >> n) & 0xFFFF | ||
} | ||
@@ -455,16 +503,28 @@ | ||
UINT64.prototype.shiftLeft = UINT64.prototype.shiftl = function (n, allowOverflow) { | ||
if (n > 16) { | ||
this._high = this._low << (n - 16) | ||
this._low = 0 | ||
} else if (n == 16) { | ||
this._high = this._low | ||
this._low = 0 | ||
if (n >= 48) { | ||
this._a48 = this._a00 << (n - 48) | ||
this._a32 = 0 | ||
this._a16 = 0 | ||
this._a00 = 0 | ||
} else if (n >= 32) { | ||
n -= 32 | ||
this._a48 = (this._a16 << n) | (this._a00 >>> (16-n)) | ||
this._a32 = (this._a00 << n) & 0xFFFF | ||
this._a16 = 0 | ||
this._a00 = 0 | ||
} else if (n >= 16) { | ||
n -= 16 | ||
this._a48 = (this._a32 << n) | (this._a16 >>> (16-n)) | ||
this._a32 = ( (this._a16 << n) | (this._a00 >>> (16-n)) ) & 0xFFFF | ||
this._a16 = (this._a00 << n) & 0xFFFF | ||
this._a00 = 0 | ||
} else { | ||
this._high = (this._high << n) | (this._low >>> (16-n)) | ||
this._low = (this._low << n) & 0xFFFF | ||
if (!allowOverflow) { | ||
// Overflow only allowed on the high bits... | ||
this._high &= 0xFFFF | ||
} | ||
this._a48 = (this._a48 << n) | (this._a32 >>> (16-n)) | ||
this._a32 = ( (this._a32 << n) | (this._a16 >>> (16-n)) ) & 0xFFFF | ||
this._a16 = ( (this._a16 << n) | (this._a00 >>> (16-n)) ) & 0xFFFF | ||
this._a00 = (this._a00 << n) & 0xFFFF | ||
} | ||
if (!allowOverflow) { | ||
this._a48 &= 0xFFFF | ||
} | ||
@@ -510,3 +570,3 @@ return this | ||
UINT64.prototype.clone = function () { | ||
return new UINT64(this._low, this._high) | ||
return new UINT64(this._a00, this._a16, this._a32, this._a48) | ||
} | ||
@@ -513,0 +573,0 @@ |
{ | ||
"name": "cuint", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Unsigned integers for Javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,3 +11,3 @@ var assert = require('assert') | ||
assert.equal( u.toNumber(), -65536 ) | ||
assert.equal( u.toNumber(), 0 ) | ||
done() | ||
@@ -20,3 +20,3 @@ }) | ||
it('should return 0', function (done) { | ||
it('should return -1', function (done) { | ||
var u = UINT32(1).negate() | ||
@@ -32,3 +32,3 @@ | ||
it('should return n', function (done) { | ||
it('should return -n', function (done) { | ||
var u = UINT32(3).negate() | ||
@@ -44,7 +44,7 @@ | ||
it('should return n', function (done) { | ||
it('should return -n', function (done) { | ||
var n = Math.pow(2, 17) | ||
var u = UINT32(n).negate() | ||
assert.equal( u.toNumber(), -196608 ) | ||
assert.equal( u.toNumber(), -n ) | ||
done() | ||
@@ -51,0 +51,0 @@ }) |
@@ -51,2 +51,24 @@ var assert = require('assert') | ||
describe('1<<31', function () { | ||
it('should return 2^31', function (done) { | ||
var u = UINT32(1).shiftLeft(31) | ||
assert.equal( u.toString(16), '80000000' ) | ||
done() | ||
}) | ||
}) | ||
describe('9<<28', function () { | ||
it('should return 2^31', function (done) { | ||
var u = UINT32(9).shiftLeft(28) | ||
assert.equal( u.toString(16), '90000000' ) | ||
done() | ||
}) | ||
}) | ||
}) |
@@ -51,2 +51,46 @@ var assert = require('assert') | ||
describe('2^31>>31', function () { | ||
it('should return 1', function (done) { | ||
var u = UINT32('80000000', 16).shiftRight(31) | ||
assert.equal( u.toNumber(), 1 ) | ||
done() | ||
}) | ||
}) | ||
describe('2^28>>28', function () { | ||
it('should return 1', function (done) { | ||
var u = UINT32('10000000', 16).shiftRight(28) | ||
assert.equal( u.toNumber(), 1 ) | ||
done() | ||
}) | ||
}) | ||
describe('2^31+2^28>>31', function () { | ||
it('should return 1', function (done) { | ||
var u = UINT32('90000000', 16).shiftRight(31) | ||
assert.equal( u.toNumber(), 1 ) | ||
done() | ||
}) | ||
}) | ||
describe('2^31+2^28>>28', function () { | ||
it('should return 9', function (done) { | ||
var u = UINT32('90000000', 16).shiftRight(28) | ||
assert.equal( u.toNumber(), 9 ) | ||
done() | ||
}) | ||
}) | ||
}) |
@@ -63,2 +63,13 @@ var assert = require('assert') | ||
describe('= radix', function () { | ||
it('should return the number', function (done) { | ||
var u = UINT32(2).toString(2) | ||
assert.equal( u, '10' ) | ||
done() | ||
}) | ||
}) | ||
}) |
@@ -178,4 +178,14 @@ var assert = require('assert') | ||
describe('80000000 with radix 16', function () { | ||
it('should properly initialize', function (done) { | ||
var u = UINT32( '80000000', 16 ) | ||
assert.equal( u._low, 0 ) | ||
assert.equal( u._high, 32768 ) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72404
2431