UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 2.94 kB
/*! Copyright 2024-2025 the gnablib contributors MPL-1.1 */ import{asLE as t}from"../endian/platform.js";import{U64 as s,U64MutArray as r}from"../primitive/number/U64.js";import{sLen as o}from"../safe/safe.js";import{APrng64 as e}from"./APrng64.js";class i extends e{constructor(){super(...arguments),this.bitGen=64}rawNext(){const t=this._state.at(0),s=this._state.at(1),r=this._gen();return s.xorEq(t),t.lRotEq(this._a).xorEq(s).xorEq(s.lShift(this._b)),s.lRotEq(this._c),r}}export class Xoroshiro128p extends i{constructor(){super(...arguments),this.safeBits=53,this._a=24,this._b=16,this._c=37}_gen(){return this._state.at(0).add(this._state.at(1)).mut()}get[Symbol.toStringTag](){return"xoroshiro128+"}static new(t=!1){return new Xoroshiro128p(r.fromBytes(Uint32Array.of(2065550767,3793791033,2713282036,1853398634)),t)}static seed(t,s,o=!1){const e=r.fromLen(2);return e.at(0).set(t),e.at(1).set(s),new Xoroshiro128p(e,o)}static restore(s,e=!1){o("state",s).exactly(16).throwNot();const i=s.slice();t.i32(i,0,4);const n=r.fromBytes(i.buffer);return new Xoroshiro128p(n,e)}}export class Xoroshiro128p_2016 extends Xoroshiro128p{constructor(){super(...arguments),this._a=55,this._b=14,this._c=36}get[Symbol.toStringTag](){return"xoroshiro128+_2016"}static new(t=!1){return new Xoroshiro128p_2016(r.fromBytes(Uint32Array.of(2065550767,3793791033,2713282036,1853398634)),t)}static seed(t,s,o=!1){const e=r.fromLen(2);return e.at(0).set(t),e.at(1).set(s),new Xoroshiro128p_2016(e,o)}static restore(s,e=!1){o("state",s).exactly(16).throwNot();const i=s.slice();t.i32(i,0,4);const n=r.fromBytes(i.buffer);return new Xoroshiro128p_2016(n,e)}}export class Xoroshiro128pp extends i{constructor(){super(...arguments),this.safeBits=64,this._a=49,this._b=21,this._c=28}_gen(){return this._state.at(0).mut().addEq(this._state.at(1)).lRotEq(17).addEq(this._state.at(0))}get[Symbol.toStringTag](){return"xoroshiro128++"}static new(t=!1){return new Xoroshiro128pp(r.fromBytes(Uint32Array.of(2065550767,3793791033,2713282036,1853398634)),t)}static seed(t,s,o=!1){const e=r.fromLen(2);return e.at(0).set(t),e.at(1).set(s),new Xoroshiro128pp(e,o)}static restore(s,e=!1){o("state",s).exactly(16).throwNot();const i=s.slice();t.i32(i,0,4);const n=r.fromBytes(i.buffer);return new Xoroshiro128pp(n,e)}}const n=s.fromI32s(5,0),a=s.fromI32s(9,0);export class Xoroshiro128ss extends i{constructor(){super(...arguments),this.safeBits=64,this._a=24,this._b=16,this._c=37}_gen(){return this._state.at(0).mut().mulEq(n).lRotEq(7).mulEq(a)}get[Symbol.toStringTag](){return"xoroshiro128**"}static new(t=!1){return new Xoroshiro128ss(r.fromBytes(Uint32Array.of(2065550767,3793791033,2713282036,1853398634)),t)}static seed(t,s,o=!1){const e=r.fromLen(2);return e.at(0).set(t),e.at(1).set(s),new Xoroshiro128ss(e,o)}static restore(s,e=!1){o("state",s).exactly(16).throwNot();const i=s.slice();t.i32(i,0,4);const n=r.fromBytes(i.buffer);return new Xoroshiro128ss(n,e)}}