Source/Packages

Std.Natural

packages/foundation/standard/src/Std/Natural.alpha

376 lines51 declarations16.0 KiBSHA-256 7e7128c5e781

def · lines 262–277

naturalAffineAccessAtMost

Full file
Every byte of the accesses offset + stride * i .. + width - 1, for first <= i < first + count, is at most maximum (an inclusive bound). Empty ranges have no accesses; nonempty ranges need positive width. Work backwards from the remaining budget instead of constructing the endpoint. Saturating subtraction and division cannot overflow, even when the mathematical index sum or product exceeds the evaluator's Nat range. In particular, stride zero needs no representable index at all. A zero divisor yields zero in naturalDivideUnchecked; that quotient is ignored by the zero-stride branch. No sum or product is evaluated in this check. This bounds resulting addresses, NOT an index evaluated in a narrow register before a predicate. Check that range separately with naturalRangeAtMost. Scalar operands, runtime inputs and a wider address base also have separate obligations. See #173 for the exchange consumer.
262def naturalAffineAccessAtMost =
263  (lambda unrestricted maximum : Nat . (lambda unrestricted offset : Nat .
264    (lambda unrestricted stride : Nat . (lambda unrestricted first : Nat .
265      (lambda unrestricted count : Nat . (lambda unrestricted width : Nat .
266        (let unrestricted remaining = (naturalSaturatingSubtract maximum offset) in
267        (let unrestricted lastByte = (naturalPredecessor width) in
268        (let unrestricted indexLimit =
269          (naturalDivideUnchecked (naturalSaturatingSubtract remaining lastByte) stride) in
270          (naturalOr (naturalIsZero count)
271            (naturalAnd (naturalNonzero width)
272              (naturalAnd (naturalLessOrEqual offset maximum)
273                (naturalAnd (naturalLessOrEqual lastByte remaining)
274                  (naturalOr (naturalIsZero stride)
275                    (naturalAnd (naturalLessOrEqual first indexLimit)
276                      (naturalLessOrEqual (naturalPredecessor count)
277                        (naturalSaturatingSubtract indexLimit first)))))))))))))))))

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.