gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 5.15 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{sLen as e}from"../../safe/safe.js";import{AInt as o}from"./_AInt.js";const n=Symbol.for("nodejs.util.inspect.custom");export class U64 extends o{constructor(t,r,s="U64"){super(t,r,2,s)}static fromInt(t){return new U64(o._fromInt(2,t),0)}static fromI32s(...t){const r=o._fromSet(2,t);return new U64(r,0)}static fromBytesBE(t,r=0){return new U64(this._fromBytesBE(8,t,r),0)}static mount(t,r=0){return e("arr",t).atLeast(r+2).throwNot(),new U64(t,r)}get low(){return this._arr[this._pos]}get high(){return this._arr[this._pos+1]}clone(){return new U64(this._arr.slice(this._pos,this._pos+2),0)}mut(){const t=this._arr.slice(this._pos,this._pos+2);return U64Mut.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(64-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 U64(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)}ctEq(t){return 0===(this._arr[this._pos]^t._arr[t._pos]|this._arr[this._pos+1]^t._arr[t._pos+1])}ctGt(t){return((65535&this._arr[this._pos])-(65535&t._arr[t._pos])-1&(this._arr[this._pos]>>>16)-(t._arr[t._pos]>>>16)-1&(65535&this._arr[this._pos+1])-(65535&t._arr[t._pos+1])-1&(this._arr[this._pos+1]>>>16)-(t._arr[t._pos+1]>>>16)-1)>>>31==0}ctGte(t){return((65535&t._arr[t._pos])-(65535&this._arr[this._pos])-1&(t._arr[t._pos]>>>16)-(this._arr[this._pos]>>>16)-1&(65535&t._arr[t._pos+1])-(65535&this._arr[this._pos+1])-1&(t._arr[t._pos+1]>>>16)-(this._arr[this._pos+1]>>>16)-1)>>>31==1}ctLt(t){return((65535&t._arr[t._pos])-(65535&this._arr[this._pos])-1&(t._arr[t._pos]>>>16)-(this._arr[this._pos]>>>16)-1&(65535&t._arr[t._pos+1])-(65535&this._arr[this._pos+1])-1&(t._arr[t._pos+1]>>>16)-(this._arr[this._pos+1]>>>16)-1)>>>31==0}ctLte(t){return((65535&this._arr[this._pos])-(65535&t._arr[t._pos])-1&(this._arr[this._pos]>>>16)-(t._arr[t._pos]>>>16)-1&(65535&this._arr[this._pos+1])-(65535&t._arr[t._pos+1])-1&(this._arr[this._pos+1]>>>16)-(t._arr[t._pos+1]>>>16)-1)>>>31==1}ctSwitch(t,r){const s=(0|r)-1;return U64.fromI32s(~s&t._arr[t._pos]|s&this._arr[this._pos],~s&t._arr[t._pos+1]|s&this._arr[this._pos+1])}static get zero(){return i}}const i=U64.mount(new Uint32Array(2),0);export class U64Mut extends U64{constructor(t,r){super(t,r,"U64Mut")}set(t){return super._setValue(t),this}zero(){return super._setZero(),this}static fromInt(t){return new U64Mut(o._fromInt(2,t),0)}static fromI32s(...t){const r=o._fromSignedSet(2,t);return new U64Mut(r,0)}static fromBytesBE(t,r=0){return new U64Mut(this._fromBytesBE(8,t,r),0)}static mount(t,r=0){return e("arr",t).atLeast(r+2).throwNot(),new U64Mut(t,r)}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(64-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}divEq32(t){const r=this._div32(t);return this._arr.set(r,this._pos),this}}export class U64MutArray{constructor(t,r=0,s){null==s&&(s=t.length-r>>1),this.buf=t,this.bufPos=r,this.arr=new Array(s);for(let t=0;t<s;t++)this.arr[t]=U64Mut.mount(this.buf,this.bufPos+t+t)}get length(){return this.arr.length}at(t){return this.arr[t]}set(t,r=0,s=0){this.buf.set(t.buf.subarray(t.bufPos+2*s),this.bufPos+2*r)}subarray(t=0,r){return new U64MutArray(this.buf.subarray(2*t,r?2*r:void 0))}clone(){return new U64MutArray(this.buf.slice(this.bufPos,this.bufPos+this.length+this.length),0)}toBytesBE(){const t=this.buf.slice(this.bufPos,this.bufPos+2*this.arr.length),s=new Uint8Array(t.buffer);let e=1;do{const o=t[e-1];t[e-1]=t[e],t[e]=o,r.i32(s,4*e-4,2),e+=2}while(e<t.length);return s}toBytesLE(){const t=new Uint8Array(this.buf.slice(this.bufPos,this.bufPos+2*this.arr.length).buffer);return s.i32(t,0,this.arr.length),t}toString(){return t.fromBytes(this.toBytesBE())}get[Symbol.toStringTag](){return"U64MutArray"}[n](){return`U64MutArray(${this.toString()})`}static fromLen(t){return new U64MutArray(new Uint32Array(2*t),0,t)}static fromU64s(...t){const r=new Uint32Array(2*t.length);for(let s=0,e=0;s<t.length;s++)r[e++]=t[s].low,r[e++]=t[s].high;return new U64MutArray(r,0)}static mount(t,r=0,s=0){return 0==s?s=(t.length-r)/2:e("arr",t).atLeast(2*s+r).throwNot(),new U64MutArray(t,r,s)}static fromBytes(t,r=0,s){return null==s&&(s=t.byteLength-r),s>>=2,new U64MutArray(new Uint32Array(t,r,s),0)}}