Public
Edited
Jul 8, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
bitpack = {
let t1 = performance.now()
let a = BigInt(1); // value to insert (8 bits wide)
let b = BigInt(2 * (128)); // value to insert (8 bits wide)
let c = a << 16n; // shift value to index 1 (starts at 8 bits)
c = c|b; // union a|b, b is now in index 0 (starts at 0 bits)
let size = BigInt(8); // bit width
let from = BigInt(0); // at 'index'
let pref = BigInt(2) ** size - BigInt(1); // bitmask prefix
let mask = pref << (from * size); // mask to retrieve 8-bit 'slice'
let exit = (c & mask) >> (from * size); // resulting value from applying mask

return {
t: performance.now()-t1,
a: a.toString(2).padStart(24,0),
b: b.toString(2).padStart(24,0),
c: c.toString(2).padStart(24,0),
c_: c,
exit
}
}
Insert cell
packs = {

let span = 10000
let nums = Uint16Array.from({length:span},()=>~~(Math.random()*5000))
let pool = BigInt(0), A = BigInt(0), B = BigInt(0);
let bits = BigInt(8);

let pref = BigInt(2) ** bits - BigInt(1); // bitmask prefix

for (let i = 0; i < span; ++i) {

let from = BigInt(nums[i]); // at 'index'
let post = from * bits; // offset bits
let mask = pref << (post); // mask to retrieve 8-bit 'slice'
let exit = ((i % 2 ? A : B) & mask) >> (post); // resulting value from applying mask
exit = (exit > 255 ? 255 : exit+BigInt(1)) << (post);
i % 2 ? A |= exit : B |= exit;
//temp.push(exit)
}

let t1 = performance.now()

let C = BigInt(0)
for (let i = 0; i < span; ++i) {
C = C | (i % 2 ? A : B)
}
return {
t:performance.now()-t1,
r:pool,A:A.toString(2),B:B.toString(2),C:C.toString(2)
}
}
Insert cell
BigInt(1) << (BigInt(64) * BigInt(8))
Insert cell
BigInt(1) * (BigInt(1) << BigInt(8*7)) // 72057594037927936n
Insert cell
stress = { // stress test

let span = 5000;
let dims = 100; // dimensions or 'words'


// initialise
let nums = Array.from({length:span},()=>new Uint16Array(dims))//.from({length:dims}))//,()=>BigInt(~~(Math.random()*Math.random()*10))))

// simulate sparse array;
for (let i = 0; i < span; ++i) {
let row = nums[i];
for (let j = 0; j < dims; ++j) {
row[j] = (~~(Math.random()*10000)) // generate random indexes
}
}

let t1 = performance.now()
let pool = [];
let bits = BigInt(8);

for (let i = 0; i < span; ++i) {
let row = nums[i];
let mem = BigInt(0);

for (let j = 0; j < dims; ++j) {
let into = BigInt(row[j]);
into = BigInt(1) << (bits * into)
//mem |= into ;
pool.push(into);
}

}

return {
t:performance.now()-t1,
r:nums,pool
}
}
Insert cell
plain = {

let span = 1000000
let nums = Uint16Array.from({length:span},()=>~~(Math.random()*span*0.1))
let t1 = performance.now()

let dims = new Uint8ClampedArray(span*0.1)
for (let i = 0; i < span; ++i) {
let idx = nums[i]
dims[idx] ? dims[idx]++ : dims[idx] = 1;
}
let exit = []; span = dims.length;
for (let i = 0; i < span; ++i) {
if(dims[i] > 32) exit.push({i,val:dims[i]})
}

return {
t:performance.now()-t1,
r:dims,exit
}
}
Insert cell
Insert cell
Insert cell
random = {
let t1= performance.now();

let bitArray = new BooleanArray(2**11, 2**11);
//bitArray.randomise()
bitArray.setAt(0,0,1)
let flat = bitArray.to1D()
let mult = bitArray.to2D()
return {
t:performance.now()-t1,
flat,
mult, bitArray
}
}
Insert cell
// densely packed array adapted from:
// stackoverflow.com/a/73666335

class BooleanArray {
constructor(height, width) {
// 2**16 ^ 2 upper limit
this.height = height;
this.width = width;
this.byteWidth = (width >> 3) + ((width & 0b111) > 0 ? 1 : 0);
this.dataArray = new Uint8Array(height * this.byteWidth);
}
getAt(x, y) {
const xBytes = x >>> 3;
const bitMask = 1 << (x & 0b111);
return !!(this.dataArray[y * this.byteWidth + xBytes] & bitMask)|0 // > 0;
}
setAt(x, y, value) {
const xBytes = x >>> 3;
const xBits = x & 0b111;
const i = y * this.byteWidth + xBytes;
if (value) {
this.dataArray[i] |= 1 << xBits;
} else {
this.dataArray[i] &= ~(1 << xBits);
}
}
randomise() { // fills array with integers
const size = 2**16
const chunks = (this.dataArray.length/size);
for (let i = 0; i < chunks; ++i) {
crypto.getRandomValues(this.dataArray.subarray(i*size,(i+1)*size))
}
}
toString() {
let result = "";
for (let y = 0; y < this.height; y++) {
result += "\n";
for (let x = 0; x < this.width; x++) {
result += this.getAt(x, y) ? "1" : "0";
}
}
return result;
}
to2D() {
let result = [];
for (let y = 0; y < this.height; y++) {
result[y] = new Uint8Array(this.width)
for (let x = 0; x < this.width; x++) {
result[y][x] = this.getAt(x, y)
}
}
return result;
}
to1D() {
let result = new Uint8Array(this.height * this.byteWidth)
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
const xBytes = x >>> 3;
const xBits = x & 0b111;
result[ y * this.byteWidth + xBytes] = this.getAt(x, y)
}
}
return result;
}
}
Insert cell
structurae = import('https://cdn.skypack.dev/structurae@4.0.2?min')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more