mind-ar
Version:
web augmented reality framework
26 lines (22 loc) • 571 B
JavaScript
// Fast computation on number of bit sets
// Ref: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
const compute = (options) => {
const {v1, v2} = options;
let d = 0;
for (let i = 0; i < v1.length; i++) {
let x = (v1[i] ^ v2[i]) >>> 0;
d += bitCount(x);
}
return d;
}
const bitCount = (v) => {
var c = v - ((v >> 1) & 0x55555555);
c = ((c >> 2) & 0x33333333) + (c & 0x33333333);
c = ((c >> 4) + c) & 0x0F0F0F0F;
c = ((c >> 8) + c) & 0x00FF00FF;
c = ((c >> 16) + c) & 0x0000FFFF;
return c;
}
export {
compute
};