UNPKG

gnablib

Version:

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

2 lines 1.58 kB
/*! Copyright 2024-2025 the gnablib contributors MPL-1.1 */ import{asLE as t}from"../endian/platform.js";import{U64MutArray as r}from"../primitive/number/U64.js";import{sLen as s}from"../safe/safe.js";import{APrng64 as e}from"./APrng64.js";class o extends e{constructor(){super(...arguments),this.bitGen=64,this.safeBits=64}rawNext(){const t=this._state.at(0).mut(),r=this._state.at(1),s=r.add(t);return this._state.at(0).set(r),t.xorEq(t.lShift(this._a)),this._state.at(1).set(t.xor(r).xor(t.rShift(this._b)).xor(r.rShift(this._c))),s}}export class XorShift128p extends o{constructor(){super(...arguments),this._a=23,this._b=18,this._c=5}get[Symbol.toStringTag](){return"xorshift128+"}static new(t=!1){return new XorShift128p(r.fromBytes(Uint32Array.of(2065550767,3793791033,2713282036,1853398634)),t)}static seed(t,s,e=!1){const o=r.fromLen(2);return o.at(0).set(t),o.at(1).set(s),new XorShift128p(o,e)}static restore(e,o=!1){s("state",e).exactly(16).throwNot();const i=e.slice();t.i32(i,0,4);const n=r.fromBytes(i.buffer);return new XorShift128p(n,o)}}export class XorShift128pV8 extends o{constructor(){super(...arguments),this._a=23,this._b=17,this._c=26}get[Symbol.toStringTag](){return"xorshift128+v8"}static new(t=!1){return new XorShift128pV8(r.fromBytes(Uint32Array.of(2065550767,3793791033,2713282036,1853398634)),t)}static seed(t,s,e=!1){const o=r.fromLen(2);return o.at(0).set(t),o.at(1).set(s),new XorShift128pV8(o,e)}static restore(e,o=!1){s("state",e).exactly(16).throwNot();const i=e.slice();t.i32(i,0,4);const n=r.fromBytes(i.buffer);return new XorShift128pV8(n,o)}}