toilscript 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +3736 -4
- package/dist/cli.js.map +2 -2
- package/dist/importmap.json +2 -2
- package/dist/web.js +3 -3
- package/package.json +1 -1
- package/std/assembly/bignum/LICENSE +201 -0
- package/std/assembly/bignum/NOTICE +13 -0
- package/std/assembly/bignum/fixed/fp128.ts +58 -0
- package/std/assembly/bignum/fixed/fp256.ts +9 -0
- package/std/assembly/bignum/fixed/index.ts +38 -0
- package/std/assembly/bignum/fixed/safe/fp128.ts +3 -0
- package/std/assembly/bignum/fixed/safe/fp256.ts +3 -0
- package/std/assembly/bignum/fixed/types.ts +103 -0
- package/std/assembly/bignum/globals.ts +546 -0
- package/std/assembly/bignum/index.ts +1 -0
- package/std/assembly/bignum/integer/i128.ts +413 -0
- package/std/assembly/bignum/integer/i256.ts +50 -0
- package/std/assembly/bignum/integer/index.ts +3 -0
- package/std/assembly/bignum/integer/u128.ts +963 -0
- package/std/assembly/bignum/integer/u256.ts +1007 -0
- package/std/assembly/bignum/utils.ts +260 -0
- package/std/assembly/bignum.ts +18 -0
- package/std/assembly/index.d.ts +257 -0
package/dist/cli.js
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* Copyright 2026 Daniel Wirtz / The ToilScript Authors
|
|
5
5
|
* SPDX-License-Identifier: Apache-2.0
|
|
6
6
|
*/
|
|
7
|
-
var se=Object.defineProperty;var Ne=Object.getOwnPropertyDescriptor;var
|
|
8
|
-
`,r={},u=[];Object.keys(e).forEach(b=>{var d=e[b];if(d.description!=null){for(var g="";g.length<n;)g+=" ";for(g+="--"+b,d.alias&&(g+=", -"+d.alias);g.length<a;)g+=" ";var y;!t.noCategories&&d.category?(y=r[d.category])||(r[d.category]=y=[]):y=u,Array.isArray(d.description)?y.push(g+d.description[0]+d.description.slice(1).map(m=>{for(let i=0;i<a;++i)m=" "+m;return s+m}).join("")):y.push(g+d.description)}});var f=[],p=!1;return Object.keys(r).forEach(b=>{p=!0,f.push(s+" "+_n.gray(b)+s),f.push(r[b].join(s))}),p&&u.length&&f.push(s+" "+_n.gray("Other")+s),f.push(u.join(s)),f.join(s)}function be(e,t){if(e!=null)switch(t){case void 0:case"b":return!!e;case"i":return Math.trunc(e)||0;case"f":return Number(e)||0;case"s":return e===!0?"":e===!1?null:String(e);case"I":return Array.isArray(e)||(e=[e]),e.map(n=>Math.trunc(n)||0);case"F":return Array.isArray(e)||(e=[e]),e.map(n=>Number(n)||0);case"S":return Array.isArray(e)||(e=[e]),e.map(String)}}function Mn(e,t,n,a){let s={};for(let[r,{type:u,mutuallyExclusive:f,isPath:p,useNodeResolution:b,cliOnly:d}]of Object.entries(e)){let g=be(t[r],u),y=be(n[r],u);if(g==null){if(y!=null){if(d)continue;if(Array.isArray(y)){let m;p&&(y=y.map(i=>en(i,a,b))),f!=null&&(m=t[f])?s[r]=y.filter(i=>!m.includes(i)):s[r]=y.slice()}else p&&(y=en(y,a,b)),s[r]=y}}else if(y==null)Array.isArray(g)?s[r]=g.slice():s[r]=g;else if(Array.isArray(g)){if(d){s[r]=g.slice();continue}let m;p&&(y=y.map(i=>en(i,a,b))),f!=null&&(m=t[f])?s[r]=[...g,...y.filter(i=>!g.includes(i)&&!m.includes(i))]:s[r]=[...g,...y.filter(i=>!g.includes(i))]}else s[r]=g}return s}function Ft(e){let t=v.parse(e);return t.root||(t.root="./"),v.format(t)}function en(e,t,n=!1){return v.isAbsolute(e)?e:n&&!e.startsWith(".")&&me.resolve?me.resolve(e,{paths:[t]}):Ft(v.join(t,e))}function Un(e,t){for(let[n,{default:a}]of Object.entries(e))t[n]==null&&a!=null&&(t[n]=a)}var ve="0.1.4",Y={version:{category:"General",description:"Prints just the compiler's version and exits.",type:"b",alias:"v"},help:{category:"General",description:"Prints this message and exits.",type:"b",alias:"h"},config:{category:"General",description:"Configuration file to apply. CLI arguments take precedence.",type:"s",cliOnly:!0},target:{category:"General",description:"Configuration file target to use. Defaults to 'release'.",type:"s",cliOnly:!0},optimize:{category:"Optimization",description:["Optimizes the module. Typical shorthands are:",""," Default optimizations -O"," Make a release build -O --noAssert"," Make a debug build --debug"," Optimize for speed -Ospeed"," Optimize for size -Osize",""],type:"b",alias:"O"},optimizeLevel:{category:"Optimization",description:"How much to focus on optimizing code. [0-3]",type:"i"},shrinkLevel:{category:"Optimization",description:"How much to focus on shrinking code size. [0-2, s=1, z=2]",type:"i"},converge:{category:"Optimization",description:"Re-optimizes until no further improvements can be made.",type:"b",default:!1},noAssert:{category:"Optimization",description:"Replaces assertions with just their value without trapping.",type:"b",default:!1},outFile:{category:"Output",description:"Specifies the WebAssembly output file (.wasm).",type:"s",alias:"o",isPath:!0},textFile:{category:"Output",description:"Specifies the WebAssembly text output file (.wat).",type:"s",alias:"t",isPath:!0},bindings:{category:"Output",description:["Specifies the bindings to generate (.js + .d.ts).",""," esm JavaScript bindings & typings for ESM integration."," raw Like esm, but exports just the instantiate function."," Useful where modules are meant to be instantiated"," multiple times or non-ESM imports must be provided."],type:"S",alias:"b"},sourceMap:{category:"Debugging",description:["Enables source map generation. Optionally takes the URL","used to reference the source map from the binary file."],type:"s"},uncheckedBehavior:{category:"Debugging",description:["Changes the behavior of unchecked() expressions.","Using this option can potentially cause breakage.",""," default The default behavior: unchecked operations are"," only used inside of unchecked()."," never Unchecked operations are never used, even when"," inside of unchecked()."," always Unchecked operations are always used if possible,"," whether or not unchecked() is used."],type:"s",default:"default"},debug:{category:"Debugging",description:"Enables debug information in emitted binaries.",type:"b",default:!1},importMemory:{category:"Features",description:"Imports the memory from 'env.memory'.",type:"b",default:!1},noExportMemory:{category:"Features",description:"Does not export the memory as 'memory'.",type:"b",default:!1},initialMemory:{category:"Features",description:"Sets the initial memory size in pages.",type:"i",default:0},maximumMemory:{category:"Features",description:"Sets the maximum memory size in pages.",type:"i",default:0},sharedMemory:{category:"Features",description:"Declare memory as shared. Requires maximumMemory.",type:"b",default:!1},zeroFilledMemory:{category:"Features",description:"Assume imported memory is zeroed. Requires importMemory.",type:"b",default:!1},importTable:{category:"Features",description:"Imports the function table from 'env.table'.",type:"b",default:!1},exportTable:{category:"Features",description:"Exports the function table as 'table'.",type:"b",default:!1},exportStart:{category:"Features",description:["Exports the start function using the specified name instead","of calling it implicitly. Useful to obtain the exported memory","before executing any code accessing it."],type:"s"},runtime:{category:"Features",description:["Specifies the runtime variant to include in the program.",""," incremental TLSF + incremental GC (default)"," minimal TLSF + lightweight GC invoked externally"," stub Minimal runtime stub (never frees)"," ... Path to a custom runtime implementation",""],type:"s",default:"incremental"},exportRuntime:{category:"Features",description:["Always exports the runtime helpers (__new, __collect, __pin etc.).","Automatically determined when generation of --bindings is enabled."],type:"b",default:!1},stackSize:{category:"Features",description:["Overrides the stack size. Only relevant for incremental GC","or when using a custom runtime that requires stack space.","Defaults to 0 without and to 32768 with incremental GC."],default:0,type:"i"},enable:{category:"Features",description:["Enables WebAssembly features being disabled by default.",""," threads Threading and atomic operations."," simd SIMD types and operations."," reference-types Reference types and operations."," gc Garbage collection (WIP)."," stringref String reference types."," relaxed-simd Relaxed SIMD operations.",""],TODO_doesNothingYet:[" exception-handling Exception handling."," tail-calls Tail call operations."," multi-value Multi value types."," memory64 Memory64 operations."," extended-const Extended const expressions."],type:"S",mutuallyExclusive:"disable"},disable:{category:"Features",description:["Disables WebAssembly features being enabled by default.",""," mutable-globals Mutable global imports and exports."," sign-extension Sign-extension operations"," nontrapping-f2i Non-trapping float to integer ops."," bulk-memory Bulk memory operations.",""],type:"S",mutuallyExclusive:"enable"},use:{category:"Features",description:["Aliases a global object under another name, e.g., to switch","the default 'Math' implementation used: --use Math=JSMath","Can also be used to introduce an integer constant."],type:"S",alias:"u"},lowMemoryLimit:{category:"Features",description:"Enforces very low (<64k) memory constraints.",default:0,type:"i"},memoryBase:{category:"Linking",description:"Sets the start offset of emitted memory segments.",type:"i",default:0},tableBase:{category:"Linking",description:"Sets the start offset of emitted table elements.",type:"i",default:0},transform:{category:"API",description:"Specifies the path to a custom transform to load.",type:"S",isPath:!0,useNodeResolution:!0},trapMode:{category:"Binaryen",description:["Sets the trap mode to use.",""," allow Allow trapping operations. This is the default."," clamp Replace trapping operations with clamping semantics."," js Replace trapping operations with JS semantics.",""],type:"s",default:"allow"},runPasses:{category:"Binaryen",description:["Specifies additional Binaryen passes to run after other","optimizations, if any. See: Binaryen/src/passes/pass.cpp"],type:"s"},noValidate:{category:"Binaryen",description:"Skips validating the module using Binaryen.",type:"b",default:!1},baseDir:{description:"Specifies the base directory of input and output files.",type:"s",default:"."},noColors:{description:"Disables terminal colors.",type:"b",default:!1},noUnsafe:{description:["Disallows the use of unsafe features in user code.","Does not affect library files and external modules."],type:"b",default:!1},disableWarning:{description:["Disables warnings matching the given diagnostic code.","If no diagnostic code is given, all warnings are disabled."],type:"I"},noEmit:{description:"Performs compilation as usual but does not emit code.",type:"b",default:!1},showConfig:{description:"Print computed compiler options and exit.",type:"b",default:!1},stats:{description:"Prints statistics on I/O and compile times.",type:"b",default:!1},pedantic:{description:"Make yourself sad for no good reason.",type:"b",default:!1},lib:{description:["Adds one or multiple paths to custom library components and","uses exports of all top-level files at this path as globals."],type:"S",isPath:!0},path:{description:["Adds one or multiple paths to package resolution, similar","to node_modules. Prefers an 'ascMain' entry in a package's","package.json and falls back to an inner 'assembly/' folder."],type:"S",isPath:!0},wasm:{description:"Uses the specified Wasm binary of the compiler.",type:"s"}," ...":{description:"Specifies node.js options (CLI only). See: node --help"},"-Os":{value:{optimizeLevel:0,shrinkLevel:1}},"-Oz":{value:{optimizeLevel:0,shrinkLevel:2}},"-O0":{value:{optimizeLevel:0,shrinkLevel:0}},"-O1":{value:{optimizeLevel:1,shrinkLevel:0}},"-O2":{value:{optimizeLevel:2,shrinkLevel:0}},"-O3":{value:{optimizeLevel:3,shrinkLevel:0}},"-O0s":{value:{optimizeLevel:0,shrinkLevel:1}},"-O1s":{value:{optimizeLevel:1,shrinkLevel:1}},"-O2s":{value:{optimizeLevel:2,shrinkLevel:1}},"-O3s":{value:{optimizeLevel:3,shrinkLevel:1}},"-O0z":{value:{optimizeLevel:0,shrinkLevel:2}},"-O1z":{value:{optimizeLevel:1,shrinkLevel:2}},"-O2z":{value:{optimizeLevel:2,shrinkLevel:2}},"-O3z":{value:{optimizeLevel:3,shrinkLevel:2}},"-Ospeed":{value:{optimizeLevel:3,shrinkLevel:0}},"-Osize":{value:{optimizeLevel:0,shrinkLevel:2,converge:!0}},"--measure":{value:{stats:!0}}},Ee="~lib/",Ae={array:`/// <reference path="./rt/index.d.ts" />
|
|
7
|
+
var se=Object.defineProperty;var Ne=Object.getOwnPropertyDescriptor;var Ue=Object.getOwnPropertyNames;var Me=Object.prototype.hasOwnProperty;var fn=(e,t)=>()=>(e&&(t=e(e=0)),t);var Q=(e,t)=>{for(var n in t)se(e,n,{get:t[n],enumerable:!0})},ae=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of Ue(t))!Me.call(e,s)&&s!==n&&se(e,s,{get:()=>t[s],enumerable:!(a=Ne(t,s))||a.enumerable});return e},Dn=(e,t,n)=>(ae(e,t,"default"),n&&ae(n,t,"default"));var le={};Q(le,{promises:()=>Pe});var Pe,ue=fn(()=>{"use strict";Pe={}});var fe={};Q(fe,{createRequire:()=>Ve});function Ve(){return function(t){throw new Error(`Cannot find module: '${t}'`)}}var ce=fn(()=>{"use strict"});var On={};Q(On,{argv:()=>Xe,cwd:()=>In,exit:()=>He,hrtime:()=>qe,platform:()=>je,umask:()=>Ge});function In(){return"."}function Ge(){return 0}function He(e=0){throw Error(`exit ${e}`)}function qe(e){var t=Ke.call(de),n=Math.floor(t*.001),a=Math.floor(t*1e6-n*1e9);return e&&(n-=e[0],a-=e[1],a<0&&(n--,a+=1e9)),[n,a]}var je,Xe,de,Ke,Ln=fn(()=>{"use strict";je="linux";Xe=[];de=globalThis.performance||{},Ke=de.now||function(){return new Date().getTime()}});var Rn={};Q(Rn,{basename:()=>Qe,delimiter:()=>it,dirname:()=>Je,extname:()=>nt,format:()=>et,isAbsolute:()=>Ye,join:()=>Ze,normalize:()=>pe,parse:()=>tt,relative:()=>$e,resolve:()=>cn,sep:()=>kn,win32:()=>rt});function V(e){if(typeof e!="string")throw new TypeError("Path must be a string. Received "+JSON.stringify(e))}function xe(e,t){for(var n="",a=0,s=-1,r=0,u,f=0;f<=e.length;++f){if(f<e.length)u=e.charCodeAt(f);else{if(u===47)break;u=47}if(u===47){if(!(s===f-1||r===1))if(s!==f-1&&r===2){if(n.length<2||a!==2||n.charCodeAt(n.length-1)!==46||n.charCodeAt(n.length-2)!==46){if(n.length>2){var p=n.lastIndexOf("/");if(p!==n.length-1){p===-1?(n="",a=0):(n=n.slice(0,p),a=n.length-1-n.lastIndexOf("/")),s=f,r=0;continue}}else if(n.length===2||n.length===1){n="",a=0,s=f,r=0;continue}}t&&(n.length>0?n+="/..":n="..",a=2)}else n.length>0?n+="/"+e.slice(s+1,f):n=e.slice(s+1,f),a=f-s-1;s=f,r=0}else u===46&&r!==-1?++r:r=-1}return n}function We(e,t){var n=t.dir||t.root,a=t.base||(t.name||"")+(t.ext||"");return n?n===t.root?n+a:n+e+a:a}function cn(){for(var e="",t=!1,n,a=arguments.length-1;a>=-1&&!t;a--){var s;a>=0?s=arguments[a]:(n===void 0&&(n=In()),s=n),V(s),s.length!==0&&(e=s+"/"+e,t=s.charCodeAt(0)===47)}return e=xe(e,!t),t?e.length>0?"/"+e:"/":e.length>0?e:"."}function pe(e){if(V(e),e.length===0)return".";var t=e.charCodeAt(0)===47,n=e.charCodeAt(e.length-1)===47;return e=xe(e,!t),e.length===0&&!t&&(e="."),e.length>0&&n&&(e+="/"),t?"/"+e:e}function Ye(e){return V(e),e.length>0&&e.charCodeAt(0)===47}function Ze(){if(arguments.length===0)return".";for(var e,t=0;t<arguments.length;++t){var n=arguments[t];V(n),n.length>0&&(e===void 0?e=n:e+="/"+n)}return e===void 0?".":pe(e)}function $e(e,t){if(V(e),V(t),e===t||(e=cn(e),t=cn(t),e===t))return"";if(e===".")return t;for(var n=1;n<e.length&&e.charCodeAt(n)===47;++n);for(var a=e.length,s=a-n,r=1;r<t.length&&t.charCodeAt(r)===47;++r);for(var u=t.length,f=u-r,p=s<f?s:f,b=-1,d=0;d<=p;++d){if(d===p){if(f>p){if(t.charCodeAt(r+d)===47)return t.slice(r+d+1);if(d===0)return t.slice(r+d)}else s>p&&(e.charCodeAt(n+d)===47?b=d:d===0&&(b=0));break}var g=e.charCodeAt(n+d),y=t.charCodeAt(r+d);if(g!==y)break;g===47&&(b=d)}var m="";for(d=n+b+1;d<=a;++d)(d===a||e.charCodeAt(d)===47)&&(m.length===0?m+="..":m+="/..");return m.length>0?m+t.slice(r+b):(r+=b,t.charCodeAt(r)===47&&++r,t.slice(r))}function Je(e){if(V(e),e.length===0)return".";for(var t=e.charCodeAt(0),n=t===47,a=-1,s=!0,r=e.length-1;r>=1;--r)if(t=e.charCodeAt(r),t===47){if(!s){a=r;break}}else s=!1;return a===-1?n?"/":".":n&&a===1?"//":e.slice(0,a)}function Qe(e,t){if(t!==void 0&&typeof t!="string")throw new TypeError('"ext" argument must be a string');V(e);var n=0,a=-1,s=!0,r;if(t!==void 0&&t.length>0&&t.length<=e.length){if(t.length===e.length&&t===e)return"";var u=t.length-1,f=-1;for(r=e.length-1;r>=0;--r){var p=e.charCodeAt(r);if(p===47){if(!s){n=r+1;break}}else f===-1&&(s=!1,f=r+1),u>=0&&(p===t.charCodeAt(u)?--u===-1&&(a=r):(u=-1,a=f))}return n===a?a=f:a===-1&&(a=e.length),e.slice(n,a)}else{for(r=e.length-1;r>=0;--r)if(e.charCodeAt(r)===47){if(!s){n=r+1;break}}else a===-1&&(s=!1,a=r+1);return a===-1?"":e.slice(n,a)}}function nt(e){V(e);for(var t=-1,n=0,a=-1,s=!0,r=0,u=e.length-1;u>=0;--u){var f=e.charCodeAt(u);if(f===47){if(!s){n=u+1;break}continue}a===-1&&(s=!1,a=u+1),f===46?t===-1?t=u:r!==1&&(r=1):t!==-1&&(r=-1)}return t===-1||a===-1||r===0||r===1&&t===a-1&&t===n+1?"":e.slice(t,a)}function et(e){if(e===null||typeof e!="object")throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof e);return We("/",e)}function tt(e){V(e);var t={root:"",dir:"",base:"",ext:"",name:""};if(e.length===0)return t;var n=e.charCodeAt(0),a=n===47,s;a?(t.root="/",s=1):s=0;for(var r=-1,u=0,f=-1,p=!0,b=e.length-1,d=0;b>=s;--b){if(n=e.charCodeAt(b),n===47){if(!p){u=b+1;break}continue}f===-1&&(p=!1,f=b+1),n===46?r===-1?r=b:d!==1&&(d=1):r!==-1&&(d=-1)}return r===-1||f===-1||d===0||d===1&&r===f-1&&r===u+1?f!==-1&&(u===0&&a?t.base=t.name=e.slice(1,f):t.base=t.name=e.slice(u,f)):(u===0&&a?(t.name=e.slice(1,r),t.base=e.slice(1,f)):(t.name=e.slice(u,r),t.base=e.slice(u,f)),t.ext=e.slice(r,f)),u>0?t.dir=e.slice(0,u-1):a&&(t.dir="/"),t}var kn,it,rt,Nn=fn(()=>{"use strict";Ln();kn="/",it=":",rt=null});var he={};Q(he,{pathToFileURL:()=>at});function ot(e){return e.replace(/%/g,"%25").replace(/\\/g,"%5C").replace(/\n/g,"%0A").replace(/\r/g,"%0D").replace(/\t/g,"%09")}function at(e){let t=cn(e);e.charCodeAt(e.length-1)===47&&t[t.length-1]!==kn&&(t+="/");let n=new URL("file://");return n.pathname=ot(t),n}var ge=fn(()=>{"use strict";Nn()});var Wn={};Q(Wn,{Stats:()=>Cn,checkDiagnostics:()=>gn,compileString:()=>wt,configToArguments:()=>qn,createMemoryStream:()=>Hn,default:()=>Wn,defaultOptimizeLevel:()=>Be,defaultShrinkLevel:()=>we,definitionFiles:()=>Bt,libraryFiles:()=>X,libraryPrefix:()=>P,main:()=>Se,options:()=>zt,tscOptions:()=>St,version:()=>Xn});var st=Object.prototype.toString.call(typeof globalThis.process<"u"?globalThis.process:0)==="[object process]",G,sn,v,M,dn;st?(G=await import("fs"),sn=await import("module"),v=await import("path"),M=globalThis.process,dn=await import("url")):(G=await Promise.resolve().then(()=>(ue(),le)),sn=await Promise.resolve().then(()=>(ce(),fe)),v=await Promise.resolve().then(()=>(Nn(),Rn)),M=await Promise.resolve().then(()=>(Ln(),On)),dn=await Promise.resolve().then(()=>(ge(),he)));var An=typeof process<"u"&&process||{},lt=An.env&&"CI"in An.env,ut="\x1B[90m",ft="\x1B[91m",ct="\x1B[92m",dt="\x1B[93m",xt="\x1B[94m",pt="\x1B[95m",ht="\x1B[96m",gt="\x1B[97m",W="\x1B[0m",nn=class{constructor(t){this.stream=t,this.enabled=!!(this.stream&&this.stream.isTTY||lt)}gray(t){return this.enabled?ut+t+W:t}red(t){return this.enabled?ft+t+W:t}green(t){return this.enabled?ct+t+W:t}yellow(t){return this.enabled?dt+t+W:t}blue(t){return this.enabled?xt+t+W:t}magenta(t){return this.enabled?pt+t+W:t}cyan(t){return this.enabled?ht+t+W:t}white(t){return this.enabled?gt+t+W:t}},_n=new nn(An.stdout),Ot=new nn(An.stderr);function mt(e){for(var t=0,n=0,a=e.length;n<a;++n){let s=e.charCodeAt(n);s<128?t+=1:s<2048?t+=2:(s&64512)===55296&&n+1<a&&(e.charCodeAt(n+1)&64512)===56320?(++n,t+=4):t+=3}return t}function bt(e,t,n){var a=n-t;if(a<1)return"";for(var s=null,r=[],u=0,f;t<n;)f=e[t++],f<128?r[u++]=f:f>191&&f<224?r[u++]=(f&31)<<6|e[t++]&63:f>239&&f<365?(f=((f&7)<<18|(e[t++]&63)<<12|(e[t++]&63)<<6|e[t++]&63)-65536,r[u++]=55296+(f>>10),r[u++]=56320+(f&1023)):r[u++]=(f&15)<<12|(e[t++]&63)<<6|e[t++]&63,u>=8192&&((s||(s=[])).push(String.fromCharCode(...r)),u=0);return s?(u&&s.push(String.fromCharCode(...r.slice(0,u))),s.join("")):String.fromCharCode(...r.slice(0,u))}function yt(e,t,n){for(var a=n,s=0,r=e.length;s<r;++s){let u=e.charCodeAt(s),f;u<128?t[n++]=u:u<2048?(t[n++]=u>>6|192,t[n++]=u&63|128):(u&64512)===55296&&s+1<r&&((f=e.charCodeAt(s+1))&64512)===56320?(u=65536+((u&1023)<<10)+(f&1023),++s,t[n++]=u>>18|240,t[n++]=u>>12&63|128,t[n++]=u>>6&63|128,t[n++]=u&63|128):(t[n++]=u>>12|224,t[n++]=u>>6&63|128,t[n++]=u&63|128)}return n-a}var Tn={length:mt,read:bt,write:yt};var me=sn.createRequire(import.meta.url);function ye(e,t,n=!0){var a={},s=[],r=[],u=[],f={};Object.keys(t).forEach(d=>{if(!d.startsWith(" ")){var g=t[d];g.alias!=null&&(typeof g.alias=="string"?f[g.alias]=d:Array.isArray(g.alias)&&g.alias.forEach(y=>f[y]=d)),n&&g.default!=null&&(a[d]=g.default)}});for(var p=0,b=(e=e.slice()).length;p<b;++p){let d=e[p];if(d=="--"){++p;break}let g=/^(?:(-\w)(?:=(.*))?|(--\w{2,})(?:=(.*))?)$/.exec(d),y,m;if(g)t[d]?y=t[m=d]:g[1]!=null?(y=t[m=f[g[1].substring(1)]],y&&g[2]!=null&&(e[p--]=g[2])):g[3]!=null&&(y=t[m=g[3].substring(2)],y&&g[4]!=null&&(e[p--]=g[4]));else if(d.charCodeAt(0)==45)y=t[m=d];else{r.push(d);continue}if(y)if(y.value)Object.keys(y.value).forEach(i=>a[i]=y.value[i]);else if(y.type==null||y.type==="b")a[m]=!0;else if(p+1<e.length&&e[p+1].charCodeAt(0)!=45)switch(y.type){case"i":a[m]=parseInt(e[++p],10);break;case"I":a[m]=(a[m]||[]).concat(parseInt(e[++p],10));break;case"f":a[m]=parseFloat(e[++p]);break;case"F":a[m]=(a[m]||[]).concat(parseFloat(e[++p]));break;case"s":a[m]=String(e[++p]);break;case"S":a[m]=(a[m]||[]).concat(e[++p].split(","));break;default:s.push(d),--p}else switch(y.type){case"i":case"f":a[m]=y.default||0;break;case"s":a[m]=y.default||"";break;case"I":case"F":case"S":a[m]=y.default||[];break;default:s.push(d)}else s.push(d)}for(;p<b;)u.push(e[p++]);return n&&Mn(t,a),{options:a,unknown:s,arguments:r,trailing:u}}function Fe(e,t){t||(t={});var n=t.indent||2,a=t.padding||24,s=t.eol||`
|
|
8
|
+
`,r={},u=[];Object.keys(e).forEach(b=>{var d=e[b];if(d.description!=null){for(var g="";g.length<n;)g+=" ";for(g+="--"+b,d.alias&&(g+=", -"+d.alias);g.length<a;)g+=" ";var y;!t.noCategories&&d.category?(y=r[d.category])||(r[d.category]=y=[]):y=u,Array.isArray(d.description)?y.push(g+d.description[0]+d.description.slice(1).map(m=>{for(let i=0;i<a;++i)m=" "+m;return s+m}).join("")):y.push(g+d.description)}});var f=[],p=!1;return Object.keys(r).forEach(b=>{p=!0,f.push(s+" "+_n.gray(b)+s),f.push(r[b].join(s))}),p&&u.length&&f.push(s+" "+_n.gray("Other")+s),f.push(u.join(s)),f.join(s)}function be(e,t){if(e!=null)switch(t){case void 0:case"b":return!!e;case"i":return Math.trunc(e)||0;case"f":return Number(e)||0;case"s":return e===!0?"":e===!1?null:String(e);case"I":return Array.isArray(e)||(e=[e]),e.map(n=>Math.trunc(n)||0);case"F":return Array.isArray(e)||(e=[e]),e.map(n=>Number(n)||0);case"S":return Array.isArray(e)||(e=[e]),e.map(String)}}function Un(e,t,n,a){let s={};for(let[r,{type:u,mutuallyExclusive:f,isPath:p,useNodeResolution:b,cliOnly:d}]of Object.entries(e)){let g=be(t[r],u),y=be(n[r],u);if(g==null){if(y!=null){if(d)continue;if(Array.isArray(y)){let m;p&&(y=y.map(i=>en(i,a,b))),f!=null&&(m=t[f])?s[r]=y.filter(i=>!m.includes(i)):s[r]=y.slice()}else p&&(y=en(y,a,b)),s[r]=y}}else if(y==null)Array.isArray(g)?s[r]=g.slice():s[r]=g;else if(Array.isArray(g)){if(d){s[r]=g.slice();continue}let m;p&&(y=y.map(i=>en(i,a,b))),f!=null&&(m=t[f])?s[r]=[...g,...y.filter(i=>!g.includes(i)&&!m.includes(i))]:s[r]=[...g,...y.filter(i=>!g.includes(i))]}else s[r]=g}return s}function Ft(e){let t=v.parse(e);return t.root||(t.root="./"),v.format(t)}function en(e,t,n=!1){return v.isAbsolute(e)?e:n&&!e.startsWith(".")&&me.resolve?me.resolve(e,{paths:[t]}):Ft(v.join(t,e))}function Mn(e,t){for(let[n,{default:a}]of Object.entries(e))t[n]==null&&a!=null&&(t[n]=a)}var ve="0.1.5",Y={version:{category:"General",description:"Prints just the compiler's version and exits.",type:"b",alias:"v"},help:{category:"General",description:"Prints this message and exits.",type:"b",alias:"h"},config:{category:"General",description:"Configuration file to apply. CLI arguments take precedence.",type:"s",cliOnly:!0},target:{category:"General",description:"Configuration file target to use. Defaults to 'release'.",type:"s",cliOnly:!0},optimize:{category:"Optimization",description:["Optimizes the module. Typical shorthands are:",""," Default optimizations -O"," Make a release build -O --noAssert"," Make a debug build --debug"," Optimize for speed -Ospeed"," Optimize for size -Osize",""],type:"b",alias:"O"},optimizeLevel:{category:"Optimization",description:"How much to focus on optimizing code. [0-3]",type:"i"},shrinkLevel:{category:"Optimization",description:"How much to focus on shrinking code size. [0-2, s=1, z=2]",type:"i"},converge:{category:"Optimization",description:"Re-optimizes until no further improvements can be made.",type:"b",default:!1},noAssert:{category:"Optimization",description:"Replaces assertions with just their value without trapping.",type:"b",default:!1},outFile:{category:"Output",description:"Specifies the WebAssembly output file (.wasm).",type:"s",alias:"o",isPath:!0},textFile:{category:"Output",description:"Specifies the WebAssembly text output file (.wat).",type:"s",alias:"t",isPath:!0},bindings:{category:"Output",description:["Specifies the bindings to generate (.js + .d.ts).",""," esm JavaScript bindings & typings for ESM integration."," raw Like esm, but exports just the instantiate function."," Useful where modules are meant to be instantiated"," multiple times or non-ESM imports must be provided."],type:"S",alias:"b"},sourceMap:{category:"Debugging",description:["Enables source map generation. Optionally takes the URL","used to reference the source map from the binary file."],type:"s"},uncheckedBehavior:{category:"Debugging",description:["Changes the behavior of unchecked() expressions.","Using this option can potentially cause breakage.",""," default The default behavior: unchecked operations are"," only used inside of unchecked()."," never Unchecked operations are never used, even when"," inside of unchecked()."," always Unchecked operations are always used if possible,"," whether or not unchecked() is used."],type:"s",default:"default"},debug:{category:"Debugging",description:"Enables debug information in emitted binaries.",type:"b",default:!1},importMemory:{category:"Features",description:"Imports the memory from 'env.memory'.",type:"b",default:!1},noExportMemory:{category:"Features",description:"Does not export the memory as 'memory'.",type:"b",default:!1},initialMemory:{category:"Features",description:"Sets the initial memory size in pages.",type:"i",default:0},maximumMemory:{category:"Features",description:"Sets the maximum memory size in pages.",type:"i",default:0},sharedMemory:{category:"Features",description:"Declare memory as shared. Requires maximumMemory.",type:"b",default:!1},zeroFilledMemory:{category:"Features",description:"Assume imported memory is zeroed. Requires importMemory.",type:"b",default:!1},importTable:{category:"Features",description:"Imports the function table from 'env.table'.",type:"b",default:!1},exportTable:{category:"Features",description:"Exports the function table as 'table'.",type:"b",default:!1},exportStart:{category:"Features",description:["Exports the start function using the specified name instead","of calling it implicitly. Useful to obtain the exported memory","before executing any code accessing it."],type:"s"},runtime:{category:"Features",description:["Specifies the runtime variant to include in the program.",""," incremental TLSF + incremental GC (default)"," minimal TLSF + lightweight GC invoked externally"," stub Minimal runtime stub (never frees)"," ... Path to a custom runtime implementation",""],type:"s",default:"incremental"},exportRuntime:{category:"Features",description:["Always exports the runtime helpers (__new, __collect, __pin etc.).","Automatically determined when generation of --bindings is enabled."],type:"b",default:!1},stackSize:{category:"Features",description:["Overrides the stack size. Only relevant for incremental GC","or when using a custom runtime that requires stack space.","Defaults to 0 without and to 32768 with incremental GC."],default:0,type:"i"},enable:{category:"Features",description:["Enables WebAssembly features being disabled by default.",""," threads Threading and atomic operations."," simd SIMD types and operations."," reference-types Reference types and operations."," gc Garbage collection (WIP)."," stringref String reference types."," relaxed-simd Relaxed SIMD operations.",""],TODO_doesNothingYet:[" exception-handling Exception handling."," tail-calls Tail call operations."," multi-value Multi value types."," memory64 Memory64 operations."," extended-const Extended const expressions."],type:"S",mutuallyExclusive:"disable"},disable:{category:"Features",description:["Disables WebAssembly features being enabled by default.",""," mutable-globals Mutable global imports and exports."," sign-extension Sign-extension operations"," nontrapping-f2i Non-trapping float to integer ops."," bulk-memory Bulk memory operations.",""],type:"S",mutuallyExclusive:"enable"},use:{category:"Features",description:["Aliases a global object under another name, e.g., to switch","the default 'Math' implementation used: --use Math=JSMath","Can also be used to introduce an integer constant."],type:"S",alias:"u"},lowMemoryLimit:{category:"Features",description:"Enforces very low (<64k) memory constraints.",default:0,type:"i"},memoryBase:{category:"Linking",description:"Sets the start offset of emitted memory segments.",type:"i",default:0},tableBase:{category:"Linking",description:"Sets the start offset of emitted table elements.",type:"i",default:0},transform:{category:"API",description:"Specifies the path to a custom transform to load.",type:"S",isPath:!0,useNodeResolution:!0},trapMode:{category:"Binaryen",description:["Sets the trap mode to use.",""," allow Allow trapping operations. This is the default."," clamp Replace trapping operations with clamping semantics."," js Replace trapping operations with JS semantics.",""],type:"s",default:"allow"},runPasses:{category:"Binaryen",description:["Specifies additional Binaryen passes to run after other","optimizations, if any. See: Binaryen/src/passes/pass.cpp"],type:"s"},noValidate:{category:"Binaryen",description:"Skips validating the module using Binaryen.",type:"b",default:!1},baseDir:{description:"Specifies the base directory of input and output files.",type:"s",default:"."},noColors:{description:"Disables terminal colors.",type:"b",default:!1},noUnsafe:{description:["Disallows the use of unsafe features in user code.","Does not affect library files and external modules."],type:"b",default:!1},disableWarning:{description:["Disables warnings matching the given diagnostic code.","If no diagnostic code is given, all warnings are disabled."],type:"I"},noEmit:{description:"Performs compilation as usual but does not emit code.",type:"b",default:!1},showConfig:{description:"Print computed compiler options and exit.",type:"b",default:!1},stats:{description:"Prints statistics on I/O and compile times.",type:"b",default:!1},pedantic:{description:"Make yourself sad for no good reason.",type:"b",default:!1},lib:{description:["Adds one or multiple paths to custom library components and","uses exports of all top-level files at this path as globals."],type:"S",isPath:!0},path:{description:["Adds one or multiple paths to package resolution, similar","to node_modules. Prefers an 'ascMain' entry in a package's","package.json and falls back to an inner 'assembly/' folder."],type:"S",isPath:!0},wasm:{description:"Uses the specified Wasm binary of the compiler.",type:"s"}," ...":{description:"Specifies node.js options (CLI only). See: node --help"},"-Os":{value:{optimizeLevel:0,shrinkLevel:1}},"-Oz":{value:{optimizeLevel:0,shrinkLevel:2}},"-O0":{value:{optimizeLevel:0,shrinkLevel:0}},"-O1":{value:{optimizeLevel:1,shrinkLevel:0}},"-O2":{value:{optimizeLevel:2,shrinkLevel:0}},"-O3":{value:{optimizeLevel:3,shrinkLevel:0}},"-O0s":{value:{optimizeLevel:0,shrinkLevel:1}},"-O1s":{value:{optimizeLevel:1,shrinkLevel:1}},"-O2s":{value:{optimizeLevel:2,shrinkLevel:1}},"-O3s":{value:{optimizeLevel:3,shrinkLevel:1}},"-O0z":{value:{optimizeLevel:0,shrinkLevel:2}},"-O1z":{value:{optimizeLevel:1,shrinkLevel:2}},"-O2z":{value:{optimizeLevel:2,shrinkLevel:2}},"-O3z":{value:{optimizeLevel:3,shrinkLevel:2}},"-Ospeed":{value:{optimizeLevel:3,shrinkLevel:0}},"-Osize":{value:{optimizeLevel:0,shrinkLevel:2,converge:!0}},"--measure":{value:{stats:!0}}},Ee="~lib/",Ae={array:`/// <reference path="./rt/index.d.ts" />
|
|
9
9
|
|
|
10
10
|
import { BLOCK_MAXSIZE } from "./rt/common";
|
|
11
11
|
import { Runtime } from "shared/runtime";
|
|
@@ -759,6 +759,3481 @@ export namespace Atomics {
|
|
|
759
759
|
return size == 1 || size == 2 || size == 4;
|
|
760
760
|
}
|
|
761
761
|
}
|
|
762
|
+
`,bignum:`// Big integer types (u128 / i128 / u256), vendored verbatim from
|
|
763
|
+
// @btc-vision/as-bignum v1.0.0 (Apache-2.0) under ./bignum. See ./bignum/LICENSE.
|
|
764
|
+
//
|
|
765
|
+
// This file is a top-level standard-library entry, so toilscript globalizes its
|
|
766
|
+
// named re-exports the same way it does Array/Map/String. That makes u128, i128
|
|
767
|
+
// and u256 usable with no import, as native types:
|
|
768
|
+
//
|
|
769
|
+
// let a = u128.fromU64(40) + u128.fromU64(2);
|
|
770
|
+
// let b = u256.fromU128(a);
|
|
771
|
+
//
|
|
772
|
+
// The vendored sources are unmodified; only this thin globalizer is toilscript's.
|
|
773
|
+
// i256 is the signed-256 backing type of the family (u128/i128 convert to it),
|
|
774
|
+
// so it is globalized too for a self-consistent surface.
|
|
775
|
+
|
|
776
|
+
export { u128 } from "./bignum/integer/u128";
|
|
777
|
+
export { i128 } from "./bignum/integer/i128";
|
|
778
|
+
export { u256 } from "./bignum/integer/u256";
|
|
779
|
+
export { i256 } from "./bignum/integer/i256";
|
|
780
|
+
`,"bignum/fixed/fp128":`import { i128 } from '../integer/i128';
|
|
781
|
+
import { u128 } from '../integer/u128';
|
|
782
|
+
|
|
783
|
+
// Support only this fractions:
|
|
784
|
+
// fp128<u8> => 8 bits (~ 2 decimal digits)
|
|
785
|
+
// fp128<u16> => 16 bits (~ 4 decimal digits)
|
|
786
|
+
// fp128<u24> => 24 bits (~ 7 decimal digits)
|
|
787
|
+
// fp128<u32> => 32 bits (~ 9 decimal digits)
|
|
788
|
+
// fp128<u40> => 40 bits (~ 12 decimal digits)
|
|
789
|
+
// fp128<u48> => 48 bits (~ 14 decimal digits)
|
|
790
|
+
// fp128<u56> => 56 bits (~ 16 decimal digits)
|
|
791
|
+
// fp128<u64> => 64 bits (~ 19 decimal digits)
|
|
792
|
+
// fp128<u72> => 72 bits (~ 21 decimal digits)
|
|
793
|
+
// fp128<u80> => 80 bits (~ 24 decimal digits)
|
|
794
|
+
// fp128<u88> => 88 bits (~ 26 decimal digits)
|
|
795
|
+
// fp128<u96> => 96 bits (~ 28 decimal digits)
|
|
796
|
+
// fp128<u104> => 104 bits (~ 31 decimal digits)
|
|
797
|
+
// fp128<u112> => 112 bits (~ 33 decimal digits)
|
|
798
|
+
// fp128<u120> => 120 bits (~ 36 decimal digits)
|
|
799
|
+
|
|
800
|
+
export class fp128<Q> {
|
|
801
|
+
|
|
802
|
+
static readonly Zero: fp128<Q> = new fp128<Q>(0);
|
|
803
|
+
static readonly One: fp128<Q> = new fp128<Q>(1);
|
|
804
|
+
|
|
805
|
+
protected value: i128 = i128.Zero;
|
|
806
|
+
|
|
807
|
+
constructor(lo: u64 = 0, hi: i64 = 0) {
|
|
808
|
+
this.value.lo = lo;
|
|
809
|
+
this.value.hi = hi;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
@inline
|
|
813
|
+
get intBits(): i32 {
|
|
814
|
+
return 128 - this.fractBits;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
@inline
|
|
818
|
+
get fractBits(): i32 {
|
|
819
|
+
if (isReference<Q>()) {
|
|
820
|
+
return 8 * offsetof<Q>();
|
|
821
|
+
} else {
|
|
822
|
+
return 8 * sizeof<Q>();
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
@inline
|
|
827
|
+
get int(): i128 {
|
|
828
|
+
return this.value >>> this.fractBits;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
@inline
|
|
832
|
+
get fract(): u128 {
|
|
833
|
+
return (chagetype<u128>(this.value)) << this.intBits;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
// TODO
|
|
837
|
+
}
|
|
838
|
+
`,"bignum/fixed/fp256":`import { i256 } from '../integer/i256';
|
|
839
|
+
|
|
840
|
+
export class fp256<Q> {
|
|
841
|
+
|
|
842
|
+
constructor(public value: i256 = i256.Zero) {
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
// TODO
|
|
846
|
+
}
|
|
847
|
+
`,"bignum/fixed/index":`import { u128 } from "../integer/u128";
|
|
848
|
+
import { fp128 } from "./fp128";
|
|
849
|
+
import { fp256 } from "./fp256";
|
|
850
|
+
import { u104, u112, u120, u136, u144, u152, u160, u24, u40, u48, u56, u72, u80, u88, u96, } from './types';
|
|
851
|
+
|
|
852
|
+
export type fp128x8 = fp128<u8>;
|
|
853
|
+
export type fp128x16 = fp128<u16>;
|
|
854
|
+
export type fp128x24 = fp128<u24>;
|
|
855
|
+
export type fp128x32 = fp128<u32>;
|
|
856
|
+
export type fp128x40 = fp128<u40>;
|
|
857
|
+
export type fp128x48 = fp128<u48>;
|
|
858
|
+
export type fp128x56 = fp128<u56>;
|
|
859
|
+
export type fp128x64 = fp128<u64>;
|
|
860
|
+
export type fp128x72 = fp128<u72>;
|
|
861
|
+
export type fp128x80 = fp128<u80>;
|
|
862
|
+
|
|
863
|
+
export type fp256x8 = fp256<u8>;
|
|
864
|
+
export type fp256x16 = fp256<u16>;
|
|
865
|
+
export type fp256x24 = fp256<u24>;
|
|
866
|
+
export type fp256x32 = fp256<u32>;
|
|
867
|
+
export type fp256x40 = fp256<u40>;
|
|
868
|
+
export type fp256x48 = fp256<u48>;
|
|
869
|
+
export type fp256x56 = fp256<u56>;
|
|
870
|
+
export type fp256x64 = fp256<u64>;
|
|
871
|
+
export type fp256x72 = fp256<u72>;
|
|
872
|
+
export type fp256x80 = fp256<u80>;
|
|
873
|
+
export type fp256x88 = fp256<u88>;
|
|
874
|
+
export type fp256x96 = fp256<u96>;
|
|
875
|
+
export type fp256x104 = fp256<u104>;
|
|
876
|
+
export type fp256x112 = fp256<u112>;
|
|
877
|
+
export type fp256x120 = fp256<u120>;
|
|
878
|
+
export type fp256x128 = fp256<u128>;
|
|
879
|
+
export type fp256x136 = fp256<u136>;
|
|
880
|
+
export type fp256x144 = fp256<u144>;
|
|
881
|
+
export type fp256x152 = fp256<u152>;
|
|
882
|
+
export type fp256x160 = fp256<u160>;
|
|
883
|
+
|
|
884
|
+
export { fp128, fp256 };
|
|
885
|
+
`,"bignum/fixed/safe/fp128":`export namespace safe {
|
|
886
|
+
// TODO
|
|
887
|
+
}
|
|
888
|
+
`,"bignum/fixed/safe/fp256":`export namespace safe {
|
|
889
|
+
// TODO
|
|
890
|
+
}
|
|
891
|
+
`,"bignum/fixed/types":`// u8
|
|
892
|
+
// u16
|
|
893
|
+
|
|
894
|
+
export class u24 {
|
|
895
|
+
private _0: u16
|
|
896
|
+
private _1: u8
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// u32
|
|
900
|
+
|
|
901
|
+
export class u40 {
|
|
902
|
+
private _0: u32
|
|
903
|
+
private _1: u8
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
export class u48 {
|
|
907
|
+
private _0: u32
|
|
908
|
+
private _1: u16
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
export class u56 {
|
|
912
|
+
private _0: u32
|
|
913
|
+
private _1: u24
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
// u64
|
|
917
|
+
|
|
918
|
+
export class u72 {
|
|
919
|
+
private _0: u64
|
|
920
|
+
private _1: u8
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
export class u80 {
|
|
924
|
+
private _0: u64
|
|
925
|
+
private _1: u16
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
export class u88 {
|
|
929
|
+
private _0: u64
|
|
930
|
+
private _1: u24
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
export class u96 {
|
|
934
|
+
private _0: u64
|
|
935
|
+
private _1: u32
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
export class u104 {
|
|
939
|
+
private _0: u64
|
|
940
|
+
private _1: u40
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
export class u112 {
|
|
944
|
+
private _0: u64
|
|
945
|
+
private _1: u48
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
export class u120 {
|
|
949
|
+
private _0: u112
|
|
950
|
+
private _1: u8
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
// u128
|
|
954
|
+
|
|
955
|
+
export class u136 {
|
|
956
|
+
private _0: u120
|
|
957
|
+
private _1: u16
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
export class u144 {
|
|
961
|
+
private _0: u136
|
|
962
|
+
private _1: u8
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
export class u152 {
|
|
966
|
+
private _0: u144
|
|
967
|
+
private _1: u8
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
export class u160 {
|
|
971
|
+
private _0: u152
|
|
972
|
+
private _1: u8
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
export class u168 {
|
|
976
|
+
private _0: u160
|
|
977
|
+
private _1: u8
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
export class u176 {
|
|
981
|
+
private _0: u168
|
|
982
|
+
private _1: u8
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
export class u184 {
|
|
986
|
+
private _0: u176
|
|
987
|
+
private _1: u8
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
export class u192 {
|
|
991
|
+
private _0: u184
|
|
992
|
+
private _1: u8
|
|
993
|
+
}
|
|
994
|
+
`,"bignum/globals":`import { u128, u256 } from "./integer";
|
|
995
|
+
|
|
996
|
+
@lazy export var __divmod_quot_hi: u64 = 0;
|
|
997
|
+
@lazy export var __divmod_rem_lo: u64 = 0;
|
|
998
|
+
@lazy export var __divmod_rem_hi: u64 = 0;
|
|
999
|
+
|
|
1000
|
+
// used for returning low and high part of __mulq64, __multi3 etc
|
|
1001
|
+
@lazy export var __res128_hi: u64 = 0;
|
|
1002
|
+
|
|
1003
|
+
// used for returning 0 or 1
|
|
1004
|
+
@lazy export var __carry: u64 = 0;
|
|
1005
|
+
@lazy export var __u256carry: u64 = 0;
|
|
1006
|
+
@lazy export var __u256carrySub: u64 = 0;
|
|
1007
|
+
|
|
1008
|
+
/**
|
|
1009
|
+
* Convert 128-bit unsigned integer to 64-bit float
|
|
1010
|
+
* @param lo lower 64-bit part of unsigned 128-bit integer
|
|
1011
|
+
* @param hi higher 64-bit part of unsigned 128-bit integer
|
|
1012
|
+
* @return 64-bit float result
|
|
1013
|
+
*/
|
|
1014
|
+
// @ts-ignore: decorator
|
|
1015
|
+
@global
|
|
1016
|
+
export function __floatuntidf(lo: u64, hi: u64): f64 {
|
|
1017
|
+
// __floatuntidf ported from LLVM sources
|
|
1018
|
+
if (!(lo | hi)) return 0.0;
|
|
1019
|
+
|
|
1020
|
+
var v = new u128(lo, hi);
|
|
1021
|
+
var sd = 128 - __clz128(lo, hi);
|
|
1022
|
+
var e = sd - 1;
|
|
1023
|
+
|
|
1024
|
+
if (sd > 53) {
|
|
1025
|
+
if (sd != 55) {
|
|
1026
|
+
if (sd == 54) {
|
|
1027
|
+
v = u128.shl(v, 1);
|
|
1028
|
+
} else {
|
|
1029
|
+
v = u128.or(
|
|
1030
|
+
u128.shr(v, sd - 55),
|
|
1031
|
+
u128.fromBool(u128.and(v, u128.shr(u128.Max, 128 + 55 - sd)).toBool())
|
|
1032
|
+
);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
v.lo |= (v.lo & 4) >> 2;
|
|
1037
|
+
v.preInc();
|
|
1038
|
+
|
|
1039
|
+
v = u128.shr(v, 2);
|
|
1040
|
+
|
|
1041
|
+
if (v.lo & (1 << 53)) {
|
|
1042
|
+
v = u128.shr(v, 1);
|
|
1043
|
+
++e;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
} else {
|
|
1047
|
+
v = u128.shl(v, 53 - sd);
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
var w: u64 = u128.shr(v, 32).lo & 0x000FFFFF;
|
|
1051
|
+
var u: u64 = <u64>(((e + 1023) << 20) | w) << 32;
|
|
1052
|
+
return reinterpret<f64>(u | (v.lo & 0xFFFFFFFF));
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* Adds two 64-bit unsigned integers \`a\` and \`b\` along with a carry-in value.
|
|
1058
|
+
* Sets the global \`__carry2\` variable to indicate whether an overflow occurred.
|
|
1059
|
+
* @param {u64} a - The first 64-bit unsigned integer.
|
|
1060
|
+
* @param {u64} b - The second 64-bit unsigned integer.
|
|
1061
|
+
* @param {u64} carryIn - Carry-in value (0 or 1).
|
|
1062
|
+
* @returns {u64} - The 64-bit result of the addition.
|
|
1063
|
+
*/
|
|
1064
|
+
@inline
|
|
1065
|
+
export function add64Local(a: u64, b: u64, carryIn: u64): u64 {
|
|
1066
|
+
let tmp = a + carryIn;
|
|
1067
|
+
let carry = tmp < a ? 1 : 0;
|
|
1068
|
+
let sum = tmp + b;
|
|
1069
|
+
carry += sum < tmp ? 1 : 0;
|
|
1070
|
+
__u256carry = carry;
|
|
1071
|
+
return sum;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* Subtracts the 64-bit unsigned integer \`b\` from \`a\` along with a borrow-in value.
|
|
1076
|
+
* Sets the global \`__carry2\` variable to indicate whether a borrow occurred.
|
|
1077
|
+
* @param {u64} a - The minuend (64-bit unsigned integer).
|
|
1078
|
+
* @param {u64} b - The subtrahend (64-bit unsigned integer).
|
|
1079
|
+
* @param {u64} borrowIn - Borrow-in value (0 or 1).
|
|
1080
|
+
* @returns {u64} - The 64-bit result of the subtraction.
|
|
1081
|
+
*/
|
|
1082
|
+
@inline
|
|
1083
|
+
export function sub64(a: u64, b: u64, borrowIn: u64): u64 {
|
|
1084
|
+
let tmp = a - b;
|
|
1085
|
+
let borrow = tmp > a ? 1 : 0;
|
|
1086
|
+
let diff = tmp - borrowIn;
|
|
1087
|
+
borrow += diff > tmp ? 1 : 0;
|
|
1088
|
+
__u256carrySub = borrow;
|
|
1089
|
+
return diff;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
// @ts-ignore: decorator
|
|
1093
|
+
@global
|
|
1094
|
+
export function __umulh64(a: u64, b: u64): u64 {
|
|
1095
|
+
var u = a & 0xFFFFFFFF;
|
|
1096
|
+
a >>= 32;
|
|
1097
|
+
var v = b & 0xFFFFFFFF;
|
|
1098
|
+
b >>= 32;
|
|
1099
|
+
|
|
1100
|
+
var uv = u * v;
|
|
1101
|
+
uv = a * v + (uv >> 32);
|
|
1102
|
+
var w0 = (u * b) + (uv & 0xFFFFFFFF);
|
|
1103
|
+
return a * b + (uv >> 32) + (w0 >> 32);
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
// @ts-ignore: decorator
|
|
1107
|
+
@global
|
|
1108
|
+
export function __umulq64(a: u64, b: u64): u64 {
|
|
1109
|
+
var u = a & 0xFFFFFFFF;
|
|
1110
|
+
a >>= 32;
|
|
1111
|
+
var v = b & 0xFFFFFFFF;
|
|
1112
|
+
b >>= 32;
|
|
1113
|
+
|
|
1114
|
+
var uv = u * v;
|
|
1115
|
+
var w0 = uv & 0xFFFFFFFF;
|
|
1116
|
+
uv = a * v + (uv >>> 32);
|
|
1117
|
+
var w1 = uv >>> 32;
|
|
1118
|
+
uv = u * b + (uv & 0xFFFFFFFF);
|
|
1119
|
+
|
|
1120
|
+
__res128_hi = a * b + w1 + (uv >>> 32);
|
|
1121
|
+
return (uv << 32) | w0;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
// __umul64Hop computes (hi * 2^64 + lo) = z + (x * y)
|
|
1125
|
+
// @ts-ignore: decorator
|
|
1126
|
+
@inline
|
|
1127
|
+
export function __umul64Hop(z: u64, x: u64, y: u64): u64 {
|
|
1128
|
+
var lo = __umulq64(x, y);
|
|
1129
|
+
lo = __uadd64(lo, z);
|
|
1130
|
+
var hi = __res128_hi + __carry;
|
|
1131
|
+
__res128_hi = hi;
|
|
1132
|
+
return lo
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
// __umul64Step computes (hi * 2^64 + lo) = z + (x * y) + carry.
|
|
1136
|
+
// @ts-ignore: decorator
|
|
1137
|
+
@inline
|
|
1138
|
+
export function __umul64Step(z: u64, x: u64, y: u64, carry: u64): u64 {
|
|
1139
|
+
var lo = __umulq64(x, y)
|
|
1140
|
+
lo = __uadd64(lo, carry);
|
|
1141
|
+
var hi = __uadd64(__res128_hi, 0, __carry);
|
|
1142
|
+
lo = __uadd64(lo, z);
|
|
1143
|
+
hi += __carry;
|
|
1144
|
+
__res128_hi = hi;
|
|
1145
|
+
return lo
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
// __uadd64 returns the sum with carry of x, y and carry: sum = x + y + carry.
|
|
1149
|
+
// The carry input must be 0 or 1; otherwise the behavior is undefined.
|
|
1150
|
+
// The carryOut output is guaranteed to be 0 or 1.
|
|
1151
|
+
// @ts-ignore: decorator
|
|
1152
|
+
@inline
|
|
1153
|
+
export function __uadd64(x: u64, y: u64, carry: u64 = 0): u64 {
|
|
1154
|
+
var sum = x + y + carry
|
|
1155
|
+
// // The sum will overflow if both top bits are set (x & y) or if one of them
|
|
1156
|
+
// // is (x | y), and a carry from the lower place happened. If such a carry
|
|
1157
|
+
// // happens, the top bit will be 1 + 0 + 1 = 0 (& ~sum).
|
|
1158
|
+
__carry = ((x & y) | ((x | y) & ~sum)) >>> 63
|
|
1159
|
+
return sum;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
// 64x64 => 128 bits
|
|
1163
|
+
@inline
|
|
1164
|
+
function mul64To128(x: u64, y: u64): u128 {
|
|
1165
|
+
let lo = __umulq64(x, y); // sets __res128_hi
|
|
1166
|
+
let hi = __res128_hi;
|
|
1167
|
+
return new u128(lo, hi);
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
/**
|
|
1171
|
+
* A correct 256\xD7256 -> 256 multiply (mod 2^256).
|
|
1172
|
+
* We do standard "schoolbook" multiplication for each 64-bit limb,
|
|
1173
|
+
* skipping products that shift above 255 bits (they vanish mod 2^256).
|
|
1174
|
+
*/
|
|
1175
|
+
@global
|
|
1176
|
+
export function __mul256(
|
|
1177
|
+
ax0: u64, ax1: u64, ax2: u64, ax3: u64,
|
|
1178
|
+
bx0: u64, bx1: u64, bx2: u64, bx3: u64
|
|
1179
|
+
): u256 {
|
|
1180
|
+
let result = new u256();
|
|
1181
|
+
let a = [ax0, ax1, ax2, ax3];
|
|
1182
|
+
let b = [bx0, bx1, bx2, bx3];
|
|
1183
|
+
|
|
1184
|
+
// For each (i,j), partial = a[i]*b[j], shift = 64*(i+j).
|
|
1185
|
+
// If shift >= 256, that partial is 0 mod 2^256.
|
|
1186
|
+
// Otherwise shift partial, add to result.
|
|
1187
|
+
for (let i = 0; i < 4; i++) {
|
|
1188
|
+
for (let j = 0; j < 4; j++) {
|
|
1189
|
+
let shift = (i + j) << 6; // 64*(i+j)
|
|
1190
|
+
if (shift >= 256) continue;
|
|
1191
|
+
let p128 = mul64To128(a[i], b[j]); // 128-bit partial product
|
|
1192
|
+
// convert to u256
|
|
1193
|
+
let part = new u256(p128.lo, p128.hi, 0, 0);
|
|
1194
|
+
// shift left by 'shift' bits
|
|
1195
|
+
if (shift != 0) {
|
|
1196
|
+
part = u256.shl(part, shift);
|
|
1197
|
+
}
|
|
1198
|
+
// add to accumulator
|
|
1199
|
+
result = result + part;
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
return result;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
// @ts-ignore: decorator
|
|
1207
|
+
@global
|
|
1208
|
+
export function __multi3(al: u64, ah: u64, bl: u64, bh: u64): u64 {
|
|
1209
|
+
var u = al, v = bl;
|
|
1210
|
+
var w: u64, k: u64;
|
|
1211
|
+
|
|
1212
|
+
var u1 = u & 0xFFFFFFFF;
|
|
1213
|
+
u >>= 32;
|
|
1214
|
+
var v1 = v & 0xFFFFFFFF;
|
|
1215
|
+
v >>= 32;
|
|
1216
|
+
var t = u1 * v1;
|
|
1217
|
+
var w1 = t & 0xFFFFFFFF;
|
|
1218
|
+
|
|
1219
|
+
t = u * v1 + (t >> 32);
|
|
1220
|
+
k = t & 0xFFFFFFFF;
|
|
1221
|
+
w = t >> 32;
|
|
1222
|
+
t = u1 * v + k;
|
|
1223
|
+
|
|
1224
|
+
var lo = (t << 32) | w1;
|
|
1225
|
+
var hi = u * v + w;
|
|
1226
|
+
hi += ah * bl;
|
|
1227
|
+
hi += al * bh;
|
|
1228
|
+
hi += t >> 32;
|
|
1229
|
+
|
|
1230
|
+
__res128_hi = hi;
|
|
1231
|
+
return lo;
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
// @ts-ignore: decorator
|
|
1235
|
+
@global
|
|
1236
|
+
export function __floatuntfdi(value: f64): u64 {
|
|
1237
|
+
var u = reinterpret<u64>(value);
|
|
1238
|
+
|
|
1239
|
+
// if (value < -1.7014118346046e38) { // -(2^127-1)
|
|
1240
|
+
if (value < reinterpret<f64>(0xC7F0000000000000)) { // -(2^128-1)
|
|
1241
|
+
// __float_u128_hi = <u64>-1; // for i128
|
|
1242
|
+
__res128_hi = 0;
|
|
1243
|
+
// overflow negative
|
|
1244
|
+
return 0;
|
|
1245
|
+
// } else if (value < -9.2233720368547e18) { // -2^63-1 // for i128
|
|
1246
|
+
} else if (value < reinterpret<f64>(0xC3F0000000000000)) { // // -(2^64-1)
|
|
1247
|
+
let lo: u64, hi: u64, m: u64;
|
|
1248
|
+
|
|
1249
|
+
m = (u & 0x000FFFFFFFFFFFFF) | (1 << 52);
|
|
1250
|
+
u = (u & 0x7FFFFFFFFFFFFFFF) >> 52;
|
|
1251
|
+
|
|
1252
|
+
u -= 1075;
|
|
1253
|
+
if (u > 64) {
|
|
1254
|
+
lo = 0;
|
|
1255
|
+
hi = m << (u - 64);
|
|
1256
|
+
} else {
|
|
1257
|
+
lo = m << u;
|
|
1258
|
+
hi = m >> (64 - u);
|
|
1259
|
+
}
|
|
1260
|
+
// Convert to 2's complement for correct negative representation
|
|
1261
|
+
lo = ~lo;
|
|
1262
|
+
hi = ~hi;
|
|
1263
|
+
lo += 1;
|
|
1264
|
+
if (lo == 0) hi += 1; // carry to high part
|
|
1265
|
+
|
|
1266
|
+
__res128_hi = hi;
|
|
1267
|
+
return lo;
|
|
1268
|
+
// } else if (value < 9.2233720368547e18) { // 2^63-1 // for i128
|
|
1269
|
+
} else if (value < reinterpret<f64>(0x43F0000000000000)) { // 2^64-1
|
|
1270
|
+
// __float_u128_hi = (value < 0) ? -1 : 0; // for int
|
|
1271
|
+
__res128_hi = 0;
|
|
1272
|
+
// fit in a u64
|
|
1273
|
+
return <u64>value;
|
|
1274
|
+
// } else if (value < 1.7014118346046e38) {
|
|
1275
|
+
} else if (value < reinterpret<f64>(0x47F0000000000000)) { // 2^128-1
|
|
1276
|
+
let lo: u64, hi: u64, m: u64;
|
|
1277
|
+
|
|
1278
|
+
m = (u & 0x000FFFFFFFFFFFFF) | (1 << 52);
|
|
1279
|
+
u = (u & 0x7FFFFFFFFFFFFFFF) >> 52;
|
|
1280
|
+
u -= 1075;
|
|
1281
|
+
if (u > 64) {
|
|
1282
|
+
lo = 0;
|
|
1283
|
+
hi = m << (u - 64);
|
|
1284
|
+
} else {
|
|
1285
|
+
lo = m << u;
|
|
1286
|
+
hi = m >> (64 - u);
|
|
1287
|
+
}
|
|
1288
|
+
__res128_hi = hi;
|
|
1289
|
+
return lo;
|
|
1290
|
+
} else {
|
|
1291
|
+
// overflow positive
|
|
1292
|
+
__res128_hi = <u64>-1; // 0x7FFFFFFFFFFFFFFF for i128
|
|
1293
|
+
return <u64>-1;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
/**
|
|
1298
|
+
* Count leading zeros in a 64-bit unsigned integer \`x\`, returning i32 in [0..64].
|
|
1299
|
+
* If x == 0, returns 64.
|
|
1300
|
+
*/
|
|
1301
|
+
function clz64(x: u64): i32 {
|
|
1302
|
+
if (x == 0) return 64;
|
|
1303
|
+
|
|
1304
|
+
let n: i32 = 0;
|
|
1305
|
+
// Check high half [ bits 63..32 ]
|
|
1306
|
+
if ((x & 0xFFFFFFFF00000000) == 0) {
|
|
1307
|
+
n += 32;
|
|
1308
|
+
x <<= 32; // shift left so next checks are for the upper bits
|
|
1309
|
+
}
|
|
1310
|
+
// Check bits [63..48]
|
|
1311
|
+
if ((x & 0xFFFF000000000000) == 0) {
|
|
1312
|
+
n += 16;
|
|
1313
|
+
x <<= 16;
|
|
1314
|
+
}
|
|
1315
|
+
// Check bits [63..56]
|
|
1316
|
+
if ((x & 0xFF00000000000000) == 0) {
|
|
1317
|
+
n += 8;
|
|
1318
|
+
x <<= 8;
|
|
1319
|
+
}
|
|
1320
|
+
// Check bits [63..60]
|
|
1321
|
+
if ((x & 0xF000000000000000) == 0) {
|
|
1322
|
+
n += 4;
|
|
1323
|
+
x <<= 4;
|
|
1324
|
+
}
|
|
1325
|
+
// Check bits [63..62]
|
|
1326
|
+
if ((x & 0xC000000000000000) == 0) {
|
|
1327
|
+
n += 2;
|
|
1328
|
+
x <<= 2;
|
|
1329
|
+
}
|
|
1330
|
+
// Check bit [63]
|
|
1331
|
+
if ((x & 0x8000000000000000) == 0) {
|
|
1332
|
+
n += 1;
|
|
1333
|
+
}
|
|
1334
|
+
return n;
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
/**
|
|
1338
|
+
* Count trailing zeros in a 64-bit unsigned integer \`x\`, returning i32 in [0..64].
|
|
1339
|
+
* If x == 0, returns 64.
|
|
1340
|
+
*/
|
|
1341
|
+
function ctz64(x: u64): i32 {
|
|
1342
|
+
if (x == 0) return 64;
|
|
1343
|
+
|
|
1344
|
+
let n: i32 = 0;
|
|
1345
|
+
// Check low half [bits 31..0]
|
|
1346
|
+
if ((x & 0xFFFFFFFF) == 0) {
|
|
1347
|
+
n += 32;
|
|
1348
|
+
x >>= 32;
|
|
1349
|
+
}
|
|
1350
|
+
// Check bits [15..0]
|
|
1351
|
+
if ((x & 0xFFFF) == 0) {
|
|
1352
|
+
n += 16;
|
|
1353
|
+
x >>= 16;
|
|
1354
|
+
}
|
|
1355
|
+
// Check bits [7..0]
|
|
1356
|
+
if ((x & 0xFF) == 0) {
|
|
1357
|
+
n += 8;
|
|
1358
|
+
x >>= 8;
|
|
1359
|
+
}
|
|
1360
|
+
// Check bits [3..0]
|
|
1361
|
+
if ((x & 0xF) == 0) {
|
|
1362
|
+
n += 4;
|
|
1363
|
+
x >>= 4;
|
|
1364
|
+
}
|
|
1365
|
+
// Check bits [1..0]
|
|
1366
|
+
if ((x & 0x3) == 0) {
|
|
1367
|
+
n += 2;
|
|
1368
|
+
x >>= 2;
|
|
1369
|
+
}
|
|
1370
|
+
// Check bit [0]
|
|
1371
|
+
if ((x & 0x1) == 0) {
|
|
1372
|
+
n += 1;
|
|
1373
|
+
}
|
|
1374
|
+
return n;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
/**
|
|
1378
|
+
* Count leading zeros in a 128-bit integer [hi:lo], returning i32 in [0..128].
|
|
1379
|
+
* If both hi and lo are 0, returns 128.
|
|
1380
|
+
*
|
|
1381
|
+
* hi is signed in i128, but we interpret it as unsigned here.
|
|
1382
|
+
*/
|
|
1383
|
+
// @ts-ignore: decorator
|
|
1384
|
+
@global @inline
|
|
1385
|
+
export function __clz128(lo: u64, hi: i64): i32 {
|
|
1386
|
+
let h: u64 = <u64>hi; // reinterpret hi as unsigned
|
|
1387
|
+
if (h == 0) {
|
|
1388
|
+
// If hi is 0, the leading zeros are "64 plus however many are in lo"
|
|
1389
|
+
return 64 + <i32>i64.clz(lo);
|
|
1390
|
+
} else {
|
|
1391
|
+
// The top 64 bits are set => just measure their leading zeros
|
|
1392
|
+
return <i32>i64.clz(h);
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
/**
|
|
1397
|
+
* Count trailing zeros in a 128-bit integer [hi:lo], returning i32 in [0..128].
|
|
1398
|
+
* If both hi and lo are 0, returns 128.
|
|
1399
|
+
*
|
|
1400
|
+
* For i128 we typically treat hi as signed, but ctz is purely bitwise, so we
|
|
1401
|
+
* can pass it as u64 as well.
|
|
1402
|
+
*/
|
|
1403
|
+
// @ts-ignore: decorator
|
|
1404
|
+
@global @inline
|
|
1405
|
+
export function __ctz128(lo: u64, hi: u64): i32 {
|
|
1406
|
+
if (lo == 0) {
|
|
1407
|
+
// Otherwise, ctz is 64 plus ctz(hi)
|
|
1408
|
+
return 64 + <i32>i64.ctz(hi);
|
|
1409
|
+
} else {
|
|
1410
|
+
// If the lower 64 bits are non-zero, measure ctz(lo)
|
|
1411
|
+
return <i32>i64.ctz(lo);
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
// @ts-ignore: decorator
|
|
1416
|
+
@global
|
|
1417
|
+
export function __udivmod128(alo: u64, ahi: u64, blo: u64, bhi: u64): u64 {
|
|
1418
|
+
var bzn = __clz128(blo, bhi); // N
|
|
1419
|
+
|
|
1420
|
+
// b == 0
|
|
1421
|
+
if (bzn == 128) {
|
|
1422
|
+
throw new RangeError("Division by zero"); // division by zero
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
// var azn = __clz128(alo, ahi); // M
|
|
1426
|
+
var btz = __ctz128(blo, bhi); // N
|
|
1427
|
+
|
|
1428
|
+
// a == 0
|
|
1429
|
+
if (!(alo | ahi)) {
|
|
1430
|
+
__divmod_quot_hi = 0;
|
|
1431
|
+
__divmod_rem_lo = 0;
|
|
1432
|
+
__divmod_rem_hi = 0;
|
|
1433
|
+
return 0;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
// a / 1
|
|
1437
|
+
if (bzn == 127) {
|
|
1438
|
+
__divmod_quot_hi = ahi;
|
|
1439
|
+
__divmod_rem_lo = 0;
|
|
1440
|
+
__divmod_rem_hi = 0;
|
|
1441
|
+
return alo;
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
// a == b
|
|
1445
|
+
if (alo == blo && ahi == bhi) {
|
|
1446
|
+
__divmod_quot_hi = 0;
|
|
1447
|
+
__divmod_rem_lo = 0;
|
|
1448
|
+
__divmod_rem_hi = 0;
|
|
1449
|
+
return 1;
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
if (!(ahi | bhi)) {
|
|
1453
|
+
__divmod_quot_hi = 0;
|
|
1454
|
+
__divmod_rem_hi = 0;
|
|
1455
|
+
// if \`b.lo\` is power of two
|
|
1456
|
+
if (!(blo & (blo - 1))) {
|
|
1457
|
+
__divmod_rem_lo = alo & (blo - 1);
|
|
1458
|
+
return alo >> btz;
|
|
1459
|
+
} else {
|
|
1460
|
+
let dlo = alo / blo;
|
|
1461
|
+
__divmod_rem_lo = alo - dlo * blo;
|
|
1462
|
+
return dlo;
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
return __udivmod128core(alo, ahi, blo, bhi);
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
function __udivmod128core(alo: u64, ahi: u64, blo: u64, bhi: u64): u64 {
|
|
1469
|
+
var r = new u128(alo, ahi);
|
|
1470
|
+
var d = new u128(blo, bhi);
|
|
1471
|
+
var q = new u128(0, 0);
|
|
1472
|
+
|
|
1473
|
+
// Standard division by shifting
|
|
1474
|
+
var leadingZeros = __clz128(blo, bhi) - __clz128(alo, ahi);
|
|
1475
|
+
|
|
1476
|
+
if (leadingZeros < 0) {
|
|
1477
|
+
__divmod_quot_hi = 0;
|
|
1478
|
+
__divmod_rem_lo = alo;
|
|
1479
|
+
__divmod_rem_hi = ahi;
|
|
1480
|
+
return 0;
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
var dShift = u128.shl(d, leadingZeros);
|
|
1484
|
+
|
|
1485
|
+
for (let i = 0; i <= leadingZeros; i++) {
|
|
1486
|
+
q = u128.shl(q, 1);
|
|
1487
|
+
if (r >= dShift) {
|
|
1488
|
+
r -= dShift;
|
|
1489
|
+
q = u128.add(q, u128.One);
|
|
1490
|
+
}
|
|
1491
|
+
dShift = u128.shr(dShift, 1);
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
__divmod_quot_hi = q.hi;
|
|
1495
|
+
__divmod_rem_lo = r.lo;
|
|
1496
|
+
__divmod_rem_hi = r.hi;
|
|
1497
|
+
return q.lo;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
// @ts-ignore: decorator
|
|
1501
|
+
@global
|
|
1502
|
+
export function __udivmod128_10(lo: u64, hi: u64): u64 {
|
|
1503
|
+
if (hi == 0) {
|
|
1504
|
+
__divmod_quot_hi = 0;
|
|
1505
|
+
let q = lo / 10;
|
|
1506
|
+
__divmod_rem_lo = lo - (q * 10);
|
|
1507
|
+
__divmod_rem_hi = 0;
|
|
1508
|
+
return q;
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
// High part division
|
|
1512
|
+
let q_hi = hi / 10;
|
|
1513
|
+
let r_hi = hi - (q_hi * 10);
|
|
1514
|
+
|
|
1515
|
+
// Low part chunking to avoid 128-bit math
|
|
1516
|
+
// r_hi becomes the high bits for the next division
|
|
1517
|
+
let lo_hi = lo >>> 32;
|
|
1518
|
+
let lo_lo = lo & 0xFFFFFFFF;
|
|
1519
|
+
|
|
1520
|
+
// Divide upper half of lo (plus carry from hi)
|
|
1521
|
+
let t1 = (r_hi << 32) | lo_hi;
|
|
1522
|
+
let q1 = t1 / 10;
|
|
1523
|
+
let r1 = t1 - (q1 * 10);
|
|
1524
|
+
|
|
1525
|
+
// Divide lower half of lo (plus carry from above)
|
|
1526
|
+
let t2 = (r1 << 32) | lo_lo;
|
|
1527
|
+
let q2 = t2 / 10;
|
|
1528
|
+
let r2 = t2 - (q2 * 10);
|
|
1529
|
+
|
|
1530
|
+
// Reassemble quotient
|
|
1531
|
+
// q1 is known to fit in 32 bits because max t1 < 10 * 2^32
|
|
1532
|
+
let q_lo = (q1 << 32) | q2;
|
|
1533
|
+
|
|
1534
|
+
__divmod_quot_hi = q_hi;
|
|
1535
|
+
__divmod_rem_lo = r2;
|
|
1536
|
+
__divmod_rem_hi = 0;
|
|
1537
|
+
|
|
1538
|
+
return q_lo;
|
|
1539
|
+
}
|
|
1540
|
+
`,"bignum/index":`export * from "./integer";
|
|
1541
|
+
`,"bignum/integer/i128":`import { u128 } from './u128';
|
|
1542
|
+
import { i256 } from './i256';
|
|
1543
|
+
import { u256 } from './u256';
|
|
1544
|
+
|
|
1545
|
+
import { __clz128, __ctz128, } from '../globals';
|
|
1546
|
+
|
|
1547
|
+
import { atou128 } from '../utils';
|
|
1548
|
+
|
|
1549
|
+
/**
|
|
1550
|
+
* 128-bit signed integer in two's complement representation.
|
|
1551
|
+
* The \`hi\` field is signed, storing sign bits for negative values.
|
|
1552
|
+
*/
|
|
1553
|
+
export class i128 {
|
|
1554
|
+
|
|
1555
|
+
constructor(public lo: u64 = 0, public hi: i64 = 0) {
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
@inline static get Zero(): i128 {
|
|
1559
|
+
return new i128();
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
@inline static get One(): i128 {
|
|
1563
|
+
return new i128(1, 0);
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
@inline static get Min(): i128 {
|
|
1567
|
+
return new i128(0, 0x8000000000000000);
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
@inline static get Max(): i128 {
|
|
1571
|
+
return new i128(u64.MAX_VALUE, 0x7FFFFFFFFFFFFFFF);
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
// Construct from decimal or hex string
|
|
1575
|
+
@inline
|
|
1576
|
+
static fromString(value: string, radix: i32 = 10): i128 {
|
|
1577
|
+
// If the string is prefixed with "-" we rely on atou128 + two\u2019s complement wrap
|
|
1578
|
+
// in the final reinterpret.
|
|
1579
|
+
return changetype<i128>(atou128(value, radix));
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
@inline
|
|
1583
|
+
static fromI256(value: i256): i128 {
|
|
1584
|
+
// Truncates top bits (value.hi1, value.hi2)
|
|
1585
|
+
return new i128(value.lo1, value.lo2);
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
@inline
|
|
1589
|
+
static fromU256(value: u256): i128 {
|
|
1590
|
+
// Also truncation
|
|
1591
|
+
return new i128(value.lo1, <i64>value.lo2);
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
@inline
|
|
1595
|
+
static fromI128(value: i128): i128 {
|
|
1596
|
+
return new i128(value.lo, value.hi);
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
@inline
|
|
1600
|
+
static fromU128(value: u128): i128 {
|
|
1601
|
+
return new i128(value.lo, <i64>value.hi);
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
@inline
|
|
1605
|
+
static fromI64(value: i64): i128 {
|
|
1606
|
+
return new i128(<u64>value, value >> 63);
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
@inline
|
|
1610
|
+
static fromU64(value: u64): i128 {
|
|
1611
|
+
return new i128(value, 0);
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
@inline
|
|
1615
|
+
static fromF64(value: f64): i128 {
|
|
1616
|
+
let i = <i64>value; // convert f64 -> i64 (round/truncate)
|
|
1617
|
+
return new i128(<u64>i, i >> 63);
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
@inline
|
|
1621
|
+
static fromF32(value: f32): i128 {
|
|
1622
|
+
let i = <i64>value; // or <i32> then sign-extend
|
|
1623
|
+
return new i128(<u64>i, i >> 63);
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
@inline
|
|
1627
|
+
static fromI32(value: i32): i128 {
|
|
1628
|
+
return new i128(<u64>value, <i64>(value >> 31));
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
@inline
|
|
1632
|
+
static fromU32(value: u32): i128 {
|
|
1633
|
+
return new i128(<u64>value, 0);
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
@inline
|
|
1637
|
+
static fromBits(lo1: i32, lo2: i32, hi1: i32, hi2: i32): i128 {
|
|
1638
|
+
let lo = ((<u64>lo2) << 32) | (<u64>(lo1) & 0xffffffff);
|
|
1639
|
+
let hi = ((<i64>hi2) << 32) | (<i64>(hi1) & 0xffffffff);
|
|
1640
|
+
return new i128(lo, hi);
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
@inline
|
|
1644
|
+
static fromBytes<T>(array: T, bigEndian: bool = false): i128 {
|
|
1645
|
+
if (array instanceof u8[]) {
|
|
1646
|
+
return bigEndian
|
|
1647
|
+
// @ts-ignore
|
|
1648
|
+
? i128.fromBytesBE(<u8[]>array)
|
|
1649
|
+
: i128.fromBytesLE(<u8[]>array);
|
|
1650
|
+
} else if (array instanceof Uint8Array) {
|
|
1651
|
+
return bigEndian
|
|
1652
|
+
? i128.fromUint8ArrayBE(<Uint8Array>array)
|
|
1653
|
+
: i128.fromUint8ArrayLE(<Uint8Array>array);
|
|
1654
|
+
} else {
|
|
1655
|
+
throw new TypeError("Unsupported generic type");
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
@inline
|
|
1660
|
+
static fromBytesLE(array: u8[]): i128 {
|
|
1661
|
+
return i128.fromUint8ArrayLE(changetype<Uint8Array>(array));
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
@inline
|
|
1665
|
+
static fromBytesBE(array: u8[]): i128 {
|
|
1666
|
+
return i128.fromUint8ArrayBE(changetype<Uint8Array>(array));
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
@inline
|
|
1670
|
+
static fromUint8ArrayLE(array: Uint8Array): i128 {
|
|
1671
|
+
assert(array.length && (array.length & 15) == 0);
|
|
1672
|
+
let buffer = array.dataStart;
|
|
1673
|
+
return new i128(
|
|
1674
|
+
load<u64>(buffer, 0 * sizeof<u64>()),
|
|
1675
|
+
load<u64>(buffer, 1 * sizeof<u64>())
|
|
1676
|
+
);
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
static fromUint8ArrayBE(array: Uint8Array): i128 {
|
|
1680
|
+
assert(array.length && (array.length & 15) == 0);
|
|
1681
|
+
let buffer = array.dataStart;
|
|
1682
|
+
return new i128(
|
|
1683
|
+
bswap<u64>(load<u64>(buffer, 1 * sizeof<u64>())),
|
|
1684
|
+
bswap<u64>(load<u64>(buffer, 0 * sizeof<u64>()))
|
|
1685
|
+
);
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
/**
|
|
1689
|
+
* Create 128-bit signed integer from a generic type T
|
|
1690
|
+
* @param value
|
|
1691
|
+
* @return 128-bit signed integer
|
|
1692
|
+
*/
|
|
1693
|
+
@inline
|
|
1694
|
+
static from<T>(value: T): i128 {
|
|
1695
|
+
if (value instanceof bool) return i128.fromU64(<u64>value);
|
|
1696
|
+
else if (value instanceof i8) return i128.fromI64(<i64>value);
|
|
1697
|
+
else if (value instanceof u8) return i128.fromU64(<u64>value);
|
|
1698
|
+
else if (value instanceof i16) return i128.fromI64(<i64>value);
|
|
1699
|
+
else if (value instanceof u16) return i128.fromU64(<u64>value);
|
|
1700
|
+
else if (value instanceof i32) return i128.fromI64(<i64>value);
|
|
1701
|
+
else if (value instanceof u32) return i128.fromU64(<u64>value);
|
|
1702
|
+
else if (value instanceof i64) return i128.fromI64(<i64>value);
|
|
1703
|
+
else if (value instanceof u64) return i128.fromU64(<u64>value);
|
|
1704
|
+
else if (value instanceof f32) return i128.fromF64(<f64>value);
|
|
1705
|
+
else if (value instanceof f64) return i128.fromF64(<f64>value);
|
|
1706
|
+
else if (value instanceof i128) return i128.fromI128(<i128>value);
|
|
1707
|
+
else if (value instanceof u128) return i128.fromU128(<u128>value);
|
|
1708
|
+
else if (value instanceof i256) return i128.fromI256(<i256>value);
|
|
1709
|
+
else if (value instanceof u256) return i128.fromU256(<u256>value);
|
|
1710
|
+
else if (value instanceof u8[]) return i128.fromBytes(<u8[]>value);
|
|
1711
|
+
else throw new TypeError("Unsupported generic type");
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
@inline @operator.prefix('!')
|
|
1715
|
+
static isEmpty(value: i128): bool {
|
|
1716
|
+
return value.isZero();
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
@inline @operator('|')
|
|
1720
|
+
static or(a: i128, b: i128): i128 {
|
|
1721
|
+
return new i128(a.lo | b.lo, a.hi | b.hi);
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
@inline @operator('^')
|
|
1725
|
+
static xor(a: i128, b: i128): i128 {
|
|
1726
|
+
return new i128(a.lo ^ b.lo, a.hi ^ b.hi);
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
@inline @operator('&')
|
|
1730
|
+
static and(a: i128, b: i128): i128 {
|
|
1731
|
+
return new i128(a.lo & b.lo, a.hi & b.hi);
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
@inline @operator('<<')
|
|
1735
|
+
static shl(value: i128, shift: i32): i128 {
|
|
1736
|
+
shift &= 127;
|
|
1737
|
+
0xFFFFFFFFFFFFFFFF
|
|
1738
|
+
if (!shift) return value;
|
|
1739
|
+
|
|
1740
|
+
let lo = value.lo;
|
|
1741
|
+
let hi = value.hi;
|
|
1742
|
+
if (shift < 64) {
|
|
1743
|
+
let newLo = lo << shift;
|
|
1744
|
+
let newHi = (hi << shift) | i64(lo >>> (64 - shift));
|
|
1745
|
+
return new i128(newLo, newHi);
|
|
1746
|
+
} else {
|
|
1747
|
+
// shift >= 64
|
|
1748
|
+
let s = shift - 64;
|
|
1749
|
+
if (s == 0) {
|
|
1750
|
+
// exactly 64
|
|
1751
|
+
return new i128(0, lo as i64);
|
|
1752
|
+
} else {
|
|
1753
|
+
// 1..63
|
|
1754
|
+
return new i128(0, (lo << s) as i64);
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
@inline @operator('>>>')
|
|
1760
|
+
static shr_u(value: i128, shift: i32): i128 {
|
|
1761
|
+
// an unsigned right shift of a *signed* 128
|
|
1762
|
+
shift &= 127;
|
|
1763
|
+
if (!shift) return value;
|
|
1764
|
+
|
|
1765
|
+
let lo = value.lo;
|
|
1766
|
+
let hi = u64(value.hi); // reinterpret sign as high bits
|
|
1767
|
+
|
|
1768
|
+
if (shift < 64) {
|
|
1769
|
+
let newLo = (lo >>> shift) | (hi << (64 - shift));
|
|
1770
|
+
let newHi = hi >>> shift;
|
|
1771
|
+
return new i128(newLo, newHi as i64);
|
|
1772
|
+
} else {
|
|
1773
|
+
// shift >= 64
|
|
1774
|
+
let s = shift - 64;
|
|
1775
|
+
if (s == 0) {
|
|
1776
|
+
return new i128(hi, 0);
|
|
1777
|
+
} else {
|
|
1778
|
+
// 1..63
|
|
1779
|
+
return new i128(hi >>> s, 0);
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1784
|
+
@inline @operator('+')
|
|
1785
|
+
static add(a: i128, b: i128): i128 {
|
|
1786
|
+
let lo = a.lo + b.lo;
|
|
1787
|
+
let carry = (lo < a.lo) ? 1 : 0;
|
|
1788
|
+
let hi = a.hi + b.hi + (carry as i64);
|
|
1789
|
+
return new i128(lo, hi);
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
@inline @operator('-')
|
|
1793
|
+
static sub(a: i128, b: i128): i128 {
|
|
1794
|
+
let lo = a.lo - b.lo;
|
|
1795
|
+
let borrow = (lo > a.lo) ? 1 : 0;
|
|
1796
|
+
let hi = a.hi - b.hi - (borrow as i64);
|
|
1797
|
+
return new i128(lo, hi);
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
@inline @operator('==')
|
|
1801
|
+
static eq(a: i128, b: i128): bool {
|
|
1802
|
+
return a.hi == b.hi && a.lo == b.lo;
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
@inline @operator('!=')
|
|
1806
|
+
static ne(a: i128, b: i128): bool {
|
|
1807
|
+
return !i128.eq(a, b);
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
@inline @operator('<')
|
|
1811
|
+
static lt(a: i128, b: i128): bool {
|
|
1812
|
+
let ah = a.hi, bh = b.hi;
|
|
1813
|
+
// If hi parts differ, that decides the sign. Otherwise compare lo.
|
|
1814
|
+
return ah == bh ? a.lo < b.lo : ah < bh;
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
@inline @operator('>')
|
|
1818
|
+
static gt(a: i128, b: i128): bool {
|
|
1819
|
+
return b < a;
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
@inline @operator('<=')
|
|
1823
|
+
static le(a: i128, b: i128): bool {
|
|
1824
|
+
return !i128.gt(a, b);
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
@inline @operator('>=')
|
|
1828
|
+
static ge(a: i128, b: i128): bool {
|
|
1829
|
+
return !i128.lt(a, b);
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
@inline
|
|
1833
|
+
static ord(a: i128, b: i128): i32 {
|
|
1834
|
+
// Return -1, 0, 1
|
|
1835
|
+
if (a == b) return 0;
|
|
1836
|
+
return i128.lt(a, b) ? -1 : 1;
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
@inline
|
|
1840
|
+
static popcnt(value: i128): i32 {
|
|
1841
|
+
return <i32>(popcnt(value.lo) + popcnt(value.hi));
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
@inline
|
|
1845
|
+
static clz(value: i128): i32 {
|
|
1846
|
+
return __clz128(value.lo, value.hi);
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
@inline
|
|
1850
|
+
static ctz(value: i128): i32 {
|
|
1851
|
+
return __ctz128(value.lo, value.hi);
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
@inline
|
|
1855
|
+
static abs(value: i128): i128 {
|
|
1856
|
+
// if negative, return -value
|
|
1857
|
+
return value.isNeg() ? value.neg() : value;
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
@inline
|
|
1861
|
+
isPos(): bool {
|
|
1862
|
+
return this.hi >= 0;
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
@inline
|
|
1866
|
+
isNeg(): bool {
|
|
1867
|
+
return this.hi < 0;
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
@inline
|
|
1871
|
+
isZero(): bool {
|
|
1872
|
+
return !(this.lo | this.hi);
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
@inline @operator.prefix('~')
|
|
1876
|
+
not(): i128 {
|
|
1877
|
+
return new i128(~this.lo, ~this.hi);
|
|
1878
|
+
}
|
|
1879
|
+
|
|
1880
|
+
@inline @operator.prefix('+')
|
|
1881
|
+
pos(): i128 {
|
|
1882
|
+
return this;
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
@inline @operator.prefix('-')
|
|
1886
|
+
neg(): i128 {
|
|
1887
|
+
// two's complement: ~x + 1
|
|
1888
|
+
let nlo = ~this.lo;
|
|
1889
|
+
let nhi = ~this.hi;
|
|
1890
|
+
|
|
1891
|
+
let sum = nlo + 1;
|
|
1892
|
+
let carry = (sum < nlo) ? 1 : 0;
|
|
1893
|
+
let hi2 = nhi + (carry as i64);
|
|
1894
|
+
|
|
1895
|
+
return new i128(sum, hi2);
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
/**
|
|
1899
|
+
* Convert to a normal array of bytes (16 bytes for 128 bits).
|
|
1900
|
+
* @param bigEndian If true, the highest bytes come first.
|
|
1901
|
+
*/
|
|
1902
|
+
@inline
|
|
1903
|
+
toBytes(bigEndian: bool = false): u8[] {
|
|
1904
|
+
let result = new Array<u8>(16);
|
|
1905
|
+
this.toArrayBuffer(result.dataStart, bigEndian);
|
|
1906
|
+
return result;
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
/**
|
|
1910
|
+
* Convert to a StaticArray<u8> of length 16.
|
|
1911
|
+
* @param bigEndian If true, the highest bytes come first.
|
|
1912
|
+
* @returns StaticArray<u8>
|
|
1913
|
+
*/
|
|
1914
|
+
@inline
|
|
1915
|
+
toStaticBytes(bigEndian: bool = false): StaticArray<u8> {
|
|
1916
|
+
let result = new StaticArray<u8>(16);
|
|
1917
|
+
this.toArrayBuffer(changetype<usize>(result), bigEndian);
|
|
1918
|
+
return result;
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
/**
|
|
1922
|
+
* Convert to Uint8Array
|
|
1923
|
+
* @param bigEndian Little or Big Endian? Default: false
|
|
1924
|
+
* @returns Uint8Array
|
|
1925
|
+
*/
|
|
1926
|
+
@inline
|
|
1927
|
+
toUint8Array(bigEndian: bool = false): Uint8Array {
|
|
1928
|
+
let result = new Uint8Array(16);
|
|
1929
|
+
this.toArrayBuffer(result.dataStart, bigEndian);
|
|
1930
|
+
return result;
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
@inline
|
|
1934
|
+
private toArrayBufferLE(buffer: usize): void {
|
|
1935
|
+
store<u64>(buffer, this.lo, 0 * sizeof<u64>());
|
|
1936
|
+
store<u64>(buffer, this.hi, 1 * sizeof<u64>());
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
@inline
|
|
1940
|
+
private toArrayBufferBE(buffer: usize): void {
|
|
1941
|
+
store<u64>(buffer, bswap<u64>(this.hi), 0 * sizeof<u64>());
|
|
1942
|
+
store<u64>(buffer, bswap<u64>(this.lo), 1 * sizeof<u64>());
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
@inline
|
|
1946
|
+
private toArrayBuffer(buffer: usize, bigEndian: bool = false): void {
|
|
1947
|
+
if (bigEndian) {
|
|
1948
|
+
this.toArrayBufferBE(buffer);
|
|
1949
|
+
} else {
|
|
1950
|
+
this.toArrayBufferLE(buffer);
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
`,"bignum/integer/i256":`export class i256 {
|
|
1955
|
+
|
|
1956
|
+
constructor(
|
|
1957
|
+
public lo1: i64 = 0,
|
|
1958
|
+
public lo2: i64 = 0,
|
|
1959
|
+
public hi1: i64 = 0,
|
|
1960
|
+
public hi2: i64 = 0,
|
|
1961
|
+
) {
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
@inline static get Zero(): i256 {
|
|
1965
|
+
return new i256();
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
@inline static get One(): i256 {
|
|
1969
|
+
return new i256(1);
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1972
|
+
@inline static get Min(): i256 {
|
|
1973
|
+
return new i256(0, 0, 0, 0x8000000000000000);
|
|
1974
|
+
}
|
|
1975
|
+
|
|
1976
|
+
@inline static get Max(): i256 {
|
|
1977
|
+
return new i256(u64.MAX_VALUE, u64.MAX_VALUE, u64.MAX_VALUE, 0x7FFFFFFFFFFFFFFF);
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1980
|
+
@inline @operator.prefix('!')
|
|
1981
|
+
static isEmpty(value: i256): bool {
|
|
1982
|
+
return value.isZero();
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1985
|
+
@inline
|
|
1986
|
+
isNeg(): bool {
|
|
1987
|
+
return <bool>(this.hi2 >>> 63);
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
@inline
|
|
1991
|
+
isZero(): bool {
|
|
1992
|
+
return !(this.lo1 | this.lo2 | this.hi1 | this.hi2);
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
/*
|
|
1996
|
+
@inline
|
|
1997
|
+
static abs(value: i128): i128 {
|
|
1998
|
+
return value < 0 ? value.neg() : value;
|
|
1999
|
+
}
|
|
2000
|
+
*/
|
|
2001
|
+
|
|
2002
|
+
// TODO
|
|
2003
|
+
}
|
|
2004
|
+
`,"bignum/integer/index":`export * from "./i128";
|
|
2005
|
+
export * from "./u128";
|
|
2006
|
+
export * from "./u256";
|
|
2007
|
+
`,"bignum/integer/u128":`import { i128 } from './i128';
|
|
2008
|
+
import { i256 } from './i256';
|
|
2009
|
+
import { u256 } from './u256';
|
|
2010
|
+
|
|
2011
|
+
// TODO import this on top level 'index.ts'
|
|
2012
|
+
import {
|
|
2013
|
+
__clz128,
|
|
2014
|
+
__ctz128,
|
|
2015
|
+
__divmod_quot_hi,
|
|
2016
|
+
__divmod_rem_hi,
|
|
2017
|
+
__divmod_rem_lo,
|
|
2018
|
+
__floatuntidf,
|
|
2019
|
+
__multi3,
|
|
2020
|
+
__res128_hi,
|
|
2021
|
+
__udivmod128,
|
|
2022
|
+
__udivmod128_10,
|
|
2023
|
+
} from '../globals';
|
|
2024
|
+
|
|
2025
|
+
import { atou128, u128toDecimalString } from '../utils';
|
|
2026
|
+
|
|
2027
|
+
@lazy const HEX_CHARS = '0123456789abcdef';
|
|
2028
|
+
|
|
2029
|
+
export class u128 {
|
|
2030
|
+
// CACHE: Static instances to avoid allocation
|
|
2031
|
+
private static readonly _ZERO: u128 = new u128(0, 0);
|
|
2032
|
+
private static readonly _ONE: u128 = new u128(1, 0);
|
|
2033
|
+
private static readonly _MAX: u128 = new u128(-1, -1);
|
|
2034
|
+
|
|
2035
|
+
/**
|
|
2036
|
+
* Create 128-bit unsigned integer from 64-bit parts
|
|
2037
|
+
* @param lo low 64-bit part of 128-bit unsigned integer
|
|
2038
|
+
* @param hi high 64-bit part of 128-bit unsigned integer
|
|
2039
|
+
*/
|
|
2040
|
+
constructor(
|
|
2041
|
+
public lo: u64 = 0,
|
|
2042
|
+
public hi: u64 = 0,
|
|
2043
|
+
) {
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
@inline static get immutableZero(): u128 {
|
|
2047
|
+
return u128._ZERO;
|
|
2048
|
+
}
|
|
2049
|
+
|
|
2050
|
+
@inline static get immutableOne(): u128 {
|
|
2051
|
+
return u128._ONE;
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
@inline static get immutableMin(): u128 {
|
|
2055
|
+
return u128._ZERO;
|
|
2056
|
+
}
|
|
2057
|
+
|
|
2058
|
+
@inline static get immutableMax(): u128 {
|
|
2059
|
+
return u128._MAX;
|
|
2060
|
+
}
|
|
2061
|
+
|
|
2062
|
+
@inline static get Zero(): u128 { return new u128(0, 0); }
|
|
2063
|
+
@inline static get One(): u128 { return new u128(1, 0); }
|
|
2064
|
+
@inline static get Min(): u128 { return new u128(0, 0); }
|
|
2065
|
+
@inline static get Max(): u128 { return new u128(-1, -1); }
|
|
2066
|
+
|
|
2067
|
+
/**
|
|
2068
|
+
* Ensures the current instance is not a static constant.
|
|
2069
|
+
* If it is, throws an error.
|
|
2070
|
+
* Cost: 3 integer comparisons. Cheap insurance.
|
|
2071
|
+
*/
|
|
2072
|
+
@inline
|
|
2073
|
+
private checkMutable(): void {
|
|
2074
|
+
const ptr = changetype<usize>(this);
|
|
2075
|
+
if (ptr == changetype<usize>(u128._ZERO) ||
|
|
2076
|
+
ptr == changetype<usize>(u128._ONE) ||
|
|
2077
|
+
ptr == changetype<usize>(u128._MAX)) {
|
|
2078
|
+
throw new Error("u128: Immutable constant");
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
|
|
2082
|
+
@inline
|
|
2083
|
+
static fromString(value: string, radix: i32 = 10): u128 {
|
|
2084
|
+
return atou128(value, radix);
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
@inline
|
|
2088
|
+
static fromI256(value: i256): u128 {
|
|
2089
|
+
return new u128(value.lo1, value.lo2);
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
@inline
|
|
2093
|
+
static fromU256(value: u256): u128 {
|
|
2094
|
+
return new u128(value.lo1, value.lo2);
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
@inline
|
|
2098
|
+
static fromI128(value: i128): u128 {
|
|
2099
|
+
return new u128(value.lo, value.hi);
|
|
2100
|
+
}
|
|
2101
|
+
|
|
2102
|
+
@inline
|
|
2103
|
+
static fromU128(value: u128): u128 {
|
|
2104
|
+
return new u128(value.lo, value.hi);
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
@inline
|
|
2108
|
+
static fromI64(value: i64): u128 {
|
|
2109
|
+
if (value == 0) return u128.Zero;
|
|
2110
|
+
if (value == 1) return u128.One;
|
|
2111
|
+
return new u128(value, value >> 63);
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
@inline
|
|
2115
|
+
static fromU64(value: u64): u128 {
|
|
2116
|
+
if (value == 0) return u128.Zero;
|
|
2117
|
+
if (value == 1) return u128.One;
|
|
2118
|
+
return new u128(value);
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
// max safe uint for f64 actually 53-bits
|
|
2122
|
+
@inline
|
|
2123
|
+
static fromF64(value: f64): u128 {
|
|
2124
|
+
return new u128(<u64>value, reinterpret<i64>(value) >> 63);
|
|
2125
|
+
}
|
|
2126
|
+
|
|
2127
|
+
// max safe int for f32 actually 23-bits
|
|
2128
|
+
@inline
|
|
2129
|
+
static fromF32(value: f32): u128 {
|
|
2130
|
+
return new u128(<u64>value, <u64>(reinterpret<i32>(value) >> 31));
|
|
2131
|
+
}
|
|
2132
|
+
|
|
2133
|
+
@inline
|
|
2134
|
+
static fromI32(value: i32): u128 {
|
|
2135
|
+
if (value == 0) return u128.Zero;
|
|
2136
|
+
if (value == 1) return u128.One;
|
|
2137
|
+
return new u128(value, value >> 31);
|
|
2138
|
+
}
|
|
2139
|
+
|
|
2140
|
+
@inline
|
|
2141
|
+
static fromU32(value: u32): u128 {
|
|
2142
|
+
if (value == 0) return u128.Zero;
|
|
2143
|
+
if (value == 1) return u128.One;
|
|
2144
|
+
return new u128(value);
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
@inline
|
|
2148
|
+
static fromBool(value: bool): u128 {
|
|
2149
|
+
return value ? u128.One : u128.Zero;
|
|
2150
|
+
}
|
|
2151
|
+
|
|
2152
|
+
@inline
|
|
2153
|
+
static fromBits(lo1: u32, lo2: u32, hi1: u32, hi2: u32): u128 {
|
|
2154
|
+
return new u128(
|
|
2155
|
+
<u64>lo1 | ((<u64>lo2) << 32),
|
|
2156
|
+
<u64>hi1 | ((<u64>hi2) << 32),
|
|
2157
|
+
);
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
@inline
|
|
2161
|
+
static fromBytes<T>(array: T, bigEndian: bool = false): u128 {
|
|
2162
|
+
// @ts-ignore
|
|
2163
|
+
if (array instanceof u8[]) {
|
|
2164
|
+
return bigEndian
|
|
2165
|
+
// @ts-ignore
|
|
2166
|
+
? u128.fromBytesBE(<u8[]>array)
|
|
2167
|
+
// @ts-ignore
|
|
2168
|
+
: u128.fromBytesLE(<u8[]>array);
|
|
2169
|
+
} else if (array instanceof Uint8Array) {
|
|
2170
|
+
return bigEndian
|
|
2171
|
+
? u128.fromUint8ArrayBE(<Uint8Array>array)
|
|
2172
|
+
: u128.fromUint8ArrayLE(<Uint8Array>array);
|
|
2173
|
+
} else {
|
|
2174
|
+
throw new TypeError("Unsupported generic type");
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
|
|
2178
|
+
@inline
|
|
2179
|
+
static fromBytesLE(array: u8[]): u128 {
|
|
2180
|
+
if (array.length !== 16) throw new Error("Invalid length");
|
|
2181
|
+
let buffer = array.dataStart;
|
|
2182
|
+
return new u128(
|
|
2183
|
+
load<u64>(buffer, 0),
|
|
2184
|
+
load<u64>(buffer, 8)
|
|
2185
|
+
);
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
@inline
|
|
2189
|
+
static fromBytesBE(array: u8[]): u128 {
|
|
2190
|
+
if (array.length !== 16) throw new Error("Invalid length");
|
|
2191
|
+
let buffer = array.dataStart;
|
|
2192
|
+
return new u128(
|
|
2193
|
+
bswap<u64>(load<u64>(buffer, 8)),
|
|
2194
|
+
bswap<u64>(load<u64>(buffer, 0))
|
|
2195
|
+
);
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
@inline
|
|
2199
|
+
static fromUint8ArrayLE(array: Uint8Array): u128 {
|
|
2200
|
+
if (array.length !== 16) throw new Error("Invalid length");
|
|
2201
|
+
let buffer = array.dataStart;
|
|
2202
|
+
return new u128(
|
|
2203
|
+
load<u64>(buffer, 0),
|
|
2204
|
+
load<u64>(buffer, 8)
|
|
2205
|
+
);
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
@inline
|
|
2209
|
+
static fromUint8ArrayBE(array: Uint8Array): u128 {
|
|
2210
|
+
if (array.length !== 16) throw new Error("Invalid length");
|
|
2211
|
+
let buffer = array.dataStart;
|
|
2212
|
+
return new u128(
|
|
2213
|
+
bswap<u64>(load<u64>(buffer, 8)),
|
|
2214
|
+
bswap<u64>(load<u64>(buffer, 0))
|
|
2215
|
+
);
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
/**
|
|
2219
|
+
* Create 128-bit unsigned integer from generic type T
|
|
2220
|
+
* @param value
|
|
2221
|
+
* @returns 128-bit unsigned integer
|
|
2222
|
+
*/
|
|
2223
|
+
@inline
|
|
2224
|
+
static from<T>(value: T): u128 {
|
|
2225
|
+
if (value instanceof bool) return u128.fromU64(<u64>value);
|
|
2226
|
+
else if (value instanceof i8) return u128.fromI64(<i64>value);
|
|
2227
|
+
else if (value instanceof u8) return u128.fromU64(<u64>value);
|
|
2228
|
+
else if (value instanceof i16) return u128.fromI64(<i64>value);
|
|
2229
|
+
else if (value instanceof u16) return u128.fromU64(<u64>value);
|
|
2230
|
+
else if (value instanceof i32) return u128.fromI64(<i64>value);
|
|
2231
|
+
else if (value instanceof u32) return u128.fromU64(<u64>value);
|
|
2232
|
+
else if (value instanceof i64) return u128.fromI64(<i64>value);
|
|
2233
|
+
else if (value instanceof u64) return u128.fromU64(<u64>value);
|
|
2234
|
+
else if (value instanceof f32) return u128.fromF64(<f64>value);
|
|
2235
|
+
else if (value instanceof f64) return u128.fromF64(<f64>value);
|
|
2236
|
+
else if (value instanceof i128) return u128.fromI128(<i128>value);
|
|
2237
|
+
else if (value instanceof u128) return u128.fromU128(<u128>value);
|
|
2238
|
+
else if (value instanceof i256) return u128.fromI256(<i256>value);
|
|
2239
|
+
else if (value instanceof u256) return u128.fromU256(<u256>value);
|
|
2240
|
+
else if (value instanceof u8[]) return u128.fromBytes(<u8[]>value);
|
|
2241
|
+
else if (value instanceof Uint8Array) return u128.fromBytes(<Uint8Array>value);
|
|
2242
|
+
else if (value instanceof String) return u128.fromString(<string>value);
|
|
2243
|
+
else throw new TypeError("Unsupported generic type");
|
|
2244
|
+
}
|
|
2245
|
+
|
|
2246
|
+
@inline @operator.prefix('!')
|
|
2247
|
+
static isEmpty(value: u128): bool {
|
|
2248
|
+
return !(value.lo | value.hi);
|
|
2249
|
+
}
|
|
2250
|
+
|
|
2251
|
+
@inline @operator('|')
|
|
2252
|
+
static or(a: u128, b: u128): u128 {
|
|
2253
|
+
return new u128(a.lo | b.lo, a.hi | b.hi);
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
@inline @operator('^')
|
|
2257
|
+
static xor(a: u128, b: u128): u128 {
|
|
2258
|
+
return new u128(a.lo ^ b.lo, a.hi ^ b.hi);
|
|
2259
|
+
}
|
|
2260
|
+
|
|
2261
|
+
@inline @operator('&')
|
|
2262
|
+
static and(a: u128, b: u128): u128 {
|
|
2263
|
+
return new u128(a.lo & b.lo, a.hi & b.hi);
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
@inline @operator('<<')
|
|
2267
|
+
static shl(value: u128, shift: i32): u128 {
|
|
2268
|
+
// Cache check: if shift is 0 or invalid, return existing constants if possible
|
|
2269
|
+
if (shift <= 0) {
|
|
2270
|
+
return shift == 0 ? value.clone() : u128.Zero; // Use cached Zero
|
|
2271
|
+
}
|
|
2272
|
+
|
|
2273
|
+
if (shift >= 128) {
|
|
2274
|
+
return u128.Zero; // Use cached Zero
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
// Unrolled shift logic
|
|
2278
|
+
if (shift < 64) {
|
|
2279
|
+
const lo = value.lo << shift;
|
|
2280
|
+
const hi = (value.hi << shift) | (value.lo >>> (64 - shift));
|
|
2281
|
+
return new u128(lo, hi);
|
|
2282
|
+
} else {
|
|
2283
|
+
// shift >= 64
|
|
2284
|
+
return new u128(0, value.lo << (shift - 64));
|
|
2285
|
+
}
|
|
2286
|
+
}
|
|
2287
|
+
|
|
2288
|
+
@inline @operator('>>')
|
|
2289
|
+
static shr(value: u128, shift: i32): u128 {
|
|
2290
|
+
shift &= 127;
|
|
2291
|
+
if (shift == 0) return value.clone();
|
|
2292
|
+
|
|
2293
|
+
// Unrolled shift logic
|
|
2294
|
+
if (shift < 64) {
|
|
2295
|
+
let hi = value.hi >> shift;
|
|
2296
|
+
let lo = (value.hi << (64 - shift)) | (value.lo >> shift);
|
|
2297
|
+
return new u128(lo, hi);
|
|
2298
|
+
} else {
|
|
2299
|
+
let s = shift - 64;
|
|
2300
|
+
return new u128(value.hi >> s, 0);
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
|
|
2304
|
+
@inline @operator('>>>')
|
|
2305
|
+
static shr_u(value: u128, shift: i32): u128 {
|
|
2306
|
+
return u128.shr(value, shift);
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
@inline
|
|
2310
|
+
static rotl(value: u128, shift: i32): u128 {
|
|
2311
|
+
let n = shift & 127;
|
|
2312
|
+
if (n == 0) return value.clone();
|
|
2313
|
+
|
|
2314
|
+
let lo = value.lo;
|
|
2315
|
+
let hi = value.hi;
|
|
2316
|
+
if (n == 64) {
|
|
2317
|
+
return new u128(hi, lo);
|
|
2318
|
+
}
|
|
2319
|
+
if (n & 64) {
|
|
2320
|
+
let t = lo;
|
|
2321
|
+
lo = hi;
|
|
2322
|
+
hi = t;
|
|
2323
|
+
}
|
|
2324
|
+
let slo = lo << n;
|
|
2325
|
+
let shi = hi << n;
|
|
2326
|
+
let rlo = lo >> (64 - n);
|
|
2327
|
+
let rhi = hi >> (64 - n);
|
|
2328
|
+
return new u128(slo | rhi, shi | rlo);
|
|
2329
|
+
}
|
|
2330
|
+
|
|
2331
|
+
@inline
|
|
2332
|
+
static rotr(value: u128, shift: i32): u128 {
|
|
2333
|
+
let n = shift & 127;
|
|
2334
|
+
if (n == 0) return value.clone();
|
|
2335
|
+
|
|
2336
|
+
let lo = value.lo;
|
|
2337
|
+
let hi = value.hi;
|
|
2338
|
+
if (n == 64) {
|
|
2339
|
+
return new u128(hi, lo);
|
|
2340
|
+
}
|
|
2341
|
+
if (n & 64) {
|
|
2342
|
+
let t = lo;
|
|
2343
|
+
lo = hi;
|
|
2344
|
+
hi = t;
|
|
2345
|
+
}
|
|
2346
|
+
let slo = lo >> n;
|
|
2347
|
+
let shi = hi >> n;
|
|
2348
|
+
let rlo = lo << (64 - n);
|
|
2349
|
+
let rhi = hi << (64 - n);
|
|
2350
|
+
return new u128(slo | rhi, shi | rlo);
|
|
2351
|
+
}
|
|
2352
|
+
|
|
2353
|
+
@inline @operator('+')
|
|
2354
|
+
static add(a: u128, b: u128): u128 {
|
|
2355
|
+
var alo = a.lo;
|
|
2356
|
+
var lo = alo + b.lo;
|
|
2357
|
+
var hi = a.hi + b.hi + u64(lo < alo);
|
|
2358
|
+
return new u128(lo, hi);
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
@inline @operator('-')
|
|
2362
|
+
static sub(a: u128, b: u128): u128 {
|
|
2363
|
+
var alo = a.lo;
|
|
2364
|
+
var lo = alo - b.lo;
|
|
2365
|
+
var hi = a.hi - b.hi - u64(lo > alo);
|
|
2366
|
+
return new u128(lo, hi);
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
// mul: u128 x u128 = u128
|
|
2370
|
+
@inline @operator('*')
|
|
2371
|
+
static mul(a: u128, b: u128): u128 {
|
|
2372
|
+
return new u128(
|
|
2373
|
+
__multi3(a.lo, a.hi, b.lo, b.hi),
|
|
2374
|
+
__res128_hi
|
|
2375
|
+
);
|
|
2376
|
+
}
|
|
2377
|
+
|
|
2378
|
+
@inline @operator('/')
|
|
2379
|
+
static div(a: u128, b: u128): u128 {
|
|
2380
|
+
return new u128(
|
|
2381
|
+
__udivmod128(a.lo, a.hi, b.lo, b.hi),
|
|
2382
|
+
__divmod_quot_hi
|
|
2383
|
+
);
|
|
2384
|
+
}
|
|
2385
|
+
|
|
2386
|
+
@inline @operator('%')
|
|
2387
|
+
static rem(a: u128, b: u128): u128 {
|
|
2388
|
+
__udivmod128(a.lo, a.hi, b.lo, b.hi);
|
|
2389
|
+
return new u128(__divmod_rem_lo, __divmod_rem_hi);
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
@inline
|
|
2393
|
+
static div10(value: u128): u128 {
|
|
2394
|
+
return new u128(
|
|
2395
|
+
__udivmod128_10(value.lo, value.hi),
|
|
2396
|
+
__divmod_quot_hi
|
|
2397
|
+
);
|
|
2398
|
+
}
|
|
2399
|
+
|
|
2400
|
+
@inline
|
|
2401
|
+
static rem10(value: u128): u128 {
|
|
2402
|
+
__udivmod128_10(value.lo, value.hi);
|
|
2403
|
+
return new u128(__divmod_rem_lo, __divmod_rem_hi);
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2406
|
+
/**
|
|
2407
|
+
* Calculate power of base with exponent
|
|
2408
|
+
* @param base 128-bit unsigned integer
|
|
2409
|
+
* @param exponent 32-bit signed integer
|
|
2410
|
+
* @returns 128-bit unsigned integer
|
|
2411
|
+
*/
|
|
2412
|
+
@operator('**')
|
|
2413
|
+
static pow(base: u128, exponent: i32): u128 {
|
|
2414
|
+
// any negative exponent produce zero
|
|
2415
|
+
|
|
2416
|
+
var result = u128.One;
|
|
2417
|
+
|
|
2418
|
+
if (base == result) return result;
|
|
2419
|
+
var tmp = base.clone();
|
|
2420
|
+
if (exponent <= 1) {
|
|
2421
|
+
if (exponent < 0) return u128.Zero;
|
|
2422
|
+
return exponent == 0 ? result : tmp;
|
|
2423
|
+
}
|
|
2424
|
+
|
|
2425
|
+
if (ASC_SHRINK_LEVEL < 1) {
|
|
2426
|
+
var lo = base.lo;
|
|
2427
|
+
var hi = base.hi;
|
|
2428
|
+
// if base > u64::max and exp > 1 always return "0"
|
|
2429
|
+
if (!lo) return u128.Zero;
|
|
2430
|
+
if (!hi) {
|
|
2431
|
+
let lo1 = lo - 1;
|
|
2432
|
+
// "1 ^ exponent" always return "1"
|
|
2433
|
+
if (!lo1) return result;
|
|
2434
|
+
|
|
2435
|
+
// if base is power of two do "1 << log2(base) * exp"
|
|
2436
|
+
if (!(lo & lo1)) {
|
|
2437
|
+
let shift = <i32>(64 - clz(lo1)) * exponent;
|
|
2438
|
+
// @ts-ignore
|
|
2439
|
+
return shift < 128 ? result << shift : u128.Zero;
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
if (exponent <= 4) {
|
|
2444
|
+
let baseSq = tmp.sqr();
|
|
2445
|
+
switch (exponent) {
|
|
2446
|
+
case 2:
|
|
2447
|
+
return baseSq; // base ^ 2
|
|
2448
|
+
// @ts-ignore
|
|
2449
|
+
case 3:
|
|
2450
|
+
return baseSq * base; // base ^ 2 * base
|
|
2451
|
+
case 4:
|
|
2452
|
+
return baseSq.sqr(); // base ^ 2 * base ^ 2
|
|
2453
|
+
default:
|
|
2454
|
+
break;
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
|
|
2458
|
+
let log = 32 - clz(exponent);
|
|
2459
|
+
if (log <= 7) {
|
|
2460
|
+
// 128 = 2 ^ 7, so need usually only seven cases
|
|
2461
|
+
switch (log) {
|
|
2462
|
+
case 7:
|
|
2463
|
+
// @ts-ignore
|
|
2464
|
+
if (exponent & 1) result *= tmp;
|
|
2465
|
+
exponent >>= 1;
|
|
2466
|
+
tmp.sqr();
|
|
2467
|
+
case 6:
|
|
2468
|
+
// @ts-ignore
|
|
2469
|
+
if (exponent & 1) result *= tmp;
|
|
2470
|
+
exponent >>= 1;
|
|
2471
|
+
tmp.sqr();
|
|
2472
|
+
case 5:
|
|
2473
|
+
// @ts-ignore
|
|
2474
|
+
if (exponent & 1) result *= tmp;
|
|
2475
|
+
exponent >>= 1;
|
|
2476
|
+
tmp.sqr();
|
|
2477
|
+
case 4:
|
|
2478
|
+
// @ts-ignore
|
|
2479
|
+
if (exponent & 1) result *= tmp;
|
|
2480
|
+
exponent >>= 1;
|
|
2481
|
+
tmp.sqr();
|
|
2482
|
+
case 3:
|
|
2483
|
+
// @ts-ignore
|
|
2484
|
+
if (exponent & 1) result *= tmp;
|
|
2485
|
+
exponent >>= 1;
|
|
2486
|
+
tmp.sqr();
|
|
2487
|
+
case 2:
|
|
2488
|
+
// @ts-ignore
|
|
2489
|
+
if (exponent & 1) result *= tmp;
|
|
2490
|
+
exponent >>= 1;
|
|
2491
|
+
tmp.sqr();
|
|
2492
|
+
case 1:
|
|
2493
|
+
// @ts-ignore
|
|
2494
|
+
if (exponent & 1) result *= tmp;
|
|
2495
|
+
}
|
|
2496
|
+
return result;
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
|
|
2500
|
+
while (exponent > 0) {
|
|
2501
|
+
// @ts-ignore
|
|
2502
|
+
if (exponent & 1) result *= tmp;
|
|
2503
|
+
exponent >>= 1;
|
|
2504
|
+
tmp.sqr();
|
|
2505
|
+
}
|
|
2506
|
+
return result;
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
// compute floor(sqrt(x))
|
|
2510
|
+
static sqrt(value: u128): u128 {
|
|
2511
|
+
var rem = value.clone();
|
|
2512
|
+
if (value < new u128(2)) {
|
|
2513
|
+
return rem;
|
|
2514
|
+
}
|
|
2515
|
+
var res = u128.Zero;
|
|
2516
|
+
// @ts-ignore
|
|
2517
|
+
var pos = u128.One << (127 - (u128.clz(value) | 1));
|
|
2518
|
+
// @ts-ignore
|
|
2519
|
+
while (!pos.isZero()) {
|
|
2520
|
+
// @ts-ignore
|
|
2521
|
+
value = res + pos;
|
|
2522
|
+
if (rem >= value) {
|
|
2523
|
+
// @ts-ignore
|
|
2524
|
+
rem = rem - value;
|
|
2525
|
+
// @ts-ignore
|
|
2526
|
+
res = pos + value;
|
|
2527
|
+
}
|
|
2528
|
+
// @ts-ignore
|
|
2529
|
+
res >>= 1;
|
|
2530
|
+
pos >>= 2;
|
|
2531
|
+
}
|
|
2532
|
+
return res;
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
@inline @operator('==')
|
|
2536
|
+
static eq(a: u128, b: u128): bool {
|
|
2537
|
+
return a.hi == b.hi && a.lo == b.lo;
|
|
2538
|
+
}
|
|
2539
|
+
|
|
2540
|
+
@inline @operator('!=')
|
|
2541
|
+
static ne(a: u128, b: u128): bool {
|
|
2542
|
+
return !u128.eq(a, b);
|
|
2543
|
+
}
|
|
2544
|
+
|
|
2545
|
+
@inline @operator('<')
|
|
2546
|
+
static lt(a: u128, b: u128): bool {
|
|
2547
|
+
var ah = a.hi, bh = b.hi;
|
|
2548
|
+
return ah == bh ? a.lo < b.lo : ah < bh;
|
|
2549
|
+
}
|
|
2550
|
+
|
|
2551
|
+
@inline @operator('>')
|
|
2552
|
+
static gt(a: u128, b: u128): bool {
|
|
2553
|
+
var ah = a.hi, bh = b.hi;
|
|
2554
|
+
return ah == bh ? a.lo > b.lo : ah > bh;
|
|
2555
|
+
}
|
|
2556
|
+
|
|
2557
|
+
@inline @operator('<=')
|
|
2558
|
+
static le(a: u128, b: u128): bool {
|
|
2559
|
+
return !u128.gt(a, b);
|
|
2560
|
+
}
|
|
2561
|
+
|
|
2562
|
+
@inline @operator('>=')
|
|
2563
|
+
static ge(a: u128, b: u128): bool {
|
|
2564
|
+
return !u128.lt(a, b);
|
|
2565
|
+
}
|
|
2566
|
+
|
|
2567
|
+
/**
|
|
2568
|
+
* Get ordering
|
|
2569
|
+
* if a > b then result is 1
|
|
2570
|
+
* if a < b then result is -1
|
|
2571
|
+
* if a = b then result is 0
|
|
2572
|
+
* @param a 128-bit unsigned integer
|
|
2573
|
+
* @param b 128-bit unsigned integer
|
|
2574
|
+
* @returns 32-bit signed integer
|
|
2575
|
+
*/
|
|
2576
|
+
@inline
|
|
2577
|
+
static ord(a: u128, b: u128): i32 {
|
|
2578
|
+
// if a > b => +1
|
|
2579
|
+
// if a < b => -1
|
|
2580
|
+
// if equal => 0
|
|
2581
|
+
if (a.hi > b.hi) return 1;
|
|
2582
|
+
if (a.hi < b.hi) return -1;
|
|
2583
|
+
if (a.lo > b.lo) return 1;
|
|
2584
|
+
if (a.lo < b.lo) return -1;
|
|
2585
|
+
return 0;
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
/**
|
|
2589
|
+
* Compute count of set (populated) bits
|
|
2590
|
+
* @param value 128-bit unsigned integer
|
|
2591
|
+
* @returns 32-bit signed integer
|
|
2592
|
+
*/
|
|
2593
|
+
@inline
|
|
2594
|
+
static popcnt(value: u128): i32 {
|
|
2595
|
+
return <i32>(popcnt(value.lo) + popcnt(value.hi));
|
|
2596
|
+
}
|
|
2597
|
+
|
|
2598
|
+
/**
|
|
2599
|
+
* Compute bit count of leading zeros
|
|
2600
|
+
* @param value 128-bit unsigned integer
|
|
2601
|
+
* @returns 32-bit signed integer
|
|
2602
|
+
*/
|
|
2603
|
+
@inline
|
|
2604
|
+
static clz(value: u128): i32 {
|
|
2605
|
+
return __clz128(value.lo, value.hi);
|
|
2606
|
+
}
|
|
2607
|
+
|
|
2608
|
+
/**
|
|
2609
|
+
* Compute bit count of trailing zeros
|
|
2610
|
+
* @param value 128-bit unsigned integer
|
|
2611
|
+
* @returns 32-bit signed integer
|
|
2612
|
+
*/
|
|
2613
|
+
@inline
|
|
2614
|
+
static ctz(value: u128): i32 {
|
|
2615
|
+
return __ctz128(value.lo, value.hi);
|
|
2616
|
+
}
|
|
2617
|
+
|
|
2618
|
+
/**
|
|
2619
|
+
* Calculate squared value (value ** 2)
|
|
2620
|
+
* @param value 128-bit unsigned integer
|
|
2621
|
+
* @returns 128-bit unsigned integer
|
|
2622
|
+
*/
|
|
2623
|
+
@inline
|
|
2624
|
+
static sqr(value: u128): u128 {
|
|
2625
|
+
return value.clone().sqr();
|
|
2626
|
+
}
|
|
2627
|
+
|
|
2628
|
+
/**
|
|
2629
|
+
* Calculate multiply and division as \`number * numerator / denominator\`
|
|
2630
|
+
* without overflow in multiplication part.
|
|
2631
|
+
*
|
|
2632
|
+
* @returns 128-bit unsigned integer
|
|
2633
|
+
*/
|
|
2634
|
+
static muldiv(a: u128, b: u128, c: u128): u128 {
|
|
2635
|
+
let A = u256.fromU128(a);
|
|
2636
|
+
let B = u256.fromU128(b);
|
|
2637
|
+
let C = u256.fromU128(c);
|
|
2638
|
+
|
|
2639
|
+
let product = u256.mul(A, B);
|
|
2640
|
+
let quotient = u256.div(product, C);
|
|
2641
|
+
|
|
2642
|
+
return quotient.toU128();
|
|
2643
|
+
}
|
|
2644
|
+
|
|
2645
|
+
@inline
|
|
2646
|
+
set(value: u128): this {
|
|
2647
|
+
this.checkMutable();
|
|
2648
|
+
this.lo = value.lo;
|
|
2649
|
+
this.hi = value.hi;
|
|
2650
|
+
return this;
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2653
|
+
@inline
|
|
2654
|
+
setI64(value: i64): this {
|
|
2655
|
+
this.checkMutable();
|
|
2656
|
+
this.lo = value;
|
|
2657
|
+
this.hi = value >> 63;
|
|
2658
|
+
return this;
|
|
2659
|
+
}
|
|
2660
|
+
|
|
2661
|
+
@inline
|
|
2662
|
+
setU64(value: u64): this {
|
|
2663
|
+
this.checkMutable();
|
|
2664
|
+
this.lo = value;
|
|
2665
|
+
this.hi = 0;
|
|
2666
|
+
return this;
|
|
2667
|
+
}
|
|
2668
|
+
|
|
2669
|
+
@inline
|
|
2670
|
+
setI32(value: i32): this {
|
|
2671
|
+
this.checkMutable();
|
|
2672
|
+
this.lo = value;
|
|
2673
|
+
this.hi = value >> 63;
|
|
2674
|
+
return this;
|
|
2675
|
+
}
|
|
2676
|
+
|
|
2677
|
+
@inline
|
|
2678
|
+
setU32(value: u32): this {
|
|
2679
|
+
this.checkMutable();
|
|
2680
|
+
this.lo = value;
|
|
2681
|
+
this.hi = 0;
|
|
2682
|
+
return this;
|
|
2683
|
+
}
|
|
2684
|
+
|
|
2685
|
+
@inline
|
|
2686
|
+
isZero(): bool {
|
|
2687
|
+
return !(this.lo | this.hi);
|
|
2688
|
+
}
|
|
2689
|
+
|
|
2690
|
+
@inline @operator.prefix('~')
|
|
2691
|
+
not(): u128 {
|
|
2692
|
+
return new u128(~this.lo, ~this.hi);
|
|
2693
|
+
}
|
|
2694
|
+
|
|
2695
|
+
@inline @operator.prefix('+')
|
|
2696
|
+
pos(): u128 {
|
|
2697
|
+
return this;
|
|
2698
|
+
}
|
|
2699
|
+
|
|
2700
|
+
@inline @operator.prefix('-')
|
|
2701
|
+
neg(): u128 {
|
|
2702
|
+
var lo = ~this.lo;
|
|
2703
|
+
var hi = ~this.hi;
|
|
2704
|
+
var lo1 = lo + 1;
|
|
2705
|
+
return new u128(lo1, hi + u64(lo1 < lo));
|
|
2706
|
+
}
|
|
2707
|
+
|
|
2708
|
+
@operator.prefix('++')
|
|
2709
|
+
preInc(): this {
|
|
2710
|
+
this.checkMutable();
|
|
2711
|
+
var lo = this.lo;
|
|
2712
|
+
var lo1 = lo + 1;
|
|
2713
|
+
this.hi += u64(lo1 < lo);
|
|
2714
|
+
this.lo = lo1;
|
|
2715
|
+
return this;
|
|
2716
|
+
}
|
|
2717
|
+
|
|
2718
|
+
@operator.prefix('--')
|
|
2719
|
+
preDec(): this {
|
|
2720
|
+
this.checkMutable();
|
|
2721
|
+
var lo = this.lo;
|
|
2722
|
+
var lo1 = lo - 1;
|
|
2723
|
+
this.hi -= u64(lo1 > lo);
|
|
2724
|
+
this.lo = lo1;
|
|
2725
|
+
return this;
|
|
2726
|
+
}
|
|
2727
|
+
|
|
2728
|
+
@operator.postfix('++')
|
|
2729
|
+
postInc(): u128 {
|
|
2730
|
+
return this.clone().preInc();
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
@operator.postfix('--')
|
|
2734
|
+
postDec(): u128 {
|
|
2735
|
+
return this.clone().preDec();
|
|
2736
|
+
}
|
|
2737
|
+
|
|
2738
|
+
/**
|
|
2739
|
+
* Calculate inplace squared 128-bit unsigned integer (this ** 2)
|
|
2740
|
+
* @returns 128-bit unsigned integer
|
|
2741
|
+
*/
|
|
2742
|
+
sqr(): this {
|
|
2743
|
+
this.checkMutable();
|
|
2744
|
+
// Use built-in 128\xD7128 => 128 multiplication
|
|
2745
|
+
// i.e. "x * x"
|
|
2746
|
+
let tmp = u128.mul(this, this);
|
|
2747
|
+
this.lo = tmp.lo;
|
|
2748
|
+
this.hi = tmp.hi;
|
|
2749
|
+
return this;
|
|
2750
|
+
}
|
|
2751
|
+
|
|
2752
|
+
/**
|
|
2753
|
+
* Convert to 256-bit signed integer
|
|
2754
|
+
* @returns 256-bit signed integer
|
|
2755
|
+
*/
|
|
2756
|
+
@inline
|
|
2757
|
+
toI256(): i256 {
|
|
2758
|
+
return new i256(this.lo, this.hi);
|
|
2759
|
+
}
|
|
2760
|
+
|
|
2761
|
+
/**
|
|
2762
|
+
* Convert to 256-bit unsigned integer
|
|
2763
|
+
* @returns 256-bit unsigned integer
|
|
2764
|
+
*/
|
|
2765
|
+
@inline
|
|
2766
|
+
toU256(): u256 {
|
|
2767
|
+
return new u256(this.lo, this.hi);
|
|
2768
|
+
}
|
|
2769
|
+
|
|
2770
|
+
/**
|
|
2771
|
+
* Convert to 128-bit signed integer
|
|
2772
|
+
* @returns 128-bit signed integer
|
|
2773
|
+
*/
|
|
2774
|
+
@inline
|
|
2775
|
+
toI128(): i128 {
|
|
2776
|
+
return new i128(this.lo, this.hi);
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
/**
|
|
2780
|
+
* Convert to 128-bit unsigned integer
|
|
2781
|
+
* @returns 128-bit unsigned integer
|
|
2782
|
+
*/
|
|
2783
|
+
@inline
|
|
2784
|
+
toU128(): this {
|
|
2785
|
+
return this;
|
|
2786
|
+
}
|
|
2787
|
+
|
|
2788
|
+
/**
|
|
2789
|
+
* Convert to 64-bit signed integer
|
|
2790
|
+
* @returns 64-bit signed integer
|
|
2791
|
+
*/
|
|
2792
|
+
@inline
|
|
2793
|
+
toI64(): i64 {
|
|
2794
|
+
return <i64>(
|
|
2795
|
+
(this.lo & 0x7FFFFFFFFFFFFFFF) |
|
|
2796
|
+
(this.hi & 0x8000000000000000)
|
|
2797
|
+
);
|
|
2798
|
+
}
|
|
2799
|
+
|
|
2800
|
+
/**
|
|
2801
|
+
* Convert to 64-bit unsigned integer
|
|
2802
|
+
* @returns 64-bit unsigned integer
|
|
2803
|
+
*/
|
|
2804
|
+
@inline
|
|
2805
|
+
toU64(): u64 {
|
|
2806
|
+
return this.lo;
|
|
2807
|
+
}
|
|
2808
|
+
|
|
2809
|
+
/**
|
|
2810
|
+
* Convert to 32-bit signed integer
|
|
2811
|
+
* @returns 32-bit signed integer
|
|
2812
|
+
*/
|
|
2813
|
+
@inline
|
|
2814
|
+
toI32(): i32 {
|
|
2815
|
+
return <i32>this.toI64();
|
|
2816
|
+
}
|
|
2817
|
+
|
|
2818
|
+
/**
|
|
2819
|
+
* Convert to 32-bit unsigned integer
|
|
2820
|
+
* @returns 32-bit unsigned integer
|
|
2821
|
+
*/
|
|
2822
|
+
@inline
|
|
2823
|
+
toU32(): u32 {
|
|
2824
|
+
return <u32>this.lo;
|
|
2825
|
+
}
|
|
2826
|
+
|
|
2827
|
+
/**
|
|
2828
|
+
* Convert to 1-bit boolean
|
|
2829
|
+
* @returns 1-bit boolean
|
|
2830
|
+
*/
|
|
2831
|
+
@inline
|
|
2832
|
+
toBool(): bool {
|
|
2833
|
+
return (this.lo | this.hi) != 0;
|
|
2834
|
+
}
|
|
2835
|
+
|
|
2836
|
+
/**
|
|
2837
|
+
* Convert to 64-bit float number in deteministic way
|
|
2838
|
+
* @returns 64-bit float
|
|
2839
|
+
*/
|
|
2840
|
+
@inline
|
|
2841
|
+
toF64(): f64 {
|
|
2842
|
+
return __floatuntidf(this.lo, this.hi);
|
|
2843
|
+
}
|
|
2844
|
+
|
|
2845
|
+
/**
|
|
2846
|
+
* Convert to 32-bit float number
|
|
2847
|
+
* @returns 32-bit float
|
|
2848
|
+
*/
|
|
2849
|
+
@inline
|
|
2850
|
+
toF32(): f32 {
|
|
2851
|
+
return <f32>this.toF64();
|
|
2852
|
+
}
|
|
2853
|
+
|
|
2854
|
+
/**
|
|
2855
|
+
* Convert to generic type \`T\`. Useful inside other generics methods
|
|
2856
|
+
* @param T is <bool | i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64 | f32 | f64 | i128 | u128 | u256 | u8[] | Uint8Array | \`StaticArray<u8>\` | string>
|
|
2857
|
+
* @returns type of \`T\`
|
|
2858
|
+
*/
|
|
2859
|
+
@inline
|
|
2860
|
+
as<T>(): T {
|
|
2861
|
+
if (isBoolean<T>()) {
|
|
2862
|
+
return <T>this.toBool();
|
|
2863
|
+
} else if (isInteger<T>()) {
|
|
2864
|
+
if (isSigned<T>()) {
|
|
2865
|
+
// i8, i16, i32, i64
|
|
2866
|
+
return <T>this.toI64();
|
|
2867
|
+
} else {
|
|
2868
|
+
// u8, u16, u32, u64
|
|
2869
|
+
return <T>this.toU64();
|
|
2870
|
+
}
|
|
2871
|
+
} else if (isFloat<T>()) {
|
|
2872
|
+
// f32, f64
|
|
2873
|
+
return <T>this.toF64();
|
|
2874
|
+
} else if (isString<T>()) {
|
|
2875
|
+
return <T>this.toString();
|
|
2876
|
+
} else if (isReference<T>()) {
|
|
2877
|
+
let dummy = changetype<T>(0);
|
|
2878
|
+
if (dummy instanceof u8[]) return <T>this.toBytes();
|
|
2879
|
+
else if (dummy instanceof StaticArray<u8>) return <T>this.toStaticBytes();
|
|
2880
|
+
else if (dummy instanceof Uint8Array) return <T>this.toUint8Array();
|
|
2881
|
+
else if (dummy instanceof i128) return <T>this.toI128();
|
|
2882
|
+
else if (dummy instanceof u128) return <T>this;
|
|
2883
|
+
else if (dummy instanceof u256) return <T>this.toU256();
|
|
2884
|
+
else throw new TypeError('Unsupported generic type');
|
|
2885
|
+
} else throw new TypeError('Unsupported generic type');
|
|
2886
|
+
}
|
|
2887
|
+
|
|
2888
|
+
/**
|
|
2889
|
+
* Convert to byte array
|
|
2890
|
+
* @param bigEndian Little or Big Endian? Default: false
|
|
2891
|
+
* @returns Array of bytes
|
|
2892
|
+
*/
|
|
2893
|
+
@inline
|
|
2894
|
+
toBytes(bigEndian: bool = false): u8[] {
|
|
2895
|
+
var result = new Array<u8>(16);
|
|
2896
|
+
this.toArrayBuffer(result.dataStart, bigEndian);
|
|
2897
|
+
return result;
|
|
2898
|
+
}
|
|
2899
|
+
|
|
2900
|
+
/**
|
|
2901
|
+
* Convert to byte static array
|
|
2902
|
+
* @param bigEndian Little or Big Endian? Default: false
|
|
2903
|
+
* @returns StaticArray of bytes
|
|
2904
|
+
*/
|
|
2905
|
+
@inline
|
|
2906
|
+
toStaticBytes(bigEndian: bool = false): StaticArray<u8> {
|
|
2907
|
+
var result = new StaticArray<u8>(16);
|
|
2908
|
+
this.toArrayBuffer(changetype<usize>(result), bigEndian);
|
|
2909
|
+
return result;
|
|
2910
|
+
}
|
|
2911
|
+
|
|
2912
|
+
/**
|
|
2913
|
+
* Convert to Uint8Array
|
|
2914
|
+
* @param bigEndian Little or Big Endian? Default: false
|
|
2915
|
+
* @returns Uint8Array
|
|
2916
|
+
*/
|
|
2917
|
+
@inline
|
|
2918
|
+
toUint8Array(bigEndian: bool = false): Uint8Array {
|
|
2919
|
+
var result = new Uint8Array(16);
|
|
2920
|
+
this.toArrayBuffer(result.dataStart, bigEndian);
|
|
2921
|
+
return result;
|
|
2922
|
+
}
|
|
2923
|
+
|
|
2924
|
+
/**
|
|
2925
|
+
* Return copy of current 128-bit value
|
|
2926
|
+
* @returns 128-bit unsigned integer
|
|
2927
|
+
*/
|
|
2928
|
+
clone(): u128 {
|
|
2929
|
+
return new u128(this.lo, this.hi);
|
|
2930
|
+
}
|
|
2931
|
+
|
|
2932
|
+
toString(radix: i32 = 10): string {
|
|
2933
|
+
assert(radix == 10 || radix == 16, 'radix argument must be between 10 or 16');
|
|
2934
|
+
if (this.isZero()) return '0';
|
|
2935
|
+
|
|
2936
|
+
var result = '';
|
|
2937
|
+
if (radix == 16) {
|
|
2938
|
+
let shift: i32 = 124 - (u128.clz(this) & ~3);
|
|
2939
|
+
while (shift >= 0) {
|
|
2940
|
+
// @ts-ignore
|
|
2941
|
+
result += HEX_CHARS.charAt(<i32>((this >> shift).lo & 15));
|
|
2942
|
+
shift -= 4;
|
|
2943
|
+
}
|
|
2944
|
+
return result;
|
|
2945
|
+
}
|
|
2946
|
+
return u128toDecimalString(this);
|
|
2947
|
+
}
|
|
2948
|
+
|
|
2949
|
+
@inline
|
|
2950
|
+
private toArrayBufferLE(buffer: usize): void {
|
|
2951
|
+
store<u64>(buffer, this.lo, 0 * sizeof<u64>());
|
|
2952
|
+
store<u64>(buffer, this.hi, 1 * sizeof<u64>());
|
|
2953
|
+
}
|
|
2954
|
+
|
|
2955
|
+
@inline
|
|
2956
|
+
private toArrayBufferBE(buffer: usize): void {
|
|
2957
|
+
store<u64>(buffer, bswap(this.hi), 0 * sizeof<u64>());
|
|
2958
|
+
store<u64>(buffer, bswap(this.lo), 1 * sizeof<u64>());
|
|
2959
|
+
}
|
|
2960
|
+
|
|
2961
|
+
@inline
|
|
2962
|
+
private toArrayBuffer(buffer: usize, bigEndian: bool = false): void {
|
|
2963
|
+
if (bigEndian) {
|
|
2964
|
+
this.toArrayBufferBE(buffer);
|
|
2965
|
+
} else {
|
|
2966
|
+
this.toArrayBufferLE(buffer);
|
|
2967
|
+
}
|
|
2968
|
+
}
|
|
2969
|
+
}
|
|
2970
|
+
`,"bignum/integer/u256":`import { i128 } from './i128';
|
|
2971
|
+
import { u128 } from './u128';
|
|
2972
|
+
import { u256toDecimalString } from "../utils";
|
|
2973
|
+
import {
|
|
2974
|
+
__mul256, __u256carry, __u256carrySub, add64Local, sub64
|
|
2975
|
+
} from '../globals';
|
|
2976
|
+
|
|
2977
|
+
@lazy const HEX_CHARS = '0123456789abcdef';
|
|
2978
|
+
|
|
2979
|
+
/**
|
|
2980
|
+
* Represents a 256-bit unsigned integer.
|
|
2981
|
+
* Optimized for gas efficiency with unrolled loops and direct memory access.
|
|
2982
|
+
*/
|
|
2983
|
+
export class u256 {
|
|
2984
|
+
/**
|
|
2985
|
+
* Constructs a new \`u256\` with the specified 64-bit limbs.
|
|
2986
|
+
* @constructor
|
|
2987
|
+
* @param {u64} [lo1=0] - The lowest 64 bits.
|
|
2988
|
+
* @param {u64} [lo2=0] - The 64 bits above \`lo1\`.
|
|
2989
|
+
* @param {u64} [hi1=0] - The 64 bits above \`lo2\`.
|
|
2990
|
+
* @param {u64} [hi2=0] - The highest 64 bits.
|
|
2991
|
+
*/
|
|
2992
|
+
constructor(
|
|
2993
|
+
public lo1: u64 = 0,
|
|
2994
|
+
public lo2: u64 = 0,
|
|
2995
|
+
public hi1: u64 = 0,
|
|
2996
|
+
public hi2: u64 = 0,
|
|
2997
|
+
) {}
|
|
2998
|
+
|
|
2999
|
+
@inline static get Zero(): u256 { return new u256(0, 0, 0, 0); }
|
|
3000
|
+
@inline static get One(): u256 { return new u256(1, 0, 0, 0); }
|
|
3001
|
+
@inline static get Min(): u256 { return new u256(0, 0, 0, 0); }
|
|
3002
|
+
@inline static get Max(): u256 { return new u256(-1, -1, -1, -1); }
|
|
3003
|
+
|
|
3004
|
+
/**
|
|
3005
|
+
* A constant representing the maximum value for 256-bit unsigned integer.
|
|
3006
|
+
* All bits set to 1.
|
|
3007
|
+
* @returns {u256}
|
|
3008
|
+
*/
|
|
3009
|
+
/**
|
|
3010
|
+
* Creates a new \`u256\` from an existing \`u256\`.
|
|
3011
|
+
* @param {u256} value - The source \`u256\` value.
|
|
3012
|
+
* @returns {u256} - A new copy of the \`value\`.
|
|
3013
|
+
*/
|
|
3014
|
+
@inline
|
|
3015
|
+
static fromU256(value: u256): u256 {
|
|
3016
|
+
return new u256(value.lo1, value.lo2, value.hi1, value.hi2);
|
|
3017
|
+
}
|
|
3018
|
+
|
|
3019
|
+
/**
|
|
3020
|
+
* Creates a new \`u256\` from a 128-bit unsigned integer.
|
|
3021
|
+
* @param {u128} value - The 128-bit unsigned integer.
|
|
3022
|
+
* @returns {u256} - The resulting 256-bit unsigned integer.
|
|
3023
|
+
*/
|
|
3024
|
+
@inline
|
|
3025
|
+
static fromU128(value: u128): u256 {
|
|
3026
|
+
return new u256(value.lo, value.hi);
|
|
3027
|
+
}
|
|
3028
|
+
|
|
3029
|
+
/**
|
|
3030
|
+
* Creates a new \`u256\` from a 64-bit unsigned integer.
|
|
3031
|
+
* @param {u64} value - The 64-bit unsigned integer.
|
|
3032
|
+
* @returns {u256} - The resulting 256-bit unsigned integer.
|
|
3033
|
+
*/
|
|
3034
|
+
@inline
|
|
3035
|
+
static fromU64(value: u64): u256 {
|
|
3036
|
+
if (value == 0) return u256.Zero;
|
|
3037
|
+
if (value == 1) return u256.One;
|
|
3038
|
+
return new u256(value);
|
|
3039
|
+
}
|
|
3040
|
+
|
|
3041
|
+
/**
|
|
3042
|
+
* Creates a new \`u256\` from a 64-bit signed integer (sign-extends).
|
|
3043
|
+
* @param {i64} value - The 64-bit signed integer.
|
|
3044
|
+
* @returns {u256} - The resulting 256-bit unsigned integer.
|
|
3045
|
+
*/
|
|
3046
|
+
@inline
|
|
3047
|
+
static fromI64(value: i64): u256 {
|
|
3048
|
+
if (value == 0) return u256.Zero;
|
|
3049
|
+
if (value == 1) return u256.One;
|
|
3050
|
+
if (value == -1) return u256.Max;
|
|
3051
|
+
let mask = value >> 63;
|
|
3052
|
+
return new u256(<u64>value, mask, mask, mask);
|
|
3053
|
+
}
|
|
3054
|
+
|
|
3055
|
+
/**
|
|
3056
|
+
* Creates a new \`u256\` from a 32-bit unsigned integer.
|
|
3057
|
+
* @param {u32} value - The 32-bit unsigned integer.
|
|
3058
|
+
* @returns {u256} - The resulting 256-bit unsigned integer.
|
|
3059
|
+
*/
|
|
3060
|
+
@inline
|
|
3061
|
+
static fromU32(value: u32): u256 {
|
|
3062
|
+
if (value == 0) return u256.Zero;
|
|
3063
|
+
if (value == 1) return u256.One;
|
|
3064
|
+
return new u256(value);
|
|
3065
|
+
}
|
|
3066
|
+
|
|
3067
|
+
/**
|
|
3068
|
+
* Creates a new \`u256\` from a 32-bit signed integer (sign-extends).
|
|
3069
|
+
* @param {i32} value - The 32-bit signed integer.
|
|
3070
|
+
* @returns {u256} - The resulting 256-bit unsigned integer.
|
|
3071
|
+
*/
|
|
3072
|
+
@inline
|
|
3073
|
+
static fromI32(value: i32): u256 {
|
|
3074
|
+
if (value == 0) return u256.Zero;
|
|
3075
|
+
if (value == 1) return u256.One;
|
|
3076
|
+
if (value == -1) return u256.Max;
|
|
3077
|
+
let mask: u64 = value >> 63;
|
|
3078
|
+
return new u256(value, mask, mask, mask);
|
|
3079
|
+
}
|
|
3080
|
+
|
|
3081
|
+
/**
|
|
3082
|
+
* Creates a new \`u256\` from 8 separate 32-bit segments (lower to higher).
|
|
3083
|
+
* @param {u32} l0 - Lower 32 bits of the first 64-bit segment.
|
|
3084
|
+
* @param {u32} l1 - Higher 32 bits of the first 64-bit segment.
|
|
3085
|
+
* @param {u32} l2 - Lower 32 bits of the second 64-bit segment.
|
|
3086
|
+
* @param {u32} l3 - Higher 32 bits of the second 64-bit segment.
|
|
3087
|
+
* @param {u32} h0 - Lower 32 bits of the third 64-bit segment.
|
|
3088
|
+
* @param {u32} h1 - Higher 32 bits of the third 64-bit segment.
|
|
3089
|
+
* @param {u32} h2 - Lower 32 bits of the fourth 64-bit segment.
|
|
3090
|
+
* @param {u32} h3 - Higher 32 bits of the fourth 64-bit segment.
|
|
3091
|
+
* @returns {u256} - The resulting 256-bit unsigned integer.
|
|
3092
|
+
*/
|
|
3093
|
+
@inline
|
|
3094
|
+
static fromBits(
|
|
3095
|
+
l0: u32, l1: u32, l2: u32, l3: u32,
|
|
3096
|
+
h0: u32, h1: u32, h2: u32, h3: u32,
|
|
3097
|
+
): u256 {
|
|
3098
|
+
return new u256(
|
|
3099
|
+
<u64>l0 | ((<u64>l1) << 32),
|
|
3100
|
+
<u64>l2 | ((<u64>l3) << 32),
|
|
3101
|
+
<u64>h0 | ((<u64>h1) << 32),
|
|
3102
|
+
<u64>h2 | ((<u64>h3) << 32),
|
|
3103
|
+
);
|
|
3104
|
+
}
|
|
3105
|
+
|
|
3106
|
+
/**
|
|
3107
|
+
* Creates a new \`u256\` from an array of bytes or \`Uint8Array\`.
|
|
3108
|
+
* @param {T} array - The source array of bytes (u8[] or Uint8Array).
|
|
3109
|
+
* @param {boolean} [bigEndian=false] - Whether the bytes are in big-endian format.
|
|
3110
|
+
* @throws {TypeError} If the generic type \`T\` is not supported.
|
|
3111
|
+
* @returns {u256} - The resulting \`u256\`.
|
|
3112
|
+
*/
|
|
3113
|
+
@inline
|
|
3114
|
+
static fromBytes<T>(array: T, bigEndian: bool = false): u256 {
|
|
3115
|
+
// @ts-ignore
|
|
3116
|
+
if (array instanceof u8[]) {
|
|
3117
|
+
return bigEndian
|
|
3118
|
+
// @ts-ignore
|
|
3119
|
+
? u256.fromBytesBE(<u8[]>array)
|
|
3120
|
+
// @ts-ignore
|
|
3121
|
+
: u256.fromBytesLE(<u8[]>array);
|
|
3122
|
+
} else if (array instanceof Uint8Array) {
|
|
3123
|
+
return bigEndian
|
|
3124
|
+
? u256.fromUint8ArrayBE(<Uint8Array>array)
|
|
3125
|
+
: u256.fromUint8ArrayLE(<Uint8Array>array);
|
|
3126
|
+
} else {
|
|
3127
|
+
throw new TypeError("Unsupported generic type");
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
|
|
3131
|
+
/**
|
|
3132
|
+
* Creates a new \`u256\` from a little-endian array of bytes.
|
|
3133
|
+
* @param {u8[]} array - A 32-byte array (little-endian).
|
|
3134
|
+
* @returns {u256} - The resulting \`u256\`.
|
|
3135
|
+
*/
|
|
3136
|
+
@inline
|
|
3137
|
+
static fromBytesLE(array: u8[]): u256 {
|
|
3138
|
+
if (array.length !== 32) throw new Error("Invalid length");
|
|
3139
|
+
let buffer = array.dataStart;
|
|
3140
|
+
return new u256(
|
|
3141
|
+
load<u64>(buffer, 0),
|
|
3142
|
+
load<u64>(buffer, 8),
|
|
3143
|
+
load<u64>(buffer, 16),
|
|
3144
|
+
load<u64>(buffer, 24),
|
|
3145
|
+
);
|
|
3146
|
+
}
|
|
3147
|
+
|
|
3148
|
+
/**
|
|
3149
|
+
* Creates a new \`u256\` from a big-endian array of bytes.
|
|
3150
|
+
* @param {u8[]} array - A 32-byte array (big-endian).
|
|
3151
|
+
* @returns {u256} - The resulting \`u256\`.
|
|
3152
|
+
*/
|
|
3153
|
+
@inline
|
|
3154
|
+
static fromBytesBE(array: u8[]): u256 {
|
|
3155
|
+
if (array.length !== 32) throw new Error("Invalid length");
|
|
3156
|
+
let buffer = array.dataStart;
|
|
3157
|
+
return new u256(
|
|
3158
|
+
bswap<u64>(load<u64>(buffer, 24)),
|
|
3159
|
+
bswap<u64>(load<u64>(buffer, 16)),
|
|
3160
|
+
bswap<u64>(load<u64>(buffer, 8)),
|
|
3161
|
+
bswap<u64>(load<u64>(buffer, 0))
|
|
3162
|
+
);
|
|
3163
|
+
}
|
|
3164
|
+
|
|
3165
|
+
/**
|
|
3166
|
+
* Creates a new \`u256\` from a little-endian \`Uint8Array\`.
|
|
3167
|
+
* @param {Uint8Array} array - A 32-byte \`Uint8Array\` (little-endian).
|
|
3168
|
+
* @returns {u256} - The resulting \`u256\`.
|
|
3169
|
+
*/
|
|
3170
|
+
@inline
|
|
3171
|
+
static fromUint8ArrayLE(array: Uint8Array): u256 {
|
|
3172
|
+
if (array.length !== 32) throw new Error("Invalid length");
|
|
3173
|
+
let buffer = array.dataStart;
|
|
3174
|
+
return new u256(
|
|
3175
|
+
load<u64>(buffer, 0),
|
|
3176
|
+
load<u64>(buffer, 8),
|
|
3177
|
+
load<u64>(buffer, 16),
|
|
3178
|
+
load<u64>(buffer, 24)
|
|
3179
|
+
);
|
|
3180
|
+
}
|
|
3181
|
+
|
|
3182
|
+
/**
|
|
3183
|
+
* Creates a new \`u256\` from a big-endian \`Uint8Array\`.
|
|
3184
|
+
* @param {Uint8Array} array - A 32-byte \`Uint8Array\` (big-endian).
|
|
3185
|
+
* @returns {u256} - The resulting \`u256\`.
|
|
3186
|
+
*/
|
|
3187
|
+
@inline
|
|
3188
|
+
static fromUint8ArrayBE(array: Uint8Array): u256 {
|
|
3189
|
+
if (array.length !== 32) throw new Error("Invalid length");
|
|
3190
|
+
let buffer = array.dataStart;
|
|
3191
|
+
return new u256(
|
|
3192
|
+
bswap<u64>(load<u64>(buffer, 24)),
|
|
3193
|
+
bswap<u64>(load<u64>(buffer, 16)),
|
|
3194
|
+
bswap<u64>(load<u64>(buffer, 8)),
|
|
3195
|
+
bswap<u64>(load<u64>(buffer, 0))
|
|
3196
|
+
);
|
|
3197
|
+
}
|
|
3198
|
+
|
|
3199
|
+
/**
|
|
3200
|
+
* Creates a new \`u256\` from a 64-bit floating-point number.
|
|
3201
|
+
* NOTE: This is only correct up to about 2^53.
|
|
3202
|
+
* @param {f64} value - The 64-bit float value.
|
|
3203
|
+
* @returns {u256} - The resulting \`u256\`.
|
|
3204
|
+
*/
|
|
3205
|
+
@inline
|
|
3206
|
+
static fromF64(value: f64): u256 {
|
|
3207
|
+
let mask = u64(reinterpret<i64>(value) >> 63);
|
|
3208
|
+
return new u256(<u64>value, mask, mask, mask);
|
|
3209
|
+
}
|
|
3210
|
+
|
|
3211
|
+
/**
|
|
3212
|
+
* Creates a new \`u256\` from a 32-bit floating-point number.
|
|
3213
|
+
* NOTE: This is only correct up to about 2^23.
|
|
3214
|
+
* @param {f32} value - The 32-bit float value.
|
|
3215
|
+
* @returns {u256} - The resulting \`u256\`.
|
|
3216
|
+
*/
|
|
3217
|
+
@inline
|
|
3218
|
+
static fromF32(value: f32): u256 {
|
|
3219
|
+
let mask = u64(reinterpret<i32>(value) >> 31);
|
|
3220
|
+
return new u256(<u64>value, mask, mask, mask);
|
|
3221
|
+
}
|
|
3222
|
+
|
|
3223
|
+
/**
|
|
3224
|
+
* Creates a new \`u256\` from a generic value \`T\`.
|
|
3225
|
+
* Supported types: bool, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, u128, u256, u8[], Uint8Array.
|
|
3226
|
+
* @param {T} value - The source value.
|
|
3227
|
+
* @throws {TypeError} If the type \`T\` is not supported.
|
|
3228
|
+
* @returns {u256} - The resulting \`u256\`.
|
|
3229
|
+
*/
|
|
3230
|
+
@inline
|
|
3231
|
+
static from<T>(value: T): u256 {
|
|
3232
|
+
if (value instanceof bool) return u256.fromU64(<u64>value);
|
|
3233
|
+
else if (value instanceof i8) return u256.fromI64(<i64>value);
|
|
3234
|
+
else if (value instanceof u8) return u256.fromU64(<u64>value);
|
|
3235
|
+
else if (value instanceof i16) return u256.fromI64(<i64>value);
|
|
3236
|
+
else if (value instanceof u16) return u256.fromU64(<u64>value);
|
|
3237
|
+
else if (value instanceof i32) return u256.fromI64(<i64>value);
|
|
3238
|
+
else if (value instanceof u32) return u256.fromU64(<u64>value);
|
|
3239
|
+
else if (value instanceof i64) return u256.fromI64(<i64>value);
|
|
3240
|
+
else if (value instanceof u64) return u256.fromU64(<u64>value);
|
|
3241
|
+
else if (value instanceof f32) return u256.fromF64(<f64>value);
|
|
3242
|
+
else if (value instanceof f64) return u256.fromF64(<f64>value);
|
|
3243
|
+
else if (value instanceof u128) return u256.fromU128(<u128>value);
|
|
3244
|
+
else if (value instanceof u256) return u256.fromU256(<u256>value);
|
|
3245
|
+
else if (value instanceof u8[]) return u256.fromBytes(<u8[]>value);
|
|
3246
|
+
else if (value instanceof Uint8Array) return u256.fromBytes(<Uint8Array>value);
|
|
3247
|
+
else throw new TypeError("Unsupported generic type");
|
|
3248
|
+
}
|
|
3249
|
+
|
|
3250
|
+
/**
|
|
3251
|
+
* Parses a string in decimal or hexadecimal format into a \`u256\`.
|
|
3252
|
+
* @param {string} str - The input string.
|
|
3253
|
+
* @param {i32} [radix=10] - The base (10 for decimal, 16 for hexadecimal).
|
|
3254
|
+
* @returns {u256} - The resulting \`u256\`.
|
|
3255
|
+
*/
|
|
3256
|
+
@inline
|
|
3257
|
+
static fromString(str: string, radix: i32 = 10): u256 {
|
|
3258
|
+
assert(radix == 10 || radix == 16, "radix must be 10 or 16");
|
|
3259
|
+
let len = str.length;
|
|
3260
|
+
if (len == 0) return u256.Zero;
|
|
3261
|
+
let result = u256.Zero; // Use cached Zero
|
|
3262
|
+
if (radix == 10) {
|
|
3263
|
+
for (let i = 0; i < len; i++) {
|
|
3264
|
+
let c = str.charCodeAt(i);
|
|
3265
|
+
assert(c >= 0x30 && c <= 0x39, "Invalid decimal digit");
|
|
3266
|
+
let digit: u64 = c - 0x30;
|
|
3267
|
+
result = result * u256.from(10) + u256.from(digit);
|
|
3268
|
+
}
|
|
3269
|
+
} else {
|
|
3270
|
+
for (let i = 0; i < len; i++) {
|
|
3271
|
+
let c = str.charCodeAt(i);
|
|
3272
|
+
let digit: u64 = 0;
|
|
3273
|
+
if (c >= 0x30 && c <= 0x39) digit = c - 0x30;
|
|
3274
|
+
else if (c >= 0x41 && c <= 0x46) digit = c - 0x41 + 10;
|
|
3275
|
+
else if (c >= 0x61 && c <= 0x66) digit = c - 0x61 + 10;
|
|
3276
|
+
else assert(false, "Invalid hexadecimal digit");
|
|
3277
|
+
result = result * u256.from(16) + u256.from(digit);
|
|
3278
|
+
}
|
|
3279
|
+
}
|
|
3280
|
+
return result;
|
|
3281
|
+
}
|
|
3282
|
+
|
|
3283
|
+
/**
|
|
3284
|
+
* Checks if a given \`u256\` value is zero using the \`!\` operator.
|
|
3285
|
+
* @param {u256} value - The \`u256\` to check.
|
|
3286
|
+
* @returns {boolean} - True if zero, false otherwise.
|
|
3287
|
+
*/
|
|
3288
|
+
@inline @operator.prefix('!')
|
|
3289
|
+
static isEmpty(value: u256): bool {
|
|
3290
|
+
return value.isZero();
|
|
3291
|
+
}
|
|
3292
|
+
|
|
3293
|
+
/**
|
|
3294
|
+
* Adds two \`u256\` values and returns a new \`u256\` with the sum.
|
|
3295
|
+
* @param {u256} a - The first \`u256\` addend.
|
|
3296
|
+
* @param {u256} b - The second \`u256\` addend.
|
|
3297
|
+
* @returns {u256} - The sum of \`a\` and \`b\`.
|
|
3298
|
+
*/
|
|
3299
|
+
@operator('+')
|
|
3300
|
+
static add(a: u256, b: u256): u256 {
|
|
3301
|
+
let lo1 = add64Local(a.lo1, b.lo1, 0);
|
|
3302
|
+
let c1 = __u256carry;
|
|
3303
|
+
let lo2 = add64Local(a.lo2, b.lo2, c1);
|
|
3304
|
+
let c2 = __u256carry;
|
|
3305
|
+
let hi1 = add64Local(a.hi1, b.hi1, c2);
|
|
3306
|
+
let c3 = __u256carry;
|
|
3307
|
+
let hi2 = add64Local(a.hi2, b.hi2, c3);
|
|
3308
|
+
return new u256(lo1, lo2, hi1, hi2);
|
|
3309
|
+
}
|
|
3310
|
+
|
|
3311
|
+
/**
|
|
3312
|
+
* Divides \`a\` by \`b\` (integer division).
|
|
3313
|
+
* @param {u256} a - The dividend.
|
|
3314
|
+
* @param {u256} b - The divisor.
|
|
3315
|
+
* @throws {Error} If \`b\` is zero.
|
|
3316
|
+
* @returns {u256} - The quotient of the division.
|
|
3317
|
+
* Optimized Division.
|
|
3318
|
+
* Performs division in-place on clones to minimize object allocation.
|
|
3319
|
+
*/
|
|
3320
|
+
@operator('/')
|
|
3321
|
+
public static div(a: u256, b: u256): u256 {
|
|
3322
|
+
if (b.isZero()) throw new Error('Division by zero');
|
|
3323
|
+
if (a.isZero() || u256.lt(a, b)) return u256.Zero; // Return cached Zero
|
|
3324
|
+
if (u256.eq(a, b)) return u256.One; // Return cached One
|
|
3325
|
+
|
|
3326
|
+
// Clone to mutate in-place
|
|
3327
|
+
let n = a.clone();
|
|
3328
|
+
let d = b.clone();
|
|
3329
|
+
let result = u256.Zero.clone(); // Start with fresh Zero
|
|
3330
|
+
|
|
3331
|
+
const shift = u256.clz(d) - u256.clz(n);
|
|
3332
|
+
|
|
3333
|
+
// Shift d left by 'shift'
|
|
3334
|
+
// Since d is a clone, we can mutate/reassign it
|
|
3335
|
+
d = u256.shl(d, shift);
|
|
3336
|
+
|
|
3337
|
+
// Unrolled restore division
|
|
3338
|
+
for (let i = shift; i >= 0; i--) {
|
|
3339
|
+
if (u256.ge(n, d)) {
|
|
3340
|
+
n.subAssign(d); // In-place subtract
|
|
3341
|
+
result.orAssignBit(i); // In-place bit set
|
|
3342
|
+
}
|
|
3343
|
+
d.rshiftOne(); // In-place right shift by 1
|
|
3344
|
+
}
|
|
3345
|
+
return result;
|
|
3346
|
+
}
|
|
3347
|
+
|
|
3348
|
+
/**
|
|
3349
|
+
* Left-shifts a \`u256\` by the specified number of bits.
|
|
3350
|
+
* @param {u256} value - The \`u256\` to shift.
|
|
3351
|
+
* @param {i32} shift - The number of bits to shift (modulo 256).
|
|
3352
|
+
* @returns {u256} - The shifted \`u256\`.
|
|
3353
|
+
* Optimized Left Shift (Massacred allocation overhead).
|
|
3354
|
+
* Unrolls the logic into local variables to avoid Array<u64> allocation.
|
|
3355
|
+
*/
|
|
3356
|
+
@operator('<<')
|
|
3357
|
+
public static shl(value: u256, shift: i32): u256 {
|
|
3358
|
+
if (shift <= 0) return shift == 0 ? value.clone() : u256.Zero; // Return cached
|
|
3359
|
+
if (shift >= 256) return u256.Zero; // Return cached
|
|
3360
|
+
|
|
3361
|
+
shift &= 255;
|
|
3362
|
+
|
|
3363
|
+
const bits = 64;
|
|
3364
|
+
const segShift = (shift / bits) | 0;
|
|
3365
|
+
const bitShift = shift % bits;
|
|
3366
|
+
const invShift = bits - bitShift;
|
|
3367
|
+
|
|
3368
|
+
let r0: u64 = 0, r1: u64 = 0, r2: u64 = 0, r3: u64 = 0;
|
|
3369
|
+
const i0 = value.lo1, i1 = value.lo2, i2 = value.hi1, i3 = value.hi2;
|
|
3370
|
+
|
|
3371
|
+
if (segShift == 0) {
|
|
3372
|
+
r0 = i0 << bitShift;
|
|
3373
|
+
r1 = (i1 << bitShift) | (bitShift == 0 ? 0 : i0 >>> invShift);
|
|
3374
|
+
r2 = (i2 << bitShift) | (bitShift == 0 ? 0 : i1 >>> invShift);
|
|
3375
|
+
r3 = (i3 << bitShift) | (bitShift == 0 ? 0 : i2 >>> invShift);
|
|
3376
|
+
} else if (segShift == 1) {
|
|
3377
|
+
r1 = i0 << bitShift;
|
|
3378
|
+
r2 = (i1 << bitShift) | (bitShift == 0 ? 0 : i0 >>> invShift);
|
|
3379
|
+
r3 = (i2 << bitShift) | (bitShift == 0 ? 0 : i1 >>> invShift);
|
|
3380
|
+
} else if (segShift == 2) {
|
|
3381
|
+
r2 = i0 << bitShift;
|
|
3382
|
+
r3 = (i1 << bitShift) | (bitShift == 0 ? 0 : i0 >>> invShift);
|
|
3383
|
+
} else if (segShift == 3) {
|
|
3384
|
+
r3 = i0 << bitShift;
|
|
3385
|
+
}
|
|
3386
|
+
|
|
3387
|
+
return new u256(r0, r1, r2, r3);
|
|
3388
|
+
}
|
|
3389
|
+
|
|
3390
|
+
/**
|
|
3391
|
+
* Subtracts \`b\` from \`a\` and returns a new \`u256\`.
|
|
3392
|
+
* @param {u256} a - The minuend.
|
|
3393
|
+
* @param {u256} b - The subtrahend.
|
|
3394
|
+
* @returns {u256} - The difference \`a - b\`.
|
|
3395
|
+
*/
|
|
3396
|
+
@operator('-')
|
|
3397
|
+
static sub(a: u256, b: u256): u256 {
|
|
3398
|
+
let lo1 = sub64(a.lo1, b.lo1, 0);
|
|
3399
|
+
let b1 = __u256carrySub;
|
|
3400
|
+
let lo2 = sub64(a.lo2, b.lo2, b1);
|
|
3401
|
+
let b2 = __u256carrySub;
|
|
3402
|
+
let hi1 = sub64(a.hi1, b.hi1, b2);
|
|
3403
|
+
let b3 = __u256carrySub;
|
|
3404
|
+
let hi2 = sub64(a.hi2, b.hi2, b3);
|
|
3405
|
+
return new u256(lo1, lo2, hi1, hi2);
|
|
3406
|
+
}
|
|
3407
|
+
|
|
3408
|
+
/**
|
|
3409
|
+
* Bitwise OR between two \`u256\` values.
|
|
3410
|
+
* @param {u256} a - The first operand.
|
|
3411
|
+
* @param {u256} b - The second operand.
|
|
3412
|
+
* @returns {u256} - The bitwise OR result.
|
|
3413
|
+
*/
|
|
3414
|
+
@inline @operator('|')
|
|
3415
|
+
static or(a: u256, b: u256): u256 {
|
|
3416
|
+
return new u256(a.lo1 | b.lo1, a.lo2 | b.lo2, a.hi1 | b.hi1, a.hi2 | b.hi2);
|
|
3417
|
+
}
|
|
3418
|
+
|
|
3419
|
+
/**
|
|
3420
|
+
* Bitwise XOR between two \`u256\` values.
|
|
3421
|
+
* @param {u256} a - The first operand.
|
|
3422
|
+
* @param {u256} b - The second operand.
|
|
3423
|
+
* @returns {u256} - The bitwise XOR result.
|
|
3424
|
+
*/
|
|
3425
|
+
@inline @operator('^')
|
|
3426
|
+
static xor(a: u256, b: u256): u256 {
|
|
3427
|
+
return new u256(a.lo1 ^ b.lo1, a.lo2 ^ b.lo2, a.hi1 ^ b.hi1, a.hi2 ^ b.hi2);
|
|
3428
|
+
}
|
|
3429
|
+
|
|
3430
|
+
/**
|
|
3431
|
+
* Bitwise AND between two \`u256\` values.
|
|
3432
|
+
* @param {u256} a - The first operand.
|
|
3433
|
+
* @param {u256} b - The second operand.
|
|
3434
|
+
* @returns {u256} - The bitwise AND result.
|
|
3435
|
+
*/
|
|
3436
|
+
@inline @operator('&')
|
|
3437
|
+
static and(a: u256, b: u256): u256 {
|
|
3438
|
+
return new u256(a.lo1 & b.lo1, a.lo2 & b.lo2, a.hi1 & b.hi1, a.hi2 & b.hi2);
|
|
3439
|
+
}
|
|
3440
|
+
|
|
3441
|
+
/**
|
|
3442
|
+
* Right-shifts a \`u256\` by the specified number of bits (logical shift).
|
|
3443
|
+
* @param {u256} value - The \`u256\` to shift.
|
|
3444
|
+
* @param {i32} shift - The number of bits to shift (modulo 256).
|
|
3445
|
+
* @returns {u256} - The shifted \`u256\`.
|
|
3446
|
+
*/
|
|
3447
|
+
@operator('>>')
|
|
3448
|
+
static shr(value: u256, shift: i32): u256 {
|
|
3449
|
+
// If shift <= 0 => shift=0 means "no shift," shift<0 is arguably invalid.
|
|
3450
|
+
// We just return a if shift=0, else zero if negative. Adjust to your preference.
|
|
3451
|
+
if (shift <= 0) {
|
|
3452
|
+
return shift == 0 ? value.clone() : u256.Zero;
|
|
3453
|
+
}
|
|
3454
|
+
|
|
3455
|
+
// If shift >= 256 => the result is zero for a 256-bit integer
|
|
3456
|
+
if (shift >= 256) {
|
|
3457
|
+
return u256.Zero;
|
|
3458
|
+
}
|
|
3459
|
+
|
|
3460
|
+
// Now shift is in [1..255]
|
|
3461
|
+
// Some code likes to do shift &= 255; but after the above check, it\u2019s no longer necessary.
|
|
3462
|
+
// We'll keep it for consistency:
|
|
3463
|
+
shift &= 255;
|
|
3464
|
+
|
|
3465
|
+
// If after masking shift is 0, that means the original shift was multiple of 256, but we handled it above
|
|
3466
|
+
if (shift == 0) return value.clone();
|
|
3467
|
+
|
|
3468
|
+
const w = shift >>> 6; // how many full 64-bit words to drop
|
|
3469
|
+
const b = shift & 63; // how many bits to shift within a word
|
|
3470
|
+
|
|
3471
|
+
let lo1 = value.lo1;
|
|
3472
|
+
let lo2 = value.lo2;
|
|
3473
|
+
let hi1 = value.hi1;
|
|
3474
|
+
let hi2 = value.hi2;
|
|
3475
|
+
|
|
3476
|
+
// Shift words down by w words
|
|
3477
|
+
if (w >= 4) return u256.Zero; // Return cached
|
|
3478
|
+
else if (w == 3) { lo1 = hi2; lo2 = 0; hi1 = 0; hi2 = 0; }
|
|
3479
|
+
else if (w == 2) { lo1 = hi1; lo2 = hi2; hi1 = 0; hi2 = 0; }
|
|
3480
|
+
else if (w == 1) { lo1 = lo2; lo2 = hi1; hi1 = hi2; hi2 = 0; }
|
|
3481
|
+
|
|
3482
|
+
// Now apply the bit shift b
|
|
3483
|
+
if (b > 0) {
|
|
3484
|
+
const invB = 64 - b;
|
|
3485
|
+
lo1 = (lo1 >>> b) | (lo2 << invB);
|
|
3486
|
+
lo2 = (lo2 >>> b) | (hi1 << invB);
|
|
3487
|
+
hi1 = (hi1 >>> b) | (hi2 << invB);
|
|
3488
|
+
hi2 = hi2 >>> b;
|
|
3489
|
+
}
|
|
3490
|
+
|
|
3491
|
+
return new u256(lo1, lo2, hi1, hi2);
|
|
3492
|
+
}
|
|
3493
|
+
|
|
3494
|
+
/**
|
|
3495
|
+
* Unsigned right shift operator (same as \`shr\` for \`u256\`).
|
|
3496
|
+
* @param {u256} value - The \`u256\` to shift.
|
|
3497
|
+
* @param {i32} shift - The number of bits to shift (modulo 256).
|
|
3498
|
+
* @returns {u256} - The shifted \`u256\`.
|
|
3499
|
+
*/
|
|
3500
|
+
@inline @operator('>>>')
|
|
3501
|
+
static shr_u(value: u256, shift: i32): u256 {
|
|
3502
|
+
return u256.shr(value, shift);
|
|
3503
|
+
}
|
|
3504
|
+
|
|
3505
|
+
/**
|
|
3506
|
+
* Checks if two \`u256\` values are equal.
|
|
3507
|
+
* @param {u256} a - The first operand.
|
|
3508
|
+
* @param {u256} b - The second operand.
|
|
3509
|
+
* @returns {boolean} - True if the two values are equal, otherwise false.
|
|
3510
|
+
*/
|
|
3511
|
+
@inline @operator('==')
|
|
3512
|
+
static eq(a: u256, b: u256): bool {
|
|
3513
|
+
return a.lo1 == b.lo1 && a.lo2 == b.lo2 && a.hi1 == b.hi1 && a.hi2 == b.hi2;
|
|
3514
|
+
}
|
|
3515
|
+
|
|
3516
|
+
/**
|
|
3517
|
+
* Checks if two \`u256\` values are not equal.
|
|
3518
|
+
* @param {u256} a - The first operand.
|
|
3519
|
+
* @param {u256} b - The second operand.
|
|
3520
|
+
* @returns {boolean} - True if the two values differ, otherwise false.
|
|
3521
|
+
*/
|
|
3522
|
+
@inline @operator('!=')
|
|
3523
|
+
static ne(a: u256, b: u256): bool {
|
|
3524
|
+
return !u256.eq(a, b);
|
|
3525
|
+
}
|
|
3526
|
+
|
|
3527
|
+
/**
|
|
3528
|
+
* Checks if \`a < b\`.
|
|
3529
|
+
* @param {u256} a - The first operand.
|
|
3530
|
+
* @param {u256} b - The second operand.
|
|
3531
|
+
* @returns {boolean} - True if \`a\` is less than \`b\`, otherwise false.
|
|
3532
|
+
*/
|
|
3533
|
+
@operator('<')
|
|
3534
|
+
static lt(a: u256, b: u256): bool {
|
|
3535
|
+
if (a.hi2 != b.hi2) return a.hi2 < b.hi2;
|
|
3536
|
+
if (a.hi1 != b.hi1) return a.hi1 < b.hi1;
|
|
3537
|
+
if (a.lo2 != b.lo2) return a.lo2 < b.lo2;
|
|
3538
|
+
return a.lo1 < b.lo1;
|
|
3539
|
+
}
|
|
3540
|
+
|
|
3541
|
+
@inline @operator('>')
|
|
3542
|
+
static gt(a: u256, b: u256): bool { return b < a; }
|
|
3543
|
+
@inline @operator('<=')
|
|
3544
|
+
static le(a: u256, b: u256): bool { return !u256.gt(a, b); }
|
|
3545
|
+
@inline @operator('>=')
|
|
3546
|
+
static ge(a: u256, b: u256): bool { return !u256.lt(a, b); }
|
|
3547
|
+
|
|
3548
|
+
@inline @operator('*')
|
|
3549
|
+
static mul(a: u256, b: u256): u256 {
|
|
3550
|
+
return __mul256(a.lo1, a.lo2, a.hi1, a.hi2, b.lo1, b.lo2, b.hi1, b.hi2);
|
|
3551
|
+
}
|
|
3552
|
+
|
|
3553
|
+
/**
|
|
3554
|
+
* Counts the number of set bits (1s) in the \`u256\`.
|
|
3555
|
+
* @param {u256} value - The \`u256\` to count bits for.
|
|
3556
|
+
* @returns {i32} - The number of bits set to 1.
|
|
3557
|
+
*/
|
|
3558
|
+
@inline
|
|
3559
|
+
static popcnt(value: u256): i32 {
|
|
3560
|
+
return <i32>(popcnt(value.lo1) + popcnt(value.lo2) + popcnt(value.hi1) + popcnt(value.hi2));
|
|
3561
|
+
}
|
|
3562
|
+
|
|
3563
|
+
/**
|
|
3564
|
+
* Counts leading zeros in the \`u256\`.
|
|
3565
|
+
* @param {u256} value - The \`u256\` to evaluate.
|
|
3566
|
+
* @returns {i32} - The number of leading zero bits.
|
|
3567
|
+
*/
|
|
3568
|
+
@inline
|
|
3569
|
+
static clz(value: u256): i32 {
|
|
3570
|
+
if (value.hi2) return <i32>clz(value.hi2);
|
|
3571
|
+
if (value.hi1) return <i32>(clz(value.hi1) + 64);
|
|
3572
|
+
if (value.lo2) return <i32>(clz(value.lo2) + 128);
|
|
3573
|
+
if (value.lo1) return <i32>(clz(value.lo1) + 192);
|
|
3574
|
+
return 256;
|
|
3575
|
+
}
|
|
3576
|
+
|
|
3577
|
+
/**
|
|
3578
|
+
* Counts trailing zeros in the \`u256\`.
|
|
3579
|
+
* @param {u256} value - The \`u256\` to evaluate.
|
|
3580
|
+
* @returns {i32} - The number of trailing zero bits.
|
|
3581
|
+
*/
|
|
3582
|
+
@inline
|
|
3583
|
+
static ctz(value: u256): i32 {
|
|
3584
|
+
if (value.lo1) return <i32>ctz(value.lo1);
|
|
3585
|
+
if (value.lo2) return <i32>(ctz(value.lo2) + 64);
|
|
3586
|
+
if (value.hi1) return <i32>(ctz(value.hi1) + 128);
|
|
3587
|
+
if (value.hi2) return <i32>(ctz(value.hi2) + 192);
|
|
3588
|
+
return 256;
|
|
3589
|
+
}
|
|
3590
|
+
|
|
3591
|
+
/**
|
|
3592
|
+
* Sets the value of this \`u256\` to another \`u256\`.
|
|
3593
|
+
* @param {u256} value - The source \`u256\`.
|
|
3594
|
+
* @returns {this} - The current instance.
|
|
3595
|
+
*/
|
|
3596
|
+
@inline
|
|
3597
|
+
set(value: u256): this {
|
|
3598
|
+
this.lo1 = value.lo1; this.lo2 = value.lo2; this.hi1 = value.hi1; this.hi2 = value.hi2;
|
|
3599
|
+
return this;
|
|
3600
|
+
}
|
|
3601
|
+
|
|
3602
|
+
/**
|
|
3603
|
+
* Sets the lower 128 bits of this \`u256\` from a \`u128\`, resetting the higher 128 bits to zero.
|
|
3604
|
+
* @param {u128} value - The source 128-bit unsigned integer.
|
|
3605
|
+
* @returns {this} - The current instance.
|
|
3606
|
+
*/
|
|
3607
|
+
@inline
|
|
3608
|
+
setU128(value: u128): this {
|
|
3609
|
+
this.lo1 = value.lo; this.lo2 = value.hi; this.hi1 = 0; this.hi2 = 0;
|
|
3610
|
+
return this;
|
|
3611
|
+
}
|
|
3612
|
+
|
|
3613
|
+
/**
|
|
3614
|
+
* Sets this \`u256\` to the sign-extended value of a 64-bit signed integer.
|
|
3615
|
+
* @param {i64} value - The 64-bit signed integer.
|
|
3616
|
+
* @returns {this} - The current instance.
|
|
3617
|
+
*/
|
|
3618
|
+
@inline
|
|
3619
|
+
setI64(value: i64): this {
|
|
3620
|
+
let mask: u64 = value >> 63;
|
|
3621
|
+
this.lo1 = <u64>value; this.lo2 = mask; this.hi1 = mask; this.hi2 = mask;
|
|
3622
|
+
return this;
|
|
3623
|
+
}
|
|
3624
|
+
|
|
3625
|
+
/**
|
|
3626
|
+
* Sets this \`u256\` to the specified 64-bit unsigned integer, clearing higher bits.
|
|
3627
|
+
* @param {u64} value - The 64-bit unsigned integer.
|
|
3628
|
+
* @returns {this} - The current instance.
|
|
3629
|
+
*/
|
|
3630
|
+
@inline
|
|
3631
|
+
setU64(value: u64): this {
|
|
3632
|
+
this.lo1 = value; this.lo2 = 0; this.hi1 = 0; this.hi2 = 0;
|
|
3633
|
+
return this;
|
|
3634
|
+
}
|
|
3635
|
+
|
|
3636
|
+
/**
|
|
3637
|
+
* Sets this \`u256\` to the sign-extended value of a 32-bit signed integer.
|
|
3638
|
+
* @param {i32} value - The 32-bit signed integer.
|
|
3639
|
+
* @returns {this} - The current instance.
|
|
3640
|
+
*/
|
|
3641
|
+
@inline
|
|
3642
|
+
setI32(value: i32): this {
|
|
3643
|
+
let mask: u64 = value >> 63;
|
|
3644
|
+
this.lo1 = value; this.lo2 = mask; this.hi1 = mask; this.hi2 = mask;
|
|
3645
|
+
return this;
|
|
3646
|
+
}
|
|
3647
|
+
|
|
3648
|
+
/**
|
|
3649
|
+
* Sets this \`u256\` to the specified 32-bit unsigned integer, clearing higher bits.
|
|
3650
|
+
* @param {u32} value - The 32-bit unsigned integer.
|
|
3651
|
+
* @returns {this} - The current instance.
|
|
3652
|
+
*/
|
|
3653
|
+
@inline
|
|
3654
|
+
setU32(value: u32): this {
|
|
3655
|
+
this.lo1 = value; this.lo2 = 0; this.hi1 = 0; this.hi2 = 0;
|
|
3656
|
+
return this;
|
|
3657
|
+
}
|
|
3658
|
+
|
|
3659
|
+
/**
|
|
3660
|
+
* Checks if this \`u256\` is equal to zero.
|
|
3661
|
+
* @returns {boolean} - True if zero, false otherwise.
|
|
3662
|
+
*/
|
|
3663
|
+
@inline
|
|
3664
|
+
isZero(): bool {
|
|
3665
|
+
return !(this.lo1 | this.lo2 | this.hi1 | this.hi2);
|
|
3666
|
+
}
|
|
3667
|
+
|
|
3668
|
+
/**
|
|
3669
|
+
* Performs a bitwise NOT operation (\`~\`) on the current \`u256\`.
|
|
3670
|
+
* @returns {u256} - A new \`u256\` with inverted bits.
|
|
3671
|
+
*/
|
|
3672
|
+
@inline @operator.prefix('~')
|
|
3673
|
+
not(): u256 {
|
|
3674
|
+
return new u256(~this.lo1, ~this.lo2, ~this.hi1, ~this.hi2);
|
|
3675
|
+
}
|
|
3676
|
+
|
|
3677
|
+
/**
|
|
3678
|
+
* A unary plus operator, returning the current \`u256\` without changes.
|
|
3679
|
+
* @returns {u256} - The current \`u256\`.
|
|
3680
|
+
*/
|
|
3681
|
+
@inline @operator.prefix('+')
|
|
3682
|
+
pos(): u256 { return this; }
|
|
3683
|
+
|
|
3684
|
+
/**
|
|
3685
|
+
* A unary negation operator (\`-x\`), computed as \`~x + 1\`.
|
|
3686
|
+
* @returns {u256} - A new \`u256\` representing the negated value (mod 2^256).
|
|
3687
|
+
*/
|
|
3688
|
+
@operator.prefix('-')
|
|
3689
|
+
neg(): u256 {
|
|
3690
|
+
let lo1 = ~this.lo1, lo2 = ~this.lo2, hi1 = ~this.hi1, hi2 = ~this.hi2;
|
|
3691
|
+
let sum = lo1 + 1;
|
|
3692
|
+
let carry = (sum < lo1) ? 1 : 0;
|
|
3693
|
+
lo1 = sum;
|
|
3694
|
+
sum = lo2 + carry;
|
|
3695
|
+
carry = (sum < lo2) ? 1 : 0;
|
|
3696
|
+
lo2 = sum;
|
|
3697
|
+
sum = hi1 + carry;
|
|
3698
|
+
carry = (sum < hi1) ? 1 : 0;
|
|
3699
|
+
hi1 = sum;
|
|
3700
|
+
hi2 = hi2 + carry;
|
|
3701
|
+
return new u256(lo1, lo2, hi1, hi2);
|
|
3702
|
+
}
|
|
3703
|
+
|
|
3704
|
+
@operator.prefix("++")
|
|
3705
|
+
public preInc(): this {
|
|
3706
|
+
let sum = this.lo1 + 1;
|
|
3707
|
+
let carry = sum < this.lo1 ? 1 : 0;
|
|
3708
|
+
this.lo1 = sum;
|
|
3709
|
+
sum = this.lo2 + carry;
|
|
3710
|
+
carry = sum < this.lo2 ? 1 : 0;
|
|
3711
|
+
this.lo2 = sum;
|
|
3712
|
+
sum = this.hi1 + carry;
|
|
3713
|
+
carry = sum < this.hi1 ? 1 : 0;
|
|
3714
|
+
this.hi1 = sum;
|
|
3715
|
+
this.hi2 += carry;
|
|
3716
|
+
return this;
|
|
3717
|
+
}
|
|
3718
|
+
|
|
3719
|
+
@operator.prefix("--")
|
|
3720
|
+
public preDec(): this {
|
|
3721
|
+
let diff = this.lo1 - 1;
|
|
3722
|
+
let borrow = (diff > this.lo1) ? 1 : 0;
|
|
3723
|
+
this.lo1 = diff;
|
|
3724
|
+
diff = this.lo2 - borrow;
|
|
3725
|
+
borrow = (diff > this.lo2) ? 1 : 0;
|
|
3726
|
+
this.lo2 = diff;
|
|
3727
|
+
diff = this.hi1 - borrow;
|
|
3728
|
+
borrow = (diff > this.hi1) ? 1 : 0;
|
|
3729
|
+
this.hi1 = diff;
|
|
3730
|
+
this.hi2 -= borrow;
|
|
3731
|
+
return this;
|
|
3732
|
+
}
|
|
3733
|
+
|
|
3734
|
+
@inline @operator.postfix('++')
|
|
3735
|
+
postInc(): u256 { return this.clone().preInc(); }
|
|
3736
|
+
|
|
3737
|
+
@inline @operator.postfix('--')
|
|
3738
|
+
postDec(): u256 { return this.clone().preDec(); }
|
|
3739
|
+
|
|
3740
|
+
/**
|
|
3741
|
+
* Converts this \`u256\` to a generic type \`T\`.
|
|
3742
|
+
* Supported types: bool, i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, u256, u8[], Uint8Array, StaticArray<u8>, string.
|
|
3743
|
+
* @returns {T} - The converted value.
|
|
3744
|
+
*/
|
|
3745
|
+
@inline
|
|
3746
|
+
as<T>(): T {
|
|
3747
|
+
if (isReference<T>()) {
|
|
3748
|
+
let typeName = nameof<T>();
|
|
3749
|
+
if (typeName.indexOf("Array<u8>") >= 0) return changetype<T>(this.toBytes());
|
|
3750
|
+
else if (typeName.indexOf("Uint8Array") >= 0) return changetype<T>(this.toUint8Array());
|
|
3751
|
+
else if (typeName.indexOf("StaticArray<u8>") >= 0) return changetype<T>(this.toStaticBytes());
|
|
3752
|
+
else if (typeName.indexOf("String") >= 0) return changetype<T>(this.toString());
|
|
3753
|
+
else if (typeName.indexOf("u128") >= 0) return changetype<T>(this.toU128());
|
|
3754
|
+
else if (typeName.indexOf("i128") >= 0) return changetype<T>(this.toI128());
|
|
3755
|
+
else if (typeName.indexOf("u256") >= 0) return changetype<T>(this.toU256());
|
|
3756
|
+
else throw new TypeError(\`Unsupported generic type \${typeName}\`);
|
|
3757
|
+
} else {
|
|
3758
|
+
if (isInteger<T>()) {
|
|
3759
|
+
if (sizeof<T>() == 1) return <T><u64>this.toU64();
|
|
3760
|
+
else if (sizeof<T>() == 2) return <T><u64>this.toU64();
|
|
3761
|
+
else if (sizeof<T>() == 4) return <T><u64>this.toU64();
|
|
3762
|
+
else if (sizeof<T>() == 8) return <T>this.toU64();
|
|
3763
|
+
else throw new TypeError(\`Unsupported generic type \${nameof<T>()}\`);
|
|
3764
|
+
} else if (isFloat<T>()) {
|
|
3765
|
+
return <T><f64>this.toU64();
|
|
3766
|
+
} else {
|
|
3767
|
+
// bool
|
|
3768
|
+
return <T>this.toBool();
|
|
3769
|
+
}
|
|
3770
|
+
}
|
|
3771
|
+
}
|
|
3772
|
+
|
|
3773
|
+
/**
|
|
3774
|
+
* Converts this \`u256\` to a signed 128-bit integer (i128).
|
|
3775
|
+
* The highest bit of the 256-bit value is sign bit for i128.
|
|
3776
|
+
* @returns {i128} - The resulting \`i128\`.
|
|
3777
|
+
*/
|
|
3778
|
+
@inline
|
|
3779
|
+
toI128(): i128 {
|
|
3780
|
+
return new i128(this.lo1, (this.lo2 & 0x7FFFFFFFFFFFFFFF) | (this.hi2 & 0x8000000000000000));
|
|
3781
|
+
}
|
|
3782
|
+
|
|
3783
|
+
/**
|
|
3784
|
+
* Converts this \`u256\` to an unsigned 128-bit integer (u128).
|
|
3785
|
+
* Truncates higher bits.
|
|
3786
|
+
* @returns {u128} - The resulting \`u128\`.
|
|
3787
|
+
*/
|
|
3788
|
+
@inline
|
|
3789
|
+
toU128(): u128 { return new u128(this.lo1, this.lo2); }
|
|
3790
|
+
|
|
3791
|
+
/**
|
|
3792
|
+
* Returns this \`u256\` itself, effectively a no-op for \`u256\`.
|
|
3793
|
+
* @returns {u256} - The current \`u256\`.
|
|
3794
|
+
*/
|
|
3795
|
+
@inline
|
|
3796
|
+
toU256(): this { return this; }
|
|
3797
|
+
|
|
3798
|
+
/**
|
|
3799
|
+
* Converts this \`u256\` to a signed 64-bit integer (i64).
|
|
3800
|
+
* Truncates and treats the highest bit of the 256-bit as sign.
|
|
3801
|
+
* @returns {i64} - The resulting 64-bit signed integer.
|
|
3802
|
+
*/
|
|
3803
|
+
@inline
|
|
3804
|
+
toI64(): i64 { return <i64>( (this.lo1 & 0x7FFFFFFFFFFFFFFF) | (this.hi2 & 0x8000000000000000) ); }
|
|
3805
|
+
|
|
3806
|
+
/**
|
|
3807
|
+
* Converts this \`u256\` to an unsigned 64-bit integer (u64).
|
|
3808
|
+
* Truncates higher bits.
|
|
3809
|
+
* @returns {u64} - The resulting 64-bit unsigned integer.
|
|
3810
|
+
*/
|
|
3811
|
+
@inline
|
|
3812
|
+
toU64(): u64 { return this.lo1; }
|
|
3813
|
+
|
|
3814
|
+
/**
|
|
3815
|
+
* Converts this \`u256\` to a signed 32-bit integer (i32).
|
|
3816
|
+
* @returns {i32} - The resulting 32-bit signed integer.
|
|
3817
|
+
*/
|
|
3818
|
+
@inline
|
|
3819
|
+
toI32(): i32 { return <i32>this.toI64(); }
|
|
3820
|
+
|
|
3821
|
+
/**
|
|
3822
|
+
* Converts this \`u256\` to an unsigned 32-bit integer (u32).
|
|
3823
|
+
* Truncates higher bits.
|
|
3824
|
+
* @returns {u32} - The resulting 32-bit unsigned integer.
|
|
3825
|
+
*/
|
|
3826
|
+
@inline
|
|
3827
|
+
toU32(): u32 { return <u32>this.lo1; }
|
|
3828
|
+
|
|
3829
|
+
/**
|
|
3830
|
+
* Checks if the \`u256\` is non-zero, then returns \`true\`; otherwise \`false\`.
|
|
3831
|
+
* @returns {boolean} - True if non-zero, otherwise false.
|
|
3832
|
+
*/
|
|
3833
|
+
@inline
|
|
3834
|
+
toBool(): bool { return <bool>(this.lo1 | this.lo2 | this.hi1 | this.hi2); }
|
|
3835
|
+
|
|
3836
|
+
/**
|
|
3837
|
+
* Converts this \`u256\` to an array of 32 bytes (u8[]).
|
|
3838
|
+
* @param {boolean} [bigEndian=false] - Output in big-endian if true, else little-endian.
|
|
3839
|
+
* @returns {u8[]} - The resulting byte array.
|
|
3840
|
+
*/
|
|
3841
|
+
@inline
|
|
3842
|
+
toBytes(bigEndian: bool = false): u8[] {
|
|
3843
|
+
let result = new Array<u8>(32);
|
|
3844
|
+
this.toArrayBuffer(changetype<usize>(result.dataStart), bigEndian);
|
|
3845
|
+
return result;
|
|
3846
|
+
}
|
|
3847
|
+
|
|
3848
|
+
/**
|
|
3849
|
+
* Converts this \`u256\` to a static array of 32 bytes (StaticArray<u8>).
|
|
3850
|
+
* @param {boolean} [bigEndian=false] - Output in big-endian if true, else little-endian.
|
|
3851
|
+
* @returns {StaticArray<u8>} - The resulting static byte array.
|
|
3852
|
+
*/
|
|
3853
|
+
@inline
|
|
3854
|
+
toStaticBytes(bigEndian: bool = false): StaticArray<u8> {
|
|
3855
|
+
let result = new StaticArray<u8>(32);
|
|
3856
|
+
this.toArrayBuffer(changetype<usize>(result), bigEndian);
|
|
3857
|
+
return result;
|
|
3858
|
+
}
|
|
3859
|
+
|
|
3860
|
+
/**
|
|
3861
|
+
* Converts this \`u256\` to a \`Uint8Array\` of length 32.
|
|
3862
|
+
* @param {boolean} [bigEndian=false] - Output in big-endian if true, else little-endian.
|
|
3863
|
+
* @returns {Uint8Array} - The resulting \`Uint8Array\`.
|
|
3864
|
+
*/
|
|
3865
|
+
@inline
|
|
3866
|
+
toUint8Array(bigEndian: bool = false): Uint8Array {
|
|
3867
|
+
let result = new Uint8Array(32);
|
|
3868
|
+
this.toArrayBuffer(changetype<usize>(result.dataStart), bigEndian);
|
|
3869
|
+
return result;
|
|
3870
|
+
}
|
|
3871
|
+
|
|
3872
|
+
/**
|
|
3873
|
+
* Creates and returns a clone (deep copy) of this \`u256\`.
|
|
3874
|
+
* @returns {u256} - A new \`u256\` instance with the same value.
|
|
3875
|
+
*/
|
|
3876
|
+
clone(): u256 { return new u256(this.lo1, this.lo2, this.hi1, this.hi2); }
|
|
3877
|
+
|
|
3878
|
+
/**
|
|
3879
|
+
* Converts this \`u256\` to a string in either decimal or hexadecimal format.
|
|
3880
|
+
* @param {i32} [radix=10] - The base (10 or 16).
|
|
3881
|
+
* @throws {Error} If \`radix\` is not 10 or 16.
|
|
3882
|
+
* @returns {string} - The string representation of the \`u256\`.
|
|
3883
|
+
*/
|
|
3884
|
+
toString(radix: i32 = 10): string {
|
|
3885
|
+
assert(radix == 10 || radix == 16, 'radix argument must be between 10 or 16');
|
|
3886
|
+
if (this.isZero()) return '0';
|
|
3887
|
+
if (radix == 16) {
|
|
3888
|
+
let result = '';
|
|
3889
|
+
let shift: i32 = 252 - (u256.clz(this) & ~3);
|
|
3890
|
+
while (shift >= 0) {
|
|
3891
|
+
// @ts-ignore
|
|
3892
|
+
result += HEX_CHARS.charAt(<i32>((this >> shift).lo1 & 15));
|
|
3893
|
+
shift -= 4;
|
|
3894
|
+
}
|
|
3895
|
+
return result;
|
|
3896
|
+
}
|
|
3897
|
+
return u256toDecimalString(this);
|
|
3898
|
+
}
|
|
3899
|
+
|
|
3900
|
+
/**
|
|
3901
|
+
* @private
|
|
3902
|
+
* Stores this \`u256\` into a buffer in little-endian format.
|
|
3903
|
+
* @param {usize} buffer - The buffer start pointer.
|
|
3904
|
+
*/
|
|
3905
|
+
// --- INTERNAL HELPERS FOR DIV OPTIMIZATION ---
|
|
3906
|
+
// These allow in-place mutation to avoid allocations inside the div loop.
|
|
3907
|
+
|
|
3908
|
+
/** In-place subtraction. Assumes this >= other. */
|
|
3909
|
+
@inline
|
|
3910
|
+
private subAssign(other: u256): void {
|
|
3911
|
+
let lo1 = sub64(this.lo1, other.lo1, 0);
|
|
3912
|
+
let b1 = __u256carrySub;
|
|
3913
|
+
let lo2 = sub64(this.lo2, other.lo2, b1);
|
|
3914
|
+
let b2 = __u256carrySub;
|
|
3915
|
+
let hi1 = sub64(this.hi1, other.hi1, b2);
|
|
3916
|
+
let b3 = __u256carrySub;
|
|
3917
|
+
let hi2 = sub64(this.hi2, other.hi2, b3);
|
|
3918
|
+
this.lo1 = lo1; this.lo2 = lo2; this.hi1 = hi1; this.hi2 = hi2;
|
|
3919
|
+
}
|
|
3920
|
+
|
|
3921
|
+
/** In-place bit set: this |= (1 << bit) */
|
|
3922
|
+
@inline
|
|
3923
|
+
private orAssignBit(bit: i32): void {
|
|
3924
|
+
const bits = 64;
|
|
3925
|
+
const seg = (bit / bits) | 0;
|
|
3926
|
+
const shift = bit % bits;
|
|
3927
|
+
// FIX: Explicitly cast literal 1 to u64 to ensure proper 64-bit shifting
|
|
3928
|
+
const mask: u64 = <u64>1 << shift;
|
|
3929
|
+
if (seg == 0) this.lo1 |= mask;
|
|
3930
|
+
else if (seg == 1) this.lo2 |= mask;
|
|
3931
|
+
else if (seg == 2) this.hi1 |= mask;
|
|
3932
|
+
else if (seg == 3) this.hi2 |= mask;
|
|
3933
|
+
}
|
|
3934
|
+
|
|
3935
|
+
/** In-place right shift by 1. */
|
|
3936
|
+
@inline
|
|
3937
|
+
private rshiftOne(): void {
|
|
3938
|
+
this.lo1 = (this.lo1 >>> 1) | (this.lo2 << 63);
|
|
3939
|
+
this.lo2 = (this.lo2 >>> 1) | (this.hi1 << 63);
|
|
3940
|
+
this.hi1 = (this.hi1 >>> 1) | (this.hi2 << 63);
|
|
3941
|
+
this.hi2 = (this.hi2 >>> 1);
|
|
3942
|
+
}
|
|
3943
|
+
|
|
3944
|
+
@inline
|
|
3945
|
+
private toArrayBufferLE(buffer: usize): void {
|
|
3946
|
+
store<u64>(buffer, this.lo1, 0);
|
|
3947
|
+
store<u64>(buffer, this.lo2, 8);
|
|
3948
|
+
store<u64>(buffer, this.hi1, 16);
|
|
3949
|
+
store<u64>(buffer, this.hi2, 24);
|
|
3950
|
+
}
|
|
3951
|
+
|
|
3952
|
+
/**
|
|
3953
|
+
* @private
|
|
3954
|
+
* Stores this \`u256\` into a buffer in big-endian format.
|
|
3955
|
+
* @param {usize} buffer - The buffer start pointer.
|
|
3956
|
+
*/
|
|
3957
|
+
@inline
|
|
3958
|
+
private toArrayBufferBE(buffer: usize): void {
|
|
3959
|
+
store<u64>(buffer, bswap(this.hi2), 0);
|
|
3960
|
+
store<u64>(buffer, bswap(this.hi1), 8);
|
|
3961
|
+
store<u64>(buffer, bswap(this.lo2), 16);
|
|
3962
|
+
store<u64>(buffer, bswap(this.lo1), 24);
|
|
3963
|
+
}
|
|
3964
|
+
|
|
3965
|
+
/**
|
|
3966
|
+
* @private
|
|
3967
|
+
* Stores this \`u256\` into a buffer in either little-endian or big-endian format.
|
|
3968
|
+
* @param {usize} buffer - The buffer start pointer.
|
|
3969
|
+
* @param {boolean} [bigEndian=false] - Use big-endian if true, else little-endian.
|
|
3970
|
+
*/
|
|
3971
|
+
@inline
|
|
3972
|
+
private toArrayBuffer(buffer: usize, bigEndian: bool = false): void {
|
|
3973
|
+
if (bigEndian) this.toArrayBufferBE(buffer);
|
|
3974
|
+
else this.toArrayBufferLE(buffer);
|
|
3975
|
+
}
|
|
3976
|
+
}
|
|
3977
|
+
`,"bignum/utils":`import { CharCode } from "util/string";
|
|
3978
|
+
import { u128 } from "./integer/u128";
|
|
3979
|
+
import { u256 } from "./integer/u256";
|
|
3980
|
+
|
|
3981
|
+
// @ts-ignore: decorator
|
|
3982
|
+
@lazy const MaxBaseForExponent128 = memory.data<u64>([
|
|
3983
|
+
u64.MAX_VALUE, // 0
|
|
3984
|
+
u64.MAX_VALUE, // 1
|
|
3985
|
+
u64.MAX_VALUE, // 2
|
|
3986
|
+
0x000006597FA94F5B, // 3
|
|
3987
|
+
0x00000000FFFFFFFF, // 4
|
|
3988
|
+
0x0000000003080C00, // 5
|
|
3989
|
+
0x0000000000285145, // 6
|
|
3990
|
+
0x000000000004E045, // 7
|
|
3991
|
+
0x000000000000FFFF, // 8
|
|
3992
|
+
0x0000000000004AA8, // 9
|
|
3993
|
+
0x0000000000001BDB, // 10
|
|
3994
|
+
0x0000000000000C6F, // 11
|
|
3995
|
+
0x0000000000000659, // 12
|
|
3996
|
+
0x0000000000000398, // 13
|
|
3997
|
+
0x0000000000000235, // 14
|
|
3998
|
+
0x0000000000000172, // 15
|
|
3999
|
+
0x00000000000000FF, // 16
|
|
4000
|
+
0x00000000000000B8, // 17
|
|
4001
|
+
0x000000000000008A, // 18
|
|
4002
|
+
0x000000000000006A, // 19
|
|
4003
|
+
0x0000000000000054, // 20
|
|
4004
|
+
0x0000000000000044, // 21
|
|
4005
|
+
0x0000000000000038, // 22
|
|
4006
|
+
0x000000000000002F, // 23
|
|
4007
|
+
0x0000000000000028, // 24
|
|
4008
|
+
0x0000000000000022, // 25
|
|
4009
|
+
0x000000000000001E, // 26
|
|
4010
|
+
0x000000000000001A, // 27
|
|
4011
|
+
0x0000000000000017, // 28
|
|
4012
|
+
0x0000000000000015, // 29
|
|
4013
|
+
0x0000000000000013, // 30
|
|
4014
|
+
0x0000000000000011, // 31
|
|
4015
|
+
0x000000000000000F, // 32
|
|
4016
|
+
0x000000000000000E, // 33
|
|
4017
|
+
0x000000000000000D, // 34
|
|
4018
|
+
0x000000000000000C, // 35
|
|
4019
|
+
0x000000000000000B, // 36
|
|
4020
|
+
0x000000000000000B, // 37
|
|
4021
|
+
0x000000000000000A, // 38
|
|
4022
|
+
]);
|
|
4023
|
+
|
|
4024
|
+
// Use LUT wrapped by function for lazy compilation
|
|
4025
|
+
// @ts-ignore: decorator
|
|
4026
|
+
@lazy const RadixCharsTable = memory.data<u8>([
|
|
4027
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 36, 36, 36, 36, 36, 36,
|
|
4028
|
+
36, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
|
4029
|
+
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 36, 36, 36,
|
|
4030
|
+
36, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
|
4031
|
+
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35
|
|
4032
|
+
]);
|
|
4033
|
+
|
|
4034
|
+
// @ts-ignore: decorator
|
|
4035
|
+
@inline export function isPowerOverflow128(base: u128, exponent: i32): bool {
|
|
4036
|
+
// never overflow
|
|
4037
|
+
if (exponent <= 1 || base <= u128.One) {
|
|
4038
|
+
return false;
|
|
4039
|
+
}
|
|
4040
|
+
// always overflow
|
|
4041
|
+
if (base.hi != 0 || exponent >= 128) {
|
|
4042
|
+
return true;
|
|
4043
|
+
}
|
|
4044
|
+
var low = base.lo;
|
|
4045
|
+
if (low <= 10) {
|
|
4046
|
+
switch (<i32>low) {
|
|
4047
|
+
case 2:
|
|
4048
|
+
return exponent > 127;
|
|
4049
|
+
case 3:
|
|
4050
|
+
return exponent > 80;
|
|
4051
|
+
case 4:
|
|
4052
|
+
return exponent > 63;
|
|
4053
|
+
case 5:
|
|
4054
|
+
return exponent > 55;
|
|
4055
|
+
case 6:
|
|
4056
|
+
return exponent > 49;
|
|
4057
|
+
case 7:
|
|
4058
|
+
return exponent > 45;
|
|
4059
|
+
case 8:
|
|
4060
|
+
return exponent > 42;
|
|
4061
|
+
case 9:
|
|
4062
|
+
return exponent > 40;
|
|
4063
|
+
case 10:
|
|
4064
|
+
return exponent > 38;
|
|
4065
|
+
}
|
|
4066
|
+
}
|
|
4067
|
+
if (exponent >= 38) return true;
|
|
4068
|
+
return low > load<u64>(MaxBaseForExponent128 + (exponent << 3));
|
|
4069
|
+
}
|
|
4070
|
+
|
|
4071
|
+
// helper function for utoa
|
|
4072
|
+
function processU64(digits: Uint8Array, value: u64): void {
|
|
4073
|
+
var length = digits.length - 1;
|
|
4074
|
+
for (let i = 63; i != -1; --i) {
|
|
4075
|
+
for (let j = 0; j <= length; ++j) {
|
|
4076
|
+
unchecked(digits[j] += (u8(digits[j] >= 5) * 3));
|
|
4077
|
+
}
|
|
4078
|
+
for (let j = length; j != -1; --j) {
|
|
4079
|
+
let d = unchecked(digits[j]) << 1;
|
|
4080
|
+
if (j < length) unchecked(digits[j + 1] |= u8(d > 15));
|
|
4081
|
+
unchecked(digits[j] = d & 15);
|
|
4082
|
+
}
|
|
4083
|
+
unchecked(digits[0] += u8((value & (1 << i)) != 0));
|
|
4084
|
+
}
|
|
4085
|
+
}
|
|
4086
|
+
|
|
4087
|
+
export function u128toDecimalString(value: u128): string {
|
|
4088
|
+
var length = 40;
|
|
4089
|
+
var digits = new Uint8Array(length);
|
|
4090
|
+
var result = "", start = false;
|
|
4091
|
+
|
|
4092
|
+
processU64(digits, value.hi);
|
|
4093
|
+
processU64(digits, value.lo);
|
|
4094
|
+
|
|
4095
|
+
for (let i = length - 1; i != -1; --i) {
|
|
4096
|
+
let d = unchecked(digits[i]);
|
|
4097
|
+
if (!start && d != 0) start = true;
|
|
4098
|
+
if (start) {
|
|
4099
|
+
assert(<u32>d <= 9);
|
|
4100
|
+
result += String.fromCharCode(0x30 + d);
|
|
4101
|
+
}
|
|
4102
|
+
}
|
|
4103
|
+
return result;
|
|
4104
|
+
}
|
|
4105
|
+
|
|
4106
|
+
export function u256toDecimalString(value: u256): string {
|
|
4107
|
+
var length = 78;
|
|
4108
|
+
var digits = new Uint8Array(length);
|
|
4109
|
+
var result = "", start = false;
|
|
4110
|
+
|
|
4111
|
+
processU64(digits, value.hi2);
|
|
4112
|
+
processU64(digits, value.hi1);
|
|
4113
|
+
processU64(digits, value.lo2);
|
|
4114
|
+
processU64(digits, value.lo1);
|
|
4115
|
+
|
|
4116
|
+
for (let i = length - 1; i != -1; --i) {
|
|
4117
|
+
let d = unchecked(digits[i]);
|
|
4118
|
+
if (!start && d != 0) start = true;
|
|
4119
|
+
if (start) {
|
|
4120
|
+
assert(<u32>d <= 9);
|
|
4121
|
+
result += String.fromCharCode(0x30 + d);
|
|
4122
|
+
}
|
|
4123
|
+
}
|
|
4124
|
+
return result;
|
|
4125
|
+
}
|
|
4126
|
+
|
|
4127
|
+
export function atou128(str: string, radix: i32 = 10): u128 {
|
|
4128
|
+
if (radix < 2 || radix > 36) {
|
|
4129
|
+
throw new Error("Invalid radix");
|
|
4130
|
+
}
|
|
4131
|
+
var len = str.length;
|
|
4132
|
+
if (!len) return u128.Zero;
|
|
4133
|
+
|
|
4134
|
+
var first = str.charCodeAt(0);
|
|
4135
|
+
if (len == 1 && first == CharCode._0) {
|
|
4136
|
+
return u128.Zero;
|
|
4137
|
+
}
|
|
4138
|
+
var isNeg = first == CharCode.MINUS;
|
|
4139
|
+
// @ts-ignore
|
|
4140
|
+
var index = i32(isNeg | (first == CharCode.PLUS));
|
|
4141
|
+
|
|
4142
|
+
if (str.charCodeAt(index) == CharCode._0) {
|
|
4143
|
+
let second = str.charCodeAt(++index);
|
|
4144
|
+
if ((second | 32) == CharCode.x) {
|
|
4145
|
+
radix = 16;
|
|
4146
|
+
++index;
|
|
4147
|
+
} else if ((second | 32) == CharCode.o) {
|
|
4148
|
+
radix = 8;
|
|
4149
|
+
++index;
|
|
4150
|
+
} else if ((second | 32) == CharCode.b) {
|
|
4151
|
+
radix = 2;
|
|
4152
|
+
++index;
|
|
4153
|
+
} else if (second == CharCode._0) {
|
|
4154
|
+
// skip leading zeros
|
|
4155
|
+
while (index < len && str.charCodeAt(index) == CharCode._0) ++index;
|
|
4156
|
+
}
|
|
4157
|
+
}
|
|
4158
|
+
var result = u128.Zero;
|
|
4159
|
+
var table = RadixCharsTable;
|
|
4160
|
+
|
|
4161
|
+
if (index >= len) return result;
|
|
4162
|
+
|
|
4163
|
+
if (ASC_SHRINK_LEVEL >= 1) {
|
|
4164
|
+
let radix128 = u128.fromU64(radix);
|
|
4165
|
+
do {
|
|
4166
|
+
let n: u32 = str.charCodeAt(index) - CharCode._0;
|
|
4167
|
+
if (n > <u32>(CharCode.z - CharCode._0)) break;
|
|
4168
|
+
|
|
4169
|
+
let num = load<u8>(table + n);
|
|
4170
|
+
if (num >= <u8>radix) break;
|
|
4171
|
+
|
|
4172
|
+
// @ts-ignore
|
|
4173
|
+
result *= radix128;
|
|
4174
|
+
// @ts-ignore
|
|
4175
|
+
result += u128.fromU64(num);
|
|
4176
|
+
} while (++index < len);
|
|
4177
|
+
} else {
|
|
4178
|
+
switch (radix) {
|
|
4179
|
+
case 2: {
|
|
4180
|
+
do {
|
|
4181
|
+
let num: u32 = str.charCodeAt(index) - CharCode._0;
|
|
4182
|
+
if (num >= 2) break;
|
|
4183
|
+
// @ts-ignore
|
|
4184
|
+
result <<= 1;
|
|
4185
|
+
// @ts-ignore
|
|
4186
|
+
result |= u128.fromU64(num);
|
|
4187
|
+
} while (++index < len);
|
|
4188
|
+
break;
|
|
4189
|
+
}
|
|
4190
|
+
case 10: {
|
|
4191
|
+
do {
|
|
4192
|
+
let num: u32 = str.charCodeAt(index) - CharCode._0;
|
|
4193
|
+
if (num >= 10) break;
|
|
4194
|
+
// @ts-ignore
|
|
4195
|
+
result = (result << 3) + (result << 1);
|
|
4196
|
+
// @ts-ignore
|
|
4197
|
+
result += u128.fromU64(num);
|
|
4198
|
+
} while (++index < len);
|
|
4199
|
+
break;
|
|
4200
|
+
}
|
|
4201
|
+
case 16: {
|
|
4202
|
+
do {
|
|
4203
|
+
let n: u32 = str.charCodeAt(index) - CharCode._0;
|
|
4204
|
+
if (n > <u32>(CharCode.z - CharCode._0)) break;
|
|
4205
|
+
|
|
4206
|
+
let num = load<u8>(table + n);
|
|
4207
|
+
if (num >= 16) break;
|
|
4208
|
+
|
|
4209
|
+
// @ts-ignore
|
|
4210
|
+
result <<= 4;
|
|
4211
|
+
// @ts-ignore
|
|
4212
|
+
result |= u128.fromU64(num);
|
|
4213
|
+
} while (++index < len);
|
|
4214
|
+
break;
|
|
4215
|
+
}
|
|
4216
|
+
default: {
|
|
4217
|
+
let radix128 = u128.fromU64(radix);
|
|
4218
|
+
do {
|
|
4219
|
+
let n: u32 = str.charCodeAt(index) - CharCode._0;
|
|
4220
|
+
if (n > <u32>(CharCode.z - CharCode._0)) break;
|
|
4221
|
+
|
|
4222
|
+
let num = load<u8>(table + n);
|
|
4223
|
+
if (num >= <u8>radix) break;
|
|
4224
|
+
|
|
4225
|
+
// @ts-ignore
|
|
4226
|
+
result *= radix128;
|
|
4227
|
+
// @ts-ignore
|
|
4228
|
+
result += u128.fromU64(num);
|
|
4229
|
+
} while (++index < len);
|
|
4230
|
+
break;
|
|
4231
|
+
}
|
|
4232
|
+
}
|
|
4233
|
+
}
|
|
4234
|
+
// @ts-ignore
|
|
4235
|
+
return isNeg ? -result : result;
|
|
4236
|
+
}
|
|
762
4237
|
`,"bindings/asyncify":`@unmanaged
|
|
763
4238
|
export class StackDescriptor {
|
|
764
4239
|
/** The index in linear memory of the start of the \u201Casyncify stack\u201D. */
|
|
@@ -24007,6 +27482,263 @@ declare namespace external {
|
|
|
24007
27482
|
|
|
24008
27483
|
/** Annotates a global for lazy compilation. */
|
|
24009
27484
|
declare function lazy(...args: any[]): any;
|
|
27485
|
+
|
|
27486
|
+
// Big integers \u2014 native globals implemented in std/assembly/bignum (vendored
|
|
27487
|
+
// from @btc-vision/as-bignum). The arithmetic/bitwise/comparison operators
|
|
27488
|
+
// (+ - * / % & | ^ << >> == != < > <= >=) are operator overloads resolved by
|
|
27489
|
+
// the compiler; the equivalent static methods are listed for tooling.
|
|
27490
|
+
|
|
27491
|
+
/** A 128-bit unsigned integer. */
|
|
27492
|
+
declare class u128 {
|
|
27493
|
+
lo: u64;
|
|
27494
|
+
hi: u64;
|
|
27495
|
+
constructor(lo?: u64, hi?: u64);
|
|
27496
|
+
|
|
27497
|
+
static readonly Zero: u128;
|
|
27498
|
+
static readonly One: u128;
|
|
27499
|
+
static readonly Min: u128;
|
|
27500
|
+
static readonly Max: u128;
|
|
27501
|
+
|
|
27502
|
+
static fromString(value: string, radix?: i32): u128;
|
|
27503
|
+
static fromI256(value: i256): u128;
|
|
27504
|
+
static fromU256(value: u256): u128;
|
|
27505
|
+
static fromI128(value: i128): u128;
|
|
27506
|
+
static fromU128(value: u128): u128;
|
|
27507
|
+
static fromI64(value: i64): u128;
|
|
27508
|
+
static fromU64(value: u64): u128;
|
|
27509
|
+
static fromF64(value: f64): u128;
|
|
27510
|
+
static fromF32(value: f32): u128;
|
|
27511
|
+
static fromI32(value: i32): u128;
|
|
27512
|
+
static fromU32(value: u32): u128;
|
|
27513
|
+
static fromBool(value: bool): u128;
|
|
27514
|
+
static fromBits(lo1: u32, lo2: u32, hi1: u32, hi2: u32): u128;
|
|
27515
|
+
static fromBytes<T>(array: T, bigEndian?: bool): u128;
|
|
27516
|
+
static fromBytesLE(array: u8[]): u128;
|
|
27517
|
+
static fromBytesBE(array: u8[]): u128;
|
|
27518
|
+
static fromUint8ArrayLE(array: Uint8Array): u128;
|
|
27519
|
+
static fromUint8ArrayBE(array: Uint8Array): u128;
|
|
27520
|
+
static from<T>(value: T): u128;
|
|
27521
|
+
|
|
27522
|
+
static isEmpty(value: u128): bool;
|
|
27523
|
+
static or(a: u128, b: u128): u128;
|
|
27524
|
+
static xor(a: u128, b: u128): u128;
|
|
27525
|
+
static and(a: u128, b: u128): u128;
|
|
27526
|
+
static shl(value: u128, shift: i32): u128;
|
|
27527
|
+
static shr(value: u128, shift: i32): u128;
|
|
27528
|
+
static shr_u(value: u128, shift: i32): u128;
|
|
27529
|
+
static rotl(value: u128, shift: i32): u128;
|
|
27530
|
+
static rotr(value: u128, shift: i32): u128;
|
|
27531
|
+
static add(a: u128, b: u128): u128;
|
|
27532
|
+
static sub(a: u128, b: u128): u128;
|
|
27533
|
+
static mul(a: u128, b: u128): u128;
|
|
27534
|
+
static div(a: u128, b: u128): u128;
|
|
27535
|
+
static rem(a: u128, b: u128): u128;
|
|
27536
|
+
static div10(value: u128): u128;
|
|
27537
|
+
static rem10(value: u128): u128;
|
|
27538
|
+
static pow(base: u128, exponent: i32): u128;
|
|
27539
|
+
static sqrt(value: u128): u128;
|
|
27540
|
+
static sqr(value: u128): u128;
|
|
27541
|
+
static muldiv(a: u128, b: u128, c: u128): u128;
|
|
27542
|
+
static eq(a: u128, b: u128): bool;
|
|
27543
|
+
static ne(a: u128, b: u128): bool;
|
|
27544
|
+
static lt(a: u128, b: u128): bool;
|
|
27545
|
+
static gt(a: u128, b: u128): bool;
|
|
27546
|
+
static le(a: u128, b: u128): bool;
|
|
27547
|
+
static ge(a: u128, b: u128): bool;
|
|
27548
|
+
static ord(a: u128, b: u128): i32;
|
|
27549
|
+
static popcnt(value: u128): i32;
|
|
27550
|
+
static clz(value: u128): i32;
|
|
27551
|
+
static ctz(value: u128): i32;
|
|
27552
|
+
|
|
27553
|
+
set(value: u128): this;
|
|
27554
|
+
setI64(value: i64): this;
|
|
27555
|
+
setU64(value: u64): this;
|
|
27556
|
+
setI32(value: i32): this;
|
|
27557
|
+
setU32(value: u32): this;
|
|
27558
|
+
isZero(): bool;
|
|
27559
|
+
not(): u128;
|
|
27560
|
+
neg(): u128;
|
|
27561
|
+
pos(): u128;
|
|
27562
|
+
sqr(): this;
|
|
27563
|
+
preInc(): this;
|
|
27564
|
+
preDec(): this;
|
|
27565
|
+
postInc(): u128;
|
|
27566
|
+
postDec(): u128;
|
|
27567
|
+
clone(): u128;
|
|
27568
|
+
as<T>(): T;
|
|
27569
|
+
toI64(): i64;
|
|
27570
|
+
toU64(): u64;
|
|
27571
|
+
toI32(): i32;
|
|
27572
|
+
toU32(): u32;
|
|
27573
|
+
toBool(): bool;
|
|
27574
|
+
toF64(): f64;
|
|
27575
|
+
toF32(): f32;
|
|
27576
|
+
toI128(): i128;
|
|
27577
|
+
toU128(): this;
|
|
27578
|
+
toI256(): i256;
|
|
27579
|
+
toU256(): u256;
|
|
27580
|
+
toBytes(bigEndian?: bool): u8[];
|
|
27581
|
+
toStaticBytes(bigEndian?: bool): StaticArray<u8>;
|
|
27582
|
+
toUint8Array(bigEndian?: bool): Uint8Array;
|
|
27583
|
+
toString(radix?: i32): string;
|
|
27584
|
+
}
|
|
27585
|
+
|
|
27586
|
+
/** A 128-bit signed integer. */
|
|
27587
|
+
declare class i128 {
|
|
27588
|
+
lo: u64;
|
|
27589
|
+
hi: i64;
|
|
27590
|
+
constructor(lo?: u64, hi?: i64);
|
|
27591
|
+
|
|
27592
|
+
static readonly Zero: i128;
|
|
27593
|
+
static readonly One: i128;
|
|
27594
|
+
static readonly Min: i128;
|
|
27595
|
+
static readonly Max: i128;
|
|
27596
|
+
|
|
27597
|
+
static fromString(value: string, radix?: i32): i128;
|
|
27598
|
+
static fromI256(value: i256): i128;
|
|
27599
|
+
static fromU256(value: u256): i128;
|
|
27600
|
+
static fromI128(value: i128): i128;
|
|
27601
|
+
static fromU128(value: u128): i128;
|
|
27602
|
+
static fromI64(value: i64): i128;
|
|
27603
|
+
static fromU64(value: u64): i128;
|
|
27604
|
+
static fromF64(value: f64): i128;
|
|
27605
|
+
static fromF32(value: f32): i128;
|
|
27606
|
+
static fromI32(value: i32): i128;
|
|
27607
|
+
static fromU32(value: u32): i128;
|
|
27608
|
+
static fromBits(lo1: i32, lo2: i32, hi1: i32, hi2: i32): i128;
|
|
27609
|
+
static fromBytes<T>(array: T, bigEndian?: bool): i128;
|
|
27610
|
+
static fromBytesLE(array: u8[]): i128;
|
|
27611
|
+
static fromBytesBE(array: u8[]): i128;
|
|
27612
|
+
static fromUint8ArrayLE(array: Uint8Array): i128;
|
|
27613
|
+
static fromUint8ArrayBE(array: Uint8Array): i128;
|
|
27614
|
+
static from<T>(value: T): i128;
|
|
27615
|
+
|
|
27616
|
+
static isEmpty(value: i128): bool;
|
|
27617
|
+
static or(a: i128, b: i128): i128;
|
|
27618
|
+
static xor(a: i128, b: i128): i128;
|
|
27619
|
+
static and(a: i128, b: i128): i128;
|
|
27620
|
+
static shl(value: i128, shift: i32): i128;
|
|
27621
|
+
static shr_u(value: i128, shift: i32): i128;
|
|
27622
|
+
static add(a: i128, b: i128): i128;
|
|
27623
|
+
static sub(a: i128, b: i128): i128;
|
|
27624
|
+
static eq(a: i128, b: i128): bool;
|
|
27625
|
+
static ne(a: i128, b: i128): bool;
|
|
27626
|
+
static lt(a: i128, b: i128): bool;
|
|
27627
|
+
static gt(a: i128, b: i128): bool;
|
|
27628
|
+
static le(a: i128, b: i128): bool;
|
|
27629
|
+
static ge(a: i128, b: i128): bool;
|
|
27630
|
+
static ord(a: i128, b: i128): i32;
|
|
27631
|
+
static popcnt(value: i128): i32;
|
|
27632
|
+
static clz(value: i128): i32;
|
|
27633
|
+
static ctz(value: i128): i32;
|
|
27634
|
+
static abs(value: i128): i128;
|
|
27635
|
+
|
|
27636
|
+
isNeg(): bool;
|
|
27637
|
+
isPos(): bool;
|
|
27638
|
+
isZero(): bool;
|
|
27639
|
+
not(): i128;
|
|
27640
|
+
neg(): i128;
|
|
27641
|
+
pos(): i128;
|
|
27642
|
+
toBytes(bigEndian?: bool): u8[];
|
|
27643
|
+
toStaticBytes(bigEndian?: bool): StaticArray<u8>;
|
|
27644
|
+
toUint8Array(bigEndian?: bool): Uint8Array;
|
|
27645
|
+
}
|
|
27646
|
+
|
|
27647
|
+
/** A 256-bit unsigned integer. */
|
|
27648
|
+
declare class u256 {
|
|
27649
|
+
lo1: u64;
|
|
27650
|
+
lo2: u64;
|
|
27651
|
+
hi1: u64;
|
|
27652
|
+
hi2: u64;
|
|
27653
|
+
constructor(lo1?: u64, lo2?: u64, hi1?: u64, hi2?: u64);
|
|
27654
|
+
|
|
27655
|
+
static readonly Zero: u256;
|
|
27656
|
+
static readonly One: u256;
|
|
27657
|
+
static readonly Min: u256;
|
|
27658
|
+
static readonly Max: u256;
|
|
27659
|
+
|
|
27660
|
+
static fromU256(value: u256): u256;
|
|
27661
|
+
static fromU128(value: u128): u256;
|
|
27662
|
+
static fromU64(value: u64): u256;
|
|
27663
|
+
static fromI64(value: i64): u256;
|
|
27664
|
+
static fromU32(value: u32): u256;
|
|
27665
|
+
static fromI32(value: i32): u256;
|
|
27666
|
+
static fromBits(l0: u32, l1: u32, l2: u32, l3: u32, h0: u32, h1: u32, h2: u32, h3: u32): u256;
|
|
27667
|
+
static fromBytes<T>(array: T, bigEndian?: bool): u256;
|
|
27668
|
+
static fromBytesLE(array: u8[]): u256;
|
|
27669
|
+
static fromBytesBE(array: u8[]): u256;
|
|
27670
|
+
static fromUint8ArrayLE(array: Uint8Array): u256;
|
|
27671
|
+
static fromUint8ArrayBE(array: Uint8Array): u256;
|
|
27672
|
+
static fromF64(value: f64): u256;
|
|
27673
|
+
static fromF32(value: f32): u256;
|
|
27674
|
+
static from<T>(value: T): u256;
|
|
27675
|
+
static fromString(str: string, radix?: i32): u256;
|
|
27676
|
+
|
|
27677
|
+
static isEmpty(value: u256): bool;
|
|
27678
|
+
static add(a: u256, b: u256): u256;
|
|
27679
|
+
static sub(a: u256, b: u256): u256;
|
|
27680
|
+
static mul(a: u256, b: u256): u256;
|
|
27681
|
+
static or(a: u256, b: u256): u256;
|
|
27682
|
+
static xor(a: u256, b: u256): u256;
|
|
27683
|
+
static and(a: u256, b: u256): u256;
|
|
27684
|
+
static shr(value: u256, shift: i32): u256;
|
|
27685
|
+
static shr_u(value: u256, shift: i32): u256;
|
|
27686
|
+
static eq(a: u256, b: u256): bool;
|
|
27687
|
+
static ne(a: u256, b: u256): bool;
|
|
27688
|
+
static lt(a: u256, b: u256): bool;
|
|
27689
|
+
static gt(a: u256, b: u256): bool;
|
|
27690
|
+
static le(a: u256, b: u256): bool;
|
|
27691
|
+
static ge(a: u256, b: u256): bool;
|
|
27692
|
+
static popcnt(value: u256): i32;
|
|
27693
|
+
static clz(value: u256): i32;
|
|
27694
|
+
static ctz(value: u256): i32;
|
|
27695
|
+
|
|
27696
|
+
set(value: u256): this;
|
|
27697
|
+
setI64(value: i64): this;
|
|
27698
|
+
setU64(value: u64): this;
|
|
27699
|
+
setI32(value: i32): this;
|
|
27700
|
+
setU32(value: u32): this;
|
|
27701
|
+
setU128(value: u128): this;
|
|
27702
|
+
isZero(): bool;
|
|
27703
|
+
not(): u256;
|
|
27704
|
+
neg(): u256;
|
|
27705
|
+
pos(): u256;
|
|
27706
|
+
postInc(): u256;
|
|
27707
|
+
postDec(): u256;
|
|
27708
|
+
clone(): u256;
|
|
27709
|
+
as<T>(): T;
|
|
27710
|
+
toI64(): i64;
|
|
27711
|
+
toU64(): u64;
|
|
27712
|
+
toI32(): i32;
|
|
27713
|
+
toU32(): u32;
|
|
27714
|
+
toBool(): bool;
|
|
27715
|
+
toI128(): i128;
|
|
27716
|
+
toU128(): u128;
|
|
27717
|
+
toU256(): this;
|
|
27718
|
+
toBytes(bigEndian?: bool): u8[];
|
|
27719
|
+
toStaticBytes(bigEndian?: bool): StaticArray<u8>;
|
|
27720
|
+
toUint8Array(bigEndian?: bool): Uint8Array;
|
|
27721
|
+
toString(radix?: i32): string;
|
|
27722
|
+
}
|
|
27723
|
+
|
|
27724
|
+
/** A 256-bit signed integer (backs the signed conversions of the family). */
|
|
27725
|
+
declare class i256 {
|
|
27726
|
+
lo1: i64;
|
|
27727
|
+
lo2: i64;
|
|
27728
|
+
hi1: i64;
|
|
27729
|
+
hi2: i64;
|
|
27730
|
+
constructor(lo1?: i64, lo2?: i64, hi1?: i64, hi2?: i64);
|
|
27731
|
+
|
|
27732
|
+
static readonly Zero: i256;
|
|
27733
|
+
static readonly One: i256;
|
|
27734
|
+
static readonly Min: i256;
|
|
27735
|
+
static readonly Max: i256;
|
|
27736
|
+
|
|
27737
|
+
static isEmpty(value: i256): bool;
|
|
27738
|
+
|
|
27739
|
+
isNeg(): bool;
|
|
27740
|
+
isZero(): bool;
|
|
27741
|
+
}
|
|
24010
27742
|
`,portable:`/**
|
|
24011
27743
|
* Environment definitions for compiling ToilScript to JavaScript using tsc.
|
|
24012
27744
|
*
|
|
@@ -24468,7 +28200,7 @@ declare interface Float64Array {
|
|
|
24468
28200
|
// FIXME: remove
|
|
24469
28201
|
declare function offsetof<T>(fieldName?: string): usize;
|
|
24470
28202
|
declare function idof<T>(): u32;
|
|
24471
|
-
`};var xn={};Q(xn,{default:()=>pn});Dn(xn,
|
|
28203
|
+
`};var xn={};Q(xn,{default:()=>pn});Dn(xn,Mt);import*as Mt from"binaryen";import{default as pn}from"binaryen";import*as At from"toilscript";var h=At,Pn=M.argv.indexOf("--wasm");if(~Pn){let e=String(M.argv[Pn+1]);M.argv.splice(Pn,2),h=await import(new URL(e,dn.pathToFileURL(M.cwd()+"/")))}var Vn=sn.createRequire(import.meta.url),Kn=M.platform==="win32",E=Kn?`\r
|
|
24472
28204
|
`:`
|
|
24473
|
-
`,jn=Kn?"\\":"/",_=".ts",_t=`.d${_}`,hn=new RegExp("\\"+_+"$"),Tt=new RegExp("^(?!.*\\.d\\"+_+"$).*\\"+_+"$");function Te(e){return e.replace(/-/g,"_").toUpperCase()}function Ct(e){return typeof e=="string"&&e!==""}var Xn=ve,zt=Y,P=Ee,X=Ae,Bt=_e,Be=3,Se=0;function qn(e,t=[]){return Object.keys(e||{}).forEach(n=>{let a=e[n],s=Y[n];s&&s.type==="b"?a&&t.push(`--${n}`):Array.isArray(a)?a.forEach(r=>{t.push(`--${n}`,String(r))}):t.push(`--${n}`,String(a))}),t}async function St(e,t={}){typeof e=="string"&&(e={[`input${_}`]:e});let n=["--outFile","binary","--textFile","text"];qn(t,n);let a={},s=await we(n.concat(Object.keys(e)),{readFile:r=>Object.prototype.hasOwnProperty.call(e,r)?e[r]:null,writeFile:(r,u)=>{a[r]=u},listFiles:()=>[]});return Object.assign(s,a)}async function we(e,t){Array.isArray(e)||(e=qn(e)),t||(t={});let n=t.stats||new Cn,a=n.begin(),s=0,r=0,u=0,f=(Xn||"").split(".");f.length===3&&(r=parseInt(f[0])|0,s=parseInt(f[1])|0,u=parseInt(f[2])|0);let p=t.stdout||Hn(),b=t.stderr||Hn(),d=t.readFile||ee,g=t.writeFile||te,y=t.listFiles||ie,m=ye(e,Y,!1),i=m.options;e=m.arguments;let zn=new nn(p),j=new nn(b);i.noColors&&(zn.enabled=!1,j.enabled=!1);let Yn=m.unknown;Yn.length&&Yn.forEach(o=>{b.write(`${j.yellow("WARNING ")}Unknown option '${o}'${E}`)});let Zn=m.trailing;Zn.length&&b.write(`${j.yellow("WARNING ")}Unsupported trailing arguments: ${Zn.join(" ")}${E}`);let H=null,R=null,z=(o,l={})=>(o&&b.write(`${j.red("FAILURE ")}${o.stack.replace(/^ERROR: /i,"")}${E}`),R&&R.dispose(),n.total||(n.total=n.end(a)),Object.assign({error:o,stdout:p,stderr:b,stats:n},l));if(i.version)return p.write(`Version ${Xn}${E}`),z(null);let B=v.normalize(i.baseDir||"."),K=en(i.config||"toilconfig.json",B),Bn=v.basename(K),Z=v.dirname(K),k=await Ce(Bn,Z,d),De=k!=null&&Array.isArray(k.entries)&&k.entries.length;if(i.help||!e.length&&!De){let o=i.help?p:b,l=i.help?zn:j;return o.write([l.white("SYNTAX")," "+l.cyan("toilscript")+" [entryFile ...] [options]","",l.white("EXAMPLES")," "+l.cyan("toilscript")+" hello"+_," "+l.cyan("toilscript")+" hello"+_+" -o hello.wasm -t hello.wat"," "+l.cyan("toilscript")+" hello1"+_+" hello2"+_+" -o -O > hello.wasm"," "+l.cyan("toilscript")+" --config toilconfig.json --target release","",l.white("OPTIONS")].concat(Fe(Y,24,E)).join(E)+E),z(null)}if(!(G.promises&&G.promises.readFile)){if(d===ee)throw Error("'options.readFile' must be specified");if(g===te)throw Error("'options.writeFile' must be specified");if(y===ie)throw Error("'options.listFiles' must be specified")}let Sn=new Set;Sn.add(K);let Ie=i.target||"release";for(;k;){if(k.targets){let l=k.targets[Ie];l&&(i=Mn(Y,i,l,Z))}let o=k.options;if(o&&(i=Mn(Y,i,o,Z)),k.entries)for(let l of k.entries)e.push(en(l,Z));if(k.extends){if(K=en(k.extends,Z,!0),Bn=v.basename(K),Z=v.dirname(K),Sn.has(K))break;Sn.add(K),k=await Ce(Bn,Z,d)}else break}if(Un(Y,i),i.showConfig)return b.write(JSON.stringify({options:i,entries:e},null,2)),z(null);function $n(o){return[...new Set(o)]}let S,tn,mn,T=h.newOptions();switch(i.runtime){case"stub":tn=0;break;case"minimal":tn=1;break;case"memory":tn=3;break;default:tn=2;break}switch(i.uncheckedBehavior){default:mn=0;break;case"never":mn=1;break;case"always":mn=2;break}if(h.setTarget(T,0),h.setDebugInfo(T,!!i.debug),h.setRuntime(T,tn),h.setNoAssert(T,i.noAssert),h.setExportMemory(T,!i.noExportMemory),h.setImportMemory(T,i.importMemory),h.setInitialMemory(T,i.initialMemory>>>0),h.setMaximumMemory(T,i.maximumMemory>>>0),h.setSharedMemory(T,i.sharedMemory),h.setImportTable(T,i.importTable),h.setExportTable(T,i.exportTable),i.exportStart!=null&&h.setExportStart(T,Ct(i.exportStart)?i.exportStart:"_start"),h.setMemoryBase(T,i.memoryBase>>>0),h.setTableBase(T,i.tableBase>>>0),h.setSourceMap(T,i.sourceMap!=null),h.setUncheckedBehavior(T,mn),h.setNoUnsafe(T,i.noUnsafe),h.setPedantic(T,i.pedantic),h.setLowMemoryLimit(T,i.lowMemoryLimit>>>0),h.setExportRuntime(T,i.exportRuntime),h.setBundleVersion(T,r,s,u),!i.stackSize&&tn===2&&(i.stackSize=h.DEFAULT_STACK_SIZE),h.setStackSize(T,i.stackSize),h.setBindingsHint(T,i.bindings&&i.bindings.length>0),i.use){let o=i.use;for(let l=0,c=o.length;l<c;++l){let x=o[l],A=x.indexOf("=");if(A<0)return z(Error(`Global alias '${x}' is invalid.`));let F=x.substring(0,A).trim(),C=x.substring(A+1).trim();if(!F.length)return z(Error(`Global alias '${x}' is invalid.`));h.addGlobalAlias(T,F,C)}}let N;if((N=i.disable)!=null){typeof N=="string"&&(N=N.split(","));for(let o=0,l=N.length;o<l;++o){let c=N[o].trim(),x=h[`FEATURE_${Te(c)}`];if(!x)return z(Error(`Feature '${c}' is unknown.`));h.setFeature(T,x,!1)}}if((N=i.enable)!=null){typeof N=="string"&&(N=N.split(","));for(let o=0,l=N.length;o<l;++o){let c=N[o].trim(),x=h[`FEATURE_${Te(c)}`];if(!x)return z(Error(`Feature '${c}' is unknown.`));h.setFeature(T,x,!0)}}let $=0,J=0;i.optimize&&($=Be,J=Se),typeof i.optimizeLevel=="number"&&($=i.optimizeLevel),typeof i.shrinkLevel=="number"&&(J=i.shrinkLevel),$=Math.min(Math.max($,0),3),J=Math.min(Math.max(J,0),2),h.setOptimizeLevelHints(T,$,J),S=h.newProgram(T);let rn=[];if(Array.isArray(t.transforms)&&rn.push(...t.transforms),i.transform){let o=$n(i.transform);for(let l=0,c=o.length;l<c;++l){let x=o[l].trim(),A,F;if(Vn.resolve)try{A=Vn.resolve(x,{paths:[U.cwd(),B]}),F=await import(dn.pathToFileURL(A)),F.default&&(F=F.default)}catch(C){try{F=Vn(A)}catch{return z(C)}}else try{F=await import(new URL(x,import.meta.url)),F.default&&(F=F.default)}catch(C){return z(C)}if(!F||typeof F!="function"&&typeof F!="object")return z(Error("not a transform: "+o[l]));rn.push(F)}}try{rn=rn.map(o=>(typeof o=="function"&&(Object.assign(o.prototype,{program:S,binaryen:pn,baseDir:B,stdout:p,stderr:b,log:console.error,readFile:d,writeFile:g,listFiles:y}),o=new o),o))}catch(o){return z(o)}async function wn(o,...l){for(let c=0,x=rn.length;c<x;++c){let A=rn[c];if(typeof A[o]=="function")try{let F=n.begin();n.transformCount++,await A[o](...l),n.transformTime+=n.end(F)}catch(F){return F}}}Object.keys(X).forEach(o=>{if(o.includes("/"))return;let l=n.begin();n.parseCount++,h.parse(S,X[o],P+o+_,!1),n.parseTime+=n.end(l)});let on=[];if(i.lib){let o=i.lib;typeof o=="string"&&(o=o.split(",")),on.push(...o.map(l=>l.trim())),on=$n(on);for(let l=0,c=on.length;l<c;++l){let x=on[l],A;x.endsWith(_)?(A=[v.basename(x)],x=v.dirname(x)):A=await y(x,B)||[];for(let F of A){let C=await d(F,x);if(C==null)return z(Error(`Library file '${F}' not found.`));X[F.replace(hn,"")]=C;let I=n.begin();n.parseCount++,h.parse(S,C,P+F,!1),n.parseTime+=n.end(I)}}}i.path=i.path||[];let bn=new Map;async function Oe(o,l){let c=null,x=null;if(!o.startsWith(P))(c=await d(x=o+_,B))==null&&(c=await d(x=o+"/index"+_,B))==null&&(x=o+_,c=await d(o+_t,B));else{let A=o.substring(P.length),F=`${A}/index`;if(Object.prototype.hasOwnProperty.call(X,A))c=X[A],x=P+A+_;else if(Object.prototype.hasOwnProperty.call(X,F))c=X[F],x=P+F+_;else{for(let C of on)if((c=await d(A+_,C))!=null){x=P+A+_;break}else if((c=await d(F+_,C))!=null){x=P+F+_;break}if(c==null){let C=o.match(/^~lib\/((?:@[^/]+\/)?[^/]+)(?:\/(.+))?/);if(C){let I=C[1],q=C[2]||"index",vn=bn.has(l)?bn.get(l):".",M=[],D=v.resolve(B,vn).split(jn);for(let w=D.length,O=Kn?1:0;w>=O;--w)D[w-1]!=="node_modules"&&M.push(`${D.slice(0,w).join(jn)}${jn}node_modules`);M.push(...i.path);for(let w of M.map(O=>v.relative(B,O))){let O=q;if((c=await d(v.join(w,I,O+_),B))!=null){x=`${P}${I}/${O}${_}`,bn.set(x.replace(hn,""),v.join(w,I));break}let un=`${q}/index`;if((c=await d(v.join(w,I,un+_),B))!=null){x=`${P}${I}/${un}${_}`,bn.set(x.replace(hn,""),v.join(w,I));break}}}}}}return c==null?null:{sourceText:c,sourcePath:x}}function Le(o=[]){do{let l=h.nextFile(S);if(l==null)break;o.push(l)}while(!0);return o}async function Jn(){let o;for(;(o=Le()).length;){let c=[];for(let x of o){let A=h.getDependee(S,x);c.push(Oe(x,A))}c=await Promise.all(c);for(let x=0,A=o.length;x<A;++x){let F=o[x],C=c[x],I=n.begin();n.parseCount++,C?h.parse(S,C.sourceText,C.sourcePath,!1):h.parse(S,null,F+_,!1),n.parseTime+=n.end(I)}}let l=gn(S,b,i.disableWarning,t.reportDiagnostic,j.enabled);if(l){let c=Error(`${l} parse error(s)`);return c.stack=c.message,z(c)}}{let o=String(i.runtime),l=`rt/index-${o}`,c=X[l];if(c==null){if(l=o,c=await d(l+_,B),c==null)return z(Error(`Runtime '${v.resolve(B,l+_)}' is not found.`))}else l=`~lib/${l}`;let x=n.begin();n.parseCount++,h.parse(S,c,l+_,!0),n.parseTime+=n.end(x)}for(let o=0,l=e.length;o<l;++o){let c=String(e[o]),x=v.isAbsolute(c)?v.relative(B,c):v.normalize(c);x=x.replace(/\\/g,"/").replace(hn,"").replace(/\/$/,"");let A=await d(x+_,B);if(A==null){let C=`${x}/index${_}`;A=await d(C,B),A!=null?x=C:x+=_}else x+=_;let F=n.begin();n.parseCount++,h.parse(S,A,x,!0),n.parseTime+=n.end(F)}{let o=await Jn();if(o)return o}{let o=await wn("afterParse",S.parser);if(o)return z(o)}{let o=await Jn();if(o)return o}{let o=n.begin();n.initializeCount++;try{h.initializeProgram(S)}catch(l){L("initialize",l)}n.initializeTime+=n.end(o)}{let o=await wn("afterInitialize",S);if(o)return z(o)}{let o=n.begin();n.compileCount++;try{H=h.compile(S)}catch(l){let c=gn(S,b,i.disableWarning,t.reportDiagnostic,j.enabled);if(c){let x=Error(`${c} compile error(s)`);return x.stack=x.message,z(x)}L("compile",l)}n.compileTime+=n.end(o)}R=pn.wrapModule(typeof H=="number"||H instanceof Number?h.getBinaryenModuleRef(H):H.ref);let ln=gn(S,b,i.disableWarning,t.reportDiagnostic,j.enabled);if(ln){let o=Error(`${ln} compile error(s)`);return o.stack=o.message,z(o)}{let o=await wn("afterCompile",R);if(o)return z(o)}if(ln=gn(S,b,i.disableWarning,t.reportDiagnostic,j.enabled),ln){let o=Error(`${ln} afterCompile error(s)`);return o.stack=o.message,z(o)}if(!i.noValidate){let o=n.begin();n.validateCount++;let l=h.validate(H);if(n.validateTime+=n.end(o),!l)return z(Error("validate error"))}if(i.trapMode==="clamp"||i.trapMode==="js"){let o=n.begin();try{R.runPasses([`trap-mode-${i.trapMode}`])}catch(l){L("runPasses",l)}n.compileTime+=n.end(o)}else if(i.trapMode!=="allow")return z(Error("Unsupported trap mode"));let Qn=i.debug,Re=i.converge,ne=i.importMemory?i.zeroFilledMemory:!1,yn=[];i.runPasses&&(typeof i.runPasses=="string"&&(i.runPasses=i.runPasses.split(",")),i.runPasses.length&&i.runPasses.forEach(o=>{yn.includes(o=o.trim())||yn.push(o)}));{let o=n.begin();try{n.optimizeCount++,h.optimize(H,$,J,Qn,ne)}catch(l){L("optimize",l)}try{R.runPasses(yn)}catch(l){L("runPasses",l)}if(Re){let l;try{let c=n.begin();n.emitCount++,l=R.emitBinary(),n.emitTime+=n.end(c)}catch(c){L("emitBinary (converge)",c)}do{try{n.optimizeCount++,h.optimize(H,$,J,Qn,ne)}catch(x){L("optimize (converge)",x)}try{R.runPasses(yn)}catch(x){L("runPasses (converge)",x)}let c;try{let x=n.begin();n.emitCount++,c=R.emitBinary(),n.emitTime+=n.end(x)}catch(x){L("emitBinary (converge)",x)}if(c.length>=l.length){c.length>l.length&&b.write(`Last converge was suboptimal.${E}`);break}l=c}while(!0)}n.optimizeTime+=n.end(o)}let an=[];if(!i.noEmit){if(i.binaryFile)return z(Error("Usage of the --binaryFile compiler option is no longer supported. Use --outFile instead."));let o=i.bindings||[],l=!1,c=i.outFile!=null,x=i.textFile!=null,A=c||x,F=c&&i.outFile.length>0||x&&i.textFile.length>0,C=F?(i.outFile||i.textFile).replace(/\.\w+$/,""):null,I=F?v.basename(C):"output";if(h.setBasenameHint(T,I),i.outFile!=null){let M=i.sourceMap!=null?i.sourceMap.length?i.sourceMap:`./${I}.wasm.map`:null,D=n.begin();n.emitCount++;let w;try{w=R.emitBinary(M)}catch(O){L("emitBinary",O)}if(n.emitTime+=n.end(D),i.outFile.length?an.push(g(i.outFile,w.binary,B)):(l=!0,Fn(w.binary)),w.sourceMap!="")if(i.outFile.length){let O=JSON.parse(w.sourceMap);O.sourceRoot=`./${I}`;let un=[];for(let En=0,ke=O.sources.length;En<ke;++En){let re=O.sources[En],oe=h.getSource(S,re.replace(hn,""));if(oe==null)return z(Error(`Source of file '${re}' not found.`));un[En]=oe}O.sourcesContent=un,an.push(g(v.join(v.dirname(i.outFile),v.basename(M)).replace(/^\.\//,""),JSON.stringify(O),B))}else b.write(`Skipped source map (no output path)${E}`)}if(i.textFile!=null||!A){let M=n.begin();n.emitCount++;let D;try{pn.setOptimizeStackIR(!0),D=i.textFile?.endsWith(".wast")?R.emitText():R.emitStackIR()}catch(w){L("emitText",w)}n.emitTime+=n.end(M),i.textFile!=null&&i.textFile.length?an.push(g(i.textFile,D,B)):l||Fn(D)}let q=o.includes("esm"),vn=!q&&o.includes("raw");if(q||vn)if(C){let M=n.begin();n.emitCount++;let D;try{D=h.buildTSD(S,q)}catch(w){L("buildTSD",w)}n.emitTime+=n.end(M),an.push(g(C+".d.ts",D,B))}else b.write(`Skipped TypeScript binding (no output path)${E}`);if(q||vn)if(C){let M=n.begin();n.emitCount++;let D;try{D=h.buildJS(S,q)}catch(w){L("buildJS",w)}n.emitTime+=n.end(M),an.push(g(C+".js",D,B))}else b.write(`Skipped JavaScript binding (no output path)${E}`)}try{await Promise.all(an)}catch(o){return z(o)}return n.total=n.end(a),i.stats&&b.write(n.toString()),z(null);async function ee(o,l){let c=v.resolve(l,o);try{return n.readCount++,await G.promises.readFile(c,"utf8")}catch{return null}}async function te(o,l,c){try{n.writeCount++;let x=v.resolve(c,v.dirname(o)),A=v.join(x,v.basename(o));return await G.promises.mkdir(x,{recursive:!0}),await G.promises.writeFile(A,l),!0}catch{return!1}}async function ie(o,l){try{return n.readCount++,(await G.promises.readdir(v.join(l,o))).filter(c=>Tt.test(c))}catch{return null}}function Fn(o){Fn.used||(Fn.used=!0,n.writeCount++),p.write(o)}function L(o,l){let c=zn.red("\u258C ");console.error([E,c,"Whoops, the ToilScript compiler has crashed during ",o," :-(",E,c,E,(typeof l.stack=="string"?[c,"Here is the stack trace hinting at the problem, perhaps it's useful?",E,c,E,l.stack.replace(/^/mg,c),E]:[c,"There is no stack trace. Perhaps a Binaryen exception above / in console?",E,c,E,c,"> "+l.stack,E]).join(""),c,E,c,"If you see where the error is, feel free to send us a pull request. If not,",E,c,"please let us know: https://github.com/dacely-cloud/toilscript/issues",E,c,E,c,"Thank you!",E].join("")),U.exit(1)}}function Gn(e){return Object.prototype.toString.call(e)==="[object Object]"}async function Ce(e,t,n){let a=await n(e,t),s=v.join(t,e);if(!a)return null;let r;try{r=JSON.parse(a)}catch(u){throw new Error(`Toilconfig is not valid json: ${s}`,{cause:u})}if(r.options&&!Gn(r.options))throw new Error(`Toilconfig.options is not an object: ${s}`);if(r.include&&!Array.isArray(r.include))throw new Error(`Toilconfig.include is not an array: ${s}`);if(r.targets){if(!Gn(r.targets))throw new Error(`Toilconfig.targets is not an object: ${s}`);let u=Object.keys(r.targets);for(let f=0;f<u.length;f++){let p=u[f];if(!Gn(r.targets[p]))throw new Error(`Toilconfig.targets.${p} is not an object: ${s}`)}}if(r.extends&&typeof r.extends!="string")throw new Error(`Toilconfig.extends is not a string: ${s}`);return r}function gn(e,t,n,a,s){typeof s>"u"&&t&&(s=t.isTTY);let r=0;do{let p=h.nextDiagnostic(e);if(!p)break;if(t){let b=d=>{if(n==null)return!1;if(!n.length)return!0;let g=h.getDiagnosticCode(d);return n.includes(g)};(h.isError(p)||!b(p))&&t.write(h.formatDiagnostic(p,s,!0)+E+E)}if(a){let b=function(g){return g&&{start:h.getRangeStart(g),end:h.getRangeEnd(g),source:d(h.getRangeSource(g))}||null},d=function(g){return g&&{normalizedPath:h.getSourceNormalizedPath(g)}||null};var u=b,f=d;a({message:h.getDiagnosticMessage(p),code:h.getDiagnosticCode(p),category:h.getDiagnosticCategory(p),range:b(h.getDiagnosticRange(p)),relatedRange:b(h.getDiagnosticRelatedRange(p))})}h.isError(p)&&++r}while(!0);return r}var Cn=class{readCount=0;writeCount=0;parseTime=0;parseCount=0;initializeTime=0;initializeCount=0;compileTime=0;compileCount=0;emitTime=0;emitCount=0;validateTime=0;validateCount=0;optimizeTime=0;optimizeCount=0;transformTime=0;transformCount=0;begin(){return U.hrtime()}end(t){let n=U.hrtime(t);return n[0]*1e9+n[1]}toString(){let t=m=>m?`${(m/1e6).toFixed(3)} ms`:"n/a",n=Object.keys(this).filter(m=>m.endsWith("Time")).map(m=>m.substring(0,m.length-4)),a=n.map(m=>t(this[`${m}Time`])),s=n.map(m=>this[`${m}Count`].toString()),r=n.reduce((m,i)=>Math.max(i.length,m),0),u=a.reduce((m,i)=>Math.max(i.length,m),0),f=s.reduce((m,i)=>Math.max(i.length,m),0),p=r+u+f+6,b=[];b.push(`\u256D\u2500${"\u2500".repeat(p)}\u2500\u256E${E}`);let d="Stats";b.push(`\u2502 ${d}${" ".repeat(p-d.length)} \u2502${E}`),b.push(`\u255E\u2550${"\u2550".repeat(r)}\u2550\u2564\u2550${"\u2550".repeat(u)}\u2550\u2564\u2550${"\u2550".repeat(f)}\u2550\u2561${E}`);for(let m=0,i=n.length;m<i;++m)b.push(`\u2502 ${n[m].padEnd(r)} \u2502 ${a[m].padStart(u)} \u2502 ${s[m].padStart(f)} \u2502${E}`);b.push(`\u251C\u2500${"\u2500".repeat(r)}\u2500\u2534\u2500${"\u2500".repeat(u)}\u2500\u2534\u2500${"\u2500".repeat(f)}\u2500\u2524${E}`);let g=`Took ${t(this.total)}`;b.push(`\u2502 ${g}${" ".repeat(p-g.length)} \u2502${E}`);let y=`${this.readCount} reads, ${this.writeCount} writes`;return b.push(`\u2502 ${y}${" ".repeat(p-y.length)} \u2502${E}`),b.push(`\u2570\u2500${"\u2500".repeat(p)}\u2500\u256F${E}`),b.join("")}},ze=typeof global<"u"&&global.Buffer?global.Buffer.allocUnsafe||(e=>new global.Buffer(e)):e=>new Uint8Array(e);function Hn(e){let t=[];return t.write=function(n){if(e&&e(n),typeof n=="string"){let a=ze(Tn.length(n));Tn.write(n,a,0),n=a}this.push(n)},t.reset=function(){t.length=0},t.toBuffer=function(){let n=0,a=0,s=this.length;for(;a<s;)n+=this[a++].length;let r=ze(n);for(n=a=0;a<s;)r.set(this[a],n),n+=this[a].length,++a;return r},t.toString=function(){let n=this.toBuffer();return Tn.read(n,0,n.length)},t}var wt={alwaysStrict:!0,strictNullChecks:!0,noImplicitAny:!0,noImplicitReturns:!0,noImplicitThis:!0,noEmitOnError:!0,noPropertyAccessFromIndexSignature:!0,experimentalDecorators:!0,target:"esnext",noLib:!0,types:[],allowJs:!1};export{Cn as Stats,gn as checkDiagnostics,St as compileString,qn as configToArguments,Hn as createMemoryStream,Wn as default,Be as defaultOptimizeLevel,Se as defaultShrinkLevel,Bt as definitionFiles,X as libraryFiles,P as libraryPrefix,we as main,zt as options,wt as tscOptions,Xn as version};
|
|
28205
|
+
`,jn=Kn?"\\":"/",_=".ts",_t=`.d${_}`,hn=new RegExp("\\"+_+"$"),Tt=new RegExp("^(?!.*\\.d\\"+_+"$).*\\"+_+"$");function Te(e){return e.replace(/-/g,"_").toUpperCase()}function Ct(e){return typeof e=="string"&&e!==""}var Xn=ve,zt=Y,P=Ee,X=Ae,Bt=_e,Be=3,we=0;function qn(e,t=[]){return Object.keys(e||{}).forEach(n=>{let a=e[n],s=Y[n];s&&s.type==="b"?a&&t.push(`--${n}`):Array.isArray(a)?a.forEach(r=>{t.push(`--${n}`,String(r))}):t.push(`--${n}`,String(a))}),t}async function wt(e,t={}){typeof e=="string"&&(e={[`input${_}`]:e});let n=["--outFile","binary","--textFile","text"];qn(t,n);let a={},s=await Se(n.concat(Object.keys(e)),{readFile:r=>Object.prototype.hasOwnProperty.call(e,r)?e[r]:null,writeFile:(r,u)=>{a[r]=u},listFiles:()=>[]});return Object.assign(s,a)}async function Se(e,t){Array.isArray(e)||(e=qn(e)),t||(t={});let n=t.stats||new Cn,a=n.begin(),s=0,r=0,u=0,f=(Xn||"").split(".");f.length===3&&(r=parseInt(f[0])|0,s=parseInt(f[1])|0,u=parseInt(f[2])|0);let p=t.stdout||Hn(),b=t.stderr||Hn(),d=t.readFile||ee,g=t.writeFile||te,y=t.listFiles||ie,m=ye(e,Y,!1),i=m.options;e=m.arguments;let zn=new nn(p),j=new nn(b);i.noColors&&(zn.enabled=!1,j.enabled=!1);let Yn=m.unknown;Yn.length&&Yn.forEach(o=>{b.write(`${j.yellow("WARNING ")}Unknown option '${o}'${E}`)});let Zn=m.trailing;Zn.length&&b.write(`${j.yellow("WARNING ")}Unsupported trailing arguments: ${Zn.join(" ")}${E}`);let H=null,k=null,z=(o,l={})=>(o&&b.write(`${j.red("FAILURE ")}${o.stack.replace(/^ERROR: /i,"")}${E}`),k&&k.dispose(),n.total||(n.total=n.end(a)),Object.assign({error:o,stdout:p,stderr:b,stats:n},l));if(i.version)return p.write(`Version ${Xn}${E}`),z(null);let B=v.normalize(i.baseDir||"."),K=en(i.config||"toilconfig.json",B),Bn=v.basename(K),Z=v.dirname(K),R=await Ce(Bn,Z,d),De=R!=null&&Array.isArray(R.entries)&&R.entries.length;if(i.help||!e.length&&!De){let o=i.help?p:b,l=i.help?zn:j;return o.write([l.white("SYNTAX")," "+l.cyan("toilscript")+" [entryFile ...] [options]","",l.white("EXAMPLES")," "+l.cyan("toilscript")+" hello"+_," "+l.cyan("toilscript")+" hello"+_+" -o hello.wasm -t hello.wat"," "+l.cyan("toilscript")+" hello1"+_+" hello2"+_+" -o -O > hello.wasm"," "+l.cyan("toilscript")+" --config toilconfig.json --target release","",l.white("OPTIONS")].concat(Fe(Y,24,E)).join(E)+E),z(null)}if(!(G.promises&&G.promises.readFile)){if(d===ee)throw Error("'options.readFile' must be specified");if(g===te)throw Error("'options.writeFile' must be specified");if(y===ie)throw Error("'options.listFiles' must be specified")}let wn=new Set;wn.add(K);let Ie=i.target||"release";for(;R;){if(R.targets){let l=R.targets[Ie];l&&(i=Un(Y,i,l,Z))}let o=R.options;if(o&&(i=Un(Y,i,o,Z)),R.entries)for(let l of R.entries)e.push(en(l,Z));if(R.extends){if(K=en(R.extends,Z,!0),Bn=v.basename(K),Z=v.dirname(K),wn.has(K))break;wn.add(K),R=await Ce(Bn,Z,d)}else break}if(Mn(Y,i),i.showConfig)return b.write(JSON.stringify({options:i,entries:e},null,2)),z(null);function $n(o){return[...new Set(o)]}let w,tn,mn,T=h.newOptions();switch(i.runtime){case"stub":tn=0;break;case"minimal":tn=1;break;case"memory":tn=3;break;default:tn=2;break}switch(i.uncheckedBehavior){default:mn=0;break;case"never":mn=1;break;case"always":mn=2;break}if(h.setTarget(T,0),h.setDebugInfo(T,!!i.debug),h.setRuntime(T,tn),h.setNoAssert(T,i.noAssert),h.setExportMemory(T,!i.noExportMemory),h.setImportMemory(T,i.importMemory),h.setInitialMemory(T,i.initialMemory>>>0),h.setMaximumMemory(T,i.maximumMemory>>>0),h.setSharedMemory(T,i.sharedMemory),h.setImportTable(T,i.importTable),h.setExportTable(T,i.exportTable),i.exportStart!=null&&h.setExportStart(T,Ct(i.exportStart)?i.exportStart:"_start"),h.setMemoryBase(T,i.memoryBase>>>0),h.setTableBase(T,i.tableBase>>>0),h.setSourceMap(T,i.sourceMap!=null),h.setUncheckedBehavior(T,mn),h.setNoUnsafe(T,i.noUnsafe),h.setPedantic(T,i.pedantic),h.setLowMemoryLimit(T,i.lowMemoryLimit>>>0),h.setExportRuntime(T,i.exportRuntime),h.setBundleVersion(T,r,s,u),!i.stackSize&&tn===2&&(i.stackSize=h.DEFAULT_STACK_SIZE),h.setStackSize(T,i.stackSize),h.setBindingsHint(T,i.bindings&&i.bindings.length>0),i.use){let o=i.use;for(let l=0,c=o.length;l<c;++l){let x=o[l],A=x.indexOf("=");if(A<0)return z(Error(`Global alias '${x}' is invalid.`));let F=x.substring(0,A).trim(),C=x.substring(A+1).trim();if(!F.length)return z(Error(`Global alias '${x}' is invalid.`));h.addGlobalAlias(T,F,C)}}let N;if((N=i.disable)!=null){typeof N=="string"&&(N=N.split(","));for(let o=0,l=N.length;o<l;++o){let c=N[o].trim(),x=h[`FEATURE_${Te(c)}`];if(!x)return z(Error(`Feature '${c}' is unknown.`));h.setFeature(T,x,!1)}}if((N=i.enable)!=null){typeof N=="string"&&(N=N.split(","));for(let o=0,l=N.length;o<l;++o){let c=N[o].trim(),x=h[`FEATURE_${Te(c)}`];if(!x)return z(Error(`Feature '${c}' is unknown.`));h.setFeature(T,x,!0)}}let $=0,J=0;i.optimize&&($=Be,J=we),typeof i.optimizeLevel=="number"&&($=i.optimizeLevel),typeof i.shrinkLevel=="number"&&(J=i.shrinkLevel),$=Math.min(Math.max($,0),3),J=Math.min(Math.max(J,0),2),h.setOptimizeLevelHints(T,$,J),w=h.newProgram(T);let rn=[];if(Array.isArray(t.transforms)&&rn.push(...t.transforms),i.transform){let o=$n(i.transform);for(let l=0,c=o.length;l<c;++l){let x=o[l].trim(),A,F;if(Vn.resolve)try{A=Vn.resolve(x,{paths:[M.cwd(),B]}),F=await import(dn.pathToFileURL(A)),F.default&&(F=F.default)}catch(C){try{F=Vn(A)}catch{return z(C)}}else try{F=await import(new URL(x,import.meta.url)),F.default&&(F=F.default)}catch(C){return z(C)}if(!F||typeof F!="function"&&typeof F!="object")return z(Error("not a transform: "+o[l]));rn.push(F)}}try{rn=rn.map(o=>(typeof o=="function"&&(Object.assign(o.prototype,{program:w,binaryen:pn,baseDir:B,stdout:p,stderr:b,log:console.error,readFile:d,writeFile:g,listFiles:y}),o=new o),o))}catch(o){return z(o)}async function Sn(o,...l){for(let c=0,x=rn.length;c<x;++c){let A=rn[c];if(typeof A[o]=="function")try{let F=n.begin();n.transformCount++,await A[o](...l),n.transformTime+=n.end(F)}catch(F){return F}}}Object.keys(X).forEach(o=>{if(o.includes("/"))return;let l=n.begin();n.parseCount++,h.parse(w,X[o],P+o+_,!1),n.parseTime+=n.end(l)});let on=[];if(i.lib){let o=i.lib;typeof o=="string"&&(o=o.split(",")),on.push(...o.map(l=>l.trim())),on=$n(on);for(let l=0,c=on.length;l<c;++l){let x=on[l],A;x.endsWith(_)?(A=[v.basename(x)],x=v.dirname(x)):A=await y(x,B)||[];for(let F of A){let C=await d(F,x);if(C==null)return z(Error(`Library file '${F}' not found.`));X[F.replace(hn,"")]=C;let I=n.begin();n.parseCount++,h.parse(w,C,P+F,!1),n.parseTime+=n.end(I)}}}i.path=i.path||[];let bn=new Map;async function Oe(o,l){let c=null,x=null;if(!o.startsWith(P))(c=await d(x=o+_,B))==null&&(c=await d(x=o+"/index"+_,B))==null&&(x=o+_,c=await d(o+_t,B));else{let A=o.substring(P.length),F=`${A}/index`;if(Object.prototype.hasOwnProperty.call(X,A))c=X[A],x=P+A+_;else if(Object.prototype.hasOwnProperty.call(X,F))c=X[F],x=P+F+_;else{for(let C of on)if((c=await d(A+_,C))!=null){x=P+A+_;break}else if((c=await d(F+_,C))!=null){x=P+F+_;break}if(c==null){let C=o.match(/^~lib\/((?:@[^/]+\/)?[^/]+)(?:\/(.+))?/);if(C){let I=C[1],q=C[2]||"index",vn=bn.has(l)?bn.get(l):".",U=[],D=v.resolve(B,vn).split(jn);for(let S=D.length,O=Kn?1:0;S>=O;--S)D[S-1]!=="node_modules"&&U.push(`${D.slice(0,S).join(jn)}${jn}node_modules`);U.push(...i.path);for(let S of U.map(O=>v.relative(B,O))){let O=q;if((c=await d(v.join(S,I,O+_),B))!=null){x=`${P}${I}/${O}${_}`,bn.set(x.replace(hn,""),v.join(S,I));break}let un=`${q}/index`;if((c=await d(v.join(S,I,un+_),B))!=null){x=`${P}${I}/${un}${_}`,bn.set(x.replace(hn,""),v.join(S,I));break}}}}}}return c==null?null:{sourceText:c,sourcePath:x}}function Le(o=[]){do{let l=h.nextFile(w);if(l==null)break;o.push(l)}while(!0);return o}async function Jn(){let o;for(;(o=Le()).length;){let c=[];for(let x of o){let A=h.getDependee(w,x);c.push(Oe(x,A))}c=await Promise.all(c);for(let x=0,A=o.length;x<A;++x){let F=o[x],C=c[x],I=n.begin();n.parseCount++,C?h.parse(w,C.sourceText,C.sourcePath,!1):h.parse(w,null,F+_,!1),n.parseTime+=n.end(I)}}let l=gn(w,b,i.disableWarning,t.reportDiagnostic,j.enabled);if(l){let c=Error(`${l} parse error(s)`);return c.stack=c.message,z(c)}}{let o=String(i.runtime),l=`rt/index-${o}`,c=X[l];if(c==null){if(l=o,c=await d(l+_,B),c==null)return z(Error(`Runtime '${v.resolve(B,l+_)}' is not found.`))}else l=`~lib/${l}`;let x=n.begin();n.parseCount++,h.parse(w,c,l+_,!0),n.parseTime+=n.end(x)}for(let o=0,l=e.length;o<l;++o){let c=String(e[o]),x=v.isAbsolute(c)?v.relative(B,c):v.normalize(c);x=x.replace(/\\/g,"/").replace(hn,"").replace(/\/$/,"");let A=await d(x+_,B);if(A==null){let C=`${x}/index${_}`;A=await d(C,B),A!=null?x=C:x+=_}else x+=_;let F=n.begin();n.parseCount++,h.parse(w,A,x,!0),n.parseTime+=n.end(F)}{let o=await Jn();if(o)return o}{let o=await Sn("afterParse",w.parser);if(o)return z(o)}{let o=await Jn();if(o)return o}{let o=n.begin();n.initializeCount++;try{h.initializeProgram(w)}catch(l){L("initialize",l)}n.initializeTime+=n.end(o)}{let o=await Sn("afterInitialize",w);if(o)return z(o)}{let o=n.begin();n.compileCount++;try{H=h.compile(w)}catch(l){let c=gn(w,b,i.disableWarning,t.reportDiagnostic,j.enabled);if(c){let x=Error(`${c} compile error(s)`);return x.stack=x.message,z(x)}L("compile",l)}n.compileTime+=n.end(o)}k=pn.wrapModule(typeof H=="number"||H instanceof Number?h.getBinaryenModuleRef(H):H.ref);let ln=gn(w,b,i.disableWarning,t.reportDiagnostic,j.enabled);if(ln){let o=Error(`${ln} compile error(s)`);return o.stack=o.message,z(o)}{let o=await Sn("afterCompile",k);if(o)return z(o)}if(ln=gn(w,b,i.disableWarning,t.reportDiagnostic,j.enabled),ln){let o=Error(`${ln} afterCompile error(s)`);return o.stack=o.message,z(o)}if(!i.noValidate){let o=n.begin();n.validateCount++;let l=h.validate(H);if(n.validateTime+=n.end(o),!l)return z(Error("validate error"))}if(i.trapMode==="clamp"||i.trapMode==="js"){let o=n.begin();try{k.runPasses([`trap-mode-${i.trapMode}`])}catch(l){L("runPasses",l)}n.compileTime+=n.end(o)}else if(i.trapMode!=="allow")return z(Error("Unsupported trap mode"));let Qn=i.debug,ke=i.converge,ne=i.importMemory?i.zeroFilledMemory:!1,yn=[];i.runPasses&&(typeof i.runPasses=="string"&&(i.runPasses=i.runPasses.split(",")),i.runPasses.length&&i.runPasses.forEach(o=>{yn.includes(o=o.trim())||yn.push(o)}));{let o=n.begin();try{n.optimizeCount++,h.optimize(H,$,J,Qn,ne)}catch(l){L("optimize",l)}try{k.runPasses(yn)}catch(l){L("runPasses",l)}if(ke){let l;try{let c=n.begin();n.emitCount++,l=k.emitBinary(),n.emitTime+=n.end(c)}catch(c){L("emitBinary (converge)",c)}do{try{n.optimizeCount++,h.optimize(H,$,J,Qn,ne)}catch(x){L("optimize (converge)",x)}try{k.runPasses(yn)}catch(x){L("runPasses (converge)",x)}let c;try{let x=n.begin();n.emitCount++,c=k.emitBinary(),n.emitTime+=n.end(x)}catch(x){L("emitBinary (converge)",x)}if(c.length>=l.length){c.length>l.length&&b.write(`Last converge was suboptimal.${E}`);break}l=c}while(!0)}n.optimizeTime+=n.end(o)}let an=[];if(!i.noEmit){if(i.binaryFile)return z(Error("Usage of the --binaryFile compiler option is no longer supported. Use --outFile instead."));let o=i.bindings||[],l=!1,c=i.outFile!=null,x=i.textFile!=null,A=c||x,F=c&&i.outFile.length>0||x&&i.textFile.length>0,C=F?(i.outFile||i.textFile).replace(/\.\w+$/,""):null,I=F?v.basename(C):"output";if(h.setBasenameHint(T,I),i.outFile!=null){let U=i.sourceMap!=null?i.sourceMap.length?i.sourceMap:`./${I}.wasm.map`:null,D=n.begin();n.emitCount++;let S;try{S=k.emitBinary(U)}catch(O){L("emitBinary",O)}if(n.emitTime+=n.end(D),i.outFile.length?an.push(g(i.outFile,S.binary,B)):(l=!0,Fn(S.binary)),S.sourceMap!="")if(i.outFile.length){let O=JSON.parse(S.sourceMap);O.sourceRoot=`./${I}`;let un=[];for(let En=0,Re=O.sources.length;En<Re;++En){let re=O.sources[En],oe=h.getSource(w,re.replace(hn,""));if(oe==null)return z(Error(`Source of file '${re}' not found.`));un[En]=oe}O.sourcesContent=un,an.push(g(v.join(v.dirname(i.outFile),v.basename(U)).replace(/^\.\//,""),JSON.stringify(O),B))}else b.write(`Skipped source map (no output path)${E}`)}if(i.textFile!=null||!A){let U=n.begin();n.emitCount++;let D;try{pn.setOptimizeStackIR(!0),D=i.textFile?.endsWith(".wast")?k.emitText():k.emitStackIR()}catch(S){L("emitText",S)}n.emitTime+=n.end(U),i.textFile!=null&&i.textFile.length?an.push(g(i.textFile,D,B)):l||Fn(D)}let q=o.includes("esm"),vn=!q&&o.includes("raw");if(q||vn)if(C){let U=n.begin();n.emitCount++;let D;try{D=h.buildTSD(w,q)}catch(S){L("buildTSD",S)}n.emitTime+=n.end(U),an.push(g(C+".d.ts",D,B))}else b.write(`Skipped TypeScript binding (no output path)${E}`);if(q||vn)if(C){let U=n.begin();n.emitCount++;let D;try{D=h.buildJS(w,q)}catch(S){L("buildJS",S)}n.emitTime+=n.end(U),an.push(g(C+".js",D,B))}else b.write(`Skipped JavaScript binding (no output path)${E}`)}try{await Promise.all(an)}catch(o){return z(o)}return n.total=n.end(a),i.stats&&b.write(n.toString()),z(null);async function ee(o,l){let c=v.resolve(l,o);try{return n.readCount++,await G.promises.readFile(c,"utf8")}catch{return null}}async function te(o,l,c){try{n.writeCount++;let x=v.resolve(c,v.dirname(o)),A=v.join(x,v.basename(o));return await G.promises.mkdir(x,{recursive:!0}),await G.promises.writeFile(A,l),!0}catch{return!1}}async function ie(o,l){try{return n.readCount++,(await G.promises.readdir(v.join(l,o))).filter(c=>Tt.test(c))}catch{return null}}function Fn(o){Fn.used||(Fn.used=!0,n.writeCount++),p.write(o)}function L(o,l){let c=zn.red("\u258C ");console.error([E,c,"Whoops, the ToilScript compiler has crashed during ",o," :-(",E,c,E,(typeof l.stack=="string"?[c,"Here is the stack trace hinting at the problem, perhaps it's useful?",E,c,E,l.stack.replace(/^/mg,c),E]:[c,"There is no stack trace. Perhaps a Binaryen exception above / in console?",E,c,E,c,"> "+l.stack,E]).join(""),c,E,c,"If you see where the error is, feel free to send us a pull request. If not,",E,c,"please let us know: https://github.com/dacely-cloud/toilscript/issues",E,c,E,c,"Thank you!",E].join("")),M.exit(1)}}function Gn(e){return Object.prototype.toString.call(e)==="[object Object]"}async function Ce(e,t,n){let a=await n(e,t),s=v.join(t,e);if(!a)return null;let r;try{r=JSON.parse(a)}catch(u){throw new Error(`Toilconfig is not valid json: ${s}`,{cause:u})}if(r.options&&!Gn(r.options))throw new Error(`Toilconfig.options is not an object: ${s}`);if(r.include&&!Array.isArray(r.include))throw new Error(`Toilconfig.include is not an array: ${s}`);if(r.targets){if(!Gn(r.targets))throw new Error(`Toilconfig.targets is not an object: ${s}`);let u=Object.keys(r.targets);for(let f=0;f<u.length;f++){let p=u[f];if(!Gn(r.targets[p]))throw new Error(`Toilconfig.targets.${p} is not an object: ${s}`)}}if(r.extends&&typeof r.extends!="string")throw new Error(`Toilconfig.extends is not a string: ${s}`);return r}function gn(e,t,n,a,s){typeof s>"u"&&t&&(s=t.isTTY);let r=0;do{let p=h.nextDiagnostic(e);if(!p)break;if(t){let b=d=>{if(n==null)return!1;if(!n.length)return!0;let g=h.getDiagnosticCode(d);return n.includes(g)};(h.isError(p)||!b(p))&&t.write(h.formatDiagnostic(p,s,!0)+E+E)}if(a){let b=function(g){return g&&{start:h.getRangeStart(g),end:h.getRangeEnd(g),source:d(h.getRangeSource(g))}||null},d=function(g){return g&&{normalizedPath:h.getSourceNormalizedPath(g)}||null};var u=b,f=d;a({message:h.getDiagnosticMessage(p),code:h.getDiagnosticCode(p),category:h.getDiagnosticCategory(p),range:b(h.getDiagnosticRange(p)),relatedRange:b(h.getDiagnosticRelatedRange(p))})}h.isError(p)&&++r}while(!0);return r}var Cn=class{readCount=0;writeCount=0;parseTime=0;parseCount=0;initializeTime=0;initializeCount=0;compileTime=0;compileCount=0;emitTime=0;emitCount=0;validateTime=0;validateCount=0;optimizeTime=0;optimizeCount=0;transformTime=0;transformCount=0;begin(){return M.hrtime()}end(t){let n=M.hrtime(t);return n[0]*1e9+n[1]}toString(){let t=m=>m?`${(m/1e6).toFixed(3)} ms`:"n/a",n=Object.keys(this).filter(m=>m.endsWith("Time")).map(m=>m.substring(0,m.length-4)),a=n.map(m=>t(this[`${m}Time`])),s=n.map(m=>this[`${m}Count`].toString()),r=n.reduce((m,i)=>Math.max(i.length,m),0),u=a.reduce((m,i)=>Math.max(i.length,m),0),f=s.reduce((m,i)=>Math.max(i.length,m),0),p=r+u+f+6,b=[];b.push(`\u256D\u2500${"\u2500".repeat(p)}\u2500\u256E${E}`);let d="Stats";b.push(`\u2502 ${d}${" ".repeat(p-d.length)} \u2502${E}`),b.push(`\u255E\u2550${"\u2550".repeat(r)}\u2550\u2564\u2550${"\u2550".repeat(u)}\u2550\u2564\u2550${"\u2550".repeat(f)}\u2550\u2561${E}`);for(let m=0,i=n.length;m<i;++m)b.push(`\u2502 ${n[m].padEnd(r)} \u2502 ${a[m].padStart(u)} \u2502 ${s[m].padStart(f)} \u2502${E}`);b.push(`\u251C\u2500${"\u2500".repeat(r)}\u2500\u2534\u2500${"\u2500".repeat(u)}\u2500\u2534\u2500${"\u2500".repeat(f)}\u2500\u2524${E}`);let g=`Took ${t(this.total)}`;b.push(`\u2502 ${g}${" ".repeat(p-g.length)} \u2502${E}`);let y=`${this.readCount} reads, ${this.writeCount} writes`;return b.push(`\u2502 ${y}${" ".repeat(p-y.length)} \u2502${E}`),b.push(`\u2570\u2500${"\u2500".repeat(p)}\u2500\u256F${E}`),b.join("")}},ze=typeof global<"u"&&global.Buffer?global.Buffer.allocUnsafe||(e=>new global.Buffer(e)):e=>new Uint8Array(e);function Hn(e){let t=[];return t.write=function(n){if(e&&e(n),typeof n=="string"){let a=ze(Tn.length(n));Tn.write(n,a,0),n=a}this.push(n)},t.reset=function(){t.length=0},t.toBuffer=function(){let n=0,a=0,s=this.length;for(;a<s;)n+=this[a++].length;let r=ze(n);for(n=a=0;a<s;)r.set(this[a],n),n+=this[a].length,++a;return r},t.toString=function(){let n=this.toBuffer();return Tn.read(n,0,n.length)},t}var St={alwaysStrict:!0,strictNullChecks:!0,noImplicitAny:!0,noImplicitReturns:!0,noImplicitThis:!0,noEmitOnError:!0,noPropertyAccessFromIndexSignature:!0,experimentalDecorators:!0,target:"esnext",noLib:!0,types:[],allowJs:!1};export{Cn as Stats,gn as checkDiagnostics,wt as compileString,qn as configToArguments,Hn as createMemoryStream,Wn as default,Be as defaultOptimizeLevel,we as defaultShrinkLevel,Bt as definitionFiles,X as libraryFiles,P as libraryPrefix,Se as main,zt as options,St as tscOptions,Xn as version};
|
|
24474
28206
|
//# sourceMappingURL=cli.js.map
|