UNPKG

gnablib

Version:

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

2 lines 2.53 kB
/*! Copyright 2024-2025 the gnablib contributors MPL-1.1 */ import{sInt as t,sLen as r}from"../../safe/safe.js";import{AInt as s}from"./_AInt.js";import{U64 as e}from"./U64.js";const n=16;export class U128 extends s{constructor(t,r,s="U128"){super(t,r,4,s)}static fromInt(t){return new U128(s._fromInt(4,t),0)}static fromI32s(...t){const r=s._fromSet(4,t);return new U128(r,0)}static fromBytesBE(t,r=0){return new U128(this._fromBytesBE(n,t,r),0)}static fromBytesLE(t,r=0){return new U128(this._fromBytesLE(n,t,r),0)}static mount(t,s=0){return r("arr",t).atLeast(s+4).throwNot(),new U128(t,s)}clone(){return new U128(this._arr.slice(this._pos,this._pos+4),0)}mut(){const t=this._arr.slice(this._pos,this._pos+4);return U128Mut.mount(t,0)}lShift(t){const r=this.clone();return r._lShiftEq(t),r}rShift(t){const r=this.clone();return r._rShiftEq(t),r}lRot(t){const r=this.clone();return r._lRotEq(t),r}rRot(t){const r=this.clone();return r._lRotEq(128-t),r}xor(t){const r=this.clone();return r._xorEq(t),r}or(t){const r=this.clone();return r._orEq(t),r}and(t){const r=this.clone();return r._andEq(t),r}not(){const t=this.clone();return t._notEq(),t}add(t){const r=this.clone();return r._addEq(t),r}sub(t){const r=this.clone();return r._subEq(t),r}mul(t){const r=this._mul(t);return new U128(r,0)}eq(t){return super.eq(t)}gt(t){return super.gt(t)}gte(t){return super.gte(t)}lt(t){return super.lt(t)}lte(t){return super.lte(t)}static get zero(){return o}}const o=U128.mount(new Uint32Array(4),0);export class U128Mut extends U128{constructor(t,r){super(t,r,"U128Mut")}set(t){return super._setValue(t),this}zero(){return super._setZero(),this}u64at(r=0){return t("idx",r).unsigned().atMost(1).throwNot(),e.mount(this._arr,this._pos+2*r)}static fromInt(t){return new U128Mut(s._fromInt(4,t),0)}static fromI32s(...t){const r=s._fromSignedSet(4,t);return new U128Mut(r,0)}static fromBytesBE(t,r=0){return new U128Mut(this._fromBytesBE(n,t,r),0)}static fromBytesLE(t,r=0){return new U128Mut(this._fromBytesLE(n,t,r),0)}static mount(t,s=0){return r("arr",t).atLeast(s+4).throwNot(),new U128Mut(t,s)}lShiftEq(t){return this._lShiftEq(t),this}lRotEq(t){return this._lRotEq(t),this}rShiftEq(t){return this._rShiftEq(t),this}rRotEq(t){return this._lRotEq(128-t),this}xorEq(t){return this._xorEq(t),this}orEq(t){return this._orEq(t),this}andEq(t){return this._andEq(t),this}notEq(){return this._notEq(),this}addEq(t){return this._addEq(t),this}subEq(t){return this._subEq(t),this}mulEq(t){const r=this._mul(t);return this._arr.set(r,this._pos),this}}