# HG changeset patch # User Leonard Ritter # Date 1695485703 -7200 # Sat Sep 23 18:15:03 2023 +0200 # Node ID 9c56f3f4d849965d8d0ab9d10d0079f2488f8221 # Parent 7e316c4b7f2199f73eba0e5f6a944556fc679553 * uheap: some dorking around * core: implemented ssign diff --git a/lib/scopes/compiler/uheap/api.sc b/lib/scopes/compiler/uheap/api.sc --- a/lib/scopes/compiler/uheap/api.sc +++ b/lib/scopes/compiler/uheap/api.sc @@ -240,7 +240,7 @@ key : u64 pnodei : PNodeIndex -inline lowerbound (self value keyf) +#inline lowerbound (self value keyf) """"Return the leftmost occurrence of `value` in sorted array `self`, or the number of values smaller than `value`. keyf := static-if (none? keyf) defaultkey @@ -256,7 +256,7 @@ else repeat L m -inline upperbound (self value keyf) +#inline upperbound (self value keyf) """"Return the index of the first value greater than `value` (or the size of the array itself). keyf := static-if (none? keyf) defaultkey @@ -303,6 +303,44 @@ stackaddr : intptr globaladdrs : Array (tuple intptr usize) + fn startoffset (self offset count start) + # return offset x0 + x0 := fold (x0 = 0:usize) for i in (range count) + x := self.indices @ (offset + i) + n := self.nodes @ x + x1 := x0 + n.size + #print "x0" x0 "x1" x1 (x1 > start) + if (x1 > start) + return i x0 + repeat x1 + return count x0 + + fn startoffset_fast (self offset count start) + #slow_offset slow_x0 := startoffset_slow self offset count start + L := offset + R := offset + count + #print L ".." R "|" start + x0 := (offset == 0) 0:usize (self.indexsize @ (offset - 1)) + #for i in (range count) + x1 := ((offset + i) == 0) 0:usize (self.indexsize @ (offset + i - 1)) + print i "->" (x1 - x0) + #print "total" ((self.indexsize @ (R - 1)) - x0) + loop (L R) + if (L >= R) + x1 := (L == 0) 0:usize (self.indexsize @ (L - 1)) + #print "done" (L - offset) (x1 - x0) + #print "should be" slow_offset slow_x0 + #assert ((L - offset) == slow_offset) + #assert ((x1 - x0) == slow_x0) + break (L - offset) (x1 - x0) + m := (L + R) // 2 + x1 := self.indexsize @ m + #print m (x1 - x0) + if ((x1 - x0) <= start) + repeat (m + 1) R + else + repeat L m + inline init-hnode (self) idx := (countof self.hnodes) as i32 'append self.hnodes (HNode) @@ -715,7 +753,8 @@ case join (offset count) if (enterf node start end) db := (uheap_instance) - fold (x0 = 0:usize) for i in (range count) + starti x0 := 'startoffset db (copy offset) (copy count) start + fold (x0) for i in (range starti count) if (x0 >= end) break x0 x := db.indices @ (offset + i) @@ -1059,7 +1098,8 @@ local last : PNodeDataIndex local t0 = 0:usize local t1 = 0:usize - fold (x0 = 0:usize) for i in (range count) + starti x0 := 'startoffset db (copy offset) (copy count) start + fold (x0) for i in (range starti count) x := db.indices @ (offset + i) m := db.nodes @ x x1 := x0 + m.size diff --git a/lib/scopes/core.sc b/lib/scopes/core.sc --- a/lib/scopes/core.sc +++ b/lib/scopes/core.sc @@ -9999,7 +9999,7 @@ inline length (v) sqrt (dot v v) -inline build_matching_real_vector (value c) +inline build_matching_vector (value c) let T = (typeof value) let ST = (storageof T) static-if (ST < vector) @@ -10013,7 +10013,7 @@ inline normalize (v) fdiv v - build_matching_real_vector v (length v) + build_matching_vector v (length v) inline distance (a b) length (a - b) @@ -10033,7 +10033,7 @@ A B C := typeof a, typeof b, typeof c static-assert ((A == B) and (B == C)) a b c := storagecast a, storagecast b, storagecast c - let one = (build_matching_real_vector a 1) + let one = (build_matching_vector a 1) let invx = (fsub one c) bitcast (fadd (fmul a invx) (fmul b c)) A @@ -10041,13 +10041,18 @@ A B := typeof a, typeof b static-assert (A == B) a b := storagecast a, storagecast b - let one = (build_matching_real_vector a 1) - let zero = (build_matching_real_vector b 0) + let one = (build_matching_vector a 1) + let zero = (build_matching_vector b 0) bitcast (? (fcmp>o a b) zero one) A +inline ssign (x) + let zero = (build_matching_vector x 0) + let T = (typeof x) + sub (zext (< zero x) T) (zext (< x zero) T) + inline fsign (x) # (0 < x) - (x < 0) - let zero = (build_matching_real_vector x 0) + let zero = (build_matching_vector x 0) let T = (typeof x) fsub (uitofp (fcmp> 64) as u64, x as u64 inline pack-u128 (h1 h2) diff --git a/testing/test_mutarray.sc b/testing/test_mutarray.sc --- a/testing/test_mutarray.sc +++ b/testing/test_mutarray.sc @@ -442,6 +442,10 @@ t @ i = k ; +# mip binary search: +#while (k <= n) + k = 2 * k + (b[k] < x) + fn tree2mipl (x) b := x & -x level := findlsb b @@ -459,11 +463,11 @@ q := 1 << (findmsb x) ((1 << maxlevel) // q) * ((x - q) * 2 + 1) -MAXLEVEL := 3 -for x in (range 1 16) - print x "=>" (mip2tree x MAXLEVEL) +MAXLEVEL := 6 +for x in (range 100) + print x "=>" ((mip2tree x MAXLEVEL) - 1) -for x in (range 1 16) +#for x in (range 100) level offset := tree2mipl x x1 := mipl2tree level offset x2 := tree2mip x MAXLEVEL