TorchLean API

FloatLib.Kernels.LimbArray.Core.Runtime

Limb arrays: representation and field access #

A LimbArray stores a natural number as a little-endian array of 32-bit limbs: limb i carries weight 2^(32 i), and reading beyond the stored limbs yields zero, so an array denotes the same number after zero limbs are appended. The value of an array is toNat. Core.Proof relates the accessors to this value, and the arithmetic and shift modules provide their own value theorems.

Using UInt32 limbs lets each limb product, plus an accumulator limb and a carry, fit in a UInt64: (2^32 - 1)^2 + 2 * (2^32 - 1) = 2^64 - 1. Array updates can also reuse storage when the array is uniquely referenced.

This module defines the representation and the non-arithmetic accessors: reading bits and 32-bit windows, masking to a bit count, resizing, comparing, and locating the leading set bit. Each loop is structural recursion over a limb count so its invariant can be stated by induction. The arithmetic kernels live in Arithmetic.Runtime and the shifts in Shift.Runtime.

A natural number stored as little-endian 32-bit limbs.

  • limbs : Array UInt32

    Little-endian limbs, where limb i has weight 2^(32 i).

Instances For
    Instances For
      @[reducible, inline]

      Limb radix 2^32.

      Instances For
        @[inline]

        Number of stored limbs.

        Instances For
          @[inline]

          Limb i, or zero beyond the stored limbs.

          Instances For

            Value of the count limbs starting at index start, in units of 2^(32 start).

            The recursion places the first limb below the remaining segment, which gives the arithmetic loops a common induction invariant.

            Instances For

              Natural number denoted by a limb array.

              Instances For

                Horner evaluation from the most significant of count limbs down to limb zero.

                Instances For

                  Executable evaluation of toNat.

                  Core.Proof.toNat_eq_internal registers this tail-recursive Horner loop as the compiled form of toNat, whose front recursion is convenient for proofs but not tail recursive.

                  Instances For

                    Append the count low base-2^32 digits of n to an accumulator array.

                    Instances For

                      The count-limb array holding n modulo 2^(32 count).

                      Instances For

                        The count-limb array holding zero.

                        Instances For

                          One more than the index of the highest nonzero limb below count, or zero if there is none.

                          Instances For
                            @[inline]

                            Whether the array denotes zero.

                            Instances For

                              Position of the leading set bit, or zero for the zero array.

                              Instances For
                                @[inline]

                                Bit k of the denoted number.

                                Instances For
                                  @[inline]

                                  The 32-bit window of the denoted number starting at bit lo.

                                  Instances For
                                    @[inline]

                                    Mask of the low r bits of a limb, for r < 32.

                                    Instances For

                                      The denoted number modulo 2^k, keeping the stored limb count.

                                      Instances For

                                        The denoted number modulo 2^(32 count), stored in exactly count limbs.

                                        Instances For

                                          Whether any of the limbs below index count is nonzero.

                                          Instances For
                                            @[inline]

                                            Whether any bit below position k is set, scanning only stored limbs.

                                            Instances For
                                              @[inline]

                                              Set the low bit when sticky holds. An empty array stays empty.

                                              Instances For

                                                Compare the limbs below index count, most significant first.

                                                Instances For
                                                  @[inline]

                                                  Compare the denoted numbers.

                                                  Instances For