Documentation

CslibFrontier.Computability.WordRAM.PackedCount

Counting packed flags on the word RAM #

Three checked instructions count packed zero-or-one fields, using a replication constant, middle-field divisor, and radix supplied in registers. Keeping the packed input below half the physical word width makes the entire multiplication legal. Loading and constructing these constants is separate from the fragment.

Main definitions #

Main results #

References #

@[reducible, inline]

Packed input flags.

@[reducible, inline]

The all-ones radix word.

@[reducible, inline]

The power selecting the middle field.

@[reducible, inline]

Field radix.

@[reducible, inline]

Product workspace and final count.

Count packed flags with three standard word operations.

The full final data, including the overwritten workspace register.

theorem Cslib.WordRAM.PackedCount.exec {w e n : ℕ} (f : ℕ → Bool) (x : Data 5) (hb : n < 2 ^ e) (hw : 2 * e * n ≤ w) (hf : x.regs flags = BitString.packRadix (2 ^ e) n fun (i : ℕ) => (f i).toNat) (ho : x.regs ones = BitString.packRadix (2 ^ e) n fun (x : ℕ) => 1) (hd : x.regs divisor = (2 ^ e) ^ (n - 1)) (hbase : x.regs base = 2 ^ e) :
Exec w code x (result x) 3 ∧ (result x).regs out = ∑ i ∈ Finset.range n, (f i).toNat ∧ (result x).mem = x.mem

A fully charged count, preserving memory and all input registers. The width bound covers the product, and n < 2^e prevents carry between fields in the arithmetic identity.