Overflow-free bit-packed arrays on the w-bit Word RAM #
This file defines a bit-packed array primitive storing N entries of k bits each (0 ≤ k ≤ w)
across packedWords N k w = ⌈N * k / w⌉ + 2 contiguous w-bit machine words starting at address
base.
To avoid intermediate arithmetic overflow (idx * k ≥ 2^w) when idx < N < 2^w approaches 2^w
and k ≤ w is Θ(w) (so that idx * k can reach 2^w), the word and bit offsets of index idx
are computed via the Euclidean decomposition idx = (idx / w) * w + (idx % w):
wordOffset k w idx = (idx / w) * k + ((idx % w) * k) / w = (idx * k) / w,bitOffset k w idx = ((idx % w) * k) % w = (idx * k) % w. Because(idx % w) * k ≤ (w - 1) * w < 2^wholds for every word widthw ≥ 1(pred_mul_self_lt_two_pow), every intermediate value fits strictly inside[0, 2^w).
Each k-bit entry at (wOff, bOff) splits into lowWidth = min k (w - bOff) low bits at offset
bOff of word base + wOff and highWidth = k - lowWidth high bits at offset 0 of word
base + wOff + 1, updated in place via bitwise XOR. Unlike QuotientFilter's single-word
extractPacked/writePackedMem (which packs ⌊w / k⌋ entries per word without crossing word
boundaries and wastes up to half of each word when k > w / 2), allowing entries to straddle
adjacent words achieves the tight ⌈N * k / w⌉ + 2 word bound needed for succinctness. Unlike
Cslib.BitString.readPackedField, which is a pure bit-level specification over a tuple of words
with no Word-RAM machine code or memory layout, this module provides O(1)-step Word-RAM read and
write programs over ℕ → ℕ.
Main definitions #
Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.packedWords,wordOffset,bitOffset,lowWidth,highWidth: space and index-offset functions.Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPacked,writePacked: pure bit-packed read and write operations on word memoryℕ → ℕ.Cslib.DataStructure.Dictionary.WordRAM.BitPackedArray.readPackedCode,writePackedCode:O(1)-step structured Word-RAM programs overBasis.exactimplementingreadPackedandwritePacked.
Main results #
div_add_packedWords_le,packedWords_add_le,div_add_mul_packedWords_le: ceiling-division subadditivity bounds forpackedWords.pred_mul_self_lt_two_pow,mod_mul_lt_two_pow:(w - 1) * w < 2 ^ wand(idx % w) * k < 2 ^ wfor allw ≥ 1andk ≤ w.wordOffset_eq_mul_div,bitOffset_eq_mul_mod: equivalence of the overflow-free formula with(idx * k) / wand(idx * k) % w.readPacked_writePacked_self,readPacked_writePacked_of_ne,readPacked_writePacked_bounded,writePacked_outside,readPacked_zero: exact array laws forreadPackedandwritePacked.readPackedCode_execWithin,writePackedCode_execWithin:O(1)-step execution certificates forreadPackedCodeandwritePackedCode.
Section 1: Overflow-free index arithmetic and word bounds #
Number of w-bit words (N * k + w - 1) / w + 2 reserved for N packed k-bit entries.
The + 2 slack ensures that for every idx < N, both wordOffset k w idx and
wordOffset k w idx + 1 lie strictly inside [0, packedWords N k w).
Adding an auxiliary bit-packed array packedWords N_aux k_aux w to an existing word bound
((B + w - 1) / w) + C₀ increases the bit numerator to B + N_aux * k_aux and the additive word
constant by at most 3.
Ceiling-division subadditivity for packedWords:
packedWords N₁ k₁ w + packedWords N₂ k₂ w ≤ (N₁ * k₁ + N₂ * k₂ + w - 1) / w + 5.
Adding c copies of packedWords N_aux k_aux w increases the bit numerator to
B + (c * N_aux) * k_aux and the additive word constant by at most 3 * c.
Overflow-free word offset (idx / w) * k + ((idx % w) * k) / w, equal to (idx * k) / w.
Overflow-free bit offset ((idx % w) * k) % w < w, equal to (idx * k) % w.
Number of bits min k (w - bitOffset k w idx) of entry idx stored in word
base + wordOffset k w idx.
Number of bits k - lowWidth k w idx of entry idx stored in word
base + wordOffset k w idx + 1.
wordOffset k w idx equals (idx * k) / w for every w > 0.
Fundamental global bit identity: wordOffset k w idx * w + bitOffset k w idx = idx * k.
For every idx < N, wordOffset k w idx + 1 < packedWords N k w.
For every idx < N, wordOffset k w idx < packedWords N k w.
Section 2: Bitwise shift/mask algebra and disjoint-interval non-interference #
Shifting x < 2 ^ len left by a, right by b, and masking to len' bits gives 0 whenever
the intervals [a, a + len) and [b, b + len') are disjoint (a + len ≤ b ∨ b + len' ≤ a).
Extracting len' bits at offset b after XORing a word with δ <<< a (δ < 2 ^ len) leaves
the extracted bits unchanged whenever [a, a + len) and [b, b + len') are disjoint.
Extracting len bits at offset a after replacing the field old with val < 2 ^ len via
word ^^^ ((old ^^^ val) <<< a) returns val.
Recombining the low k₀ bits val &&& (2 ^ k₀ - 1) and high k₁ bits
(val >>> k₀) &&& (2 ^ k₁ - 1) recovers val whenever val < 2 ^ (k₀ + k₁).
Section 3: Pure bit-slice and bit-packed array read/write and laws #
Every value returned by readBitSlice base wOff bOff k w mem fits in k bits (< 2 ^ k).
Updating word addr_w by XORing at [a, a + len) preserves extracting [b, b + len') from
addr_r whenever every overlap addr_w = addr_r has disjoint bit intervals
(a + len ≤ b ∨ b + len' ≤ a).
Non-interference of writeBitSlice and readBitSlice for disjoint global bit intervals.
Every value returned by readPacked base k w mem idx fits in k bits (< 2 ^ k).
Reading from zeroed words returns 0.
A packed write updates the bounded logical array and preserves its zero padding.
Writing at idx < N leaves all memory addresses outside [base, base + packedWords N k w)
unchanged.
readPacked depends only on memory addresses in [base, base + packedWords N k w).
readPacked on [off_r, off_r + packedWords N_r k_r w) is invariant under writePacked on a
disjoint segment [off_w, off_w + packedWords N_w k_w w).
Updating a single plain word at addr < off_r or off_r + packedWords N_r k_r w ≤ addr
leaves readPacked off_r k_r w unchanged on 0 .. N_r - 1.
Reading idx < N from a packed segment whose backing words are all 0 returns 0.
writePacked base k w mem idx val preserves word-boundedness (< 2 ^ w) at every memory
address whenever 0 < w and k ≤ w.
Section 4: Structured Word-RAM programs (readPackedCode and writePackedCode) #
First half of addrSetupCode: given reg 0 = base + offset, reg 1 = idx, reg 8 = w, and
reg 9 = k, computes addr₀ (reg 3), addr₁ (reg 4), and bOff (reg 5) in 10
deterministic instructions while preserving reg 0, 1, 2, 8, 9.
Second half of addrSetupCode: given reg 5 = bOff, reg 8 = w, and reg 9 = k, computes
k₀ (reg 6), mask₀ = 2 ^ k₀ - 1 (reg 7), and mask₁ = 2 ^ k₁ - 1 (reg 8) in at most
15 deterministic instructions without overflowing < 2 ^ w even when k₀ = w.
Parameter-free address and bit-mask setup subprogram on Data 10:
Given reg 0 = base + offset, reg 1 = idx, reg 8 = w, and reg 9 = k (preserving
reg 0, 1, 2), computes:
reg 3 = base + offset + wordOffset k w idx,reg 4 = base + offset + wordOffset k w idx + 1,reg 5 = bitOffset k w idx,reg 6 = lowWidth k w idx,reg 7 = 2 ^ lowWidth k w idx - 1,reg 8 = 2 ^ highWidth k w idx - 1.
Read suffix on Data 10 after addrSetupCode: loads the two words at reg 3 and reg 4,
extracts and combines the low and high bit slices into reg 2.
Parameter-free structured Word-RAM program on Data 10 that reads
readPacked (base + offset) k w mem idx into reg 2, given reg 0 = base + offset, reg 1 = idx,
reg 8 = w, and reg 9 = k, preserving reg 0, reg 1, and mem.
Write suffix on Data 10 after addrSetupCode: updates the low bit slice at reg 3 and the
high bit slice at reg 4 from reg 2 = val, preserving reg 0, 1, 2.
Parameter-free structured Word-RAM program on Data 10 that writes val (reg 2) at idx
(reg 1) of the bit-packed array at base + offset (reg 0 = base + offset, reg 8 = w,
reg 9 = k), preserving reg 0, 1, 2.
offsetSetupCode executes deterministically in 10 instructions.
addrSetupCode executes deterministically in at most 25 instructions.
readPackedCode executes deterministically in at most 34 instructions, placing
readPacked (base + offset) k w x.mem idx into reg 2 while preserving reg 0, reg 1, and
x.mem.
writePackedCode executes deterministically in at most 45 instructions, updating
x.mem to writePacked (base + offset) k w x.mem idx val while preserving reg 0 = base + offset,
reg 1 = idx, and reg 2 = val.