A multiword counter on the word RAM #
Schöning's algorithm runs about (4 / 3) ^ n trials for 3-SAT, a number of 0.415 n bits,
which does not fit a word of O(log n) bits. This module stores such a number in memory as its
little-endian binary digits, one digit per word, and provides the three routines the amplified
algorithm needs (Cslib.Algorithms.WordRAM.Schoening.ampProgram):
Cslib.Algorithms.WordRAM.Schoening.mulAddreplaces the numbervbyv a + cfor wordsaandc, appending digits as needed;Cslib.Algorithms.WordRAM.Schoening.divFloorreplacesvby⌊v / d⌋for a wordd;Cslib.Algorithms.WordRAM.Schoening.decCounterdecrements a nonzero number and reports whether it was nonzero.
Each routine is specified against a function on digit lists (mulAddDigits, divDigits,
decDigits), whose value under Nat.ofDigits 2 is proved separately. A multiply-add or a division
takes time linear in the number of digits. A decrement stops at the lowest nonzero digit, so it
takes decSteps ds iterations, and decSteps_add_count_le bounds this by the number of zero
digits it consumes, which gives the amortized constant cost per decrement used by the amplified
algorithm.
The routines use registers of the register file of Schöning's algorithm
(Cslib.Algorithms.WordRAM.Schoening.Reg). Those beyond the trial's own (bBase, bLen, flag)
keep the counter between trials; the others share registers with the scan, which are dead between
trials.
Main definitions #
Cslib.Algorithms.WordRAM.Schoening.mulAddPass,Cslib.Algorithms.WordRAM.Schoening.mulAddDigits: multiply-add on digit lists.Cslib.Algorithms.WordRAM.Schoening.divDigits: division with remainder on digit lists.Cslib.Algorithms.WordRAM.Schoening.decDigits,Cslib.Algorithms.WordRAM.Schoening.decSteps: decrement on digit lists and its number of loop iterations.Cslib.Algorithms.WordRAM.Schoening.mulAdd,Cslib.Algorithms.WordRAM.Schoening.divFloor,Cslib.Algorithms.WordRAM.Schoening.decCounter: the routines.
Main results #
Cslib.Algorithms.WordRAM.Schoening.ofDigits_mulAddDigits,Cslib.Algorithms.WordRAM.Schoening.ofDigits_divDigits_zero,Cslib.Algorithms.WordRAM.Schoening.ofDigits_decDigits: the values computed.Cslib.Algorithms.WordRAM.Schoening.decSteps_add_count_le: the amortized cost of a decrement.Cslib.Algorithms.WordRAM.Schoening.mulAdd_spec,Cslib.Algorithms.WordRAM.Schoening.divFloor_spec,Cslib.Algorithms.WordRAM.Schoening.decCounter_spec: the routines compute these functions within explicit instruction bounds and keep the memory below the counter.
References #
Digit lists #
The number of digits a decrement reads: up to and including the lowest nonzero digit.
The pass computes v a + cy, split into its low digits and the final carry.
The carry of the pass stays below any bound on the multiplier and the initial carry.
The digits of the pass are binary.
The value of a multiply-add.
The digits of a multiply-add are binary.
A multiply-add does not shorten the digit list.
Dividing a list with one more digit on top: the top digit is divided first.
The value of a division from remainder 0: the floor of the quotient.
A number is zero exactly when all its digits are.
The value of a decrement of a nonzero number with binary digits.
Amortized cost of a decrement. The digits read, minus two, are paid for by the zero digits consumed.
Registers #
Base address of the counter digits.
Number of counter digits.
Whether the amplified algorithm runs another trial.
Digit index of the counter routines; shares register 12 with sgn.
Multiplier of a multiply-add; shares register 8 with lend.
Carry of a multiply-add; shares register 9 with sat.
Divisor of a division; shares register 10 with lit.
Remainder of a division; shares register 11 with var.
Scratch value of a division; shares register 16 with cPtr.
Multiply-add #
One digit of the multiply-add pass.
The multiply-add pass over the digits from idx.
Append the lowest digit of the carry.
Append the digits of the carry.
The multiply-add pass. From the digits ds with idx = 0, the loop replaces them by the
digits of the pass and leaves its final carry in carry.
Appending the carry.
Multiply-add. On a counter with binary digits ds, with mulr = a, carry = cy and both
at most Bd, the routine leaves the digits mulAddDigits a cy ds of v a + cy.
Division #
Divide the digits below idx, from the top.
Division: replace the counter v by ⌊v / dvs⌋.
The division loop. From the digits ds with idx at their end and remainder 0, the
loop replaces them, from the top, by the digits of the quotient.
Division. On a counter with binary digits ds and dvs = d > 0, the routine leaves the
digits (divDigits d 0 ds).1 of ⌊v / d⌋, with the same length.
Decrement #
One digit of the decrement: a zero becomes one and the scan continues; a nonzero digit becomes zero, the routine records that the counter was nonzero, and the scan stops.
Decrement from the digit idx.
Decrement: decrement a nonzero counter and set found = 1; on a zero counter set
found = 0.
The decrement loop.
Decrement. On a counter with digits ds, the routine leaves the digits decDigits ds
within 9 · decSteps ds + 4 instructions and sets found to whether some digit was nonzero.