gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 3.79 kB
JavaScript
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */
import{hex as t}from"../../codec/Hex.js";import{asBE as r,asLE as s}from"../../endian/platform.js";import{sInt as i}from"../../safe/safe.js";const o=Symbol.for("nodejs.util.inspect.custom");export class U32{constructor(t,r=0,s="U32"){this._arr=t,this._pos=r,this._name=s,this.size32=1}get value(){return this._arr[this._pos]}_value(t){return t instanceof U32?t._arr[t._pos]:t}static fromInt(t){return i("uint32",t).unsigned().atMost(4294967295).throwNot(),new U32(Uint32Array.of(t))}static mount(t,r=0){return new U32(t,r)}clone(){return new U32(this._arr.slice(this._pos,this._pos+1))}clone32(){return this._arr.slice(this._pos,this._pos+1)}mut(){const t=this._arr.slice(this._pos,this._pos+1);return U32Mut.mount(t,0)}lShift(t){const r=new Uint32Array(1);return r[t>>>5]=this._arr[this._pos]<<t,new U32(r)}rShift(t){const r=new Uint32Array(1);return r[t>>>5]=this._arr[this._pos]>>>t,new U32(r)}lRot(t){return t&=31,new U32(Uint32Array.of(this._arr[this._pos]<<t|this._arr[this._pos]>>>32-t))}rRot(t){return t&=31,new U32(Uint32Array.of(this._arr[this._pos]>>>t|this._arr[this._pos]<<32-t))}xor(t){return new U32(Uint32Array.of(this._arr[this._pos]^t._arr[t._pos]))}or(t){return new U32(Uint32Array.of(this._arr[this._pos]|t._arr[t._pos]))}and(t){return new U32(Uint32Array.of(this._arr[this._pos]&t._arr[t._pos]))}not(){return new U32(Uint32Array.of(~this._arr[this._pos]))}add(t){return new U32(Uint32Array.of(this._arr[this._pos]+t._arr[t._pos]))}sub(t){return new U32(Uint32Array.of(this._arr[this._pos]-t._arr[t._pos]))}mul(t){return new U32(Uint32Array.of(Math.imul(this._arr[this._pos],t._arr[t._pos])))}eq(t){const r=t instanceof U32?t._arr[t._pos]:t;return this._arr[this._pos]==r}gt(t){const r=t instanceof U32?t._arr[t._pos]:t;return this._arr[this._pos]>r}gte(t){const r=t instanceof U32?t._arr[t._pos]:t;return this._arr[this._pos]>=r}lt(t){const r=t instanceof U32?t._arr[t._pos]:t;return this._arr[this._pos]<r}lte(t){const r=t instanceof U32?t._arr[t._pos]:t;return this._arr[this._pos]<=r}toBytesBE(){const t=new Uint8Array(this._arr.slice(this._pos,this._pos+1).buffer);return r.i32(t,0),t}toBytesLE(){const t=new Uint8Array(this._arr.slice(this._pos,this._pos+1).buffer);return s.i32(t,0),t}getByte(t=0){i("byteIdx",t).unsigned().atMost(3).throwNot();const r=8*t;return this._arr[this._pos]>>>r&255}toString(){return t.fromI32(this._arr[this._pos])}get[Symbol.toStringTag](){return this._name}[o](){return`${this._name}(${this.toString()})`}static get zero(){return n}}const n=U32.mount(Uint32Array.of(0));export class U32Mut extends U32{constructor(t,r){super(t,r,"U32Mut")}set(t){const r=t instanceof U32?t.value:t;return this._arr[this._pos]=r,this}zero(){return this._arr[this._pos]=0,this}static fromInt(t){return i("uint32",t).unsigned().atMost(4294967295).throwNot(),new U32Mut(Uint32Array.of(t),0)}static mount(t,r=0){return new U32Mut(t,r)}lShiftEq(t){const r=new Uint32Array(1);return r[t>>>5]=this._arr[this._pos]<<t,this._arr[this._pos]=r[0],this}lRotEq(t){return t&=31,this._arr[this._pos]=this._arr[this._pos]<<t|this._arr[this._pos]>>>32-t,this}rShiftEq(t){const r=new Uint32Array(1);return r[t>>>5]=this._arr[this._pos]>>>t,this._arr[this._pos]=r[0],this}rRotEq(t){return t&=31,this._arr[this._pos]=this._arr[this._pos]>>>t|this._arr[this._pos]<<32-t,this}xorEq(t){return this._arr[this._pos]^=this._value(t),this}orEq(t){return this._arr[this._pos]|=this._value(t),this}andEq(t){return this._arr[this._pos]&=this._value(t),this}notEq(){return this._arr[this._pos]=~this._arr[this._pos],this}addEq(t){return this._arr[this._pos]+=this._value(t),this}subEq(t){return this._arr[this._pos]-=this._value(t),this}mulEq(t){return this._arr[this._pos]=Math.imul(this._arr[this._pos],this._value(t)),this}}