UNPKG

gnablib

Version:

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

2 lines 2.14 kB
/*! Copyright 2024-2025 the gnablib contributors MPL-1.1 */ import{asLE as t}from"../endian/platform.js";import{sLen as s}from"../safe/safe.js";import{APrng32 as e}from"./APrng32.js";class r extends e{constructor(){super(...arguments),this.bitGen=32}trueSave(){const s=new Uint8Array(this._state.slice().buffer);return t.i32(s,0,4),s}rawNext(){const t=this._gen(),s=this._state[1]<<9;return this._state[2]^=this._state[0],this._state[3]^=this._state[1],this._state[1]^=this._state[2],this._state[0]^=this._state[3],this._state[2]^=s,this._state[3]=this._state[3]<<11|this._state[3]>>>21,t>>>0}}export class Xoshiro128p extends r{constructor(){super(...arguments),this.safeBits=24}_gen(){return this._state[0]+this._state[3]}get[Symbol.toStringTag](){return"xoshiro128+"}static new(t=!1){return new Xoshiro128p(Uint32Array.of(53,30301,71423,49323),t)}static seed(t,s,e,r,o=!1){const i=Uint32Array.of(t,s,e,r);return new Xoshiro128p(i,o)}static restore(e,r=!1){s("state",e).exactly(16).throwNot();const o=e.slice();t.i32(o,0,4);const i=new Uint32Array(o.buffer);return new Xoshiro128p(i,r)}}export class Xoshiro128pp extends r{constructor(){super(...arguments),this.safeBits=32}_gen(){let t=this._state[0]+this._state[3];return t=t<<7|t>>>25,t+this._state[0]}get[Symbol.toStringTag](){return"xoshiro128++"}static new(t=!1){return new Xoshiro128pp(Uint32Array.of(53,30301,71423,49323),t)}static seed(t,s,e,r,o=!1){const i=Uint32Array.of(t,s,e,r);return new Xoshiro128pp(i,o)}static restore(e,r=!1){s("state",e).exactly(16).throwNot();const o=e.slice();t.i32(o,0,4);const i=new Uint32Array(o.buffer);return new Xoshiro128pp(i,r)}}export class Xoshiro128ss extends r{constructor(){super(...arguments),this.safeBits=32}_gen(){let t=5*this._state[1];return t=t<<7|t>>>25,t*=9,t>>>0}get[Symbol.toStringTag](){return"xoshiro128**"}static new(t=!1){return new Xoshiro128ss(Uint32Array.of(53,30301,71423,49323),t)}static seed(t,s,e,r,o=!1){const i=Uint32Array.of(t,s,e,r);return new Xoshiro128ss(i,o)}static restore(e,r=!1){s("state",e).exactly(16).throwNot();const o=e.slice();t.i32(o,0,4);const i=new Uint32Array(o.buffer);return new Xoshiro128ss(i,r)}}