Documentation

CslibFrontier.DataStructure.BTree.WordRAM.Loops

Shared Word-RAM B-Tree Scratch Registers, Inner Loops, and Node-Header Scanner #

Internal infrastructure shared privately across the B-tree lookup, split, and insertion modules (never re-exported publicly by CslibFrontier.DataStructure.BTree.WordRAM):

@[reducible, inline]

Scratch register holding constant 0.

@[reducible, inline]

Scratch register holding constant 1.

@[reducible, inline]

Scratch register holding nodeStride t = 6 * t.

@[reducible, inline]

Scratch register holding the current node index u.

@[reducible, inline]

Scratch register holding the child node index c.

@[reducible, inline]

Scratch register holding the allocated node count cnt.

@[reducible, inline]

Scratch register holding the scanned key slot slot.

@[reducible, inline]

Scratch register holding the current node's key count m.

@[reducible, inline]

Scratch register holding a source array address.

@[reducible, inline]

Scratch register holding a destination array address or upper bound.

@[reducible, inline]

Scratch register holding a loop limit N.

@[reducible, inline]

Scratch register holding a loop counter j.

@[reducible, inline]

Scratch register holding the loop-continuation flag (1 active, 0 done).

@[reducible, inline]

Temporary arithmetic/address scratch register 1.

@[reducible, inline]

Temporary arithmetic/address scratch register 2.

theorem Cslib.DataStructure.BTree.WordRAM.stride_le_arena {base capNodes t : ℕ} (hcap : 1 ≤ capNodes) :
6 * t ≤ base + arenaWords t capNodes

One node stride 6 * t fits inside any non-empty arena capacity 1 ≤ capNodes.

theorem Cslib.DataStructure.BTree.WordRAM.nodeAddr_add_le_arena {base capNodes t u o : ℕ} (hu : u < capNodes) (ho : o ≤ 6 * t) :
nodeAddr base t u + o ≤ base + arenaWords t capNodes

Every offset o ≤ 6 * t within node u < capNodes lies within base + arenaWords t capNodes.

theorem Cslib.DataStructure.BTree.WordRAM.slotCount_le (base t : ℕ) (mem : ℕ → ℕ) (u k j : ℕ) :
slotCount base t mem u k j ≤ j

slotCount base t mem u k j never exceeds the scan length j.

theorem Cslib.DataStructure.BTree.WordRAM.keyAt_gt_of_slotCount_not_hit {base t u k m : ℕ} {mem : ℕ → ℕ} (hstrict : ∀ ⦃i j : ℕ⦄, i < j → j < m → keyAt base t mem u i < keyAt base t mem u j) (hhit : ¬(slotCount base t mem u k m < m ∧ keyAt base t mem u (slotCount base t mem u k m) = k)) (i : ℕ) :
slotCount base t mem u k m ≤ i → i < m → k < keyAt base t mem u i

When slot = slotCount base t mem u k m is not an exact hit on a strictly sorted node, every key at index i ≥ slot (i < m) is strictly greater than k.

Copy rLim words from rSrc to rDst via ArrayCopy.program, restoring rOne = 1.

theorem Cslib.DataStructure.BTree.WordRAM.copyArrayCode_spec {w srcBase dstBase N : ℕ} {x : WordRAM.Data 20} (hbdd : WordRAM.Data.Bounded w x) (ho : x.regs rOne = 1) (hsrc : x.regs rSrc = srcBase) (hdst : x.regs rDst = dstBase) (hlim : x.regs rLim = N) (hf_src : WordRAM.Fits w (srcBase + N)) (hf_dst : WordRAM.Fits w (dstBase + N)) (hdisj : srcBase + N ≤ dstBase ∨ dstBase + N ≤ srcBase ∨ srcBase = dstBase) :
∃ (y : WordRAM.Data 20), WordRAM.ExecWithin w copyArrayCode x y (7 * N + 5) ∧ WordRAM.Data.Bounded w y ∧ y.mem = WordRAM.ArrayCopy.copyPrefix x.mem srcBase dstBase N ∧ ∀ reg ∉ [rIdx, rTmp1, rTmp2], y.regs reg = x.regs reg

