Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uint32

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uint32 - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

4

package.json
{
"name": "uint32",
"version": "0.1.1",
"version": "0.1.2",
"description": "a javascript library for dealing with (bitwise) uint32 operations",

@@ -8,3 +8,3 @@ "homepage": "https://www.github.com/fxa/uint32.js",

"scripts": {
"test": "jshint uint32.js test_uint32.js && mocha test_uint32",
"test": "jshint uint32.js test_uint32.js && mocha --reporter spec test_uint32",
"hint": "jshint uint32.js test_uint32.js"

@@ -11,0 +11,0 @@ },

@@ -82,13 +82,16 @@ uint32

uint32.rotateLeft(0x80000000, 1); // 0x00000001
uint32.rotateLeft(0x00000001, 1); // 0x80000000
uint32.rotateRight(0x00000001, 1); // 0x80000000
// Logical Gates
// Logical Gates
uint32.choose(0x01010202, 0x00010001, 0x01000100); // 0x00010100
// function majority (x, y, z)
uint32.majority (0x01, 0x00, 0x01); // 0x01
// Arithmetic
uint32.addMod32(0x80000001, 0x80000001); // 2
uint32.log2(0xffffffff); // 31
uint32.multLow(0xffffffff, 0xffffffff); // 1
uint32.multHigh(0xffffffff, 0xffffffff); // 0xfffffffe
// That's all! Detailed specifications are in the tests!
// That's all!
// Detailed specifications are in the tests!
```

@@ -95,0 +98,0 @@

@@ -184,3 +184,24 @@ /* global describe, it, require, window */

});
describe('majority()', function () {});
describe('majority()', function () {
it('should return 0, if all parameters are 0', function () {
expect(uint32.majority(0, 0, 0)).to.be(0);
});
it('should return 0, if all but one parameters are 0', function () {
expect(uint32.majority(0xffffffff, 0, 0)).to.be(0);
expect(uint32.majority(0, 0xffffffff, 0)).to.be(0);
expect(uint32.majority(0, 0, 0xffffffff)).to.be(0);
});
it('should return 0xffffffff, if two parameters are 0xffffffff', function () {
expect(uint32.majority(0xffffffff, 0xffffffff, 0)).to.be(0xffffffff);
expect(uint32.majority(0xffffffff, 0, 0xffffffff)).to.be(0xffffffff);
expect(uint32.majority(0, 0xffffffff, 0xffffffff)).to.be(0xffffffff);
});
it('should return 0xffffffff, if all parameters are 0xffffffff', function () {
expect(uint32.majority(0xffffffff, 0xffffffff, 0xffffffff)).to.be(0xffffffff);
});
it('should work bitwise', function () {
// all above tests bitwise
expect(uint32.majority(parseInt('01001101', 2), parseInt('00101011', 2), parseInt('00010111', 2))).to.be(parseInt('00001111', 2));
});
});
});

@@ -206,3 +227,53 @@

});
describe('log2', function () {
it('should work for 0', function () {
expect(uint32.log2(0)).to.be(-Infinity);
});
it('should work for 1', function () {
expect(uint32.log2(1)).to.be(0);
});
it('should work for 2', function () {
expect(uint32.log2(2)).to.be(1);
});
it('should work for values between 2 and 2^31', function () {
for (var exp = 2; exp < 32; exp += 1) {
var pow = Math.pow(2, exp);
expect(uint32.log2(pow - 1)).to.be(exp - 1);
expect(uint32.log2(pow)).to.be(exp);
}
});
it('should work for 2^32-1', function () {
expect(uint32.log2(0xffffffff)).to.be(31);
});
});
describe('multLow', function () {
it('should work, if the product is smaller than 2^52', function () {
expect(uint32.multLow(0x04000000, 0x03ffffff)).to.be(0xfc000000);
});
it('should work, if the product is 2^52', function () {
expect(uint32.multLow(0x04000000, 0x04000000)).to.be(0);
});
it('should work, if the product is greater than 2^52', function () {
expect(uint32.multLow(0xff030201, 0xff030201)).to.be(0x0a0a0401);
expect(uint32.multLow(0xffffffff, 0xffffffff)).to.be(0x00000001);
});
});
describe('multHigh', function () {
it('should return 0, if the product is less than 2^32', function () {
expect(uint32.multHigh(0xffff, 0xffff)).to.be(0);
});
it('should work, if the product is smaller than 2^52', function () {
expect(uint32.multHigh(0x04000000, 0x03ffffff)).to.be(0xfffff);
});
it('should work, if the product is 2^52', function () {
expect(uint32.multHigh(0x04000000, 0x04000000)).to.be(0x00100000);
});
it('should work, if the product is greater than 2^52', function () {
expect(uint32.multHigh(0xffffffff, 0xffffffff)).to.be(0xfffffffe);
expect(uint32.multHigh(0xff030201, 0xff030201)).to.be(0xfe06fe07);
});
});
});
})();

@@ -9,2 +9,5 @@ /* jshint bitwise: false */

var POW_2_32 = 0x0100000000;
var POW_2_52 = 0x10000000000000;
//

@@ -68,3 +71,3 @@ // Creating and Extracting

exporter.highPart = function (number) {
return exporter.toUint32(number / 0x0100000000);
return exporter.toUint32(number / POW_2_32);
};

@@ -189,4 +192,63 @@

/**
* Returns the log base 2 of the given value. That is the number of the highest set bit.
* @param uint32val
* @returns {Number} the logarithm base 2, an integer between 0 and 31
*/
exporter.log2 = function (uint32val) {
return Math.floor(Math.log(uint32val) / Math.LN2);
};
/*
// this implementation does the same, looks much funnier, but takes 2 times longer (according to jsperf) ...
var log2_u = new Uint32Array(2);
var log2_d = new Float64Array(log2_u.buffer);
exporter.log2 = function (uint32val) {
// Ported from http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogIEEE64Float to javascript
// (public domain)
if (uint32val === 0) {
return -Infinity;
}
// fill in the low part
log2_u[0] = uint32val;
// set the mantissa to 2^52
log2_u[1] = 0x43300000;
// subtract 2^52
log2_d[0] -= 0x10000000000000;
return (log2_u[1] >>> 20) - 0x3FF;
};
*/
/**
* Returns the "low" part of the multiplication.
* @param {Number} factor1 an uint32
* @param {Number} factor2 an uint32
* @returns {Number} the low part of the product, that is factor1 * factor2 modulus 2^32
*/
exporter.multLow = function (factor1, factor2) {
// We could test with log2(factor1) + log2(factor2) < 52,
// but it is easier to check the product than to check the number of bits of both
var prod = factor1 * factor2;
if (prod <= POW_2_52) {
return prod >>> 0;
}
// a*b mod x can be divided to (a1*b) mod x + (a2*b) mod x with a1+a2=a
// so lets make 2 multiplications with 16 and 32 bits
return ((((factor1 & 0xffff0000) * factor2) >>> 0) + (((factor1 & 0x0000ffff) * factor2) >>> 0)) >>> 0;
};
/**
* Returns the "high" part of the multiplication.
* @param {Number} factor1 an uint32
* @param {Number} factor2 an uint32
* @returns {Number} the high part of the product, that is factor1 * factor2 divided 2^32 without fraction
*/
exporter.multHigh = function (factor1, factor2) {
var prod = factor1 * factor2;
// the top 52 are ok, so the top 32 bits, too
return exporter.highPart(prod);
};
}) ((typeof module !== 'undefined') ? module.exports = {} : window.uint32 = {});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc