Documentation

Complexity.Computability.WordRAM.Multiword

Multiword arithmetic on the checked word RAM #

An elementary schoolbook multiplication update uses half-word digits. For radix β = 2 ^ (w / 2), four digits a, b, d, c < β satisfy a * b + d + c < β ^ 2 ≤ 2 ^ w. Thus the product and both partial sums fit the physical word, and division and remainder recover an exact digit and carry.

Main definitions #

Main results #

This module supplies the elementary update. WordRAM.Multiword.Mul assembles the full schoolbook routine for arbitrary fixed operand lengths.

References #

Radix for digits whose multiply-add updates fit a single physical word.

Instances For

    Widths below two have a degenerate floor-half radix, so nonzero inputs require a different representation and routine. The schoolbook routines explicitly assume 2 ≤ w.

    theorem Cslib.WordRAM.Multiword.exists_six_word_digits {w v : ℕ} (hw : 2 ≤ w) (hv : v < 2 ^ (6 * w)) :
    ∃ (x : Fin 18 → ℕ), (∀ (i : Fin 18), x i < radix w) ∧ Nat.ofDigits (radix w) (List.ofFn x) = v

    Eighteen half-word digits suffice for a value occupying at most six physical words, including odd word sizes. This is a representation bound, not a multiplication algorithm.

    theorem Cslib.WordRAM.Multiword.limb_update_lt_sq {β a b d c : ℕ} (ha : a < β) (hb : b < β) (hd : d < β) (hc : c < β) :
    a * b + d + c < β ^ 2

    The sharp bound for a schoolbook multiply-add: the maximum is β ^ 2 - 1.

    theorem Cslib.WordRAM.Multiword.limb_update_fits {w a b d c : ℕ} (ha : a < radix w) (hb : b < radix w) (hd : d < radix w) (hc : c < radix w) :
    Fits w (a * b + d + c)

    Every intermediate arithmetic result of the update fits the physical word.

    @[reducible, inline]

    First multiplicand digit (preserved).

    Instances For
      @[reducible, inline]

      Second multiplicand digit (preserved).

      Instances For
        @[reducible, inline]

        Previous destination digit (preserved).

        Instances For
          @[reducible, inline]

          Input carry, replaced by the output carry.

          Instances For
            @[reducible, inline]

            Radix (preserved).

            Instances For
              @[reducible, inline]

              Product and partial-sum workspace.

              Instances For
                @[reducible, inline]

                Output digit.

                Instances For

                  One schoolbook update, using only checked single-word arithmetic.

                  Instances For

                    Compute the radix from the physical word size, using only the two output registers as workspace. The four input digits are preserved.

                    Instances For

                      The result of radix setup.

                      Instances For
                        theorem Cslib.WordRAM.Multiword.LimbUpdate.exec_setup {w : ℕ} (x : Data 7) (hw : 2 ≤ w) :
                        Exec w setup x (setupResult w x) 5

                        Radix setup costs five instructions for every physical word size at least two.

                        Fixed code computing the radix and then executing a schoolbook update.

                        Instances For

                          The mathematical result of the five instructions, including the workspace.

                          Instances For
                            theorem Cslib.WordRAM.Multiword.LimbUpdate.exec_exact {w : ℕ} (x : Data 7) (ha : x.regs left < radix w) (hb : x.regs right < radix w) (hd : x.regs addend < radix w) (hc : x.regs carry < radix w) :
                            Exec w program x (result x) 5

                            Checked execution of the elementary update. All three intermediate results are justified from the digit bounds; no high-product primitive or enlarged physical word is assumed.

                            theorem Cslib.WordRAM.Multiword.LimbUpdate.exec {w : ℕ} (x : Data 7) (hw : 2 ≤ w) (hbase : x.regs base = radix w) (ha : x.regs left < radix w) (hb : x.regs right < radix w) (hd : x.regs addend < radix w) (hc : x.regs carry < radix w) :

                            In five instructions the elementary update returns an exact low digit and carry, both strictly below the radix. Memory and the two operands, addend, and radix are preserved. For globally bounded input data, ExecWithin.bounded also gives globally bounded output data.

                            The update with charged radix setup, in ten instructions at any word size at least two. The initial radix and workspace registers may contain arbitrary words.