KoboldAI-Client/extern/lualibs/nums/bn.lua

7 lines
9.9 KiB
Lua
Raw Normal View History

-- Lua-nums @fef161a940aaafdbb8d9c75fe073b8bb43152474 | /nums/bn.lua | https://github.com/user-none/lua-nums | License: MIT License | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
-- Copyright (c) 2016 John Schember
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local math=math;local a={}local b={}local c;local d=1;local e=0;local f=0;while d>f do f=d;d=d<<1;e=e+1 end;c=e-1//2-1;local g=1<<c-1;local h=g;local i=1<<c+1-1;d=nil;e=nil;f=nil;local j={["0"]=0,["1"]=1,["2"]=2,["3"]=3,["4"]=4,["5"]=5,["6"]=6,["7"]=7,["8"]=8,["9"]=9,["A"]=10,["B"]=11,["C"]=12,["D"]=13,["E"]=14,["F"]=15}local k={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}local function l(m,n)local function o(m,p)while#m._digits<p do m._digits[#m._digits+1]=0 end end;if type(n)=="number"then o(m,n)return end;if#m._digits>=#n._digits then o(n,#m._digits)else o(m,#n._digits)end end;local function q(m)if#m._digits==0 then m._pos=true;m._digits={0}return end;for r=#m._digits,2,-1 do if m._digits[r]~=0 then break end;table.remove(m._digits,r)end;if#m._digits==1 and m._digits[1]==0 then m._pos=true end end;local function s(m)m._digits={0}m._pos=true end;local function t(m)if a.isbn(m)then m=m:copy()else m=a:new(m)end;return m end;local function u(m,n)m=t(m)n=t(n)return m,n end;local function v(m,n)if m._pos==n._pos then return true end;return false end;local function w(m,n)m=t(m)if n==0 then return m end;for r=1,n do table.insert(m._digits,1,0)end;q(m)return m end;local function x(m,n)m=t(m)if n==0 then return m elseif n>=#m._digits then return a:new()end;for r=1,n do table.remove(m._digits,1)end;q(m)return m end;local function y(m,n)local z=a:new()local A=0;m,n=u(m,n)l(m,n)for r=1,#m._digits do z._digits[r]=m._digits[r]+n._digits[r]+A;A=z._digits[r]>>c;z._digits[r]=z._digits[r]&g end;if A~=0 then z._digits[#z._digits+1]=A end;q(z)return z end;local function B(m,n)local z=a()local A=0;m,n=u(m,n)l(m,n)for r=1,#m._digits do z._digits[r]=m._digits[r]-n._digits[r]-A&i;A=z._digits[r]>>c;z._digits[r]=z._digits[r]&g end;q(z)return z end;local function C(m,n,D)local z;m,n=u(m,n)l(m,n)z=a:new()l(z,m)for r=1,#m._digits do z._digits[r]=D(m._digits[r],n._digits[r])end;q(z)return z end;local function E(m,n)local F;local G;local H;local I;local J;m,n=u(m,n)if m==a.ZERO then return a:new(),a:new()elseif n==a.ZERO then return nil,"divide by 0"elseif n==a:new(1)then return m,a:new()end;I=v(m,n)J=m._pos;m._pos=true;n._pos=true;F=m:len_bits()-1;H=a:new()G=a:new()while F>=0 do H=H<<1;if m&(a:new(1)<<F)>0 then H=H|1 end;if H>=n then H=H-n;G=G|(a:new(1)<<F)end;F=F-1 end;G._pos=I;if H~=0 then H._pos=J end;q(G)q(H)return G,H end;local function K(L,M)local A;local z;local n;local N=10;M=tostring(M)M=M:gsub("%.%d*","")M=M:gsub("U?L?L?$","")if M==""then return true end;if M:sub(1,1)=="-"then L._pos=false;M=M:sub(2)end;M=M:upper()if M:sub(1,2)=="0X"then N=16;M=M:sub(3)end;for r=1,#M do z=M:sub(r,r)n=j[z]if n==nil then s(L)return false end;A=0;for r=1,#L._digits do L._digits[r]=L._digits[r]*N+A;A=L._digits[r]>>c;L._digits[r]=L._digits[r]&g end;if A~=0 then L._digits[#L._digits+1]=A end;L._digits[1]=L._digits[1]+n;A=L._digits[1]>>c;L._digits[1]=L._digits[1]&g;for r=2,#L._digits do if A==0 then break end;L._digits[r]=L._digits[r]+A;A=L._digits[r]>>c;L._digits[r]=L._digits[r]&g end;if A~=0 then L._digits[#L._digits+1]=A end end;q(L)return true end;local function O(L,M)M=math.floor(M)if M>=-h and M<=h then if M<0 then M=-M;L._pos=false end;L._digits[1]=M;return true end;K(L,M)return true end;local function P(m,n)n._pos=m._pos;n._digits={}for r=1,#m._digits do n._digits[r]=m._digits[r]end end;local function Q(m,N)local n;local R={}local S={}local T;local A;local U;if#m._digits==1 and m._digits[1]==0 then return"0"end;if N~=10 and N~=16 then return nil,"base not supported"end;U=m._pos;m=m:copy()while#m._digits>1 or m._digits[1]~=0 do n=a:new()l(n,m)T=0;for r=#m._digits,1,-1 do T=T<<c|m._digits[r]if T>=N then A=T//N;T=T-A*N else A=0 end;n._digits[r]=A end;R[#R+1]=k[T+1]m=n;q(m)end;if not U then R[#R+1]="-"end;for r=#R,1,-1 do S[#S+1]=R[r]end;return table.concat(S)end;b.__index=a;b.__add=function(m,n)local z;local V;local W;m,n=u(m,n)V=m._pos;W=n._pos;m._pos=true;n._pos=true;if V==W then z=y(m,n)z._pos=V elseif m<n then z=B(n,m)z._pos=W else z=B(m,n)z._pos=V end;return z end;b.__sub=function(m,n)local z;local V;local W;m,n=u(m,n)V=m._pos;W=n.