Comparing version 0.1.2 to 0.1.3
{ | ||
"name": "uint32", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "a javascript library for dealing with (bitwise) uint32 operations", | ||
@@ -5,0 +5,0 @@ "homepage": "https://www.github.com/fxa/uint32.js", |
@@ -93,2 +93,5 @@ uint32 | ||
uint32.multHigh(0xffffffff, 0xffffffff); // 0xfffffffe | ||
var result = new Uint32Array(2); | ||
uint32.mult(0xffffffff, 0xffffffff, result); // result -> [0xfffffffe, 0x00000001] | ||
@@ -95,0 +98,0 @@ // That's all! |
@@ -11,2 +11,17 @@ /* global describe, it, require, window */ | ||
function toHex (ui32) { | ||
var tmp = ui32.toString(16); | ||
var neededZeros = 8 - tmp.length; | ||
return new Array(neededZeros + 1).join('0') + tmp; | ||
} | ||
function expectHex(ui32) { | ||
return { | ||
to: { | ||
be: function (expected) { | ||
expect(toHex(ui32)).to.be(toHex(expected)); | ||
} | ||
} | ||
}; | ||
} | ||
describe('Creating and Extracting', function () { | ||
@@ -271,6 +286,36 @@ describe('fromBytesBigEndian()', function () { | ||
it('should work, if the product is greater than 2^52', function () { | ||
expect(uint32.multHigh(0xff030201, 0xff030201)).to.be(0xfe06fe07); | ||
expect(uint32.multHigh(0xffffffff, 0xffffffff)).to.be(0xfffffffe); | ||
expect(uint32.multHigh(0xff030201, 0xff030201)).to.be(0xfe06fe07); | ||
}); | ||
}); | ||
describe('mult', function () { | ||
it('should work, if the product is less than 2^32', function () { | ||
var result = new Uint32Array(2); | ||
uint32.mult(0xffff, 0xffff, result); | ||
expect(result[0]).to.be(0); | ||
expect(result[1]).to.be(0xfffe0001); | ||
}); | ||
it('should work, if the product is smaller than 2^52', function () { | ||
var result = new Uint32Array(2); | ||
uint32.mult(0x04000000, 0x03ffffff, result); | ||
expect(result[0]).to.be(0xfffff); | ||
expect(result[1]).to.be(0xfc000000); | ||
}); | ||
it('should work, if the product is 2^52', function () { | ||
var result = new Uint32Array(2); | ||
uint32.mult(0x04000000, 0x04000000, result); | ||
expect(result[0]).to.be(0x00100000); | ||
expect(result[1]).to.be(0); | ||
}); | ||
it('should work, if the product is greater than 2^52', function () { | ||
var result = new Uint32Array(2); | ||
uint32.mult(0xff030201, 0xff030201, result); | ||
expectHex(result[0]).to.be(0xfe06fe07); | ||
expectHex(result[1]).to.be(0x0a0a0401); | ||
uint32.mult(0xffffffff, 0xffffffff, result); | ||
expect(result[0]).to.be(0xfffffffe); | ||
expect(result[1]).to.be(1); | ||
}); | ||
}); | ||
@@ -277,0 +322,0 @@ }); |
@@ -38,2 +38,13 @@ /* jshint bitwise: false */ | ||
}; | ||
/** | ||
* Returns the bytes as array. | ||
* e.g. when byteNo is 0, the high byte is returned, when byteNo = 3 the low byte is returned. | ||
* @param {Number} uint32value the source to be extracted | ||
* @param {Number} byteNo 0-3 the byte number, 0 is the high byte, 3 the low byte | ||
* @returns {Array} the array [highByte, 2ndHighByte, 3rdHighByte, lowByte] | ||
*/ | ||
exporter.getByteBigsEndian = function (uint32value, byteNo) { | ||
return [exporter.getByteBigEndian(0), exporter.getByteBigEndian(1), exporter.getByteBigEndian(2), exporter.getByteBigEndian(3)]; | ||
}; | ||
@@ -247,7 +258,22 @@ /** | ||
var prod = factor1 * factor2; | ||
// the top 52 are ok, so the top 32 bits, too | ||
// the top 52 bits are ok, so the top 32 bits, too | ||
return exporter.highPart(prod); | ||
}; | ||
/** | ||
* Returns the the low and the high uint32 of the multiplication. | ||
* @param {Number} factor1 an uint32 | ||
* @param {Number} factor2 an uint32 | ||
* @param {Uint32Array[2]} resultUint32Array2 the Array, where the result will be written to | ||
* @returns undefined | ||
*/ | ||
exporter.mult = function (factor1, factor2, resultUint32Array2) { | ||
var high16 = ((factor1 & 0xffff0000) >>> 0) * factor2; | ||
var low16 = (factor1 & 0x0000ffff) * factor2; | ||
resultUint32Array2[0] = ((high16 + low16) / POW_2_32) >>> 0; | ||
// the last >>> 0 is not needed, it is done implicitly by the uin32array | ||
resultUint32Array2[1] = ((high16 >>> 0) + (low16 >>> 0));// >>> 0; | ||
}; | ||
}) ((typeof module !== 'undefined') ? module.exports = {} : window.uint32 = {}); | ||
30068
544
108