From f36cefc9acf494ed2cfcf920d8771ee37f2d52bb Mon Sep 17 00:00:00 2001 From: dzaima Date: Thu, 8 Aug 2024 23:31:37 +0300 Subject: [PATCH] more Singeli utils --- src/singeli/src/base.singeli | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/singeli/src/base.singeli b/src/singeli/src/base.singeli index a9cc5687..bbe45815 100644 --- a/src/singeli/src/base.singeli +++ b/src/singeli/src/base.singeli @@ -59,16 +59,23 @@ def unreachable{} = emit{void, 'si_unreachable'} def assert{x:(u1)} = { if (not x) emit{void, 'si_unreachable'} } # various checks +def all{{...vs}} = tree_fold{&, vs} +def all{{}} = 1 +def all{G, ...vs if kgen{G}} = if (all{each{{c}=>length{c}==0, vs}}) 1 else G{...each{select{.,0}, vs}} and all{G, ...each{slice{.,1}, vs}} + def oneVal{{h, ...t}} = { each{{c} => assert{same{c,h}}, t} h } def oneVal{{}} = {} def oneType{x} = oneVal{each{type, x}} -def allSame{{h, ...t}} = tree_fold{&, each{is{h,.}, t}} +def allSame{{h, ...t}} = all{is{h,.}, t} def allSame{{_}} = 1 def allSame{{}} = 1 +def try_same_type{_, default} = default +def try_same_type{{h:T, ...t} if all{hastype{.,T}, t}, _} = T + def anyNum = match { {x:T}=>isprim{T}; {x} => knum{x} } def anyInt = match { {x:T}=>isint{T} ; {x} => knum{x} and (x>>0) == x } @@ -257,9 +264,10 @@ def truncBits{n, v if n==64} = cast_i{u64, v} def lb{n if knum{n} and (n>>1<<1) == n and n>0} = lb{n>>1}+1 def lb{1} = 0 -def zlow{n,x} = (x >> n) << n # zero out n least significant bits -def tail{n,x} = x & ((1<> n) << n # zero out n least significant bits +def tail{n,x} = x & tail{n} # get the n least significant bits +def bit {k,x} = x & (1<