Exact-basis Word-RAM binary logarithms and arithmetic helpers #
This file provides structured subprograms over Cslib.WordRAM.Basis.exact for floor and ceiling
base-2 logarithms (log2Loop and clog2Loop), an equal-cost register-maximum gadget (maxCode),
and an .exactBin execution helper (exec_exactBin).
Whereas Cslib.Algorithms.WordRAM.NatLog and Cslib.Algorithms.WordRAM.NatClog are
Code .standard 4 programs with ExecWithin upper bounds, the subprograms here operate in the
.exact basis using right-shift division by 2 (>>> 1) and carry exact closed-form Exec step
counts (4 * Nat.log 2 rem + 2 and 5 * Nat.clog 2 rem + 2) together with explicit final-state
formulas (log2LoopData and clog2LoopData) for embedding via Code.rename.
Main definitions #
Cslib.WordRAM.ExactLog.maxCode: 3-instruction equal-cost branch computing the maximum of two registers.Cslib.WordRAM.ExactLog.log2Body,Cslib.WordRAM.ExactLog.log2Loop,Cslib.WordRAM.ExactLog.log2LoopData: floor-log₂loop overBasis.exact.Cslib.WordRAM.ExactLog.clog2Body,Cslib.WordRAM.ExactLog.clog2Loop,Cslib.WordRAM.ExactLog.clog2LoopData: ceiling-log₂loop overBasis.exact.
Main results #
Cslib.WordRAM.ExactLog.exec_exactBin: single-instruction execution of.exactBin.Cslib.WordRAM.ExactLog.maxCode_exec: 3-instruction execution ofmaxCode.Cslib.WordRAM.ExactLog.log2Loop_exec: exact4 * Nat.log 2 rem + 2-instruction execution oflog2Loop.Cslib.WordRAM.ExactLog.clog2Loop_exec: exact5 * Nat.clog 2 rem + 2-instruction execution ofclog2Loop.
Execute a single .exactBin operation when evalExact returns some v and Fits w v.
3-instruction equal-cost branch setting register dst to max (x.regs a) (x.regs b).
Instances For
One iteration of the floor-log₂ loop on Data 3 (reg 0 = rem, reg 1 = acc,
reg 2 = 1): shifts rem right by 1 (rem / 2) and increments acc.
Instances For
Floor-log₂ loop while 1 < rem do [ rem ← rem >>> 1; acc ← acc + 1 ].
Instances For
log2Loop executes deterministically in 4 * Nat.log 2 rem + 2 instructions, accumulating
acc + Nat.log 2 rem into register 1 and preserving all memory.
Body of clog2Loop on Data 3 (reg 0 = rem, reg 1 = acc, reg 2 = 1): updates
rem ← (rem + 1) >>> 1 (= (rem + 1) / 2) and acc ← acc + 1.
Instances For
Ceiling-log₂ loop while 1 < rem do [ rem ← (rem + 1) >>> 1; acc ← acc + 1 ].
Instances For
clog2Loop executes deterministically in 5 * Nat.clog 2 rem + 2 instructions, accumulating
acc + Nat.clog 2 rem into register 1 and preserving all memory.