const w=1n<<15n,f=0b11111n<<10n,b=1n<<9n,d=b-1n,I=b|d,g=1n<<31n,l=0b11111111n<<23n,A=1n<<22n,E=A-1n,p=A|E,N=1n<<63n,a=0b11111111111n<<52n,c=1n<<51n,u=c-1n,F=c|u,_=u-(d<<42n),S=u-(E<<29n),m={2:"0b",8:"0o",16:"0x"};var x=(t=>(t[t.NATURAL=-2]="NATURAL",t[t.UNKNOWN=-1]="UNKNOWN",t[t.F16=2]="F16",t[t.F32=4]="F32",t[t.F64=8]="F64",t))(x||{});function U(i,n,e,r){let o="nan'";return i.quiet||(o+="!"),i.sign===-1&&(o+="-"),o+=r(Math.abs(i.payload),e),o+="'",o+=i.encodingIndicator,o}class O extends Number{#n;#t=-1;constructor(n,e=!0,r=-1){super(NaN);const o=n;if(typeof n=="number"){if(!Number.isSafeInteger(n))throw new Error(`Invalid NAN payload: ${n}`);n=BigInt(n);let t=0n;if(n<0&&(t=N,n=-n),n>=c)throw new Error(`Payload too large: ${o}`);const s=e?c:0n;switch(this.#n=t|a|s|n,r){case-2:throw new Error("NAN_SIZE.NATURAL only valid for bigint constructor");case-1:r=this.preferredSize;break;case 2:if(this.#n&_)throw new Error("Invalid size for payload");break;case 4:if(this.#n&S)throw new Error("Invalid size for payload");break;case 8:break;default:throw new Error(`Invalid size: ${r}`)}this.#t=r}else if(typeof n=="bigint"){let t=-1;if((n&a)===a)this.#n=n,t=8;else if((n&l)===l){const s=(n&g)<<32n;this.#n=s|a|(n&p)<<29n,t=4}else if((n&f)===f){const s=(n&w)<<48n;this.#n=s|a|(n&I)<<42n,t=2}else throw new Error(`Invalid raw NaN value: ${n}`);if(r===-1)this.#t=this.preferredSize;else if(r===-2)this.#t=t;else{if(r<t)throw new Error("Invalid bigint NaN size");this.#t=r}}else{const t=new DataView(n.buffer,n.byteOffset,n.byteLength);switch(n.length){case 3:{if(n[0]!==249)throw new Error("Invalid CBOR encoding for half float");const s=BigInt(t.getUint16(1,!1));if((s&f)!==f)throw new Error("Not a NaN");const h=(s&w)<<48n;this.#n=h|a|(s&I)<<42n,this.#t=2;break}case 5:{if(n[0]!==250)throw new Error("Invalid CBOR encoding for single float");const s=BigInt(t.getUint32(1,!1));if((s&l)!==l)throw new Error("Not a NaN");const h=(s&g)<<32n;this.#n=h|a|(s&p)<<29n,this.#t=4;break}case 9:{if(n[0]!==251)throw new Error("Invalid CBOR encoding for double float");if(this.#n=t.getBigUint64(1,!1),(this.#n&a)!==a)throw new Error("Not a NaN (NaNaN)");this.#t=8;break}default:throw new RangeError(`Invalid NAN size (should be 2, 4, or 8): ${n.length-1}`)}}if(!this.payload&&!this.quiet)throw new Error("Signalling NaN with zero payload")}get bytes(){const n=new ArrayBuffer(this.#t+1),e=new DataView(n);switch(this.#t){case 2:{e.setUint8(0,249);const o=(this.#n&N?w:0n)|f|(this.#n&F)>>42n;e.setUint16(1,Number(o),!1);break}case 4:{e.setUint8(0,250);const o=(this.#n&N?g:0n)|l|(this.#n&F)>>29n;e.setUint32(1,Number(o),!1);break}case 8:e.setUint8(0,251),e.setBigUint64(1,this.#n);break}return new Uint8Array(n)}get quiet(){return!!(this.#n&c)}get sign(){return this.#n&N?-1:1}get payload(){return Number(this.#n&u)*this.sign}get raw(){return this.#n}get encodingIndicator(){switch(this.#t){case 2:return"_1";case 4:return"_2"}return"_3"}get size(){return this.#t}get preferredSize(){return(this.#n&_)===0n?2:(this.#n&S)===0n?4:8}get isShortestEncoding(){return this.preferredSize===this.#t}toCBOR(n){n.write(this.bytes)}toString(n=10){return U(this,1,{},e=>(m[n]??"")+e.toString(n))}[Symbol.for("nodejs.util.inspect.custom")](n,e,r){return U(this,n,e,r)}}function v(i,n=0,e=!1){const r=i[n]&128?-1:1,o=(i[n]&124)>>2,t=(i[n]&3)<<8|i[n+1];if(o===0){if(e&&t!==0)throw new Error(`Unwanted subnormal: ${r*5960464477539063e-23*t}`);return r*5960464477539063e-23*t}else if(o===31)return t?NaN:r*(1/0);return r*2**(o-25)*(1024+t)}function Z(i){const n=new DataView(new ArrayBuffer(4));n.setFloat32(0,i,!1);const e=n.getUint32(0,!1);if((e&8191)!==0)return null;let r=e>>16&32768;const o=e>>23&255,t=e&8388607;if(!(o===0&&t===0))if(o>=113&&o<=142)r+=(o-112<<10)+(t>>13);else if(o>=103&&o<113){if(t&(1<<126-o)-1)return null;r+=t+8388608>>126-o}else if(o===255)r|=31744,r|=t>>13;else return null;return r}function k(i){if(i!==0){const n=new ArrayBuffer(8),e=new DataView(n);e.setFloat64(0,i,!1);const r=e.getBigUint64(0,!1);if((r&0x7ff0000000000000n)===0n)return r&0x8000000000000000n?-0:0}return i}function B(i){switch(i.length){case 2:v(i,0,!0);break;case 4:{const n=new DataView(i.buffer,i.byteOffset,i.byteLength),e=n.getUint32(0,!1);if((e&2139095040)===0&&e&8388607)throw new Error(`Unwanted subnormal: ${n.getFloat32(0,!1)}`);break}case 8:{const n=new DataView(i.buffer,i.byteOffset,i.byteLength),e=n.getBigUint64(0,!1);if((e&0x7ff0000000000000n)===0n&&e&0x000fffffffffffn)throw new Error(`Unwanted subnormal: ${n.getFloat64(0,!1)}`);break}default:throw new TypeError(`Bad input to isSubnormal: ${i}`)}}export{O as NAN,x as NAN_SIZE,B as checkSubnormal,k as flushToZero,Z as halfToUint,v as parseHalf};
