gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.24 kB
JavaScript
/*! Copyright 2025 the gnablib contributors MPL-1.1 */
import{U128 as t,U128Mut as s}from"../primitive/number/U128.js";import{sLen as r}from"../safe/safe.js";import{APrng64 as e}from"./APrng64.js";const i=t.fromI32s(1),o=t.fromI32s(2681009733,1132846948,533093796,593554693);export class Pcg64 extends e{constructor(t,s,r){super(t,r),this.bitGen=64,this.safeBits=64,this._inc=s,this._u64_0=t.u64at(0),this._u64_1=t.u64at(1)}rawNext(){this._state.mulEq(o).addEq(this._inc);const t=this._u64_1.rShift(58).low;return this._u64_0.mut().xorEq(this._u64_1).rRotEq(t)}save(){if(!this.saveable)return new Uint8Array(0);const t=new Uint8Array(32);return t.set(this._state.toBytesLE()),t.set(this._inc.toBytesLE(),16),t}get[Symbol.toStringTag](){return"pcg64"}static new(r=!1){const e=t.fromI32s(2495175643,3661511115,1,0),i=s.fromI32s(3487585435,2101255350,3628474373,2543622808);return new Pcg64(i,e,r)}static seed(r,e,n=!1){const a=null!=e?e.mut().lShiftEq(1).xorEq(i):t.fromI32s(2495175643,3661511115,1,0),u=s.fromI32s(0,0,0,0);return u.mulEq(o).addEq(a),u.addEq(r),u.mulEq(o).addEq(a),new Pcg64(u,a,n)}static restore(e,i=!1){r("state",e).exactly(32).throwNot();const o=e.slice(),n=s.fromBytesLE(o),a=t.fromBytesLE(o,16);return new Pcg64(n,a,i)}}