Specification of copyArrayCode on separated or identical ranges of length N.

Backward-shift rLim elements inside the array at rSrc from upper index rDst.

theorem Cslib.DataStructure.BTree.WordRAM.shiftLoop_spec {w arrBase upper count : ℕ} {x : WordRAM.Data 20} (hbdd : WordRAM.Data.Bounded w x) (ho : x.regs rOne = 1) (hsrc : x.regs rSrc = arrBase) (hdst : x.regs rDst = upper) (hlim : x.regs rLim = count) (hcount : count ≤ upper) (hf_arr : WordRAM.Fits w (arrBase + upper)) :
∃ (y : WordRAM.Data 20), WordRAM.ExecWithin w shiftLoop x y (9 * count + 3) ∧ WordRAM.Data.Bounded w y ∧ y.mem = shiftArrRight arrBase upper count x.mem ∧ ∀ reg ∉ [rIdx, rTmp1, rTmp2], y.regs reg = x.regs reg

Specification of shiftLoop, matching shiftArrRight arrBase upper count.

Scan rLim keys starting at rSrc, counting keys strictly less than rKey into rSlot.

theorem Cslib.DataStructure.BTree.WordRAM.slotScanLoop_spec {w base t u k m : ℕ} {x : WordRAM.Data 20} (hbdd : WordRAM.Data.Bounded w x) (hz : x.regs rZero = 0) (ho : x.regs rOne = 1) (hkey : x.regs Reg.rKey = k) (hsrc : x.regs rSrc = nodeAddr base t u + 2) (hlim : x.regs rLim = m) (hf_keys : WordRAM.Fits w (nodeAddr base t u + 2 + m)) :
∃ (y : WordRAM.Data 20), WordRAM.ExecWithin w slotScanLoop x y (8 * m + 4) ∧ WordRAM.Data.Bounded w y ∧ y.mem = x.mem ∧ y.regs rSlot = slotCount base t x.mem u k m ∧ y.regs rZero = 0 ∧ ∀ reg ∉ [rSlot, rIdx, rTmp1, rTmp2, rZero], y.regs reg = x.regs reg

Specification of slotScanLoop, computing slotCount base t x.mem u k m in rSlot.

Load rNumK = numKeys u and rSrc = nodeAddr u + 2 for rCur = u, then scan rSlot.

theorem Cslib.DataStructure.BTree.WordRAM.loadAndScan_spec {w base capNodes t u k : ℕ} {x : WordRAM.Data 20} (ht : 2 ≤ t) (hu_cap : u < capNodes) (hm_le : numKeys base t x.mem u ≤ 2 * t - 1) (hbdd : WordRAM.Data.Bounded w x) (hfit : WordRAM.Fits w (base + arenaWords t capNodes)) (hz : x.regs rZero = 0) (ho : x.regs rOne = 1) (hbase : x.regs Reg.rBase = base) (hdeg : x.regs Reg.rDeg = t) (hstride : x.regs rStride = 6 * t) (hkey : x.regs Reg.rKey = k) (hcur : x.regs rCur = u) :
have m := numKeys base t x.mem u; ∃ (y : WordRAM.Data 20), WordRAM.ExecWithin w loadAndScan x y (8 * m + 12) ∧ WordRAM.Data.Bounded w y ∧ y.mem = x.mem ∧ y.regs rZero = 0 ∧ y.regs rOne = 1 ∧ y.regs Reg.rBase = base ∧ y.regs Reg.rDeg = t ∧ y.regs rStride = 6 * t ∧ y.regs Reg.rKey = k ∧ y.regs rCur = u ∧ y.regs rSrc = nodeAddr base t u + 2 ∧ y.regs rNumK = m ∧ y.regs rSlot = slotCount base t x.mem u k m ∧ ∀ reg ∉ [rSrc, rNumK, rLim, rSlot, rIdx, rTmp1, rTmp2, rZero], y.regs reg = x.regs reg

Specification of loadAndScan on node u < capNodes.