create tidepool-utils js package, with prng impl

This commit is contained in:
tali 2023-04-13 14:38:05 -04:00
parent 62f48df58e
commit 059c059fdd
4 changed files with 77 additions and 0 deletions

1
tidepool-utils/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

View File

@ -0,0 +1,37 @@
export class Rng {
genU32() { throw new Error('not implemented'); }
// precisely mirrors Rust's `rand::Rng::gen_range()` for `Range<u32>`
genRange(min, max) {
let range = max - min;
let zone = (range << Math.clz32(range)) - 1 >>> 0;
let v, lo, hi;
do {
v = this.genU32();
lo = Math.imul(v, range) >>> 0; // (u32) (v * range)
} while (lo > zone);
hi = BigInt(v) * BigInt(range) >> 32n;
return min + Number(hi);
}
}
export class SplitMix64 extends Rng {
constructor(seed) {
super();
this.seed = BigInt(seed);
}
clone() {
return new this.constructor(this.seed);
}
genU32() {
this.seed = BigInt.asUintN(64, this.seed + 0x9E3779B97F4A7C15n);
let z = this.seed;
z ^= BigInt.asUintN(64, z >> 33n);
z = BigInt.asUintN(64, z * 0x62A9D9ED799705F5n);
z ^= BigInt.asUintN(64, z >> 28n);
z = BigInt.asUintN(64, z * 0xCB24D0A5C88C35B3n);
return Number(z >> 32n);
}
}

28
tidepool-utils/package-lock.json generated Normal file
View File

@ -0,0 +1,28 @@
{
"name": "tidepool-utils",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "tidepool-utils",
"version": "1.0.0",
"license": "LGPL-2.1-or-later",
"dependencies": {
"tetris-fumen": "^1.1.3"
}
},
"node_modules/tetris-fumen": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/tetris-fumen/-/tetris-fumen-1.1.3.tgz",
"integrity": "sha512-cMGKYvkL11fH5iRk8SpIl4+5gREWk084Q8VPtkhUc0pSyCjT99+I5+SaBjlBG/WkP2U3ve30OyFfvCTUSbl2Qg=="
}
},
"dependencies": {
"tetris-fumen": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/tetris-fumen/-/tetris-fumen-1.1.3.tgz",
"integrity": "sha512-cMGKYvkL11fH5iRk8SpIl4+5gREWk084Q8VPtkhUc0pSyCjT99+I5+SaBjlBG/WkP2U3ve30OyFfvCTUSbl2Qg=="
}
}
}

View File

@ -0,0 +1,11 @@
{
"name": "tidepool-utils",
"type": "module",
"version": "1.0.0",
"description": "Utilities for working with JSON data generated by tidepool",
"author": "iitalics",
"license": "LGPL-2.1-or-later",
"dependencies": {
"tetris-fumen": "^1.1.3"
}
}